]>
git.saurik.com Git - apple/xnu.git/blob - EXTERNAL_HEADERS/corecrypto/ccmode_impl.h
1 /* Copyright (c) (2010,2011,2012,2015,2016,2017,2018,2019) Apple Inc. All rights reserved.
3 * corecrypto is licensed under Apple Inc.’s Internal Use License Agreement (which
4 * is contained in the License.txt file distributed with corecrypto) and only to
5 * people who accept that license. IMPORTANT: Any license rights granted to you by
6 * Apple Inc. (if any) are limited to internal use within your organization only on
7 * devices and computers you own or control, for the sole purpose of verifying the
8 * security characteristics and correct functioning of the Apple Software. You may
9 * not, directly or indirectly, redistribute the Apple Software or any portions thereof.
12 #ifndef _CORECRYPTO_CCMODE_IMPL_H_
13 #define _CORECRYPTO_CCMODE_IMPL_H_
15 #include <corecrypto/cc.h>
18 cc_aligned_struct(16) ccecb_ctx
;
20 /* Actual symmetric algorithm implementation should provide you one of these. */
22 size_t size
; /* first argument to ccecb_ctx_decl(). */
24 int (*CC_SPTR(ccmode_ecb
, init
))(const struct ccmode_ecb
*ecb
, ccecb_ctx
*ctx
, size_t key_nbytes
, const void *key
);
25 int (*CC_SPTR(ccmode_ecb
, ecb
))(const ccecb_ctx
*ctx
, size_t nblocks
, const void *in
, void *out
);
26 void (*CC_SPTR(ccmode_ecb
, roundkey
))(const ccecb_ctx
*ctx
, unsigned r
, void *key
);
30 * @brief corecrypto symmetrical encryption and decryption modes
32 * corecrypto supports 6 stateless en(de)cryption modes and 2 stateful authenticated en(de)cryption modes
33 * stateless modes CBC, CFB, CFB8, CTR, OFB, XTS: They provide 3 interface functions that do not return errors codes
35 * 2- ccmod_xxx_decrypt()
36 * 3- ccmod_xxx_encrypt()
38 * stateful modes CCM and GCM: They provide 7 interface functions that return error codes if a function is called out of state
40 * 2- ccmod_xxx_setiv()
42 * 4- ccmod_xxx_decrypt()
43 * 5- ccmod_xxx_encrypt()
44 * 6- ccmod_xxx_finalize()
45 * 7- ccmod_xxx_reset()
47 * the correct call sequences are:
49 * calls to 1, 2 and 6 arerequired
50 * 2 and 3 can be called as mant times as needed
51 * calls to 3, 4, 5 can be skipped
59 // 1- CBC mode, stateless
60 cc_aligned_struct(16) cccbc_ctx
;
61 cc_aligned_struct(16) cccbc_iv
;
64 size_t size
; /* first argument to cccbc_ctx_decl(). */
66 int (*CC_SPTR(ccmode_cbc
, init
))(const struct ccmode_cbc
*cbc
, cccbc_ctx
*ctx
, size_t key_len
, const void *key
);
67 /* cbc encrypt or decrypt nblocks from in to out, iv will be used and updated. */
68 int (*CC_SPTR(ccmode_cbc
, cbc
))(const cccbc_ctx
*ctx
, cccbc_iv
*iv
, size_t nblocks
, const void *in
, void *out
);
72 // 2- CFB mode, stateless
73 cc_aligned_struct(16) cccfb_ctx
;
76 size_t size
; /* first argument to cccfb_ctx_decl(). */
78 int (*CC_SPTR(ccmode_cfb
,
79 init
))(const struct ccmode_cfb
*cfb
, cccfb_ctx
*ctx
, size_t key_len
, const void *key
, const void *iv
);
80 int (*CC_SPTR(ccmode_cfb
, cfb
))(cccfb_ctx
*ctx
, size_t nbytes
, const void *in
, void *out
);
84 // 3- CFB8 mode, stateless
85 cc_aligned_struct(16) cccfb8_ctx
;
88 size_t size
; /* first argument to cccfb8_ctx_decl(). */
90 int (*CC_SPTR(ccmode_cfb8
,
91 init
))(const struct ccmode_cfb8
*cfb8
, cccfb8_ctx
*ctx
, size_t key_len
, const void *key
, const void *iv
);
92 int (*CC_SPTR(ccmode_cfb8
, cfb8
))(cccfb8_ctx
*ctx
, size_t nbytes
, const void *in
, void *out
);
96 // 4- CTR mode, stateless
97 cc_aligned_struct(16) ccctr_ctx
;
100 size_t size
; /* first argument to ccctr_ctx_decl(). */
101 size_t block_size
; /* for historical reasons, this is set to 1 */
102 size_t ecb_block_size
; /* the actual block size of the underlying cipher */
103 int (*CC_SPTR(ccmode_ctr
,
104 init
))(const struct ccmode_ctr
*mode
, ccctr_ctx
*ctx
, size_t key_len
, const void *key
, const void *iv
);
105 int (*CC_SPTR(ccmode_ctr
, setctr
))(const struct ccmode_ctr
*mode
, ccctr_ctx
*ctx
, const void *ctr
);
106 int (*CC_SPTR(ccmode_ctr
, ctr
))(ccctr_ctx
*ctx
, size_t nbytes
, const void *in
, void *out
);
110 // 5- OFB mode, stateless
111 cc_aligned_struct(16) ccofb_ctx
;
114 size_t size
; /* first argument to ccofb_ctx_decl(). */
116 int (*CC_SPTR(ccmode_ofb
,
117 init
))(const struct ccmode_ofb
*ofb
, ccofb_ctx
*ctx
, size_t key_len
, const void *key
, const void *iv
);
118 int (*CC_SPTR(ccmode_ofb
, ofb
))(ccofb_ctx
*ctx
, size_t nbytes
, const void *in
, void *out
);
122 // 6- XTS mode, stateless
123 cc_aligned_struct(16) ccxts_ctx
;
124 cc_aligned_struct(16) ccxts_tweak
;
127 size_t size
; /* first argument to ccxts_ctx_decl(). Size of the ctx data structure */
128 size_t tweak_size
; /* first argument to ccxts_tweak_decl(). Size of the tweak structure, not the expected tweak size */
131 /* Create a xts key from a xts mode object.
132 key must point to at least 'size' bytes of free storage.
133 tweak_key must point to at least 'tweak_size' bytes of free storage.
134 key and tweak_key must differ.
135 Returns nonzero on failure.
137 int (*CC_SPTR(ccmode_xts
, init
))(const struct ccmode_xts
*xts
,
140 const void *data_key
,
141 const void *tweak_key
);
143 void (*CC_SPTR(ccmode_xts
, key_sched
))(const struct ccmode_xts
*xts
,
146 const void *data_key
,
147 const void *tweak_key
);
149 /* Set the tweak (sector number), the block within the sector zero. */
150 int (*CC_SPTR(ccmode_xts
, set_tweak
))(const ccxts_ctx
*ctx
, ccxts_tweak
*tweak
, const void *iv
);
152 /* Encrypt blocks for a sector, clients must call set_tweak before calling
153 this function. Return a pointer to the tweak buffer */
154 void *(*CC_SPTR(ccmode_xts
, xts
))(const ccxts_ctx
*ctx
, ccxts_tweak
*tweak
, size_t nblocks
, const void *in
, void *out
);
160 // 7- GCM mode, statful
161 cc_aligned_struct(16) ccgcm_ctx
;
162 #define CCMODE_GCM_DECRYPTOR 78647
163 #define CCMODE_GCM_ENCRYPTOR 4073947
166 size_t size
; /* first argument to ccgcm_ctx_decl(). */
167 int encdec
; // is it encrypt or decrypt object
169 int (*CC_SPTR(ccmode_gcm
, init
))(const struct ccmode_gcm
*gcm
, ccgcm_ctx
*ctx
, size_t key_nbytes
, const void *key
);
170 int (*CC_SPTR(ccmode_gcm
, set_iv
))(ccgcm_ctx
*ctx
, size_t iv_nbytes
, const void *iv
);
171 int (*CC_SPTR(ccmode_gcm
, gmac
))(ccgcm_ctx
*ctx
, size_t nbytes
, const void *in
); // could just be gcm with NULL out
172 int (*CC_SPTR(ccmode_gcm
, gcm
))(ccgcm_ctx
*ctx
, size_t nbytes
, const void *in
, void *out
);
173 int (*CC_SPTR(ccmode_gcm
, finalize
))(ccgcm_ctx
*key
, size_t tag_nbytes
, void *tag
);
174 int (*CC_SPTR(ccmode_gcm
, reset
))(ccgcm_ctx
*ctx
);
178 // 8- CCM mode, stateful
179 cc_aligned_struct(16) ccccm_ctx
;
180 cc_aligned_struct(16) ccccm_nonce
;
183 size_t size
; /* first argument to ccccm_ctx_decl(). */
184 size_t nonce_size
; /* first argument to ccccm_nonce_decl(). */
186 int (*CC_SPTR(ccmode_ccm
, init
))(const struct ccmode_ccm
*ccm
, ccccm_ctx
*ctx
, size_t key_len
, const void *key
);
187 int (*CC_SPTR(ccmode_ccm
, set_iv
))(ccccm_ctx
*ctx
,
188 ccccm_nonce
*nonce_ctx
,
194 int (*CC_SPTR(ccmode_ccm
, cbcmac
))(ccccm_ctx
*ctx
,
195 ccccm_nonce
*nonce_ctx
,
197 const void *in
); // could just be ccm with NULL out
198 int (*CC_SPTR(ccmode_ccm
, ccm
))(ccccm_ctx
*ctx
, ccccm_nonce
*nonce_ctx
, size_t nbytes
, const void *in
, void *out
);
199 int (*CC_SPTR(ccmode_ccm
, finalize
))(ccccm_ctx
*key
, ccccm_nonce
*nonce_ctx
, void *mac
);
200 int (*CC_SPTR(ccmode_ccm
, reset
))(ccccm_ctx
*key
, ccccm_nonce
*nonce_ctx
);
204 /* We need to expose this (currently)to keep CommonCrypto happy. */
205 struct _ccmode_ccm_nonce
{
206 unsigned char A_i
[16]; /* crypto block iv */
207 unsigned char B_i
[16]; /* mac block iv */
208 unsigned char MAC
[16]; /* crypted mac */
209 unsigned char buf
[16]; /* crypt buffer */
211 uint32_t mode
; /* mode: IV -> AD -> DATA */
212 uint32_t buflen
; /* length of data in buf */
213 uint32_t b_i_len
; /* length of cbcmac data in B_i */
220 cc_aligned_struct(16) ccomac_ctx
;
223 size_t size
; /* first argument to ccomac_ctx_decl(). */
225 int (*CC_SPTR(ccmode_omac
,
226 init
))(const struct ccmode_omac
*omac
, ccomac_ctx
*ctx
, size_t tweak_len
, size_t key_len
, const void *key
);
227 int (*CC_SPTR(ccmode_omac
, omac
))(ccomac_ctx
*ctx
, size_t nblocks
, const void *tweak
, const void *in
, void *out
);
231 #endif /* _CORECRYPTO_CCMODE_IMPL_H_ */