1 /* ====================================================================
2 * Copyright (c) 2014 The OpenSSL Project. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in
13 * the documentation and/or other materials provided with the
16 * 3. All advertising materials mentioning features or use of this
17 * software must display the following acknowledgment:
18 * "This product includes software developed by the OpenSSL Project
19 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
21 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
22 * endorse or promote products derived from this software without
23 * prior written permission. For written permission, please contact
24 * openssl-core@openssl.org.
26 * 5. Products derived from this software may not be called "OpenSSL"
27 * nor may "OpenSSL" appear in their names without prior written
28 * permission of the OpenSSL Project.
30 * 6. Redistributions of any form whatsoever must retain the following
32 * "This product includes software developed by the OpenSSL Project
33 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
35 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
36 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
37 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
38 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
39 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
41 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
42 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
43 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
44 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
45 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
46 * OF THE POSSIBILITY OF SUCH DAMAGE.
47 * ====================================================================
51 #include <openssl/crypto.h>
52 #include "modes_lcl.h"
54 #ifndef OPENSSL_NO_OCB
57 unsigned char *chrblk;
62 * Calculate the number of binary trailing zero's in any given number
64 static u32 ocb_ntz(u64 n)
69 * We do a right-to-left simple sequential search. This is surprisingly
70 * efficient as the distribution of trailing zeros is not uniform,
71 * e.g. the number of possible inputs with no trailing zeros is equal to
72 * the number with 1 or more; the number with exactly 1 is equal to the
73 * number with 2 or more, etc. Checking the last two bits covers 75% of
74 * all numbers. Checking the last three covers 87.5%
84 * Shift a block of 16 bytes left by shift bits
86 static void ocb_block_lshift(OCB_BLOCK *in, size_t shift, OCB_BLOCK *out)
88 unsigned char shift_mask;
90 unsigned char mask[15];
98 shift_mask <<= (8 - shift);
99 for (i = 15; i >= 0; i--) {
101 mask[i - 1] = locin.chrblk[i] & shift_mask;
102 mask[i - 1] >>= 8 - shift;
104 locout.chrblk[i] = locin.chrblk[i] << shift;
107 locout.chrblk[i] ^= mask[i];
113 * Perform a "double" operation as per OCB spec
115 static void ocb_double(OCB_BLOCK *in, OCB_BLOCK *out)
125 * Calculate the mask based on the most significant bit. There are more
126 * efficient ways to do this - but this way is constant time
128 mask = locin.chrblk[0] & 0x80;
132 ocb_block_lshift(in, 1, out);
134 locout.chrblk[15] ^= mask;
138 * Perform an xor on in1 and in2 - each of len bytes. Store result in out
140 static void ocb_block_xor(const unsigned char *in1,
141 const unsigned char *in2, size_t len,
145 for (i = 0; i < len; i++) {
146 out[i] = in1[i] ^ in2[i];
151 * Lookup L_index in our lookup table. If we haven't already got it we need to
154 static OCB_BLOCK *ocb_lookup_l(OCB128_CONTEXT * ctx, size_t index)
156 if (index <= ctx->l_index) {
157 return ctx->l + index;
160 /* We don't have it - so calculate it */
162 if (ctx->l_index == ctx->max_l_index) {
163 ctx->max_l_index *= 2;
164 ctx->l = OPENSSL_realloc(ctx->l, ctx->max_l_index * sizeof(OCB_BLOCK));
168 ocb_double(ctx->l + (index - 1), ctx->l + index);
170 return ctx->l + index;
174 * Encrypt a block from |in| and store the result in |out|
176 static void ocb_encrypt(OCB128_CONTEXT *ctx, OCB_BLOCK *in, OCB_BLOCK *out, void *keyenc)
184 ctx->encrypt(locin.chrblk, locout.chrblk, keyenc);
188 * Decrypt a block from |in| and store the result in |out|
190 static void ocb_decrypt(OCB128_CONTEXT *ctx, OCB_BLOCK *in, OCB_BLOCK *out, void *keydec)
198 ctx->decrypt(locin.chrblk, locout.chrblk, keydec);
202 * Create a new OCB128_CONTEXT
204 OCB128_CONTEXT *CRYPTO_ocb128_new(void *keyenc, void *keydec,
205 block128_f encrypt, block128_f decrypt)
207 OCB128_CONTEXT *octx;
210 if ((octx = (OCB128_CONTEXT *) OPENSSL_malloc(sizeof(OCB128_CONTEXT)))) {
211 ret = CRYPTO_ocb128_init(octx, keyenc, keydec, encrypt, decrypt);
221 * Initialise an existing OCB128_CONTEXT
223 int CRYPTO_ocb128_init(OCB128_CONTEXT *ctx, void *keyenc, void *keydec,
224 block128_f encrypt, block128_f decrypt)
226 /* Clear everything to NULLs */
227 memset(ctx, 0, sizeof(*ctx));
230 ctx->max_l_index = 1;
231 ctx->l = OPENSSL_malloc(ctx->max_l_index * 16);
236 * We set both the encryption and decryption key schedules - decryption
237 * needs both. Don't really need decryption schedule if only doing
238 * encryption - but it simplifies things to take it anyway
240 ctx->encrypt = encrypt;
241 ctx->decrypt = decrypt;
242 ctx->keyenc = keyenc;
243 ctx->keydec = keydec;
245 /* L_* = ENCIPHER(K, zeros(128)) */
246 ocb_encrypt(ctx, &ctx->l_star, &ctx->l_star, ctx->keyenc);
248 /* L_$ = double(L_*) */
249 ocb_double(&ctx->l_star, &ctx->l_dollar);
251 /* L_0 = double(L_$) */
252 ocb_double(&ctx->l_dollar, ctx->l);
258 * Copy an OCB128_CONTEXT object
260 int CRYPTO_ocb128_copy_ctx(OCB128_CONTEXT * dest, OCB128_CONTEXT * src,
261 void *keyenc, void *keydec)
263 memcpy(dest, src, sizeof(OCB128_CONTEXT));
265 dest->keyenc = keyenc;
267 dest->keydec = keydec;
269 dest->l = OPENSSL_malloc(src->max_l_index * 16);
272 memcpy(dest->l, src->l, (src->l_index + 1) * 16);
278 * Set the IV to be used for this operation. Must be 1 - 15 bytes.
280 int CRYPTO_ocb128_setiv(OCB128_CONTEXT * ctx, const unsigned char *iv,
281 size_t len, size_t taglen)
283 unsigned char ktop[16], tmp[16], mask;
284 unsigned char stretch[24], nonce[16];
285 size_t bottom, shift;
288 offset.ocbblk = &ctx->offset;
291 * Spec says IV is 120 bits or fewer - it allows non byte aligned lengths.
292 * We don't support this at this stage
294 if ((len > 15) || (len < 1) || (taglen > 16) || (taglen < 1)) {
298 /* Nonce = num2str(TAGLEN mod 128,7) || zeros(120-bitlen(N)) || 1 || N */
299 nonce[0] = ((taglen * 8) % 128) << 1;
300 memset(nonce + 1, 0, 15);
301 memcpy(nonce + 16 - len, iv, len);
302 nonce[15 - len] |= 1;
304 /* Ktop = ENCIPHER(K, Nonce[1..122] || zeros(6)) */
305 memcpy(tmp, nonce, 16);
307 ctx->encrypt(tmp, ktop, ctx->keyenc);
309 /* Stretch = Ktop || (Ktop[1..64] xor Ktop[9..72]) */
310 memcpy(stretch, ktop, 16);
311 ocb_block_xor(ktop, ktop + 1, 8, stretch + 16);
313 /* bottom = str2num(Nonce[123..128]) */
314 bottom = nonce[15] & 0x3f;
316 /* Offset_0 = Stretch[1+bottom..128+bottom] */
318 ocb_block_lshift((OCB_BLOCK *)(stretch + (bottom / 8)), shift, &ctx->offset);
321 offset.chrblk[15] |= (*(stretch + (bottom / 8) + 16) & mask) >> (8 - shift);
327 * Provide any AAD. This can be called multiple times. Only the final time can
328 * have a partial block
330 int CRYPTO_ocb128_aad(OCB128_CONTEXT * ctx, const unsigned char *aad,
333 u64 all_num_blocks, num_blocks;
339 /* Calculate the number of blocks of AAD provided now, and so far */
340 num_blocks = len / 16;
341 all_num_blocks = num_blocks + ctx->blocks_hashed;
343 /* Loop through all full blocks of AAD */
344 for (i = ctx->blocks_hashed + 1; i <= all_num_blocks; i++) {
346 OCB_BLOCK *aad_block;
348 /* Offset_i = Offset_{i-1} xor L_{ntz(i)} */
349 lookup = ocb_lookup_l(ctx, ocb_ntz(i));
352 ocb_block16_xor(&ctx->offset_aad, lookup, &ctx->offset_aad);
354 /* Sum_i = Sum_{i-1} xor ENCIPHER(K, A_i xor Offset_i) */
355 aad_block = (OCB_BLOCK *) (aad + ((i - ctx->blocks_hashed - 1) * 16));
356 ocb_block16_xor(&ctx->offset_aad, aad_block, &tmp1);
357 ocb_encrypt(ctx, &tmp1, &tmp2, ctx->keyenc);
358 ocb_block16_xor(&ctx->sum, &tmp2, &ctx->sum);
362 * Check if we have any partial blocks left over. This is only valid in the
363 * last call to this function
368 /* Offset_* = Offset_m xor L_* */
369 ocb_block16_xor(&ctx->offset_aad, &ctx->l_star, &ctx->offset_aad);
371 /* CipherInput = (A_* || 1 || zeros(127-bitlen(A_*))) xor Offset_* */
372 memset((void *)&tmp1, 0, 16);
373 memcpy((void *)&tmp1, aad + (num_blocks * 16), last_len);
374 ((unsigned char *)&tmp1)[last_len] = 0x80;
375 ocb_block16_xor(&ctx->offset_aad, &tmp1, &tmp2);
377 /* Sum = Sum_m xor ENCIPHER(K, CipherInput) */
378 ocb_encrypt(ctx, &tmp2, &tmp1, ctx->keyenc);
379 ocb_block16_xor(&ctx->sum, &tmp1, &ctx->sum);
382 ctx->blocks_hashed = all_num_blocks;
388 * Provide any data to be encrypted. This can be called multiple times. Only
389 * the final time can have a partial block
391 int CRYPTO_ocb128_encrypt(OCB128_CONTEXT * ctx,
392 const unsigned char *in, unsigned char *out,
396 u64 all_num_blocks, num_blocks;
403 * Calculate the number of blocks of data to be encrypted provided now, and
406 num_blocks = len / 16;
407 all_num_blocks = num_blocks + ctx->blocks_processed;
409 /* Loop through all full blocks to be encrypted */
410 for (i = ctx->blocks_processed + 1; i <= all_num_blocks; i++) {
415 /* Offset_i = Offset_{i-1} xor L_{ntz(i)} */
416 lookup = ocb_lookup_l(ctx, ocb_ntz(i));
419 ocb_block16_xor(&ctx->offset, lookup, &ctx->offset);
421 /* C_i = Offset_i xor ENCIPHER(K, P_i xor Offset_i) */
422 inblock = (OCB_BLOCK *) (in + ((i - ctx->blocks_processed - 1) * 16));
423 ocb_block16_xor(&ctx->offset, inblock, &tmp1);
424 ocb_encrypt(ctx, &tmp1, &tmp2, ctx->keyenc);
426 (OCB_BLOCK *) (out + ((i - ctx->blocks_processed - 1) * 16));
427 ocb_block16_xor(&ctx->offset, &tmp2, outblock);
429 /* Checksum_i = Checksum_{i-1} xor P_i */
430 ocb_block16_xor(&ctx->checksum, inblock, &ctx->checksum);
434 * Check if we have any partial blocks left over. This is only valid in the
435 * last call to this function
440 /* Offset_* = Offset_m xor L_* */
441 ocb_block16_xor(&ctx->offset, &ctx->l_star, &ctx->offset);
443 /* Pad = ENCIPHER(K, Offset_*) */
444 ocb_encrypt(ctx, &ctx->offset, &pad, ctx->keyenc);
446 /* C_* = P_* xor Pad[1..bitlen(P_*)] */
447 ocb_block_xor(in + (len / 16) * 16, (unsigned char *)&pad, last_len,
448 out + (num_blocks * 16));
450 /* Checksum_* = Checksum_m xor (P_* || 1 || zeros(127-bitlen(P_*))) */
451 memset((void *)&tmp1, 0, 16);
452 memcpy((void *)&tmp1, in + (len / 16) * 16, last_len);
453 ((unsigned char *)(&tmp1))[last_len] = 0x80;
454 ocb_block16_xor(&ctx->checksum, &tmp1, &ctx->checksum);
457 ctx->blocks_processed = all_num_blocks;
463 * Provide any data to be decrypted. This can be called multiple times. Only
464 * the final time can have a partial block
466 int CRYPTO_ocb128_decrypt(OCB128_CONTEXT * ctx,
467 const unsigned char *in, unsigned char *out,
471 u64 all_num_blocks, num_blocks;
477 * Calculate the number of blocks of data to be decrypted provided now, and
480 num_blocks = len / 16;
481 all_num_blocks = num_blocks + ctx->blocks_processed;
483 /* Loop through all full blocks to be decrypted */
484 for (i = ctx->blocks_processed + 1; i <= all_num_blocks; i++) {
488 /* Offset_i = Offset_{i-1} xor L_{ntz(i)} */
489 OCB_BLOCK *lookup = ocb_lookup_l(ctx, ocb_ntz(i));
492 ocb_block16_xor(&ctx->offset, lookup, &ctx->offset);
494 /* P_i = Offset_i xor DECIPHER(K, C_i xor Offset_i) */
495 inblock = (OCB_BLOCK *) (in + ((i - ctx->blocks_processed - 1) * 16));
496 ocb_block16_xor(&ctx->offset, inblock, &tmp1);
497 ocb_decrypt(ctx, &tmp1, &tmp2, ctx->keydec);
498 outblock = (OCB_BLOCK *) (out + ((i - ctx->blocks_processed - 1) * 16));
499 ocb_block16_xor(&ctx->offset, &tmp2, outblock);
501 /* Checksum_i = Checksum_{i-1} xor P_i */
502 ocb_block16_xor(&ctx->checksum, outblock, &ctx->checksum);
506 * Check if we have any partial blocks left over. This is only valid in the
507 * last call to this function
512 /* Offset_* = Offset_m xor L_* */
513 ocb_block16_xor(&ctx->offset, &ctx->l_star, &ctx->offset);
515 /* Pad = ENCIPHER(K, Offset_*) */
516 ocb_encrypt(ctx, &ctx->offset, &pad, ctx->keyenc);
518 /* P_* = C_* xor Pad[1..bitlen(C_*)] */
519 ocb_block_xor(in + (len / 16) * 16, (unsigned char *)&pad, last_len,
520 out + (num_blocks * 16));
522 /* Checksum_* = Checksum_m xor (P_* || 1 || zeros(127-bitlen(P_*))) */
523 memset((void *)&tmp1, 0, 16);
524 memcpy((void *)&tmp1, out + (len / 16) * 16, last_len);
525 ((unsigned char *)(&tmp1))[last_len] = 0x80;
526 ocb_block16_xor(&ctx->checksum, &tmp1, &ctx->checksum);
529 ctx->blocks_processed = all_num_blocks;
535 * Calculate the tag and verify it against the supplied tag
537 int CRYPTO_ocb128_finish(OCB128_CONTEXT * ctx, const unsigned char *tag,
540 OCB_BLOCK tmp1, tmp2;
542 /*Tag = ENCIPHER(K, Checksum_* xor Offset_* xor L_$) xor HASH(K,A) */
543 ocb_block16_xor(&ctx->checksum, &ctx->offset, &tmp1);
544 ocb_block16_xor(&tmp1, &ctx->l_dollar, &tmp2);
545 ocb_encrypt(ctx, &tmp2, &tmp1, ctx->keyenc);
546 ocb_block16_xor(&tmp1, &ctx->sum, &ctx->tag);
548 if (len > 16 || len < 1) {
552 /* Compare the tag if we've been given one */
554 return CRYPTO_memcmp(&ctx->tag, tag, len);
560 * Retrieve the calculated tag
562 int CRYPTO_ocb128_tag(OCB128_CONTEXT * ctx, unsigned char *tag, size_t len)
564 if (len > 16 || len < 1) {
568 /* Calculate the tag */
569 CRYPTO_ocb128_finish(ctx, NULL, 0);
571 /* Copy the tag into the supplied buffer */
572 memcpy(tag, &ctx->tag, len);
578 * Release all resources
580 void CRYPTO_ocb128_cleanup(OCB128_CONTEXT * ctx)
584 OPENSSL_cleanse(ctx->l, ctx->max_l_index * 16);
585 OPENSSL_free(ctx->l);
587 OPENSSL_cleanse(ctx, sizeof(*ctx));
591 #endif /* OPENSSL_NO_OCB */