]> git.saurik.com Git - apple/xnu.git/blame - EXTERNAL_HEADERS/corecrypto/ccmode_impl.h
xnu-7195.60.75.tar.gz
[apple/xnu.git] / EXTERNAL_HEADERS / corecrypto / ccmode_impl.h
CommitLineData
f427ee49 1/* Copyright (c) (2010,2011,2012,2015,2016,2017,2018,2019) Apple Inc. All rights reserved.
316670eb 2 *
f427ee49
A
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.
316670eb
A
10 */
11
12#ifndef _CORECRYPTO_CCMODE_IMPL_H_
13#define _CORECRYPTO_CCMODE_IMPL_H_
14
15#include <corecrypto/cc.h>
16
17/* ECB mode. */
18cc_aligned_struct(16) ccecb_ctx;
19
316670eb
A
20/* Actual symmetric algorithm implementation should provide you one of these. */
21struct ccmode_ecb {
2a1bd2d3 22 size_t size; /* first argument to ccecb_ctx_decl(). */
39037602 23 size_t block_size;
2a1bd2d3
A
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);
316670eb
A
27};
28
39037602
A
29/*!
30 * @brief corecrypto symmetrical encryption and decryption modes
31 *
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
34 * 1- ccmod_xxx_init()
35 * 2- ccmod_xxx_decrypt()
36 * 3- ccmod_xxx_encrypt()
d9a64523 37 *
39037602
A
38 * stateful modes CCM and GCM: They provide 7 interface functions that return error codes if a function is called out of state
39 * 1- ccmod_xxx_init()
40 * 2- ccmod_xxx_setiv()
41 * 3- ccmod_xxx_aad()
42 * 4- ccmod_xxx_decrypt()
43 * 5- ccmod_xxx_encrypt()
44 * 6- ccmod_xxx_finalize()
45 * 7- ccmod_xxx_reset()
46 *
47 * the correct call sequences are:
48 *
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
52 *
53 * 1, 2*n, 3*n, 4|5, 6
54 * 1, 2*n, , 4|5, 6
55 * 1, 2*n, , , 6
56 * 1, 2*n, 3*n, , 6
57 */
58
59// 1- CBC mode, stateless
316670eb
A
60cc_aligned_struct(16) cccbc_ctx;
61cc_aligned_struct(16) cccbc_iv;
62
63struct ccmode_cbc {
2a1bd2d3 64 size_t size; /* first argument to cccbc_ctx_decl(). */
39037602 65 size_t block_size;
2a1bd2d3 66 int (*CC_SPTR(ccmode_cbc, init))(const struct ccmode_cbc *cbc, cccbc_ctx *ctx, size_t key_len, const void *key);
316670eb 67 /* cbc encrypt or decrypt nblocks from in to out, iv will be used and updated. */
2a1bd2d3 68 int (*CC_SPTR(ccmode_cbc, cbc))(const cccbc_ctx *ctx, cccbc_iv *iv, size_t nblocks, const void *in, void *out);
316670eb
A
69 const void *custom;
70};
71
39037602 72// 2- CFB mode, stateless
316670eb
A
73cc_aligned_struct(16) cccfb_ctx;
74
75struct ccmode_cfb {
2a1bd2d3 76 size_t size; /* first argument to cccfb_ctx_decl(). */
39037602 77 size_t block_size;
2a1bd2d3
A
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);
316670eb
A
81 const void *custom;
82};
83
39037602 84// 3- CFB8 mode, stateless
316670eb
A
85cc_aligned_struct(16) cccfb8_ctx;
86
87struct ccmode_cfb8 {
2a1bd2d3 88 size_t size; /* first argument to cccfb8_ctx_decl(). */
39037602 89 size_t block_size;
2a1bd2d3
A
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);
316670eb
A
93 const void *custom;
94};
95
39037602 96// 4- CTR mode, stateless
316670eb
A
97cc_aligned_struct(16) ccctr_ctx;
98
99struct ccmode_ctr {
2a1bd2d3
A
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);
316670eb
A
107 const void *custom;
108};
109
39037602 110// 5- OFB mode, stateless
316670eb
A
111cc_aligned_struct(16) ccofb_ctx;
112
113struct ccmode_ofb {
2a1bd2d3 114 size_t size; /* first argument to ccofb_ctx_decl(). */
39037602 115 size_t block_size;
2a1bd2d3
A
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);
316670eb
A
119 const void *custom;
120};
121
39037602 122// 6- XTS mode, stateless
316670eb
A
123cc_aligned_struct(16) ccxts_ctx;
124cc_aligned_struct(16) ccxts_tweak;
125
126struct ccmode_xts {
2a1bd2d3
A
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 */
39037602 129 size_t block_size;
316670eb 130
d9a64523 131 /* Create a xts key from a xts mode object.
5ba3f43e
A
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.
d190cdc3
A
134 key and tweak_key must differ.
135 Returns nonzero on failure.
136 */
2a1bd2d3
A
137 int (*CC_SPTR(ccmode_xts, init))(const struct ccmode_xts *xts,
138 ccxts_ctx *ctx,
139 size_t key_nbytes,
140 const void *data_key,
141 const void *tweak_key);
142
143 void (*CC_SPTR(ccmode_xts, key_sched))(const struct ccmode_xts *xts,
144 ccxts_ctx *ctx,
145 size_t key_nbytes,
146 const void *data_key,
147 const void *tweak_key);
316670eb
A
148
149 /* Set the tweak (sector number), the block within the sector zero. */
2a1bd2d3 150 int (*CC_SPTR(ccmode_xts, set_tweak))(const ccxts_ctx *ctx, ccxts_tweak *tweak, const void *iv);
316670eb
A
151
152 /* Encrypt blocks for a sector, clients must call set_tweak before calling
153 this function. Return a pointer to the tweak buffer */
2a1bd2d3 154 void *(*CC_SPTR(ccmode_xts, xts))(const ccxts_ctx *ctx, ccxts_tweak *tweak, size_t nblocks, const void *in, void *out);
316670eb
A
155
156 const void *custom;
157 const void *custom1;
158};
159
2a1bd2d3 160// 7- GCM mode, statful
316670eb 161cc_aligned_struct(16) ccgcm_ctx;
2a1bd2d3
A
162#define CCMODE_GCM_DECRYPTOR 78647
163#define CCMODE_GCM_ENCRYPTOR 4073947
316670eb
A
164
165struct ccmode_gcm {
2a1bd2d3
A
166 size_t size; /* first argument to ccgcm_ctx_decl(). */
167 int encdec; // is it encrypt or decrypt object
39037602 168 size_t block_size;
2a1bd2d3
A
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);
316670eb
A
175 const void *custom;
176};
177
2a1bd2d3 178// 8- CCM mode, stateful
fe8ab488
A
179cc_aligned_struct(16) ccccm_ctx;
180cc_aligned_struct(16) ccccm_nonce;
181
182struct ccmode_ccm {
2a1bd2d3
A
183 size_t size; /* first argument to ccccm_ctx_decl(). */
184 size_t nonce_size; /* first argument to ccccm_nonce_decl(). */
39037602 185 size_t block_size;
2a1bd2d3
A
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,
189 size_t nonce_len,
190 const void *nonce,
191 size_t mac_size,
192 size_t auth_len,
193 size_t data_len);
194 int (*CC_SPTR(ccmode_ccm, cbcmac))(ccccm_ctx *ctx,
195 ccccm_nonce *nonce_ctx,
196 size_t nbytes,
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);
fe8ab488
A
201 const void *custom;
202};
203
d9a64523
A
204/* We need to expose this (currently)to keep CommonCrypto happy. */
205struct _ccmode_ccm_nonce {
2a1bd2d3
A
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 */
d9a64523 210
2a1bd2d3
A
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 */
d9a64523
A
214
215 size_t nonce_size;
216 size_t mac_size;
217};
fe8ab488 218
316670eb 219/* OMAC mode. */
316670eb
A
220cc_aligned_struct(16) ccomac_ctx;
221
222struct ccmode_omac {
2a1bd2d3 223 size_t size; /* first argument to ccomac_ctx_decl(). */
39037602 224 size_t block_size;
2a1bd2d3
A
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);
316670eb
A
228 const void *custom;
229};
230
231#endif /* _CORECRYPTO_CCMODE_IMPL_H_ */