1/* Copyright (c) (2015-2022) Apple Inc. All rights reserved.
2 *
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.
10 */
11
12#ifndef _CORECRYPTO_CCMODE_SIV_H_
13#define _CORECRYPTO_CCMODE_SIV_H_
14
15#include <corecrypto/cc.h>
16#include <corecrypto/ccmode.h>
17#include <corecrypto/ccmode_impl.h>
18
19#include <corecrypto/cccmac.h>
20
21#define ccsiv_ctx_decl(_size_, _name_) cc_ctx_decl_vla(ccsiv_ctx, _size_, _name_)
22#define ccsiv_ctx_clear(_size_, _name_) cc_clear(_size_, _name_)
23
24// Functions
25
26size_t ccsiv_context_size(const struct ccmode_siv *mode);
27
28/*!
29@function ccsiv_block_size
30@abstract Return the block_size = block_length = tag_length used in the mode.
31
32@param mode ccsiv mode descriptor
33
34@discussion Used to return the current block size of the SIV mode. Note that the tag in this mode is an output of the underlying blockcipher and therefore the tag length corresponds to the block size.
35*/
36size_t ccsiv_block_size(const struct ccmode_siv *mode);
37
38/*!
39 @function ccsiv_ciphertext_size
40 @abstract Return size of Ciphertext (which is the ciphertext and corresponding tag) given the mode and plaintext length
41
42 @param mode ccsiv mode descriptor
43 @param plaintext_size Size of the plaintext
44
45 @discussion returns the length of the aead ciphertext that the context will generate which includes both the encrypted plaintext and tag.
46 */
47size_t ccsiv_ciphertext_size(const struct ccmode_siv *mode, size_t plaintext_size);
48
49/*!
50 @function ccsiv_plaintext_size
51 @abstract Return size of plaintext given a ciphertext length and mode.
52
53 @param mode ccsiv mode descriptor
54 @param ciphertext_size Size of the ciphertext
55
56 @discussion returns the length of the plaintext which results from the decryption of a ciphertext of the corresponding size (here ciphertext size includes the tag).
57 */
58
59size_t ccsiv_plaintext_size(const struct ccmode_siv *mode, size_t ciphertext_size);
60
61/*!
62 @function ccsiv_init
63 @abstract Initialize a context for ccsiv with an associated mode, and given key.
64
65 @param mode Descriptor for the mode
66 @param ctx Alocated context to be intialized
67 @param key_byte_len Length of the key: Supported key sizes are 32, 48, 64 bytes.
68 @param key key for siv. All bits of this key should be random. (See discussion)
69
70 @discussion In order to compute SIV_Enc_k(a1,...,am, n, x) where ai is the ith piece of associated data, n is a nonce and x is a plaintext, we use the following sequence of calls :
71
72
73 @code
74 ccsiv_init(...)
75 ccsiv_aad(...) (may be called zero or more times)
76 ccsiv_set_nonce(...)
77 ccsiv_crypt(...)
78 @endcode
79
80 To reuse the context for additional encryptions, follow this sequence:
81
82 @code
83 ccsiv_reset(...)
84 ccsiv_aad(...) (may be called zero or more times)
85 ccsiv_set_nonce(...)
86 ccsiv_crypt(...)
87 @endcode
88
89Importantly, all the bits in the key need to be random. Duplicating a smaller key to achieve a longer key length will result in an insecure implementation.
90 */
91int ccsiv_init(const struct ccmode_siv *mode, ccsiv_ctx *ctx,
92 size_t key_byte_len, const uint8_t *key);
93
94/*!
95 @function ccsiv_set_nonce
96 @abstract Add the nonce to the siv's computation of the the tag. Changes the internal state of the context
97 so that after the call only a crypt or reset call is permitted.
98
99 @param mode Descriptor for the mode
100 @param ctx Intialized ctx
101 @param nbytes Length of the current nonce data being added
102 @param in Nonce data to be authenticated.
103
104 @discussion The nonce is a special form of authenticated data. If provided (a call to ccsiv_set_nonce is optional) it allows
105 randomization of the ciphertext (preventing deterministic encryption). While the length of the nonce is not limmited, the
106 amount of entropy that can be provided is limited by the number of bits in the block of the associated block-cipher.
107 */
108int ccsiv_set_nonce(const struct ccmode_siv *mode, ccsiv_ctx *ctx,
109 size_t nbytes, const uint8_t *in);
110
111/*!
112 @function ccsiv_aad
113 @abstract Add the next piece of associated data to the SIV's computation of the tag.
114 @param mode Descriptor for the mode
115 @param ctx Intialized ctx
116 @param nbytes Length of the current associated data being added
117 @param in Associated data to be authenticated.
118
119 @discussion Adds the associated data given by in to the computation of the tag in the associated data. Note this call is optional and no associated data needs to be provided. Multiple pieces of associated data can be provided by multiple calls to this function. Note the associated data in this case is simply computed as the concatenation of all of the associated data inputs.
120 */
121int ccsiv_aad(const struct ccmode_siv *mode, ccsiv_ctx *ctx,
122 size_t nbytes, const uint8_t *in);
123
124/*!
125 @function ccsiv_crypt
126 @abstract Depdening on mode, 1) Encrypts a plaintext , or 2) Decrypts a ciphertext
127
128 @param mode Descriptor for the mode
129 @param ctx Intialized ctx
130 @param nbytes Case 1) Length of the current plaintext
131 Case 2) Length of the current ciphertext (block length + plaintext length).
132 @param in Case 1) Plaintext
133 Case 2) Ciphertext
134 @param out Case 1) Tag+ciphertext (buffer should be already allocated and of length block_length+plaintext_length.)
135 Case 2) Plaintext (buffer should be already allocated and of length ciphertext_length - block_length length
136
137 @discussion Depending on whether mode has been setup to encrypt or decrypt, this function
138 1) Encrypts the plaintext given as input in, and provides the ciphertext (which is a concatenation of the cbc-tag
139 followed by the encrypted plaintext) as output out. 2) Decrypts plaintext using the input ciphertext at in (which again is the cbc-tag, followed by encrypted plaintext), and then verifies that the computed tag and provided tags match.
140
141 This function is only called once. If one wishes to compute another (en)/(de)cryption, one resets the state with
142 ccsiv_reset, and then begins the process again. There is no way to stream large plaintext/ciphertext inputs into the
143 function.
144
145 In the case of a decryption, if there is a failure in verifying the computed tag against the provided tag (embedded int he ciphertext), then a decryption/verification
146 failure is returned, and any internally computed plaintexts and tags are zeroed out.
147 Lastly the contexts internal state is reset, so that a new decryption/encryption can be commenced.
148
149 Decryption can be done in place in memory by setting in=out. Encryption cannot be done in place. However, if one is trying to minimize memory usage one can set out = in - block_length, which results in the ciphertext being encrypted inplace, and the IV being prepended before the ciphertext.
150 */
151int ccsiv_crypt(const struct ccmode_siv *mode, ccsiv_ctx *ctx,
152 size_t nbytes, const uint8_t *in, uint8_t *out);
153
154/*!
155 @function ccsiv_reset
156 @abstract Resets the state of the ccsiv_ctx ctx, maintaing the key, but preparing the
157 ctx to preform a new Associated Data Authenticated (En)/(De)cryption.
158 @param mode Descriptor for the mode
159 @param ctx Intialized ctx
160 */
161int ccsiv_reset(const struct ccmode_siv *mode, ccsiv_ctx *ctx);
162
163/*!
164 @function ccsiv_one_shot
165 @abstract A simplified but more constrained way of performing a AES SIV (en)/(de)cryption. It is limited because only
166 one piece of associated data may be provided.
167
168 @param mode Descriptor for the mode
169 @param key_len Length of the key: Supported key sizes are 32, 48, 64 bytes
170 @param key key for siv
171 @param nonce_nbytes Length of the current nonce data being added
172 @param nonce Nonce data to be authenticated.
173 @param adata_nbytes Length of the associated data.
174 @param adata Associated data to be authenticated.
175 @param in_nbytes Length of either the plaintext (for encryption) or ciphertext (for decryption), in the latter case the length includes the length of the tag.
176 @param in Plaintext or ciphertext. Note that the ciphertext includes a tag of length tag_length prepended to it.
177 @param out Buffer to hold ciphertext/plaintext. (Note Ciphertext is of size plaintext_length + block_length and plaintext is of ciphertext_length - block_length, as the tag has the length of one block.
178 Must be the case that out<= in - block length || out>= in + plaintext_length
179
180 @discussion Decryption can be done in place in memory by setting in=out. Encryption cannot be done in place. However, is one is trying to minimize memory usage
181 one can set out = in - block_length, which results in the ciphertext being encrypted inplace, and the IV being prepended before the ciphertext.
182
183 Suppose the block length is 16 bytes long (AES) and plaintext of length 20, then we could set in = 16, out = 0 let the bytes of the plaintext be denoted as P_1...P_20
184 then memory is depicted as:
185 | 0 = ? | 1 = ? | ... | 15 = ? | 16 = P_1 | ... | 35 = P_20 |
186 | | | | |
187 V V V V V
188 |IV_1 | IV_2 | ... | IV_16 | C_1 | ... | C_20 |
189
190Note that the ciphrtext itself is encrypted in place, but the IV prefixes the ciphertext.
191
192
193 */
194
195int ccsiv_one_shot(const struct ccmode_siv *mode,
196 size_t key_len, const uint8_t *key,
197 unsigned nonce_nbytes, const uint8_t* nonce,
198 unsigned adata_nbytes, const uint8_t* adata,
199 size_t in_nbytes, const uint8_t *in, uint8_t *out);
200
201#endif /* _CORECRYPTO_CCMODE_H_ */
202