ccm_mode.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /* ccm_mode.c - TinyCrypt implementation of CCM mode */
  2. /*
  3. * Copyright (C) 2017 by Intel Corporation, All Rights Reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * - Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. *
  11. * - Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. *
  15. * - Neither the name of Intel Corporation nor the names of its contributors
  16. * may be used to endorse or promote products derived from this software
  17. * without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  20. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  21. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  22. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  23. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  24. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  25. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  26. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  27. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  28. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  29. * POSSIBILITY OF SUCH DAMAGE.
  30. */
  31. #include <tinycrypt/ccm_mode.h>
  32. #include <tinycrypt/constants.h>
  33. #include <tinycrypt/utils.h>
  34. #include <stdio.h>
  35. int tc_ccm_config(TCCcmMode_t c, TCAesKeySched_t sched, uint8_t *nonce,
  36. unsigned int nlen, unsigned int mlen)
  37. {
  38. /* input sanity check: */
  39. if (c == (TCCcmMode_t) 0 ||
  40. sched == (TCAesKeySched_t) 0 ||
  41. nonce == (uint8_t *) 0) {
  42. return TC_CRYPTO_FAIL;
  43. } else if (nlen != 13) {
  44. return TC_CRYPTO_FAIL; /* The allowed nonce size is: 13. See documentation.*/
  45. } else if ((mlen < 4) || (mlen > 16) || (mlen & 1)) {
  46. return TC_CRYPTO_FAIL; /* The allowed mac sizes are: 4, 6, 8, 10, 12, 14, 16.*/
  47. }
  48. c->mlen = mlen;
  49. c->sched = sched;
  50. c->nonce = nonce;
  51. return TC_CRYPTO_SUCCESS;
  52. }
  53. /**
  54. * Variation of CBC-MAC mode used in CCM.
  55. */
  56. static void ccm_cbc_mac(uint8_t *T, const uint8_t *data, unsigned int dlen,
  57. unsigned int flag, TCAesKeySched_t sched)
  58. {
  59. unsigned int i;
  60. if (flag > 0) {
  61. T[0] ^= (uint8_t)(dlen >> 8);
  62. T[1] ^= (uint8_t)(dlen);
  63. dlen += 2; i = 2;
  64. } else {
  65. i = 0;
  66. }
  67. while (i < dlen) {
  68. T[i++ % (Nb * Nk)] ^= *data++;
  69. if (((i % (Nb * Nk)) == 0) || dlen == i) {
  70. (void) tc_aes_encrypt(T, T, sched);
  71. }
  72. }
  73. }
  74. /**
  75. * Variation of CTR mode used in CCM.
  76. * The CTR mode used by CCM is slightly different than the conventional CTR
  77. * mode (the counter is increased before encryption, instead of after
  78. * encryption). Besides, it is assumed that the counter is stored in the last
  79. * 2 bytes of the nonce.
  80. */
  81. static int ccm_ctr_mode(uint8_t *out, unsigned int outlen, const uint8_t *in,
  82. unsigned int inlen, uint8_t *ctr, const TCAesKeySched_t sched)
  83. {
  84. uint8_t buffer[TC_AES_BLOCK_SIZE];
  85. uint8_t nonce[TC_AES_BLOCK_SIZE];
  86. uint16_t block_num;
  87. unsigned int i;
  88. /* input sanity check: */
  89. if (out == (uint8_t *) 0 ||
  90. in == (uint8_t *) 0 ||
  91. ctr == (uint8_t *) 0 ||
  92. sched == (TCAesKeySched_t) 0 ||
  93. inlen == 0 ||
  94. outlen == 0 ||
  95. outlen != inlen) {
  96. return TC_CRYPTO_FAIL;
  97. }
  98. /* copy the counter to the nonce */
  99. (void) _copy(nonce, sizeof(nonce), ctr, sizeof(nonce));
  100. /* select the last 2 bytes of the nonce to be incremented */
  101. block_num = (uint16_t) ((nonce[14] << 8)|(nonce[15]));
  102. for (i = 0; i < inlen; ++i) {
  103. if ((i % (TC_AES_BLOCK_SIZE)) == 0) {
  104. block_num++;
  105. nonce[14] = (uint8_t)(block_num >> 8);
  106. nonce[15] = (uint8_t)(block_num);
  107. if (!tc_aes_encrypt(buffer, nonce, sched)) {
  108. return TC_CRYPTO_FAIL;
  109. }
  110. }
  111. /* update the output */
  112. *out++ = buffer[i % (TC_AES_BLOCK_SIZE)] ^ *in++;
  113. }
  114. /* update the counter */
  115. ctr[14] = nonce[14]; ctr[15] = nonce[15];
  116. return TC_CRYPTO_SUCCESS;
  117. }
  118. int tc_ccm_generation_encryption(uint8_t *out, unsigned int olen,
  119. const uint8_t *associated_data,
  120. unsigned int alen, const uint8_t *payload,
  121. unsigned int plen, TCCcmMode_t c)
  122. {
  123. /* input sanity check: */
  124. if ((out == (uint8_t *) 0) ||
  125. (c == (TCCcmMode_t) 0) ||
  126. ((plen > 0) && (payload == (uint8_t *) 0)) ||
  127. ((alen > 0) && (associated_data == (uint8_t *) 0)) ||
  128. (alen >= TC_CCM_AAD_MAX_BYTES) || /* associated data size unsupported */
  129. (plen >= TC_CCM_PAYLOAD_MAX_BYTES) || /* payload size unsupported */
  130. (olen < (plen + c->mlen))) { /* invalid output buffer size */
  131. return TC_CRYPTO_FAIL;
  132. }
  133. uint8_t b[Nb * Nk];
  134. uint8_t tag[Nb * Nk];
  135. unsigned int i;
  136. /* GENERATING THE AUTHENTICATION TAG: */
  137. /* formatting the sequence b for authentication: */
  138. b[0] = ((alen > 0) ? 0x40:0) | (((c->mlen - 2) / 2 << 3)) | (1);
  139. for (i = 1; i <= 13; ++i) {
  140. b[i] = c->nonce[i - 1];
  141. }
  142. b[14] = (uint8_t)(plen >> 8);
  143. b[15] = (uint8_t)(plen);
  144. /* computing the authentication tag using cbc-mac: */
  145. (void) tc_aes_encrypt(tag, b, c->sched);
  146. if (alen > 0) {
  147. ccm_cbc_mac(tag, associated_data, alen, 1, c->sched);
  148. }
  149. if (plen > 0) {
  150. ccm_cbc_mac(tag, payload, plen, 0, c->sched);
  151. }
  152. /* ENCRYPTION: */
  153. /* formatting the sequence b for encryption: */
  154. b[0] = 1; /* q - 1 = 2 - 1 = 1 */
  155. b[14] = b[15] = TC_ZERO_BYTE;
  156. /* encrypting payload using ctr mode: */
  157. ccm_ctr_mode(out, plen, payload, plen, b, c->sched);
  158. b[14] = b[15] = TC_ZERO_BYTE; /* restoring initial counter for ctr_mode (0):*/
  159. /* encrypting b and adding the tag to the output: */
  160. (void) tc_aes_encrypt(b, b, c->sched);
  161. out += plen;
  162. for (i = 0; i < c->mlen; ++i) {
  163. *out++ = tag[i] ^ b[i];
  164. }
  165. return TC_CRYPTO_SUCCESS;
  166. }
  167. int tc_ccm_decryption_verification(uint8_t *out, unsigned int olen,
  168. const uint8_t *associated_data,
  169. unsigned int alen, const uint8_t *payload,
  170. unsigned int plen, TCCcmMode_t c)
  171. {
  172. /* input sanity check: */
  173. if ((out == (uint8_t *) 0) ||
  174. (c == (TCCcmMode_t) 0) ||
  175. ((plen > 0) && (payload == (uint8_t *) 0)) ||
  176. ((alen > 0) && (associated_data == (uint8_t *) 0)) ||
  177. (alen >= TC_CCM_AAD_MAX_BYTES) || /* associated data size unsupported */
  178. (plen >= TC_CCM_PAYLOAD_MAX_BYTES) || /* payload size unsupported */
  179. (olen < plen - c->mlen)) { /* invalid output buffer size */
  180. return TC_CRYPTO_FAIL;
  181. }
  182. uint8_t b[Nb * Nk];
  183. uint8_t tag[Nb * Nk];
  184. unsigned int i;
  185. /* DECRYPTION: */
  186. /* formatting the sequence b for decryption: */
  187. b[0] = 1; /* q - 1 = 2 - 1 = 1 */
  188. for (i = 1; i < 14; ++i) {
  189. b[i] = c->nonce[i - 1];
  190. }
  191. b[14] = b[15] = TC_ZERO_BYTE; /* initial counter value is 0 */
  192. /* decrypting payload using ctr mode: */
  193. ccm_ctr_mode(out, plen - c->mlen, payload, plen - c->mlen, b, c->sched);
  194. b[14] = b[15] = TC_ZERO_BYTE; /* restoring initial counter value (0) */
  195. /* encrypting b and restoring the tag from input: */
  196. (void) tc_aes_encrypt(b, b, c->sched);
  197. for (i = 0; i < c->mlen; ++i) {
  198. tag[i] = *(payload + plen - c->mlen + i) ^ b[i];
  199. }
  200. /* VERIFYING THE AUTHENTICATION TAG: */
  201. /* formatting the sequence b for authentication: */
  202. b[0] = ((alen > 0) ? 0x40:0)|(((c->mlen - 2) / 2 << 3)) | (1);
  203. for (i = 1; i < 14; ++i) {
  204. b[i] = c->nonce[i - 1];
  205. }
  206. b[14] = (uint8_t)((plen - c->mlen) >> 8);
  207. b[15] = (uint8_t)(plen - c->mlen);
  208. /* computing the authentication tag using cbc-mac: */
  209. (void) tc_aes_encrypt(b, b, c->sched);
  210. if (alen > 0) {
  211. ccm_cbc_mac(b, associated_data, alen, 1, c->sched);
  212. }
  213. if (plen > 0) {
  214. ccm_cbc_mac(b, out, plen - c->mlen, 0, c->sched);
  215. }
  216. /* comparing the received tag and the computed one: */
  217. if (_compare(b, tag, c->mlen) == 0) {
  218. return TC_CRYPTO_SUCCESS;
  219. } else {
  220. /* erase the decrypted buffer in case of mac validation failure: */
  221. _set(out, 0, plen - c->mlen);
  222. return TC_CRYPTO_FAIL;
  223. }
  224. }