1/*
2 * Copyright (c) 2012 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#ifndef CommonCrypto_corecryptoSymmetricBridge_h
25#define CommonCrypto_corecryptoSymmetricBridge_h
26
27#include <stdbool.h>
28#include <corecrypto/ccmode.h>
29#include <corecrypto/ccmode_impl.h>
30#include <corecrypto/ccaes.h>
31#include <corecrypto/ccdes.h>
32#include <corecrypto/cccast.h>
33#include <corecrypto/ccrc2.h>
34#include <corecrypto/ccblowfish.h>
35#include <corecrypto/ccpad.h>
36
37#define CC_SUPPORTED_CIPHERS 7
38#define CC_DIRECTIONS 2
39
40typedef union {
41    const struct ccmode_ecb *ecb;
42    const struct ccmode_cbc *cbc;
43    const struct ccmode_cfb *cfb;
44    const struct ccmode_cfb8 *cfb8;
45    const struct ccmode_ctr *ctr;
46    const struct ccmode_ofb *ofb;
47    const struct ccmode_xts *xts;
48    const struct ccmode_gcm *gcm;
49} corecryptoMode;
50
51typedef const struct ccmode_ecb* (*ecb_p) (void);
52typedef const struct ccmode_cbc* (*cbc_p) (void);
53typedef const struct ccmode_cfb* (*cfb_p) (void);
54typedef const struct ccmode_cfb8* (*cfb8_p) (void);
55typedef const struct ccmode_ctr* (*ctr_p) (void);
56typedef const struct ccmode_ofb* (*ofb_p) (void);
57typedef const struct ccmode_xts* (*xts_p) (void);
58typedef const struct ccmode_gcm* (*gcm_p) (void);
59
60
61
62typedef struct modes_t {
63    ecb_p   ecb;
64    cbc_p   cbc;
65    cfb_p   cfb;
66    cfb8_p  cfb8;
67    ctr_p   ctr;
68    ofb_p   ofb;
69    xts_p   xts;
70    gcm_p   gcm;
71} modeList;
72
73const modeList ccmodeList[CC_SUPPORTED_CIPHERS][CC_DIRECTIONS];
74
75typedef struct cbc_with_iv_t {
76    uint8_t iv[16];
77    cccbc_ctx cbc;
78} cbc_iv_ctx;
79
80typedef union {
81    void *data;
82    ccecb_ctx *ecb;
83    cbc_iv_ctx *cbc;
84    cccfb_ctx *cfb;
85    cccfb8_ctx *cfb8;
86    ccctr_ctx *ctr;
87    ccofb_ctx *ofb;
88    ccxts_ctx *xts;
89    ccgcm_ctx *gcm;
90} modeCtx;
91
92
93#pragma mark Modes
94
95/** Setup the mode
96 @param cipher		The index of the LTC Cipher - must be registered
97 @param IV		The initial vector
98 @param key		The input symmetric key
99 @param keylen		The length of the input key (octets)
100 @param tweak		The input tweak or salt
101 @param tweaklen	The length of the tweak or salt (if variable)
102 (octets)
103 @param options		Mask for any mode options
104 @param ctx		[out] The destination of the mode context
105 */
106
107typedef void (*ccmode_setup_p)(const corecryptoMode modeObj, const void *iv,
108                            const void *key, size_t keylen, const void *tweak,
109                            size_t tweaklen, int options, modeCtx ctx);
110/** Encrypt a block
111 @param pt		The plaintext
112 @param ct		[out] The ciphertext
113 @param len		the length of data (in == out) octets
114 @param ctx		The mode context
115 @return # bytes encrypted
116 */
117
118typedef void (*ccmode_encrypt_p)(const corecryptoMode modeObj, const void *pt, void *ct, size_t len, modeCtx ctx);
119
120/** Decrypt a block
121 @param ct		The ciphertext
122 @param pt		[out] The plaintext
123 @param len		the length of data (in == out) octets
124 @param ctx		The mode context
125 @return # bytes encrypted
126 */
127typedef void (*ccmode_decrypt_p)(const corecryptoMode modeObj, const void *ct, void *pt, size_t len, modeCtx ctx);
128
129/** Encrypt a block with a tweak (XTS mode currently)
130 @param pt		The plaintext
131 @param ct		[out] The ciphertext
132 @param len		the length of data (in == out) octets
133 @param tweak		The 128--bit encryption tweak (e.g. sector
134 number)
135 @param ctx		The mode context
136 @return # bytes encrypted
137 */
138typedef void (*ccmode_encrypt_tweaked_p)(const corecryptoMode modeObj, const void *pt, size_t len,
139                                      void *ct, const void *tweak, modeCtx ctx);
140/** Decrypt a block with a tweak (XTS mode currently)
141 @param ct		The ciphertext
142 @param pt		[out] The plaintext
143 @param len		the length of data (in == out) octets
144 @param ctx		The mode context
145 @return # bytes encrypted
146 */
147typedef void (*ccmode_decrypt_tweaked_p)(const corecryptoMode modeObj, const void *ct, size_t len,
148                                      void *pt, const void *tweak, modeCtx ctx);
149/** Terminate the mode
150 @param ctx		[out] The mode context
151 */
152typedef int (*ccmode_done_p)(const corecryptoMode modeObj, modeCtx ctx);
153/** Set an Initial Vector
154 @param IV		The initial vector
155 @param len		The length of the initial vector
156 @param ctx		The mode context
157 */
158typedef int (*ccmode_setiv_p)(const corecryptoMode modeObj, const void *iv, uint32_t len, modeCtx ctx);
159/** Get an Initial Vector
160 @param IV		[out] The initial vector
161 @param len		The length of the initial vector
162 @param ctx		The mode context
163 */
164typedef int (*ccmode_getiv_p)(const corecryptoMode modeObj, void *iv, uint32_t *len, modeCtx ctx);
165
166/** Get the mode context size
167 @param modeObj a pointer to the mode object.
168 @return the size of the context
169 */
170typedef size_t (*ccmode_get_ctx_size)(const corecryptoMode modeObj);
171
172/** Get the mode block size
173 @param modeObj a pointer to the mode object.
174 @return the size of the block
175 */
176typedef size_t (*ccmode_get_block_size)(const corecryptoMode modeObj);
177
178typedef struct cc2CCModeDescriptor_t {
179//    ccBufStrat              bufStrat;
180    ccmode_get_ctx_size     mode_get_ctx_size;
181    ccmode_get_block_size   mode_get_block_size;
182	ccmode_setup_p          mode_setup;
183	ccmode_encrypt_p        mode_encrypt;
184	ccmode_decrypt_p        mode_decrypt;
185	ccmode_encrypt_tweaked_p mode_encrypt_tweaked;
186	ccmode_decrypt_tweaked_p mode_decrypt_tweaked;
187	ccmode_done_p           mode_done;
188	ccmode_setiv_p          mode_setiv;
189	ccmode_getiv_p          mode_getiv;
190} cc2CCModeDescriptor, *cc2CCModeDescriptorPtr;
191
192
193extern const cc2CCModeDescriptor ccecb_mode;
194extern const cc2CCModeDescriptor cccbc_mode;
195extern const cc2CCModeDescriptor cccfb_mode;
196extern const cc2CCModeDescriptor cccfb8_mode;
197extern const cc2CCModeDescriptor ccctr_mode;
198extern const cc2CCModeDescriptor ccofb_mode;
199extern const cc2CCModeDescriptor ccxts_mode;
200extern const cc2CCModeDescriptor ccgcm_mode;
201
202
203// Buffer and Padding Handling
204
205/*
206 * Fill out the padding for a buffer.  The blocksize and starting points are
207 * used to determine how much needs to be padded.  If startpoint is 0
208 * then a full new buffer is added.  Blocksize cannot be greater than 256.
209 */
210
211typedef int (*cc_encrypt_pad_p)(modeCtx ctx, const cc2CCModeDescriptor *modeptr, const corecryptoMode modeObj, void *buff, size_t startpoint, void *cipherText, size_t *moved);
212typedef int (*cc_decrypt_pad_p)(modeCtx ctx, const cc2CCModeDescriptor *modeptr, const corecryptoMode modeObj, void *buff, size_t startpoint, void *plainText, size_t *moved);
213
214/*
215 * Maximum space needed for padding.
216 */
217
218typedef size_t (*ccpadlen_p) (int encrypt, const cc2CCModeDescriptor *modeptr, const corecryptoMode modeObj, size_t inputLength, bool final);
219
220/*
221 * How many bytes to reserve to enable padding - this is pre-encrypt/decrypt bytes.
222 */
223
224typedef size_t (*ccreserve_p) (int encrypt, const cc2CCModeDescriptor *modeptr, const corecryptoMode modeObj);
225
226typedef struct cc2CCPaddingDescriptor_t {
227    cc_encrypt_pad_p    encrypt_pad;
228    cc_decrypt_pad_p    decrypt_pad;
229    ccpadlen_p          padlen;
230    ccreserve_p         padreserve;
231} cc2CCPaddingDescriptor, *cc2CCPaddingDescriptorPtr;
232
233extern const cc2CCPaddingDescriptor ccnopad_pad;
234extern const cc2CCPaddingDescriptor cccts1_pad;
235extern const cc2CCPaddingDescriptor cccts2_pad;
236extern const cc2CCPaddingDescriptor cccts3_pad;
237extern const cc2CCPaddingDescriptor ccpkcs7_pad;
238extern const cc2CCPaddingDescriptor ccpkcs7_ecb_pad;
239
240#endif
241