ccm128.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  1. /* ====================================================================
  2. * Copyright (c) 2011 The OpenSSL Project. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. *
  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
  14. * distribution.
  15. *
  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/)"
  20. *
  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.
  25. *
  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.
  29. *
  30. * 6. Redistributions of any form whatsoever must retain the following
  31. * acknowledgment:
  32. * "This product includes software developed by the OpenSSL Project
  33. * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
  34. *
  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. * ====================================================================
  48. */
  49. #include <openssl/crypto.h>
  50. #include "modes_lcl.h"
  51. #include <string.h>
  52. #ifndef MODES_DEBUG
  53. # ifndef NDEBUG
  54. # define NDEBUG
  55. # endif
  56. #endif
  57. #include <assert.h>
  58. /*
  59. * First you setup M and L parameters and pass the key schedule. This is
  60. * called once per session setup...
  61. */
  62. void CRYPTO_ccm128_init(CCM128_CONTEXT *ctx,
  63. unsigned int M, unsigned int L, void *key,
  64. block128_f block)
  65. {
  66. memset(ctx->nonce.c, 0, sizeof(ctx->nonce.c));
  67. ctx->nonce.c[0] = ((u8)(L - 1) & 7) | (u8)(((M - 2) / 2) & 7) << 3;
  68. ctx->blocks = 0;
  69. ctx->block = block;
  70. ctx->key = key;
  71. }
  72. /* !!! Following interfaces are to be called *once* per packet !!! */
  73. /* Then you setup per-message nonce and pass the length of the message */
  74. int CRYPTO_ccm128_setiv(CCM128_CONTEXT *ctx,
  75. const unsigned char *nonce, size_t nlen, size_t mlen)
  76. {
  77. unsigned int L = ctx->nonce.c[0] & 7; /* the L parameter */
  78. if (nlen < (14 - L))
  79. return -1; /* nonce is too short */
  80. if (sizeof(mlen) == 8 && L >= 3) {
  81. ctx->nonce.c[8] = (u8)(mlen >> (56 % (sizeof(mlen) * 8)));
  82. ctx->nonce.c[9] = (u8)(mlen >> (48 % (sizeof(mlen) * 8)));
  83. ctx->nonce.c[10] = (u8)(mlen >> (40 % (sizeof(mlen) * 8)));
  84. ctx->nonce.c[11] = (u8)(mlen >> (32 % (sizeof(mlen) * 8)));
  85. } else
  86. ctx->nonce.u[1] = 0;
  87. ctx->nonce.c[12] = (u8)(mlen >> 24);
  88. ctx->nonce.c[13] = (u8)(mlen >> 16);
  89. ctx->nonce.c[14] = (u8)(mlen >> 8);
  90. ctx->nonce.c[15] = (u8)mlen;
  91. ctx->nonce.c[0] &= ~0x40; /* clear Adata flag */
  92. memcpy(&ctx->nonce.c[1], nonce, 14 - L);
  93. return 0;
  94. }
  95. /* Then you pass additional authentication data, this is optional */
  96. void CRYPTO_ccm128_aad(CCM128_CONTEXT *ctx,
  97. const unsigned char *aad, size_t alen)
  98. {
  99. unsigned int i;
  100. block128_f block = ctx->block;
  101. if (alen == 0)
  102. return;
  103. ctx->nonce.c[0] |= 0x40; /* set Adata flag */
  104. (*block) (ctx->nonce.c, ctx->cmac.c, ctx->key), ctx->blocks++;
  105. if (alen < (0x10000 - 0x100)) {
  106. ctx->cmac.c[0] ^= (u8)(alen >> 8);
  107. ctx->cmac.c[1] ^= (u8)alen;
  108. i = 2;
  109. } else if (sizeof(alen) == 8
  110. && alen >= (size_t)1 << (32 % (sizeof(alen) * 8))) {
  111. ctx->cmac.c[0] ^= 0xFF;
  112. ctx->cmac.c[1] ^= 0xFF;
  113. ctx->cmac.c[2] ^= (u8)(alen >> (56 % (sizeof(alen) * 8)));
  114. ctx->cmac.c[3] ^= (u8)(alen >> (48 % (sizeof(alen) * 8)));
  115. ctx->cmac.c[4] ^= (u8)(alen >> (40 % (sizeof(alen) * 8)));
  116. ctx->cmac.c[5] ^= (u8)(alen >> (32 % (sizeof(alen) * 8)));
  117. ctx->cmac.c[6] ^= (u8)(alen >> 24);
  118. ctx->cmac.c[7] ^= (u8)(alen >> 16);
  119. ctx->cmac.c[8] ^= (u8)(alen >> 8);
  120. ctx->cmac.c[9] ^= (u8)alen;
  121. i = 10;
  122. } else {
  123. ctx->cmac.c[0] ^= 0xFF;
  124. ctx->cmac.c[1] ^= 0xFE;
  125. ctx->cmac.c[2] ^= (u8)(alen >> 24);
  126. ctx->cmac.c[3] ^= (u8)(alen >> 16);
  127. ctx->cmac.c[4] ^= (u8)(alen >> 8);
  128. ctx->cmac.c[5] ^= (u8)alen;
  129. i = 6;
  130. }
  131. do {
  132. for (; i < 16 && alen; ++i, ++aad, --alen)
  133. ctx->cmac.c[i] ^= *aad;
  134. (*block) (ctx->cmac.c, ctx->cmac.c, ctx->key), ctx->blocks++;
  135. i = 0;
  136. } while (alen);
  137. }
  138. /* Finally you encrypt or decrypt the message */
  139. /*
  140. * counter part of nonce may not be larger than L*8 bits, L is not larger
  141. * than 8, therefore 64-bit counter...
  142. */
  143. static void ctr64_inc(unsigned char *counter)
  144. {
  145. unsigned int n = 8;
  146. u8 c;
  147. counter += 8;
  148. do {
  149. --n;
  150. c = counter[n];
  151. ++c;
  152. counter[n] = c;
  153. if (c)
  154. return;
  155. } while (n);
  156. }
  157. int CRYPTO_ccm128_encrypt(CCM128_CONTEXT *ctx,
  158. const unsigned char *inp, unsigned char *out,
  159. size_t len)
  160. {
  161. size_t n;
  162. unsigned int i, L;
  163. unsigned char flags0 = ctx->nonce.c[0];
  164. block128_f block = ctx->block;
  165. void *key = ctx->key;
  166. union {
  167. u64 u[2];
  168. u8 c[16];
  169. } scratch;
  170. if (!(flags0 & 0x40))
  171. (*block) (ctx->nonce.c, ctx->cmac.c, key), ctx->blocks++;
  172. ctx->nonce.c[0] = L = flags0 & 7;
  173. for (n = 0, i = 15 - L; i < 15; ++i) {
  174. n |= ctx->nonce.c[i];
  175. ctx->nonce.c[i] = 0;
  176. n <<= 8;
  177. }
  178. n |= ctx->nonce.c[15]; /* reconstructed length */
  179. ctx->nonce.c[15] = 1;
  180. if (n != len)
  181. return -1; /* length mismatch */
  182. ctx->blocks += ((len + 15) >> 3) | 1;
  183. if (ctx->blocks > (U64(1) << 61))
  184. return -2; /* too much data */
  185. while (len >= 16) {
  186. #if defined(STRICT_ALIGNMENT)
  187. union {
  188. u64 u[2];
  189. u8 c[16];
  190. } temp;
  191. memcpy(temp.c, inp, 16);
  192. ctx->cmac.u[0] ^= temp.u[0];
  193. ctx->cmac.u[1] ^= temp.u[1];
  194. #else
  195. ctx->cmac.u[0] ^= ((u64 *)inp)[0];
  196. ctx->cmac.u[1] ^= ((u64 *)inp)[1];
  197. #endif
  198. (*block) (ctx->cmac.c, ctx->cmac.c, key);
  199. (*block) (ctx->nonce.c, scratch.c, key);
  200. ctr64_inc(ctx->nonce.c);
  201. #if defined(STRICT_ALIGNMENT)
  202. temp.u[0] ^= scratch.u[0];
  203. temp.u[1] ^= scratch.u[1];
  204. memcpy(out, temp.c, 16);
  205. #else
  206. ((u64 *)out)[0] = scratch.u[0] ^ ((u64 *)inp)[0];
  207. ((u64 *)out)[1] = scratch.u[1] ^ ((u64 *)inp)[1];
  208. #endif
  209. inp += 16;
  210. out += 16;
  211. len -= 16;
  212. }
  213. if (len) {
  214. for (i = 0; i < len; ++i)
  215. ctx->cmac.c[i] ^= inp[i];
  216. (*block) (ctx->cmac.c, ctx->cmac.c, key);
  217. (*block) (ctx->nonce.c, scratch.c, key);
  218. for (i = 0; i < len; ++i)
  219. out[i] = scratch.c[i] ^ inp[i];
  220. }
  221. for (i = 15 - L; i < 16; ++i)
  222. ctx->nonce.c[i] = 0;
  223. (*block) (ctx->nonce.c, scratch.c, key);
  224. ctx->cmac.u[0] ^= scratch.u[0];
  225. ctx->cmac.u[1] ^= scratch.u[1];
  226. ctx->nonce.c[0] = flags0;
  227. return 0;
  228. }
  229. int CRYPTO_ccm128_decrypt(CCM128_CONTEXT *ctx,
  230. const unsigned char *inp, unsigned char *out,
  231. size_t len)
  232. {
  233. size_t n;
  234. unsigned int i, L;
  235. unsigned char flags0 = ctx->nonce.c[0];
  236. block128_f block = ctx->block;
  237. void *key = ctx->key;
  238. union {
  239. u64 u[2];
  240. u8 c[16];
  241. } scratch;
  242. if (!(flags0 & 0x40))
  243. (*block) (ctx->nonce.c, ctx->cmac.c, key);
  244. ctx->nonce.c[0] = L = flags0 & 7;
  245. for (n = 0, i = 15 - L; i < 15; ++i) {
  246. n |= ctx->nonce.c[i];
  247. ctx->nonce.c[i] = 0;
  248. n <<= 8;
  249. }
  250. n |= ctx->nonce.c[15]; /* reconstructed length */
  251. ctx->nonce.c[15] = 1;
  252. if (n != len)
  253. return -1;
  254. while (len >= 16) {
  255. #if defined(STRICT_ALIGNMENT)
  256. union {
  257. u64 u[2];
  258. u8 c[16];
  259. } temp;
  260. #endif
  261. (*block) (ctx->nonce.c, scratch.c, key);
  262. ctr64_inc(ctx->nonce.c);
  263. #if defined(STRICT_ALIGNMENT)
  264. memcpy(temp.c, inp, 16);
  265. ctx->cmac.u[0] ^= (scratch.u[0] ^= temp.u[0]);
  266. ctx->cmac.u[1] ^= (scratch.u[1] ^= temp.u[1]);
  267. memcpy(out, scratch.c, 16);
  268. #else
  269. ctx->cmac.u[0] ^= (((u64 *)out)[0] = scratch.u[0] ^ ((u64 *)inp)[0]);
  270. ctx->cmac.u[1] ^= (((u64 *)out)[1] = scratch.u[1] ^ ((u64 *)inp)[1]);
  271. #endif
  272. (*block) (ctx->cmac.c, ctx->cmac.c, key);
  273. inp += 16;
  274. out += 16;
  275. len -= 16;
  276. }
  277. if (len) {
  278. (*block) (ctx->nonce.c, scratch.c, key);
  279. for (i = 0; i < len; ++i)
  280. ctx->cmac.c[i] ^= (out[i] = scratch.c[i] ^ inp[i]);
  281. (*block) (ctx->cmac.c, ctx->cmac.c, key);
  282. }
  283. for (i = 15 - L; i < 16; ++i)
  284. ctx->nonce.c[i] = 0;
  285. (*block) (ctx->nonce.c, scratch.c, key);
  286. ctx->cmac.u[0] ^= scratch.u[0];
  287. ctx->cmac.u[1] ^= scratch.u[1];
  288. ctx->nonce.c[0] = flags0;
  289. return 0;
  290. }
  291. static void ctr64_add(unsigned char *counter, size_t inc)
  292. {
  293. size_t n = 8, val = 0;
  294. counter += 8;
  295. do {
  296. --n;
  297. val += counter[n] + (inc & 0xff);
  298. counter[n] = (unsigned char)val;
  299. val >>= 8; /* carry bit */
  300. inc >>= 8;
  301. } while (n && (inc || val));
  302. }
  303. int CRYPTO_ccm128_encrypt_ccm64(CCM128_CONTEXT *ctx,
  304. const unsigned char *inp, unsigned char *out,
  305. size_t len, ccm128_f stream)
  306. {
  307. size_t n;
  308. unsigned int i, L;
  309. unsigned char flags0 = ctx->nonce.c[0];
  310. block128_f block = ctx->block;
  311. void *key = ctx->key;
  312. union {
  313. u64 u[2];
  314. u8 c[16];
  315. } scratch;
  316. if (!(flags0 & 0x40))
  317. (*block) (ctx->nonce.c, ctx->cmac.c, key), ctx->blocks++;
  318. ctx->nonce.c[0] = L = flags0 & 7;
  319. for (n = 0, i = 15 - L; i < 15; ++i) {
  320. n |= ctx->nonce.c[i];
  321. ctx->nonce.c[i] = 0;
  322. n <<= 8;
  323. }
  324. n |= ctx->nonce.c[15]; /* reconstructed length */
  325. ctx->nonce.c[15] = 1;
  326. if (n != len)
  327. return -1; /* length mismatch */
  328. ctx->blocks += ((len + 15) >> 3) | 1;
  329. if (ctx->blocks > (U64(1) << 61))
  330. return -2; /* too much data */
  331. if ((n = len / 16)) {
  332. (*stream) (inp, out, n, key, ctx->nonce.c, ctx->cmac.c);
  333. n *= 16;
  334. inp += n;
  335. out += n;
  336. len -= n;
  337. if (len)
  338. ctr64_add(ctx->nonce.c, n / 16);
  339. }
  340. if (len) {
  341. for (i = 0; i < len; ++i)
  342. ctx->cmac.c[i] ^= inp[i];
  343. (*block) (ctx->cmac.c, ctx->cmac.c, key);
  344. (*block) (ctx->nonce.c, scratch.c, key);
  345. for (i = 0; i < len; ++i)
  346. out[i] = scratch.c[i] ^ inp[i];
  347. }
  348. for (i = 15 - L; i < 16; ++i)
  349. ctx->nonce.c[i] = 0;
  350. (*block) (ctx->nonce.c, scratch.c, key);
  351. ctx->cmac.u[0] ^= scratch.u[0];
  352. ctx->cmac.u[1] ^= scratch.u[1];
  353. ctx->nonce.c[0] = flags0;
  354. return 0;
  355. }
  356. int CRYPTO_ccm128_decrypt_ccm64(CCM128_CONTEXT *ctx,
  357. const unsigned char *inp, unsigned char *out,
  358. size_t len, ccm128_f stream)
  359. {
  360. size_t n;
  361. unsigned int i, L;
  362. unsigned char flags0 = ctx->nonce.c[0];
  363. block128_f block = ctx->block;
  364. void *key = ctx->key;
  365. union {
  366. u64 u[2];
  367. u8 c[16];
  368. } scratch;
  369. if (!(flags0 & 0x40))
  370. (*block) (ctx->nonce.c, ctx->cmac.c, key);
  371. ctx->nonce.c[0] = L = flags0 & 7;
  372. for (n = 0, i = 15 - L; i < 15; ++i) {
  373. n |= ctx->nonce.c[i];
  374. ctx->nonce.c[i] = 0;
  375. n <<= 8;
  376. }
  377. n |= ctx->nonce.c[15]; /* reconstructed length */
  378. ctx->nonce.c[15] = 1;
  379. if (n != len)
  380. return -1;
  381. if ((n = len / 16)) {
  382. (*stream) (inp, out, n, key, ctx->nonce.c, ctx->cmac.c);
  383. n *= 16;
  384. inp += n;
  385. out += n;
  386. len -= n;
  387. if (len)
  388. ctr64_add(ctx->nonce.c, n / 16);
  389. }
  390. if (len) {
  391. (*block) (ctx->nonce.c, scratch.c, key);
  392. for (i = 0; i < len; ++i)
  393. ctx->cmac.c[i] ^= (out[i] = scratch.c[i] ^ inp[i]);
  394. (*block) (ctx->cmac.c, ctx->cmac.c, key);
  395. }
  396. for (i = 15 - L; i < 16; ++i)
  397. ctx->nonce.c[i] = 0;
  398. (*block) (ctx->nonce.c, scratch.c, key);
  399. ctx->cmac.u[0] ^= scratch.u[0];
  400. ctx->cmac.u[1] ^= scratch.u[1];
  401. ctx->nonce.c[0] = flags0;
  402. return 0;
  403. }
  404. size_t CRYPTO_ccm128_tag(CCM128_CONTEXT *ctx, unsigned char *tag, size_t len)
  405. {
  406. unsigned int M = (ctx->nonce.c[0] >> 3) & 7; /* the M parameter */
  407. M *= 2;
  408. M += 2;
  409. if (len < M)
  410. return 0;
  411. memcpy(tag, ctx->cmac.c, M);
  412. return M;
  413. }