1 /* crypto/modes/wrap128.c */
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).
7 /* ====================================================================
8 * Copyright (c) 2013 The OpenSSL Project. All rights reserved.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
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
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/)"
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.
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.
36 * 6. Redistributions of any form whatsoever must retain the following
38 * "This product includes software developed by the OpenSSL Project
39 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
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 * ====================================================================
58 * Following wrapping modes were designed for AES but this implementation
59 * allows you to use them for any 128 bit block cipher.
63 #include <openssl/modes.h>
65 /** RFC 3394 section 2.2.3.1 Default Initial Value */
66 static const unsigned char default_iv[] = {
67 0xA6, 0xA6, 0xA6, 0xA6, 0xA6, 0xA6, 0xA6, 0xA6,
70 /** RFC 5649 section 3 Alternative Initial Value 32-bit constant */
71 static const unsigned char default_aiv[] = {
72 0xA6, 0x59, 0x59, 0xA6
75 /** Input size limit: lower than maximum of standards but far larger than
76 * anything that will be used in practice.
78 #define CRYPTO128_WRAP_MAX (1UL << 31)
80 /** Wrapping according to RFC 3394 section 2.2.1.
82 * @param[in] key Key value.
83 * @param[in] iv IV value. Length = 8 bytes. NULL = use default_iv.
84 * @param[in] in Plaintext as n 64-bit blocks, n >= 2.
85 * @param[in] inlen Length of in.
86 * @param[out] out Ciphertext. Minimal buffer length = (inlen + 8) bytes.
87 * Input and output buffers can overlap if block function
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.
94 size_t CRYPTO_128_wrap(void *key, const unsigned char *iv,
96 const unsigned char *in, size_t inlen,
99 unsigned char *A, B[16], *R;
101 if ((inlen & 0x7) || (inlen < 16) || (inlen > CRYPTO128_WRAP_MAX))
105 memmove(out + 8, in, inlen);
111 for (j = 0; j < 6; j++) {
113 for (i = 0; i < inlen; i += 8, t++, R += 8) {
116 A[7] ^= (unsigned char)(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);
129 /** Unwrapping according to RFC 3394 section 2.2.2 steps 1-2.
130 * The IV check (step 3) is responsibility of the caller.
132 * @param[in] key Key value.
133 * @param[out] iv Unchecked IV value. Minimal buffer length = 8 bytes.
134 * @param[out] out Plaintext without IV.
135 * Minimal buffer length = (inlen - 8) bytes.
136 * Input and output buffers can overlap if block function
138 * @param[in] in Ciphertext 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 a multiple of 8.
143 * Output length otherwise.
145 static size_t crypto_128_unwrap_raw(void *key, unsigned char *iv,
147 const unsigned char *in, size_t inlen,
150 unsigned char *A, B[16], *R;
153 if ((inlen & 0x7) || (inlen < 16) || (inlen > CRYPTO128_WRAP_MAX))
156 t = 6 * (inlen >> 3);
158 memmove(out, in + 8, inlen);
159 for (j = 0; j < 6; j++) {
161 for (i = 0; i < inlen; i += 8, t--, R -= 8) {
162 A[7] ^= (unsigned char)(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);
177 /** Unwrapping according to RFC 3394 section 2.2.2, including the IV check.
178 * The first block of plaintext has to match the supplied IV, otherwise an
181 * @param[in] key Key value.
182 * @param[out] iv IV value to match against. Length = 8 bytes.
183 * NULL = use default_iv.
184 * @param[out] out Plaintext without IV.
185 * Minimal buffer length = (inlen - 8) bytes.
186 * Input and output buffers can overlap if block function
188 * @param[in] in Ciphertext as n 64-bit blocks.
189 * @param[in] inlen Length of in.
190 * @param[in] block Block processing function.
191 * @return 0 if inlen is out of range [24, CRYPTO128_WRAP_MAX]
192 * or if inlen is not a multiple of 8
193 * or if IV doesn't match expected value.
194 * Output length otherwise.
196 size_t CRYPTO_128_unwrap(void *key, const unsigned char *iv,
197 unsigned char *out, const unsigned char *in,
198 size_t inlen, block128_f block)
201 unsigned char got_iv[8];
203 ret = crypto_128_unwrap_raw(key, got_iv, out, in, inlen, block);
209 if (CRYPTO_memcmp(out, iv, 8)) {
210 OPENSSL_cleanse(out, inlen);
216 /** Wrapping according to RFC 5649 section 4.1.
218 * @param[in] key Key value.
219 * @param[in] icv (Non-standard) IV, 4 bytes. NULL = use default_aiv.
220 * @param[out] out Ciphertext. Minimal buffer length = (inlen + 15) bytes.
221 * Input and output buffers can overlap if block function
223 * @param[in] in Plaintext as n 64-bit blocks, n >= 2.
224 * @param[in] inlen Length of in.
225 * @param[in] block Block processing function.
226 * @return 0 if inlen is out of range [1, CRYPTO128_WRAP_MAX].
227 * Output length if wrapping succeeded.
229 size_t CRYPTO_128_wrap_pad(void *key, const unsigned char *icv,
231 const unsigned char *in, size_t inlen,
234 /* n: number of 64-bit blocks in the padded key data
236 * If length of plain text is not a multiple of 8, pad the plain text octet
237 * string on the right with octets of zeros, where final length is the
238 * smallest multiple of 8 that is greater than length of plain text.
239 * If length of plain text is a multiple of 8, then there is no padding. */
240 const size_t blocks_padded = (inlen + 7) / 8; /* CEILING(m/8) */
241 const size_t padded_len = blocks_padded * 8;
242 const size_t padding_len = padded_len - inlen;
243 /* RFC 5649 section 3: Alternative Initial Value */
244 unsigned char aiv[8];
247 /* Section 1: use 32-bit fixed field for plaintext octet length */
248 if (inlen == 0 || inlen >= CRYPTO128_WRAP_MAX)
251 /* Section 3: Alternative Initial Value */
253 memcpy(aiv, default_aiv, 4);
255 memcpy(aiv, icv, 4); /* Standard doesn't mention this. */
257 aiv[4] = (inlen >> 24) & 0xFF;
258 aiv[5] = (inlen >> 16) & 0xFF;
259 aiv[6] = (inlen >> 8) & 0xFF;
260 aiv[7] = inlen & 0xFF;
262 if (padded_len == 8) {
264 * Section 4.1 - special case in step 2: If the padded plaintext
265 * contains exactly eight octets, then prepend the AIV and encrypt
266 * the resulting 128-bit block using AES in ECB mode.
268 memmove(out + 8, in, inlen);
270 memset(out + 8 + inlen, 0, padding_len);
271 block(out, out, key);
272 ret = 16; /* AIV + padded input */
274 memmove(out, in, inlen);
275 memset(out + inlen, 0, padding_len); /* Section 4.1 step 1 */
276 ret = CRYPTO_128_wrap(key, aiv, out, out, padded_len, block);
282 /** Unwrapping according to RFC 5649 section 4.2.
284 * @param[in] key Key value.
285 * @param[in] icv (Non-standard) IV, 4 bytes. NULL = use default_aiv.
286 * @param[out] out Plaintext. Minimal buffer length = inlen bytes.
287 * Input and output buffers can overlap if block function
289 * @param[in] in Ciphertext as n 64-bit blocks.
290 * @param[in] inlen Length of in.
291 * @param[in] block Block processing function.
292 * @return 0 if inlen is out of range [16, CRYPTO128_WRAP_MAX],
293 * or if inlen is not a multiple of 8
294 * or if IV and message length indicator doesn't match.
295 * Output length if unwrapping succeeded and IV matches.
297 size_t CRYPTO_128_unwrap_pad(void *key, const unsigned char *icv,
299 const unsigned char *in, size_t inlen,
302 /* n: number of 64-bit blocks in the padded key data */
303 size_t n = inlen / 8 - 1;
307 /* RFC 5649 section 3: Alternative Initial Value */
308 unsigned char aiv[8];
309 static unsigned char zeros[8] = { 0x0 };
312 /* Section 4.2: Ciphertext length has to be (n+1) 64-bit blocks. */
313 if ((inlen & 0x7) != 0 || inlen < 16 || inlen >= CRYPTO128_WRAP_MAX)
316 memmove(out, in, inlen);
319 * Section 4.2 - special case in step 1: When n=1, the ciphertext
320 * contains exactly two 64-bit blocks and they are decrypted as a
321 * single AES block using AES in ECB mode: AIV | P[1] = DEC(K, C[0] |
324 block(out, out, key);
327 memmove(out, out + 8, 8);
330 padded_len = inlen - 8;
331 ret = crypto_128_unwrap_raw(key, aiv, out, out, inlen, block);
332 if (padded_len != ret) {
333 OPENSSL_cleanse(out, inlen);
339 * Section 3: AIV checks: Check that MSB(32,A) = A65959A6. Optionally a
340 * user-supplied value can be used (even if standard doesn't mention
343 if ((!icv && CRYPTO_memcmp(aiv, default_aiv, 4))
344 || (icv && CRYPTO_memcmp(aiv, icv, 4))) {
345 OPENSSL_cleanse(out, inlen);
350 * Check that 8*(n-1) < LSB(32,AIV) <= 8*n. If so, let ptext_len =
354 ptext_len = ((unsigned int)aiv[4] << 24)
355 | ((unsigned int)aiv[5] << 16)
356 | ((unsigned int)aiv[6] << 8)
357 | (unsigned int)aiv[7];
358 if (8 * (n - 1) >= ptext_len || ptext_len > 8 * n) {
359 OPENSSL_cleanse(out, inlen);
364 * Check that the rightmost padding_len octets of the output data are
367 padding_len = padded_len - ptext_len;
368 if (CRYPTO_memcmp(out + ptext_len, zeros, padding_len) != 0) {
369 OPENSSL_cleanse(out, inlen);
373 /* Section 4.2 step 3: Remove padding */