sha256.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939
  1. /*
  2. * FIPS-180-2 compliant SHA-256 implementation
  3. *
  4. * Copyright The Mbed TLS Contributors
  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. /*
  20. * The SHA-256 Secure Hash Standard was published by NIST in 2002.
  21. *
  22. * http://csrc.nist.gov/publications/fips/fips180-2/fips180-2.pdf
  23. */
  24. #if defined(__aarch64__) && !defined(__ARM_FEATURE_CRYPTO) && \
  25. defined(__clang__) && __clang_major__ >= 4
  26. /* TODO: Re-consider above after https://reviews.llvm.org/D131064 merged.
  27. *
  28. * The intrinsic declaration are guarded by predefined ACLE macros in clang:
  29. * these are normally only enabled by the -march option on the command line.
  30. * By defining the macros ourselves we gain access to those declarations without
  31. * requiring -march on the command line.
  32. *
  33. * `arm_neon.h` could be included by any header file, so we put these defines
  34. * at the top of this file, before any includes.
  35. */
  36. #define __ARM_FEATURE_CRYPTO 1
  37. /* See: https://arm-software.github.io/acle/main/acle.html#cryptographic-extensions
  38. *
  39. * `__ARM_FEATURE_CRYPTO` is deprecated, but we need to continue to specify it
  40. * for older compilers.
  41. */
  42. #define __ARM_FEATURE_SHA2 1
  43. #define MBEDTLS_ENABLE_ARM_CRYPTO_EXTENSIONS_COMPILER_FLAG
  44. #endif
  45. #include "common.h"
  46. #if defined(MBEDTLS_SHA256_C) || defined(MBEDTLS_SHA224_C)
  47. #include "mbedtls/sha256.h"
  48. #include "mbedtls/platform_util.h"
  49. #include "mbedtls/error.h"
  50. #include <string.h>
  51. #include "mbedtls/platform.h"
  52. #if defined(__aarch64__)
  53. # if defined(MBEDTLS_SHA256_USE_A64_CRYPTO_IF_PRESENT) || \
  54. defined(MBEDTLS_SHA256_USE_A64_CRYPTO_ONLY)
  55. /* *INDENT-OFF* */
  56. # if !defined(__ARM_FEATURE_CRYPTO) || defined(MBEDTLS_ENABLE_ARM_CRYPTO_EXTENSIONS_COMPILER_FLAG)
  57. # if defined(__clang__)
  58. # if __clang_major__ < 4
  59. # error "A more recent Clang is required for MBEDTLS_SHA256_USE_A64_CRYPTO_*"
  60. # endif
  61. # pragma clang attribute push (__attribute__((target("crypto"))), apply_to=function)
  62. # define MBEDTLS_POP_TARGET_PRAGMA
  63. # elif defined(__GNUC__)
  64. /* FIXME: GCC 5 claims to support Armv8 Crypto Extensions, but some
  65. * intrinsics are missing. Missing intrinsics could be worked around.
  66. */
  67. # if __GNUC__ < 6
  68. # error "A more recent GCC is required for MBEDTLS_SHA256_USE_A64_CRYPTO_*"
  69. # else
  70. # pragma GCC push_options
  71. # pragma GCC target ("arch=armv8-a+crypto")
  72. # define MBEDTLS_POP_TARGET_PRAGMA
  73. # endif
  74. # else
  75. # error "Only GCC and Clang supported for MBEDTLS_SHA256_USE_A64_CRYPTO_*"
  76. # endif
  77. # endif
  78. /* *INDENT-ON* */
  79. # include <arm_neon.h>
  80. # endif
  81. # if defined(MBEDTLS_SHA256_USE_A64_CRYPTO_IF_PRESENT)
  82. # if defined(__unix__)
  83. # if defined(__linux__)
  84. /* Our preferred method of detection is getauxval() */
  85. # include <sys/auxv.h>
  86. # endif
  87. /* Use SIGILL on Unix, and fall back to it on Linux */
  88. # include <signal.h>
  89. # endif
  90. # endif
  91. #elif defined(_M_ARM64)
  92. # if defined(MBEDTLS_SHA256_USE_A64_CRYPTO_IF_PRESENT) || \
  93. defined(MBEDTLS_SHA256_USE_A64_CRYPTO_ONLY)
  94. # include <arm64_neon.h>
  95. # endif
  96. #else
  97. # undef MBEDTLS_SHA256_USE_A64_CRYPTO_ONLY
  98. # undef MBEDTLS_SHA256_USE_A64_CRYPTO_IF_PRESENT
  99. #endif
  100. #if defined(MBEDTLS_SHA256_USE_A64_CRYPTO_IF_PRESENT)
  101. /*
  102. * Capability detection code comes early, so we can disable
  103. * MBEDTLS_SHA256_USE_A64_CRYPTO_IF_PRESENT if no detection mechanism found
  104. */
  105. #if defined(HWCAP_SHA2)
  106. static int mbedtls_a64_crypto_sha256_determine_support(void)
  107. {
  108. return (getauxval(AT_HWCAP) & HWCAP_SHA2) ? 1 : 0;
  109. }
  110. #elif defined(__APPLE__)
  111. static int mbedtls_a64_crypto_sha256_determine_support(void)
  112. {
  113. return 1;
  114. }
  115. #elif defined(_M_ARM64)
  116. #define WIN32_LEAN_AND_MEAN
  117. #include <Windows.h>
  118. #include <processthreadsapi.h>
  119. static int mbedtls_a64_crypto_sha256_determine_support(void)
  120. {
  121. return IsProcessorFeaturePresent(PF_ARM_V8_CRYPTO_INSTRUCTIONS_AVAILABLE) ?
  122. 1 : 0;
  123. }
  124. #elif defined(__unix__) && defined(SIG_SETMASK)
  125. /* Detection with SIGILL, setjmp() and longjmp() */
  126. #include <signal.h>
  127. #include <setjmp.h>
  128. static jmp_buf return_from_sigill;
  129. /*
  130. * A64 SHA256 support detection via SIGILL
  131. */
  132. static void sigill_handler(int signal)
  133. {
  134. (void) signal;
  135. longjmp(return_from_sigill, 1);
  136. }
  137. static int mbedtls_a64_crypto_sha256_determine_support(void)
  138. {
  139. struct sigaction old_action, new_action;
  140. sigset_t old_mask;
  141. if (sigprocmask(0, NULL, &old_mask)) {
  142. return 0;
  143. }
  144. sigemptyset(&new_action.sa_mask);
  145. new_action.sa_flags = 0;
  146. new_action.sa_handler = sigill_handler;
  147. sigaction(SIGILL, &new_action, &old_action);
  148. static int ret = 0;
  149. if (setjmp(return_from_sigill) == 0) { /* First return only */
  150. /* If this traps, we will return a second time from setjmp() with 1 */
  151. asm ("sha256h q0, q0, v0.4s" : : : "v0");
  152. ret = 1;
  153. }
  154. sigaction(SIGILL, &old_action, NULL);
  155. sigprocmask(SIG_SETMASK, &old_mask, NULL);
  156. return ret;
  157. }
  158. #else
  159. #warning "No mechanism to detect A64_CRYPTO found, using C code only"
  160. #undef MBEDTLS_SHA256_USE_A64_CRYPTO_IF_PRESENT
  161. #endif /* HWCAP_SHA2, __APPLE__, __unix__ && SIG_SETMASK */
  162. #endif /* MBEDTLS_SHA256_USE_A64_CRYPTO_IF_PRESENT */
  163. #if !defined(MBEDTLS_SHA256_ALT)
  164. #define SHA256_BLOCK_SIZE 64
  165. void mbedtls_sha256_init(mbedtls_sha256_context *ctx)
  166. {
  167. memset(ctx, 0, sizeof(mbedtls_sha256_context));
  168. }
  169. void mbedtls_sha256_free(mbedtls_sha256_context *ctx)
  170. {
  171. if (ctx == NULL) {
  172. return;
  173. }
  174. mbedtls_platform_zeroize(ctx, sizeof(mbedtls_sha256_context));
  175. }
  176. void mbedtls_sha256_clone(mbedtls_sha256_context *dst,
  177. const mbedtls_sha256_context *src)
  178. {
  179. *dst = *src;
  180. }
  181. /*
  182. * SHA-256 context setup
  183. */
  184. int mbedtls_sha256_starts(mbedtls_sha256_context *ctx, int is224)
  185. {
  186. #if defined(MBEDTLS_SHA224_C) && defined(MBEDTLS_SHA256_C)
  187. if (is224 != 0 && is224 != 1) {
  188. return MBEDTLS_ERR_SHA256_BAD_INPUT_DATA;
  189. }
  190. #elif defined(MBEDTLS_SHA256_C)
  191. if (is224 != 0) {
  192. return MBEDTLS_ERR_SHA256_BAD_INPUT_DATA;
  193. }
  194. #else /* defined MBEDTLS_SHA224_C only */
  195. if (is224 == 0) {
  196. return MBEDTLS_ERR_SHA256_BAD_INPUT_DATA;
  197. }
  198. #endif
  199. ctx->total[0] = 0;
  200. ctx->total[1] = 0;
  201. if (is224 == 0) {
  202. #if defined(MBEDTLS_SHA256_C)
  203. ctx->state[0] = 0x6A09E667;
  204. ctx->state[1] = 0xBB67AE85;
  205. ctx->state[2] = 0x3C6EF372;
  206. ctx->state[3] = 0xA54FF53A;
  207. ctx->state[4] = 0x510E527F;
  208. ctx->state[5] = 0x9B05688C;
  209. ctx->state[6] = 0x1F83D9AB;
  210. ctx->state[7] = 0x5BE0CD19;
  211. #endif
  212. } else {
  213. #if defined(MBEDTLS_SHA224_C)
  214. ctx->state[0] = 0xC1059ED8;
  215. ctx->state[1] = 0x367CD507;
  216. ctx->state[2] = 0x3070DD17;
  217. ctx->state[3] = 0xF70E5939;
  218. ctx->state[4] = 0xFFC00B31;
  219. ctx->state[5] = 0x68581511;
  220. ctx->state[6] = 0x64F98FA7;
  221. ctx->state[7] = 0xBEFA4FA4;
  222. #endif
  223. }
  224. #if defined(MBEDTLS_SHA224_C)
  225. ctx->is224 = is224;
  226. #endif
  227. return 0;
  228. }
  229. #if !defined(MBEDTLS_SHA256_PROCESS_ALT)
  230. static const uint32_t K[] =
  231. {
  232. 0x428A2F98, 0x71374491, 0xB5C0FBCF, 0xE9B5DBA5,
  233. 0x3956C25B, 0x59F111F1, 0x923F82A4, 0xAB1C5ED5,
  234. 0xD807AA98, 0x12835B01, 0x243185BE, 0x550C7DC3,
  235. 0x72BE5D74, 0x80DEB1FE, 0x9BDC06A7, 0xC19BF174,
  236. 0xE49B69C1, 0xEFBE4786, 0x0FC19DC6, 0x240CA1CC,
  237. 0x2DE92C6F, 0x4A7484AA, 0x5CB0A9DC, 0x76F988DA,
  238. 0x983E5152, 0xA831C66D, 0xB00327C8, 0xBF597FC7,
  239. 0xC6E00BF3, 0xD5A79147, 0x06CA6351, 0x14292967,
  240. 0x27B70A85, 0x2E1B2138, 0x4D2C6DFC, 0x53380D13,
  241. 0x650A7354, 0x766A0ABB, 0x81C2C92E, 0x92722C85,
  242. 0xA2BFE8A1, 0xA81A664B, 0xC24B8B70, 0xC76C51A3,
  243. 0xD192E819, 0xD6990624, 0xF40E3585, 0x106AA070,
  244. 0x19A4C116, 0x1E376C08, 0x2748774C, 0x34B0BCB5,
  245. 0x391C0CB3, 0x4ED8AA4A, 0x5B9CCA4F, 0x682E6FF3,
  246. 0x748F82EE, 0x78A5636F, 0x84C87814, 0x8CC70208,
  247. 0x90BEFFFA, 0xA4506CEB, 0xBEF9A3F7, 0xC67178F2,
  248. };
  249. #endif
  250. #if defined(MBEDTLS_SHA256_USE_A64_CRYPTO_IF_PRESENT) || \
  251. defined(MBEDTLS_SHA256_USE_A64_CRYPTO_ONLY)
  252. #if defined(MBEDTLS_SHA256_USE_A64_CRYPTO_ONLY)
  253. # define mbedtls_internal_sha256_process_many_a64_crypto mbedtls_internal_sha256_process_many
  254. # define mbedtls_internal_sha256_process_a64_crypto mbedtls_internal_sha256_process
  255. #endif
  256. static size_t mbedtls_internal_sha256_process_many_a64_crypto(
  257. mbedtls_sha256_context *ctx, const uint8_t *msg, size_t len)
  258. {
  259. uint32x4_t abcd = vld1q_u32(&ctx->state[0]);
  260. uint32x4_t efgh = vld1q_u32(&ctx->state[4]);
  261. size_t processed = 0;
  262. for (;
  263. len >= SHA256_BLOCK_SIZE;
  264. processed += SHA256_BLOCK_SIZE,
  265. msg += SHA256_BLOCK_SIZE,
  266. len -= SHA256_BLOCK_SIZE) {
  267. uint32x4_t tmp, abcd_prev;
  268. uint32x4_t abcd_orig = abcd;
  269. uint32x4_t efgh_orig = efgh;
  270. uint32x4_t sched0 = (uint32x4_t) vld1q_u8(msg + 16 * 0);
  271. uint32x4_t sched1 = (uint32x4_t) vld1q_u8(msg + 16 * 1);
  272. uint32x4_t sched2 = (uint32x4_t) vld1q_u8(msg + 16 * 2);
  273. uint32x4_t sched3 = (uint32x4_t) vld1q_u8(msg + 16 * 3);
  274. #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ /* Will be true if not defined */
  275. /* Untested on BE */
  276. sched0 = vreinterpretq_u32_u8(vrev32q_u8(vreinterpretq_u8_u32(sched0)));
  277. sched1 = vreinterpretq_u32_u8(vrev32q_u8(vreinterpretq_u8_u32(sched1)));
  278. sched2 = vreinterpretq_u32_u8(vrev32q_u8(vreinterpretq_u8_u32(sched2)));
  279. sched3 = vreinterpretq_u32_u8(vrev32q_u8(vreinterpretq_u8_u32(sched3)));
  280. #endif
  281. /* Rounds 0 to 3 */
  282. tmp = vaddq_u32(sched0, vld1q_u32(&K[0]));
  283. abcd_prev = abcd;
  284. abcd = vsha256hq_u32(abcd_prev, efgh, tmp);
  285. efgh = vsha256h2q_u32(efgh, abcd_prev, tmp);
  286. /* Rounds 4 to 7 */
  287. tmp = vaddq_u32(sched1, vld1q_u32(&K[4]));
  288. abcd_prev = abcd;
  289. abcd = vsha256hq_u32(abcd_prev, efgh, tmp);
  290. efgh = vsha256h2q_u32(efgh, abcd_prev, tmp);
  291. /* Rounds 8 to 11 */
  292. tmp = vaddq_u32(sched2, vld1q_u32(&K[8]));
  293. abcd_prev = abcd;
  294. abcd = vsha256hq_u32(abcd_prev, efgh, tmp);
  295. efgh = vsha256h2q_u32(efgh, abcd_prev, tmp);
  296. /* Rounds 12 to 15 */
  297. tmp = vaddq_u32(sched3, vld1q_u32(&K[12]));
  298. abcd_prev = abcd;
  299. abcd = vsha256hq_u32(abcd_prev, efgh, tmp);
  300. efgh = vsha256h2q_u32(efgh, abcd_prev, tmp);
  301. for (int t = 16; t < 64; t += 16) {
  302. /* Rounds t to t + 3 */
  303. sched0 = vsha256su1q_u32(vsha256su0q_u32(sched0, sched1), sched2, sched3);
  304. tmp = vaddq_u32(sched0, vld1q_u32(&K[t]));
  305. abcd_prev = abcd;
  306. abcd = vsha256hq_u32(abcd_prev, efgh, tmp);
  307. efgh = vsha256h2q_u32(efgh, abcd_prev, tmp);
  308. /* Rounds t + 4 to t + 7 */
  309. sched1 = vsha256su1q_u32(vsha256su0q_u32(sched1, sched2), sched3, sched0);
  310. tmp = vaddq_u32(sched1, vld1q_u32(&K[t + 4]));
  311. abcd_prev = abcd;
  312. abcd = vsha256hq_u32(abcd_prev, efgh, tmp);
  313. efgh = vsha256h2q_u32(efgh, abcd_prev, tmp);
  314. /* Rounds t + 8 to t + 11 */
  315. sched2 = vsha256su1q_u32(vsha256su0q_u32(sched2, sched3), sched0, sched1);
  316. tmp = vaddq_u32(sched2, vld1q_u32(&K[t + 8]));
  317. abcd_prev = abcd;
  318. abcd = vsha256hq_u32(abcd_prev, efgh, tmp);
  319. efgh = vsha256h2q_u32(efgh, abcd_prev, tmp);
  320. /* Rounds t + 12 to t + 15 */
  321. sched3 = vsha256su1q_u32(vsha256su0q_u32(sched3, sched0), sched1, sched2);
  322. tmp = vaddq_u32(sched3, vld1q_u32(&K[t + 12]));
  323. abcd_prev = abcd;
  324. abcd = vsha256hq_u32(abcd_prev, efgh, tmp);
  325. efgh = vsha256h2q_u32(efgh, abcd_prev, tmp);
  326. }
  327. abcd = vaddq_u32(abcd, abcd_orig);
  328. efgh = vaddq_u32(efgh, efgh_orig);
  329. }
  330. vst1q_u32(&ctx->state[0], abcd);
  331. vst1q_u32(&ctx->state[4], efgh);
  332. return processed;
  333. }
  334. #if defined(MBEDTLS_SHA256_USE_A64_CRYPTO_IF_PRESENT)
  335. /*
  336. * This function is for internal use only if we are building both C and A64
  337. * versions, otherwise it is renamed to be the public mbedtls_internal_sha256_process()
  338. */
  339. static
  340. #endif
  341. int mbedtls_internal_sha256_process_a64_crypto(mbedtls_sha256_context *ctx,
  342. const unsigned char data[SHA256_BLOCK_SIZE])
  343. {
  344. return (mbedtls_internal_sha256_process_many_a64_crypto(ctx, data,
  345. SHA256_BLOCK_SIZE) ==
  346. SHA256_BLOCK_SIZE) ? 0 : -1;
  347. }
  348. #if defined(MBEDTLS_POP_TARGET_PRAGMA)
  349. #if defined(__clang__)
  350. #pragma clang attribute pop
  351. #elif defined(__GNUC__)
  352. #pragma GCC pop_options
  353. #endif
  354. #undef MBEDTLS_POP_TARGET_PRAGMA
  355. #endif
  356. #endif /* MBEDTLS_SHA256_USE_A64_CRYPTO_IF_PRESENT || MBEDTLS_SHA256_USE_A64_CRYPTO_ONLY */
  357. #if !defined(MBEDTLS_SHA256_USE_A64_CRYPTO_IF_PRESENT)
  358. #define mbedtls_internal_sha256_process_many_c mbedtls_internal_sha256_process_many
  359. #define mbedtls_internal_sha256_process_c mbedtls_internal_sha256_process
  360. #endif
  361. #if !defined(MBEDTLS_SHA256_PROCESS_ALT) && \
  362. !defined(MBEDTLS_SHA256_USE_A64_CRYPTO_ONLY)
  363. #define SHR(x, n) (((x) & 0xFFFFFFFF) >> (n))
  364. #define ROTR(x, n) (SHR(x, n) | ((x) << (32 - (n))))
  365. #define S0(x) (ROTR(x, 7) ^ ROTR(x, 18) ^ SHR(x, 3))
  366. #define S1(x) (ROTR(x, 17) ^ ROTR(x, 19) ^ SHR(x, 10))
  367. #define S2(x) (ROTR(x, 2) ^ ROTR(x, 13) ^ ROTR(x, 22))
  368. #define S3(x) (ROTR(x, 6) ^ ROTR(x, 11) ^ ROTR(x, 25))
  369. #define F0(x, y, z) (((x) & (y)) | ((z) & ((x) | (y))))
  370. #define F1(x, y, z) ((z) ^ ((x) & ((y) ^ (z))))
  371. #define R(t) \
  372. ( \
  373. local.W[t] = S1(local.W[(t) - 2]) + local.W[(t) - 7] + \
  374. S0(local.W[(t) - 15]) + local.W[(t) - 16] \
  375. )
  376. #define P(a, b, c, d, e, f, g, h, x, K) \
  377. do \
  378. { \
  379. local.temp1 = (h) + S3(e) + F1((e), (f), (g)) + (K) + (x); \
  380. local.temp2 = S2(a) + F0((a), (b), (c)); \
  381. (d) += local.temp1; (h) = local.temp1 + local.temp2; \
  382. } while (0)
  383. #if defined(MBEDTLS_SHA256_USE_A64_CRYPTO_IF_PRESENT)
  384. /*
  385. * This function is for internal use only if we are building both C and A64
  386. * versions, otherwise it is renamed to be the public mbedtls_internal_sha256_process()
  387. */
  388. static
  389. #endif
  390. int mbedtls_internal_sha256_process_c(mbedtls_sha256_context *ctx,
  391. const unsigned char data[SHA256_BLOCK_SIZE])
  392. {
  393. struct {
  394. uint32_t temp1, temp2, W[64];
  395. uint32_t A[8];
  396. } local;
  397. unsigned int i;
  398. for (i = 0; i < 8; i++) {
  399. local.A[i] = ctx->state[i];
  400. }
  401. #if defined(MBEDTLS_SHA256_SMALLER)
  402. for (i = 0; i < 64; i++) {
  403. if (i < 16) {
  404. local.W[i] = MBEDTLS_GET_UINT32_BE(data, 4 * i);
  405. } else {
  406. R(i);
  407. }
  408. P(local.A[0], local.A[1], local.A[2], local.A[3], local.A[4],
  409. local.A[5], local.A[6], local.A[7], local.W[i], K[i]);
  410. local.temp1 = local.A[7]; local.A[7] = local.A[6];
  411. local.A[6] = local.A[5]; local.A[5] = local.A[4];
  412. local.A[4] = local.A[3]; local.A[3] = local.A[2];
  413. local.A[2] = local.A[1]; local.A[1] = local.A[0];
  414. local.A[0] = local.temp1;
  415. }
  416. #else /* MBEDTLS_SHA256_SMALLER */
  417. for (i = 0; i < 16; i++) {
  418. local.W[i] = MBEDTLS_GET_UINT32_BE(data, 4 * i);
  419. }
  420. for (i = 0; i < 16; i += 8) {
  421. P(local.A[0], local.A[1], local.A[2], local.A[3], local.A[4],
  422. local.A[5], local.A[6], local.A[7], local.W[i+0], K[i+0]);
  423. P(local.A[7], local.A[0], local.A[1], local.A[2], local.A[3],
  424. local.A[4], local.A[5], local.A[6], local.W[i+1], K[i+1]);
  425. P(local.A[6], local.A[7], local.A[0], local.A[1], local.A[2],
  426. local.A[3], local.A[4], local.A[5], local.W[i+2], K[i+2]);
  427. P(local.A[5], local.A[6], local.A[7], local.A[0], local.A[1],
  428. local.A[2], local.A[3], local.A[4], local.W[i+3], K[i+3]);
  429. P(local.A[4], local.A[5], local.A[6], local.A[7], local.A[0],
  430. local.A[1], local.A[2], local.A[3], local.W[i+4], K[i+4]);
  431. P(local.A[3], local.A[4], local.A[5], local.A[6], local.A[7],
  432. local.A[0], local.A[1], local.A[2], local.W[i+5], K[i+5]);
  433. P(local.A[2], local.A[3], local.A[4], local.A[5], local.A[6],
  434. local.A[7], local.A[0], local.A[1], local.W[i+6], K[i+6]);
  435. P(local.A[1], local.A[2], local.A[3], local.A[4], local.A[5],
  436. local.A[6], local.A[7], local.A[0], local.W[i+7], K[i+7]);
  437. }
  438. for (i = 16; i < 64; i += 8) {
  439. P(local.A[0], local.A[1], local.A[2], local.A[3], local.A[4],
  440. local.A[5], local.A[6], local.A[7], R(i+0), K[i+0]);
  441. P(local.A[7], local.A[0], local.A[1], local.A[2], local.A[3],
  442. local.A[4], local.A[5], local.A[6], R(i+1), K[i+1]);
  443. P(local.A[6], local.A[7], local.A[0], local.A[1], local.A[2],
  444. local.A[3], local.A[4], local.A[5], R(i+2), K[i+2]);
  445. P(local.A[5], local.A[6], local.A[7], local.A[0], local.A[1],
  446. local.A[2], local.A[3], local.A[4], R(i+3), K[i+3]);
  447. P(local.A[4], local.A[5], local.A[6], local.A[7], local.A[0],
  448. local.A[1], local.A[2], local.A[3], R(i+4), K[i+4]);
  449. P(local.A[3], local.A[4], local.A[5], local.A[6], local.A[7],
  450. local.A[0], local.A[1], local.A[2], R(i+5), K[i+5]);
  451. P(local.A[2], local.A[3], local.A[4], local.A[5], local.A[6],
  452. local.A[7], local.A[0], local.A[1], R(i+6), K[i+6]);
  453. P(local.A[1], local.A[2], local.A[3], local.A[4], local.A[5],
  454. local.A[6], local.A[7], local.A[0], R(i+7), K[i+7]);
  455. }
  456. #endif /* MBEDTLS_SHA256_SMALLER */
  457. for (i = 0; i < 8; i++) {
  458. ctx->state[i] += local.A[i];
  459. }
  460. /* Zeroise buffers and variables to clear sensitive data from memory. */
  461. mbedtls_platform_zeroize(&local, sizeof(local));
  462. return 0;
  463. }
  464. #endif /* !MBEDTLS_SHA256_PROCESS_ALT && !MBEDTLS_SHA256_USE_A64_CRYPTO_ONLY */
  465. #if !defined(MBEDTLS_SHA256_USE_A64_CRYPTO_ONLY)
  466. static size_t mbedtls_internal_sha256_process_many_c(
  467. mbedtls_sha256_context *ctx, const uint8_t *data, size_t len)
  468. {
  469. size_t processed = 0;
  470. while (len >= SHA256_BLOCK_SIZE) {
  471. if (mbedtls_internal_sha256_process_c(ctx, data) != 0) {
  472. return 0;
  473. }
  474. data += SHA256_BLOCK_SIZE;
  475. len -= SHA256_BLOCK_SIZE;
  476. processed += SHA256_BLOCK_SIZE;
  477. }
  478. return processed;
  479. }
  480. #endif /* !MBEDTLS_SHA256_USE_A64_CRYPTO_ONLY */
  481. #if defined(MBEDTLS_SHA256_USE_A64_CRYPTO_IF_PRESENT)
  482. static int mbedtls_a64_crypto_sha256_has_support(void)
  483. {
  484. static int done = 0;
  485. static int supported = 0;
  486. if (!done) {
  487. supported = mbedtls_a64_crypto_sha256_determine_support();
  488. done = 1;
  489. }
  490. return supported;
  491. }
  492. static size_t mbedtls_internal_sha256_process_many(mbedtls_sha256_context *ctx,
  493. const uint8_t *msg, size_t len)
  494. {
  495. if (mbedtls_a64_crypto_sha256_has_support()) {
  496. return mbedtls_internal_sha256_process_many_a64_crypto(ctx, msg, len);
  497. } else {
  498. return mbedtls_internal_sha256_process_many_c(ctx, msg, len);
  499. }
  500. }
  501. int mbedtls_internal_sha256_process(mbedtls_sha256_context *ctx,
  502. const unsigned char data[SHA256_BLOCK_SIZE])
  503. {
  504. if (mbedtls_a64_crypto_sha256_has_support()) {
  505. return mbedtls_internal_sha256_process_a64_crypto(ctx, data);
  506. } else {
  507. return mbedtls_internal_sha256_process_c(ctx, data);
  508. }
  509. }
  510. #endif /* MBEDTLS_SHA256_USE_A64_CRYPTO_IF_PRESENT */
  511. /*
  512. * SHA-256 process buffer
  513. */
  514. int mbedtls_sha256_update(mbedtls_sha256_context *ctx,
  515. const unsigned char *input,
  516. size_t ilen)
  517. {
  518. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  519. size_t fill;
  520. uint32_t left;
  521. if (ilen == 0) {
  522. return 0;
  523. }
  524. left = ctx->total[0] & 0x3F;
  525. fill = SHA256_BLOCK_SIZE - left;
  526. ctx->total[0] += (uint32_t) ilen;
  527. ctx->total[0] &= 0xFFFFFFFF;
  528. if (ctx->total[0] < (uint32_t) ilen) {
  529. ctx->total[1]++;
  530. }
  531. if (left && ilen >= fill) {
  532. memcpy((void *) (ctx->buffer + left), input, fill);
  533. if ((ret = mbedtls_internal_sha256_process(ctx, ctx->buffer)) != 0) {
  534. return ret;
  535. }
  536. input += fill;
  537. ilen -= fill;
  538. left = 0;
  539. }
  540. while (ilen >= SHA256_BLOCK_SIZE) {
  541. size_t processed =
  542. mbedtls_internal_sha256_process_many(ctx, input, ilen);
  543. if (processed < SHA256_BLOCK_SIZE) {
  544. return MBEDTLS_ERR_ERROR_GENERIC_ERROR;
  545. }
  546. input += processed;
  547. ilen -= processed;
  548. }
  549. if (ilen > 0) {
  550. memcpy((void *) (ctx->buffer + left), input, ilen);
  551. }
  552. return 0;
  553. }
  554. /*
  555. * SHA-256 final digest
  556. */
  557. int mbedtls_sha256_finish(mbedtls_sha256_context *ctx,
  558. unsigned char *output)
  559. {
  560. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  561. uint32_t used;
  562. uint32_t high, low;
  563. /*
  564. * Add padding: 0x80 then 0x00 until 8 bytes remain for the length
  565. */
  566. used = ctx->total[0] & 0x3F;
  567. ctx->buffer[used++] = 0x80;
  568. if (used <= 56) {
  569. /* Enough room for padding + length in current block */
  570. memset(ctx->buffer + used, 0, 56 - used);
  571. } else {
  572. /* We'll need an extra block */
  573. memset(ctx->buffer + used, 0, SHA256_BLOCK_SIZE - used);
  574. if ((ret = mbedtls_internal_sha256_process(ctx, ctx->buffer)) != 0) {
  575. return ret;
  576. }
  577. memset(ctx->buffer, 0, 56);
  578. }
  579. /*
  580. * Add message length
  581. */
  582. high = (ctx->total[0] >> 29)
  583. | (ctx->total[1] << 3);
  584. low = (ctx->total[0] << 3);
  585. MBEDTLS_PUT_UINT32_BE(high, ctx->buffer, 56);
  586. MBEDTLS_PUT_UINT32_BE(low, ctx->buffer, 60);
  587. if ((ret = mbedtls_internal_sha256_process(ctx, ctx->buffer)) != 0) {
  588. return ret;
  589. }
  590. /*
  591. * Output final state
  592. */
  593. MBEDTLS_PUT_UINT32_BE(ctx->state[0], output, 0);
  594. MBEDTLS_PUT_UINT32_BE(ctx->state[1], output, 4);
  595. MBEDTLS_PUT_UINT32_BE(ctx->state[2], output, 8);
  596. MBEDTLS_PUT_UINT32_BE(ctx->state[3], output, 12);
  597. MBEDTLS_PUT_UINT32_BE(ctx->state[4], output, 16);
  598. MBEDTLS_PUT_UINT32_BE(ctx->state[5], output, 20);
  599. MBEDTLS_PUT_UINT32_BE(ctx->state[6], output, 24);
  600. int truncated = 0;
  601. #if defined(MBEDTLS_SHA224_C)
  602. truncated = ctx->is224;
  603. #endif
  604. if (!truncated) {
  605. MBEDTLS_PUT_UINT32_BE(ctx->state[7], output, 28);
  606. }
  607. return 0;
  608. }
  609. #endif /* !MBEDTLS_SHA256_ALT */
  610. /*
  611. * output = SHA-256( input buffer )
  612. */
  613. int mbedtls_sha256(const unsigned char *input,
  614. size_t ilen,
  615. unsigned char *output,
  616. int is224)
  617. {
  618. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  619. mbedtls_sha256_context ctx;
  620. #if defined(MBEDTLS_SHA224_C) && defined(MBEDTLS_SHA256_C)
  621. if (is224 != 0 && is224 != 1) {
  622. return MBEDTLS_ERR_SHA256_BAD_INPUT_DATA;
  623. }
  624. #elif defined(MBEDTLS_SHA256_C)
  625. if (is224 != 0) {
  626. return MBEDTLS_ERR_SHA256_BAD_INPUT_DATA;
  627. }
  628. #else /* defined MBEDTLS_SHA224_C only */
  629. if (is224 == 0) {
  630. return MBEDTLS_ERR_SHA256_BAD_INPUT_DATA;
  631. }
  632. #endif
  633. mbedtls_sha256_init(&ctx);
  634. if ((ret = mbedtls_sha256_starts(&ctx, is224)) != 0) {
  635. goto exit;
  636. }
  637. if ((ret = mbedtls_sha256_update(&ctx, input, ilen)) != 0) {
  638. goto exit;
  639. }
  640. if ((ret = mbedtls_sha256_finish(&ctx, output)) != 0) {
  641. goto exit;
  642. }
  643. exit:
  644. mbedtls_sha256_free(&ctx);
  645. return ret;
  646. }
  647. #if defined(MBEDTLS_SELF_TEST)
  648. /*
  649. * FIPS-180-2 test vectors
  650. */
  651. static const unsigned char sha_test_buf[3][57] =
  652. {
  653. { "abc" },
  654. { "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" },
  655. { "" }
  656. };
  657. static const size_t sha_test_buflen[3] =
  658. {
  659. 3, 56, 1000
  660. };
  661. typedef const unsigned char (sha_test_sum_t)[32];
  662. /*
  663. * SHA-224 test vectors
  664. */
  665. #if defined(MBEDTLS_SHA224_C)
  666. static sha_test_sum_t sha224_test_sum[] =
  667. {
  668. { 0x23, 0x09, 0x7D, 0x22, 0x34, 0x05, 0xD8, 0x22,
  669. 0x86, 0x42, 0xA4, 0x77, 0xBD, 0xA2, 0x55, 0xB3,
  670. 0x2A, 0xAD, 0xBC, 0xE4, 0xBD, 0xA0, 0xB3, 0xF7,
  671. 0xE3, 0x6C, 0x9D, 0xA7 },
  672. { 0x75, 0x38, 0x8B, 0x16, 0x51, 0x27, 0x76, 0xCC,
  673. 0x5D, 0xBA, 0x5D, 0xA1, 0xFD, 0x89, 0x01, 0x50,
  674. 0xB0, 0xC6, 0x45, 0x5C, 0xB4, 0xF5, 0x8B, 0x19,
  675. 0x52, 0x52, 0x25, 0x25 },
  676. { 0x20, 0x79, 0x46, 0x55, 0x98, 0x0C, 0x91, 0xD8,
  677. 0xBB, 0xB4, 0xC1, 0xEA, 0x97, 0x61, 0x8A, 0x4B,
  678. 0xF0, 0x3F, 0x42, 0x58, 0x19, 0x48, 0xB2, 0xEE,
  679. 0x4E, 0xE7, 0xAD, 0x67 }
  680. };
  681. #endif
  682. /*
  683. * SHA-256 test vectors
  684. */
  685. #if defined(MBEDTLS_SHA256_C)
  686. static sha_test_sum_t sha256_test_sum[] =
  687. {
  688. { 0xBA, 0x78, 0x16, 0xBF, 0x8F, 0x01, 0xCF, 0xEA,
  689. 0x41, 0x41, 0x40, 0xDE, 0x5D, 0xAE, 0x22, 0x23,
  690. 0xB0, 0x03, 0x61, 0xA3, 0x96, 0x17, 0x7A, 0x9C,
  691. 0xB4, 0x10, 0xFF, 0x61, 0xF2, 0x00, 0x15, 0xAD },
  692. { 0x24, 0x8D, 0x6A, 0x61, 0xD2, 0x06, 0x38, 0xB8,
  693. 0xE5, 0xC0, 0x26, 0x93, 0x0C, 0x3E, 0x60, 0x39,
  694. 0xA3, 0x3C, 0xE4, 0x59, 0x64, 0xFF, 0x21, 0x67,
  695. 0xF6, 0xEC, 0xED, 0xD4, 0x19, 0xDB, 0x06, 0xC1 },
  696. { 0xCD, 0xC7, 0x6E, 0x5C, 0x99, 0x14, 0xFB, 0x92,
  697. 0x81, 0xA1, 0xC7, 0xE2, 0x84, 0xD7, 0x3E, 0x67,
  698. 0xF1, 0x80, 0x9A, 0x48, 0xA4, 0x97, 0x20, 0x0E,
  699. 0x04, 0x6D, 0x39, 0xCC, 0xC7, 0x11, 0x2C, 0xD0 }
  700. };
  701. #endif
  702. /*
  703. * Checkup routine
  704. */
  705. static int mbedtls_sha256_common_self_test(int verbose, int is224)
  706. {
  707. int i, buflen, ret = 0;
  708. unsigned char *buf;
  709. unsigned char sha256sum[32];
  710. mbedtls_sha256_context ctx;
  711. #if defined(MBEDTLS_SHA224_C) && defined(MBEDTLS_SHA256_C)
  712. sha_test_sum_t *sha_test_sum = (is224) ? sha224_test_sum : sha256_test_sum;
  713. #elif defined(MBEDTLS_SHA256_C)
  714. sha_test_sum_t *sha_test_sum = sha256_test_sum;
  715. #else
  716. sha_test_sum_t *sha_test_sum = sha224_test_sum;
  717. #endif
  718. buf = mbedtls_calloc(1024, sizeof(unsigned char));
  719. if (NULL == buf) {
  720. if (verbose != 0) {
  721. mbedtls_printf("Buffer allocation failed\n");
  722. }
  723. return 1;
  724. }
  725. mbedtls_sha256_init(&ctx);
  726. for (i = 0; i < 3; i++) {
  727. if (verbose != 0) {
  728. mbedtls_printf(" SHA-%d test #%d: ", 256 - is224 * 32, i + 1);
  729. }
  730. if ((ret = mbedtls_sha256_starts(&ctx, is224)) != 0) {
  731. goto fail;
  732. }
  733. if (i == 2) {
  734. memset(buf, 'a', buflen = 1000);
  735. for (int j = 0; j < 1000; j++) {
  736. ret = mbedtls_sha256_update(&ctx, buf, buflen);
  737. if (ret != 0) {
  738. goto fail;
  739. }
  740. }
  741. } else {
  742. ret = mbedtls_sha256_update(&ctx, sha_test_buf[i],
  743. sha_test_buflen[i]);
  744. if (ret != 0) {
  745. goto fail;
  746. }
  747. }
  748. if ((ret = mbedtls_sha256_finish(&ctx, sha256sum)) != 0) {
  749. goto fail;
  750. }
  751. if (memcmp(sha256sum, sha_test_sum[i], 32 - is224 * 4) != 0) {
  752. ret = 1;
  753. goto fail;
  754. }
  755. if (verbose != 0) {
  756. mbedtls_printf("passed\n");
  757. }
  758. }
  759. if (verbose != 0) {
  760. mbedtls_printf("\n");
  761. }
  762. goto exit;
  763. fail:
  764. if (verbose != 0) {
  765. mbedtls_printf("failed\n");
  766. }
  767. exit:
  768. mbedtls_sha256_free(&ctx);
  769. mbedtls_free(buf);
  770. return ret;
  771. }
  772. #if defined(MBEDTLS_SHA256_C)
  773. int mbedtls_sha256_self_test(int verbose)
  774. {
  775. return mbedtls_sha256_common_self_test(verbose, 0);
  776. }
  777. #endif /* MBEDTLS_SHA256_C */
  778. #if defined(MBEDTLS_SHA224_C)
  779. int mbedtls_sha224_self_test(int verbose)
  780. {
  781. return mbedtls_sha256_common_self_test(verbose, 1);
  782. }
  783. #endif /* MBEDTLS_SHA224_C */
  784. #endif /* MBEDTLS_SELF_TEST */
  785. #endif /* MBEDTLS_SHA256_C || MBEDTLS_SHA224_C */