cmac.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /*
  2. * Copyright 2010-2018 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 <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 && !EVP_EncryptInit_ex(ctx->cctx, cipher, impl, NULL, NULL))
  107. return 0;
  108. /* Non-NULL key means initialisation complete */
  109. if (key) {
  110. int bl;
  111. if (!EVP_CIPHER_CTX_cipher(ctx->cctx))
  112. return 0;
  113. if (!EVP_CIPHER_CTX_set_key_length(ctx->cctx, keylen))
  114. return 0;
  115. if (!EVP_EncryptInit_ex(ctx->cctx, NULL, NULL, key, zero_iv))
  116. return 0;
  117. bl = EVP_CIPHER_CTX_block_size(ctx->cctx);
  118. if (!EVP_Cipher(ctx->cctx, ctx->tbl, zero_iv, bl))
  119. return 0;
  120. make_kn(ctx->k1, ctx->tbl, bl);
  121. make_kn(ctx->k2, ctx->k1, bl);
  122. OPENSSL_cleanse(ctx->tbl, bl);
  123. /* Reset context again ready for first data block */
  124. if (!EVP_EncryptInit_ex(ctx->cctx, NULL, NULL, NULL, zero_iv))
  125. return 0;
  126. /* Zero tbl so resume works */
  127. memset(ctx->tbl, 0, bl);
  128. ctx->nlast_block = 0;
  129. }
  130. return 1;
  131. }
  132. int CMAC_Update(CMAC_CTX *ctx, const void *in, size_t dlen)
  133. {
  134. const unsigned char *data = in;
  135. size_t bl;
  136. if (ctx->nlast_block == -1)
  137. return 0;
  138. if (dlen == 0)
  139. return 1;
  140. bl = EVP_CIPHER_CTX_block_size(ctx->cctx);
  141. /* Copy into partial block if we need to */
  142. if (ctx->nlast_block > 0) {
  143. size_t nleft;
  144. nleft = bl - ctx->nlast_block;
  145. if (dlen < nleft)
  146. nleft = dlen;
  147. memcpy(ctx->last_block + ctx->nlast_block, data, nleft);
  148. dlen -= nleft;
  149. ctx->nlast_block += nleft;
  150. /* If no more to process return */
  151. if (dlen == 0)
  152. return 1;
  153. data += nleft;
  154. /* Else not final block so encrypt it */
  155. if (!EVP_Cipher(ctx->cctx, ctx->tbl, ctx->last_block, bl))
  156. return 0;
  157. }
  158. /* Encrypt all but one of the complete blocks left */
  159. while (dlen > bl) {
  160. if (!EVP_Cipher(ctx->cctx, ctx->tbl, data, bl))
  161. return 0;
  162. dlen -= bl;
  163. data += bl;
  164. }
  165. /* Copy any data left to last block buffer */
  166. memcpy(ctx->last_block, data, dlen);
  167. ctx->nlast_block = dlen;
  168. return 1;
  169. }
  170. int CMAC_Final(CMAC_CTX *ctx, unsigned char *out, size_t *poutlen)
  171. {
  172. int i, bl, lb;
  173. if (ctx->nlast_block == -1)
  174. return 0;
  175. bl = EVP_CIPHER_CTX_block_size(ctx->cctx);
  176. *poutlen = (size_t)bl;
  177. if (!out)
  178. return 1;
  179. lb = ctx->nlast_block;
  180. /* Is last block complete? */
  181. if (lb == bl) {
  182. for (i = 0; i < bl; i++)
  183. out[i] = ctx->last_block[i] ^ ctx->k1[i];
  184. } else {
  185. ctx->last_block[lb] = 0x80;
  186. if (bl - lb > 1)
  187. memset(ctx->last_block + lb + 1, 0, bl - lb - 1);
  188. for (i = 0; i < bl; i++)
  189. out[i] = ctx->last_block[i] ^ ctx->k2[i];
  190. }
  191. if (!EVP_Cipher(ctx->cctx, out, out, bl)) {
  192. OPENSSL_cleanse(out, bl);
  193. return 0;
  194. }
  195. return 1;
  196. }
  197. int CMAC_resume(CMAC_CTX *ctx)
  198. {
  199. if (ctx->nlast_block == -1)
  200. return 0;
  201. /*
  202. * The buffer "tbl" contains the last fully encrypted block which is the
  203. * last IV (or all zeroes if no last encrypted block). The last block has
  204. * not been modified since CMAC_final(). So reinitialising using the last
  205. * decrypted block will allow CMAC to continue after calling
  206. * CMAC_Final().
  207. */
  208. return EVP_EncryptInit_ex(ctx->cctx, NULL, NULL, NULL, ctx->tbl);
  209. }