2
0

cmac.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /*
  2. * Copyright 2010-2020 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 <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include "internal/cryptlib.h"
  13. #include <openssl/cmac.h>
  14. #include <openssl/err.h>
  15. struct CMAC_CTX_st {
  16. /* Cipher context to use */
  17. EVP_CIPHER_CTX *cctx;
  18. /* Keys k1 and k2 */
  19. unsigned char k1[EVP_MAX_BLOCK_LENGTH];
  20. unsigned char k2[EVP_MAX_BLOCK_LENGTH];
  21. /* Temporary block */
  22. unsigned char tbl[EVP_MAX_BLOCK_LENGTH];
  23. /* Last (possibly partial) block */
  24. unsigned char last_block[EVP_MAX_BLOCK_LENGTH];
  25. /* Number of bytes in last block: -1 means context not initialised */
  26. int nlast_block;
  27. };
  28. /* Make temporary keys K1 and K2 */
  29. static void make_kn(unsigned char *k1, const unsigned char *l, int bl)
  30. {
  31. int i;
  32. unsigned char c = l[0], carry = c >> 7, cnext;
  33. /* Shift block to left, including carry */
  34. for (i = 0; i < bl - 1; i++, c = cnext)
  35. k1[i] = (c << 1) | ((cnext = l[i + 1]) >> 7);
  36. /* If MSB set fixup with R */
  37. k1[i] = (c << 1) ^ ((0 - carry) & (bl == 16 ? 0x87 : 0x1b));
  38. }
  39. CMAC_CTX *CMAC_CTX_new(void)
  40. {
  41. CMAC_CTX *ctx;
  42. if ((ctx = OPENSSL_malloc(sizeof(*ctx))) == NULL) {
  43. CRYPTOerr(CRYPTO_F_CMAC_CTX_NEW, ERR_R_MALLOC_FAILURE);
  44. return NULL;
  45. }
  46. ctx->cctx = EVP_CIPHER_CTX_new();
  47. if (ctx->cctx == NULL) {
  48. OPENSSL_free(ctx);
  49. return NULL;
  50. }
  51. ctx->nlast_block = -1;
  52. return ctx;
  53. }
  54. void CMAC_CTX_cleanup(CMAC_CTX *ctx)
  55. {
  56. EVP_CIPHER_CTX_reset(ctx->cctx);
  57. OPENSSL_cleanse(ctx->tbl, EVP_MAX_BLOCK_LENGTH);
  58. OPENSSL_cleanse(ctx->k1, EVP_MAX_BLOCK_LENGTH);
  59. OPENSSL_cleanse(ctx->k2, EVP_MAX_BLOCK_LENGTH);
  60. OPENSSL_cleanse(ctx->last_block, EVP_MAX_BLOCK_LENGTH);
  61. ctx->nlast_block = -1;
  62. }
  63. EVP_CIPHER_CTX *CMAC_CTX_get0_cipher_ctx(CMAC_CTX *ctx)
  64. {
  65. return ctx->cctx;
  66. }
  67. void CMAC_CTX_free(CMAC_CTX *ctx)
  68. {
  69. if (!ctx)
  70. return;
  71. CMAC_CTX_cleanup(ctx);
  72. EVP_CIPHER_CTX_free(ctx->cctx);
  73. OPENSSL_free(ctx);
  74. }
  75. int CMAC_CTX_copy(CMAC_CTX *out, const CMAC_CTX *in)
  76. {
  77. int bl;
  78. if (in->nlast_block == -1)
  79. return 0;
  80. if (!EVP_CIPHER_CTX_copy(out->cctx, in->cctx))
  81. return 0;
  82. bl = EVP_CIPHER_CTX_block_size(in->cctx);
  83. memcpy(out->k1, in->k1, bl);
  84. memcpy(out->k2, in->k2, bl);
  85. memcpy(out->tbl, in->tbl, bl);
  86. memcpy(out->last_block, in->last_block, bl);
  87. out->nlast_block = in->nlast_block;
  88. return 1;
  89. }
  90. int CMAC_Init(CMAC_CTX *ctx, const void *key, size_t keylen,
  91. const EVP_CIPHER *cipher, ENGINE *impl)
  92. {
  93. static const unsigned char zero_iv[EVP_MAX_BLOCK_LENGTH] = { 0 };
  94. /* All zeros means restart */
  95. if (!key && !cipher && !impl && keylen == 0) {
  96. /* Not initialised */
  97. if (ctx->nlast_block == -1)
  98. return 0;
  99. if (!EVP_EncryptInit_ex(ctx->cctx, NULL, NULL, NULL, zero_iv))
  100. return 0;
  101. memset(ctx->tbl, 0, EVP_CIPHER_CTX_block_size(ctx->cctx));
  102. ctx->nlast_block = 0;
  103. return 1;
  104. }
  105. /* Initialise context */
  106. if (cipher != NULL) {
  107. /* Ensure we can't use this ctx until we also have a key */
  108. ctx->nlast_block = -1;
  109. if (!EVP_EncryptInit_ex(ctx->cctx, cipher, impl, NULL, NULL))
  110. return 0;
  111. }
  112. /* Non-NULL key means initialisation complete */
  113. if (key != NULL) {
  114. int bl;
  115. /* If anything fails then ensure we can't use this ctx */
  116. ctx->nlast_block = -1;
  117. if (!EVP_CIPHER_CTX_cipher(ctx->cctx))
  118. return 0;
  119. if (!EVP_CIPHER_CTX_set_key_length(ctx->cctx, keylen))
  120. return 0;
  121. if (!EVP_EncryptInit_ex(ctx->cctx, NULL, NULL, key, zero_iv))
  122. return 0;
  123. bl = EVP_CIPHER_CTX_block_size(ctx->cctx);
  124. if (EVP_Cipher(ctx->cctx, ctx->tbl, zero_iv, bl) <= 0)
  125. return 0;
  126. make_kn(ctx->k1, ctx->tbl, bl);
  127. make_kn(ctx->k2, ctx->k1, bl);
  128. OPENSSL_cleanse(ctx->tbl, bl);
  129. /* Reset context again ready for first data block */
  130. if (!EVP_EncryptInit_ex(ctx->cctx, NULL, NULL, NULL, zero_iv))
  131. return 0;
  132. /* Zero tbl so resume works */
  133. memset(ctx->tbl, 0, bl);
  134. ctx->nlast_block = 0;
  135. }
  136. return 1;
  137. }
  138. int CMAC_Update(CMAC_CTX *ctx, const void *in, size_t dlen)
  139. {
  140. const unsigned char *data = in;
  141. size_t bl;
  142. if (ctx->nlast_block == -1)
  143. return 0;
  144. if (dlen == 0)
  145. return 1;
  146. bl = EVP_CIPHER_CTX_block_size(ctx->cctx);
  147. /* Copy into partial block if we need to */
  148. if (ctx->nlast_block > 0) {
  149. size_t nleft;
  150. nleft = bl - ctx->nlast_block;
  151. if (dlen < nleft)
  152. nleft = dlen;
  153. memcpy(ctx->last_block + ctx->nlast_block, data, nleft);
  154. dlen -= nleft;
  155. ctx->nlast_block += nleft;
  156. /* If no more to process return */
  157. if (dlen == 0)
  158. return 1;
  159. data += nleft;
  160. /* Else not final block so encrypt it */
  161. if (EVP_Cipher(ctx->cctx, ctx->tbl, ctx->last_block, bl) <= 0)
  162. return 0;
  163. }
  164. /* Encrypt all but one of the complete blocks left */
  165. while (dlen > bl) {
  166. if (EVP_Cipher(ctx->cctx, ctx->tbl, data, bl) <= 0)
  167. return 0;
  168. dlen -= bl;
  169. data += bl;
  170. }
  171. /* Copy any data left to last block buffer */
  172. memcpy(ctx->last_block, data, dlen);
  173. ctx->nlast_block = dlen;
  174. return 1;
  175. }
  176. int CMAC_Final(CMAC_CTX *ctx, unsigned char *out, size_t *poutlen)
  177. {
  178. int i, bl, lb;
  179. if (ctx->nlast_block == -1)
  180. return 0;
  181. bl = EVP_CIPHER_CTX_block_size(ctx->cctx);
  182. *poutlen = (size_t)bl;
  183. if (!out)
  184. return 1;
  185. lb = ctx->nlast_block;
  186. /* Is last block complete? */
  187. if (lb == bl) {
  188. for (i = 0; i < bl; i++)
  189. out[i] = ctx->last_block[i] ^ ctx->k1[i];
  190. } else {
  191. ctx->last_block[lb] = 0x80;
  192. if (bl - lb > 1)
  193. memset(ctx->last_block + lb + 1, 0, bl - lb - 1);
  194. for (i = 0; i < bl; i++)
  195. out[i] = ctx->last_block[i] ^ ctx->k2[i];
  196. }
  197. if (!EVP_Cipher(ctx->cctx, out, out, bl)) {
  198. OPENSSL_cleanse(out, bl);
  199. return 0;
  200. }
  201. return 1;
  202. }
  203. int CMAC_resume(CMAC_CTX *ctx)
  204. {
  205. if (ctx->nlast_block == -1)
  206. return 0;
  207. /*
  208. * The buffer "tbl" contains the last fully encrypted block which is the
  209. * last IV (or all zeroes if no last encrypted block). The last block has
  210. * not been modified since CMAC_final(). So reinitialising using the last
  211. * decrypted block will allow CMAC to continue after calling
  212. * CMAC_Final().
  213. */
  214. return EVP_EncryptInit_ex(ctx->cctx, NULL, NULL, NULL, ctx->tbl);
  215. }