sha256.c 29 KB

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