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