sha256.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. /*
  2. * FIPS-180-2 compliant SHA-256 implementation
  3. *
  4. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  5. * SPDX-License-Identifier: Apache-2.0
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  8. * not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  15. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. *
  19. * This file is part of mbed TLS (https://tls.mbed.org)
  20. */
  21. /*
  22. * The SHA-256 Secure Hash Standard was published by NIST in 2002.
  23. *
  24. * http://csrc.nist.gov/publications/fips/fips180-2/fips180-2.pdf
  25. */
  26. #include "sha256.h"
  27. #include "flash.h"
  28. #include <string.h>
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31. #define mbedtls_printf(...) printf(__VA_ARGS__)
  32. #define MBEDTLS_PARAM_FAILED( cond ) \
  33. mbedtls_printf( "mbed param faile-%s %s", __FILE__, __LINE__ )
  34. /**
  35. * \brief User supplied callback function for parameter validation failure.
  36. * See #MBEDTLS_CHECK_PARAMS for context.
  37. *
  38. * This function will be called unless an alternative treatement
  39. * is defined through the #MBEDTLS_PARAM_FAILED macro.
  40. *
  41. * This function can return, and the operation will be aborted, or
  42. * alternatively, through use of setjmp()/longjmp() can resume
  43. * execution in the application code.
  44. *
  45. * \param failure_condition The assertion that didn't hold.
  46. * \param file The file where the assertion failed.
  47. * \param line The line in the file where the assertion failed.
  48. */
  49. /* Internal macro meant to be called only from within the library. */
  50. #define MBEDTLS_INTERNAL_VALIDATE_RET( cond, ret ) \
  51. do { \
  52. if( !(cond) ) \
  53. { \
  54. MBEDTLS_PARAM_FAILED( cond ); \
  55. return( ret ); \
  56. } \
  57. } while( 0 )
  58. /* Internal macro meant to be called only from within the library. */
  59. #define MBEDTLS_INTERNAL_VALIDATE( cond ) \
  60. do { \
  61. if( !(cond) ) \
  62. { \
  63. MBEDTLS_PARAM_FAILED( cond ); \
  64. return; \
  65. } \
  66. } while( 0 )
  67. #define SHA256_VALIDATE_RET(cond) \
  68. MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_SHA256_BAD_INPUT_DATA )
  69. #define SHA256_VALIDATE(cond) MBEDTLS_INTERNAL_VALIDATE( cond )
  70. /*
  71. * 32-bit integer manipulation macros (big endian)
  72. */
  73. #ifndef GET_UINT32_BE
  74. #define GET_UINT32_BE(n,b,i) \
  75. do { \
  76. (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
  77. | ( (uint32_t) (b)[(i) + 1] << 16 ) \
  78. | ( (uint32_t) (b)[(i) + 2] << 8 ) \
  79. | ( (uint32_t) (b)[(i) + 3] ); \
  80. } while( 0 )
  81. #endif
  82. #ifndef PUT_UINT32_BE
  83. #define PUT_UINT32_BE(n,b,i) \
  84. do { \
  85. (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
  86. (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
  87. (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
  88. (b)[(i) + 3] = (unsigned char) ( (n) ); \
  89. } while( 0 )
  90. #endif
  91. void mbedtls_sha256_init( mbedtls_sha256_context *ctx )
  92. {
  93. // SHA256_VALIDATE( ctx != NULL );
  94. memset( ctx, 0, sizeof( mbedtls_sha256_context ) );
  95. }
  96. void mbedtls_sha256_free( mbedtls_sha256_context *ctx )
  97. {
  98. if( ctx == NULL )
  99. return;
  100. //mbedtls_platform_zeroize( ctx, sizeof( mbedtls_sha256_context ) );
  101. memset( ctx, 0, sizeof( mbedtls_sha256_context ) );
  102. }
  103. void mbedtls_sha256_clone( mbedtls_sha256_context *dst,
  104. const mbedtls_sha256_context *src )
  105. {
  106. // SHA256_VALIDATE( dst != NULL );
  107. // SHA256_VALIDATE( src != NULL );
  108. *dst = *src;
  109. }
  110. /*
  111. * SHA-256 context setup
  112. */
  113. int mbedtls_sha256_starts_ret( mbedtls_sha256_context *ctx, int is224 )
  114. {
  115. // SHA256_VALIDATE_RET( ctx != NULL );
  116. // SHA256_VALIDATE_RET( is224 == 0 || is224 == 1 );
  117. ctx->total[0] = 0;
  118. ctx->total[1] = 0;
  119. if( is224 == 0 )
  120. {
  121. /* SHA-256 */
  122. ctx->state[0] = 0x6A09E667;
  123. ctx->state[1] = 0xBB67AE85;
  124. ctx->state[2] = 0x3C6EF372;
  125. ctx->state[3] = 0xA54FF53A;
  126. ctx->state[4] = 0x510E527F;
  127. ctx->state[5] = 0x9B05688C;
  128. ctx->state[6] = 0x1F83D9AB;
  129. ctx->state[7] = 0x5BE0CD19;
  130. }
  131. else
  132. {
  133. /* SHA-224 */
  134. ctx->state[0] = 0xC1059ED8;
  135. ctx->state[1] = 0x367CD507;
  136. ctx->state[2] = 0x3070DD17;
  137. ctx->state[3] = 0xF70E5939;
  138. ctx->state[4] = 0xFFC00B31;
  139. ctx->state[5] = 0x68581511;
  140. ctx->state[6] = 0x64F98FA7;
  141. ctx->state[7] = 0xBEFA4FA4;
  142. }
  143. ctx->is224 = is224;
  144. return( 0 );
  145. }
  146. #if !defined(MBEDTLS_DEPRECATED_REMOVED)
  147. void mbedtls_sha256_starts( mbedtls_sha256_context *ctx,
  148. int is224 )
  149. {
  150. mbedtls_sha256_starts_ret( ctx, is224 );
  151. }
  152. #endif
  153. #if !defined(MBEDTLS_SHA256_PROCESS_ALT)
  154. static const uint32_t K[] =
  155. {
  156. 0x428A2F98, 0x71374491, 0xB5C0FBCF, 0xE9B5DBA5,
  157. 0x3956C25B, 0x59F111F1, 0x923F82A4, 0xAB1C5ED5,
  158. 0xD807AA98, 0x12835B01, 0x243185BE, 0x550C7DC3,
  159. 0x72BE5D74, 0x80DEB1FE, 0x9BDC06A7, 0xC19BF174,
  160. 0xE49B69C1, 0xEFBE4786, 0x0FC19DC6, 0x240CA1CC,
  161. 0x2DE92C6F, 0x4A7484AA, 0x5CB0A9DC, 0x76F988DA,
  162. 0x983E5152, 0xA831C66D, 0xB00327C8, 0xBF597FC7,
  163. 0xC6E00BF3, 0xD5A79147, 0x06CA6351, 0x14292967,
  164. 0x27B70A85, 0x2E1B2138, 0x4D2C6DFC, 0x53380D13,
  165. 0x650A7354, 0x766A0ABB, 0x81C2C92E, 0x92722C85,
  166. 0xA2BFE8A1, 0xA81A664B, 0xC24B8B70, 0xC76C51A3,
  167. 0xD192E819, 0xD6990624, 0xF40E3585, 0x106AA070,
  168. 0x19A4C116, 0x1E376C08, 0x2748774C, 0x34B0BCB5,
  169. 0x391C0CB3, 0x4ED8AA4A, 0x5B9CCA4F, 0x682E6FF3,
  170. 0x748F82EE, 0x78A5636F, 0x84C87814, 0x8CC70208,
  171. 0x90BEFFFA, 0xA4506CEB, 0xBEF9A3F7, 0xC67178F2,
  172. };
  173. #define SHR(x,n) ((x & 0xFFFFFFFF) >> n)
  174. #define ROTR(x,n) (SHR(x,n) | (x << (32 - n)))
  175. #define S0(x) (ROTR(x, 7) ^ ROTR(x,18) ^ SHR(x, 3))
  176. #define S1(x) (ROTR(x,17) ^ ROTR(x,19) ^ SHR(x,10))
  177. #define S2(x) (ROTR(x, 2) ^ ROTR(x,13) ^ ROTR(x,22))
  178. #define S3(x) (ROTR(x, 6) ^ ROTR(x,11) ^ ROTR(x,25))
  179. #define F0(x,y,z) ((x & y) | (z & (x | y)))
  180. #define F1(x,y,z) (z ^ (x & (y ^ z)))
  181. #define R(t) \
  182. ( \
  183. W[t] = S1(W[t - 2]) + W[t - 7] + \
  184. S0(W[t - 15]) + W[t - 16] \
  185. )
  186. #define P(a,b,c,d,e,f,g,h,x,K) \
  187. { \
  188. temp1 = h + S3(e) + F1(e,f,g) + K + x; \
  189. temp2 = S2(a) + F0(a,b,c); \
  190. d += temp1; h = temp1 + temp2; \
  191. }
  192. int mbedtls_internal_sha256_process( mbedtls_sha256_context *ctx,
  193. const unsigned char data[64] )
  194. {
  195. uint32_t temp1, temp2, W[64];
  196. uint32_t A[8];
  197. unsigned int i;
  198. // SHA256_VALIDATE_RET( ctx != NULL );
  199. // SHA256_VALIDATE_RET( (const unsigned char *)data != NULL );
  200. for( i = 0; i < 8; i++ )
  201. A[i] = ctx->state[i];
  202. #if defined(MBEDTLS_SHA256_SMALLER)
  203. for( i = 0; i < 64; i++ )
  204. {
  205. if( i < 16 )
  206. GET_UINT32_BE( W[i], data, 4 * i );
  207. else
  208. R( i );
  209. P( A[0], A[1], A[2], A[3], A[4], A[5], A[6], A[7], W[i], K[i] );
  210. temp1 = A[7]; A[7] = A[6]; A[6] = A[5]; A[5] = A[4]; A[4] = A[3];
  211. A[3] = A[2]; A[2] = A[1]; A[1] = A[0]; A[0] = temp1;
  212. }
  213. #else /* MBEDTLS_SHA256_SMALLER */
  214. for( i = 0; i < 16; i++ )
  215. GET_UINT32_BE( W[i], data, 4 * i );
  216. for( i = 0; i < 16; i += 8 )
  217. {
  218. P( A[0], A[1], A[2], A[3], A[4], A[5], A[6], A[7], W[i+0], K[i+0] );
  219. P( A[7], A[0], A[1], A[2], A[3], A[4], A[5], A[6], W[i+1], K[i+1] );
  220. P( A[6], A[7], A[0], A[1], A[2], A[3], A[4], A[5], W[i+2], K[i+2] );
  221. P( A[5], A[6], A[7], A[0], A[1], A[2], A[3], A[4], W[i+3], K[i+3] );
  222. P( A[4], A[5], A[6], A[7], A[0], A[1], A[2], A[3], W[i+4], K[i+4] );
  223. P( A[3], A[4], A[5], A[6], A[7], A[0], A[1], A[2], W[i+5], K[i+5] );
  224. P( A[2], A[3], A[4], A[5], A[6], A[7], A[0], A[1], W[i+6], K[i+6] );
  225. P( A[1], A[2], A[3], A[4], A[5], A[6], A[7], A[0], W[i+7], K[i+7] );
  226. }
  227. for( i = 16; i < 64; i += 8 )
  228. {
  229. P( A[0], A[1], A[2], A[3], A[4], A[5], A[6], A[7], R(i+0), K[i+0] );
  230. P( A[7], A[0], A[1], A[2], A[3], A[4], A[5], A[6], R(i+1), K[i+1] );
  231. P( A[6], A[7], A[0], A[1], A[2], A[3], A[4], A[5], R(i+2), K[i+2] );
  232. P( A[5], A[6], A[7], A[0], A[1], A[2], A[3], A[4], R(i+3), K[i+3] );
  233. P( A[4], A[5], A[6], A[7], A[0], A[1], A[2], A[3], R(i+4), K[i+4] );
  234. P( A[3], A[4], A[5], A[6], A[7], A[0], A[1], A[2], R(i+5), K[i+5] );
  235. P( A[2], A[3], A[4], A[5], A[6], A[7], A[0], A[1], R(i+6), K[i+6] );
  236. P( A[1], A[2], A[3], A[4], A[5], A[6], A[7], A[0], R(i+7), K[i+7] );
  237. }
  238. #endif /* MBEDTLS_SHA256_SMALLER */
  239. for( i = 0; i < 8; i++ )
  240. ctx->state[i] += A[i];
  241. return( 0 );
  242. }
  243. #if !defined(MBEDTLS_DEPRECATED_REMOVED)
  244. void mbedtls_sha256_process( mbedtls_sha256_context *ctx,
  245. const unsigned char data[64] )
  246. {
  247. mbedtls_internal_sha256_process( ctx, data );
  248. }
  249. #endif
  250. #endif /* !MBEDTLS_SHA256_PROCESS_ALT */
  251. /*
  252. * SHA-256 process buffer
  253. */
  254. int mbedtls_sha256_update_ret( mbedtls_sha256_context *ctx,
  255. const unsigned char *input,
  256. size_t ilen )
  257. {
  258. int ret;
  259. size_t fill;
  260. uint32_t left;
  261. // SHA256_VALIDATE_RET( ctx != NULL );
  262. // SHA256_VALIDATE_RET( ilen == 0 || input != NULL );
  263. if( ilen == 0 )
  264. return( 0 );
  265. left = ctx->total[0] & 0x3F;
  266. fill = 64 - left;
  267. ctx->total[0] += (uint32_t) ilen;
  268. ctx->total[0] &= 0xFFFFFFFF;
  269. if( ctx->total[0] < (uint32_t) ilen )
  270. ctx->total[1]++;
  271. if( left && ilen >= fill )
  272. {
  273. memcpy( (void *) (ctx->buffer + left), input, fill );
  274. if( ( ret = mbedtls_internal_sha256_process( ctx, ctx->buffer ) ) != 0 )
  275. return( ret );
  276. input += fill;
  277. ilen -= fill;
  278. left = 0;
  279. }
  280. while( ilen >= 64 )
  281. {
  282. if( ( ret = mbedtls_internal_sha256_process( ctx, input ) ) != 0 )
  283. return( ret );
  284. input += 64;
  285. ilen -= 64;
  286. }
  287. if( ilen > 0 )
  288. memcpy( (void *) (ctx->buffer + left), input, ilen );
  289. return( 0 );
  290. }
  291. #if !defined(MBEDTLS_DEPRECATED_REMOVED)
  292. void mbedtls_sha256_update( mbedtls_sha256_context *ctx,
  293. const unsigned char *input,
  294. size_t ilen )
  295. {
  296. mbedtls_sha256_update_ret( ctx, input, ilen );
  297. }
  298. #endif
  299. /*
  300. * SHA-256 final digest
  301. */
  302. int mbedtls_sha256_finish_ret( mbedtls_sha256_context *ctx,
  303. unsigned char output[32] )
  304. {
  305. int ret;
  306. uint32_t used;
  307. uint32_t high, low;
  308. // SHA256_VALIDATE_RET( ctx != NULL );
  309. // SHA256_VALIDATE_RET( (unsigned char *)output != NULL );
  310. /*
  311. * Add padding: 0x80 then 0x00 until 8 bytes remain for the length
  312. */
  313. used = ctx->total[0] & 0x3F;
  314. ctx->buffer[used++] = 0x80;
  315. if( used <= 56 )
  316. {
  317. /* Enough room for padding + length in current block */
  318. memset( ctx->buffer + used, 0, 56 - used );
  319. }
  320. else
  321. {
  322. /* We'll need an extra block */
  323. memset( ctx->buffer + used, 0, 64 - used );
  324. if( ( ret = mbedtls_internal_sha256_process( ctx, ctx->buffer ) ) != 0 )
  325. return( ret );
  326. memset( ctx->buffer, 0, 56 );
  327. }
  328. /*
  329. * Add message length
  330. */
  331. high = ( ctx->total[0] >> 29 )
  332. | ( ctx->total[1] << 3 );
  333. low = ( ctx->total[0] << 3 );
  334. PUT_UINT32_BE( high, ctx->buffer, 56 );
  335. PUT_UINT32_BE( low, ctx->buffer, 60 );
  336. if( ( ret = mbedtls_internal_sha256_process( ctx, ctx->buffer ) ) != 0 )
  337. return( ret );
  338. /*
  339. * Output final state
  340. */
  341. PUT_UINT32_BE( ctx->state[0], output, 0 );
  342. PUT_UINT32_BE( ctx->state[1], output, 4 );
  343. PUT_UINT32_BE( ctx->state[2], output, 8 );
  344. PUT_UINT32_BE( ctx->state[3], output, 12 );
  345. PUT_UINT32_BE( ctx->state[4], output, 16 );
  346. PUT_UINT32_BE( ctx->state[5], output, 20 );
  347. PUT_UINT32_BE( ctx->state[6], output, 24 );
  348. if( ctx->is224 == 0 )
  349. PUT_UINT32_BE( ctx->state[7], output, 28 );
  350. return( 0 );
  351. }