]>
Commit | Line | Data |
---|---|---|
97cf1f6c | 1 | /* crypto/modes/wrap128.c */ |
0f113f3e MC |
2 | /* |
3 | * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL | |
4 | * project. Mode with padding contributed by Petr Spacek | |
5 | * (pspacek@redhat.com). | |
97cf1f6c DSH |
6 | */ |
7 | /* ==================================================================== | |
8 | * Copyright (c) 2013 The OpenSSL Project. All rights reserved. | |
9 | * | |
10 | * Redistribution and use in source and binary forms, with or without | |
11 | * modification, are permitted provided that the following conditions | |
12 | * are met: | |
13 | * | |
14 | * 1. Redistributions of source code must retain the above copyright | |
0f113f3e | 15 | * notice, this list of conditions and the following disclaimer. |
97cf1f6c DSH |
16 | * |
17 | * 2. Redistributions in binary form must reproduce the above copyright | |
18 | * notice, this list of conditions and the following disclaimer in | |
19 | * the documentation and/or other materials provided with the | |
20 | * distribution. | |
21 | * | |
22 | * 3. All advertising materials mentioning features or use of this | |
23 | * software must display the following acknowledgment: | |
24 | * "This product includes software developed by the OpenSSL Project | |
25 | * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" | |
26 | * | |
27 | * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to | |
28 | * endorse or promote products derived from this software without | |
29 | * prior written permission. For written permission, please contact | |
30 | * licensing@OpenSSL.org. | |
31 | * | |
32 | * 5. Products derived from this software may not be called "OpenSSL" | |
33 | * nor may "OpenSSL" appear in their names without prior written | |
34 | * permission of the OpenSSL Project. | |
35 | * | |
36 | * 6. Redistributions of any form whatsoever must retain the following | |
37 | * acknowledgment: | |
38 | * "This product includes software developed by the OpenSSL Project | |
39 | * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" | |
40 | * | |
41 | * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY | |
42 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
43 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | |
44 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR | |
45 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
46 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | |
47 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | |
48 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
49 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | |
50 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
51 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED | |
52 | * OF THE POSSIBILITY OF SUCH DAMAGE. | |
53 | * ==================================================================== | |
54 | */ | |
55 | ||
d31fed73 DSH |
56 | /** Beware! |
57 | * | |
58 | * Following wrapping modes were designed for AES but this implementation | |
59 | * allows you to use them for any 128 bit block cipher. | |
60 | */ | |
61 | ||
97cf1f6c DSH |
62 | #include "cryptlib.h" |
63 | #include <openssl/modes.h> | |
64 | ||
d31fed73 | 65 | /** RFC 3394 section 2.2.3.1 Default Initial Value */ |
97cf1f6c | 66 | static const unsigned char default_iv[] = { |
0f113f3e | 67 | 0xA6, 0xA6, 0xA6, 0xA6, 0xA6, 0xA6, 0xA6, 0xA6, |
97cf1f6c | 68 | }; |
d31fed73 DSH |
69 | |
70 | /** RFC 5649 section 3 Alternative Initial Value 32-bit constant */ | |
71 | static const unsigned char default_aiv[] = { | |
0f113f3e | 72 | 0xA6, 0x59, 0x59, 0xA6 |
d31fed73 DSH |
73 | }; |
74 | ||
75 | /** Input size limit: lower than maximum of standards but far larger than | |
76 | * anything that will be used in practice. | |
97cf1f6c DSH |
77 | */ |
78 | #define CRYPTO128_WRAP_MAX (1UL << 31) | |
79 | ||
d31fed73 DSH |
80 | /** Wrapping according to RFC 3394 section 2.2.1. |
81 | * | |
0f113f3e | 82 | * @param[in] key Key value. |
d31fed73 DSH |
83 | * @param[in] iv IV value. Length = 8 bytes. NULL = use default_iv. |
84 | * @param[in] in Plain text as n 64-bit blocks, n >= 2. | |
85 | * @param[in] inlen Length of in. | |
86 | * @param[out] out Cipher text. Minimal buffer length = (inlen + 8) bytes. | |
87 | * Input and output buffers can overlap if block function | |
88 | * supports that. | |
89 | * @param[in] block Block processing function. | |
90 | * @return 0 if inlen does not consist of n 64-bit blocks, n >= 2. | |
91 | * or if inlen > CRYPTO128_WRAP_MAX. | |
92 | * Output length if wrapping succeeded. | |
93 | */ | |
97cf1f6c | 94 | size_t CRYPTO_128_wrap(void *key, const unsigned char *iv, |
0f113f3e MC |
95 | unsigned char *out, |
96 | const unsigned char *in, size_t inlen, | |
97 | block128_f block) | |
98 | { | |
99 | unsigned char *A, B[16], *R; | |
100 | size_t i, j, t; | |
101 | if ((inlen & 0x7) || (inlen < 16) || (inlen > CRYPTO128_WRAP_MAX)) | |
102 | return 0; | |
103 | A = B; | |
104 | t = 1; | |
105 | memmove(out + 8, in, inlen); | |
106 | if (!iv) | |
107 | iv = default_iv; | |
97cf1f6c | 108 | |
0f113f3e | 109 | memcpy(A, iv, 8); |
97cf1f6c | 110 | |
0f113f3e MC |
111 | for (j = 0; j < 6; j++) { |
112 | R = out + 8; | |
113 | for (i = 0; i < inlen; i += 8, t++, R += 8) { | |
114 | memcpy(B + 8, R, 8); | |
115 | block(B, B, key); | |
116 | A[7] ^= (unsigned char)(t & 0xff); | |
117 | if (t > 0xff) { | |
118 | A[6] ^= (unsigned char)((t >> 8) & 0xff); | |
119 | A[5] ^= (unsigned char)((t >> 16) & 0xff); | |
120 | A[4] ^= (unsigned char)((t >> 24) & 0xff); | |
121 | } | |
122 | memcpy(R, B + 8, 8); | |
123 | } | |
124 | } | |
125 | memcpy(out, A, 8); | |
126 | return inlen + 8; | |
127 | } | |
d31fed73 DSH |
128 | |
129 | /** Unwrapping according to RFC 3394 section 2.2.2 steps 1-2. | |
130 | * IV check (step 3) is responsibility of the caller. | |
131 | * | |
0f113f3e | 132 | * @param[in] key Key value. |
d31fed73 DSH |
133 | * @param[out] iv Unchecked IV value. Minimal buffer length = 8 bytes. |
134 | * @param[out] out Plain text without IV. | |
135 | * Minimal buffer length = (inlen - 8) bytes. | |
136 | * Input and output buffers can overlap if block function | |
137 | * supports that. | |
138 | * @param[in] in Ciphertext text as n 64-bit blocks | |
139 | * @param[in] inlen Length of in. | |
140 | * @param[in] block Block processing function. | |
141 | * @return 0 if inlen is out of range [24, CRYPTO128_WRAP_MAX] | |
142 | * or if inlen is not multiply of 8. | |
143 | * Output length otherwise. | |
144 | */ | |
145 | static size_t crypto_128_unwrap_raw(void *key, unsigned char *iv, | |
0f113f3e MC |
146 | unsigned char *out, |
147 | const unsigned char *in, size_t inlen, | |
148 | block128_f block) | |
149 | { | |
150 | unsigned char *A, B[16], *R; | |
151 | size_t i, j, t; | |
152 | inlen -= 8; | |
153 | if ((inlen & 0x7) || (inlen < 16) || (inlen > CRYPTO128_WRAP_MAX)) | |
154 | return 0; | |
155 | A = B; | |
156 | t = 6 * (inlen >> 3); | |
157 | memcpy(A, in, 8); | |
158 | memmove(out, in + 8, inlen); | |
159 | for (j = 0; j < 6; j++) { | |
160 | R = out + inlen - 8; | |
161 | for (i = 0; i < inlen; i += 8, t--, R -= 8) { | |
162 | A[7] ^= (unsigned char)(t & 0xff); | |
163 | if (t > 0xff) { | |
164 | A[6] ^= (unsigned char)((t >> 8) & 0xff); | |
165 | A[5] ^= (unsigned char)((t >> 16) & 0xff); | |
166 | A[4] ^= (unsigned char)((t >> 24) & 0xff); | |
167 | } | |
168 | memcpy(B + 8, R, 8); | |
169 | block(B, B, key); | |
170 | memcpy(R, B + 8, 8); | |
171 | } | |
172 | } | |
173 | memcpy(iv, A, 8); | |
174 | return inlen; | |
175 | } | |
d31fed73 DSH |
176 | |
177 | /** Unwrapping according to RFC 3394 section 2.2.2 including IV check. | |
178 | * First block of plain text have to match supplied IV otherwise an error is | |
179 | * returned. | |
180 | * | |
0f113f3e | 181 | * @param[in] key Key value. |
d31fed73 DSH |
182 | * @param[out] iv Unchecked IV value. Minimal buffer length = 8 bytes. |
183 | * @param[out] out Plain text without IV. | |
184 | * Minimal buffer length = (inlen - 8) bytes. | |
185 | * Input and output buffers can overlap if block function | |
186 | * supports that. | |
187 | * @param[in] in Ciphertext text as n 64-bit blocks | |
188 | * @param[in] inlen Length of in. | |
189 | * @param[in] block Block processing function. | |
190 | * @return 0 if inlen is out of range [24, CRYPTO128_WRAP_MAX] | |
191 | * or if inlen is not multiply of 8 | |
192 | * or if IV doesn't match expected value. | |
193 | * Output length otherwise. | |
194 | */ | |
195 | size_t CRYPTO_128_unwrap(void *key, const unsigned char *iv, | |
0f113f3e MC |
196 | unsigned char *out, const unsigned char *in, |
197 | size_t inlen, block128_f block) | |
198 | { | |
199 | size_t ret; | |
200 | unsigned char got_iv[8]; | |
d31fed73 | 201 | |
0f113f3e MC |
202 | ret = crypto_128_unwrap_raw(key, got_iv, out, in, inlen, block); |
203 | if (ret != inlen) | |
204 | return ret; | |
d31fed73 | 205 | |
0f113f3e MC |
206 | if (!iv) |
207 | iv = default_iv; | |
208 | if (CRYPTO_memcmp(out, iv, 8)) { | |
209 | OPENSSL_cleanse(out, inlen); | |
210 | return 0; | |
211 | } | |
212 | return inlen; | |
213 | } | |
d31fed73 DSH |
214 | |
215 | /** Wrapping according to RFC 5649 section 4.1. | |
216 | * | |
0f113f3e | 217 | * @param[in] key Key value. |
d31fed73 DSH |
218 | * @param[in] icv (Non-standard) IV, 4 bytes. NULL = use default_aiv. |
219 | * @param[out] out Cipher text. Minimal buffer length = (inlen + 15) bytes. | |
220 | * Input and output buffers can overlap if block function | |
221 | * supports that. | |
222 | * @param[in] in Plain text as n 64-bit blocks, n >= 2. | |
223 | * @param[in] inlen Length of in. | |
224 | * @param[in] block Block processing function. | |
225 | * @return 0 if inlen is out of range [1, CRYPTO128_WRAP_MAX]. | |
226 | * Output length if wrapping succeeded. | |
227 | */ | |
228 | size_t CRYPTO_128_wrap_pad(void *key, const unsigned char *icv, | |
0f113f3e MC |
229 | unsigned char *out, |
230 | const unsigned char *in, size_t inlen, | |
231 | block128_f block) | |
232 | { | |
233 | /* n: number of 64-bit blocks in the padded key data */ | |
234 | const size_t blocks_padded = (inlen + 8) / 8; | |
235 | const size_t padded_len = blocks_padded * 8; | |
236 | const size_t padding_len = padded_len - inlen; | |
237 | /* RFC 5649 section 3: Alternative Initial Value */ | |
238 | unsigned char aiv[8]; | |
239 | int ret; | |
d31fed73 | 240 | |
0f113f3e MC |
241 | /* Section 1: use 32-bit fixed field for plaintext octet length */ |
242 | if (inlen == 0 || inlen >= CRYPTO128_WRAP_MAX) | |
243 | return 0; | |
d31fed73 | 244 | |
0f113f3e MC |
245 | /* Section 3: Alternative Initial Value */ |
246 | if (!icv) | |
247 | memcpy(aiv, default_aiv, 4); | |
248 | else | |
249 | memcpy(aiv, icv, 4); /* Standard doesn't mention this. */ | |
d31fed73 | 250 | |
0f113f3e MC |
251 | aiv[4] = (inlen >> 24) & 0xFF; |
252 | aiv[5] = (inlen >> 16) & 0xFF; | |
253 | aiv[6] = (inlen >> 8) & 0xFF; | |
254 | aiv[7] = inlen & 0xFF; | |
d31fed73 | 255 | |
0f113f3e MC |
256 | if (padded_len == 8) { |
257 | /* | |
258 | * Section 4.1 - special case in step 2: If the padded plaintext | |
259 | * contains exactly eight octets, then prepend the AIV and encrypt | |
260 | * the resulting 128-bit block using AES in ECB mode. | |
261 | */ | |
262 | memmove(out + 8, in, inlen); | |
263 | memcpy(out, aiv, 8); | |
264 | memset(out + 8 + inlen, 0, padding_len); | |
265 | block(out, out, key); | |
266 | ret = 16; /* AIV + padded input */ | |
267 | } else { | |
268 | memmove(out, in, inlen); | |
269 | memset(out + inlen, 0, padding_len); /* Section 4.1 step 1 */ | |
270 | ret = CRYPTO_128_wrap(key, aiv, out, out, padded_len, block); | |
271 | } | |
d31fed73 | 272 | |
0f113f3e MC |
273 | return ret; |
274 | } | |
d31fed73 DSH |
275 | |
276 | /** Unwrapping according to RFC 5649 section 4.2. | |
277 | * | |
0f113f3e | 278 | * @param[in] key Key value. |
d31fed73 DSH |
279 | * @param[in] icv (Non-standard) IV, 4 bytes. NULL = use default_aiv. |
280 | * @param[out] out Plain text. Minimal buffer length = inlen bytes. | |
281 | * Input and output buffers can overlap if block function | |
282 | * supports that. | |
283 | * @param[in] in Ciphertext text as n 64-bit blocks | |
284 | * @param[in] inlen Length of in. | |
285 | * @param[in] block Block processing function. | |
286 | * @return 0 if inlen is out of range [16, CRYPTO128_WRAP_MAX], | |
287 | * or if inlen is not multiply of 8 | |
288 | * or if IV and message length indicator doesn't match. | |
289 | * Output length if unwrapping succeeded and IV matches. | |
290 | */ | |
291 | size_t CRYPTO_128_unwrap_pad(void *key, const unsigned char *icv, | |
0f113f3e MC |
292 | unsigned char *out, |
293 | const unsigned char *in, size_t inlen, | |
294 | block128_f block) | |
295 | { | |
296 | /* n: number of 64-bit blocks in the padded key data */ | |
297 | size_t n = inlen / 8 - 1; | |
298 | size_t padded_len; | |
299 | size_t padding_len; | |
300 | size_t ptext_len; | |
301 | /* RFC 5649 section 3: Alternative Initial Value */ | |
302 | unsigned char aiv[8]; | |
303 | static unsigned char zeros[8] = { 0x0 }; | |
304 | size_t ret; | |
d31fed73 | 305 | |
0f113f3e MC |
306 | /* Section 4.2: Cipher text length has to be (n+1) 64-bit blocks. */ |
307 | if ((inlen & 0x7) != 0 || inlen < 16 || inlen >= CRYPTO128_WRAP_MAX) | |
308 | return 0; | |
d31fed73 | 309 | |
0f113f3e MC |
310 | memmove(out, in, inlen); |
311 | if (inlen == 16) { | |
312 | /* | |
313 | * Section 4.2 - special case in step 1: When n=1, the ciphertext | |
314 | * contains exactly two 64-bit blocks and they are decrypted as a | |
315 | * single AES block using AES in ECB mode: AIV | P[1] = DEC(K, C[0] | | |
316 | * C[1]) | |
317 | */ | |
318 | block(out, out, key); | |
319 | memcpy(aiv, out, 8); | |
320 | /* Remove AIV */ | |
321 | memmove(out, out + 8, 8); | |
322 | padded_len = 8; | |
323 | } else { | |
324 | padded_len = inlen - 8; | |
325 | ret = crypto_128_unwrap_raw(key, aiv, out, out, inlen, block); | |
326 | if (padded_len != ret) { | |
327 | OPENSSL_cleanse(out, inlen); | |
328 | return 0; | |
329 | } | |
330 | } | |
d31fed73 | 331 | |
0f113f3e MC |
332 | /* |
333 | * Section 3: AIV checks: Check that MSB(32,A) = A65959A6. Optionally a | |
334 | * user-supplied value can be used (even if standard doesn't mention | |
335 | * this). | |
336 | */ | |
337 | if ((!icv && CRYPTO_memcmp(aiv, default_aiv, 4)) | |
338 | || (icv && CRYPTO_memcmp(aiv, icv, 4))) { | |
339 | OPENSSL_cleanse(out, inlen); | |
340 | return 0; | |
341 | } | |
d31fed73 | 342 | |
0f113f3e MC |
343 | /* |
344 | * Check that 8*(n-1) < LSB(32,AIV) <= 8*n. If so, let ptext_len = | |
345 | * LSB(32,AIV). | |
346 | */ | |
d31fed73 | 347 | |
0f113f3e MC |
348 | ptext_len = (aiv[4] << 24) | (aiv[5] << 16) | (aiv[6] << 8) | aiv[7]; |
349 | if (8 * (n - 1) >= ptext_len || ptext_len > 8 * n) { | |
350 | OPENSSL_cleanse(out, inlen); | |
351 | return 0; | |
352 | } | |
d31fed73 | 353 | |
0f113f3e MC |
354 | /* |
355 | * Check that the rightmost padding_len octets of the output data are | |
356 | * zero. | |
357 | */ | |
358 | padding_len = padded_len - ptext_len; | |
359 | if (CRYPTO_memcmp(out + ptext_len, zeros, padding_len) != 0) { | |
360 | OPENSSL_cleanse(out, inlen); | |
361 | return 0; | |
362 | } | |
d31fed73 | 363 | |
0f113f3e MC |
364 | /* Section 4.2 step 3: Remove padding */ |
365 | return ptext_len; | |
366 | } |