sha256_alt.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. /*
  2. * Copyright (c) 2023 HPMicro
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. *
  6. */
  7. #include "sha256_alt.h"
  8. #if defined(MBEDTLS_SHA256_C)
  9. #include "hpm_l1c_drv.h"
  10. #include "mbedtls/platform_util.h"
  11. #include "mbedtls/sha256.h"
  12. #include <string.h>
  13. #if defined(MBEDTLS_PADLOCK_C)
  14. #include "mbedtls/padlock.h"
  15. #endif
  16. #if defined(MBEDTLS_AESNI_C)
  17. #include "mbedtls/aesni.h"
  18. #endif
  19. #if defined(MBEDTLS_THREADING_C)
  20. #include "ksdk_mbedtls.h"
  21. #include "mbedtls/threading.h"
  22. #endif
  23. #define SHA256_VALIDATE_RET(cond) \
  24. MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_SHA256_BAD_INPUT_DATA )
  25. #define SHA256_VALIDATE(cond) MBEDTLS_INTERNAL_VALIDATE( cond )
  26. /*
  27. * 32-bit integer manipulation macros (big endian)
  28. */
  29. #ifndef GET_UINT32_BE
  30. #define GET_UINT32_BE(n,b,i) \
  31. do { \
  32. (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
  33. | ( (uint32_t) (b)[(i) + 1] << 16 ) \
  34. | ( (uint32_t) (b)[(i) + 2] << 8 ) \
  35. | ( (uint32_t) (b)[(i) + 3] ); \
  36. } while( 0 )
  37. #endif
  38. #ifndef PUT_UINT32_BE
  39. #define PUT_UINT32_BE(n,b,i) \
  40. do { \
  41. (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
  42. (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
  43. (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
  44. (b)[(i) + 3] = (unsigned char) ( (n) ); \
  45. } while( 0 )
  46. #endif
  47. /******************************************************************************/
  48. /*************************** SHA256********************************************/
  49. /******************************************************************************/
  50. #if defined(MBEDTLS_HPM_SDP_SHA256)
  51. #include "mbedtls/sha256.h"
  52. #include "board.h"
  53. #ifdef MBEDTLS_HPM_SHA224_ALT_SW
  54. int mbedtls_internal_sha224_process( mbedtls_sha256_context *ctx,
  55. const unsigned char data[64] );
  56. int mbedtls_sha224_update_ret( mbedtls_sha256_context *ctx,
  57. const unsigned char *input,
  58. size_t ilen );
  59. int mbedtls_sha224_finish_ret( mbedtls_sha256_context *ctx,
  60. unsigned char output[32] );
  61. #endif /* MBEDTLS_HPM_SHA224_ALT_SW */
  62. void mbedtls_sha256_init(mbedtls_sha256_context *ctx)
  63. {
  64. sdp_hash_ctx_t *p_sys_sdp_ctx = (sdp_hash_ctx_t *)core_local_mem_to_sys_address(BOARD_RUNNING_CORE, (uint32_t)&s_hash_ctx);
  65. memset(ctx, 0, sizeof(mbedtls_sha256_context));
  66. hpm_sdp_api_init();
  67. (void)rom_sdp_hash_init(p_sys_sdp_ctx, sdp_hash_alg_sha256);
  68. }
  69. void mbedtls_sha256_free(mbedtls_sha256_context *ctx)
  70. {
  71. if (ctx == NULL)
  72. return;
  73. mbedtls_zeroize(ctx, sizeof(mbedtls_sha256_context));
  74. }
  75. void mbedtls_sha256_clone(mbedtls_sha256_context *dst, const mbedtls_sha256_context *src)
  76. {
  77. memcpy(dst, src, sizeof(*dst));
  78. }
  79. /*
  80. * SHA-256 context setup
  81. */
  82. int mbedtls_sha256_starts_ret(mbedtls_sha256_context *ctx, int is224)
  83. {
  84. hpm_stat_t ret = status_fail;
  85. if (is224)
  86. {
  87. #ifdef MBEDTLS_HPM_SHA224_ALT_SW
  88. /* SHA-224 */
  89. ctx->state[0] = 0xC1059ED8;
  90. ctx->state[1] = 0x367CD507;
  91. ctx->state[2] = 0x3070DD17;
  92. ctx->state[3] = 0xF70E5939;
  93. ctx->state[4] = 0xFFC00B31;
  94. ctx->state[5] = 0x68581511;
  95. ctx->state[6] = 0x64F98FA7;
  96. ctx->state[7] = 0xBEFA4FA4;
  97. #else
  98. return MBEDTLS_ERROR_HW_NOT_SUPPORTED;
  99. #endif
  100. }
  101. else
  102. {
  103. sdp_hash_ctx_t *p_sys_sdp_ctx = (sdp_hash_ctx_t *)core_local_mem_to_sys_address(BOARD_RUNNING_CORE, (uint32_t)&s_hash_ctx);
  104. ret = rom_sdp_hash_init(p_sys_sdp_ctx, sdp_hash_alg_sha256);
  105. if (ret != status_success)
  106. {
  107. return MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED;
  108. }
  109. }
  110. ctx->is224 = is224;
  111. return 0;
  112. }
  113. int mbedtls_internal_sha256_process(mbedtls_sha256_context *ctx, const unsigned char data[64])
  114. {
  115. static uint8_t ATTR_PLACE_AT_NONCACHEABLE local[64];
  116. hpm_stat_t ret = status_fail;
  117. if (ctx->is224)
  118. {
  119. #ifdef MBEDTLS_HPM_SHA224_ALT_SW
  120. return mbedtls_internal_sha224_process(ctx, data);
  121. #else
  122. return MBEDTLS_ERROR_HW_NOT_SUPPORTED;
  123. #endif
  124. }
  125. sdp_hash_ctx_t *p_sys_sdp_ctx = (sdp_hash_ctx_t *)core_local_mem_to_sys_address(BOARD_RUNNING_CORE, (uint32_t)&s_hash_ctx);
  126. memcpy(local, data, 64);
  127. ret = rom_sdp_hash_update(p_sys_sdp_ctx, local, 64);
  128. if (ret != status_success)
  129. {
  130. return MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED;
  131. }
  132. return 0;
  133. }
  134. /*
  135. * SHA-256 process buffer
  136. */
  137. int mbedtls_sha256_update_ret(mbedtls_sha256_context *ctx, const unsigned char *input, size_t ilen)
  138. {
  139. static uint8_t ATTR_PLACE_AT_NONCACHEABLE local[64];
  140. hpm_stat_t ret = status_fail;
  141. if (ctx->is224)
  142. {
  143. #ifdef MBEDTLS_HPM_SHA224_ALT_SW
  144. return mbedtls_sha224_update_ret(ctx, input, ilen);
  145. #else
  146. return MBEDTLS_ERROR_HW_NOT_SUPPORTED;
  147. #endif
  148. }
  149. for (uint32_t i = 0; i < ilen; i += 64)
  150. {
  151. size_t len = ilen - i > 64 ? 64 : ilen - i;
  152. if (len < 64)
  153. {
  154. sdp_hash_ctx_t *p_sys_sdp_ctx = (sdp_hash_ctx_t *)core_local_mem_to_sys_address(BOARD_RUNNING_CORE, (uint32_t)&s_hash_ctx);
  155. memcpy(local, input + i, len);
  156. ret = rom_sdp_hash_update(p_sys_sdp_ctx, local, len);
  157. if (ret != status_success)
  158. {
  159. return MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED;
  160. }
  161. return 0;
  162. }
  163. else
  164. {
  165. ret = mbedtls_internal_sha256_process(ctx, input + i);
  166. if (ret != status_success)
  167. {
  168. return MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED;
  169. }
  170. }
  171. }
  172. return 0;
  173. }
  174. /*
  175. * SHA-256 final digest
  176. */
  177. int mbedtls_sha256_finish_ret(mbedtls_sha256_context *ctx, unsigned char output[32])
  178. {
  179. static uint8_t ATTR_PLACE_AT_NONCACHEABLE local[HPM_L1C_CACHELINE_SIZE];
  180. hpm_stat_t ret = status_fail;
  181. if (ctx->is224)
  182. {
  183. #ifdef MBEDTLS_HPM_SHA224_ALT_SW
  184. return mbedtls_sha224_finish_ret(ctx, output);
  185. #else
  186. return MBEDTLS_ERROR_HW_NOT_SUPPORTED;
  187. #endif
  188. }
  189. sdp_hash_ctx_t *p_sys_sdp_ctx = (sdp_hash_ctx_t *)core_local_mem_to_sys_address(BOARD_RUNNING_CORE, (uint32_t)&s_hash_ctx);
  190. uint8_t *pout = (uint8_t *)core_local_mem_to_sys_address(BOARD_RUNNING_CORE, (uint32_t)local);
  191. ret = rom_sdp_hash_finish(p_sys_sdp_ctx, pout);
  192. if (ret != status_success)
  193. {
  194. return MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED;
  195. }
  196. memcpy(output, pout, 32);
  197. return 0;
  198. }
  199. #ifdef MBEDTLS_HPM_SHA224_ALT_SW
  200. static const uint32_t K[] =
  201. {
  202. 0x428A2F98, 0x71374491, 0xB5C0FBCF, 0xE9B5DBA5,
  203. 0x3956C25B, 0x59F111F1, 0x923F82A4, 0xAB1C5ED5,
  204. 0xD807AA98, 0x12835B01, 0x243185BE, 0x550C7DC3,
  205. 0x72BE5D74, 0x80DEB1FE, 0x9BDC06A7, 0xC19BF174,
  206. 0xE49B69C1, 0xEFBE4786, 0x0FC19DC6, 0x240CA1CC,
  207. 0x2DE92C6F, 0x4A7484AA, 0x5CB0A9DC, 0x76F988DA,
  208. 0x983E5152, 0xA831C66D, 0xB00327C8, 0xBF597FC7,
  209. 0xC6E00BF3, 0xD5A79147, 0x06CA6351, 0x14292967,
  210. 0x27B70A85, 0x2E1B2138, 0x4D2C6DFC, 0x53380D13,
  211. 0x650A7354, 0x766A0ABB, 0x81C2C92E, 0x92722C85,
  212. 0xA2BFE8A1, 0xA81A664B, 0xC24B8B70, 0xC76C51A3,
  213. 0xD192E819, 0xD6990624, 0xF40E3585, 0x106AA070,
  214. 0x19A4C116, 0x1E376C08, 0x2748774C, 0x34B0BCB5,
  215. 0x391C0CB3, 0x4ED8AA4A, 0x5B9CCA4F, 0x682E6FF3,
  216. 0x748F82EE, 0x78A5636F, 0x84C87814, 0x8CC70208,
  217. 0x90BEFFFA, 0xA4506CEB, 0xBEF9A3F7, 0xC67178F2,
  218. };
  219. #define SHR(x,n) (((x) & 0xFFFFFFFF) >> (n))
  220. #define ROTR(x,n) (SHR(x,n) | ((x) << (32 - (n))))
  221. #define S0(x) (ROTR(x, 7) ^ ROTR(x,18) ^ SHR(x, 3))
  222. #define S1(x) (ROTR(x,17) ^ ROTR(x,19) ^ SHR(x,10))
  223. #define S2(x) (ROTR(x, 2) ^ ROTR(x,13) ^ ROTR(x,22))
  224. #define S3(x) (ROTR(x, 6) ^ ROTR(x,11) ^ ROTR(x,25))
  225. #define F0(x,y,z) (((x) & (y)) | ((z) & ((x) | (y))))
  226. #define F1(x,y,z) ((z) ^ ((x) & ((y) ^ (z))))
  227. #define R(t) \
  228. ( \
  229. local.W[t] = S1(local.W[(t) - 2]) + local.W[(t) - 7] + \
  230. S0(local.W[(t) - 15]) + local.W[(t) - 16] \
  231. )
  232. #define P(a,b,c,d,e,f,g,h,x,K) \
  233. do \
  234. { \
  235. local.temp1 = (h) + S3(e) + F1((e),(f),(g)) + (K) + (x); \
  236. local.temp2 = S2(a) + F0((a),(b),(c)); \
  237. (d) += local.temp1; (h) = local.temp1 + local.temp2; \
  238. } while( 0 )
  239. int mbedtls_internal_sha224_process( mbedtls_sha256_context *ctx,
  240. const unsigned char data[64] )
  241. {
  242. struct
  243. {
  244. uint32_t temp1, temp2, W[64];
  245. uint32_t A[8];
  246. } local;
  247. unsigned int i;
  248. SHA256_VALIDATE_RET( ctx != NULL );
  249. SHA256_VALIDATE_RET( (const unsigned char *)data != NULL );
  250. for( i = 0; i < 8; i++ )
  251. local.A[i] = ctx->state[i];
  252. #if defined(MBEDTLS_SHA256_SMALLER)
  253. for( i = 0; i < 64; i++ )
  254. {
  255. if( i < 16 )
  256. GET_UINT32_BE( local.W[i], data, 4 * i );
  257. else
  258. R( i );
  259. P( local.A[0], local.A[1], local.A[2], local.A[3], local.A[4],
  260. local.A[5], local.A[6], local.A[7], local.W[i], K[i] );
  261. local.temp1 = local.A[7]; local.A[7] = local.A[6];
  262. local.A[6] = local.A[5]; local.A[5] = local.A[4];
  263. local.A[4] = local.A[3]; local.A[3] = local.A[2];
  264. local.A[2] = local.A[1]; local.A[1] = local.A[0];
  265. local.A[0] = local.temp1;
  266. }
  267. #else /* MBEDTLS_SHA256_SMALLER */
  268. for( i = 0; i < 16; i++ )
  269. GET_UINT32_BE( local.W[i], data, 4 * i );
  270. for( i = 0; i < 16; i += 8 )
  271. {
  272. P( local.A[0], local.A[1], local.A[2], local.A[3], local.A[4],
  273. local.A[5], local.A[6], local.A[7], local.W[i+0], K[i+0] );
  274. P( local.A[7], local.A[0], local.A[1], local.A[2], local.A[3],
  275. local.A[4], local.A[5], local.A[6], local.W[i+1], K[i+1] );
  276. P( local.A[6], local.A[7], local.A[0], local.A[1], local.A[2],
  277. local.A[3], local.A[4], local.A[5], local.W[i+2], K[i+2] );
  278. P( local.A[5], local.A[6], local.A[7], local.A[0], local.A[1],
  279. local.A[2], local.A[3], local.A[4], local.W[i+3], K[i+3] );
  280. P( local.A[4], local.A[5], local.A[6], local.A[7], local.A[0],
  281. local.A[1], local.A[2], local.A[3], local.W[i+4], K[i+4] );
  282. P( local.A[3], local.A[4], local.A[5], local.A[6], local.A[7],
  283. local.A[0], local.A[1], local.A[2], local.W[i+5], K[i+5] );
  284. P( local.A[2], local.A[3], local.A[4], local.A[5], local.A[6],
  285. local.A[7], local.A[0], local.A[1], local.W[i+6], K[i+6] );
  286. P( local.A[1], local.A[2], local.A[3], local.A[4], local.A[5],
  287. local.A[6], local.A[7], local.A[0], local.W[i+7], K[i+7] );
  288. }
  289. for( i = 16; i < 64; i += 8 )
  290. {
  291. P( local.A[0], local.A[1], local.A[2], local.A[3], local.A[4],
  292. local.A[5], local.A[6], local.A[7], R(i+0), K[i+0] );
  293. P( local.A[7], local.A[0], local.A[1], local.A[2], local.A[3],
  294. local.A[4], local.A[5], local.A[6], R(i+1), K[i+1] );
  295. P( local.A[6], local.A[7], local.A[0], local.A[1], local.A[2],
  296. local.A[3], local.A[4], local.A[5], R(i+2), K[i+2] );
  297. P( local.A[5], local.A[6], local.A[7], local.A[0], local.A[1],
  298. local.A[2], local.A[3], local.A[4], R(i+3), K[i+3] );
  299. P( local.A[4], local.A[5], local.A[6], local.A[7], local.A[0],
  300. local.A[1], local.A[2], local.A[3], R(i+4), K[i+4] );
  301. P( local.A[3], local.A[4], local.A[5], local.A[6], local.A[7],
  302. local.A[0], local.A[1], local.A[2], R(i+5), K[i+5] );
  303. P( local.A[2], local.A[3], local.A[4], local.A[5], local.A[6],
  304. local.A[7], local.A[0], local.A[1], R(i+6), K[i+6] );
  305. P( local.A[1], local.A[2], local.A[3], local.A[4], local.A[5],
  306. local.A[6], local.A[7], local.A[0], R(i+7), K[i+7] );
  307. }
  308. #endif /* MBEDTLS_SHA256_SMALLER */
  309. for( i = 0; i < 8; i++ )
  310. ctx->state[i] += local.A[i];
  311. /* Zeroise buffers and variables to clear sensitive data from memory. */
  312. mbedtls_platform_zeroize( &local, sizeof( local ) );
  313. return( 0 );
  314. }
  315. /*
  316. * SHA-256 process buffer
  317. */
  318. int mbedtls_sha224_update_ret( mbedtls_sha256_context *ctx,
  319. const unsigned char *input,
  320. size_t ilen )
  321. {
  322. int ret = MBEDTLS_ERROR_DEFAULT;
  323. size_t fill;
  324. uint32_t left;
  325. SHA256_VALIDATE_RET( ctx != NULL );
  326. SHA256_VALIDATE_RET( ilen == 0 || input != NULL );
  327. if( ilen == 0 )
  328. return( 0 );
  329. left = ctx->total[0] & 0x3F;
  330. fill = 64 - left;
  331. ctx->total[0] += (uint32_t) ilen;
  332. ctx->total[0] &= 0xFFFFFFFF;
  333. if( ctx->total[0] < (uint32_t) ilen )
  334. ctx->total[1]++;
  335. if( left && ilen >= fill )
  336. {
  337. memcpy( (void *) (ctx->buffer + left), input, fill );
  338. if( ( ret = mbedtls_internal_sha256_process( ctx, ctx->buffer ) ) != 0 )
  339. return( ret );
  340. input += fill;
  341. ilen -= fill;
  342. left = 0;
  343. }
  344. while( ilen >= 64 )
  345. {
  346. if( ( ret = mbedtls_internal_sha256_process( ctx, input ) ) != 0 )
  347. return( ret );
  348. input += 64;
  349. ilen -= 64;
  350. }
  351. if( ilen > 0 )
  352. memcpy( (void *) (ctx->buffer + left), input, ilen );
  353. return( 0 );
  354. }
  355. /*
  356. * SHA-256 final digest
  357. */
  358. int mbedtls_sha224_finish_ret( mbedtls_sha256_context *ctx,
  359. unsigned char output[32] )
  360. {
  361. int ret = MBEDTLS_ERROR_DEFAULT;
  362. uint32_t used;
  363. uint32_t high, low;
  364. SHA256_VALIDATE_RET( ctx != NULL );
  365. SHA256_VALIDATE_RET( (unsigned char *)output != NULL );
  366. /*
  367. * Add padding: 0x80 then 0x00 until 8 bytes remain for the length
  368. */
  369. used = ctx->total[0] & 0x3F;
  370. ctx->buffer[used++] = 0x80;
  371. if( used <= 56 )
  372. {
  373. /* Enough room for padding + length in current block */
  374. memset( ctx->buffer + used, 0, 56 - used );
  375. }
  376. else
  377. {
  378. /* We'll need an extra block */
  379. memset( ctx->buffer + used, 0, 64 - used );
  380. if( ( ret = mbedtls_internal_sha256_process( ctx, ctx->buffer ) ) != 0 )
  381. return( ret );
  382. memset( ctx->buffer, 0, 56 );
  383. }
  384. /*
  385. * Add message length
  386. */
  387. high = ( ctx->total[0] >> 29 )
  388. | ( ctx->total[1] << 3 );
  389. low = ( ctx->total[0] << 3 );
  390. PUT_UINT32_BE( high, ctx->buffer, 56 );
  391. PUT_UINT32_BE( low, ctx->buffer, 60 );
  392. if( ( ret = mbedtls_internal_sha256_process( ctx, ctx->buffer ) ) != 0 )
  393. return( ret );
  394. /*
  395. * Output final state
  396. */
  397. PUT_UINT32_BE( ctx->state[0], output, 0 );
  398. PUT_UINT32_BE( ctx->state[1], output, 4 );
  399. PUT_UINT32_BE( ctx->state[2], output, 8 );
  400. PUT_UINT32_BE( ctx->state[3], output, 12 );
  401. PUT_UINT32_BE( ctx->state[4], output, 16 );
  402. PUT_UINT32_BE( ctx->state[5], output, 20 );
  403. PUT_UINT32_BE( ctx->state[6], output, 24 );
  404. if( ctx->is224 == 0 )
  405. PUT_UINT32_BE( ctx->state[7], output, 28 );
  406. return( 0 );
  407. }
  408. #endif /* MBEDTLS_HPM_SHA224_ALT_SW */
  409. #endif /* MBEDTLS_HPM_SDP_SHA256 */
  410. #endif /* MBEDTLS_SHA256_C */