2
0

ocb128.c 16 KB

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