ocb128.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  1. /*
  2. * Copyright 2014-2020 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <string.h>
  10. #include <openssl/crypto.h>
  11. #include <openssl/err.h>
  12. #include "crypto/modes.h"
  13. #ifndef OPENSSL_NO_OCB
  14. /*
  15. * Calculate the number of binary trailing zero's in any given number
  16. */
  17. static u32 ocb_ntz(u64 n)
  18. {
  19. u32 cnt = 0;
  20. /*
  21. * We do a right-to-left simple sequential search. This is surprisingly
  22. * efficient as the distribution of trailing zeros is not uniform,
  23. * e.g. the number of possible inputs with no trailing zeros is equal to
  24. * the number with 1 or more; the number with exactly 1 is equal to the
  25. * number with 2 or more, etc. Checking the last two bits covers 75% of
  26. * all numbers. Checking the last three covers 87.5%
  27. */
  28. while (!(n & 1)) {
  29. n >>= 1;
  30. cnt++;
  31. }
  32. return cnt;
  33. }
  34. /*
  35. * Shift a block of 16 bytes left by shift bits
  36. */
  37. static void ocb_block_lshift(const unsigned char *in, size_t shift,
  38. unsigned char *out)
  39. {
  40. int i;
  41. unsigned char carry = 0, carry_next;
  42. for (i = 15; i >= 0; i--) {
  43. carry_next = in[i] >> (8 - shift);
  44. out[i] = (in[i] << shift) | carry;
  45. carry = carry_next;
  46. }
  47. }
  48. /*
  49. * Perform a "double" operation as per OCB spec
  50. */
  51. static void ocb_double(OCB_BLOCK *in, OCB_BLOCK *out)
  52. {
  53. unsigned char mask;
  54. /*
  55. * Calculate the mask based on the most significant bit. There are more
  56. * efficient ways to do this - but this way is constant time
  57. */
  58. mask = in->c[0] & 0x80;
  59. mask >>= 7;
  60. mask = (0 - mask) & 0x87;
  61. ocb_block_lshift(in->c, 1, out->c);
  62. out->c[15] ^= mask;
  63. }
  64. /*
  65. * Perform an xor on in1 and in2 - each of len bytes. Store result in out
  66. */
  67. static void ocb_block_xor(const unsigned char *in1,
  68. const unsigned char *in2, size_t len,
  69. unsigned char *out)
  70. {
  71. size_t i;
  72. for (i = 0; i < len; i++) {
  73. out[i] = in1[i] ^ in2[i];
  74. }
  75. }
  76. /*
  77. * Lookup L_index in our lookup table. If we haven't already got it we need to
  78. * calculate it
  79. */
  80. static OCB_BLOCK *ocb_lookup_l(OCB128_CONTEXT *ctx, size_t idx)
  81. {
  82. size_t l_index = ctx->l_index;
  83. if (idx <= l_index) {
  84. return ctx->l + idx;
  85. }
  86. /* We don't have it - so calculate it */
  87. if (idx >= ctx->max_l_index) {
  88. void *tmp_ptr;
  89. /*
  90. * Each additional entry allows to process almost double as
  91. * much data, so that in linear world the table will need to
  92. * be expanded with smaller and smaller increments. Originally
  93. * it was doubling in size, which was a waste. Growing it
  94. * linearly is not formally optimal, but is simpler to implement.
  95. * We grow table by minimally required 4*n that would accommodate
  96. * the index.
  97. */
  98. ctx->max_l_index += (idx - ctx->max_l_index + 4) & ~3;
  99. tmp_ptr = OPENSSL_realloc(ctx->l, ctx->max_l_index * sizeof(OCB_BLOCK));
  100. if (tmp_ptr == NULL) /* prevent ctx->l from being clobbered */
  101. return NULL;
  102. ctx->l = tmp_ptr;
  103. }
  104. while (l_index < idx) {
  105. ocb_double(ctx->l + l_index, ctx->l + l_index + 1);
  106. l_index++;
  107. }
  108. ctx->l_index = l_index;
  109. return ctx->l + idx;
  110. }
  111. /*
  112. * Create a new OCB128_CONTEXT
  113. */
  114. OCB128_CONTEXT *CRYPTO_ocb128_new(void *keyenc, void *keydec,
  115. block128_f encrypt, block128_f decrypt,
  116. ocb128_f stream)
  117. {
  118. OCB128_CONTEXT *octx;
  119. int ret;
  120. if ((octx = OPENSSL_malloc(sizeof(*octx))) != NULL) {
  121. ret = CRYPTO_ocb128_init(octx, keyenc, keydec, encrypt, decrypt,
  122. stream);
  123. if (ret)
  124. return octx;
  125. OPENSSL_free(octx);
  126. }
  127. return NULL;
  128. }
  129. /*
  130. * Initialise an existing OCB128_CONTEXT
  131. */
  132. int CRYPTO_ocb128_init(OCB128_CONTEXT *ctx, void *keyenc, void *keydec,
  133. block128_f encrypt, block128_f decrypt,
  134. ocb128_f stream)
  135. {
  136. memset(ctx, 0, sizeof(*ctx));
  137. ctx->l_index = 0;
  138. ctx->max_l_index = 5;
  139. if ((ctx->l = OPENSSL_malloc(ctx->max_l_index * 16)) == NULL)
  140. return 0;
  141. /*
  142. * We set both the encryption and decryption key schedules - decryption
  143. * needs both. Don't really need decryption schedule if only doing
  144. * encryption - but it simplifies things to take it anyway
  145. */
  146. ctx->encrypt = encrypt;
  147. ctx->decrypt = decrypt;
  148. ctx->stream = stream;
  149. ctx->keyenc = keyenc;
  150. ctx->keydec = keydec;
  151. /* L_* = ENCIPHER(K, zeros(128)) */
  152. ctx->encrypt(ctx->l_star.c, ctx->l_star.c, ctx->keyenc);
  153. /* L_$ = double(L_*) */
  154. ocb_double(&ctx->l_star, &ctx->l_dollar);
  155. /* L_0 = double(L_$) */
  156. ocb_double(&ctx->l_dollar, ctx->l);
  157. /* L_{i} = double(L_{i-1}) */
  158. ocb_double(ctx->l, ctx->l+1);
  159. ocb_double(ctx->l+1, ctx->l+2);
  160. ocb_double(ctx->l+2, ctx->l+3);
  161. ocb_double(ctx->l+3, ctx->l+4);
  162. ctx->l_index = 4; /* enough to process up to 496 bytes */
  163. return 1;
  164. }
  165. /*
  166. * Copy an OCB128_CONTEXT object
  167. */
  168. int CRYPTO_ocb128_copy_ctx(OCB128_CONTEXT *dest, OCB128_CONTEXT *src,
  169. void *keyenc, void *keydec)
  170. {
  171. memcpy(dest, src, sizeof(OCB128_CONTEXT));
  172. if (keyenc)
  173. dest->keyenc = keyenc;
  174. if (keydec)
  175. dest->keydec = keydec;
  176. if (src->l) {
  177. if ((dest->l = OPENSSL_malloc(src->max_l_index * 16)) == NULL)
  178. return 0;
  179. memcpy(dest->l, src->l, (src->l_index + 1) * 16);
  180. }
  181. return 1;
  182. }
  183. /*
  184. * Set the IV to be used for this operation. Must be 1 - 15 bytes.
  185. */
  186. int CRYPTO_ocb128_setiv(OCB128_CONTEXT *ctx, const unsigned char *iv,
  187. size_t len, size_t taglen)
  188. {
  189. unsigned char ktop[16], tmp[16], mask;
  190. unsigned char stretch[24], nonce[16];
  191. size_t bottom, shift;
  192. /*
  193. * Spec says IV is 120 bits or fewer - it allows non byte aligned lengths.
  194. * We don't support this at this stage
  195. */
  196. if ((len > 15) || (len < 1) || (taglen > 16) || (taglen < 1)) {
  197. return -1;
  198. }
  199. /* Reset nonce-dependent variables */
  200. memset(&ctx->sess, 0, sizeof(ctx->sess));
  201. /* Nonce = num2str(TAGLEN mod 128,7) || zeros(120-bitlen(N)) || 1 || N */
  202. nonce[0] = ((taglen * 8) % 128) << 1;
  203. memset(nonce + 1, 0, 15);
  204. memcpy(nonce + 16 - len, iv, len);
  205. nonce[15 - len] |= 1;
  206. /* Ktop = ENCIPHER(K, Nonce[1..122] || zeros(6)) */
  207. memcpy(tmp, nonce, 16);
  208. tmp[15] &= 0xc0;
  209. ctx->encrypt(tmp, ktop, ctx->keyenc);
  210. /* Stretch = Ktop || (Ktop[1..64] xor Ktop[9..72]) */
  211. memcpy(stretch, ktop, 16);
  212. ocb_block_xor(ktop, ktop + 1, 8, stretch + 16);
  213. /* bottom = str2num(Nonce[123..128]) */
  214. bottom = nonce[15] & 0x3f;
  215. /* Offset_0 = Stretch[1+bottom..128+bottom] */
  216. shift = bottom % 8;
  217. ocb_block_lshift(stretch + (bottom / 8), shift, ctx->sess.offset.c);
  218. mask = 0xff;
  219. mask <<= 8 - shift;
  220. ctx->sess.offset.c[15] |=
  221. (*(stretch + (bottom / 8) + 16) & mask) >> (8 - shift);
  222. return 1;
  223. }
  224. /*
  225. * Provide any AAD. This can be called multiple times. Only the final time can
  226. * have a partial block
  227. */
  228. int CRYPTO_ocb128_aad(OCB128_CONTEXT *ctx, const unsigned char *aad,
  229. size_t len)
  230. {
  231. u64 i, all_num_blocks;
  232. size_t num_blocks, last_len;
  233. OCB_BLOCK tmp;
  234. /* Calculate the number of blocks of AAD provided now, and so far */
  235. num_blocks = len / 16;
  236. all_num_blocks = num_blocks + ctx->sess.blocks_hashed;
  237. /* Loop through all full blocks of AAD */
  238. for (i = ctx->sess.blocks_hashed + 1; i <= all_num_blocks; i++) {
  239. OCB_BLOCK *lookup;
  240. /* Offset_i = Offset_{i-1} xor L_{ntz(i)} */
  241. lookup = ocb_lookup_l(ctx, ocb_ntz(i));
  242. if (lookup == NULL)
  243. return 0;
  244. ocb_block16_xor(&ctx->sess.offset_aad, lookup, &ctx->sess.offset_aad);
  245. memcpy(tmp.c, aad, 16);
  246. aad += 16;
  247. /* Sum_i = Sum_{i-1} xor ENCIPHER(K, A_i xor Offset_i) */
  248. ocb_block16_xor(&ctx->sess.offset_aad, &tmp, &tmp);
  249. ctx->encrypt(tmp.c, tmp.c, ctx->keyenc);
  250. ocb_block16_xor(&tmp, &ctx->sess.sum, &ctx->sess.sum);
  251. }
  252. /*
  253. * Check if we have any partial blocks left over. This is only valid in the
  254. * last call to this function
  255. */
  256. last_len = len % 16;
  257. if (last_len > 0) {
  258. /* Offset_* = Offset_m xor L_* */
  259. ocb_block16_xor(&ctx->sess.offset_aad, &ctx->l_star,
  260. &ctx->sess.offset_aad);
  261. /* CipherInput = (A_* || 1 || zeros(127-bitlen(A_*))) xor Offset_* */
  262. memset(tmp.c, 0, 16);
  263. memcpy(tmp.c, aad, last_len);
  264. tmp.c[last_len] = 0x80;
  265. ocb_block16_xor(&ctx->sess.offset_aad, &tmp, &tmp);
  266. /* Sum = Sum_m xor ENCIPHER(K, CipherInput) */
  267. ctx->encrypt(tmp.c, tmp.c, ctx->keyenc);
  268. ocb_block16_xor(&tmp, &ctx->sess.sum, &ctx->sess.sum);
  269. }
  270. ctx->sess.blocks_hashed = all_num_blocks;
  271. return 1;
  272. }
  273. /*
  274. * Provide any data to be encrypted. This can be called multiple times. Only
  275. * the final time can have a partial block
  276. */
  277. int CRYPTO_ocb128_encrypt(OCB128_CONTEXT *ctx,
  278. const unsigned char *in, unsigned char *out,
  279. size_t len)
  280. {
  281. u64 i, all_num_blocks;
  282. size_t num_blocks, last_len;
  283. /*
  284. * Calculate the number of blocks of data to be encrypted provided now, and
  285. * so far
  286. */
  287. num_blocks = len / 16;
  288. all_num_blocks = num_blocks + ctx->sess.blocks_processed;
  289. if (num_blocks && all_num_blocks == (size_t)all_num_blocks
  290. && ctx->stream != NULL) {
  291. size_t max_idx = 0, top = (size_t)all_num_blocks;
  292. /*
  293. * See how many L_{i} entries we need to process data at hand
  294. * and pre-compute missing entries in the table [if any]...
  295. */
  296. while (top >>= 1)
  297. max_idx++;
  298. if (ocb_lookup_l(ctx, max_idx) == NULL)
  299. return 0;
  300. ctx->stream(in, out, num_blocks, ctx->keyenc,
  301. (size_t)ctx->sess.blocks_processed + 1, ctx->sess.offset.c,
  302. (const unsigned char (*)[16])ctx->l, ctx->sess.checksum.c);
  303. } else {
  304. /* Loop through all full blocks to be encrypted */
  305. for (i = ctx->sess.blocks_processed + 1; i <= all_num_blocks; i++) {
  306. OCB_BLOCK *lookup;
  307. OCB_BLOCK tmp;
  308. /* Offset_i = Offset_{i-1} xor L_{ntz(i)} */
  309. lookup = ocb_lookup_l(ctx, ocb_ntz(i));
  310. if (lookup == NULL)
  311. return 0;
  312. ocb_block16_xor(&ctx->sess.offset, lookup, &ctx->sess.offset);
  313. memcpy(tmp.c, in, 16);
  314. in += 16;
  315. /* Checksum_i = Checksum_{i-1} xor P_i */
  316. ocb_block16_xor(&tmp, &ctx->sess.checksum, &ctx->sess.checksum);
  317. /* C_i = Offset_i xor ENCIPHER(K, P_i xor Offset_i) */
  318. ocb_block16_xor(&ctx->sess.offset, &tmp, &tmp);
  319. ctx->encrypt(tmp.c, tmp.c, ctx->keyenc);
  320. ocb_block16_xor(&ctx->sess.offset, &tmp, &tmp);
  321. memcpy(out, tmp.c, 16);
  322. out += 16;
  323. }
  324. }
  325. /*
  326. * Check if we have any partial blocks left over. This is only valid in the
  327. * last call to this function
  328. */
  329. last_len = len % 16;
  330. if (last_len > 0) {
  331. OCB_BLOCK pad;
  332. /* Offset_* = Offset_m xor L_* */
  333. ocb_block16_xor(&ctx->sess.offset, &ctx->l_star, &ctx->sess.offset);
  334. /* Pad = ENCIPHER(K, Offset_*) */
  335. ctx->encrypt(ctx->sess.offset.c, pad.c, ctx->keyenc);
  336. /* C_* = P_* xor Pad[1..bitlen(P_*)] */
  337. ocb_block_xor(in, pad.c, last_len, out);
  338. /* Checksum_* = Checksum_m xor (P_* || 1 || zeros(127-bitlen(P_*))) */
  339. memset(pad.c, 0, 16); /* borrow pad */
  340. memcpy(pad.c, in, last_len);
  341. pad.c[last_len] = 0x80;
  342. ocb_block16_xor(&pad, &ctx->sess.checksum, &ctx->sess.checksum);
  343. }
  344. ctx->sess.blocks_processed = all_num_blocks;
  345. return 1;
  346. }
  347. /*
  348. * Provide any data to be decrypted. This can be called multiple times. Only
  349. * the final time can have a partial block
  350. */
  351. int CRYPTO_ocb128_decrypt(OCB128_CONTEXT *ctx,
  352. const unsigned char *in, unsigned char *out,
  353. size_t len)
  354. {
  355. u64 i, all_num_blocks;
  356. size_t num_blocks, last_len;
  357. /*
  358. * Calculate the number of blocks of data to be decrypted provided now, and
  359. * so far
  360. */
  361. num_blocks = len / 16;
  362. all_num_blocks = num_blocks + ctx->sess.blocks_processed;
  363. if (num_blocks && all_num_blocks == (size_t)all_num_blocks
  364. && ctx->stream != NULL) {
  365. size_t max_idx = 0, top = (size_t)all_num_blocks;
  366. /*
  367. * See how many L_{i} entries we need to process data at hand
  368. * and pre-compute missing entries in the table [if any]...
  369. */
  370. while (top >>= 1)
  371. max_idx++;
  372. if (ocb_lookup_l(ctx, max_idx) == NULL)
  373. return 0;
  374. ctx->stream(in, out, num_blocks, ctx->keydec,
  375. (size_t)ctx->sess.blocks_processed + 1, ctx->sess.offset.c,
  376. (const unsigned char (*)[16])ctx->l, ctx->sess.checksum.c);
  377. } else {
  378. OCB_BLOCK tmp;
  379. /* Loop through all full blocks to be decrypted */
  380. for (i = ctx->sess.blocks_processed + 1; i <= all_num_blocks; i++) {
  381. /* Offset_i = Offset_{i-1} xor L_{ntz(i)} */
  382. OCB_BLOCK *lookup = ocb_lookup_l(ctx, ocb_ntz(i));
  383. if (lookup == NULL)
  384. return 0;
  385. ocb_block16_xor(&ctx->sess.offset, lookup, &ctx->sess.offset);
  386. memcpy(tmp.c, in, 16);
  387. in += 16;
  388. /* P_i = Offset_i xor DECIPHER(K, C_i xor Offset_i) */
  389. ocb_block16_xor(&ctx->sess.offset, &tmp, &tmp);
  390. ctx->decrypt(tmp.c, tmp.c, ctx->keydec);
  391. ocb_block16_xor(&ctx->sess.offset, &tmp, &tmp);
  392. /* Checksum_i = Checksum_{i-1} xor P_i */
  393. ocb_block16_xor(&tmp, &ctx->sess.checksum, &ctx->sess.checksum);
  394. memcpy(out, tmp.c, 16);
  395. out += 16;
  396. }
  397. }
  398. /*
  399. * Check if we have any partial blocks left over. This is only valid in the
  400. * last call to this function
  401. */
  402. last_len = len % 16;
  403. if (last_len > 0) {
  404. OCB_BLOCK pad;
  405. /* Offset_* = Offset_m xor L_* */
  406. ocb_block16_xor(&ctx->sess.offset, &ctx->l_star, &ctx->sess.offset);
  407. /* Pad = ENCIPHER(K, Offset_*) */
  408. ctx->encrypt(ctx->sess.offset.c, pad.c, ctx->keyenc);
  409. /* P_* = C_* xor Pad[1..bitlen(C_*)] */
  410. ocb_block_xor(in, pad.c, last_len, out);
  411. /* Checksum_* = Checksum_m xor (P_* || 1 || zeros(127-bitlen(P_*))) */
  412. memset(pad.c, 0, 16); /* borrow pad */
  413. memcpy(pad.c, out, last_len);
  414. pad.c[last_len] = 0x80;
  415. ocb_block16_xor(&pad, &ctx->sess.checksum, &ctx->sess.checksum);
  416. }
  417. ctx->sess.blocks_processed = all_num_blocks;
  418. return 1;
  419. }
  420. static int ocb_finish(OCB128_CONTEXT *ctx, unsigned char *tag, size_t len,
  421. int write)
  422. {
  423. OCB_BLOCK tmp;
  424. if (len > 16 || len < 1) {
  425. return -1;
  426. }
  427. /*
  428. * Tag = ENCIPHER(K, Checksum_* xor Offset_* xor L_$) xor HASH(K,A)
  429. */
  430. ocb_block16_xor(&ctx->sess.checksum, &ctx->sess.offset, &tmp);
  431. ocb_block16_xor(&ctx->l_dollar, &tmp, &tmp);
  432. ctx->encrypt(tmp.c, tmp.c, ctx->keyenc);
  433. ocb_block16_xor(&tmp, &ctx->sess.sum, &tmp);
  434. if (write) {
  435. memcpy(tag, &tmp, len);
  436. return 1;
  437. } else {
  438. return CRYPTO_memcmp(&tmp, tag, len);
  439. }
  440. }
  441. /*
  442. * Calculate the tag and verify it against the supplied tag
  443. */
  444. int CRYPTO_ocb128_finish(OCB128_CONTEXT *ctx, const unsigned char *tag,
  445. size_t len)
  446. {
  447. return ocb_finish(ctx, (unsigned char*)tag, len, 0);
  448. }
  449. /*
  450. * Retrieve the calculated tag
  451. */
  452. int CRYPTO_ocb128_tag(OCB128_CONTEXT *ctx, unsigned char *tag, size_t len)
  453. {
  454. return ocb_finish(ctx, tag, len, 1);
  455. }
  456. /*
  457. * Release all resources
  458. */
  459. void CRYPTO_ocb128_cleanup(OCB128_CONTEXT *ctx)
  460. {
  461. if (ctx) {
  462. OPENSSL_clear_free(ctx->l, ctx->max_l_index * 16);
  463. OPENSSL_cleanse(ctx, sizeof(*ctx));
  464. }
  465. }
  466. #endif /* OPENSSL_NO_OCB */