xref: /xnu-10063.121.3/EXTERNAL_HEADERS/corecrypto/ccmode.h (revision 2c2f96dc2b9a4408a43d3150ae9c105355ca3daa)
1*2c2f96dcSApple OSS Distributions /* Copyright (c) (2010-2012,2014-2023) Apple Inc. All rights reserved.
2*2c2f96dcSApple OSS Distributions  *
3*2c2f96dcSApple OSS Distributions  * corecrypto is licensed under Apple Inc.’s Internal Use License Agreement (which
4*2c2f96dcSApple OSS Distributions  * is contained in the License.txt file distributed with corecrypto) and only to
5*2c2f96dcSApple OSS Distributions  * people who accept that license. IMPORTANT:  Any license rights granted to you by
6*2c2f96dcSApple OSS Distributions  * Apple Inc. (if any) are limited to internal use within your organization only on
7*2c2f96dcSApple OSS Distributions  * devices and computers you own or control, for the sole purpose of verifying the
8*2c2f96dcSApple OSS Distributions  * security characteristics and correct functioning of the Apple Software.  You may
9*2c2f96dcSApple OSS Distributions  * not, directly or indirectly, redistribute the Apple Software or any portions thereof.
10*2c2f96dcSApple OSS Distributions  */
11*2c2f96dcSApple OSS Distributions 
12*2c2f96dcSApple OSS Distributions #ifndef _CORECRYPTO_CCMODE_H_
13*2c2f96dcSApple OSS Distributions #define _CORECRYPTO_CCMODE_H_
14*2c2f96dcSApple OSS Distributions 
15*2c2f96dcSApple OSS Distributions #include <corecrypto/cc_config.h>
16*2c2f96dcSApple OSS Distributions #include <corecrypto/cc_priv.h>
17*2c2f96dcSApple OSS Distributions #include <corecrypto/ccmode_impl.h>
18*2c2f96dcSApple OSS Distributions #include <corecrypto/ccmode_siv.h>
19*2c2f96dcSApple OSS Distributions #include <corecrypto/ccmode_siv_hmac.h>
20*2c2f96dcSApple OSS Distributions 
21*2c2f96dcSApple OSS Distributions CC_PTRCHECK_CAPABLE_HEADER()
22*2c2f96dcSApple OSS Distributions 
23*2c2f96dcSApple OSS Distributions /* ECB mode. */
24*2c2f96dcSApple OSS Distributions 
25*2c2f96dcSApple OSS Distributions /* Declare a ecb key named _name_.  Pass the size field of a struct ccmode_ecb
26*2c2f96dcSApple OSS Distributions    for _size_. */
27*2c2f96dcSApple OSS Distributions #define ccecb_ctx_decl(_size_, _name_) cc_ctx_decl_vla(ccecb_ctx, _size_, _name_)
28*2c2f96dcSApple OSS Distributions #define ccecb_ctx_clear(_size_, _name_) cc_clear(_size_, _name_)
29*2c2f96dcSApple OSS Distributions 
30*2c2f96dcSApple OSS Distributions size_t ccecb_context_size(const struct ccmode_ecb *mode);
31*2c2f96dcSApple OSS Distributions 
32*2c2f96dcSApple OSS Distributions size_t ccecb_block_size(const struct ccmode_ecb *mode);
33*2c2f96dcSApple OSS Distributions 
34*2c2f96dcSApple OSS Distributions int ccecb_init(const struct ccmode_ecb *mode, ccecb_ctx *ctx, size_t key_len, const void *cc_sized_by(key_len) key);
35*2c2f96dcSApple OSS Distributions 
36*2c2f96dcSApple OSS Distributions int ccecb_update(const struct ccmode_ecb *mode, const ccecb_ctx *ctx, size_t nblocks, const void *cc_indexable in, void *cc_indexable out);
37*2c2f96dcSApple OSS Distributions 
38*2c2f96dcSApple OSS Distributions cc_ptrcheck_unavailable_r(ccecb_one_shot_explicit)
39*2c2f96dcSApple OSS Distributions int ccecb_one_shot(const struct ccmode_ecb *mode,
40*2c2f96dcSApple OSS Distributions                    size_t key_len,
41*2c2f96dcSApple OSS Distributions                    const void *cc_sized_by(key_len) key,
42*2c2f96dcSApple OSS Distributions                    size_t nblocks,
43*2c2f96dcSApple OSS Distributions                    const void *cc_unsafe_indexable in,
44*2c2f96dcSApple OSS Distributions                    void *cc_unsafe_indexable out);
45*2c2f96dcSApple OSS Distributions 
46*2c2f96dcSApple OSS Distributions int ccecb_one_shot_explicit(const struct ccmode_ecb *mode,
47*2c2f96dcSApple OSS Distributions                             size_t key_len,
48*2c2f96dcSApple OSS Distributions                             size_t block_size,
49*2c2f96dcSApple OSS Distributions                             size_t nblocks,
50*2c2f96dcSApple OSS Distributions                             const void *cc_sized_by(key_len) key,
51*2c2f96dcSApple OSS Distributions                             const void *cc_sized_by(block_size * nblocks) in,
52*2c2f96dcSApple OSS Distributions                             void *cc_sized_by(block_size * nblocks) out);
53*2c2f96dcSApple OSS Distributions 
54*2c2f96dcSApple OSS Distributions /* CBC mode. */
55*2c2f96dcSApple OSS Distributions 
56*2c2f96dcSApple OSS Distributions /* Declare a cbc key named _name_.  Pass the size field of a struct ccmode_cbc
57*2c2f96dcSApple OSS Distributions    for _size_. */
58*2c2f96dcSApple OSS Distributions #define cccbc_ctx_decl(_size_, _name_) cc_ctx_decl_vla(cccbc_ctx, _size_, _name_)
59*2c2f96dcSApple OSS Distributions #define cccbc_ctx_clear(_size_, _name_) cc_clear(_size_, _name_)
60*2c2f96dcSApple OSS Distributions 
61*2c2f96dcSApple OSS Distributions /* Declare a cbc iv tweak named _name_.  Pass the blocksize field of a
62*2c2f96dcSApple OSS Distributions    struct ccmode_cbc for _size_. */
63*2c2f96dcSApple OSS Distributions #define cccbc_iv_decl(_size_, _name_) cc_ctx_decl_vla(cccbc_iv, _size_, _name_)
64*2c2f96dcSApple OSS Distributions #define cccbc_iv_clear(_size_, _name_) cc_clear(_size_, _name_)
65*2c2f96dcSApple OSS Distributions 
66*2c2f96dcSApple OSS Distributions /* Actual symmetric algorithm implementation can provide you one of these.
67*2c2f96dcSApple OSS Distributions 
68*2c2f96dcSApple OSS Distributions    Alternatively you can create a ccmode_cbc instance from any ccmode_ecb
69*2c2f96dcSApple OSS Distributions    cipher.  To do so, statically initialize a struct ccmode_cbc using the
70*2c2f96dcSApple OSS Distributions    CCMODE_FACTORY_CBC_DECRYPT or CCMODE_FACTORY_CBC_ENCRYPT macros.
71*2c2f96dcSApple OSS Distributions    Alternatively you can dynamically initialize a struct ccmode_cbc
72*2c2f96dcSApple OSS Distributions    ccmode_factory_cbc_decrypt() or ccmode_factory_cbc_encrypt(). */
73*2c2f96dcSApple OSS Distributions 
74*2c2f96dcSApple OSS Distributions size_t cccbc_context_size(const struct ccmode_cbc *mode);
75*2c2f96dcSApple OSS Distributions 
76*2c2f96dcSApple OSS Distributions size_t cccbc_block_size(const struct ccmode_cbc *mode);
77*2c2f96dcSApple OSS Distributions 
78*2c2f96dcSApple OSS Distributions int cccbc_init(const struct ccmode_cbc *mode, cccbc_ctx *ctx, size_t key_len, const void *cc_sized_by(key_len) key);
79*2c2f96dcSApple OSS Distributions 
80*2c2f96dcSApple OSS Distributions int cccbc_copy_iv(cccbc_iv *cc_sized_by(len) iv_ctx, const void *cc_sized_by(len) iv, size_t len);
81*2c2f96dcSApple OSS Distributions int cccbc_clear_iv(cccbc_iv *cc_sized_by(len) iv_ctx, size_t len);
82*2c2f96dcSApple OSS Distributions 
83*2c2f96dcSApple OSS Distributions cc_ptrcheck_unavailable() // Use cccbc_copy_iv() or cccbc_clear_iv() directly.
84*2c2f96dcSApple OSS Distributions int cccbc_set_iv(const struct ccmode_cbc *mode, cccbc_iv *iv_ctx, const void *iv);
85*2c2f96dcSApple OSS Distributions 
86*2c2f96dcSApple OSS Distributions int cccbc_update(const struct ccmode_cbc *mode, const cccbc_ctx *ctx, cccbc_iv *iv, size_t nblocks, const void *cc_indexable in, void *cc_indexable out);
87*2c2f96dcSApple OSS Distributions 
88*2c2f96dcSApple OSS Distributions cc_ptrcheck_unavailable_r(cccbc_one_shot_explicit)
89*2c2f96dcSApple OSS Distributions int cccbc_one_shot(const struct ccmode_cbc *mode,
90*2c2f96dcSApple OSS Distributions                    size_t key_len,
91*2c2f96dcSApple OSS Distributions                    const void *cc_sized_by(key_len) key,
92*2c2f96dcSApple OSS Distributions                    const void *iv,
93*2c2f96dcSApple OSS Distributions                    size_t nblocks,
94*2c2f96dcSApple OSS Distributions                    const void *cc_unsafe_indexable in,
95*2c2f96dcSApple OSS Distributions                    void *cc_unsafe_indexable out);
96*2c2f96dcSApple OSS Distributions 
97*2c2f96dcSApple OSS Distributions int cccbc_one_shot_explicit(const struct ccmode_cbc *mode,
98*2c2f96dcSApple OSS Distributions                             size_t key_len,
99*2c2f96dcSApple OSS Distributions                             size_t iv_len,
100*2c2f96dcSApple OSS Distributions                             size_t block_size,
101*2c2f96dcSApple OSS Distributions                             size_t nblocks,
102*2c2f96dcSApple OSS Distributions                             const void *cc_sized_by(key_len) key,
103*2c2f96dcSApple OSS Distributions                             const void *cc_sized_by(iv_len) iv,
104*2c2f96dcSApple OSS Distributions                             const void *cc_sized_by(block_size * nblocks) in,
105*2c2f96dcSApple OSS Distributions                             void *cc_sized_by(block_size * nblocks) out);
106*2c2f96dcSApple OSS Distributions 
107*2c2f96dcSApple OSS Distributions /* CFB mode. */
108*2c2f96dcSApple OSS Distributions 
109*2c2f96dcSApple OSS Distributions /* Declare a cfb key named _name_.  Pass the size field of a struct ccmode_cfb
110*2c2f96dcSApple OSS Distributions    for _size_. */
111*2c2f96dcSApple OSS Distributions #define cccfb_ctx_decl(_size_, _name_) cc_ctx_decl_vla(cccfb_ctx, _size_, _name_)
112*2c2f96dcSApple OSS Distributions #define cccfb_ctx_clear(_size_, _name_) cc_clear(_size_, _name_)
113*2c2f96dcSApple OSS Distributions 
114*2c2f96dcSApple OSS Distributions size_t cccfb_context_size(const struct ccmode_cfb *mode);
115*2c2f96dcSApple OSS Distributions 
116*2c2f96dcSApple OSS Distributions size_t cccfb_block_size(const struct ccmode_cfb *mode);
117*2c2f96dcSApple OSS Distributions 
118*2c2f96dcSApple OSS Distributions int cccfb_init(const struct ccmode_cfb *mode, cccfb_ctx *ctx, size_t key_len, const void *cc_sized_by(key_len) key, const void *cc_indexable iv);
119*2c2f96dcSApple OSS Distributions 
120*2c2f96dcSApple OSS Distributions int cccfb_update(const struct ccmode_cfb *mode, cccfb_ctx *ctx, size_t nbytes, const void *cc_sized_by(nbytes) in, void *cc_sized_by(nbytes) out);
121*2c2f96dcSApple OSS Distributions 
122*2c2f96dcSApple OSS Distributions int cccfb_one_shot(const struct ccmode_cfb *mode,
123*2c2f96dcSApple OSS Distributions                    size_t key_len,
124*2c2f96dcSApple OSS Distributions                    const void *cc_sized_by(key_len) key,
125*2c2f96dcSApple OSS Distributions                    const void *cc_indexable iv,
126*2c2f96dcSApple OSS Distributions                    size_t nbytes,
127*2c2f96dcSApple OSS Distributions                    const void *cc_sized_by(nbytes) in,
128*2c2f96dcSApple OSS Distributions                    void *cc_sized_by(nbytes) out);
129*2c2f96dcSApple OSS Distributions 
130*2c2f96dcSApple OSS Distributions /* CFB8 mode. */
131*2c2f96dcSApple OSS Distributions 
132*2c2f96dcSApple OSS Distributions /* Declare a cfb8 key named _name_.  Pass the size field of a struct ccmode_cfb8
133*2c2f96dcSApple OSS Distributions  for _size_. */
134*2c2f96dcSApple OSS Distributions #define cccfb8_ctx_decl(_size_, _name_) cc_ctx_decl_vla(cccfb8_ctx, _size_, _name_)
135*2c2f96dcSApple OSS Distributions #define cccfb8_ctx_clear(_size_, _name_) cc_clear(_size_, _name_)
136*2c2f96dcSApple OSS Distributions 
137*2c2f96dcSApple OSS Distributions size_t cccfb8_context_size(const struct ccmode_cfb8 *mode);
138*2c2f96dcSApple OSS Distributions 
139*2c2f96dcSApple OSS Distributions size_t cccfb8_block_size(const struct ccmode_cfb8 *mode);
140*2c2f96dcSApple OSS Distributions 
141*2c2f96dcSApple OSS Distributions int cccfb8_init(const struct ccmode_cfb8 *mode, cccfb8_ctx *ctx, size_t key_len, const void *cc_sized_by(key_len) key, const void *cc_indexable iv);
142*2c2f96dcSApple OSS Distributions 
143*2c2f96dcSApple OSS Distributions int cccfb8_update(const struct ccmode_cfb8 *mode, cccfb8_ctx *ctx, size_t nbytes, const void *cc_sized_by(nbytes) in, void *cc_sized_by(nbytes) out);
144*2c2f96dcSApple OSS Distributions 
145*2c2f96dcSApple OSS Distributions int cccfb8_one_shot(const struct ccmode_cfb8 *mode,
146*2c2f96dcSApple OSS Distributions                     size_t key_len,
147*2c2f96dcSApple OSS Distributions                     const void *cc_sized_by(key_len) key,
148*2c2f96dcSApple OSS Distributions                     const void *cc_indexable iv,
149*2c2f96dcSApple OSS Distributions                     size_t nbytes,
150*2c2f96dcSApple OSS Distributions                     const void *cc_sized_by(nbytes) in,
151*2c2f96dcSApple OSS Distributions                     void *cc_sized_by(nbytes) out);
152*2c2f96dcSApple OSS Distributions 
153*2c2f96dcSApple OSS Distributions /* CTR mode. */
154*2c2f96dcSApple OSS Distributions 
155*2c2f96dcSApple OSS Distributions /* Declare a ctr key named _name_.  Pass the size field of a struct ccmode_ctr
156*2c2f96dcSApple OSS Distributions  for _size_. */
157*2c2f96dcSApple OSS Distributions #define ccctr_ctx_decl(_size_, _name_) cc_ctx_decl_vla(ccctr_ctx, _size_, _name_)
158*2c2f96dcSApple OSS Distributions #define ccctr_ctx_clear(_size_, _name_) cc_clear(_size_, _name_)
159*2c2f96dcSApple OSS Distributions 
160*2c2f96dcSApple OSS Distributions /* This is Integer Counter Mode: The IV is the initial value of the counter
161*2c2f96dcSApple OSS Distributions  that is incremented by 1 for each new block. Use the mode flags to select
162*2c2f96dcSApple OSS Distributions  if the IV/Counter is stored in big or little endian. */
163*2c2f96dcSApple OSS Distributions 
164*2c2f96dcSApple OSS Distributions size_t ccctr_context_size(const struct ccmode_ctr *mode);
165*2c2f96dcSApple OSS Distributions 
166*2c2f96dcSApple OSS Distributions size_t ccctr_block_size(const struct ccmode_ctr *mode);
167*2c2f96dcSApple OSS Distributions 
168*2c2f96dcSApple OSS Distributions int ccctr_init(const struct ccmode_ctr *mode, ccctr_ctx *ctx, size_t key_len, const void *cc_sized_by(key_len) key, const void *cc_indexable iv);
169*2c2f96dcSApple OSS Distributions 
170*2c2f96dcSApple OSS Distributions int ccctr_update(const struct ccmode_ctr *mode, ccctr_ctx *ctx, size_t nbytes, const void *cc_sized_by(nbytes) in, void *cc_sized_by(nbytes) out);
171*2c2f96dcSApple OSS Distributions 
172*2c2f96dcSApple OSS Distributions int ccctr_one_shot(const struct ccmode_ctr *mode,
173*2c2f96dcSApple OSS Distributions                    size_t key_len,
174*2c2f96dcSApple OSS Distributions                    const void *cc_sized_by(key_len) key,
175*2c2f96dcSApple OSS Distributions                    const void *cc_indexable iv,
176*2c2f96dcSApple OSS Distributions                    size_t nbytes,
177*2c2f96dcSApple OSS Distributions                    const void *cc_sized_by(nbytes) in,
178*2c2f96dcSApple OSS Distributions                    void *cc_sized_by(nbytes) out);
179*2c2f96dcSApple OSS Distributions 
180*2c2f96dcSApple OSS Distributions /* OFB mode. */
181*2c2f96dcSApple OSS Distributions 
182*2c2f96dcSApple OSS Distributions /* Declare a ofb key named _name_.  Pass the size field of a struct ccmode_ofb
183*2c2f96dcSApple OSS Distributions  for _size_. */
184*2c2f96dcSApple OSS Distributions #define ccofb_ctx_decl(_size_, _name_) cc_ctx_decl_vla(ccofb_ctx, _size_, _name_)
185*2c2f96dcSApple OSS Distributions #define ccofb_ctx_clear(_size_, _name_) cc_clear(_size_, _name_)
186*2c2f96dcSApple OSS Distributions 
187*2c2f96dcSApple OSS Distributions size_t ccofb_context_size(const struct ccmode_ofb *mode);
188*2c2f96dcSApple OSS Distributions 
189*2c2f96dcSApple OSS Distributions size_t ccofb_block_size(const struct ccmode_ofb *mode);
190*2c2f96dcSApple OSS Distributions 
191*2c2f96dcSApple OSS Distributions int ccofb_init(const struct ccmode_ofb *mode, ccofb_ctx *ctx, size_t key_len, const void *cc_sized_by(key_len) key, const void *cc_indexable iv);
192*2c2f96dcSApple OSS Distributions 
193*2c2f96dcSApple OSS Distributions int ccofb_update(const struct ccmode_ofb *mode, ccofb_ctx *ctx, size_t nbytes, const void *cc_sized_by(nbytes) in, void *cc_sized_by(nbytes) out);
194*2c2f96dcSApple OSS Distributions 
195*2c2f96dcSApple OSS Distributions int ccofb_one_shot(const struct ccmode_ofb *mode,
196*2c2f96dcSApple OSS Distributions                    size_t key_len,
197*2c2f96dcSApple OSS Distributions                    const void *cc_sized_by(key_len) key,
198*2c2f96dcSApple OSS Distributions                    const void *cc_indexable iv,
199*2c2f96dcSApple OSS Distributions                    size_t nbytes,
200*2c2f96dcSApple OSS Distributions                    const void *cc_sized_by(nbytes) in,
201*2c2f96dcSApple OSS Distributions                    void *cc_sized_by(nbytes) out);
202*2c2f96dcSApple OSS Distributions 
203*2c2f96dcSApple OSS Distributions /* XTS mode. */
204*2c2f96dcSApple OSS Distributions 
205*2c2f96dcSApple OSS Distributions /* Declare a xts key named _name_.  Pass the size field of a struct ccmode_xts
206*2c2f96dcSApple OSS Distributions  for _size_. */
207*2c2f96dcSApple OSS Distributions #define ccxts_ctx_decl(_size_, _name_) cc_ctx_decl_vla(ccxts_ctx, _size_, _name_)
208*2c2f96dcSApple OSS Distributions #define ccxts_ctx_clear(_size_, _name_) cc_clear(_size_, _name_)
209*2c2f96dcSApple OSS Distributions 
210*2c2f96dcSApple OSS Distributions /* Declare a xts tweak named _name_.  Pass the tweak_size field of a
211*2c2f96dcSApple OSS Distributions    struct ccmode_xts for _size_. */
212*2c2f96dcSApple OSS Distributions #define ccxts_tweak_decl(_size_, _name_) cc_ctx_decl_vla(ccxts_tweak, _size_, _name_)
213*2c2f96dcSApple OSS Distributions #define ccxts_tweak_clear(_size_, _name_) cc_clear(_size_, _name_)
214*2c2f96dcSApple OSS Distributions 
215*2c2f96dcSApple OSS Distributions /* Actual symmetric algorithm implementation can provide you one of these.
216*2c2f96dcSApple OSS Distributions 
217*2c2f96dcSApple OSS Distributions  Alternatively you can create a ccmode_xts instance from any ccmode_ecb
218*2c2f96dcSApple OSS Distributions  cipher.  To do so, statically initialize a struct ccmode_xts using the
219*2c2f96dcSApple OSS Distributions  CCMODE_FACTORY_XTS_DECRYPT or CCMODE_FACTORY_XTS_ENCRYPT macros. Alternatively
220*2c2f96dcSApple OSS Distributions  you can dynamically initialize a struct ccmode_xts
221*2c2f96dcSApple OSS Distributions  ccmode_factory_xts_decrypt() or ccmode_factory_xts_encrypt(). */
222*2c2f96dcSApple OSS Distributions 
223*2c2f96dcSApple OSS Distributions /* NOTE that xts mode does not do cts padding.  It's really an xex mode.
224*2c2f96dcSApple OSS Distributions    If you need cts padding use the ccpad_xts_encrypt and ccpad_xts_decrypt
225*2c2f96dcSApple OSS Distributions    functions.   Also note that xts only works for ecb modes with a block_size
226*2c2f96dcSApple OSS Distributions    of 16.  */
227*2c2f96dcSApple OSS Distributions 
228*2c2f96dcSApple OSS Distributions size_t ccxts_context_size(const struct ccmode_xts *mode);
229*2c2f96dcSApple OSS Distributions 
230*2c2f96dcSApple OSS Distributions size_t ccxts_block_size(const struct ccmode_xts *mode);
231*2c2f96dcSApple OSS Distributions 
232*2c2f96dcSApple OSS Distributions /*!
233*2c2f96dcSApple OSS Distributions  @function   ccxts_init
234*2c2f96dcSApple OSS Distributions  @abstract   Initialize an XTS context.
235*2c2f96dcSApple OSS Distributions 
236*2c2f96dcSApple OSS Distributions  @param      mode       Descriptor for the mode
237*2c2f96dcSApple OSS Distributions  @param      ctx        Context for this instance
238*2c2f96dcSApple OSS Distributions  @param      key_nbytes Length of the key arguments in bytes
239*2c2f96dcSApple OSS Distributions  @param      data_key   Key for data encryption
240*2c2f96dcSApple OSS Distributions  @param      tweak_key  Key for tweak generation
241*2c2f96dcSApple OSS Distributions 
242*2c2f96dcSApple OSS Distributions  @result     0 iff successful.
243*2c2f96dcSApple OSS Distributions 
244*2c2f96dcSApple OSS Distributions  @discussion For security reasons, the two keys must be different.
245*2c2f96dcSApple OSS Distributions  */
246*2c2f96dcSApple OSS Distributions int ccxts_init(const struct ccmode_xts *mode, ccxts_ctx *ctx, size_t key_nbytes, const void *cc_sized_by(key_nbytes) data_key, const void *cc_sized_by(key_nbytes) tweak_key);
247*2c2f96dcSApple OSS Distributions 
248*2c2f96dcSApple OSS Distributions /*!
249*2c2f96dcSApple OSS Distributions  @function   ccxts_set_tweak
250*2c2f96dcSApple OSS Distributions  @abstract   Initialize the tweak for a sector.
251*2c2f96dcSApple OSS Distributions 
252*2c2f96dcSApple OSS Distributions  @param      mode       Descriptor for the mode
253*2c2f96dcSApple OSS Distributions  @param      ctx        Context for this instance
254*2c2f96dcSApple OSS Distributions  @param      tweak      Context for the tweak for this sector
255*2c2f96dcSApple OSS Distributions  @param      iv         Data used to generate the tweak
256*2c2f96dcSApple OSS Distributions 
257*2c2f96dcSApple OSS Distributions  @discussion The IV must be exactly one block in length.
258*2c2f96dcSApple OSS Distributions  */
259*2c2f96dcSApple OSS Distributions int ccxts_set_tweak(const struct ccmode_xts *mode, ccxts_ctx *ctx, ccxts_tweak *tweak, const void *cc_indexable iv);
260*2c2f96dcSApple OSS Distributions 
261*2c2f96dcSApple OSS Distributions /*!
262*2c2f96dcSApple OSS Distributions  @function   ccxts_update
263*2c2f96dcSApple OSS Distributions  @abstract   Encrypt or decrypt data.
264*2c2f96dcSApple OSS Distributions 
265*2c2f96dcSApple OSS Distributions  @param      mode       Descriptor for the mode
266*2c2f96dcSApple OSS Distributions  @param      ctx        Context for an instance
267*2c2f96dcSApple OSS Distributions  @param      tweak      Context for the tweak for this sector
268*2c2f96dcSApple OSS Distributions  @param      nblocks    Length of the data in blocks
269*2c2f96dcSApple OSS Distributions  @param      in         Input data
270*2c2f96dcSApple OSS Distributions  @param      out        Output buffer
271*2c2f96dcSApple OSS Distributions 
272*2c2f96dcSApple OSS Distributions  @result     The updated internal buffer of the tweak context. May be ignored.
273*2c2f96dcSApple OSS Distributions   */
274*2c2f96dcSApple OSS Distributions void *cc_unsafe_indexable
275*2c2f96dcSApple OSS Distributions ccxts_update(const struct ccmode_xts *mode, ccxts_ctx *ctx, ccxts_tweak *tweak, size_t nblocks, const void *cc_indexable in, void *cc_indexable out);
276*2c2f96dcSApple OSS Distributions 
277*2c2f96dcSApple OSS Distributions /*!
278*2c2f96dcSApple OSS Distributions  @function   ccxts_one_shot
279*2c2f96dcSApple OSS Distributions  @abstract   Encrypt or decrypt data in XTS mode.
280*2c2f96dcSApple OSS Distributions 
281*2c2f96dcSApple OSS Distributions  @param      mode       Descriptor for the mode
282*2c2f96dcSApple OSS Distributions  @param      key_nbytes Length of the key arguments in bytes
283*2c2f96dcSApple OSS Distributions  @param      data_key   Key for data encryption
284*2c2f96dcSApple OSS Distributions  @param      tweak_key  Key for tweak generation
285*2c2f96dcSApple OSS Distributions  @param      iv         Data used to generate the tweak
286*2c2f96dcSApple OSS Distributions  @param      nblocks    Length of the data in blocks
287*2c2f96dcSApple OSS Distributions  @param      in         Input data
288*2c2f96dcSApple OSS Distributions  @param      out        Output buffer
289*2c2f96dcSApple OSS Distributions 
290*2c2f96dcSApple OSS Distributions  @result     0 iff successful.
291*2c2f96dcSApple OSS Distributions 
292*2c2f96dcSApple OSS Distributions  @discussion For security reasons, the two keys must be different.
293*2c2f96dcSApple OSS Distributions  */
294*2c2f96dcSApple OSS Distributions int ccxts_one_shot(const struct ccmode_xts *mode,
295*2c2f96dcSApple OSS Distributions                    size_t key_nbytes,
296*2c2f96dcSApple OSS Distributions                    const void *cc_sized_by(key_nbytes) data_key,
297*2c2f96dcSApple OSS Distributions                    const void *cc_sized_by(key_nbytes) tweak_key,
298*2c2f96dcSApple OSS Distributions                    const void *cc_unsafe_indexable iv,
299*2c2f96dcSApple OSS Distributions                    size_t nblocks,
300*2c2f96dcSApple OSS Distributions                    const void *cc_unsafe_indexable in,
301*2c2f96dcSApple OSS Distributions                    void *cc_unsafe_indexable out);
302*2c2f96dcSApple OSS Distributions 
303*2c2f96dcSApple OSS Distributions /* Authenticated cipher modes. */
304*2c2f96dcSApple OSS Distributions 
305*2c2f96dcSApple OSS Distributions /* GCM mode. */
306*2c2f96dcSApple OSS Distributions 
307*2c2f96dcSApple OSS Distributions /* Declare a gcm key named _name_.  Pass the size field of a struct ccmode_gcm
308*2c2f96dcSApple OSS Distributions  for _size_. */
309*2c2f96dcSApple OSS Distributions #define ccgcm_ctx_decl(_size_, _name_) cc_ctx_decl_vla(ccgcm_ctx, _size_, _name_)
310*2c2f96dcSApple OSS Distributions #define ccgcm_ctx_clear(_size_, _name_) cc_clear(_size_, _name_)
311*2c2f96dcSApple OSS Distributions 
312*2c2f96dcSApple OSS Distributions #define CCGCM_IV_NBYTES 12
313*2c2f96dcSApple OSS Distributions #define CCGCM_BLOCK_NBYTES 16
314*2c2f96dcSApple OSS Distributions 
315*2c2f96dcSApple OSS Distributions /* (2^32 - 2) blocks */
316*2c2f96dcSApple OSS Distributions /* (2^36 - 32) bytes */
317*2c2f96dcSApple OSS Distributions /* (2^39 - 256) bits */
318*2c2f96dcSApple OSS Distributions /* Exceeding this figure breaks confidentiality and authenticity. */
319*2c2f96dcSApple OSS Distributions #define CCGCM_TEXT_MAX_NBYTES ((1ULL << 36) - 32ULL)
320*2c2f96dcSApple OSS Distributions 
321*2c2f96dcSApple OSS Distributions size_t ccgcm_context_size(const struct ccmode_gcm *mode);
322*2c2f96dcSApple OSS Distributions 
323*2c2f96dcSApple OSS Distributions size_t ccgcm_block_size(const struct ccmode_gcm *mode);
324*2c2f96dcSApple OSS Distributions 
325*2c2f96dcSApple OSS Distributions /*!
326*2c2f96dcSApple OSS Distributions  @function   ccgcm_init
327*2c2f96dcSApple OSS Distributions  @abstract   Initialize a GCM context.
328*2c2f96dcSApple OSS Distributions 
329*2c2f96dcSApple OSS Distributions  @param      mode       Descriptor for the mode
330*2c2f96dcSApple OSS Distributions  @param      ctx        Context for this instance
331*2c2f96dcSApple OSS Distributions  @param      key_nbytes Length of the key in bytes
332*2c2f96dcSApple OSS Distributions  @param      key        Key for the underlying blockcipher (AES)
333*2c2f96dcSApple OSS Distributions 
334*2c2f96dcSApple OSS Distributions  @result     0 iff successful.
335*2c2f96dcSApple OSS Distributions 
336*2c2f96dcSApple OSS Distributions  @discussion The correct sequence of calls is:
337*2c2f96dcSApple OSS Distributions 
338*2c2f96dcSApple OSS Distributions  @code ccgcm_init(...)
339*2c2f96dcSApple OSS Distributions  ccgcm_set_iv(...)
340*2c2f96dcSApple OSS Distributions  ccgcm_aad(...)       (may be called zero or more times)
341*2c2f96dcSApple OSS Distributions  ccgcm_update(...)    (may be called zero or more times)
342*2c2f96dcSApple OSS Distributions  ccgcm_finalize(...)
343*2c2f96dcSApple OSS Distributions 
344*2c2f96dcSApple OSS Distributions  To reuse the context for additional encryptions, follow this sequence:
345*2c2f96dcSApple OSS Distributions 
346*2c2f96dcSApple OSS Distributions  @code ccgcm_reset(...)
347*2c2f96dcSApple OSS Distributions  ccgcm_set_iv(...)
348*2c2f96dcSApple OSS Distributions  ccgcm_aad(...)       (may be called zero or more times)
349*2c2f96dcSApple OSS Distributions  ccgcm_update(...)    (may be called zero or more times)
350*2c2f96dcSApple OSS Distributions  ccgcm_finalize(...)
351*2c2f96dcSApple OSS Distributions 
352*2c2f96dcSApple OSS Distributions  @warning The key-IV pair must be unique per encryption. The IV must be nonzero in length.
353*2c2f96dcSApple OSS Distributions 
354*2c2f96dcSApple OSS Distributions  @warning It is not permitted to call @p ccgcm_inc_iv after initializing the cipher via the @p ccgcm_init interface. Nonzero is
355*2c2f96dcSApple OSS Distributions  returned in the event of an improper call sequence.
356*2c2f96dcSApple OSS Distributions 
357*2c2f96dcSApple OSS Distributions  @warning This function is not FIPS-compliant. Use @p ccgcm_init_with_iv instead.
358*2c2f96dcSApple OSS Distributions  */
359*2c2f96dcSApple OSS Distributions int ccgcm_init(const struct ccmode_gcm *mode, ccgcm_ctx *ctx, size_t key_nbytes, const void *cc_sized_by(key_nbytes) key);
360*2c2f96dcSApple OSS Distributions 
361*2c2f96dcSApple OSS Distributions /*!
362*2c2f96dcSApple OSS Distributions  @function   ccgcm_init_with_iv
363*2c2f96dcSApple OSS Distributions  @abstract   Initialize a GCM context to manage IVs internally.
364*2c2f96dcSApple OSS Distributions 
365*2c2f96dcSApple OSS Distributions  @param      mode       Descriptor for the mode
366*2c2f96dcSApple OSS Distributions  @param      ctx        Context for this instance
367*2c2f96dcSApple OSS Distributions  @param      key_nbytes Length of the key in bytes
368*2c2f96dcSApple OSS Distributions  @param      key        Key for the underlying blockcipher (AES)
369*2c2f96dcSApple OSS Distributions  @param      iv         IV for the first encryption
370*2c2f96dcSApple OSS Distributions 
371*2c2f96dcSApple OSS Distributions  @result     0 iff successful.
372*2c2f96dcSApple OSS Distributions 
373*2c2f96dcSApple OSS Distributions  @discussion The correct sequence of calls is:
374*2c2f96dcSApple OSS Distributions 
375*2c2f96dcSApple OSS Distributions  @code ccgcm_init_with_iv(...)
376*2c2f96dcSApple OSS Distributions  ccgcm_aad(...)       (may be called zero or more times)
377*2c2f96dcSApple OSS Distributions  ccgcm_update(...)    (may be called zero or more times)
378*2c2f96dcSApple OSS Distributions  ccgcm_finalize(...)
379*2c2f96dcSApple OSS Distributions 
380*2c2f96dcSApple OSS Distributions  To reuse the context for additional encryptions, follow this sequence:
381*2c2f96dcSApple OSS Distributions 
382*2c2f96dcSApple OSS Distributions  @code ccgcm_reset(...)
383*2c2f96dcSApple OSS Distributions  ccgcm_inc_iv(...)
384*2c2f96dcSApple OSS Distributions  ccgcm_aad(...)       (may be called zero or more times)
385*2c2f96dcSApple OSS Distributions  ccgcm_update(...)    (may be called zero or more times)
386*2c2f96dcSApple OSS Distributions  ccgcm_finalize(...)
387*2c2f96dcSApple OSS Distributions 
388*2c2f96dcSApple OSS Distributions  The IV must be exactly 12 bytes in length.
389*2c2f96dcSApple OSS Distributions 
390*2c2f96dcSApple OSS Distributions  Internally, the IV is treated as a four-byte salt followed by an eight-byte counter. This is to match the behavior of certain
391*2c2f96dcSApple OSS Distributions  protocols (e.g. TLS). In the call to @p ccgcm_inc_iv, the counter component will be interpreted as a big-endian, unsigned value
392*2c2f96dcSApple OSS Distributions  and incremented in place.
393*2c2f96dcSApple OSS Distributions 
394*2c2f96dcSApple OSS Distributions  @warning It is not permitted to call @p ccgcm_set_iv after initializing the cipher via the @p ccgcm_init_with_iv interface.
395*2c2f96dcSApple OSS Distributions  Nonzero is returned in the event of an improper call sequence.
396*2c2f96dcSApple OSS Distributions 
397*2c2f96dcSApple OSS Distributions  @warning The security of GCM depends on the uniqueness of key-IV pairs. To avoid key-IV repetition, callers should not initialize
398*2c2f96dcSApple OSS Distributions  multiple contexts with the same key material via the @p ccgcm_init_with_iv interface.
399*2c2f96dcSApple OSS Distributions  */
400*2c2f96dcSApple OSS Distributions int ccgcm_init_with_iv(const struct ccmode_gcm *mode, ccgcm_ctx *ctx, size_t key_nbytes, const void *cc_sized_by(key_nbytes) key, const void *cc_unsafe_indexable iv);
401*2c2f96dcSApple OSS Distributions 
402*2c2f96dcSApple OSS Distributions /*!
403*2c2f96dcSApple OSS Distributions  @function   ccgcm_set_iv
404*2c2f96dcSApple OSS Distributions  @abstract   Set the IV for encryption.
405*2c2f96dcSApple OSS Distributions 
406*2c2f96dcSApple OSS Distributions  @param      mode       Descriptor for the mode
407*2c2f96dcSApple OSS Distributions  @param      ctx        Context for this instance
408*2c2f96dcSApple OSS Distributions  @param      iv_nbytes  Length of the IV in bytes
409*2c2f96dcSApple OSS Distributions  @param      iv         Initialization vector
410*2c2f96dcSApple OSS Distributions 
411*2c2f96dcSApple OSS Distributions  @result     0 iff successful.
412*2c2f96dcSApple OSS Distributions 
413*2c2f96dcSApple OSS Distributions  @discussion Set the initialization vector for encryption.
414*2c2f96dcSApple OSS Distributions 
415*2c2f96dcSApple OSS Distributions  @warning The key-IV pair must be unique per encryption. The IV must be nonzero in length.
416*2c2f96dcSApple OSS Distributions 
417*2c2f96dcSApple OSS Distributions  In stateful protocols, if each packet exposes a guaranteed-unique value, it is recommended to format this as a 12-byte value for
418*2c2f96dcSApple OSS Distributions  use as the IV.
419*2c2f96dcSApple OSS Distributions 
420*2c2f96dcSApple OSS Distributions  In stateless protocols, it is recommended to choose a 16-byte value using a cryptographically-secure pseudorandom number
421*2c2f96dcSApple OSS Distributions  generator (e.g. @p ccrng).
422*2c2f96dcSApple OSS Distributions 
423*2c2f96dcSApple OSS Distributions  @warning This function may not be used after initializing the cipher via @p ccgcm_init_with_iv. Nonzero is returned in the event
424*2c2f96dcSApple OSS Distributions  of an improper call sequence.
425*2c2f96dcSApple OSS Distributions 
426*2c2f96dcSApple OSS Distributions  @warning This function is not FIPS-compliant. Use @p ccgcm_init_with_iv instead.
427*2c2f96dcSApple OSS Distributions  */
428*2c2f96dcSApple OSS Distributions int ccgcm_set_iv(const struct ccmode_gcm *mode, ccgcm_ctx *ctx, size_t iv_nbytes, const void *cc_sized_by(iv_nbytes) iv);
429*2c2f96dcSApple OSS Distributions 
430*2c2f96dcSApple OSS Distributions /*!
431*2c2f96dcSApple OSS Distributions  @function   ccgcm_set_iv_legacy
432*2c2f96dcSApple OSS Distributions  @abstract   Set the IV for encryption.
433*2c2f96dcSApple OSS Distributions 
434*2c2f96dcSApple OSS Distributions  @param      mode       Descriptor for the mode
435*2c2f96dcSApple OSS Distributions  @param      ctx        Context for this instance
436*2c2f96dcSApple OSS Distributions  @param      iv_nbytes  Length of the IV in bytes
437*2c2f96dcSApple OSS Distributions  @param      iv         Initialization vector
438*2c2f96dcSApple OSS Distributions 
439*2c2f96dcSApple OSS Distributions  @result     0 iff successful.
440*2c2f96dcSApple OSS Distributions 
441*2c2f96dcSApple OSS Distributions  @discussion Identical to @p ccgcm_set_iv except that it allows zero-length IVs.
442*2c2f96dcSApple OSS Distributions 
443*2c2f96dcSApple OSS Distributions  @warning Zero-length IVs nullify the authenticity guarantees of GCM.
444*2c2f96dcSApple OSS Distributions 
445*2c2f96dcSApple OSS Distributions  @warning Do not use this function in new applications.
446*2c2f96dcSApple OSS Distributions  */
447*2c2f96dcSApple OSS Distributions int ccgcm_set_iv_legacy(const struct ccmode_gcm *mode, ccgcm_ctx *ctx, size_t iv_nbytes, const void *cc_sized_by(iv_nbytes) iv);
448*2c2f96dcSApple OSS Distributions 
449*2c2f96dcSApple OSS Distributions /*!
450*2c2f96dcSApple OSS Distributions  @function   ccgcm_inc_iv
451*2c2f96dcSApple OSS Distributions  @abstract   Increment the IV for another encryption.
452*2c2f96dcSApple OSS Distributions 
453*2c2f96dcSApple OSS Distributions  @param      mode       Descriptor for the mode
454*2c2f96dcSApple OSS Distributions  @param      ctx        Context for this instance
455*2c2f96dcSApple OSS Distributions  @param      iv         Updated initialization vector
456*2c2f96dcSApple OSS Distributions 
457*2c2f96dcSApple OSS Distributions  @result     0 iff successful.
458*2c2f96dcSApple OSS Distributions 
459*2c2f96dcSApple OSS Distributions  @discussion Updates the IV internally for another encryption.
460*2c2f96dcSApple OSS Distributions 
461*2c2f96dcSApple OSS Distributions  Internally, the IV is treated as a four-byte salt followed by an eight-byte counter. This is to match the behavior of certain
462*2c2f96dcSApple OSS Distributions  protocols (e.g. TLS). The counter component is interpreted as a big-endian, unsigned value and incremented in place.
463*2c2f96dcSApple OSS Distributions 
464*2c2f96dcSApple OSS Distributions  The updated IV is copied to @p iv. This is to support protocols that require part of the IV to be specified explicitly in each
465*2c2f96dcSApple OSS Distributions  packet (e.g. TLS).
466*2c2f96dcSApple OSS Distributions 
467*2c2f96dcSApple OSS Distributions  @warning This function may be used only after initializing the cipher via @p ccgcm_init_with_iv.
468*2c2f96dcSApple OSS Distributions  */
469*2c2f96dcSApple OSS Distributions int ccgcm_inc_iv(const struct ccmode_gcm *mode, ccgcm_ctx *ctx, void *cc_unsafe_indexable iv);
470*2c2f96dcSApple OSS Distributions 
471*2c2f96dcSApple OSS Distributions /*!
472*2c2f96dcSApple OSS Distributions  @function   ccgcm_aad
473*2c2f96dcSApple OSS Distributions  @abstract   Authenticate additional data.
474*2c2f96dcSApple OSS Distributions 
475*2c2f96dcSApple OSS Distributions  @param      mode               Descriptor for the mode
476*2c2f96dcSApple OSS Distributions  @param      ctx                Context for this instance
477*2c2f96dcSApple OSS Distributions  @param      nbytes             Length of the additional data in bytes
478*2c2f96dcSApple OSS Distributions  @param      additional_data    Additional data to authenticate
479*2c2f96dcSApple OSS Distributions 
480*2c2f96dcSApple OSS Distributions  @result     0 iff successful.
481*2c2f96dcSApple OSS Distributions 
482*2c2f96dcSApple OSS Distributions  @discussion This is typically used to authenticate data that cannot be encrypted (e.g. packet headers).
483*2c2f96dcSApple OSS Distributions 
484*2c2f96dcSApple OSS Distributions  This function may be called zero or more times.
485*2c2f96dcSApple OSS Distributions  */
486*2c2f96dcSApple OSS Distributions int ccgcm_aad(const struct ccmode_gcm *mode, ccgcm_ctx *ctx, size_t nbytes, const void *cc_sized_by(nbytes) additional_data);
487*2c2f96dcSApple OSS Distributions 
488*2c2f96dcSApple OSS Distributions /*!
489*2c2f96dcSApple OSS Distributions  @function   ccgcm_gmac
490*2c2f96dcSApple OSS Distributions 
491*2c2f96dcSApple OSS Distributions  @discussion ccgcm_gmac is deprecated. Use the drop-in replacement 'ccgcm_aad' instead.
492*2c2f96dcSApple OSS Distributions  */
493*2c2f96dcSApple OSS Distributions int ccgcm_gmac(const struct ccmode_gcm *mode, ccgcm_ctx *ctx, size_t nbytes, const void *cc_sized_by(nbytes) in)
494*2c2f96dcSApple OSS Distributions cc_deprecate_with_replacement("ccgcm_aad", 13.0, 10.15, 13.0, 6.0, 4.0);
495*2c2f96dcSApple OSS Distributions 
496*2c2f96dcSApple OSS Distributions /*!
497*2c2f96dcSApple OSS Distributions  @function   ccgcm_update
498*2c2f96dcSApple OSS Distributions  @abstract   Encrypt or decrypt data.
499*2c2f96dcSApple OSS Distributions 
500*2c2f96dcSApple OSS Distributions  @param      mode       Descriptor for the mode
501*2c2f96dcSApple OSS Distributions  @param      ctx        Context for this instance
502*2c2f96dcSApple OSS Distributions  @param      nbytes     Length of the data in bytes
503*2c2f96dcSApple OSS Distributions  @param      in         Input plaintext or ciphertext
504*2c2f96dcSApple OSS Distributions  @param      out        Output ciphertext or plaintext
505*2c2f96dcSApple OSS Distributions 
506*2c2f96dcSApple OSS Distributions  @result     0 iff successful.
507*2c2f96dcSApple OSS Distributions 
508*2c2f96dcSApple OSS Distributions  @discussion In-place processing is supported.
509*2c2f96dcSApple OSS Distributions 
510*2c2f96dcSApple OSS Distributions  This function may be called zero or more times.
511*2c2f96dcSApple OSS Distributions  */
512*2c2f96dcSApple OSS Distributions int ccgcm_update(const struct ccmode_gcm *mode, ccgcm_ctx *ctx, size_t nbytes, const void *cc_sized_by(nbytes) in, void *cc_sized_by(nbytes) out);
513*2c2f96dcSApple OSS Distributions 
514*2c2f96dcSApple OSS Distributions /*!
515*2c2f96dcSApple OSS Distributions  @function   ccgcm_finalize
516*2c2f96dcSApple OSS Distributions  @abstract   Finish processing and authenticate.
517*2c2f96dcSApple OSS Distributions 
518*2c2f96dcSApple OSS Distributions  @param      mode       Descriptor for the mode
519*2c2f96dcSApple OSS Distributions  @param      ctx        Context for this instance
520*2c2f96dcSApple OSS Distributions  @param      tag_nbytes Length of the tag in bytes
521*2c2f96dcSApple OSS Distributions  @param      tag        Authentication tag
522*2c2f96dcSApple OSS Distributions 
523*2c2f96dcSApple OSS Distributions  @result     0 iff successful.
524*2c2f96dcSApple OSS Distributions 
525*2c2f96dcSApple OSS Distributions  @discussion Finish processing a packet and generate the authentication tag.
526*2c2f96dcSApple OSS Distributions 
527*2c2f96dcSApple OSS Distributions  On encryption, @p tag is purely an output parameter. The generated tag is written to @p tag.
528*2c2f96dcSApple OSS Distributions 
529*2c2f96dcSApple OSS Distributions  On decryption, @p tag is both an input and an output parameter. Well-behaved callers should provide the authentication tag
530*2c2f96dcSApple OSS Distributions  generated during encryption. The function will return nonzero if the input tag does not match the generated tag. The generated
531*2c2f96dcSApple OSS Distributions  tag will be written into the @p tag buffer whether authentication succeeds or fails.
532*2c2f96dcSApple OSS Distributions 
533*2c2f96dcSApple OSS Distributions  @warning The generated tag is written to @p tag to support legacy applications that perform authentication manually. Do not
534*2c2f96dcSApple OSS Distributions  follow this usage pattern in new applications. Rely on the function's error code to verify authenticity.
535*2c2f96dcSApple OSS Distributions  */
536*2c2f96dcSApple OSS Distributions int ccgcm_finalize(const struct ccmode_gcm *mode, ccgcm_ctx *ctx, size_t tag_nbytes, void *cc_sized_by(tag_nbytes) tag);
537*2c2f96dcSApple OSS Distributions 
538*2c2f96dcSApple OSS Distributions /*!
539*2c2f96dcSApple OSS Distributions  @function   ccgcm_reset
540*2c2f96dcSApple OSS Distributions  @abstract   Reset the context for another encryption.
541*2c2f96dcSApple OSS Distributions 
542*2c2f96dcSApple OSS Distributions  @param      mode       Descriptor for the mode
543*2c2f96dcSApple OSS Distributions  @param      ctx        Context for this instance
544*2c2f96dcSApple OSS Distributions 
545*2c2f96dcSApple OSS Distributions  @result     0 iff successful.
546*2c2f96dcSApple OSS Distributions 
547*2c2f96dcSApple OSS Distributions  @discussion Refer to @p ccgcm_init for correct usage.
548*2c2f96dcSApple OSS Distributions  */
549*2c2f96dcSApple OSS Distributions int ccgcm_reset(const struct ccmode_gcm *mode, ccgcm_ctx *ctx);
550*2c2f96dcSApple OSS Distributions 
551*2c2f96dcSApple OSS Distributions /*!
552*2c2f96dcSApple OSS Distributions  @function   ccgcm_one_shot
553*2c2f96dcSApple OSS Distributions  @abstract   Encrypt or decrypt with GCM.
554*2c2f96dcSApple OSS Distributions 
555*2c2f96dcSApple OSS Distributions  @param      mode           Descriptor for the mode
556*2c2f96dcSApple OSS Distributions  @param      key_nbytes     Length of the key in bytes
557*2c2f96dcSApple OSS Distributions  @param      key            Key for the underlying blockcipher (AES)
558*2c2f96dcSApple OSS Distributions  @param      iv_nbytes      Length of the IV in bytes
559*2c2f96dcSApple OSS Distributions  @param      iv             Initialization vector
560*2c2f96dcSApple OSS Distributions  @param      adata_nbytes   Length of the additional data in bytes
561*2c2f96dcSApple OSS Distributions  @param      adata          Additional data to authenticate
562*2c2f96dcSApple OSS Distributions  @param      nbytes         Length of the data in bytes
563*2c2f96dcSApple OSS Distributions  @param      in             Input plaintext or ciphertext
564*2c2f96dcSApple OSS Distributions  @param      out            Output ciphertext or plaintext
565*2c2f96dcSApple OSS Distributions  @param      tag_nbytes     Length of the tag in bytes
566*2c2f96dcSApple OSS Distributions  @param      tag            Authentication tag
567*2c2f96dcSApple OSS Distributions 
568*2c2f96dcSApple OSS Distributions  @result     0 iff successful.
569*2c2f96dcSApple OSS Distributions 
570*2c2f96dcSApple OSS Distributions  @discussion Perform GCM encryption or decryption.
571*2c2f96dcSApple OSS Distributions 
572*2c2f96dcSApple OSS Distributions  @warning The key-IV pair must be unique per encryption. The IV must be nonzero in length.
573*2c2f96dcSApple OSS Distributions 
574*2c2f96dcSApple OSS Distributions  In stateful protocols, if each packet exposes a guaranteed-unique value, it is recommended to format this as a 12-byte value for
575*2c2f96dcSApple OSS Distributions  use as the IV.
576*2c2f96dcSApple OSS Distributions 
577*2c2f96dcSApple OSS Distributions  In stateless protocols, it is recommended to choose a 16-byte value using a cryptographically-secure pseudorandom number
578*2c2f96dcSApple OSS Distributions  generator (e.g. @p ccrng).
579*2c2f96dcSApple OSS Distributions 
580*2c2f96dcSApple OSS Distributions  In-place processing is supported.
581*2c2f96dcSApple OSS Distributions 
582*2c2f96dcSApple OSS Distributions  On encryption, @p tag is purely an output parameter. The generated tag is written to @p tag.
583*2c2f96dcSApple OSS Distributions 
584*2c2f96dcSApple OSS Distributions  On decryption, @p tag is primarily an input parameter. The caller should provide the authentication tag generated during
585*2c2f96dcSApple OSS Distributions  encryption. The function will return nonzero if the input tag does not match the generated tag.
586*2c2f96dcSApple OSS Distributions 
587*2c2f96dcSApple OSS Distributions  @warning To support legacy applications, @p tag is also an output parameter during decryption. The generated tag is written to @p
588*2c2f96dcSApple OSS Distributions  tag. Legacy callers may choose to compare this to the tag generated during encryption. Do not follow this usage pattern in new
589*2c2f96dcSApple OSS Distributions  applications.
590*2c2f96dcSApple OSS Distributions  */
591*2c2f96dcSApple OSS Distributions int ccgcm_one_shot(const struct ccmode_gcm *mode,
592*2c2f96dcSApple OSS Distributions                    size_t key_nbytes,
593*2c2f96dcSApple OSS Distributions                    const void *cc_sized_by(key_nbytes) key,
594*2c2f96dcSApple OSS Distributions                    size_t iv_nbytes,
595*2c2f96dcSApple OSS Distributions                    const void *cc_sized_by(iv_nbytes) iv,
596*2c2f96dcSApple OSS Distributions                    size_t adata_nbytes,
597*2c2f96dcSApple OSS Distributions                    const void *cc_sized_by(adata_nbytes) adata,
598*2c2f96dcSApple OSS Distributions                    size_t nbytes,
599*2c2f96dcSApple OSS Distributions                    const void *cc_sized_by(nbytes) in,
600*2c2f96dcSApple OSS Distributions                    void *cc_sized_by(nbytes) out,
601*2c2f96dcSApple OSS Distributions                    size_t tag_nbytes,
602*2c2f96dcSApple OSS Distributions                    void *cc_sized_by(tag_nbytes) tag);
603*2c2f96dcSApple OSS Distributions 
604*2c2f96dcSApple OSS Distributions /*!
605*2c2f96dcSApple OSS Distributions  @function   ccgcm_one_shot_legacy
606*2c2f96dcSApple OSS Distributions  @abstract   Encrypt or decrypt with GCM.
607*2c2f96dcSApple OSS Distributions 
608*2c2f96dcSApple OSS Distributions  @param      mode           Descriptor for the mode
609*2c2f96dcSApple OSS Distributions  @param      key_nbytes     Length of the key in bytes
610*2c2f96dcSApple OSS Distributions  @param      key            Key for the underlying blockcipher (AES)
611*2c2f96dcSApple OSS Distributions  @param      iv_nbytes      Length of the IV in bytes
612*2c2f96dcSApple OSS Distributions  @param      iv             Initialization vector
613*2c2f96dcSApple OSS Distributions  @param      adata_nbytes   Length of the additional data in bytes
614*2c2f96dcSApple OSS Distributions  @param      adata          Additional data to authenticate
615*2c2f96dcSApple OSS Distributions  @param      nbytes         Length of the data in bytes
616*2c2f96dcSApple OSS Distributions  @param      in             Input plaintext or ciphertext
617*2c2f96dcSApple OSS Distributions  @param      out            Output ciphertext or plaintext
618*2c2f96dcSApple OSS Distributions  @param      tag_nbytes     Length of the tag in bytes
619*2c2f96dcSApple OSS Distributions  @param      tag            Authentication tag
620*2c2f96dcSApple OSS Distributions 
621*2c2f96dcSApple OSS Distributions  @result     0 iff successful.
622*2c2f96dcSApple OSS Distributions 
623*2c2f96dcSApple OSS Distributions  @discussion Identical to @p ccgcm_one_shot except that it allows zero-length IVs.
624*2c2f96dcSApple OSS Distributions 
625*2c2f96dcSApple OSS Distributions  @warning Zero-length IVs nullify the authenticity guarantees of GCM.
626*2c2f96dcSApple OSS Distributions 
627*2c2f96dcSApple OSS Distributions  @warning Do not use this function in new applications.
628*2c2f96dcSApple OSS Distributions  */
629*2c2f96dcSApple OSS Distributions int ccgcm_one_shot_legacy(const struct ccmode_gcm *mode,
630*2c2f96dcSApple OSS Distributions                           size_t key_nbytes,
631*2c2f96dcSApple OSS Distributions                           const void *cc_sized_by(key_nbytes) key,
632*2c2f96dcSApple OSS Distributions                           size_t iv_nbytes,
633*2c2f96dcSApple OSS Distributions                           const void *cc_sized_by(iv_nbytes) iv,
634*2c2f96dcSApple OSS Distributions                           size_t adata_nbytes,
635*2c2f96dcSApple OSS Distributions                           const void *cc_sized_by(adata_nbytes) adata,
636*2c2f96dcSApple OSS Distributions                           size_t nbytes,
637*2c2f96dcSApple OSS Distributions                           const void *cc_sized_by(nbytes) in,
638*2c2f96dcSApple OSS Distributions                           void *cc_sized_by(nbytes) out,
639*2c2f96dcSApple OSS Distributions                           size_t tag_nbytes,
640*2c2f96dcSApple OSS Distributions                           void *cc_sized_by(tag_nbytes) tag);
641*2c2f96dcSApple OSS Distributions 
642*2c2f96dcSApple OSS Distributions /* CCM */
643*2c2f96dcSApple OSS Distributions #define CCM_MAX_TAG_SIZE 16
644*2c2f96dcSApple OSS Distributions #define ccccm_ctx_decl(_size_, _name_) cc_ctx_decl_vla(ccccm_ctx, _size_, _name_)
645*2c2f96dcSApple OSS Distributions #define ccccm_ctx_clear(_size_, _name_) cc_clear(_size_, _name_)
646*2c2f96dcSApple OSS Distributions 
647*2c2f96dcSApple OSS Distributions /* Declare a ccm nonce named _name_.  Pass the mode->nonce_ctx_size for _size_. */
648*2c2f96dcSApple OSS Distributions #define ccccm_nonce_decl(_size_, _name_) cc_ctx_decl_vla(ccccm_nonce, _size_, _name_)
649*2c2f96dcSApple OSS Distributions #define ccccm_nonce_clear(_size_, _name_) cc_clear(_size_, _name_)
650*2c2f96dcSApple OSS Distributions 
651*2c2f96dcSApple OSS Distributions size_t ccccm_context_size(const struct ccmode_ccm *mode);
652*2c2f96dcSApple OSS Distributions 
653*2c2f96dcSApple OSS Distributions size_t ccccm_block_size(const struct ccmode_ccm *mode);
654*2c2f96dcSApple OSS Distributions 
655*2c2f96dcSApple OSS Distributions /// Initialize a ccm authenticated encryption/decryption mode
656*2c2f96dcSApple OSS Distributions /// @param mode mode descriptor
657*2c2f96dcSApple OSS Distributions /// @param ctx  context for this instance
658*2c2f96dcSApple OSS Distributions /// @param key_len length in bytes of key provided
659*2c2f96dcSApple OSS Distributions /// @param key bytes defining key
660*2c2f96dcSApple OSS Distributions int ccccm_init(const struct ccmode_ccm *mode, ccccm_ctx *ctx, size_t key_len, const void *cc_sized_by(key_len) key);
661*2c2f96dcSApple OSS Distributions 
662*2c2f96dcSApple OSS Distributions /// Set the initialization value/nonce for the ccm authenticated encryption/decryption
663*2c2f96dcSApple OSS Distributions /// @param mode mode descriptor
664*2c2f96dcSApple OSS Distributions /// @param ctx context for this ccm instance
665*2c2f96dcSApple OSS Distributions /// @param nonce_ctx  context for this nonce
666*2c2f96dcSApple OSS Distributions /// @param nonce_len length in bytes of cmac nonce/iv
667*2c2f96dcSApple OSS Distributions /// @param nonce bytes defining none
668*2c2f96dcSApple OSS Distributions /// @param mac_size length in bytes of mac tag
669*2c2f96dcSApple OSS Distributions /// @param auth_len length in bytes of authenticating data
670*2c2f96dcSApple OSS Distributions /// @param data_len length in bytes of plaintext
671*2c2f96dcSApple OSS Distributions int ccccm_set_iv(const struct ccmode_ccm *mode,
672*2c2f96dcSApple OSS Distributions                  ccccm_ctx *ctx,
673*2c2f96dcSApple OSS Distributions                  ccccm_nonce *nonce_ctx,
674*2c2f96dcSApple OSS Distributions                  size_t nonce_len,
675*2c2f96dcSApple OSS Distributions                  const void *cc_sized_by(nonce_len) nonce,
676*2c2f96dcSApple OSS Distributions                  size_t mac_size,
677*2c2f96dcSApple OSS Distributions                  size_t auth_len,
678*2c2f96dcSApple OSS Distributions                  size_t data_len);
679*2c2f96dcSApple OSS Distributions 
680*2c2f96dcSApple OSS Distributions /// (Deprecated) Add associated data to the ccm authenticated encryption/decryption
681*2c2f96dcSApple OSS Distributions /// @param mode mode descriptor
682*2c2f96dcSApple OSS Distributions /// @param ctx context for this ccm instance
683*2c2f96dcSApple OSS Distributions /// @param nonce_ctx  context for this nonce
684*2c2f96dcSApple OSS Distributions /// @param nbytes nbytes length in bytes of associated data being provided in this invocation
685*2c2f96dcSApple OSS Distributions /// @param in authenticated data being provided in this invocation
686*2c2f96dcSApple OSS Distributions int ccccm_cbcmac(const struct ccmode_ccm *mode, ccccm_ctx *ctx, ccccm_nonce *nonce_ctx, size_t nbytes, const void *cc_sized_by(nbytes) in);
687*2c2f96dcSApple OSS Distributions 
688*2c2f96dcSApple OSS Distributions ///Add associated data to the ccm authenticated encryption/decryption
689*2c2f96dcSApple OSS Distributions /// @param mode mode descriptor
690*2c2f96dcSApple OSS Distributions /// @param ctx context for this ccm instance
691*2c2f96dcSApple OSS Distributions /// @param nonce_ctx  context for this nonce
692*2c2f96dcSApple OSS Distributions /// @param ad_nbytes nbytes length in bytes of associated data being provided in this invocation
693*2c2f96dcSApple OSS Distributions /// @param ad authenticated data being provided in this invocation
694*2c2f96dcSApple OSS Distributions int ccccm_aad(const struct ccmode_ccm *mode, ccccm_ctx *ctx, ccccm_nonce *nonce_ctx, size_t ad_nbytes, const uint8_t *cc_sized_by(ad_nbytes) ad);
695*2c2f96dcSApple OSS Distributions 
696*2c2f96dcSApple OSS Distributions /// Add plaintext data to the ccm authenticated encryption/decryption
697*2c2f96dcSApple OSS Distributions /// @param mode mode descriptor
698*2c2f96dcSApple OSS Distributions /// @param ctx context for this ccm instance
699*2c2f96dcSApple OSS Distributions /// @param nonce_ctx  context for this nonce
700*2c2f96dcSApple OSS Distributions /// @param nbytes length in bytes of both plaintext and encrypted plaintext
701*2c2f96dcSApple OSS Distributions /// @param in In encryption mode plaintext data, in decryption mode encrypted plaintext data.
702*2c2f96dcSApple OSS Distributions /// @param out in encryption mode resulting encrypted plaintext data. In decryption mode resulting plaintext data
703*2c2f96dcSApple OSS Distributions int ccccm_update(const struct ccmode_ccm *mode, ccccm_ctx *ctx, ccccm_nonce *nonce_ctx, size_t nbytes, const void *cc_sized_by(nbytes) in, void *cc_sized_by(nbytes) out);
704*2c2f96dcSApple OSS Distributions 
705*2c2f96dcSApple OSS Distributions /// Add plaintext data to the ccm authenticated encryption
706*2c2f96dcSApple OSS Distributions /// @param mode mode descriptor
707*2c2f96dcSApple OSS Distributions /// @param ctx context for this ccm instance
708*2c2f96dcSApple OSS Distributions /// @param nonce_ctx  context for this nonce
709*2c2f96dcSApple OSS Distributions /// @param nbytes length in bytes of both plaintext and encrypted plaintext
710*2c2f96dcSApple OSS Distributions /// @param plaintext In encryption mode plaintext data, in decryption mode encrypted plaintext data.
711*2c2f96dcSApple OSS Distributions /// @param encrypted_plaintext in encryption mode resulting encrypted plaintext data. In decryption mode resulting plaintext data
712*2c2f96dcSApple OSS Distributions int ccccm_encrypt(const struct ccmode_ccm *mode, ccccm_ctx *ctx, ccccm_nonce *nonce_ctx, size_t nbytes, const uint8_t *cc_sized_by(nbytes) plaintext, uint8_t *cc_sized_by(nbytes) encrypted_plaintext);
713*2c2f96dcSApple OSS Distributions 
714*2c2f96dcSApple OSS Distributions /// Add ciphertext data to the ccm authenticated decryption
715*2c2f96dcSApple OSS Distributions /// @param mode mode descriptor
716*2c2f96dcSApple OSS Distributions /// @param ctx context for this ccm instance
717*2c2f96dcSApple OSS Distributions /// @param nonce_ctx  context for this nonce
718*2c2f96dcSApple OSS Distributions /// @param nbytes length in bytes of both plaintext and encrypted plaintext
719*2c2f96dcSApple OSS Distributions /// @param encrypted_plaintext In encryption mode plaintext data, in decryption mode encrypted plaintext data.
720*2c2f96dcSApple OSS Distributions /// @param plaintext in encryption mode resulting encrypted plaintext data. In decryption mode resulting plaintext data
721*2c2f96dcSApple OSS Distributions int ccccm_decrypt(const struct ccmode_ccm *mode, ccccm_ctx *ctx, ccccm_nonce *nonce_ctx, size_t nbytes, const uint8_t *cc_sized_by(nbytes) encrypted_plaintext, uint8_t *cc_sized_by(nbytes) plaintext);
722*2c2f96dcSApple OSS Distributions 
723*2c2f96dcSApple OSS Distributions 
724*2c2f96dcSApple OSS Distributions /// (Deprecated) Compute tag for ccm
725*2c2f96dcSApple OSS Distributions /// @param mode mode descriptor
726*2c2f96dcSApple OSS Distributions /// @param ctx context for this ccm instance
727*2c2f96dcSApple OSS Distributions /// @param nonce_ctx  context for this nonce
728*2c2f96dcSApple OSS Distributions /// @param mac tag portion of ciphertext that is computed from ccm MAC.
729*2c2f96dcSApple OSS Distributions /// @discussion This is being deprecated, as it requires the caller to manually verify that the returned mac tag is correct when decrypting. Please use ccccm_finalize_and_verify instead.
730*2c2f96dcSApple OSS Distributions int ccccm_finalize(const struct ccmode_ccm *mode, ccccm_ctx *ctx, ccccm_nonce *nonce_ctx, void *cc_indexable mac);
731*2c2f96dcSApple OSS Distributions 
732*2c2f96dcSApple OSS Distributions /// Ends encryption and computes tag when in encryption mode
733*2c2f96dcSApple OSS Distributions /// @param mode mode descriptor
734*2c2f96dcSApple OSS Distributions /// @param ctx context for this ccm instance
735*2c2f96dcSApple OSS Distributions /// @param nonce_ctx  context for this nonce
736*2c2f96dcSApple OSS Distributions /// @param mac For encryption mode the resulting mac tag portion of the ciphertext is copied to this buffer. For decryption mode, it provides an input of the expected tag in the ciphertext
737*2c2f96dcSApple OSS Distributions /// @return For decryption returns CCERR_OK if the provided mac matches the computed mac, and otherwise returns CCMODE_INTEGRITY_FAILURE.
738*2c2f96dcSApple OSS Distributions int ccccm_finalize_and_generate_tag(const struct ccmode_ccm *mode, ccccm_ctx *ctx, ccccm_nonce *nonce_ctx, uint8_t *cc_indexable mac);
739*2c2f96dcSApple OSS Distributions 
740*2c2f96dcSApple OSS Distributions /// Ends decryption and verifies tag when in decryption mode
741*2c2f96dcSApple OSS Distributions /// @param mode mode descriptor
742*2c2f96dcSApple OSS Distributions /// @param ctx context for this ccm instance
743*2c2f96dcSApple OSS Distributions /// @param nonce_ctx  context for this nonce
744*2c2f96dcSApple OSS Distributions /// @param mac It provides an input of the expected tag in the ciphertext
745*2c2f96dcSApple OSS Distributions /// @return Returns CCERR_OK if the provided mac matches the computed mac, and otherwise returns CCMODE_INTEGRITY_FAILURE.
746*2c2f96dcSApple OSS Distributions int ccccm_finalize_and_verify_tag(const struct ccmode_ccm *mode, ccccm_ctx *ctx, ccccm_nonce *nonce_ctx, const uint8_t *cc_indexable mac);
747*2c2f96dcSApple OSS Distributions 
748*2c2f96dcSApple OSS Distributions /// Resets the state of the encryptor/decryptor, maintaining the key, but clearing the nonce/iv, allowing for a new encryption or decryption
749*2c2f96dcSApple OSS Distributions /// @param mode mode descriptor
750*2c2f96dcSApple OSS Distributions /// @param ctx context for this ccm instance
751*2c2f96dcSApple OSS Distributions /// @param nonce_ctx  context for this nonce
752*2c2f96dcSApple OSS Distributions int ccccm_reset(const struct ccmode_ccm *mode, ccccm_ctx *ctx, ccccm_nonce *nonce_ctx);
753*2c2f96dcSApple OSS Distributions 
754*2c2f96dcSApple OSS Distributions /// (Deprecated) Encrypts/Decrypts a plaintext/ciphertext using the AEAD CCM mode.
755*2c2f96dcSApple OSS Distributions /// @param mode mode descriptor
756*2c2f96dcSApple OSS Distributions /// @param key_len key length in bytes
757*2c2f96dcSApple OSS Distributions /// @param key buffer holding key
758*2c2f96dcSApple OSS Distributions /// @param nonce_len nonce length in bytes
759*2c2f96dcSApple OSS Distributions /// @param nonce buffer holding nonce
760*2c2f96dcSApple OSS Distributions /// @param nbytes the length of the plaintext and encrypted-plaintext
761*2c2f96dcSApple OSS Distributions /// @param in buffer holding plaintext in encryption mode, and encrypted plaintext portion of ciphertext in decryption mode
762*2c2f96dcSApple OSS Distributions /// @param out buffer receiving resulting encrypted plaintext in encryption mode, and resulting plaintext in decryption mode
763*2c2f96dcSApple OSS Distributions /// @param adata_len length in bytes of associated data
764*2c2f96dcSApple OSS Distributions /// @param adata authenticated data being provided in this invocation.
765*2c2f96dcSApple OSS Distributions /// @param mac_size length in bytes of CCM mac tag
766*2c2f96dcSApple OSS Distributions /// @param mac portion of ciphertext that is computed from ccm MAC.
767*2c2f96dcSApple OSS Distributions /// @return This is being deprecated, as it requires the caller to manually  verify that the returned mac tag is correct when decrypting. Please use ccccm_one_shot_with_verify instead
768*2c2f96dcSApple OSS Distributions int ccccm_one_shot(const struct ccmode_ccm *mode,
769*2c2f96dcSApple OSS Distributions                    size_t key_len,
770*2c2f96dcSApple OSS Distributions                    const void *cc_sized_by(key_len) key,
771*2c2f96dcSApple OSS Distributions                    size_t nonce_len,
772*2c2f96dcSApple OSS Distributions                    const void *cc_sized_by(nonce_len) nonce,
773*2c2f96dcSApple OSS Distributions                    size_t nbytes,
774*2c2f96dcSApple OSS Distributions                    const void *cc_sized_by(nbytes) in,
775*2c2f96dcSApple OSS Distributions                    void *cc_sized_by(nbytes) out,
776*2c2f96dcSApple OSS Distributions                    size_t adata_len,
777*2c2f96dcSApple OSS Distributions                    const void *cc_sized_by(adata_len) adata,
778*2c2f96dcSApple OSS Distributions                    size_t mac_size,
779*2c2f96dcSApple OSS Distributions                    void *cc_sized_by(mac_size) mac);
780*2c2f96dcSApple OSS Distributions 
781*2c2f96dcSApple OSS Distributions /// Encrypts a plaintext using the AEAD CCM mode, and provides corresponding  mac tag. The encrypted plaintext and tag together are the AEAD ciphertext
782*2c2f96dcSApple OSS Distributions /// @param mode mode descriptor
783*2c2f96dcSApple OSS Distributions /// @param key_nbytes key length in bytes
784*2c2f96dcSApple OSS Distributions /// @param key buffer holding key
785*2c2f96dcSApple OSS Distributions /// @param nonce_nbytes nonce length in bytes
786*2c2f96dcSApple OSS Distributions /// @param nonce buffer holding nonce
787*2c2f96dcSApple OSS Distributions /// @param nbytes  the length of the plaintext and encrypted-plaintext
788*2c2f96dcSApple OSS Distributions /// @param plaintext buffer holding plaintext in encryption mode, and encrypted plaintext portion of ciphertext in decryption mode
789*2c2f96dcSApple OSS Distributions /// @param encrypted_plaintext buffer receiving resulting encrypted plaintext in encryption mode
790*2c2f96dcSApple OSS Distributions /// @param adata_nbytes length in bytes of associated data
791*2c2f96dcSApple OSS Distributions /// @param adata authenticated data being provided in this invocation.
792*2c2f96dcSApple OSS Distributions /// @param mac_tag_nbytes length in bytes of CCM mac tag
793*2c2f96dcSApple OSS Distributions /// @param mac_tag portion of ciphertext that is computed from ccm MAC.
794*2c2f96dcSApple OSS Distributions /// @return CERR_OK on successful encryption
795*2c2f96dcSApple OSS Distributions int ccccm_one_shot_encrypt(const struct ccmode_ccm *mode,
796*2c2f96dcSApple OSS Distributions                              size_t key_nbytes,
797*2c2f96dcSApple OSS Distributions                              const uint8_t *cc_sized_by(key_nbytes) key,
798*2c2f96dcSApple OSS Distributions                              size_t nonce_nbytes,
799*2c2f96dcSApple OSS Distributions                              const uint8_t *cc_sized_by(nonce_nbytes) nonce,
800*2c2f96dcSApple OSS Distributions                              size_t nbytes,
801*2c2f96dcSApple OSS Distributions                              const uint8_t *cc_sized_by(nbytes) plaintext,
802*2c2f96dcSApple OSS Distributions                              uint8_t *cc_sized_by(nbytes) encrypted_plaintext,
803*2c2f96dcSApple OSS Distributions                              size_t adata_nbytes,
804*2c2f96dcSApple OSS Distributions                              const uint8_t *cc_sized_by(adata_nbytes) adata,
805*2c2f96dcSApple OSS Distributions                              size_t mac_tag_nbytes,
806*2c2f96dcSApple OSS Distributions                              uint8_t *cc_sized_by(mac_tag_nbytes) mac_tag);
807*2c2f96dcSApple OSS Distributions 
808*2c2f96dcSApple OSS Distributions /// Decrypts a ciphertext using the AEAD CCM mode and ensures authenticity of the ciphertext. An AEAD CCM ciphertext consists of encrypted plaintext and mac tag
809*2c2f96dcSApple OSS Distributions /// @param mode mode descriptor
810*2c2f96dcSApple OSS Distributions /// @param key_nbytes key length in bytes
811*2c2f96dcSApple OSS Distributions /// @param key buffer holding key
812*2c2f96dcSApple OSS Distributions /// @param nonce_nbytes nonce length in bytes
813*2c2f96dcSApple OSS Distributions /// @param nonce buffer holding nonce
814*2c2f96dcSApple OSS Distributions /// @param nbytes  the length of the plaintext and encrypted-plaintext
815*2c2f96dcSApple OSS Distributions /// @param encrypted_plaintext buffer holding the encrypted plaintext portion of ciphertext
816*2c2f96dcSApple OSS Distributions /// @param plaintext buffer receiving resulting plaintext
817*2c2f96dcSApple OSS Distributions /// @param adata_nbytes length in bytes of associated data
818*2c2f96dcSApple OSS Distributions /// @param adata authenticated data being provided in this invocation.
819*2c2f96dcSApple OSS Distributions /// @param mac_tag_nbytes length in bytes of CCM mac tag
820*2c2f96dcSApple OSS Distributions /// @param mac_tag portion of ciphertext that is computed from ccm MAC.
821*2c2f96dcSApple OSS Distributions /// @return For decryption returns CCERR_OK if the provided mac matches the computed mac, and otherwise returns CCMODE_INTEGRITY_FAILURE.
822*2c2f96dcSApple OSS Distributions int ccccm_one_shot_decrypt(const struct ccmode_ccm *mode,
823*2c2f96dcSApple OSS Distributions                              size_t key_nbytes,
824*2c2f96dcSApple OSS Distributions                              const uint8_t *cc_sized_by(key_nbytes) key,
825*2c2f96dcSApple OSS Distributions                              size_t nonce_nbytes,
826*2c2f96dcSApple OSS Distributions                              const uint8_t *cc_sized_by(nonce_nbytes) nonce,
827*2c2f96dcSApple OSS Distributions                              size_t nbytes,
828*2c2f96dcSApple OSS Distributions                              const uint8_t *cc_sized_by(nbytes) encrypted_plaintext,
829*2c2f96dcSApple OSS Distributions                              uint8_t *cc_sized_by(nbytes) plaintext,
830*2c2f96dcSApple OSS Distributions                              size_t adata_nbytes,
831*2c2f96dcSApple OSS Distributions                              const uint8_t *cc_sized_by(adata_nbytes) adata,
832*2c2f96dcSApple OSS Distributions                              size_t mac_tag_nbytes,
833*2c2f96dcSApple OSS Distributions                              const uint8_t *cc_sized_by(mac_tag_nbytes) mac_tag);
834*2c2f96dcSApple OSS Distributions 
835*2c2f96dcSApple OSS Distributions /* OMAC mode. */
836*2c2f96dcSApple OSS Distributions 
837*2c2f96dcSApple OSS Distributions /* Declare a omac key named _name_.  Pass the size field of a struct ccmode_omac
838*2c2f96dcSApple OSS Distributions  for _size_. */
839*2c2f96dcSApple OSS Distributions #define ccomac_ctx_decl(_size_, _name_) cc_ctx_decl_vla(ccomac_ctx, _size_, _name_)
840*2c2f96dcSApple OSS Distributions #define ccomac_ctx_clear(_size_, _name_) cc_clear(_size_, _name_)
841*2c2f96dcSApple OSS Distributions 
842*2c2f96dcSApple OSS Distributions size_t ccomac_context_size(const struct ccmode_omac *mode);
843*2c2f96dcSApple OSS Distributions 
844*2c2f96dcSApple OSS Distributions size_t ccomac_block_size(const struct ccmode_omac *mode);
845*2c2f96dcSApple OSS Distributions 
846*2c2f96dcSApple OSS Distributions int ccomac_init(const struct ccmode_omac *mode, ccomac_ctx *ctx, size_t tweak_len, size_t key_len, const void *cc_sized_by(key_len) key);
847*2c2f96dcSApple OSS Distributions 
848*2c2f96dcSApple OSS Distributions int ccomac_update(const struct ccmode_omac *mode, ccomac_ctx *ctx, size_t nblocks, const void *tweak, const void *cc_indexable in, void *cc_indexable out);
849*2c2f96dcSApple OSS Distributions 
850*2c2f96dcSApple OSS Distributions int ccomac_one_shot(const struct ccmode_omac *mode,
851*2c2f96dcSApple OSS Distributions                     size_t tweak_len,
852*2c2f96dcSApple OSS Distributions                     size_t key_len,
853*2c2f96dcSApple OSS Distributions                     const void *cc_sized_by(key_len) key,
854*2c2f96dcSApple OSS Distributions                     const void *cc_sized_by(tweak_len) tweak,
855*2c2f96dcSApple OSS Distributions                     size_t nblocks,
856*2c2f96dcSApple OSS Distributions                     const void *cc_indexable in,
857*2c2f96dcSApple OSS Distributions                     void *cc_indexable out);
858*2c2f96dcSApple OSS Distributions 
859*2c2f96dcSApple OSS Distributions #endif /* _CORECRYPTO_CCMODE_H_ */
860