]>
git.saurik.com Git - apple/xnu.git/blob - osfmk/corecrypto/ccmode/src/ccmode_internal.h
5 * Created on 12/12/2010
7 * Copyright (c) 2010,2011,2012,2014,2015 Apple Inc. All rights reserved.
10 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
12 * This file contains Original Code and/or Modifications of Original Code
13 * as defined in and that are subject to the Apple Public Source License
14 * Version 2.0 (the 'License'). You may not use this file except in
15 * compliance with the License. The rights granted to you under the License
16 * may not be used to create, or enable the creation or redistribution of,
17 * unlawful or unlicensed copies of an Apple operating system, or to
18 * circumvent, violate, or enable the circumvention or violation of, any
19 * terms of an Apple operating system software license agreement.
21 * Please obtain a copy of the License at
22 * http://www.opensource.apple.com/apsl/ and read it before using this file.
24 * The Original Code and all software distributed under the License are
25 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
26 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
27 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
28 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
29 * Please see the License for the specific language governing rights and
30 * limitations under the License.
32 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
35 #ifndef _CORECRYPTO_CCMODE_INTERNAL_H_
36 #define _CORECRYPTO_CCMODE_INTERNAL_H_
38 #include <corecrypto/ccmode.h>
39 #include <corecrypto/ccmode_factory.h>
40 #include <corecrypto/cc_priv.h>
41 #include <corecrypto/cc_macros.h>
43 #define CCMODE_INVALID_INPUT -1
44 #define CCMODE_INVALID_CALL_SEQUENCE -2
45 #define CCMODE_INTEGRITY_FAILURE -3
46 #define CCMODE_NOT_SUPPORTED -4
47 #define CCMODE_INTERNAL_ERROR -5
49 // VNG speed up for GCM's AES encrypton and finite fileld multiplication
51 ((CCAES_INTEL_ASM && defined(__x86_64__)) || (CCAES_ARM_ASM && defined(__ARM_NEON__)))
52 #define CCMODE_GCM_VNG_SPEEDUP 1
54 #define CCMODE_GCM_VNG_SPEEDUP 0
58 #define CCMODE_GCM_USE_GF_LOOKUP_TABLES 1
60 /* Helper function used. TODO: Probably not specific to xts, since
62 void ccmode_xts_mult_alpha(cc_unit
*tweak
);
64 /* Macros for accessing a CCMODE_CBC_KEY.
66 const struct ccmode_ecb *ecb
67 ccn_unit ecb_key[ecb->n]
69 #define _CCMODE_CBC_KEY(K) ((struct _ccmode_cbc_key *)(K))
70 #define _CCMODE_CBC_KEY_CONST(K) ((const struct _ccmode_cbc_key *)(K))
71 #define CCMODE_CBC_KEY_ECB(K) (_CCMODE_CBC_KEY(K)->ecb)
72 #define CCMODE_CBC_KEY_ECB_KEY(K) ((ccecb_ctx *)&_CCMODE_CBC_KEY(K)->u[0])
75 const struct ccmode_ecb
* ccmode_cbc_key_ecb(const cccbc_ctx
*K
) {
76 return ((const struct _ccmode_cbc_key
*)K
)->ecb
;
80 const ccecb_ctx
* ccmode_cbc_key_ecb_key(const cccbc_ctx
*K
) {
81 return (const ccecb_ctx
*)&((const struct _ccmode_cbc_key
*)K
)->u
[0];
84 /* Macros for accessing a CCMODE_CFB_KEY.
86 const struct ccmode_ecb *ecb
88 ccn_unit pad[ecb->block_size / CCN_UNIT_SIZE];
89 ccn_unit iv[ecb->block_size / CCN_UNIT_SIZE];
90 ccn_unit ecb_key[ecb->n]
92 #define _CCMODE_CFB_KEY(K) ((struct _ccmode_cfb_key *)(K))
93 #define CCMODE_CFB_KEY_ECB(K) (_CCMODE_CFB_KEY(K)->ecb)
94 #define CCMODE_CFB_KEY_PAD_LEN(K) (_CCMODE_CFB_KEY(K)->pad_len)
95 #define CCMODE_CFB_KEY_PAD(K) (&_CCMODE_CFB_KEY(K)->u[0])
96 #define CCMODE_CFB_KEY_IV(K) (&_CCMODE_CFB_KEY(K)->u[ccn_nof_size(CCMODE_CFB_KEY_ECB(K)->block_size)])
97 #define CCMODE_CFB_KEY_ECB_KEY(K) ((ccecb_ctx *)&_CCMODE_CFB_KEY(K)->u[2 * ccn_nof_size(CCMODE_CFB_KEY_ECB(K)->block_size)])
99 /* Macros for accessing a CCMODE_CFB8_KEY.
101 const struct ccmode_ecb *ecb
102 ccn_unit pad[ecb->block_size / CCN_UNIT_SIZE];
103 ccn_unit iv[ecb->block_size / CCN_UNIT_SIZE];
104 ccn_unit ecb_key[ecb->n]
106 #define _CCMODE_CFB8_KEY(K) ((struct _ccmode_cfb8_key *)(K))
107 #define CCMODE_CFB8_KEY_ECB(K) (_CCMODE_CFB8_KEY(K)->ecb)
108 #define CCMODE_CFB8_KEY_PAD(K) (&_CCMODE_CFB8_KEY(K)->u[0])
109 #define CCMODE_CFB8_KEY_IV(K) (&_CCMODE_CFB8_KEY(K)->u[ccn_nof_size(CCMODE_CFB8_KEY_ECB(K)->block_size)])
110 #define CCMODE_CFB8_KEY_ECB_KEY(K) ((ccecb_ctx *)&_CCMODE_CFB8_KEY(K)->u[2 * ccn_nof_size(CCMODE_CFB8_KEY_ECB(K)->block_size)])
113 /* Macros for accessing a CCMODE_CTR_KEY.
115 const struct ccmode_ecb *ecb
117 ccn_unit pad[ecb->block_size / CCN_UNIT_SIZE];
118 ccn_unit ctr[ecb->block_size / CCN_UNIT_SIZE];
119 ccn_unit ecb_key[ecb->n]
121 #define _CCMODE_CTR_KEY(K) ((struct _ccmode_ctr_key *)(K))
122 #define CCMODE_CTR_KEY_ECB(K) (_CCMODE_CTR_KEY(K)->ecb)
123 #define CCMODE_CTR_KEY_PAD_OFFSET(K) (_CCMODE_CTR_KEY(K)->pad_offset)
124 #define CCMODE_CTR_KEY_PAD(K) (&_CCMODE_CTR_KEY(K)->u[0])
125 #define CCMODE_CTR_KEY_CTR(K) (&_CCMODE_CTR_KEY(K)->u[ccn_nof_size(CCMODE_CTR_KEY_ECB(K)->block_size)])
126 #define CCMODE_CTR_KEY_ECB_KEY(K) ((ccecb_ctx *)&_CCMODE_CTR_KEY(K)->u[2 * ccn_nof_size(CCMODE_CTR_KEY_ECB(K)->block_size)])
128 CC_INLINE
int ccctr_setctr(const struct ccmode_ctr
*mode
, ccctr_ctx
*ctx
, const void *ctr
)
130 return mode
->setctr(mode
, ctx
, ctr
);
133 /* Macros for accessing a CCMODE_OFB_KEY.
135 const struct ccmode_ecb *ecb
137 ccn_unit iv[ecb->block_size / CCN_UNIT_SIZE];
138 ccn_unit ecb_key[ecb->n]
140 #define _CCMODE_OFB_KEY(K) ((struct _ccmode_ofb_key *)(K))
141 #define CCMODE_OFB_KEY_ECB(K) (_CCMODE_OFB_KEY(K)->ecb)
142 #define CCMODE_OFB_KEY_PAD_LEN(K) (_CCMODE_OFB_KEY(K)->pad_len)
143 #define CCMODE_OFB_KEY_IV(K) (&_CCMODE_OFB_KEY(K)->u[0])
144 #define CCMODE_OFB_KEY_ECB_KEY(K) ((ccecb_ctx *)&_CCMODE_OFB_KEY(K)->u[ccn_nof_size(CCMODE_OFB_KEY_ECB(K)->block_size)])
147 /* Macros for accessing a CCMODE_XTS_KEY.
149 const struct ccmode_ecb *ecb
150 const struct ccmode_ecb *ecb_encrypt
151 ccn_unit data_key[ecb->size]
152 ccn_unit tweak_key[ecb_encrypt->size]
154 #define _CCMODE_XTS_KEY(K) ((struct _ccmode_xts_key *)(K))
155 #define CCMODE_XTS_KEY_ECB(K) (_CCMODE_XTS_KEY(K)->ecb)
156 #define CCMODE_XTS_KEY_ECB_ENCRYPT(K) (_CCMODE_XTS_KEY(K)->ecb_encrypt)
157 #define CCMODE_XTS_KEY_DATA_KEY(K) ((ccecb_ctx *)&_CCMODE_XTS_KEY(K)->u[0])
158 #define CCMODE_XTS_KEY_TWEAK_KEY(K) ((ccecb_ctx *)&_CCMODE_XTS_KEY(K)->u[ccn_nof_size(CCMODE_XTS_KEY_ECB(K)->size)])
161 const struct ccmode_ecb
* ccmode_xts_key_ecb(const ccxts_ctx
*K
) {
162 return ((const struct _ccmode_xts_key
*)K
)->ecb
;
166 const struct ccmode_ecb
* ccmode_xts_key_ecb_encrypt(const ccxts_ctx
*K
) {
167 return ((const struct _ccmode_xts_key
*)K
)->ecb_encrypt
;
171 const ccecb_ctx
* ccmode_xts_key_data_key(const ccxts_ctx
*K
) {
172 return (const ccecb_ctx
*)&((const struct _ccmode_xts_key
*)K
)->u
[0];
176 const ccecb_ctx
* ccmode_xts_key_tweak_key(const ccxts_ctx
*K
) {
177 return (const ccecb_ctx
*)&((const struct _ccmode_xts_key
*)K
)->u
[ccn_nof_size(ccmode_xts_key_ecb(K
)->size
)];
180 /* Macros for accessing a CCMODE_XTS_TWEAK.
182 size_t blocks_processed;
185 #define _CCMODE_XTS_TWEAK(T) ((struct _ccmode_xts_tweak *)(T))
186 #define CCMODE_XTS_TWEAK_BLOCK_PROCESSED(T)(_CCMODE_XTS_TWEAK(T)->blocks_processed)
187 #define CCMODE_XTS_TWEAK_VALUE(T) (_CCMODE_XTS_TWEAK(T)->u)
190 /* Macros for accessing a CCMODE_GCM_KEY.
191 Common to the generic (factory) and the VNG implementation
194 #define _CCMODE_GCM_KEY(K) ((struct _ccmode_gcm_key *)(K))
195 #define CCMODE_GCM_KEY_H(K) (_CCMODE_GCM_KEY(K)->H)
196 #define CCMODE_GCM_KEY_X(K) (_CCMODE_GCM_KEY(K)->X)
197 #define CCMODE_GCM_KEY_Y(K) (_CCMODE_GCM_KEY(K)->Y)
198 #define CCMODE_GCM_KEY_Y_0(K) (_CCMODE_GCM_KEY(K)->Y_0)
199 #define CCMODE_GCM_KEY_PAD_LEN(K) (_CCMODE_GCM_KEY(K)->buf_nbytes)
200 #define CCMODE_GCM_KEY_PAD(K) (_CCMODE_GCM_KEY(K)->buf)
202 #define _CCMODE_GCM_ECB_MODE(K) ((struct _ccmode_gcm_key *)(K))
203 #define CCMODE_GCM_KEY_ECB(K) (_CCMODE_GCM_ECB_MODE(K)->ecb)
204 #define CCMODE_GCM_KEY_ECB_KEY(K) ((ccecb_ctx *)_CCMODE_GCM_ECB_MODE(K)->ecb_key) // set in init function
206 #define CCMODE_GCM_STATE_IV 1
207 #define CCMODE_GCM_STATE_AAD 2
208 #define CCMODE_GCM_STATE_TEXT 3
209 #define CCMODE_GCM_STATE_FINAL 4
211 #define CCMODE_STATE_INIT 2 //first call to init
212 #define CCMODE_STATE_IV_START 3 //first call to set_iv
214 // rdar://problem/23523093
215 //this allows users to bypass set_iv().
216 //this is a temporary setting mainly to allow Security framework to adapt
217 //ccgcm_set_iv_legacy() and check the tack on decyption without
218 //need to change the Security twice
219 //#define CCMODE_STATE_IV_CONT 2 //subsequent calls to set_iv
220 #define CCMODE_STATE_IV_CONT CCMODE_STATE_IV_START
222 #define CCMODE_STATE_AAD 4
223 #define CCMODE_STATE_TEXT 5
225 #define CCMODE_CCM_STATE_IV 1
227 void ccmode_gcm_gf_mult(const unsigned char *a
, const unsigned char *b
,
229 void ccmode_gcm_mult_h(ccgcm_ctx
*key
, unsigned char *I
);
231 /* Macros for accessing a CCMODE_CCM_KEY. */
232 #define _CCMODE_CCM_KEY(K) ((struct _ccmode_ccm_key *)(K))
233 #define CCMODE_CCM_KEY_ECB(K) (_CCMODE_CCM_KEY(K)->ecb)
234 #define CCMODE_CCM_KEY_ECB_KEY(K) ((ccecb_ctx *)&_CCMODE_CCM_KEY(K)->u[0])
236 #define _CCMODE_CCM_NONCE(N) ((struct _ccmode_ccm_nonce *)(N))
237 #define CCMODE_CCM_KEY_MAC(N) (_CCMODE_CCM_NONCE(N)->MAC)
238 #define CCMODE_CCM_KEY_A_I(N) (_CCMODE_CCM_NONCE(N)->A_i)
239 #define CCMODE_CCM_KEY_B_I(N) (_CCMODE_CCM_NONCE(N)->B_i)
240 #define CCMODE_CCM_KEY_PAD_LEN(N) (_CCMODE_CCM_NONCE(N)->buflen)
241 #define CCMODE_CCM_KEY_PAD(N) (_CCMODE_CCM_NONCE(N)->buf)
242 #define CCMODE_CCM_KEY_MAC_LEN(N) (_CCMODE_CCM_NONCE(N)->mac_size)
243 #define CCMODE_CCM_KEY_NONCE_LEN(N) (_CCMODE_CCM_NONCE(N)->nonce_size)
244 #define CCMODE_CCM_KEY_AUTH_LEN(N) (_CCMODE_CCM_NONCE(N)->b_i_len)
246 /* Macros for accessing a CCMODE_OMAC_KEY.
248 const struct ccmode_ecb *ecb
250 ccn_unit ecb_key1[ecb->n]
251 ccn_unit ecb_key2[ecb->n]
253 #define _CCMODE_OMAC_KEY(K) ((struct _ccmode_omac_key *)(K))
254 #define CCMODE_OMAC_KEY_ECB(K) (_CCMODE_OMAC_KEY(K)->ecb)
255 #define CCMODE_OMAC_KEY_TWEAK_LEN(K) (_CCMODE_OMAC_KEY(K)->tweak_len)
256 #define CCMODE_OMAC_KEY_ECB_KEY(K) ((ccecb_ctx *)&_CCMODE_OMAC_KEY(K)->u[0])
258 CC_INLINE
void inc_uint(uint8_t *buf
, size_t nbytes
)
261 for (i
= 0; i
< nbytes
; i
+= 1) {
262 if (++buf
[nbytes
-1-i
] & 255) { break; }
266 CC_INLINE
void ccmode_gcm_update_pad(ccgcm_ctx
*key
)
268 inc_uint(CCMODE_GCM_KEY_Y(key
) + 12, 4);
269 CCMODE_GCM_KEY_ECB(key
)->ecb(CCMODE_GCM_KEY_ECB_KEY(key
), 1,
270 CCMODE_GCM_KEY_Y(key
),
271 CCMODE_GCM_KEY_PAD(key
));
274 CC_INLINE
void ccmode_gcm_aad_finalize(ccgcm_ctx
*key
)
276 if (_CCMODE_GCM_KEY(key
)->state
== CCMODE_GCM_STATE_AAD
) {
277 if (_CCMODE_GCM_KEY(key
)->aad_nbytes
% CCGCM_BLOCK_NBYTES
> 0) {
278 ccmode_gcm_mult_h(key
, CCMODE_GCM_KEY_X(key
));
280 _CCMODE_GCM_KEY(key
)->state
= CCMODE_GCM_STATE_TEXT
;
284 CC_INLINE
void xor_128bits(unsigned char *r
, const unsigned char *a
, const unsigned char *b
)
286 cc_unit
*r1
= (cc_unit
*)r
;
287 const cc_unit
*a1
= (const cc_unit
*)a
;
288 const cc_unit
*b1
= (const cc_unit
*)b
;
290 for (int i
=0; i
<128/(CCN_UNIT_SIZE
*8); i
++) {
291 r1
[i
] = a1
[i
] ^ b1
[i
];
297 #endif /* _CORECRYPTO_CCMODE_INTERNAL_H_ */