hal_crypto.c 62 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747
  1. /*
  2. ChibiOS - Copyright (C) 2006..2018 Giovanni Di Sirio
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. /**
  14. * @file hal_crypto.c
  15. * @brief Cryptographic Driver code.
  16. *
  17. * @addtogroup CRYPTO
  18. * @{
  19. */
  20. #include "hal.h"
  21. #if (HAL_USE_CRY == TRUE) || defined(__DOXYGEN__)
  22. /*===========================================================================*/
  23. /* Driver local definitions. */
  24. /*===========================================================================*/
  25. /*===========================================================================*/
  26. /* Driver exported variables. */
  27. /*===========================================================================*/
  28. /*===========================================================================*/
  29. /* Driver local variables and types. */
  30. /*===========================================================================*/
  31. /*===========================================================================*/
  32. /* Driver local functions. */
  33. /*===========================================================================*/
  34. /*===========================================================================*/
  35. /* Driver exported functions. */
  36. /*===========================================================================*/
  37. /**
  38. * @brief Cryptographic Driver initialization.
  39. * @note This function is implicitly invoked by @p halInit(), there is
  40. * no need to explicitly initialize the driver.
  41. *
  42. * @init
  43. */
  44. void cryInit(void) {
  45. #if HAL_CRY_ENFORCE_FALLBACK == FALSE
  46. cry_lld_init();
  47. #endif
  48. }
  49. /**
  50. * @brief Initializes the standard part of a @p CRYDriver structure.
  51. *
  52. * @param[out] cryp pointer to the @p CRYDriver object
  53. *
  54. * @init
  55. */
  56. void cryObjectInit(CRYDriver *cryp) {
  57. cryp->state = CRY_STOP;
  58. cryp->config = NULL;
  59. #if defined(CRY_DRIVER_EXT_INIT_HOOK)
  60. CRY_DRIVER_EXT_INIT_HOOK(cryp);
  61. #endif
  62. }
  63. /**
  64. * @brief Configures and activates the cryptographic peripheral.
  65. *
  66. * @param[in] cryp pointer to the @p CRYDriver object
  67. * @param[in] config pointer to the @p CRYConfig object. Depending
  68. * on the implementation the value can be @p NULL.
  69. *
  70. * @api
  71. */
  72. void cryStart(CRYDriver *cryp, const CRYConfig *config) {
  73. osalDbgCheck(cryp != NULL);
  74. osalSysLock();
  75. osalDbgAssert((cryp->state == CRY_STOP) || (cryp->state == CRY_READY),
  76. "invalid state");
  77. cryp->config = config;
  78. #if HAL_CRY_ENFORCE_FALLBACK == FALSE
  79. cry_lld_start(cryp);
  80. #endif
  81. cryp->state = CRY_READY;
  82. osalSysUnlock();
  83. }
  84. /**
  85. * @brief Deactivates the cryptographic peripheral.
  86. *
  87. * @param[in] cryp pointer to the @p CRYDriver object
  88. *
  89. * @api
  90. */
  91. void cryStop(CRYDriver *cryp) {
  92. osalDbgCheck(cryp != NULL);
  93. osalSysLock();
  94. osalDbgAssert((cryp->state == CRY_STOP) || (cryp->state == CRY_READY),
  95. "invalid state");
  96. #if HAL_CRY_ENFORCE_FALLBACK == FALSE
  97. cry_lld_stop(cryp);
  98. #endif
  99. cryp->config = NULL;
  100. cryp->state = CRY_STOP;
  101. osalSysUnlock();
  102. }
  103. /**
  104. * @brief Initializes the AES transient key.
  105. * @note It is the underlying implementation to decide which key sizes are
  106. * allowable.
  107. *
  108. * @param[in] cryp pointer to the @p CRYDriver object
  109. * @param[in] size key size in bytes
  110. * @param[in] keyp pointer to the key data
  111. * @return The operation status.
  112. * @retval CRY_NOERROR if the operation succeeded.
  113. * @retval CRY_ERR_INV_ALGO if the algorithm is unsupported.
  114. * @retval CRY_ERR_INV_KEY_SIZE if the specified key size is invalid for
  115. * the specified algorithm.
  116. *
  117. * @api
  118. */
  119. cryerror_t cryLoadAESTransientKey(CRYDriver *cryp,
  120. size_t size,
  121. const uint8_t *keyp) {
  122. osalDbgCheck((cryp != NULL) && (keyp != NULL));
  123. #if CRY_LLD_SUPPORTS_AES == TRUE
  124. return cry_lld_aes_loadkey(cryp, size, keyp);
  125. #elif HAL_CRY_USE_FALLBACK == TRUE
  126. return cry_fallback_aes_loadkey(cryp, size, keyp);
  127. #else
  128. (void)cryp;
  129. (void)size;
  130. (void)keyp;
  131. return CRY_ERR_INV_ALGO;
  132. #endif
  133. }
  134. /**
  135. * @brief Encryption of a single block using AES.
  136. * @note The implementation of this function must guarantee that it can
  137. * be called from any context.
  138. *
  139. * @param[in] cryp pointer to the @p CRYDriver object
  140. * @param[in] key_id the key to be used for the operation, zero is
  141. * the transient key, other values are keys stored
  142. * in an unspecified way
  143. * @param[in] in buffer containing the input plaintext
  144. * @param[out] out buffer for the output cyphertext
  145. * @return The operation status.
  146. * @retval CRY_NOERROR if the operation succeeded.
  147. * @retval CRY_ERR_INV_ALGO if the operation is unsupported on this
  148. * device instance.
  149. * @retval CRY_ERR_INV_KEY_TYPE the selected key is invalid for this operation.
  150. * @retval CRY_ERR_INV_KEY_ID if the specified key identifier is invalid
  151. * or refers to an empty key slot.
  152. * @retval CRY_ERR_OP_FAILURE if the operation failed, implementation
  153. * dependent.
  154. *
  155. * @special
  156. */
  157. cryerror_t cryEncryptAES(CRYDriver *cryp,
  158. crykey_t key_id,
  159. const uint8_t *in,
  160. uint8_t *out) {
  161. osalDbgCheck((cryp != NULL) && (in != NULL) && (out != NULL));
  162. osalDbgAssert(cryp->state == CRY_READY, "not ready");
  163. #if CRY_LLD_SUPPORTS_AES == TRUE
  164. return cry_lld_encrypt_AES(cryp, key_id, in, out);
  165. #elif HAL_CRY_USE_FALLBACK == TRUE
  166. return cry_fallback_encrypt_AES(cryp, key_id, in, out);
  167. #else
  168. (void)cryp;
  169. (void)key_id;
  170. (void)in;
  171. (void)out;
  172. return CRY_ERR_INV_ALGO;
  173. #endif
  174. }
  175. /**
  176. * @brief Decryption of a single block using AES.
  177. * @note The implementation of this function must guarantee that it can
  178. * be called from any context.
  179. *
  180. * @param[in] cryp pointer to the @p CRYDriver object
  181. * @param[in] key_id the key to be used for the operation, zero is
  182. * the transient key, other values are keys stored
  183. * in an unspecified way
  184. * @param[in] in buffer containing the input cyphertext
  185. * @param[out] out buffer for the output plaintext
  186. * @return The operation status.
  187. * @retval CRY_NOERROR if the operation succeeded.
  188. * @retval CRY_ERR_INV_ALGO if the operation is unsupported on this
  189. * device instance.
  190. * @retval CRY_ERR_INV_KEY_TYPE the selected key is invalid for this operation.
  191. * @retval CRY_ERR_INV_KEY_ID if the specified key identifier is invalid
  192. * or refers to an empty key slot.
  193. * @retval CRY_ERR_OP_FAILURE if the operation failed, implementation
  194. * dependent.
  195. *
  196. * @special
  197. */
  198. cryerror_t cryDecryptAES(CRYDriver *cryp,
  199. crykey_t key_id,
  200. const uint8_t *in,
  201. uint8_t *out) {
  202. osalDbgCheck((cryp != NULL) && (in != NULL) && (out != NULL));
  203. osalDbgAssert(cryp->state == CRY_READY, "not ready");
  204. #if CRY_LLD_SUPPORTS_AES == TRUE
  205. return cry_lld_decrypt_AES(cryp, key_id, in, out);
  206. #elif HAL_CRY_USE_FALLBACK == TRUE
  207. return cry_fallback_decrypt_AES(cryp, key_id, in, out);
  208. #else
  209. (void)cryp;
  210. (void)key_id;
  211. (void)in;
  212. (void)out;
  213. return CRY_ERR_INV_ALGO;
  214. #endif
  215. }
  216. /**
  217. * @brief Encryption operation using AES-ECB.
  218. * @note The function operates on data buffers whose length is a multiple
  219. * of an AES block, this means that padding must be done by the
  220. * caller.
  221. *
  222. * @param[in] cryp pointer to the @p CRYDriver object
  223. * @param[in] key_id the key to be used for the operation, zero is
  224. * the transient key, other values are keys stored
  225. * in an unspecified way
  226. * @param[in] size size of both buffers, this number must be a
  227. * multiple of 16
  228. * @param[in] in buffer containing the input plaintext
  229. * @param[out] out buffer for the output cyphertext
  230. * @return The operation status.
  231. * @retval CRY_NOERROR if the operation succeeded.
  232. * @retval CRY_ERR_INV_ALGO if the operation is unsupported on this
  233. * device instance.
  234. * @retval CRY_ERR_INV_KEY_TYPE the selected key is invalid for this operation.
  235. * @retval CRY_ERR_INV_KEY_ID if the specified key identifier is invalid
  236. * or refers to an empty key slot.
  237. * @retval CRY_ERR_OP_FAILURE if the operation failed, implementation
  238. * dependent.
  239. *
  240. * @api
  241. */
  242. cryerror_t cryEncryptAES_ECB(CRYDriver *cryp,
  243. crykey_t key_id,
  244. size_t size,
  245. const uint8_t *in,
  246. uint8_t *out) {
  247. osalDbgCheck((cryp != NULL) && (in != NULL) && (out != NULL) &&
  248. ((size & (size_t)15) == (size_t)0));
  249. osalDbgAssert(cryp->state == CRY_READY, "not ready");
  250. #if CRY_LLD_SUPPORTS_AES_ECB == TRUE
  251. return cry_lld_encrypt_AES_ECB(cryp, key_id, size, in, out);
  252. #elif HAL_CRY_USE_FALLBACK == TRUE
  253. return cry_fallback_encrypt_AES_ECB(cryp, key_id, size, in, out);
  254. #else
  255. (void)cryp;
  256. (void)key_id;
  257. (void)size;
  258. (void)in;
  259. (void)out;
  260. return CRY_ERR_INV_ALGO;
  261. #endif
  262. }
  263. /**
  264. * @brief Decryption operation using AES-ECB.
  265. * @note The function operates on data buffers whose length is a multiple
  266. * of an AES block, this means that padding must be done by the
  267. * caller.
  268. *
  269. * @param[in] cryp pointer to the @p CRYDriver object
  270. * @param[in] key_id the key to be used for the operation, zero is
  271. * the transient key, other values are keys stored
  272. * in an unspecified way
  273. * @param[in] size size of both buffers, this number must be a
  274. * multiple of 16
  275. * @param[in] in buffer containing the input cyphertext
  276. * @param[out] out buffer for the output plaintext
  277. * @return The operation status.
  278. * @retval CRY_NOERROR if the operation succeeded.
  279. * @retval CRY_ERR_INV_ALGO if the operation is unsupported on this
  280. * device instance.
  281. * @retval CRY_ERR_INV_KEY_TYPE the selected key is invalid for this operation.
  282. * @retval CRY_ERR_INV_KEY_ID if the specified key identifier is invalid
  283. * or refers to an empty key slot.
  284. * @retval CRY_ERR_OP_FAILURE if the operation failed, implementation
  285. * dependent.
  286. *
  287. * @api
  288. */
  289. cryerror_t cryDecryptAES_ECB(CRYDriver *cryp,
  290. crykey_t key_id,
  291. size_t size,
  292. const uint8_t *in,
  293. uint8_t *out) {
  294. osalDbgCheck((cryp != NULL) && (in != NULL) && (out != NULL) &&
  295. ((size & (size_t)15) == (size_t)0));
  296. osalDbgAssert(cryp->state == CRY_READY, "not ready");
  297. #if CRY_LLD_SUPPORTS_AES_ECB == TRUE
  298. return cry_lld_decrypt_AES_ECB(cryp, key_id, size, in, out);
  299. #elif HAL_CRY_USE_FALLBACK == TRUE
  300. return cry_fallback_decrypt_AES_ECB(cryp, key_id, size, in, out);
  301. #else
  302. (void)cryp;
  303. (void)key_id;
  304. (void)size;
  305. (void)in;
  306. (void)out;
  307. return CRY_ERR_INV_ALGO;
  308. #endif
  309. }
  310. /**
  311. * @brief Encryption operation using AES-CBC.
  312. * @note The function operates on data buffers whose length is a multiple
  313. * of an AES block, this means that padding must be done by the
  314. * caller.
  315. *
  316. * @param[in] cryp pointer to the @p CRYDriver object
  317. * @param[in] key_id the key to be used for the operation, zero is
  318. * the transient key, other values are keys stored
  319. * in an unspecified way
  320. * @param[in] size size of both buffers, this number must be a
  321. * multiple of 16
  322. * @param[in] in buffer containing the input plaintext
  323. * @param[out] out buffer for the output cyphertext
  324. * @param[in] iv 128 bits input vector
  325. * @return The operation status.
  326. * @retval CRY_NOERROR if the operation succeeded.
  327. * @retval CRY_ERR_INV_ALGO if the operation is unsupported on this
  328. * device instance.
  329. * @retval CRY_ERR_INV_KEY_TYPE the selected key is invalid for this operation.
  330. * @retval CRY_ERR_INV_KEY_ID if the specified key identifier is invalid
  331. * or refers to an empty key slot.
  332. * @retval CRY_ERR_OP_FAILURE if the operation failed, implementation
  333. * dependent.
  334. *
  335. * @api
  336. */
  337. cryerror_t cryEncryptAES_CBC(CRYDriver *cryp,
  338. crykey_t key_id,
  339. size_t size,
  340. const uint8_t *in,
  341. uint8_t *out,
  342. const uint8_t *iv) {
  343. osalDbgCheck((cryp != NULL) && (in != NULL) && (out != NULL) &&
  344. (iv != NULL) && ((size & (size_t)15) == (size_t)0));
  345. osalDbgAssert(cryp->state == CRY_READY, "not ready");
  346. #if CRY_LLD_SUPPORTS_AES_CBC == TRUE
  347. return cry_lld_encrypt_AES_CBC(cryp, key_id, size, in, out, iv);
  348. #elif HAL_CRY_USE_FALLBACK == TRUE
  349. return cry_fallback_encrypt_AES_CBC(cryp, key_id, size, in, out, iv);
  350. #else
  351. (void)cryp;
  352. (void)key_id;
  353. (void)size;
  354. (void)in;
  355. (void)out;
  356. (void)iv;
  357. return CRY_ERR_INV_ALGO;
  358. #endif
  359. }
  360. /**
  361. * @brief Decryption operation using AES-CBC.
  362. * @note The function operates on data buffers whose length is a multiple
  363. * of an AES block, this means that padding must be done by the
  364. * caller.
  365. *
  366. * @param[in] cryp pointer to the @p CRYDriver object
  367. * @param[in] key_id the key to be used for the operation, zero is
  368. * the transient key, other values are keys stored
  369. * in an unspecified way
  370. * @param[in] size size of both buffers, this number must be a
  371. * multiple of 16
  372. * @param[in] in buffer containing the input cyphertext
  373. * @param[out] out buffer for the output plaintext
  374. * @param[in] iv 128 bits input vector
  375. * @return The operation status.
  376. * @retval CRY_NOERROR if the operation succeeded.
  377. * @retval CRY_ERR_INV_ALGO if the operation is unsupported on this
  378. * device instance.
  379. * @retval CRY_ERR_INV_KEY_TYPE the selected key is invalid for this operation.
  380. * @retval CRY_ERR_INV_KEY_ID if the specified key identifier is invalid
  381. * or refers to an empty key slot.
  382. * @retval CRY_ERR_OP_FAILURE if the operation failed, implementation
  383. * dependent.
  384. *
  385. * @api
  386. */
  387. cryerror_t cryDecryptAES_CBC(CRYDriver *cryp,
  388. crykey_t key_id,
  389. size_t size,
  390. const uint8_t *in,
  391. uint8_t *out,
  392. const uint8_t *iv) {
  393. osalDbgCheck((cryp != NULL) && (in != NULL) && (out != NULL) &&
  394. (iv != NULL) && ((size & (size_t)15) == (size_t)0));
  395. osalDbgAssert(cryp->state == CRY_READY, "not ready");
  396. #if CRY_LLD_SUPPORTS_AES_CBC == TRUE
  397. return cry_lld_decrypt_AES_CBC(cryp, key_id, size, in, out, iv);
  398. #elif HAL_CRY_USE_FALLBACK == TRUE
  399. return cry_fallback_decrypt_AES_CBC(cryp, key_id, size, in, out, iv);
  400. #else
  401. (void)cryp;
  402. (void)key_id;
  403. (void)size;
  404. (void)in;
  405. (void)out;
  406. (void)iv;
  407. return CRY_ERR_INV_ALGO;
  408. #endif
  409. }
  410. /**
  411. * @brief Encryption operation using AES-CFB.
  412. * @note The function operates on data buffers whose length is a multiple
  413. * of an AES block, this means that padding must be done by the
  414. * caller.
  415. *
  416. * @param[in] cryp pointer to the @p CRYDriver object
  417. * @param[in] key_id the key to be used for the operation, zero is
  418. * the transient key, other values are keys stored
  419. * in an unspecified way
  420. * @param[in] size size of both buffers, this number must be a
  421. * multiple of 16
  422. * @param[in] in buffer containing the input plaintext
  423. * @param[out] out buffer for the output cyphertext
  424. * @param[in] iv 128 bits input vector
  425. * @return The operation status.
  426. * @retval CRY_NOERROR if the operation succeeded.
  427. * @retval CRY_ERR_INV_ALGO if the operation is unsupported on this
  428. * device instance.
  429. * @retval CRY_ERR_INV_KEY_TYPE the selected key is invalid for this operation.
  430. * @retval CRY_ERR_INV_KEY_ID if the specified key identifier is invalid
  431. * or refers to an empty key slot.
  432. * @retval CRY_ERR_OP_FAILURE if the operation failed, implementation
  433. * dependent.
  434. *
  435. * @api
  436. */
  437. cryerror_t cryEncryptAES_CFB(CRYDriver *cryp,
  438. crykey_t key_id,
  439. size_t size,
  440. const uint8_t *in,
  441. uint8_t *out,
  442. const uint8_t *iv) {
  443. osalDbgCheck((cryp != NULL) && (in != NULL) && (out != NULL) &&
  444. (iv != NULL) && ((size & (size_t)15) == (size_t)0));
  445. osalDbgAssert(cryp->state == CRY_READY, "not ready");
  446. #if CRY_LLD_SUPPORTS_AES_CFB == TRUE
  447. return cry_lld_encrypt_AES_CFB(cryp, key_id, size, in, out, iv);
  448. #elif HAL_CRY_USE_FALLBACK == TRUE
  449. return cry_fallback_encrypt_AES_CFB(cryp, key_id, size, in, out, iv);
  450. #else
  451. (void)cryp;
  452. (void)key_id;
  453. (void)size;
  454. (void)in;
  455. (void)out;
  456. (void)iv;
  457. return CRY_ERR_INV_ALGO;
  458. #endif
  459. }
  460. /**
  461. * @brief Decryption operation using AES-CFB.
  462. * @note The function operates on data buffers whose length is a multiple
  463. * of an AES block, this means that padding must be done by the
  464. * caller.
  465. *
  466. * @param[in] cryp pointer to the @p CRYDriver object
  467. * @param[in] key_id the key to be used for the operation, zero is
  468. * the transient key, other values are keys stored
  469. * in an unspecified way
  470. * @param[in] size size of both buffers, this number must be a
  471. * multiple of 16
  472. * @param[in] in buffer containing the input cyphertext
  473. * @param[out] out buffer for the output plaintext
  474. * @param[in] iv 128 bits input vector
  475. * @return The operation status.
  476. * @retval CRY_NOERROR if the operation succeeded.
  477. * @retval CRY_ERR_INV_ALGO if the operation is unsupported on this
  478. * device instance.
  479. * @retval CRY_ERR_INV_KEY_TYPE the selected key is invalid for this operation.
  480. * @retval CRY_ERR_INV_KEY_ID if the specified key identifier is invalid
  481. * or refers to an empty key slot.
  482. * @retval CRY_ERR_OP_FAILURE if the operation failed, implementation
  483. * dependent.
  484. *
  485. * @api
  486. */
  487. cryerror_t cryDecryptAES_CFB(CRYDriver *cryp,
  488. crykey_t key_id,
  489. size_t size,
  490. const uint8_t *in,
  491. uint8_t *out,
  492. const uint8_t *iv) {
  493. osalDbgCheck((cryp != NULL) && (in != NULL) && (out != NULL) &&
  494. (iv != NULL) && ((size & (size_t)15) == (size_t)0));
  495. osalDbgAssert(cryp->state == CRY_READY, "not ready");
  496. #if CRY_LLD_SUPPORTS_AES_CFB == TRUE
  497. return cry_lld_decrypt_AES_CFB(cryp, key_id, size, in, out, iv);
  498. #elif HAL_CRY_USE_FALLBACK == TRUE
  499. return cry_fallback_decrypt_AES_CFB(cryp, key_id, size, in, out, iv);
  500. #else
  501. (void)cryp;
  502. (void)key_id;
  503. (void)size;
  504. (void)in;
  505. (void)out;
  506. (void)iv;
  507. return CRY_ERR_INV_ALGO;
  508. #endif
  509. }
  510. /**
  511. * @brief Encryption operation using AES-CTR.
  512. * @note The function operates on data buffers whose length is a multiple
  513. * of an AES block, this means that padding must be done by the
  514. * caller.
  515. *
  516. * @param[in] cryp pointer to the @p CRYDriver object
  517. * @param[in] key_id the key to be used for the operation, zero is
  518. * the transient key, other values are keys stored
  519. * in an unspecified way
  520. * @param[in] size size of both buffers, this number must be a
  521. * multiple of 16
  522. * @param[in] in buffer containing the input plaintext
  523. * @param[out] out buffer for the output cyphertext
  524. * @param[in] iv 128 bits input vector + counter, it contains
  525. * a 96 bits IV and a 32 bits counter
  526. * @return The operation status.
  527. * @retval CRY_NOERROR if the operation succeeded.
  528. * @retval CRY_ERR_INV_ALGO if the operation is unsupported on this
  529. * device instance.
  530. * @retval CRY_ERR_INV_KEY_TYPE the selected key is invalid for this operation.
  531. * @retval CRY_ERR_INV_KEY_ID if the specified key identifier is invalid
  532. * or refers to an empty key slot.
  533. * @retval CRY_ERR_OP_FAILURE if the operation failed, implementation
  534. * dependent.
  535. *
  536. * @api
  537. */
  538. cryerror_t cryEncryptAES_CTR(CRYDriver *cryp,
  539. crykey_t key_id,
  540. size_t size,
  541. const uint8_t *in,
  542. uint8_t *out,
  543. const uint8_t *iv) {
  544. osalDbgCheck((cryp != NULL) && (in != NULL) && (out != NULL) &&
  545. (iv != NULL) && ((size & (size_t)15) == (size_t)0));
  546. osalDbgAssert(cryp->state == CRY_READY, "not ready");
  547. #if CRY_LLD_SUPPORTS_AES_CTR == TRUE
  548. return cry_lld_encrypt_AES_CTR(cryp, key_id, size, in, out, iv);
  549. #elif HAL_CRY_USE_FALLBACK == TRUE
  550. return cry_fallback_encrypt_AES_CTR(cryp, key_id, size, in, out, iv);
  551. #else
  552. (void)cryp;
  553. (void)key_id;
  554. (void)size;
  555. (void)in;
  556. (void)out;
  557. (void)iv;
  558. return CRY_ERR_INV_ALGO;
  559. #endif
  560. }
  561. /**
  562. * @brief Decryption operation using AES-CTR.
  563. * @note The function operates on data buffers whose length is a multiple
  564. * of an AES block, this means that padding must be done by the
  565. * caller.
  566. *
  567. * @param[in] cryp pointer to the @p CRYDriver object
  568. * @param[in] key_id the key to be used for the operation, zero is
  569. * the transient key, other values are keys stored
  570. * in an unspecified way
  571. * @param[in] size size of both buffers, this number must be a
  572. * multiple of 16
  573. * @param[in] in buffer containing the input cyphertext
  574. * @param[out] out buffer for the output plaintext
  575. * @param[in] iv 128 bits input vector + counter, it contains
  576. * a 96 bits IV and a 32 bits counter
  577. * @return The operation status.
  578. * @retval CRY_NOERROR if the operation succeeded.
  579. * @retval CRY_ERR_INV_ALGO if the operation is unsupported on this
  580. * device instance.
  581. * @retval CRY_ERR_INV_KEY_TYPE the selected key is invalid for this operation.
  582. * @retval CRY_ERR_INV_KEY_ID if the specified key identifier is invalid
  583. * or refers to an empty key slot.
  584. * @retval CRY_ERR_OP_FAILURE if the operation failed, implementation
  585. * dependent.
  586. *
  587. * @api
  588. */
  589. cryerror_t cryDecryptAES_CTR(CRYDriver *cryp,
  590. crykey_t key_id,
  591. size_t size,
  592. const uint8_t *in,
  593. uint8_t *out,
  594. const uint8_t *iv) {
  595. osalDbgCheck((cryp != NULL) && (in != NULL) && (out != NULL) &&
  596. (iv != NULL) && ((size & (size_t)15) == (size_t)0));
  597. osalDbgAssert(cryp->state == CRY_READY, "not ready");
  598. #if CRY_LLD_SUPPORTS_AES_CTR == TRUE
  599. return cry_lld_decrypt_AES_CTR(cryp, key_id, size, in, out, iv);
  600. #elif HAL_CRY_USE_FALLBACK == TRUE
  601. return cry_fallback_decrypt_AES_CTR(cryp, key_id, size, in, out, iv);
  602. #else
  603. (void)cryp;
  604. (void)key_id;
  605. (void)size;
  606. (void)in;
  607. (void)out;
  608. (void)iv;
  609. return CRY_ERR_INV_ALGO;
  610. #endif
  611. }
  612. /**
  613. * @brief Encryption operation using AES-GCM.
  614. * @note The function operates on data buffers whose length is a multiple
  615. * of an AES block, this means that padding must be done by the
  616. * caller.
  617. *
  618. * @param[in] cryp pointer to the @p CRYDriver object
  619. * @param[in] key_id the key to be used for the operation, zero is
  620. * the transient key, other values are keys stored
  621. * in an unspecified way
  622. * @param[in] size size of the text buffers, this number must be a
  623. * multiple of 16
  624. * @param[in] in buffer containing the input plaintext
  625. * @param[out] out buffer for the output cyphertext
  626. * @param[in] iv 128 bits input vector + counter, it contains
  627. * a 96 bits IV and a 32 bits counter
  628. * @param[in] aadsize size of the authentication data, this number
  629. * must be a multiple of 16
  630. * @param[in] aad buffer containing the authentication data
  631. * @param[in] authtag 128 bits buffer for the generated authentication
  632. * tag
  633. * @return The operation status.
  634. * @retval CRY_NOERROR if the operation succeeded.
  635. * @retval CRY_ERR_INV_ALGO if the operation is unsupported on this
  636. * device instance.
  637. * @retval CRY_ERR_INV_KEY_TYPE the selected key is invalid for this operation.
  638. * @retval CRY_ERR_INV_KEY_ID if the specified key identifier is invalid
  639. * or refers to an empty key slot.
  640. * @retval CRY_ERR_OP_FAILURE if the operation failed, implementation
  641. * dependent.
  642. *
  643. * @api
  644. */
  645. cryerror_t cryEncryptAES_GCM(CRYDriver *cryp,
  646. crykey_t key_id,
  647. size_t size,
  648. const uint8_t *in,
  649. uint8_t *out,
  650. const uint8_t *iv,
  651. size_t aadsize,
  652. const uint8_t *aad,
  653. uint8_t *authtag) {
  654. osalDbgCheck((cryp != NULL) && (in != NULL) && (out != NULL) &&
  655. (iv != NULL) && (aad != NULL) && (authtag != NULL) &&
  656. ((size & (size_t)15) == (size_t)0) &&
  657. ((aadsize & (size_t)15) == (size_t)0));
  658. osalDbgAssert(cryp->state == CRY_READY, "not ready");
  659. #if CRY_LLD_SUPPORTS_AES_GCM== TRUE
  660. return cry_lld_encrypt_AES_GCM(cryp, key_id, size, in, out, iv,
  661. aadsize, aad, authtag);
  662. #elif HAL_CRY_USE_FALLBACK == TRUE
  663. return cry_fallback_encrypt_AES_GCM(cryp, key_id, size, in, out, iv,
  664. aadsize, aad, authtag);
  665. #else
  666. (void)cryp;
  667. (void)key_id;
  668. (void)size;
  669. (void)in;
  670. (void)out;
  671. (void)iv;
  672. (void)aadsize;
  673. (void)aad;
  674. (void)authtag;
  675. return CRY_ERR_INV_ALGO;
  676. #endif
  677. }
  678. /**
  679. * @brief Decryption operation using AES-GCM.
  680. * @note The function operates on data buffers whose length is a multiple
  681. * of an AES block, this means that padding must be done by the
  682. * caller.
  683. *
  684. * @param[in] cryp pointer to the @p CRYDriver object
  685. * @param[in] key_id the key to be used for the operation, zero is
  686. * the transient key, other values are keys stored
  687. * in an unspecified way
  688. * @param[in] size size of the text buffers, this number must be a
  689. * multiple of 16
  690. * @param[in] in buffer for the output cyphertext
  691. * @param[out] out buffer containing the input plaintext
  692. * @param[in] iv 128 bits input vector + counter, it contains
  693. * a 96 bits IV and a 32 bits counter
  694. * @param[in] aadsize size of the authentication data, this number
  695. * must be a multiple of 16
  696. * @param[in] aad buffer containing the authentication data
  697. * @param[in] authtag 128 bits buffer for the generated authentication
  698. * tag
  699. * @return The operation status.
  700. * @retval CRY_NOERROR if the operation succeeded.
  701. * @retval CRY_ERR_INV_ALGO if the operation is unsupported on this
  702. * device instance.
  703. * @retval CRY_ERR_INV_KEY_TYPE the selected key is invalid for this operation.
  704. * @retval CRY_ERR_INV_KEY_ID if the specified key identifier is invalid
  705. * or refers to an empty key slot.
  706. * @retval CRY_ERR_OP_FAILURE if the operation failed, implementation
  707. * dependent.
  708. *
  709. * @api
  710. */
  711. cryerror_t cryDecryptAES_GCM(CRYDriver *cryp,
  712. crykey_t key_id,
  713. size_t size,
  714. const uint8_t *in,
  715. uint8_t *out,
  716. const uint8_t *iv,
  717. size_t aadsize,
  718. const uint8_t *aad,
  719. uint8_t *authtag) {
  720. osalDbgCheck((cryp != NULL) && (in != NULL) && (out != NULL) &&
  721. (iv != NULL) && (aad != NULL) && (authtag != NULL) &&
  722. ((size & (size_t)15) == (size_t)0) &&
  723. ((aadsize & (size_t)15) == (size_t)0));
  724. osalDbgAssert(cryp->state == CRY_READY, "not ready");
  725. #if CRY_LLD_SUPPORTS_AES_GCM== TRUE
  726. return cry_lld_decrypt_AES_GCM(cryp, key_id, size, in, out, iv,
  727. aadsize, aad, authtag);
  728. #elif HAL_CRY_USE_FALLBACK == TRUE
  729. return cry_fallback_decrypt_AES_GCM(cryp, key_id, size, in, out, iv,
  730. aadsize, aad, authtag);
  731. #else
  732. (void)cryp;
  733. (void)key_id;
  734. (void)size;
  735. (void)in;
  736. (void)out;
  737. (void)iv;
  738. (void)aadsize;
  739. (void)aad;
  740. (void)authtag;
  741. return CRY_ERR_INV_ALGO;
  742. #endif
  743. }
  744. /**
  745. * @brief Initializes the DES transient key.
  746. * @note It is the underlying implementation to decide which key sizes are
  747. * allowable.
  748. *
  749. * @param[in] cryp pointer to the @p CRYDriver object
  750. * @param[in] size key size in bytes
  751. * @param[in] keyp pointer to the key data
  752. * @return The operation status.
  753. * @retval CRY_NOERROR if the operation succeeded.
  754. * @retval CRY_ERR_INV_ALGO if the algorithm is unsupported.
  755. * @retval CRY_ERR_INV_KEY_SIZE if the specified key size is invalid for
  756. * the specified algorithm.
  757. *
  758. * @api
  759. */
  760. cryerror_t cryLoadDESTransientKey(CRYDriver *cryp,
  761. size_t size,
  762. const uint8_t *keyp) {
  763. osalDbgCheck((cryp != NULL) && (keyp != NULL));
  764. #if CRY_LLD_SUPPORTS_DES == TRUE
  765. return cry_lld_des_loadkey(cryp, size, keyp);
  766. #elif HAL_CRY_USE_FALLBACK == TRUE
  767. return cry_fallback_des_loadkey(cryp, size, keyp);
  768. #else
  769. (void)cryp;
  770. (void)size;
  771. (void)keyp;
  772. return CRY_ERR_INV_ALGO;
  773. #endif
  774. }
  775. /**
  776. * @brief Encryption of a single block using (T)DES.
  777. * @note The implementation of this function must guarantee that it can
  778. * be called from any context.
  779. *
  780. * @param[in] cryp pointer to the @p CRYDriver object
  781. * @param[in] key_id the key to be used for the operation, zero is
  782. * the transient key, other values are keys stored
  783. * in an unspecified way
  784. * @param[in] in buffer containing the input plaintext
  785. * @param[out] out buffer for the output cyphertext
  786. * @return The operation status.
  787. * @retval CRY_NOERROR if the operation succeeded.
  788. * @retval CRY_ERR_INV_ALGO if the operation is unsupported on this
  789. * device instance.
  790. * @retval CRY_ERR_INV_KEY_TYPE the selected key is invalid for this operation.
  791. * @retval CRY_ERR_INV_KEY_ID if the specified key identifier is invalid
  792. * or refers to an empty key slot.
  793. * @retval CRY_ERR_OP_FAILURE if the operation failed, implementation
  794. * dependent.
  795. *
  796. * @special
  797. */
  798. cryerror_t cryEncryptDES(CRYDriver *cryp,
  799. crykey_t key_id,
  800. const uint8_t *in,
  801. uint8_t *out) {
  802. osalDbgCheck((cryp != NULL) && (in != NULL) && (out != NULL));
  803. osalDbgAssert(cryp->state == CRY_READY, "not ready");
  804. #if CRY_LLD_SUPPORTS_DES == TRUE
  805. return cry_lld_encrypt_DES(cryp, key_id, in, out);
  806. #elif HAL_CRY_USE_FALLBACK == TRUE
  807. return cry_fallback_encrypt_DES(cryp, key_id, in, out);
  808. #else
  809. (void)cryp;
  810. (void)key_id;
  811. (void)in;
  812. (void)out;
  813. return CRY_ERR_INV_ALGO;
  814. #endif
  815. }
  816. /**
  817. * @brief Decryption of a single block using (T)DES.
  818. * @note The implementation of this function must guarantee that it can
  819. * be called from any context.
  820. *
  821. *
  822. * @param[in] cryp pointer to the @p CRYDriver object
  823. * @param[in] key_id the key to be used for the operation, zero is
  824. * the transient key, other values are keys stored
  825. * in an unspecified way
  826. * @param[in] in buffer containing the input cyphertext
  827. * @param[out] out buffer for the output plaintext
  828. * @return The operation status.
  829. * @retval CRY_NOERROR if the operation succeeded.
  830. * @retval CRY_ERR_INV_ALGO if the operation is unsupported on this
  831. * device instance.
  832. * @retval CRY_ERR_INV_KEY_TYPE the selected key is invalid for this operation.
  833. * @retval CRY_ERR_INV_KEY_ID if the specified key identifier is invalid
  834. * or refers to an empty key slot.
  835. * @retval CRY_ERR_OP_FAILURE if the operation failed, implementation
  836. * dependent.
  837. *
  838. * @special
  839. */
  840. cryerror_t cryDecryptDES(CRYDriver *cryp,
  841. crykey_t key_id,
  842. const uint8_t *in,
  843. uint8_t *out) {
  844. osalDbgCheck((cryp != NULL) && (in != NULL) && (out != NULL));
  845. osalDbgAssert(cryp->state == CRY_READY, "not ready");
  846. #if CRY_LLD_SUPPORTS_DES == TRUE
  847. return cry_lld_decrypt_DES(cryp, key_id, in, out);
  848. #elif HAL_CRY_USE_FALLBACK == TRUE
  849. return cry_fallback_decrypt_DES(cryp, key_id, in, out);
  850. #else
  851. (void)cryp;
  852. (void)key_id;
  853. (void)in;
  854. (void)out;
  855. return CRY_ERR_INV_ALGO;
  856. #endif
  857. }
  858. /**
  859. * @brief Encryption operation using (T)DES-ECB.
  860. * @note The function operates on data buffers whose length is a multiple
  861. * of an DES block, this means that padding must be done by the
  862. * caller.
  863. *
  864. * @param[in] cryp pointer to the @p CRYDriver object
  865. * @param[in] key_id the key to be used for the operation, zero is
  866. * the transient key, other values are keys stored
  867. * in an unspecified way
  868. * @param[in] size size of both buffers, this number must be a
  869. * multiple of 8
  870. * @param[in] in buffer containing the input plaintext
  871. * @param[out] out buffer for the output cyphertext
  872. * @return The operation status.
  873. * @retval CRY_NOERROR if the operation succeeded.
  874. * @retval CRY_ERR_INV_ALGO if the operation is unsupported on this
  875. * device instance.
  876. * @retval CRY_ERR_INV_KEY_TYPE the selected key is invalid for this operation.
  877. * @retval CRY_ERR_INV_KEY_ID if the specified key identifier is invalid
  878. * or refers to an empty key slot.
  879. * @retval CRY_ERR_OP_FAILURE if the operation failed, implementation
  880. * dependent.
  881. *
  882. * @api
  883. */
  884. cryerror_t cryEncryptDES_ECB(CRYDriver *cryp,
  885. crykey_t key_id,
  886. size_t size,
  887. const uint8_t *in,
  888. uint8_t *out) {
  889. osalDbgCheck((cryp != NULL) && (in != NULL) && (out != NULL) &&
  890. ((size & (size_t)7) == (size_t)0));
  891. osalDbgAssert(cryp->state == CRY_READY, "not ready");
  892. #if CRY_LLD_SUPPORTS_DES_ECB == TRUE
  893. return cry_lld_encrypt_DES_ECB(cryp, key_id, size, in, out);
  894. #elif HAL_CRY_USE_FALLBACK == TRUE
  895. return cry_fallback_encrypt_DES_ECB(cryp, key_id, size, in, out);
  896. #else
  897. (void)cryp;
  898. (void)key_id;
  899. (void)size;
  900. (void)in;
  901. (void)out;
  902. return CRY_ERR_INV_ALGO;
  903. #endif
  904. }
  905. /**
  906. * @brief Decryption operation using (T)DES-ECB.
  907. * @note The function operates on data buffers whose length is a multiple
  908. * of an DES block, this means that padding must be done by the
  909. * caller.
  910. *
  911. * @param[in] cryp pointer to the @p CRYDriver object
  912. * @param[in] key_id the key to be used for the operation, zero is
  913. * the transient key, other values are keys stored
  914. * in an unspecified way
  915. * @param[in] size size of both buffers, this number must be a
  916. * multiple of 8
  917. * @param[in] in buffer containing the input cyphertext
  918. * @param[out] out buffer for the output plaintext
  919. * @return The operation status.
  920. * @retval CRY_NOERROR if the operation succeeded.
  921. * @retval CRY_ERR_INV_ALGO if the operation is unsupported on this
  922. * device instance.
  923. * @retval CRY_ERR_INV_KEY_TYPE the selected key is invalid for this operation.
  924. * @retval CRY_ERR_INV_KEY_ID if the specified key identifier is invalid
  925. * or refers to an empty key slot.
  926. * @retval CRY_ERR_OP_FAILURE if the operation failed, implementation
  927. * dependent.
  928. *
  929. * @api
  930. */
  931. cryerror_t cryDecryptDES_ECB(CRYDriver *cryp,
  932. crykey_t key_id,
  933. size_t size,
  934. const uint8_t *in,
  935. uint8_t *out) {
  936. osalDbgCheck((cryp != NULL) && (in != NULL) && (out != NULL) &&
  937. ((size & (size_t)7) == (size_t)0));
  938. osalDbgAssert(cryp->state == CRY_READY, "not ready");
  939. #if CRY_LLD_SUPPORTS_DES_ECB == TRUE
  940. return cry_lld_decrypt_DES_ECB(cryp, key_id, size, in, out);
  941. #elif HAL_CRY_USE_FALLBACK == TRUE
  942. return cry_fallback_decrypt_DES_ECB(cryp, key_id, size, in, out);
  943. #else
  944. (void)cryp;
  945. (void)key_id;
  946. (void)size;
  947. (void)in;
  948. (void)out;
  949. return CRY_ERR_INV_ALGO;
  950. #endif
  951. }
  952. /**
  953. * @brief Encryption operation using (T)DES-CBC.
  954. * @note The function operates on data buffers whose length is a multiple
  955. * of an DES block, this means that padding must be done by the
  956. * caller.
  957. *
  958. * @param[in] cryp pointer to the @p CRYDriver object
  959. * @param[in] key_id the key to be used for the operation, zero is
  960. * the transient key, other values are keys stored
  961. * in an unspecified way
  962. * @param[in] size size of both buffers, this number must be a
  963. * multiple of 8
  964. * @param[in] in buffer containing the input plaintext
  965. * @param[out] out buffer for the output cyphertext
  966. * @param[in] iv 64 bits input vector
  967. * @return The operation status.
  968. * @retval CRY_NOERROR if the operation succeeded.
  969. * @retval CRY_ERR_INV_ALGO if the operation is unsupported on this
  970. * device instance.
  971. * @retval CRY_ERR_INV_KEY_TYPE the selected key is invalid for this operation.
  972. * @retval CRY_ERR_INV_KEY_ID if the specified key identifier is invalid
  973. * or refers to an empty key slot.
  974. * @retval CRY_ERR_OP_FAILURE if the operation failed, implementation
  975. * dependent.
  976. *
  977. * @api
  978. */
  979. cryerror_t cryEncryptDES_CBC(CRYDriver *cryp,
  980. crykey_t key_id,
  981. size_t size,
  982. const uint8_t *in,
  983. uint8_t *out,
  984. const uint8_t *iv) {
  985. osalDbgCheck((cryp != NULL) && (in != NULL) && (out != NULL) &&
  986. (iv != NULL) && ((size & (size_t)7) == (size_t)0));
  987. osalDbgAssert(cryp->state == CRY_READY, "not ready");
  988. #if CRY_LLD_SUPPORTS_DES_CBC == TRUE
  989. return cry_lld_encrypt_DES_CBC(cryp, key_id, size, in, out, iv);
  990. #elif HAL_CRY_USE_FALLBACK == TRUE
  991. return cry_fallback_encrypt_DES_CBC(cryp, key_id, size, in, out, iv);
  992. #else
  993. (void)cryp;
  994. (void)key_id;
  995. (void)size;
  996. (void)in;
  997. (void)out;
  998. (void)iv;
  999. return CRY_ERR_INV_ALGO;
  1000. #endif
  1001. }
  1002. /**
  1003. * @brief Decryption operation using (T)DES-CBC.
  1004. * @note The function operates on data buffers whose length is a multiple
  1005. * of an DES block, this means that padding must be done by the
  1006. * caller.
  1007. *
  1008. * @param[in] cryp pointer to the @p CRYDriver object
  1009. * @param[in] key_id the key to be used for the operation, zero is
  1010. * the transient key, other values are keys stored
  1011. * in an unspecified way
  1012. * @param[in] size size of both buffers, this number must be a
  1013. * multiple of 8
  1014. * @param[in] in buffer containing the input cyphertext
  1015. * @param[out] out buffer for the output plaintext
  1016. * @param[in] iv 64 bits input vector
  1017. * @return The operation status.
  1018. * @retval CRY_NOERROR if the operation succeeded.
  1019. * @retval CRY_ERR_INV_ALGO if the operation is unsupported on this
  1020. * device instance.
  1021. * @retval CRY_ERR_INV_KEY_TYPE the selected key is invalid for this operation.
  1022. * @retval CRY_ERR_INV_KEY_ID if the specified key identifier is invalid
  1023. * or refers to an empty key slot.
  1024. * @retval CRY_ERR_OP_FAILURE if the operation failed, implementation
  1025. * dependent.
  1026. *
  1027. * @api
  1028. */
  1029. cryerror_t cryDecryptDES_CBC(CRYDriver *cryp,
  1030. crykey_t key_id,
  1031. size_t size,
  1032. const uint8_t *in,
  1033. uint8_t *out,
  1034. const uint8_t *iv) {
  1035. osalDbgCheck((cryp != NULL) && (in != NULL) && (out != NULL) &&
  1036. (iv != NULL) && ((size & (size_t)7) == (size_t)0));
  1037. osalDbgAssert(cryp->state == CRY_READY, "not ready");
  1038. #if CRY_LLD_SUPPORTS_DES_CBC == TRUE
  1039. return cry_lld_decrypt_DES_CBC(cryp, key_id, size, in, out, iv);
  1040. #elif HAL_CRY_USE_FALLBACK == TRUE
  1041. return cry_fallback_decrypt_DES_CBC(cryp, key_id, size, in, out, iv);
  1042. #else
  1043. (void)cryp;
  1044. (void)key_id;
  1045. (void)size;
  1046. (void)in;
  1047. (void)out;
  1048. (void)iv;
  1049. return CRY_ERR_INV_ALGO;
  1050. #endif
  1051. }
  1052. /**
  1053. * @brief Hash initialization using SHA1.
  1054. * @note Use of this algorithm is not recommended because proven weak.
  1055. *
  1056. * @param[in] cryp pointer to the @p CRYDriver object
  1057. * @param[out] sha1ctxp pointer to a SHA1 context to be initialized
  1058. * @return The operation status.
  1059. * @retval CRY_NOERROR if the operation succeeded.
  1060. * @retval CRY_ERR_INV_ALGO if the operation is unsupported on this
  1061. * device instance.
  1062. * @retval CRY_ERR_OP_FAILURE if the operation failed, implementation
  1063. * dependent.
  1064. *
  1065. * @api
  1066. */
  1067. cryerror_t crySHA1Init(CRYDriver *cryp, SHA1Context *sha1ctxp) {
  1068. osalDbgCheck((cryp != NULL) && (sha1ctxp != NULL));
  1069. osalDbgAssert(cryp->state == CRY_READY, "not ready");
  1070. #if CRY_LLD_SUPPORTS_SHA1 == TRUE
  1071. return cry_lld_SHA1_init(cryp, sha1ctxp);
  1072. #elif HAL_CRY_USE_FALLBACK == TRUE
  1073. return cry_fallback_SHA1_init(cryp, sha1ctxp);
  1074. #else
  1075. (void)cryp;
  1076. (void)sha1ctxp;
  1077. return CRY_ERR_INV_ALGO;
  1078. #endif
  1079. }
  1080. /**
  1081. * @brief Hash update using SHA1.
  1082. * @note Use of this algorithm is not recommended because proven weak.
  1083. *
  1084. * @param[in] cryp pointer to the @p CRYDriver object
  1085. * @param[in] sha1ctxp pointer to a SHA1 context
  1086. * @param[in] size size of input buffer
  1087. * @param[in] in buffer containing the input text
  1088. * @return The operation status.
  1089. * @retval CRY_NOERROR if the operation succeeded.
  1090. * @retval CRY_ERR_INV_ALGO if the operation is unsupported on this
  1091. * device instance.
  1092. * @retval CRY_ERR_OP_FAILURE if the operation failed, implementation
  1093. * dependent.
  1094. *
  1095. * @api
  1096. */
  1097. cryerror_t crySHA1Update(CRYDriver *cryp, SHA1Context *sha1ctxp,
  1098. size_t size, const uint8_t *in) {
  1099. osalDbgCheck((cryp != NULL) && (sha1ctxp != NULL) && (in != NULL));
  1100. osalDbgAssert(cryp->state == CRY_READY, "not ready");
  1101. #if CRY_LLD_SUPPORTS_SHA1 == TRUE
  1102. return cry_lld_SHA1_update(cryp, sha1ctxp, size, in);
  1103. #elif HAL_CRY_USE_FALLBACK == TRUE
  1104. return cry_fallback_SHA1_update(cryp, sha1ctxp, size, in);
  1105. #else
  1106. (void)cryp;
  1107. (void)sha1ctxp;
  1108. (void)size;
  1109. (void)in;
  1110. return CRY_ERR_INV_ALGO;
  1111. #endif
  1112. }
  1113. /**
  1114. * @brief Hash finalization using SHA1.
  1115. * @note Use of this algorithm is not recommended because proven weak.
  1116. *
  1117. * @param[in] cryp pointer to the @p CRYDriver object
  1118. * @param[in] sha1ctxp pointer to a SHA1 context
  1119. * @param[out] out 160 bits output buffer
  1120. * @return The operation status.
  1121. * @retval CRY_NOERROR if the operation succeeded.
  1122. * @retval CRY_ERR_INV_ALGO if the operation is unsupported on this
  1123. * device instance.
  1124. * @retval CRY_ERR_OP_FAILURE if the operation failed, implementation
  1125. * dependent.
  1126. *
  1127. * @api
  1128. */
  1129. cryerror_t crySHA1Final(CRYDriver *cryp, SHA1Context *sha1ctxp, uint8_t *out) {
  1130. osalDbgCheck((cryp != NULL) && (sha1ctxp != NULL) && (out != NULL));
  1131. osalDbgAssert(cryp->state == CRY_READY, "not ready");
  1132. #if CRY_LLD_SUPPORTS_SHA1 == TRUE
  1133. return cry_lld_SHA1_final(cryp, sha1ctxp, out);
  1134. #elif HAL_CRY_USE_FALLBACK == TRUE
  1135. return cry_fallback_SHA1_final(cryp, sha1ctxp, out);
  1136. #else
  1137. (void)cryp;
  1138. (void)sha1ctxp;
  1139. (void)out;
  1140. return CRY_ERR_INV_ALGO;
  1141. #endif
  1142. }
  1143. /**
  1144. * @brief Hash initialization using SHA256.
  1145. *
  1146. * @param[in] cryp pointer to the @p CRYDriver object
  1147. * @param[out] sha256ctxp pointer to a SHA256 context to be initialized
  1148. * @return The operation status.
  1149. * @retval CRY_NOERROR if the operation succeeded.
  1150. * @retval CRY_ERR_INV_ALGO if the operation is unsupported on this
  1151. * device instance.
  1152. * @retval CRY_ERR_OP_FAILURE if the operation failed, implementation
  1153. * dependent.
  1154. *
  1155. * @api
  1156. */
  1157. cryerror_t crySHA256Init(CRYDriver *cryp, SHA256Context *sha256ctxp) {
  1158. osalDbgCheck((cryp != NULL) && (sha256ctxp != NULL));
  1159. osalDbgAssert(cryp->state == CRY_READY, "not ready");
  1160. #if CRY_LLD_SUPPORTS_SHA256 == TRUE
  1161. return cry_lld_SHA256_init(cryp, sha256ctxp);
  1162. #elif HAL_CRY_USE_FALLBACK == TRUE
  1163. return cry_fallback_SHA256_init(cryp, sha256ctxp);
  1164. #else
  1165. (void)cryp;
  1166. (void)sha256ctxp;
  1167. return CRY_ERR_INV_ALGO;
  1168. #endif
  1169. }
  1170. /**
  1171. * @brief Hash update using SHA256.
  1172. *
  1173. * @param[in] cryp pointer to the @p CRYDriver object
  1174. * @param[in] sha256ctxp pointer to a SHA256 context
  1175. * @param[in] size size of input buffer
  1176. * @param[in] in buffer containing the input text
  1177. * @return The operation status.
  1178. * @retval CRY_NOERROR if the operation succeeded.
  1179. * @retval CRY_ERR_INV_ALGO if the operation is unsupported on this
  1180. * device instance.
  1181. * @retval CRY_ERR_OP_FAILURE if the operation failed, implementation
  1182. * dependent.
  1183. *
  1184. * @api
  1185. */
  1186. cryerror_t crySHA256Update(CRYDriver *cryp, SHA256Context *sha256ctxp,
  1187. size_t size, const uint8_t *in) {
  1188. osalDbgCheck((cryp != NULL) && (sha256ctxp != NULL) && (in != NULL));
  1189. osalDbgAssert(cryp->state == CRY_READY, "not ready");
  1190. #if CRY_LLD_SUPPORTS_SHA256 == TRUE
  1191. return cry_lld_SHA256_update(cryp, sha256ctxp, size, in);
  1192. #elif HAL_CRY_USE_FALLBACK == TRUE
  1193. return cry_fallback_SHA256_update(cryp, sha256ctxp, size, in);
  1194. #else
  1195. (void)cryp;
  1196. (void)sha256ctxp;
  1197. (void)size;
  1198. (void)in;
  1199. return CRY_ERR_INV_ALGO;
  1200. #endif
  1201. }
  1202. /**
  1203. * @brief Hash finalization using SHA256.
  1204. *
  1205. * @param[in] cryp pointer to the @p CRYDriver object
  1206. * @param[in] sha256ctxp pointer to a SHA256 context
  1207. * @param[out] out 256 bits output buffer
  1208. * @return The operation status.
  1209. * @retval CRY_NOERROR if the operation succeeded.
  1210. * @retval CRY_ERR_INV_ALGO if the operation is unsupported on this
  1211. * device instance.
  1212. * @retval CRY_ERR_OP_FAILURE if the operation failed, implementation
  1213. * dependent.
  1214. *
  1215. * @api
  1216. */
  1217. cryerror_t crySHA256Final(CRYDriver *cryp, SHA256Context *sha256ctxp,
  1218. uint8_t *out) {
  1219. osalDbgCheck((cryp != NULL) && (sha256ctxp != NULL) && (out != NULL));
  1220. osalDbgAssert(cryp->state == CRY_READY, "not ready");
  1221. #if CRY_LLD_SUPPORTS_SHA256 == TRUE
  1222. return cry_lld_SHA256_final(cryp, sha256ctxp, out);
  1223. #elif HAL_CRY_USE_FALLBACK == TRUE
  1224. return cry_fallback_SHA256_final(cryp, sha256ctxp, out);
  1225. #else
  1226. (void)cryp;
  1227. (void)sha256ctxp;
  1228. (void)out;
  1229. return CRY_ERR_INV_ALGO;
  1230. #endif
  1231. }
  1232. /**
  1233. * @brief Hash initialization using SHA512.
  1234. *
  1235. * @param[in] cryp pointer to the @p CRYDriver object
  1236. * @param[out] sha512ctxp pointer to a SHA512 context to be initialized
  1237. * @return The operation status.
  1238. * @retval CRY_NOERROR if the operation succeeded.
  1239. * @retval CRY_ERR_INV_ALGO if the operation is unsupported on this
  1240. * device instance.
  1241. * @retval CRY_ERR_OP_FAILURE if the operation failed, implementation
  1242. * dependent.
  1243. *
  1244. * @api
  1245. */
  1246. cryerror_t crySHA512Init(CRYDriver *cryp, SHA512Context *sha512ctxp) {
  1247. osalDbgCheck((cryp != NULL) && (sha512ctxp != NULL));
  1248. osalDbgAssert(cryp->state == CRY_READY, "not ready");
  1249. #if CRY_LLD_SUPPORTS_SHA512 == TRUE
  1250. return cry_lld_SHA512_init(cryp, sha512ctxp);
  1251. #elif HAL_CRY_USE_FALLBACK == TRUE
  1252. return cry_fallback_SHA512_init(cryp, sha512ctxp);
  1253. #else
  1254. (void)cryp;
  1255. (void)sha512ctxp;
  1256. return CRY_ERR_INV_ALGO;
  1257. #endif
  1258. }
  1259. /**
  1260. * @brief Hash update using SHA512.
  1261. *
  1262. * @param[in] cryp pointer to the @p CRYDriver object
  1263. * @param[in] sha512ctxp pointer to a SHA512 context
  1264. * @param[in] size size of input buffer
  1265. * @param[in] in buffer containing the input text
  1266. * @return The operation status.
  1267. * @retval CRY_NOERROR if the operation succeeded.
  1268. * @retval CRY_ERR_INV_ALGO if the operation is unsupported on this
  1269. * device instance.
  1270. * @retval CRY_ERR_OP_FAILURE if the operation failed, implementation
  1271. * dependent.
  1272. *
  1273. * @api
  1274. */
  1275. cryerror_t crySHA512Update(CRYDriver *cryp, SHA512Context *sha512ctxp,
  1276. size_t size, const uint8_t *in) {
  1277. osalDbgCheck((cryp != NULL) && (sha512ctxp != NULL) && (in != NULL));
  1278. osalDbgAssert(cryp->state == CRY_READY, "not ready");
  1279. #if CRY_LLD_SUPPORTS_SHA512 == TRUE
  1280. return cry_lld_SHA512_update(cryp, sha512ctxp, size, in);
  1281. #elif HAL_CRY_USE_FALLBACK == TRUE
  1282. return cry_fallback_SHA512_update(cryp, sha512ctxp, size, in);
  1283. #else
  1284. (void)cryp;
  1285. (void)sha512ctxp;
  1286. (void)size;
  1287. (void)in;
  1288. return CRY_ERR_INV_ALGO;
  1289. #endif
  1290. }
  1291. /**
  1292. * @brief Hash finalization using SHA512.
  1293. *
  1294. * @param[in] cryp pointer to the @p CRYDriver object
  1295. * @param[in] sha512ctxp pointer to a SHA512 context
  1296. * @param[out] out 512 bits output buffer
  1297. * @return The operation status.
  1298. * @retval CRY_NOERROR if the operation succeeded.
  1299. * @retval CRY_ERR_INV_ALGO if the operation is unsupported on this
  1300. * device instance.
  1301. * @retval CRY_ERR_OP_FAILURE if the operation failed, implementation
  1302. * dependent.
  1303. *
  1304. * @api
  1305. */
  1306. cryerror_t crySHA512Final(CRYDriver *cryp, SHA512Context *sha512ctxp,
  1307. uint8_t *out) {
  1308. osalDbgCheck((cryp != NULL) && (sha512ctxp != NULL) && (out != NULL));
  1309. osalDbgAssert(cryp->state == CRY_READY, "not ready");
  1310. #if CRY_LLD_SUPPORTS_SHA512 == TRUE
  1311. return cry_lld_SHA512_final(cryp, sha512ctxp, out);
  1312. #elif HAL_CRY_USE_FALLBACK == TRUE
  1313. return cry_fallback_SHA512_final(cryp, sha512ctxp, out);
  1314. #else
  1315. (void)cryp;
  1316. (void)sha512ctxp;
  1317. (void)out;
  1318. return CRY_ERR_INV_ALGO;
  1319. #endif
  1320. }
  1321. /**
  1322. * @brief Initializes the HMAC transient key.
  1323. * @note It is the underlying implementation to decide which key sizes are
  1324. * allowable.
  1325. *
  1326. * @param[in] cryp pointer to the @p CRYDriver object
  1327. * @param[in] size key size in bytes
  1328. * @param[in] keyp pointer to the key data
  1329. * @return The operation status.
  1330. * @retval CRY_NOERROR if the operation succeeded.
  1331. * @retval CRY_ERR_INV_ALGO if the algorithm is unsupported.
  1332. * @retval CRY_ERR_INV_KEY_SIZE if the specified key size is invalid for
  1333. * the specified algorithm.
  1334. *
  1335. * @api
  1336. */
  1337. cryerror_t cryLoadHMACTransientKey(CRYDriver *cryp,
  1338. size_t size,
  1339. const uint8_t *keyp) {
  1340. osalDbgCheck((cryp != NULL) && (keyp != NULL));
  1341. #if (CRY_LLD_SUPPORTS_HMAC_SHA256 == TRUE) || \
  1342. (CRY_LLD_SUPPORTS_HMAC_SHA512 == TRUE)
  1343. return cry_lld_hmac_loadkey(cryp, size, keyp);
  1344. #elif HAL_CRY_USE_FALLBACK == TRUE
  1345. return cry_fallback_hmac_loadkey(cryp, size, keyp);
  1346. #else
  1347. (void)cryp;
  1348. (void)size;
  1349. (void)keyp;
  1350. return CRY_ERR_INV_ALGO;
  1351. #endif
  1352. }
  1353. /**
  1354. * @brief Hash initialization using HMAC_SHA256.
  1355. *
  1356. * @param[in] cryp pointer to the @p CRYDriver object
  1357. * @param[out] hmacsha256ctxp pointer to a HMAC_SHA256 context to be
  1358. * initialized
  1359. * @return The operation status.
  1360. * @retval CRY_NOERROR if the operation succeeded.
  1361. * @retval CRY_ERR_INV_ALGO if the operation is unsupported on this
  1362. * device instance.
  1363. * @retval CRY_ERR_OP_FAILURE if the operation failed, implementation
  1364. * dependent.
  1365. *
  1366. * @api
  1367. */
  1368. cryerror_t cryHMACSHA256Init(CRYDriver *cryp,
  1369. HMACSHA256Context *hmacsha256ctxp) {
  1370. osalDbgCheck((cryp != NULL) && (hmacsha256ctxp != NULL));
  1371. osalDbgAssert(cryp->state == CRY_READY, "not ready");
  1372. #if CRY_LLD_SUPPORTS_HMAC_SHA256 == TRUE
  1373. return cry_lld_HMACSHA256_init(cryp, hmacsha256ctxp);
  1374. #elif HAL_CRY_USE_FALLBACK == TRUE
  1375. return cry_fallback_HMACSHA256_init(cryp, hmacsha256ctxp);
  1376. #else
  1377. (void)cryp;
  1378. (void)hmacsha256ctxp;
  1379. return CRY_ERR_INV_ALGO;
  1380. #endif
  1381. }
  1382. /**
  1383. * @brief Hash update using HMAC.
  1384. *
  1385. * @param[in] cryp pointer to the @p CRYDriver object
  1386. * @param[in] hmacsha256ctxp pointer to a HMAC_SHA256 context
  1387. * @param[in] size size of input buffer
  1388. * @param[in] in buffer containing the input text
  1389. * @return The operation status.
  1390. * @retval CRY_NOERROR if the operation succeeded.
  1391. * @retval CRY_ERR_INV_ALGO if the operation is unsupported on this
  1392. * device instance.
  1393. * @retval CRY_ERR_OP_FAILURE if the operation failed, implementation
  1394. * dependent.
  1395. *
  1396. * @api
  1397. */
  1398. cryerror_t cryHMACSHA256Update(CRYDriver *cryp,
  1399. HMACSHA256Context *hmacsha256ctxp,
  1400. size_t size,
  1401. const uint8_t *in) {
  1402. osalDbgCheck((cryp != NULL) && (hmacsha256ctxp != NULL) && (in != NULL));
  1403. osalDbgAssert(cryp->state == CRY_READY, "not ready");
  1404. #if CRY_LLD_SUPPORTS_HMAC_SHA256 == TRUE
  1405. return cry_lld_HMACSHA256_update(cryp, hmacsha256ctxp, size, in);
  1406. #elif HAL_CRY_USE_FALLBACK == TRUE
  1407. return cry_fallback_HMACSHA256_update(cryp, hmacsha256ctxp, size, in);
  1408. #else
  1409. (void)cryp;
  1410. (void)hmacsha256ctxp;
  1411. (void)size;
  1412. (void)in;
  1413. return CRY_ERR_INV_ALGO;
  1414. #endif
  1415. }
  1416. /**
  1417. * @brief Hash finalization using HMAC.
  1418. *
  1419. * @param[in] cryp pointer to the @p CRYDriver object
  1420. * @param[in] hmacsha256ctxp pointer to a HMAC_SHA256 context
  1421. * @param[out] out 256 bits output buffer
  1422. * @return The operation status.
  1423. * @retval CRY_NOERROR if the operation succeeded.
  1424. * @retval CRY_ERR_INV_ALGO if the operation is unsupported on this
  1425. * device instance.
  1426. * @retval CRY_ERR_OP_FAILURE if the operation failed, implementation
  1427. * dependent.
  1428. *
  1429. * @api
  1430. */
  1431. cryerror_t cryHMACSHA256Final(CRYDriver *cryp,
  1432. HMACSHA256Context *hmacsha256ctxp,
  1433. uint8_t *out) {
  1434. osalDbgCheck((cryp != NULL) && (hmacsha256ctxp != NULL) && (out != NULL));
  1435. osalDbgAssert(cryp->state == CRY_READY, "not ready");
  1436. #if CRY_LLD_SUPPORTS_HMAC_SHA256 == TRUE
  1437. return cry_lld_HMACSHA256_final(cryp, hmacsha256ctxp, out);
  1438. #elif HAL_CRY_USE_FALLBACK == TRUE
  1439. return cry_fallback_HMACSHA256_final(cryp, hmacsha256ctxp, out);
  1440. #else
  1441. (void)cryp;
  1442. (void)hmacsha256ctxp;
  1443. (void)out;
  1444. return CRY_ERR_INV_ALGO;
  1445. #endif
  1446. }
  1447. /**
  1448. * @brief Hash initialization using HMAC_SHA512.
  1449. *
  1450. * @param[in] cryp pointer to the @p CRYDriver object
  1451. * @param[out] hmacsha512ctxp pointer to a HMAC_SHA512 context to be
  1452. * initialized
  1453. * @return The operation status.
  1454. * @retval CRY_NOERROR if the operation succeeded.
  1455. * @retval CRY_ERR_INV_ALGO if the operation is unsupported on this
  1456. * device instance.
  1457. * @retval CRY_ERR_OP_FAILURE if the operation failed, implementation
  1458. * dependent.
  1459. *
  1460. * @api
  1461. */
  1462. cryerror_t cryHMACSHA512Init(CRYDriver *cryp,
  1463. HMACSHA512Context *hmacsha512ctxp) {
  1464. osalDbgCheck((cryp != NULL) && (hmacsha512ctxp != NULL));
  1465. osalDbgAssert(cryp->state == CRY_READY, "not ready");
  1466. #if CRY_LLD_SUPPORTS_HMAC_SHA512 == TRUE
  1467. return cry_lld_HMACSHA512_init(cryp, hmacsha512ctxp);
  1468. #elif HAL_CRY_USE_FALLBACK == TRUE
  1469. return cry_fallback_HMACSHA512_init(cryp, hmacsha512ctxp);
  1470. #else
  1471. (void)cryp;
  1472. (void)hmacsha512ctxp;
  1473. return CRY_ERR_INV_ALGO;
  1474. #endif
  1475. }
  1476. /**
  1477. * @brief Hash update using HMAC.
  1478. *
  1479. * @param[in] cryp pointer to the @p CRYDriver object
  1480. * @param[in] hmacsha512ctxp pointer to a HMAC_SHA512 context
  1481. * @param[in] size size of input buffer
  1482. * @param[in] in buffer containing the input text
  1483. * @return The operation status.
  1484. * @retval CRY_NOERROR if the operation succeeded.
  1485. * @retval CRY_ERR_INV_ALGO if the operation is unsupported on this
  1486. * device instance.
  1487. * @retval CRY_ERR_OP_FAILURE if the operation failed, implementation
  1488. * dependent.
  1489. *
  1490. * @api
  1491. */
  1492. cryerror_t cryHMACSHA512Update(CRYDriver *cryp,
  1493. HMACSHA512Context *hmacsha512ctxp,
  1494. size_t size,
  1495. const uint8_t *in) {
  1496. osalDbgCheck((cryp != NULL) && (hmacsha512ctxp != NULL) && (in != NULL));
  1497. osalDbgAssert(cryp->state == CRY_READY, "not ready");
  1498. #if CRY_LLD_SUPPORTS_HMAC_SHA512 == TRUE
  1499. return cry_lld_HMACSHA512_update(cryp, hmacsha512ctxp, size, in);
  1500. #elif HAL_CRY_USE_FALLBACK == TRUE
  1501. return cry_fallback_HMACSHA512_update(cryp, hmacsha512ctxp, size, in);
  1502. #else
  1503. (void)cryp;
  1504. (void)hmacsha512ctxp;
  1505. (void)size;
  1506. (void)in;
  1507. return CRY_ERR_INV_ALGO;
  1508. #endif
  1509. }
  1510. /**
  1511. * @brief Hash finalization using HMAC.
  1512. *
  1513. * @param[in] cryp pointer to the @p CRYDriver object
  1514. * @param[in] hmacsha512ctxp pointer to a HMAC_SHA512 context
  1515. * @param[out] out 512 bits output buffer
  1516. * @return The operation status.
  1517. * @retval CRY_NOERROR if the operation succeeded.
  1518. * @retval CRY_ERR_INV_ALGO if the operation is unsupported on this
  1519. * device instance.
  1520. * @retval CRY_ERR_OP_FAILURE if the operation failed, implementation
  1521. * dependent.
  1522. *
  1523. * @api
  1524. */
  1525. cryerror_t cryHMACSHA512Final(CRYDriver *cryp,
  1526. HMACSHA512Context *hmacsha512ctxp,
  1527. uint8_t *out) {
  1528. osalDbgCheck((cryp != NULL) && (hmacsha512ctxp != NULL) && (out != NULL));
  1529. osalDbgAssert(cryp->state == CRY_READY, "not ready");
  1530. #if CRY_LLD_SUPPORTS_HMAC_SHA512 == TRUE
  1531. return cry_lld_HMACSHA512_final(cryp, hmacsha512ctxp, out);
  1532. #elif HAL_CRY_USE_FALLBACK == TRUE
  1533. return cry_fallback_HMACSHA512_final(cryp, hmacsha512ctxp, out);
  1534. #else
  1535. (void)cryp;
  1536. (void)hmacsha512ctxp;
  1537. (void)out;
  1538. return CRY_ERR_INV_ALGO;
  1539. #endif
  1540. }
  1541. #endif /* HAL_USE_CRY == TRUE */
  1542. /** @} */