1/*
2 * Copyright (c) 2006-2010 Apple, Inc. All Rights Reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24/*
25 * CommonCryptorPriv.h - interface between CommonCryptor and operation- and
26 *           algorithm-specific service providers.
27 */
28
29#ifndef _CC_COMMON_CRYPTOR_PRIV_
30#define _CC_COMMON_CRYPTOR_PRIV_
31
32#include "CommonCryptor.h"
33#include "CommonCryptorSPI.h"
34#include <dispatch/dispatch.h>
35#include "corecryptoSymmetricBridge.h"
36
37#ifdef DEBUG
38#include <stdio.h>
39#endif
40
41#ifdef __cplusplus
42extern "C" {
43#endif
44
45    /* Byte-Size Constants */
46#define CCMAXBUFFERSIZE 128             /* RC2/RC5 Max blocksize */
47#define DEFAULT_CRYPTOR_MALLOC 4096
48#define CC_STREAMKEYSCHED  2048
49#define CC_MODEKEYSCHED  2048
50#define CC_MAXBLOCKSIZE  128
51
52typedef struct cipherMode_t {
53    dispatch_once_t init;
54    const struct ccmode_ecb* ecb;
55    const struct ccmode_cbc* cbc;
56    const struct ccmode_cfb* cfb;
57    const struct ccmode_cfb8* cfb8;
58    const struct ccmode_ctr* ctr;
59    const struct ccmode_ofb* ofb;
60    const struct ccmode_xts* xts;
61    const struct ccmode_gcm* gcm;
62} cipherMode;
63
64#define ACTIVE 1
65#define RELEASED 0xDEADBEEF
66
67typedef struct _CCCryptor {
68    struct _CCCryptor *compat;
69#ifdef DEBUG
70    uint64_t        active;
71    uint64_t        cryptorID;
72#endif
73    uint8_t         buffptr[32];
74    size_t          bufferPos;
75    size_t          bytesProcessed;
76    size_t          cipherBlocksize;
77
78    CCAlgorithm     cipher;
79    CCMode          mode;
80    CCOperation     op;        /* kCCEncrypt, kCCDecrypt, or kCCBoth */
81
82    corecryptoMode  symMode[CC_DIRECTIONS];
83    const cc2CCModeDescriptor *modeDesc;
84    modeCtx         ctx[CC_DIRECTIONS];
85    const cc2CCPaddingDescriptor *padptr;
86
87} CCCryptor;
88
89static inline CCCryptor *
90getRealCryptor(CCCryptorRef p, int checkactive) {
91    if(!p) return NULL;
92    if(p->compat) p = p->compat;
93#ifdef DEBUG
94    if(checkactive && p->active != ACTIVE) printf("Using Finalized Cryptor %16llx\n", p->cryptorID);
95#endif
96    return p;
97}
98
99
100
101
102#define CCCRYPTOR_SIZE  sizeof(struct _CCCryptor)
103#define kCCContextSizeGENERIC (sizeof(struct _CCCryptor))
104#define CC_COMPAT_SIZE (sizeof(void *)*2)
105
106
107const corecryptoMode getCipherMode(CCAlgorithm cipher, CCMode mode, CCOperation direction);
108
109#ifdef __cplusplus
110}
111#endif
112
113#endif  /* _CC_COMMON_CRYPTOR_PRIV_ */
114