cmac.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. /* cmac.c
  2. *
  3. * Copyright (C) 2006-2022 wolfSSL Inc.
  4. *
  5. * This file is part of wolfSSL.
  6. *
  7. * wolfSSL is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * wolfSSL is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
  20. */
  21. #ifdef HAVE_CONFIG_H
  22. #include <config.h>
  23. #endif
  24. #include <wolfssl/wolfcrypt/settings.h>
  25. #ifdef WOLFSSL_QNX_CAAM
  26. #include <wolfssl/wolfcrypt/port/caam/wolfcaam.h>
  27. #endif
  28. #if defined(WOLFSSL_HASH_KEEP)
  29. #include <wolfssl/wolfcrypt/hash.h>
  30. #endif
  31. #if defined(WOLFSSL_CMAC) && !defined(NO_AES) && defined(WOLFSSL_AES_DIRECT)
  32. #if defined(HAVE_FIPS) && defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2)
  33. /* set NO_WRAPPERS before headers, use direct internal f()s not wrappers */
  34. #define FIPS_NO_WRAPPERS
  35. #ifdef USE_WINDOWS_API
  36. #pragma code_seg(".fipsA$n")
  37. #pragma const_seg(".fipsB$n")
  38. #endif
  39. #endif
  40. #ifdef NO_INLINE
  41. #include <wolfssl/wolfcrypt/misc.h>
  42. #else
  43. #define WOLFSSL_MISC_INCLUDED
  44. #include <wolfcrypt/src/misc.c>
  45. #endif
  46. #include <wolfssl/wolfcrypt/error-crypt.h>
  47. #include <wolfssl/wolfcrypt/aes.h>
  48. #include <wolfssl/wolfcrypt/cmac.h>
  49. #ifdef WOLF_CRYPTO_CB
  50. #include <wolfssl/wolfcrypt/cryptocb.h>
  51. #endif
  52. #ifdef WOLFSSL_HASH_KEEP
  53. /* Some hardware have issues with update, this function stores the data to be
  54. * hashed into an array. Once ready, the Final operation is called on all of the
  55. * data to be hashed at once.
  56. * returns 0 on success
  57. */
  58. int wc_CMAC_Grow(Cmac* cmac, const byte* in, int inSz)
  59. {
  60. return _wc_Hash_Grow(&cmac->msg, &cmac->used, &cmac->len, in, inSz, NULL);
  61. }
  62. #endif /* WOLFSSL_HASH_KEEP */
  63. /* Used by AES-SIV. See aes.c. */
  64. void ShiftAndXorRb(byte* out, byte* in)
  65. {
  66. int i, j, xorRb;
  67. int mask = 0, last = 0;
  68. byte Rb = 0x87;
  69. xorRb = (in[0] & 0x80) != 0;
  70. for (i = 1, j = AES_BLOCK_SIZE - 1; i <= AES_BLOCK_SIZE; i++, j--) {
  71. last = (in[j] & 0x80) ? 1 : 0;
  72. out[j] = (byte)((in[j] << 1) | mask);
  73. mask = last;
  74. if (xorRb) {
  75. out[j] ^= Rb;
  76. Rb = 0;
  77. }
  78. }
  79. }
  80. /* returns 0 on success */
  81. int wc_InitCmac_ex(Cmac* cmac, const byte* key, word32 keySz,
  82. int type, void* unused, void* heap, int devId)
  83. {
  84. int ret;
  85. #if defined(WOLFSSL_SE050) && defined(WOLFSSL_SE050_CRYPT)
  86. byte useSW = 0;
  87. #endif
  88. (void)unused;
  89. (void)heap;
  90. if (cmac == NULL || type != WC_CMAC_AES) {
  91. return BAD_FUNC_ARG;
  92. }
  93. #if defined(WOLFSSL_SE050) && defined(WOLFSSL_SE050_CRYPT)
  94. /* save if we should use SW crypt, restore after memset */
  95. useSW = cmac->useSWCrypt;
  96. #endif
  97. XMEMSET(cmac, 0, sizeof(Cmac));
  98. #ifdef WOLF_CRYPTO_CB
  99. if (devId != INVALID_DEVID) {
  100. cmac->devId = devId;
  101. cmac->devCtx = NULL;
  102. ret = wc_CryptoCb_Cmac(cmac, key, keySz, NULL, 0, NULL, NULL,
  103. type, unused);
  104. if (ret != CRYPTOCB_UNAVAILABLE)
  105. return ret;
  106. /* fall-through when unavailable */
  107. }
  108. #else
  109. (void)devId;
  110. #endif
  111. if (key == NULL || keySz == 0) {
  112. return BAD_FUNC_ARG;
  113. }
  114. #if defined(WOLFSSL_SE050) && defined(WOLFSSL_SE050_CRYPT)
  115. cmac->useSWCrypt = useSW;
  116. if (cmac->useSWCrypt == 1) {
  117. cmac->aes.useSWCrypt = 1;
  118. }
  119. #endif
  120. ret = wc_AesSetKey(&cmac->aes, key, keySz, NULL, AES_ENCRYPTION);
  121. if (ret == 0) {
  122. byte l[AES_BLOCK_SIZE];
  123. XMEMSET(l, 0, AES_BLOCK_SIZE);
  124. ret = wc_AesEncryptDirect(&cmac->aes, l, l);
  125. if (ret == 0) {
  126. ShiftAndXorRb(cmac->k1, l);
  127. ShiftAndXorRb(cmac->k2, cmac->k1);
  128. ForceZero(l, AES_BLOCK_SIZE);
  129. }
  130. }
  131. return ret;
  132. }
  133. int wc_InitCmac(Cmac* cmac, const byte* key, word32 keySz,
  134. int type, void* unused)
  135. {
  136. #ifdef WOLFSSL_QNX_CAAM
  137. int devId = WOLFSSL_CAAM_DEVID;
  138. #else
  139. int devId = INVALID_DEVID;
  140. #endif
  141. return wc_InitCmac_ex(cmac, key, keySz, type, unused, NULL, devId);
  142. }
  143. int wc_CmacUpdate(Cmac* cmac, const byte* in, word32 inSz)
  144. {
  145. int ret = 0;
  146. if ((cmac == NULL) || (in == NULL && inSz != 0)) {
  147. return BAD_FUNC_ARG;
  148. }
  149. #ifdef WOLF_CRYPTO_CB
  150. if (cmac->devId != INVALID_DEVID) {
  151. ret = wc_CryptoCb_Cmac(cmac, NULL, 0, in, inSz,
  152. NULL, NULL, 0, NULL);
  153. if (ret != CRYPTOCB_UNAVAILABLE)
  154. return ret;
  155. /* fall-through when unavailable */
  156. ret = 0; /* reset error code */
  157. }
  158. #endif
  159. while (inSz != 0) {
  160. word32 add = min(inSz, AES_BLOCK_SIZE - cmac->bufferSz);
  161. XMEMCPY(&cmac->buffer[cmac->bufferSz], in, add);
  162. cmac->bufferSz += add;
  163. in += add;
  164. inSz -= add;
  165. if (cmac->bufferSz == AES_BLOCK_SIZE && inSz != 0) {
  166. if (cmac->totalSz != 0) {
  167. xorbuf(cmac->buffer, cmac->digest, AES_BLOCK_SIZE);
  168. }
  169. ret = wc_AesEncryptDirect(&cmac->aes, cmac->digest, cmac->buffer);
  170. if (ret == 0) {
  171. cmac->totalSz += AES_BLOCK_SIZE;
  172. cmac->bufferSz = 0;
  173. }
  174. }
  175. }
  176. return ret;
  177. }
  178. int wc_CmacFinal(Cmac* cmac, byte* out, word32* outSz)
  179. {
  180. int ret;
  181. const byte* subKey;
  182. if (cmac == NULL || out == NULL || outSz == NULL) {
  183. return BAD_FUNC_ARG;
  184. }
  185. if (*outSz < WC_CMAC_TAG_MIN_SZ || *outSz > WC_CMAC_TAG_MAX_SZ) {
  186. return BUFFER_E;
  187. }
  188. #ifdef WOLF_CRYPTO_CB
  189. if (cmac->devId != INVALID_DEVID) {
  190. ret = wc_CryptoCb_Cmac(cmac, NULL, 0, NULL, 0, out, outSz, 0, NULL);
  191. if (ret != CRYPTOCB_UNAVAILABLE)
  192. return ret;
  193. /* fall-through when unavailable */
  194. }
  195. #endif
  196. if (cmac->bufferSz == AES_BLOCK_SIZE) {
  197. subKey = cmac->k1;
  198. }
  199. else {
  200. word32 remainder = AES_BLOCK_SIZE - cmac->bufferSz;
  201. if (remainder == 0) {
  202. remainder = AES_BLOCK_SIZE;
  203. }
  204. if (remainder > 1) {
  205. XMEMSET(cmac->buffer + AES_BLOCK_SIZE - remainder, 0, remainder);
  206. }
  207. cmac->buffer[AES_BLOCK_SIZE - remainder] = 0x80;
  208. subKey = cmac->k2;
  209. }
  210. xorbuf(cmac->buffer, cmac->digest, AES_BLOCK_SIZE);
  211. xorbuf(cmac->buffer, subKey, AES_BLOCK_SIZE);
  212. ret = wc_AesEncryptDirect(&cmac->aes, cmac->digest, cmac->buffer);
  213. if (ret == 0) {
  214. XMEMCPY(out, cmac->digest, *outSz);
  215. }
  216. #if defined(WOLFSSL_HASH_KEEP)
  217. /* TODO: msg is leaked if wc_CmacFinal() is not called
  218. * e.g. when multiple calls to wc_CmacUpdate() and one fails but
  219. * wc_CmacFinal() not called. */
  220. if (cmac->msg != NULL) {
  221. XFREE(cmac->msg, cmac->heap, DYNAMIC_TYPE_TMP_BUFFER);
  222. cmac->msg = NULL;
  223. }
  224. #endif
  225. wc_AesFree(&cmac->aes);
  226. ForceZero(cmac, sizeof(Cmac));
  227. return ret;
  228. }
  229. int wc_AesCmacGenerate(byte* out, word32* outSz,
  230. const byte* in, word32 inSz,
  231. const byte* key, word32 keySz)
  232. {
  233. int ret;
  234. #ifdef WOLFSSL_SMALL_STACK
  235. Cmac *cmac;
  236. #else
  237. Cmac cmac[1];
  238. #endif
  239. if (out == NULL || (in == NULL && inSz > 0) || key == NULL || keySz == 0) {
  240. return BAD_FUNC_ARG;
  241. }
  242. #ifdef WOLFSSL_SMALL_STACK
  243. if ((cmac = (Cmac *)XMALLOC(sizeof *cmac, NULL,
  244. DYNAMIC_TYPE_CMAC)) == NULL) {
  245. return MEMORY_E;
  246. }
  247. #endif
  248. #ifdef WOLFSSL_CHECK_MEM_ZERO
  249. /* Aes part is checked by wc_AesFree. */
  250. wc_MemZero_Add("wc_AesCmacGenerate cmac",
  251. ((unsigned char *)cmac) + sizeof(Aes), sizeof(Cmac) - sizeof(Aes));
  252. #endif
  253. ret = wc_InitCmac(cmac, key, keySz, WC_CMAC_AES, NULL);
  254. if (ret == 0) {
  255. ret = wc_CmacUpdate(cmac, in, inSz);
  256. }
  257. if (ret == 0) {
  258. ret = wc_CmacFinal(cmac, out, outSz);
  259. }
  260. #ifdef WOLFSSL_SMALL_STACK
  261. if (cmac) {
  262. XFREE(cmac, NULL, DYNAMIC_TYPE_CMAC);
  263. }
  264. #elif defined(WOLFSSL_CHECK_MEM_ZERO)
  265. wc_MemZero_Check(cmac, sizeof(Cmac));
  266. #endif
  267. return ret;
  268. }
  269. int wc_AesCmacVerify(const byte* check, word32 checkSz,
  270. const byte* in, word32 inSz,
  271. const byte* key, word32 keySz)
  272. {
  273. int ret;
  274. byte a[AES_BLOCK_SIZE];
  275. word32 aSz = sizeof(a);
  276. int compareRet;
  277. if (check == NULL || checkSz == 0 || (in == NULL && inSz != 0) ||
  278. key == NULL || keySz == 0) {
  279. return BAD_FUNC_ARG;
  280. }
  281. XMEMSET(a, 0, aSz);
  282. ret = wc_AesCmacGenerate(a, &aSz, in, inSz, key, keySz);
  283. compareRet = ConstantCompare(check, a, min(checkSz, aSz));
  284. if (ret == 0)
  285. ret = compareRet ? 1 : 0;
  286. return ret;
  287. }
  288. #endif /* WOLFSSL_CMAC && NO_AES && WOLFSSL_AES_DIRECT */