fsl_mmcau.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  1. /*
  2. * The Clear BSD License
  3. * Copyright (c) 2015, Freescale Semiconductor, Inc.
  4. * Copyright 2017, NXP
  5. * All rights reserved.
  6. *
  7. *
  8. * Redistribution and use in source and binary forms, with or without modification,
  9. * are permitted (subject to the limitations in the disclaimer below) provided
  10. * that the following conditions are met:
  11. *
  12. * o Redistributions of source code must retain the above copyright notice, this list
  13. * of conditions and the following disclaimer.
  14. *
  15. * o Redistributions in binary form must reproduce the above copyright notice, this
  16. * list of conditions and the following disclaimer in the documentation and/or
  17. * other materials provided with the distribution.
  18. *
  19. * o Neither the name of the copyright holder nor the names of its
  20. * contributors may be used to endorse or promote products derived from this
  21. * software without specific prior written permission.
  22. *
  23. * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY THIS LICENSE.
  24. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  25. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  26. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  27. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
  28. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  29. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  30. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  31. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  32. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  33. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  34. */
  35. #include "fsl_mmcau.h"
  36. #include "cau_api.h"
  37. #define MMCAU_AES_BLOCK_SIZE (16)
  38. #define MMCAU_DES_BLOCK_SIZE (8)
  39. #define MMCAU_MD5_STATE_SIZE (16)
  40. #define MMCAU_SHA1_STATE_SIZE (20)
  41. #define MMCAU_SHA256_STATE_SIZE (32)
  42. /* these are maximum for CAU API has functions. HAST_STATE shall be set to maximum of MD5_STATE,SHA1_STATE and
  43. * SHA256_STATE */
  44. #define MMCAU_HASH_STATE_SIZE (32)
  45. #define MMCAU_HASH_BLOCK_SIZE (64)
  46. /* typedef for pointer to CAU API functions */
  47. typedef void (*cau_hash_api_t)(const uint8_t *msgData, const int numBlocks, uint32_t *hashState);
  48. typedef void (*cau_hash_md5_api_t)(const uint8_t *msgData, const int numBlocks, uint8_t *hashState);
  49. /*******************************************************************************
  50. * Code
  51. ******************************************************************************/
  52. static void mmcau_memcpy(void *dst, const void *src, size_t size)
  53. {
  54. register uint8_t *to = dst;
  55. register const uint8_t *from = src;
  56. while (size)
  57. {
  58. *to = *from;
  59. size--;
  60. to++;
  61. from++;
  62. }
  63. }
  64. /* check if in pointer is aligned. if not, copy in to inAlign. return pointer to aligned data. */
  65. static const void *mmcau_align_const(const void *in, void *inAlign, size_t size)
  66. {
  67. const void *inWork = in;
  68. /* if one or two least significant bits in the address are set, the address is unaligned */
  69. if ((uint32_t)in & 3u)
  70. {
  71. mmcau_memcpy(inAlign, in, size);
  72. inWork = inAlign;
  73. }
  74. return inWork;
  75. }
  76. /* check if out pointer is aligned. if not, set bool variable to notify caller. return pointer to aligned data. */
  77. static void *mmcau_align(void *out, void *outAlign, bool *copyOut)
  78. {
  79. void *outWork;
  80. /* if one or two least significant bits in the address are set, the address is unaligned */
  81. if ((uint32_t)out & 3u)
  82. {
  83. outWork = outAlign;
  84. *copyOut = true;
  85. }
  86. else
  87. {
  88. outWork = out;
  89. *copyOut = false;
  90. }
  91. return outWork;
  92. }
  93. /* common function for AES process. "encrypt" argument selects between en-/de-cryption */
  94. static status_t mmcau_AesCrypt(const uint8_t *in, const uint8_t *keySch, uint32_t aesRounds, uint8_t *out, bool encrypt)
  95. {
  96. status_t status;
  97. /* check validity of input arguments */
  98. if (((aesRounds != 10u) && (aesRounds != 12u) && (aesRounds != 14u)) || (NULL == in) || (NULL == out) ||
  99. (NULL == keySch))
  100. {
  101. status = kStatus_InvalidArgument;
  102. }
  103. else
  104. {
  105. uint8_t inAlign[MMCAU_AES_BLOCK_SIZE]; /* 16 bytes aligned input block */
  106. uint8_t outAlign[MMCAU_AES_BLOCK_SIZE]; /* 16 bytes aligned output block */
  107. uint32_t keySchAlign[60]; /* max 60 longwords in case of 32 bytes AES key */
  108. size_t keySchSize = 0;
  109. const uint8_t *keySchWork;
  110. const uint8_t *inWork;
  111. uint8_t *outWork;
  112. bool copyOut;
  113. /* compute keySchSize in bytes per CAU API documentation */
  114. if (aesRounds == 10u)
  115. {
  116. keySchSize = 44u * sizeof(uint32_t);
  117. }
  118. else if (aesRounds == 12u)
  119. {
  120. keySchSize = 52u * sizeof(uint32_t);
  121. }
  122. else /* aesRounds = 14u */
  123. {
  124. keySchSize = 60u * sizeof(uint32_t);
  125. }
  126. /* align pointers */
  127. inWork = mmcau_align_const(in, inAlign, MMCAU_AES_BLOCK_SIZE);
  128. keySchWork = mmcau_align_const(keySch, keySchAlign, keySchSize);
  129. outWork = mmcau_align(out, outAlign, &copyOut);
  130. /* call actual CAU API */
  131. if (encrypt)
  132. {
  133. cau_aes_encrypt(inWork, keySchWork, aesRounds, outWork);
  134. }
  135. else
  136. {
  137. cau_aes_decrypt(inWork, keySchWork, aesRounds, outWork);
  138. }
  139. /* copy to unaligned out pointer */
  140. if (copyOut)
  141. {
  142. mmcau_memcpy(out, outAlign, MMCAU_AES_BLOCK_SIZE);
  143. }
  144. status = kStatus_Success;
  145. }
  146. return status;
  147. }
  148. /* common function for DES process. "encrypt" argument selects between en-/de-cryption */
  149. static status_t mmcau_DesCrypt(const uint8_t *in, const uint8_t *key, uint8_t *out, bool encrypt)
  150. {
  151. status_t status;
  152. if (in && key && out)
  153. {
  154. uint8_t keyAlign[MMCAU_DES_BLOCK_SIZE]; /* 8 bytes key size aligned */
  155. uint8_t inAlign[MMCAU_DES_BLOCK_SIZE]; /* 8 bytes input block aligned */
  156. uint8_t outAlign[MMCAU_DES_BLOCK_SIZE]; /* 8 bytes output block aligned */
  157. const uint8_t *inWork;
  158. const uint8_t *keyWork;
  159. uint8_t *outWork;
  160. bool copyOut;
  161. /* align pointers */
  162. inWork = mmcau_align_const(in, inAlign, MMCAU_DES_BLOCK_SIZE);
  163. keyWork = mmcau_align_const(key, keyAlign, MMCAU_DES_BLOCK_SIZE);
  164. outWork = mmcau_align(out, outAlign, &copyOut);
  165. /* call CAU API */
  166. if (encrypt)
  167. {
  168. cau_des_encrypt(inWork, keyWork, outWork);
  169. }
  170. else
  171. {
  172. cau_des_decrypt(inWork, keyWork, outWork);
  173. }
  174. if (copyOut)
  175. {
  176. mmcau_memcpy(out, outAlign, MMCAU_DES_BLOCK_SIZE);
  177. }
  178. status = kStatus_Success;
  179. }
  180. else
  181. {
  182. status = kStatus_InvalidArgument;
  183. }
  184. return status;
  185. }
  186. static status_t mmcau_hash_API(
  187. cau_hash_api_t cauFunc, const uint8_t *msgData, uint32_t numBlocks, void *hashState, size_t stateSize)
  188. {
  189. status_t status;
  190. if (msgData && hashState && numBlocks)
  191. {
  192. const uint8_t *msgDataWork;
  193. void *hashStateWork;
  194. uint8_t msgDataAlign[MMCAU_HASH_BLOCK_SIZE];
  195. uint8_t hashStateAlign[MMCAU_HASH_STATE_SIZE];
  196. bool copyInOut;
  197. /* get aligned pointers */
  198. msgDataWork = mmcau_align_const(msgData, msgDataAlign, MMCAU_HASH_BLOCK_SIZE);
  199. hashStateWork = mmcau_align(hashState, hashStateAlign, &copyInOut);
  200. if (copyInOut)
  201. {
  202. mmcau_memcpy(hashStateAlign, hashState, stateSize);
  203. }
  204. /* CAU API */
  205. cauFunc(msgDataWork, numBlocks, hashStateWork);
  206. /* copy result to misaligned address */
  207. if (copyInOut)
  208. {
  209. mmcau_memcpy(hashState, hashStateAlign, stateSize);
  210. }
  211. status = kStatus_Success;
  212. }
  213. else
  214. {
  215. status = kStatus_InvalidArgument;
  216. }
  217. return status;
  218. }
  219. static status_t mmcau_hash_MD5API(
  220. cau_hash_md5_api_t cauFunc, const uint8_t *msgData, uint32_t numBlocks, void *hashState, size_t stateSize)
  221. {
  222. status_t status;
  223. if (msgData && hashState && numBlocks)
  224. {
  225. const uint8_t *msgDataWork;
  226. void *hashStateWork;
  227. uint8_t msgDataAlign[MMCAU_HASH_BLOCK_SIZE];
  228. uint8_t hashStateAlign[MMCAU_HASH_STATE_SIZE];
  229. bool copyInOut;
  230. /* get aligned pointers */
  231. msgDataWork = mmcau_align_const(msgData, msgDataAlign, MMCAU_HASH_BLOCK_SIZE);
  232. hashStateWork = mmcau_align(hashState, hashStateAlign, &copyInOut);
  233. if (copyInOut)
  234. {
  235. mmcau_memcpy(hashStateAlign, hashState, stateSize);
  236. }
  237. /* CAU API */
  238. cauFunc(msgDataWork, numBlocks, hashStateWork);
  239. /* copy result to misaligned address */
  240. if (copyInOut)
  241. {
  242. mmcau_memcpy(hashState, hashStateAlign, stateSize);
  243. }
  244. status = kStatus_Success;
  245. }
  246. else
  247. {
  248. status = kStatus_InvalidArgument;
  249. }
  250. return status;
  251. }
  252. status_t MMCAU_AES_SetKey(const uint8_t *key, const size_t keySize, uint8_t *keySch)
  253. {
  254. status_t status;
  255. /* check validity of input arguments */
  256. if (((keySize != 16u) && (keySize != 24u) && (keySize != 32u)) || (NULL == key) || (NULL == keySch))
  257. {
  258. status = kStatus_InvalidArgument;
  259. }
  260. else
  261. {
  262. uint8_t keyAlign[32] = {0}; /* max 32 bytes key supported by CAU lib */
  263. uint32_t keySchAlign[60] = {0}; /* max 60 longwords in case of 32 bytes AES key */
  264. const uint8_t *keyWork; /* aligned CAU lib input address argument */
  265. uint8_t *keySchWork; /* aligned CAU lib output address argument */
  266. bool copyOut;
  267. size_t sizeOut = 0;
  268. keyWork = mmcau_align_const(key, keyAlign, sizeof(keyAlign));
  269. keySchWork = mmcau_align(keySch, keySchAlign, &copyOut);
  270. /* call CAU lib API with all addresses aligned */
  271. cau_aes_set_key(keyWork, keySize * 8, keySchWork);
  272. /* in case we have aligned output to local, copy the result out */
  273. if (copyOut)
  274. {
  275. if (keySize == 16u)
  276. {
  277. sizeOut = 44u * sizeof(uint32_t);
  278. }
  279. else if (keySize == 24u)
  280. {
  281. sizeOut = 52u * sizeof(uint32_t);
  282. }
  283. else /* keySize = 32u */
  284. {
  285. sizeOut = 60u * sizeof(uint32_t);
  286. }
  287. mmcau_memcpy(keySch, keySchAlign, sizeOut);
  288. }
  289. status = kStatus_Success;
  290. }
  291. return status;
  292. }
  293. status_t MMCAU_AES_EncryptEcb(const uint8_t *in, const uint8_t *keySch, uint32_t aesRounds, uint8_t *out)
  294. {
  295. return mmcau_AesCrypt(in, keySch, aesRounds, out, true /* true for encryption */);
  296. }
  297. status_t MMCAU_AES_DecryptEcb(const uint8_t *in, const uint8_t *keySch, uint32_t aesRounds, uint8_t *out)
  298. {
  299. return mmcau_AesCrypt(in, keySch, aesRounds, out, false /* false for decryption */);
  300. }
  301. status_t MMCAU_DES_ChkParity(const uint8_t *key)
  302. {
  303. status_t status;
  304. if (key)
  305. {
  306. uint8_t keyAlign[8]; /* 8 bytes key size aligned */
  307. const uint8_t *keyWork;
  308. /* align key[] */
  309. keyWork = mmcau_align_const(key, keyAlign, sizeof(keyAlign));
  310. /* call CAU API */
  311. if (0 == cau_des_chk_parity(keyWork))
  312. {
  313. status = kStatus_Success;
  314. }
  315. else
  316. {
  317. status = kStatus_Fail;
  318. }
  319. }
  320. else
  321. {
  322. status = kStatus_InvalidArgument;
  323. }
  324. return status;
  325. }
  326. status_t MMCAU_DES_EncryptEcb(const uint8_t *in, const uint8_t *key, uint8_t *out)
  327. {
  328. return mmcau_DesCrypt(in, key, out, true /* 1 for encryption */);
  329. }
  330. status_t MMCAU_DES_DecryptEcb(const uint8_t *in, const uint8_t *key, uint8_t *out)
  331. {
  332. return mmcau_DesCrypt(in, key, out, false /* 0 for decryption */);
  333. }
  334. /* cau_md5_initialize_output() */
  335. status_t MMCAU_MD5_InitializeOutput(uint32_t *md5State)
  336. {
  337. status_t status;
  338. if (md5State)
  339. {
  340. uint8_t hashStateAlign[MMCAU_HASH_STATE_SIZE];
  341. void *hashStateWork;
  342. bool copyInOut;
  343. /* align pointer */
  344. hashStateWork = mmcau_align(md5State, hashStateAlign, &copyInOut);
  345. if (copyInOut)
  346. {
  347. mmcau_memcpy(hashStateAlign, md5State, MMCAU_MD5_STATE_SIZE);
  348. }
  349. /* CAU API */
  350. cau_md5_initialize_output(hashStateWork);
  351. /* copy result to unaligned pointer */
  352. if (copyInOut)
  353. {
  354. mmcau_memcpy(md5State, hashStateAlign, MMCAU_MD5_STATE_SIZE);
  355. }
  356. status = kStatus_Success;
  357. }
  358. else
  359. {
  360. status = kStatus_InvalidArgument;
  361. }
  362. return status;
  363. }
  364. /* cau_md5_hash_n */
  365. status_t MMCAU_MD5_HashN(const uint8_t *msgData, uint32_t numBlocks, uint32_t *md5State)
  366. {
  367. return mmcau_hash_MD5API((cau_hash_md5_api_t)cau_md5_hash_n, msgData, numBlocks, md5State, MMCAU_MD5_STATE_SIZE);
  368. }
  369. /* cau_md5_update */
  370. status_t MMCAU_MD5_Update(const uint8_t *msgData, uint32_t numBlocks, uint32_t *md5State)
  371. {
  372. return mmcau_hash_MD5API((cau_hash_md5_api_t)cau_md5_update, msgData, numBlocks, md5State, MMCAU_MD5_STATE_SIZE);
  373. }
  374. /* cau_sha1_initialize_output */
  375. status_t MMCAU_SHA1_InitializeOutput(uint32_t *sha1State)
  376. {
  377. status_t status;
  378. if (sha1State)
  379. {
  380. uint8_t hashStateAlign[MMCAU_HASH_STATE_SIZE];
  381. void *hashStateWork;
  382. bool copyInOut;
  383. /* align pointer */
  384. hashStateWork = mmcau_align(sha1State, hashStateAlign, &copyInOut);
  385. if (copyInOut)
  386. {
  387. mmcau_memcpy(hashStateAlign, sha1State, MMCAU_SHA1_STATE_SIZE);
  388. }
  389. /* CAU API */
  390. cau_sha1_initialize_output(hashStateWork);
  391. /* copy result to unaligned pointer */
  392. if (copyInOut)
  393. {
  394. mmcau_memcpy(sha1State, hashStateAlign, MMCAU_SHA1_STATE_SIZE);
  395. }
  396. status = kStatus_Success;
  397. }
  398. else
  399. {
  400. status = kStatus_InvalidArgument;
  401. }
  402. return status;
  403. }
  404. /* cau_sha1_hash_n */
  405. status_t MMCAU_SHA1_HashN(const uint8_t *msgData, uint32_t numBlocks, uint32_t *sha1State)
  406. {
  407. return mmcau_hash_API((cau_hash_api_t)cau_sha1_hash_n, msgData, numBlocks, sha1State, MMCAU_SHA1_STATE_SIZE);
  408. }
  409. /* cau_sha1_update */
  410. status_t MMCAU_SHA1_Update(const uint8_t *msgData, uint32_t numBlocks, uint32_t *sha1State)
  411. {
  412. return mmcau_hash_API((cau_hash_api_t)cau_sha1_update, msgData, numBlocks, sha1State, MMCAU_SHA1_STATE_SIZE);
  413. }
  414. /* cau_sha256_initialize_output(). not this function has different return value (int) that the other two (void) */
  415. status_t MMCAU_SHA256_InitializeOutput(uint32_t *sha256State)
  416. {
  417. status_t status;
  418. int ret;
  419. if (sha256State)
  420. {
  421. uint8_t hashStateAlign[MMCAU_HASH_STATE_SIZE];
  422. void *hashStateWork;
  423. bool copyInOut;
  424. /* align pointer */
  425. hashStateWork = mmcau_align(sha256State, hashStateAlign, &copyInOut);
  426. if (copyInOut)
  427. {
  428. mmcau_memcpy(hashStateAlign, sha256State, MMCAU_SHA256_STATE_SIZE);
  429. }
  430. /* CAU API */
  431. ret = cau_sha256_initialize_output(hashStateWork);
  432. /* copy result to unaligned pointer */
  433. if (copyInOut)
  434. {
  435. mmcau_memcpy(sha256State, hashStateAlign, MMCAU_SHA256_STATE_SIZE);
  436. }
  437. if (ret == 0)
  438. {
  439. status = kStatus_Success;
  440. }
  441. else
  442. {
  443. status = kStatus_Fail;
  444. }
  445. }
  446. else
  447. {
  448. status = kStatus_InvalidArgument;
  449. }
  450. return status;
  451. }
  452. /* cau_sha256_hash_n */
  453. status_t MMCAU_SHA256_HashN(const uint8_t *input, uint32_t numBlocks, uint32_t *sha256State)
  454. {
  455. return mmcau_hash_API((cau_hash_api_t)cau_sha256_hash_n, input, numBlocks, sha256State, MMCAU_SHA256_STATE_SIZE);
  456. }
  457. /* cau_sha256_update */
  458. status_t MMCAU_SHA256_Update(const uint8_t *input, uint32_t numBlocks, uint32_t *sha256State)
  459. {
  460. return mmcau_hash_API((cau_hash_api_t)cau_sha256_update, input, numBlocks, sha256State, MMCAU_SHA256_STATE_SIZE);
  461. }