cmac.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. /* cmac.c
  2. *
  3. * Copyright (C) 2006-2023 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. /* Set devId regardless of value (invalid or not) */
  100. cmac->devId = devId;
  101. #ifndef WOLF_CRYPTO_CB_FIND
  102. if (devId != INVALID_DEVID)
  103. #endif
  104. {
  105. cmac->devCtx = NULL;
  106. ret = wc_CryptoCb_Cmac(cmac, key, keySz, NULL, 0, NULL, NULL,
  107. type, unused);
  108. if (ret != CRYPTOCB_UNAVAILABLE)
  109. return ret;
  110. /* fall-through when unavailable */
  111. }
  112. #else
  113. (void)devId;
  114. #endif
  115. if (key == NULL || keySz == 0) {
  116. return BAD_FUNC_ARG;
  117. }
  118. #if defined(WOLFSSL_SE050) && defined(WOLFSSL_SE050_CRYPT)
  119. cmac->useSWCrypt = useSW;
  120. if (cmac->useSWCrypt == 1) {
  121. cmac->aes.useSWCrypt = 1;
  122. }
  123. #endif
  124. ret = wc_AesSetKey(&cmac->aes, key, keySz, NULL, AES_ENCRYPTION);
  125. if (ret == 0) {
  126. byte l[AES_BLOCK_SIZE];
  127. XMEMSET(l, 0, AES_BLOCK_SIZE);
  128. ret = wc_AesEncryptDirect(&cmac->aes, l, l);
  129. if (ret == 0) {
  130. ShiftAndXorRb(cmac->k1, l);
  131. ShiftAndXorRb(cmac->k2, cmac->k1);
  132. ForceZero(l, AES_BLOCK_SIZE);
  133. }
  134. }
  135. return ret;
  136. }
  137. int wc_InitCmac(Cmac* cmac, const byte* key, word32 keySz,
  138. int type, void* unused)
  139. {
  140. #ifdef WOLFSSL_QNX_CAAM
  141. int devId = WOLFSSL_CAAM_DEVID;
  142. #else
  143. int devId = INVALID_DEVID;
  144. #endif
  145. return wc_InitCmac_ex(cmac, key, keySz, type, unused, NULL, devId);
  146. }
  147. int wc_CmacUpdate(Cmac* cmac, const byte* in, word32 inSz)
  148. {
  149. int ret = 0;
  150. if ((cmac == NULL) || (in == NULL && inSz != 0)) {
  151. return BAD_FUNC_ARG;
  152. }
  153. #ifdef WOLF_CRYPTO_CB
  154. #ifndef WOLF_CRYPTO_CB_FIND
  155. if (cmac->devId != INVALID_DEVID)
  156. #endif
  157. {
  158. ret = wc_CryptoCb_Cmac(cmac, NULL, 0, in, inSz,
  159. NULL, NULL, 0, NULL);
  160. if (ret != CRYPTOCB_UNAVAILABLE)
  161. return ret;
  162. /* fall-through when unavailable */
  163. ret = 0; /* reset error code */
  164. }
  165. #endif
  166. while (inSz != 0) {
  167. word32 add = min(inSz, AES_BLOCK_SIZE - cmac->bufferSz);
  168. XMEMCPY(&cmac->buffer[cmac->bufferSz], in, add);
  169. cmac->bufferSz += add;
  170. in += add;
  171. inSz -= add;
  172. if (cmac->bufferSz == AES_BLOCK_SIZE && inSz != 0) {
  173. if (cmac->totalSz != 0) {
  174. xorbuf(cmac->buffer, cmac->digest, AES_BLOCK_SIZE);
  175. }
  176. ret = wc_AesEncryptDirect(&cmac->aes, cmac->digest, cmac->buffer);
  177. if (ret == 0) {
  178. cmac->totalSz += AES_BLOCK_SIZE;
  179. cmac->bufferSz = 0;
  180. }
  181. }
  182. }
  183. return ret;
  184. }
  185. int wc_CmacFinal(Cmac* cmac, byte* out, word32* outSz)
  186. {
  187. int ret;
  188. const byte* subKey;
  189. word32 remainder;
  190. if (cmac == NULL || out == NULL || outSz == NULL) {
  191. return BAD_FUNC_ARG;
  192. }
  193. if (*outSz < WC_CMAC_TAG_MIN_SZ || *outSz > WC_CMAC_TAG_MAX_SZ) {
  194. return BUFFER_E;
  195. }
  196. #ifdef WOLF_CRYPTO_CB
  197. #ifndef WOLF_CRYPTO_CB_FIND
  198. if (cmac->devId != INVALID_DEVID)
  199. #endif
  200. {
  201. ret = wc_CryptoCb_Cmac(cmac, NULL, 0, NULL, 0, out, outSz, 0, NULL);
  202. if (ret != CRYPTOCB_UNAVAILABLE)
  203. return ret;
  204. /* fall-through when unavailable */
  205. }
  206. #endif
  207. if (cmac->bufferSz == AES_BLOCK_SIZE) {
  208. subKey = cmac->k1;
  209. }
  210. else {
  211. /* ensure we will have a valid remainder value */
  212. if (cmac->bufferSz > AES_BLOCK_SIZE) {
  213. return BAD_STATE_E;
  214. }
  215. remainder = AES_BLOCK_SIZE - cmac->bufferSz;
  216. if (remainder == 0) {
  217. remainder = AES_BLOCK_SIZE;
  218. }
  219. if (remainder > 1) {
  220. XMEMSET(cmac->buffer + AES_BLOCK_SIZE - remainder, 0, remainder);
  221. }
  222. cmac->buffer[AES_BLOCK_SIZE - remainder] = 0x80;
  223. subKey = cmac->k2;
  224. }
  225. xorbuf(cmac->buffer, cmac->digest, AES_BLOCK_SIZE);
  226. xorbuf(cmac->buffer, subKey, AES_BLOCK_SIZE);
  227. ret = wc_AesEncryptDirect(&cmac->aes, cmac->digest, cmac->buffer);
  228. if (ret == 0) {
  229. XMEMCPY(out, cmac->digest, *outSz);
  230. }
  231. #if defined(WOLFSSL_HASH_KEEP)
  232. /* TODO: msg is leaked if wc_CmacFinal() is not called
  233. * e.g. when multiple calls to wc_CmacUpdate() and one fails but
  234. * wc_CmacFinal() not called. */
  235. if (cmac->msg != NULL) {
  236. XFREE(cmac->msg, cmac->heap, DYNAMIC_TYPE_TMP_BUFFER);
  237. cmac->msg = NULL;
  238. }
  239. #endif
  240. wc_AesFree(&cmac->aes);
  241. ForceZero(cmac, sizeof(Cmac));
  242. return ret;
  243. }
  244. int wc_AesCmacGenerate(byte* out, word32* outSz,
  245. const byte* in, word32 inSz,
  246. const byte* key, word32 keySz)
  247. {
  248. int ret;
  249. #ifdef WOLFSSL_SMALL_STACK
  250. Cmac *cmac;
  251. #else
  252. Cmac cmac[1];
  253. #endif
  254. if (out == NULL || (in == NULL && inSz > 0) || key == NULL || keySz == 0) {
  255. return BAD_FUNC_ARG;
  256. }
  257. #ifdef WOLFSSL_SMALL_STACK
  258. if ((cmac = (Cmac *)XMALLOC(sizeof *cmac, NULL,
  259. DYNAMIC_TYPE_CMAC)) == NULL) {
  260. return MEMORY_E;
  261. }
  262. #endif
  263. #ifdef WOLFSSL_CHECK_MEM_ZERO
  264. XMEMSET(((unsigned char *)cmac) + sizeof(Aes), 0xff,
  265. sizeof(Cmac) - sizeof(Aes));
  266. /* Aes part is checked by wc_AesFree. */
  267. wc_MemZero_Add("wc_AesCmacGenerate cmac",
  268. ((unsigned char *)cmac) + sizeof(Aes), sizeof(Cmac) - sizeof(Aes));
  269. #endif
  270. ret = wc_InitCmac(cmac, key, keySz, WC_CMAC_AES, NULL);
  271. if (ret == 0) {
  272. ret = wc_CmacUpdate(cmac, in, inSz);
  273. }
  274. if (ret == 0) {
  275. ret = wc_CmacFinal(cmac, out, outSz);
  276. }
  277. #ifdef WOLFSSL_SMALL_STACK
  278. if (cmac) {
  279. XFREE(cmac, NULL, DYNAMIC_TYPE_CMAC);
  280. }
  281. #elif defined(WOLFSSL_CHECK_MEM_ZERO)
  282. wc_MemZero_Check(cmac, sizeof(Cmac));
  283. #endif
  284. return ret;
  285. }
  286. int wc_AesCmacVerify(const byte* check, word32 checkSz,
  287. const byte* in, word32 inSz,
  288. const byte* key, word32 keySz)
  289. {
  290. int ret;
  291. byte a[AES_BLOCK_SIZE];
  292. word32 aSz = sizeof(a);
  293. int compareRet;
  294. if (check == NULL || checkSz == 0 || (in == NULL && inSz != 0) ||
  295. key == NULL || keySz == 0) {
  296. return BAD_FUNC_ARG;
  297. }
  298. XMEMSET(a, 0, aSz);
  299. ret = wc_AesCmacGenerate(a, &aSz, in, inSz, key, keySz);
  300. compareRet = ConstantCompare(check, a, (int)min(checkSz, aSz));
  301. if (ret == 0)
  302. ret = compareRet ? 1 : 0;
  303. return ret;
  304. }
  305. #endif /* WOLFSSL_CMAC && NO_AES && WOLFSSL_AES_DIRECT */