sha512.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108
  1. /*
  2. * FIPS-180-2 compliant SHA-384/512 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-512 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_SHA512) && \
  25. defined(__clang__) && __clang_major__ >= 7
  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_SHA512 1
  37. #define MBEDTLS_ENABLE_ARM_SHA3_EXTENSIONS_COMPILER_FLAG
  38. #endif
  39. #include "common.h"
  40. #if defined(MBEDTLS_SHA512_C) || defined(MBEDTLS_SHA384_C)
  41. #include "mbedtls/sha512.h"
  42. #include "mbedtls/platform_util.h"
  43. #include "mbedtls/error.h"
  44. #if defined(_MSC_VER) || defined(__WATCOMC__)
  45. #define UL64(x) x##ui64
  46. #else
  47. #define UL64(x) x##ULL
  48. #endif
  49. #include <string.h>
  50. #include "mbedtls/platform.h"
  51. #if defined(__aarch64__)
  52. # if defined(MBEDTLS_SHA512_USE_A64_CRYPTO_IF_PRESENT) || \
  53. defined(MBEDTLS_SHA512_USE_A64_CRYPTO_ONLY)
  54. /* *INDENT-OFF* */
  55. /*
  56. * Best performance comes from most recent compilers, with intrinsics and -O3.
  57. * Must compile with -march=armv8.2-a+sha3, but we can't detect armv8.2-a, and
  58. * can't always detect __ARM_FEATURE_SHA512 (notably clang 7-12).
  59. *
  60. * GCC < 8 won't work at all (lacks the sha512 instructions)
  61. * GCC >= 8 uses intrinsics, sets __ARM_FEATURE_SHA512
  62. *
  63. * Clang < 7 won't work at all (lacks the sha512 instructions)
  64. * Clang 7-12 don't have intrinsics (but we work around that with inline
  65. * assembler) or __ARM_FEATURE_SHA512
  66. * Clang == 13.0.0 same as clang 12 (only seen on macOS)
  67. * Clang >= 13.0.1 has __ARM_FEATURE_SHA512 and intrinsics
  68. */
  69. # if !defined(__ARM_FEATURE_SHA512) || defined(MBEDTLS_ENABLE_ARM_SHA3_EXTENSIONS_COMPILER_FLAG)
  70. /* Test Clang first, as it defines __GNUC__ */
  71. # if defined(__clang__)
  72. # if __clang_major__ < 7
  73. # error "A more recent Clang is required for MBEDTLS_SHA512_USE_A64_CRYPTO_*"
  74. # else
  75. # pragma clang attribute push (__attribute__((target("sha3"))), apply_to=function)
  76. # define MBEDTLS_POP_TARGET_PRAGMA
  77. # endif
  78. # elif defined(__GNUC__)
  79. # if __GNUC__ < 8
  80. # error "A more recent GCC is required for MBEDTLS_SHA512_USE_A64_CRYPTO_*"
  81. # else
  82. # pragma GCC push_options
  83. # pragma GCC target ("arch=armv8.2-a+sha3")
  84. # define MBEDTLS_POP_TARGET_PRAGMA
  85. # endif
  86. # else
  87. # error "Only GCC and Clang supported for MBEDTLS_SHA512_USE_A64_CRYPTO_*"
  88. # endif
  89. # endif
  90. /* *INDENT-ON* */
  91. # include <arm_neon.h>
  92. # endif
  93. # if defined(MBEDTLS_SHA512_USE_A64_CRYPTO_IF_PRESENT)
  94. # if defined(__unix__)
  95. # if defined(__linux__)
  96. /* Our preferred method of detection is getauxval() */
  97. # include <sys/auxv.h>
  98. # endif
  99. /* Use SIGILL on Unix, and fall back to it on Linux */
  100. # include <signal.h>
  101. # endif
  102. # endif
  103. #elif defined(_M_ARM64)
  104. # if defined(MBEDTLS_SHA512_USE_A64_CRYPTO_IF_PRESENT) || \
  105. defined(MBEDTLS_SHA512_USE_A64_CRYPTO_ONLY)
  106. # include <arm64_neon.h>
  107. # endif
  108. #else
  109. # undef MBEDTLS_SHA512_USE_A64_CRYPTO_ONLY
  110. # undef MBEDTLS_SHA512_USE_A64_CRYPTO_IF_PRESENT
  111. #endif
  112. #if defined(MBEDTLS_SHA512_USE_A64_CRYPTO_IF_PRESENT)
  113. /*
  114. * Capability detection code comes early, so we can disable
  115. * MBEDTLS_SHA512_USE_A64_CRYPTO_IF_PRESENT if no detection mechanism found
  116. */
  117. #if defined(HWCAP_SHA512)
  118. static int mbedtls_a64_crypto_sha512_determine_support(void)
  119. {
  120. return (getauxval(AT_HWCAP) & HWCAP_SHA512) ? 1 : 0;
  121. }
  122. #elif defined(__APPLE__)
  123. #include <sys/types.h>
  124. #include <sys/sysctl.h>
  125. static int mbedtls_a64_crypto_sha512_determine_support(void)
  126. {
  127. int value = 0;
  128. size_t value_len = sizeof(value);
  129. int ret = sysctlbyname("hw.optional.armv8_2_sha512", &value, &value_len,
  130. NULL, 0);
  131. return ret == 0 && value != 0;
  132. }
  133. #elif defined(_M_ARM64)
  134. /*
  135. * As of March 2022, there don't appear to be any PF_ARM_V8_* flags
  136. * available to pass to IsProcessorFeaturePresent() to check for
  137. * SHA-512 support. So we fall back to the C code only.
  138. */
  139. #if defined(_MSC_VER)
  140. #pragma message "No mechanism to detect A64_CRYPTO found, using C code only"
  141. #else
  142. #warning "No mechanism to detect A64_CRYPTO found, using C code only"
  143. #endif
  144. #elif defined(__unix__) && defined(SIG_SETMASK)
  145. /* Detection with SIGILL, setjmp() and longjmp() */
  146. #include <signal.h>
  147. #include <setjmp.h>
  148. static jmp_buf return_from_sigill;
  149. /*
  150. * A64 SHA512 support detection via SIGILL
  151. */
  152. static void sigill_handler(int signal)
  153. {
  154. (void) signal;
  155. longjmp(return_from_sigill, 1);
  156. }
  157. static int mbedtls_a64_crypto_sha512_determine_support(void)
  158. {
  159. struct sigaction old_action, new_action;
  160. sigset_t old_mask;
  161. if (sigprocmask(0, NULL, &old_mask)) {
  162. return 0;
  163. }
  164. sigemptyset(&new_action.sa_mask);
  165. new_action.sa_flags = 0;
  166. new_action.sa_handler = sigill_handler;
  167. sigaction(SIGILL, &new_action, &old_action);
  168. static int ret = 0;
  169. if (setjmp(return_from_sigill) == 0) { /* First return only */
  170. /* If this traps, we will return a second time from setjmp() with 1 */
  171. asm ("sha512h q0, q0, v0.2d" : : : "v0");
  172. ret = 1;
  173. }
  174. sigaction(SIGILL, &old_action, NULL);
  175. sigprocmask(SIG_SETMASK, &old_mask, NULL);
  176. return ret;
  177. }
  178. #else
  179. #warning "No mechanism to detect A64_CRYPTO found, using C code only"
  180. #undef MBEDTLS_SHA512_USE_A64_CRYPTO_IF_PRESENT
  181. #endif /* HWCAP_SHA512, __APPLE__, __unix__ && SIG_SETMASK */
  182. #endif /* MBEDTLS_SHA512_USE_A64_CRYPTO_IF_PRESENT */
  183. #if !defined(MBEDTLS_SHA512_ALT)
  184. #define SHA512_BLOCK_SIZE 128
  185. #if defined(MBEDTLS_SHA512_SMALLER)
  186. static void sha512_put_uint64_be(uint64_t n, unsigned char *b, uint8_t i)
  187. {
  188. MBEDTLS_PUT_UINT64_BE(n, b, i);
  189. }
  190. #else
  191. #define sha512_put_uint64_be MBEDTLS_PUT_UINT64_BE
  192. #endif /* MBEDTLS_SHA512_SMALLER */
  193. void mbedtls_sha512_init(mbedtls_sha512_context *ctx)
  194. {
  195. memset(ctx, 0, sizeof(mbedtls_sha512_context));
  196. }
  197. void mbedtls_sha512_free(mbedtls_sha512_context *ctx)
  198. {
  199. if (ctx == NULL) {
  200. return;
  201. }
  202. mbedtls_platform_zeroize(ctx, sizeof(mbedtls_sha512_context));
  203. }
  204. void mbedtls_sha512_clone(mbedtls_sha512_context *dst,
  205. const mbedtls_sha512_context *src)
  206. {
  207. *dst = *src;
  208. }
  209. /*
  210. * SHA-512 context setup
  211. */
  212. int mbedtls_sha512_starts(mbedtls_sha512_context *ctx, int is384)
  213. {
  214. #if defined(MBEDTLS_SHA384_C) && defined(MBEDTLS_SHA512_C)
  215. if (is384 != 0 && is384 != 1) {
  216. return MBEDTLS_ERR_SHA512_BAD_INPUT_DATA;
  217. }
  218. #elif defined(MBEDTLS_SHA512_C)
  219. if (is384 != 0) {
  220. return MBEDTLS_ERR_SHA512_BAD_INPUT_DATA;
  221. }
  222. #else /* defined MBEDTLS_SHA384_C only */
  223. if (is384 == 0) {
  224. return MBEDTLS_ERR_SHA512_BAD_INPUT_DATA;
  225. }
  226. #endif
  227. ctx->total[0] = 0;
  228. ctx->total[1] = 0;
  229. if (is384 == 0) {
  230. #if defined(MBEDTLS_SHA512_C)
  231. ctx->state[0] = UL64(0x6A09E667F3BCC908);
  232. ctx->state[1] = UL64(0xBB67AE8584CAA73B);
  233. ctx->state[2] = UL64(0x3C6EF372FE94F82B);
  234. ctx->state[3] = UL64(0xA54FF53A5F1D36F1);
  235. ctx->state[4] = UL64(0x510E527FADE682D1);
  236. ctx->state[5] = UL64(0x9B05688C2B3E6C1F);
  237. ctx->state[6] = UL64(0x1F83D9ABFB41BD6B);
  238. ctx->state[7] = UL64(0x5BE0CD19137E2179);
  239. #endif /* MBEDTLS_SHA512_C */
  240. } else {
  241. #if defined(MBEDTLS_SHA384_C)
  242. ctx->state[0] = UL64(0xCBBB9D5DC1059ED8);
  243. ctx->state[1] = UL64(0x629A292A367CD507);
  244. ctx->state[2] = UL64(0x9159015A3070DD17);
  245. ctx->state[3] = UL64(0x152FECD8F70E5939);
  246. ctx->state[4] = UL64(0x67332667FFC00B31);
  247. ctx->state[5] = UL64(0x8EB44A8768581511);
  248. ctx->state[6] = UL64(0xDB0C2E0D64F98FA7);
  249. ctx->state[7] = UL64(0x47B5481DBEFA4FA4);
  250. #endif /* MBEDTLS_SHA384_C */
  251. }
  252. #if defined(MBEDTLS_SHA384_C)
  253. ctx->is384 = is384;
  254. #endif
  255. return 0;
  256. }
  257. #if !defined(MBEDTLS_SHA512_PROCESS_ALT)
  258. /*
  259. * Round constants
  260. */
  261. static const uint64_t K[80] =
  262. {
  263. UL64(0x428A2F98D728AE22), UL64(0x7137449123EF65CD),
  264. UL64(0xB5C0FBCFEC4D3B2F), UL64(0xE9B5DBA58189DBBC),
  265. UL64(0x3956C25BF348B538), UL64(0x59F111F1B605D019),
  266. UL64(0x923F82A4AF194F9B), UL64(0xAB1C5ED5DA6D8118),
  267. UL64(0xD807AA98A3030242), UL64(0x12835B0145706FBE),
  268. UL64(0x243185BE4EE4B28C), UL64(0x550C7DC3D5FFB4E2),
  269. UL64(0x72BE5D74F27B896F), UL64(0x80DEB1FE3B1696B1),
  270. UL64(0x9BDC06A725C71235), UL64(0xC19BF174CF692694),
  271. UL64(0xE49B69C19EF14AD2), UL64(0xEFBE4786384F25E3),
  272. UL64(0x0FC19DC68B8CD5B5), UL64(0x240CA1CC77AC9C65),
  273. UL64(0x2DE92C6F592B0275), UL64(0x4A7484AA6EA6E483),
  274. UL64(0x5CB0A9DCBD41FBD4), UL64(0x76F988DA831153B5),
  275. UL64(0x983E5152EE66DFAB), UL64(0xA831C66D2DB43210),
  276. UL64(0xB00327C898FB213F), UL64(0xBF597FC7BEEF0EE4),
  277. UL64(0xC6E00BF33DA88FC2), UL64(0xD5A79147930AA725),
  278. UL64(0x06CA6351E003826F), UL64(0x142929670A0E6E70),
  279. UL64(0x27B70A8546D22FFC), UL64(0x2E1B21385C26C926),
  280. UL64(0x4D2C6DFC5AC42AED), UL64(0x53380D139D95B3DF),
  281. UL64(0x650A73548BAF63DE), UL64(0x766A0ABB3C77B2A8),
  282. UL64(0x81C2C92E47EDAEE6), UL64(0x92722C851482353B),
  283. UL64(0xA2BFE8A14CF10364), UL64(0xA81A664BBC423001),
  284. UL64(0xC24B8B70D0F89791), UL64(0xC76C51A30654BE30),
  285. UL64(0xD192E819D6EF5218), UL64(0xD69906245565A910),
  286. UL64(0xF40E35855771202A), UL64(0x106AA07032BBD1B8),
  287. UL64(0x19A4C116B8D2D0C8), UL64(0x1E376C085141AB53),
  288. UL64(0x2748774CDF8EEB99), UL64(0x34B0BCB5E19B48A8),
  289. UL64(0x391C0CB3C5C95A63), UL64(0x4ED8AA4AE3418ACB),
  290. UL64(0x5B9CCA4F7763E373), UL64(0x682E6FF3D6B2B8A3),
  291. UL64(0x748F82EE5DEFB2FC), UL64(0x78A5636F43172F60),
  292. UL64(0x84C87814A1F0AB72), UL64(0x8CC702081A6439EC),
  293. UL64(0x90BEFFFA23631E28), UL64(0xA4506CEBDE82BDE9),
  294. UL64(0xBEF9A3F7B2C67915), UL64(0xC67178F2E372532B),
  295. UL64(0xCA273ECEEA26619C), UL64(0xD186B8C721C0C207),
  296. UL64(0xEADA7DD6CDE0EB1E), UL64(0xF57D4F7FEE6ED178),
  297. UL64(0x06F067AA72176FBA), UL64(0x0A637DC5A2C898A6),
  298. UL64(0x113F9804BEF90DAE), UL64(0x1B710B35131C471B),
  299. UL64(0x28DB77F523047D84), UL64(0x32CAAB7B40C72493),
  300. UL64(0x3C9EBE0A15C9BEBC), UL64(0x431D67C49C100D4C),
  301. UL64(0x4CC5D4BECB3E42B6), UL64(0x597F299CFC657E2A),
  302. UL64(0x5FCB6FAB3AD6FAEC), UL64(0x6C44198C4A475817)
  303. };
  304. #endif
  305. #if defined(MBEDTLS_SHA512_USE_A64_CRYPTO_IF_PRESENT) || \
  306. defined(MBEDTLS_SHA512_USE_A64_CRYPTO_ONLY)
  307. #if defined(MBEDTLS_SHA512_USE_A64_CRYPTO_ONLY)
  308. # define mbedtls_internal_sha512_process_many_a64_crypto mbedtls_internal_sha512_process_many
  309. # define mbedtls_internal_sha512_process_a64_crypto mbedtls_internal_sha512_process
  310. #endif
  311. /* Accelerated SHA-512 implementation originally written by Simon Tatham for PuTTY,
  312. * under the MIT licence; dual-licensed as Apache 2 with his kind permission.
  313. */
  314. #if defined(__clang__) && \
  315. (__clang_major__ < 13 || \
  316. (__clang_major__ == 13 && __clang_minor__ == 0 && __clang_patchlevel__ == 0))
  317. static inline uint64x2_t vsha512su0q_u64(uint64x2_t x, uint64x2_t y)
  318. {
  319. asm ("sha512su0 %0.2D,%1.2D" : "+w" (x) : "w" (y));
  320. return x;
  321. }
  322. static inline uint64x2_t vsha512su1q_u64(uint64x2_t x, uint64x2_t y, uint64x2_t z)
  323. {
  324. asm ("sha512su1 %0.2D,%1.2D,%2.2D" : "+w" (x) : "w" (y), "w" (z));
  325. return x;
  326. }
  327. static inline uint64x2_t vsha512hq_u64(uint64x2_t x, uint64x2_t y, uint64x2_t z)
  328. {
  329. asm ("sha512h %0,%1,%2.2D" : "+w" (x) : "w" (y), "w" (z));
  330. return x;
  331. }
  332. static inline uint64x2_t vsha512h2q_u64(uint64x2_t x, uint64x2_t y, uint64x2_t z)
  333. {
  334. asm ("sha512h2 %0,%1,%2.2D" : "+w" (x) : "w" (y), "w" (z));
  335. return x;
  336. }
  337. #endif /* __clang__ etc */
  338. static size_t mbedtls_internal_sha512_process_many_a64_crypto(
  339. mbedtls_sha512_context *ctx, const uint8_t *msg, size_t len)
  340. {
  341. uint64x2_t ab = vld1q_u64(&ctx->state[0]);
  342. uint64x2_t cd = vld1q_u64(&ctx->state[2]);
  343. uint64x2_t ef = vld1q_u64(&ctx->state[4]);
  344. uint64x2_t gh = vld1q_u64(&ctx->state[6]);
  345. size_t processed = 0;
  346. for (;
  347. len >= SHA512_BLOCK_SIZE;
  348. processed += SHA512_BLOCK_SIZE,
  349. msg += SHA512_BLOCK_SIZE,
  350. len -= SHA512_BLOCK_SIZE) {
  351. uint64x2_t initial_sum, sum, intermed;
  352. uint64x2_t ab_orig = ab;
  353. uint64x2_t cd_orig = cd;
  354. uint64x2_t ef_orig = ef;
  355. uint64x2_t gh_orig = gh;
  356. uint64x2_t s0 = (uint64x2_t) vld1q_u8(msg + 16 * 0);
  357. uint64x2_t s1 = (uint64x2_t) vld1q_u8(msg + 16 * 1);
  358. uint64x2_t s2 = (uint64x2_t) vld1q_u8(msg + 16 * 2);
  359. uint64x2_t s3 = (uint64x2_t) vld1q_u8(msg + 16 * 3);
  360. uint64x2_t s4 = (uint64x2_t) vld1q_u8(msg + 16 * 4);
  361. uint64x2_t s5 = (uint64x2_t) vld1q_u8(msg + 16 * 5);
  362. uint64x2_t s6 = (uint64x2_t) vld1q_u8(msg + 16 * 6);
  363. uint64x2_t s7 = (uint64x2_t) vld1q_u8(msg + 16 * 7);
  364. #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ /* assume LE if these not defined; untested on BE */
  365. s0 = vreinterpretq_u64_u8(vrev64q_u8(vreinterpretq_u8_u64(s0)));
  366. s1 = vreinterpretq_u64_u8(vrev64q_u8(vreinterpretq_u8_u64(s1)));
  367. s2 = vreinterpretq_u64_u8(vrev64q_u8(vreinterpretq_u8_u64(s2)));
  368. s3 = vreinterpretq_u64_u8(vrev64q_u8(vreinterpretq_u8_u64(s3)));
  369. s4 = vreinterpretq_u64_u8(vrev64q_u8(vreinterpretq_u8_u64(s4)));
  370. s5 = vreinterpretq_u64_u8(vrev64q_u8(vreinterpretq_u8_u64(s5)));
  371. s6 = vreinterpretq_u64_u8(vrev64q_u8(vreinterpretq_u8_u64(s6)));
  372. s7 = vreinterpretq_u64_u8(vrev64q_u8(vreinterpretq_u8_u64(s7)));
  373. #endif
  374. /* Rounds 0 and 1 */
  375. initial_sum = vaddq_u64(s0, vld1q_u64(&K[0]));
  376. sum = vaddq_u64(vextq_u64(initial_sum, initial_sum, 1), gh);
  377. intermed = vsha512hq_u64(sum, vextq_u64(ef, gh, 1), vextq_u64(cd, ef, 1));
  378. gh = vsha512h2q_u64(intermed, cd, ab);
  379. cd = vaddq_u64(cd, intermed);
  380. /* Rounds 2 and 3 */
  381. initial_sum = vaddq_u64(s1, vld1q_u64(&K[2]));
  382. sum = vaddq_u64(vextq_u64(initial_sum, initial_sum, 1), ef);
  383. intermed = vsha512hq_u64(sum, vextq_u64(cd, ef, 1), vextq_u64(ab, cd, 1));
  384. ef = vsha512h2q_u64(intermed, ab, gh);
  385. ab = vaddq_u64(ab, intermed);
  386. /* Rounds 4 and 5 */
  387. initial_sum = vaddq_u64(s2, vld1q_u64(&K[4]));
  388. sum = vaddq_u64(vextq_u64(initial_sum, initial_sum, 1), cd);
  389. intermed = vsha512hq_u64(sum, vextq_u64(ab, cd, 1), vextq_u64(gh, ab, 1));
  390. cd = vsha512h2q_u64(intermed, gh, ef);
  391. gh = vaddq_u64(gh, intermed);
  392. /* Rounds 6 and 7 */
  393. initial_sum = vaddq_u64(s3, vld1q_u64(&K[6]));
  394. sum = vaddq_u64(vextq_u64(initial_sum, initial_sum, 1), ab);
  395. intermed = vsha512hq_u64(sum, vextq_u64(gh, ab, 1), vextq_u64(ef, gh, 1));
  396. ab = vsha512h2q_u64(intermed, ef, cd);
  397. ef = vaddq_u64(ef, intermed);
  398. /* Rounds 8 and 9 */
  399. initial_sum = vaddq_u64(s4, vld1q_u64(&K[8]));
  400. sum = vaddq_u64(vextq_u64(initial_sum, initial_sum, 1), gh);
  401. intermed = vsha512hq_u64(sum, vextq_u64(ef, gh, 1), vextq_u64(cd, ef, 1));
  402. gh = vsha512h2q_u64(intermed, cd, ab);
  403. cd = vaddq_u64(cd, intermed);
  404. /* Rounds 10 and 11 */
  405. initial_sum = vaddq_u64(s5, vld1q_u64(&K[10]));
  406. sum = vaddq_u64(vextq_u64(initial_sum, initial_sum, 1), ef);
  407. intermed = vsha512hq_u64(sum, vextq_u64(cd, ef, 1), vextq_u64(ab, cd, 1));
  408. ef = vsha512h2q_u64(intermed, ab, gh);
  409. ab = vaddq_u64(ab, intermed);
  410. /* Rounds 12 and 13 */
  411. initial_sum = vaddq_u64(s6, vld1q_u64(&K[12]));
  412. sum = vaddq_u64(vextq_u64(initial_sum, initial_sum, 1), cd);
  413. intermed = vsha512hq_u64(sum, vextq_u64(ab, cd, 1), vextq_u64(gh, ab, 1));
  414. cd = vsha512h2q_u64(intermed, gh, ef);
  415. gh = vaddq_u64(gh, intermed);
  416. /* Rounds 14 and 15 */
  417. initial_sum = vaddq_u64(s7, vld1q_u64(&K[14]));
  418. sum = vaddq_u64(vextq_u64(initial_sum, initial_sum, 1), ab);
  419. intermed = vsha512hq_u64(sum, vextq_u64(gh, ab, 1), vextq_u64(ef, gh, 1));
  420. ab = vsha512h2q_u64(intermed, ef, cd);
  421. ef = vaddq_u64(ef, intermed);
  422. for (unsigned int t = 16; t < 80; t += 16) {
  423. /* Rounds t and t + 1 */
  424. s0 = vsha512su1q_u64(vsha512su0q_u64(s0, s1), s7, vextq_u64(s4, s5, 1));
  425. initial_sum = vaddq_u64(s0, vld1q_u64(&K[t]));
  426. sum = vaddq_u64(vextq_u64(initial_sum, initial_sum, 1), gh);
  427. intermed = vsha512hq_u64(sum, vextq_u64(ef, gh, 1), vextq_u64(cd, ef, 1));
  428. gh = vsha512h2q_u64(intermed, cd, ab);
  429. cd = vaddq_u64(cd, intermed);
  430. /* Rounds t + 2 and t + 3 */
  431. s1 = vsha512su1q_u64(vsha512su0q_u64(s1, s2), s0, vextq_u64(s5, s6, 1));
  432. initial_sum = vaddq_u64(s1, vld1q_u64(&K[t + 2]));
  433. sum = vaddq_u64(vextq_u64(initial_sum, initial_sum, 1), ef);
  434. intermed = vsha512hq_u64(sum, vextq_u64(cd, ef, 1), vextq_u64(ab, cd, 1));
  435. ef = vsha512h2q_u64(intermed, ab, gh);
  436. ab = vaddq_u64(ab, intermed);
  437. /* Rounds t + 4 and t + 5 */
  438. s2 = vsha512su1q_u64(vsha512su0q_u64(s2, s3), s1, vextq_u64(s6, s7, 1));
  439. initial_sum = vaddq_u64(s2, vld1q_u64(&K[t + 4]));
  440. sum = vaddq_u64(vextq_u64(initial_sum, initial_sum, 1), cd);
  441. intermed = vsha512hq_u64(sum, vextq_u64(ab, cd, 1), vextq_u64(gh, ab, 1));
  442. cd = vsha512h2q_u64(intermed, gh, ef);
  443. gh = vaddq_u64(gh, intermed);
  444. /* Rounds t + 6 and t + 7 */
  445. s3 = vsha512su1q_u64(vsha512su0q_u64(s3, s4), s2, vextq_u64(s7, s0, 1));
  446. initial_sum = vaddq_u64(s3, vld1q_u64(&K[t + 6]));
  447. sum = vaddq_u64(vextq_u64(initial_sum, initial_sum, 1), ab);
  448. intermed = vsha512hq_u64(sum, vextq_u64(gh, ab, 1), vextq_u64(ef, gh, 1));
  449. ab = vsha512h2q_u64(intermed, ef, cd);
  450. ef = vaddq_u64(ef, intermed);
  451. /* Rounds t + 8 and t + 9 */
  452. s4 = vsha512su1q_u64(vsha512su0q_u64(s4, s5), s3, vextq_u64(s0, s1, 1));
  453. initial_sum = vaddq_u64(s4, vld1q_u64(&K[t + 8]));
  454. sum = vaddq_u64(vextq_u64(initial_sum, initial_sum, 1), gh);
  455. intermed = vsha512hq_u64(sum, vextq_u64(ef, gh, 1), vextq_u64(cd, ef, 1));
  456. gh = vsha512h2q_u64(intermed, cd, ab);
  457. cd = vaddq_u64(cd, intermed);
  458. /* Rounds t + 10 and t + 11 */
  459. s5 = vsha512su1q_u64(vsha512su0q_u64(s5, s6), s4, vextq_u64(s1, s2, 1));
  460. initial_sum = vaddq_u64(s5, vld1q_u64(&K[t + 10]));
  461. sum = vaddq_u64(vextq_u64(initial_sum, initial_sum, 1), ef);
  462. intermed = vsha512hq_u64(sum, vextq_u64(cd, ef, 1), vextq_u64(ab, cd, 1));
  463. ef = vsha512h2q_u64(intermed, ab, gh);
  464. ab = vaddq_u64(ab, intermed);
  465. /* Rounds t + 12 and t + 13 */
  466. s6 = vsha512su1q_u64(vsha512su0q_u64(s6, s7), s5, vextq_u64(s2, s3, 1));
  467. initial_sum = vaddq_u64(s6, vld1q_u64(&K[t + 12]));
  468. sum = vaddq_u64(vextq_u64(initial_sum, initial_sum, 1), cd);
  469. intermed = vsha512hq_u64(sum, vextq_u64(ab, cd, 1), vextq_u64(gh, ab, 1));
  470. cd = vsha512h2q_u64(intermed, gh, ef);
  471. gh = vaddq_u64(gh, intermed);
  472. /* Rounds t + 14 and t + 15 */
  473. s7 = vsha512su1q_u64(vsha512su0q_u64(s7, s0), s6, vextq_u64(s3, s4, 1));
  474. initial_sum = vaddq_u64(s7, vld1q_u64(&K[t + 14]));
  475. sum = vaddq_u64(vextq_u64(initial_sum, initial_sum, 1), ab);
  476. intermed = vsha512hq_u64(sum, vextq_u64(gh, ab, 1), vextq_u64(ef, gh, 1));
  477. ab = vsha512h2q_u64(intermed, ef, cd);
  478. ef = vaddq_u64(ef, intermed);
  479. }
  480. ab = vaddq_u64(ab, ab_orig);
  481. cd = vaddq_u64(cd, cd_orig);
  482. ef = vaddq_u64(ef, ef_orig);
  483. gh = vaddq_u64(gh, gh_orig);
  484. }
  485. vst1q_u64(&ctx->state[0], ab);
  486. vst1q_u64(&ctx->state[2], cd);
  487. vst1q_u64(&ctx->state[4], ef);
  488. vst1q_u64(&ctx->state[6], gh);
  489. return processed;
  490. }
  491. #if defined(MBEDTLS_SHA512_USE_A64_CRYPTO_IF_PRESENT)
  492. /*
  493. * This function is for internal use only if we are building both C and A64
  494. * versions, otherwise it is renamed to be the public mbedtls_internal_sha512_process()
  495. */
  496. static
  497. #endif
  498. int mbedtls_internal_sha512_process_a64_crypto(mbedtls_sha512_context *ctx,
  499. const unsigned char data[SHA512_BLOCK_SIZE])
  500. {
  501. return (mbedtls_internal_sha512_process_many_a64_crypto(ctx, data,
  502. SHA512_BLOCK_SIZE) ==
  503. SHA512_BLOCK_SIZE) ? 0 : -1;
  504. }
  505. #if defined(MBEDTLS_POP_TARGET_PRAGMA)
  506. #if defined(__clang__)
  507. #pragma clang attribute pop
  508. #elif defined(__GNUC__)
  509. #pragma GCC pop_options
  510. #endif
  511. #undef MBEDTLS_POP_TARGET_PRAGMA
  512. #endif
  513. #endif /* MBEDTLS_SHA512_USE_A64_CRYPTO_IF_PRESENT || MBEDTLS_SHA512_USE_A64_CRYPTO_ONLY */
  514. #if !defined(MBEDTLS_SHA512_USE_A64_CRYPTO_IF_PRESENT)
  515. #define mbedtls_internal_sha512_process_many_c mbedtls_internal_sha512_process_many
  516. #define mbedtls_internal_sha512_process_c mbedtls_internal_sha512_process
  517. #endif
  518. #if !defined(MBEDTLS_SHA512_PROCESS_ALT) && !defined(MBEDTLS_SHA512_USE_A64_CRYPTO_ONLY)
  519. #if defined(MBEDTLS_SHA512_USE_A64_CRYPTO_IF_PRESENT)
  520. /*
  521. * This function is for internal use only if we are building both C and A64
  522. * versions, otherwise it is renamed to be the public mbedtls_internal_sha512_process()
  523. */
  524. static
  525. #endif
  526. int mbedtls_internal_sha512_process_c(mbedtls_sha512_context *ctx,
  527. const unsigned char data[SHA512_BLOCK_SIZE])
  528. {
  529. int i;
  530. struct {
  531. uint64_t temp1, temp2, W[80];
  532. uint64_t A[8];
  533. } local;
  534. #define SHR(x, n) ((x) >> (n))
  535. #define ROTR(x, n) (SHR((x), (n)) | ((x) << (64 - (n))))
  536. #define S0(x) (ROTR(x, 1) ^ ROTR(x, 8) ^ SHR(x, 7))
  537. #define S1(x) (ROTR(x, 19) ^ ROTR(x, 61) ^ SHR(x, 6))
  538. #define S2(x) (ROTR(x, 28) ^ ROTR(x, 34) ^ ROTR(x, 39))
  539. #define S3(x) (ROTR(x, 14) ^ ROTR(x, 18) ^ ROTR(x, 41))
  540. #define F0(x, y, z) (((x) & (y)) | ((z) & ((x) | (y))))
  541. #define F1(x, y, z) ((z) ^ ((x) & ((y) ^ (z))))
  542. #define P(a, b, c, d, e, f, g, h, x, K) \
  543. do \
  544. { \
  545. local.temp1 = (h) + S3(e) + F1((e), (f), (g)) + (K) + (x); \
  546. local.temp2 = S2(a) + F0((a), (b), (c)); \
  547. (d) += local.temp1; (h) = local.temp1 + local.temp2; \
  548. } while (0)
  549. for (i = 0; i < 8; i++) {
  550. local.A[i] = ctx->state[i];
  551. }
  552. #if defined(MBEDTLS_SHA512_SMALLER)
  553. for (i = 0; i < 80; i++) {
  554. if (i < 16) {
  555. local.W[i] = MBEDTLS_GET_UINT64_BE(data, i << 3);
  556. } else {
  557. local.W[i] = S1(local.W[i - 2]) + local.W[i - 7] +
  558. S0(local.W[i - 15]) + local.W[i - 16];
  559. }
  560. P(local.A[0], local.A[1], local.A[2], local.A[3], local.A[4],
  561. local.A[5], local.A[6], local.A[7], local.W[i], K[i]);
  562. local.temp1 = local.A[7]; local.A[7] = local.A[6];
  563. local.A[6] = local.A[5]; local.A[5] = local.A[4];
  564. local.A[4] = local.A[3]; local.A[3] = local.A[2];
  565. local.A[2] = local.A[1]; local.A[1] = local.A[0];
  566. local.A[0] = local.temp1;
  567. }
  568. #else /* MBEDTLS_SHA512_SMALLER */
  569. for (i = 0; i < 16; i++) {
  570. local.W[i] = MBEDTLS_GET_UINT64_BE(data, i << 3);
  571. }
  572. for (; i < 80; i++) {
  573. local.W[i] = S1(local.W[i - 2]) + local.W[i - 7] +
  574. S0(local.W[i - 15]) + local.W[i - 16];
  575. }
  576. i = 0;
  577. do {
  578. P(local.A[0], local.A[1], local.A[2], local.A[3], local.A[4],
  579. local.A[5], local.A[6], local.A[7], local.W[i], K[i]); i++;
  580. P(local.A[7], local.A[0], local.A[1], local.A[2], local.A[3],
  581. local.A[4], local.A[5], local.A[6], local.W[i], K[i]); i++;
  582. P(local.A[6], local.A[7], local.A[0], local.A[1], local.A[2],
  583. local.A[3], local.A[4], local.A[5], local.W[i], K[i]); i++;
  584. P(local.A[5], local.A[6], local.A[7], local.A[0], local.A[1],
  585. local.A[2], local.A[3], local.A[4], local.W[i], K[i]); i++;
  586. P(local.A[4], local.A[5], local.A[6], local.A[7], local.A[0],
  587. local.A[1], local.A[2], local.A[3], local.W[i], K[i]); i++;
  588. P(local.A[3], local.A[4], local.A[5], local.A[6], local.A[7],
  589. local.A[0], local.A[1], local.A[2], local.W[i], K[i]); i++;
  590. P(local.A[2], local.A[3], local.A[4], local.A[5], local.A[6],
  591. local.A[7], local.A[0], local.A[1], local.W[i], K[i]); i++;
  592. P(local.A[1], local.A[2], local.A[3], local.A[4], local.A[5],
  593. local.A[6], local.A[7], local.A[0], local.W[i], K[i]); i++;
  594. } while (i < 80);
  595. #endif /* MBEDTLS_SHA512_SMALLER */
  596. for (i = 0; i < 8; i++) {
  597. ctx->state[i] += local.A[i];
  598. }
  599. /* Zeroise buffers and variables to clear sensitive data from memory. */
  600. mbedtls_platform_zeroize(&local, sizeof(local));
  601. return 0;
  602. }
  603. #endif /* !MBEDTLS_SHA512_PROCESS_ALT && !MBEDTLS_SHA512_USE_A64_CRYPTO_ONLY */
  604. #if !defined(MBEDTLS_SHA512_USE_A64_CRYPTO_ONLY)
  605. static size_t mbedtls_internal_sha512_process_many_c(
  606. mbedtls_sha512_context *ctx, const uint8_t *data, size_t len)
  607. {
  608. size_t processed = 0;
  609. while (len >= SHA512_BLOCK_SIZE) {
  610. if (mbedtls_internal_sha512_process_c(ctx, data) != 0) {
  611. return 0;
  612. }
  613. data += SHA512_BLOCK_SIZE;
  614. len -= SHA512_BLOCK_SIZE;
  615. processed += SHA512_BLOCK_SIZE;
  616. }
  617. return processed;
  618. }
  619. #endif /* !MBEDTLS_SHA512_USE_A64_CRYPTO_ONLY */
  620. #if defined(MBEDTLS_SHA512_USE_A64_CRYPTO_IF_PRESENT)
  621. static int mbedtls_a64_crypto_sha512_has_support(void)
  622. {
  623. static int done = 0;
  624. static int supported = 0;
  625. if (!done) {
  626. supported = mbedtls_a64_crypto_sha512_determine_support();
  627. done = 1;
  628. }
  629. return supported;
  630. }
  631. static size_t mbedtls_internal_sha512_process_many(mbedtls_sha512_context *ctx,
  632. const uint8_t *msg, size_t len)
  633. {
  634. if (mbedtls_a64_crypto_sha512_has_support()) {
  635. return mbedtls_internal_sha512_process_many_a64_crypto(ctx, msg, len);
  636. } else {
  637. return mbedtls_internal_sha512_process_many_c(ctx, msg, len);
  638. }
  639. }
  640. int mbedtls_internal_sha512_process(mbedtls_sha512_context *ctx,
  641. const unsigned char data[SHA512_BLOCK_SIZE])
  642. {
  643. if (mbedtls_a64_crypto_sha512_has_support()) {
  644. return mbedtls_internal_sha512_process_a64_crypto(ctx, data);
  645. } else {
  646. return mbedtls_internal_sha512_process_c(ctx, data);
  647. }
  648. }
  649. #endif /* MBEDTLS_SHA512_USE_A64_CRYPTO_IF_PRESENT */
  650. /*
  651. * SHA-512 process buffer
  652. */
  653. int mbedtls_sha512_update(mbedtls_sha512_context *ctx,
  654. const unsigned char *input,
  655. size_t ilen)
  656. {
  657. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  658. size_t fill;
  659. unsigned int left;
  660. if (ilen == 0) {
  661. return 0;
  662. }
  663. left = (unsigned int) (ctx->total[0] & 0x7F);
  664. fill = SHA512_BLOCK_SIZE - left;
  665. ctx->total[0] += (uint64_t) ilen;
  666. if (ctx->total[0] < (uint64_t) ilen) {
  667. ctx->total[1]++;
  668. }
  669. if (left && ilen >= fill) {
  670. memcpy((void *) (ctx->buffer + left), input, fill);
  671. if ((ret = mbedtls_internal_sha512_process(ctx, ctx->buffer)) != 0) {
  672. return ret;
  673. }
  674. input += fill;
  675. ilen -= fill;
  676. left = 0;
  677. }
  678. while (ilen >= SHA512_BLOCK_SIZE) {
  679. size_t processed =
  680. mbedtls_internal_sha512_process_many(ctx, input, ilen);
  681. if (processed < SHA512_BLOCK_SIZE) {
  682. return MBEDTLS_ERR_ERROR_GENERIC_ERROR;
  683. }
  684. input += processed;
  685. ilen -= processed;
  686. }
  687. if (ilen > 0) {
  688. memcpy((void *) (ctx->buffer + left), input, ilen);
  689. }
  690. return 0;
  691. }
  692. /*
  693. * SHA-512 final digest
  694. */
  695. int mbedtls_sha512_finish(mbedtls_sha512_context *ctx,
  696. unsigned char *output)
  697. {
  698. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  699. unsigned used;
  700. uint64_t high, low;
  701. /*
  702. * Add padding: 0x80 then 0x00 until 16 bytes remain for the length
  703. */
  704. used = ctx->total[0] & 0x7F;
  705. ctx->buffer[used++] = 0x80;
  706. if (used <= 112) {
  707. /* Enough room for padding + length in current block */
  708. memset(ctx->buffer + used, 0, 112 - used);
  709. } else {
  710. /* We'll need an extra block */
  711. memset(ctx->buffer + used, 0, SHA512_BLOCK_SIZE - used);
  712. if ((ret = mbedtls_internal_sha512_process(ctx, ctx->buffer)) != 0) {
  713. return ret;
  714. }
  715. memset(ctx->buffer, 0, 112);
  716. }
  717. /*
  718. * Add message length
  719. */
  720. high = (ctx->total[0] >> 61)
  721. | (ctx->total[1] << 3);
  722. low = (ctx->total[0] << 3);
  723. sha512_put_uint64_be(high, ctx->buffer, 112);
  724. sha512_put_uint64_be(low, ctx->buffer, 120);
  725. if ((ret = mbedtls_internal_sha512_process(ctx, ctx->buffer)) != 0) {
  726. return ret;
  727. }
  728. /*
  729. * Output final state
  730. */
  731. sha512_put_uint64_be(ctx->state[0], output, 0);
  732. sha512_put_uint64_be(ctx->state[1], output, 8);
  733. sha512_put_uint64_be(ctx->state[2], output, 16);
  734. sha512_put_uint64_be(ctx->state[3], output, 24);
  735. sha512_put_uint64_be(ctx->state[4], output, 32);
  736. sha512_put_uint64_be(ctx->state[5], output, 40);
  737. int truncated = 0;
  738. #if defined(MBEDTLS_SHA384_C)
  739. truncated = ctx->is384;
  740. #endif
  741. if (!truncated) {
  742. sha512_put_uint64_be(ctx->state[6], output, 48);
  743. sha512_put_uint64_be(ctx->state[7], output, 56);
  744. }
  745. return 0;
  746. }
  747. #endif /* !MBEDTLS_SHA512_ALT */
  748. /*
  749. * output = SHA-512( input buffer )
  750. */
  751. int mbedtls_sha512(const unsigned char *input,
  752. size_t ilen,
  753. unsigned char *output,
  754. int is384)
  755. {
  756. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  757. mbedtls_sha512_context ctx;
  758. #if defined(MBEDTLS_SHA384_C) && defined(MBEDTLS_SHA512_C)
  759. if (is384 != 0 && is384 != 1) {
  760. return MBEDTLS_ERR_SHA512_BAD_INPUT_DATA;
  761. }
  762. #elif defined(MBEDTLS_SHA512_C)
  763. if (is384 != 0) {
  764. return MBEDTLS_ERR_SHA512_BAD_INPUT_DATA;
  765. }
  766. #else /* defined MBEDTLS_SHA384_C only */
  767. if (is384 == 0) {
  768. return MBEDTLS_ERR_SHA512_BAD_INPUT_DATA;
  769. }
  770. #endif
  771. mbedtls_sha512_init(&ctx);
  772. if ((ret = mbedtls_sha512_starts(&ctx, is384)) != 0) {
  773. goto exit;
  774. }
  775. if ((ret = mbedtls_sha512_update(&ctx, input, ilen)) != 0) {
  776. goto exit;
  777. }
  778. if ((ret = mbedtls_sha512_finish(&ctx, output)) != 0) {
  779. goto exit;
  780. }
  781. exit:
  782. mbedtls_sha512_free(&ctx);
  783. return ret;
  784. }
  785. #if defined(MBEDTLS_SELF_TEST)
  786. /*
  787. * FIPS-180-2 test vectors
  788. */
  789. static const unsigned char sha_test_buf[3][113] =
  790. {
  791. { "abc" },
  792. {
  793. "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu"
  794. },
  795. { "" }
  796. };
  797. static const size_t sha_test_buflen[3] =
  798. {
  799. 3, 112, 1000
  800. };
  801. typedef const unsigned char (sha_test_sum_t)[64];
  802. /*
  803. * SHA-384 test vectors
  804. */
  805. #if defined(MBEDTLS_SHA384_C)
  806. static sha_test_sum_t sha384_test_sum[] =
  807. {
  808. { 0xCB, 0x00, 0x75, 0x3F, 0x45, 0xA3, 0x5E, 0x8B,
  809. 0xB5, 0xA0, 0x3D, 0x69, 0x9A, 0xC6, 0x50, 0x07,
  810. 0x27, 0x2C, 0x32, 0xAB, 0x0E, 0xDE, 0xD1, 0x63,
  811. 0x1A, 0x8B, 0x60, 0x5A, 0x43, 0xFF, 0x5B, 0xED,
  812. 0x80, 0x86, 0x07, 0x2B, 0xA1, 0xE7, 0xCC, 0x23,
  813. 0x58, 0xBA, 0xEC, 0xA1, 0x34, 0xC8, 0x25, 0xA7 },
  814. { 0x09, 0x33, 0x0C, 0x33, 0xF7, 0x11, 0x47, 0xE8,
  815. 0x3D, 0x19, 0x2F, 0xC7, 0x82, 0xCD, 0x1B, 0x47,
  816. 0x53, 0x11, 0x1B, 0x17, 0x3B, 0x3B, 0x05, 0xD2,
  817. 0x2F, 0xA0, 0x80, 0x86, 0xE3, 0xB0, 0xF7, 0x12,
  818. 0xFC, 0xC7, 0xC7, 0x1A, 0x55, 0x7E, 0x2D, 0xB9,
  819. 0x66, 0xC3, 0xE9, 0xFA, 0x91, 0x74, 0x60, 0x39 },
  820. { 0x9D, 0x0E, 0x18, 0x09, 0x71, 0x64, 0x74, 0xCB,
  821. 0x08, 0x6E, 0x83, 0x4E, 0x31, 0x0A, 0x4A, 0x1C,
  822. 0xED, 0x14, 0x9E, 0x9C, 0x00, 0xF2, 0x48, 0x52,
  823. 0x79, 0x72, 0xCE, 0xC5, 0x70, 0x4C, 0x2A, 0x5B,
  824. 0x07, 0xB8, 0xB3, 0xDC, 0x38, 0xEC, 0xC4, 0xEB,
  825. 0xAE, 0x97, 0xDD, 0xD8, 0x7F, 0x3D, 0x89, 0x85 }
  826. };
  827. #endif /* MBEDTLS_SHA384_C */
  828. /*
  829. * SHA-512 test vectors
  830. */
  831. #if defined(MBEDTLS_SHA512_C)
  832. static sha_test_sum_t sha512_test_sum[] =
  833. {
  834. { 0xDD, 0xAF, 0x35, 0xA1, 0x93, 0x61, 0x7A, 0xBA,
  835. 0xCC, 0x41, 0x73, 0x49, 0xAE, 0x20, 0x41, 0x31,
  836. 0x12, 0xE6, 0xFA, 0x4E, 0x89, 0xA9, 0x7E, 0xA2,
  837. 0x0A, 0x9E, 0xEE, 0xE6, 0x4B, 0x55, 0xD3, 0x9A,
  838. 0x21, 0x92, 0x99, 0x2A, 0x27, 0x4F, 0xC1, 0xA8,
  839. 0x36, 0xBA, 0x3C, 0x23, 0xA3, 0xFE, 0xEB, 0xBD,
  840. 0x45, 0x4D, 0x44, 0x23, 0x64, 0x3C, 0xE8, 0x0E,
  841. 0x2A, 0x9A, 0xC9, 0x4F, 0xA5, 0x4C, 0xA4, 0x9F },
  842. { 0x8E, 0x95, 0x9B, 0x75, 0xDA, 0xE3, 0x13, 0xDA,
  843. 0x8C, 0xF4, 0xF7, 0x28, 0x14, 0xFC, 0x14, 0x3F,
  844. 0x8F, 0x77, 0x79, 0xC6, 0xEB, 0x9F, 0x7F, 0xA1,
  845. 0x72, 0x99, 0xAE, 0xAD, 0xB6, 0x88, 0x90, 0x18,
  846. 0x50, 0x1D, 0x28, 0x9E, 0x49, 0x00, 0xF7, 0xE4,
  847. 0x33, 0x1B, 0x99, 0xDE, 0xC4, 0xB5, 0x43, 0x3A,
  848. 0xC7, 0xD3, 0x29, 0xEE, 0xB6, 0xDD, 0x26, 0x54,
  849. 0x5E, 0x96, 0xE5, 0x5B, 0x87, 0x4B, 0xE9, 0x09 },
  850. { 0xE7, 0x18, 0x48, 0x3D, 0x0C, 0xE7, 0x69, 0x64,
  851. 0x4E, 0x2E, 0x42, 0xC7, 0xBC, 0x15, 0xB4, 0x63,
  852. 0x8E, 0x1F, 0x98, 0xB1, 0x3B, 0x20, 0x44, 0x28,
  853. 0x56, 0x32, 0xA8, 0x03, 0xAF, 0xA9, 0x73, 0xEB,
  854. 0xDE, 0x0F, 0xF2, 0x44, 0x87, 0x7E, 0xA6, 0x0A,
  855. 0x4C, 0xB0, 0x43, 0x2C, 0xE5, 0x77, 0xC3, 0x1B,
  856. 0xEB, 0x00, 0x9C, 0x5C, 0x2C, 0x49, 0xAA, 0x2E,
  857. 0x4E, 0xAD, 0xB2, 0x17, 0xAD, 0x8C, 0xC0, 0x9B }
  858. };
  859. #endif /* MBEDTLS_SHA512_C */
  860. #define ARRAY_LENGTH(a) (sizeof(a) / sizeof((a)[0]))
  861. static int mbedtls_sha512_common_self_test(int verbose, int is384)
  862. {
  863. int i, buflen, ret = 0;
  864. unsigned char *buf;
  865. unsigned char sha512sum[64];
  866. mbedtls_sha512_context ctx;
  867. #if defined(MBEDTLS_SHA384_C) && defined(MBEDTLS_SHA512_C)
  868. sha_test_sum_t *sha_test_sum = (is384) ? sha384_test_sum : sha512_test_sum;
  869. #elif defined(MBEDTLS_SHA512_C)
  870. sha_test_sum_t *sha_test_sum = sha512_test_sum;
  871. #else
  872. sha_test_sum_t *sha_test_sum = sha384_test_sum;
  873. #endif
  874. buf = mbedtls_calloc(1024, sizeof(unsigned char));
  875. if (NULL == buf) {
  876. if (verbose != 0) {
  877. mbedtls_printf("Buffer allocation failed\n");
  878. }
  879. return 1;
  880. }
  881. mbedtls_sha512_init(&ctx);
  882. for (i = 0; i < 3; i++) {
  883. if (verbose != 0) {
  884. mbedtls_printf(" SHA-%d test #%d: ", 512 - is384 * 128, i + 1);
  885. }
  886. if ((ret = mbedtls_sha512_starts(&ctx, is384)) != 0) {
  887. goto fail;
  888. }
  889. if (i == 2) {
  890. memset(buf, 'a', buflen = 1000);
  891. for (int j = 0; j < 1000; j++) {
  892. ret = mbedtls_sha512_update(&ctx, buf, buflen);
  893. if (ret != 0) {
  894. goto fail;
  895. }
  896. }
  897. } else {
  898. ret = mbedtls_sha512_update(&ctx, sha_test_buf[i],
  899. sha_test_buflen[i]);
  900. if (ret != 0) {
  901. goto fail;
  902. }
  903. }
  904. if ((ret = mbedtls_sha512_finish(&ctx, sha512sum)) != 0) {
  905. goto fail;
  906. }
  907. if (memcmp(sha512sum, sha_test_sum[i], 64 - is384 * 16) != 0) {
  908. ret = 1;
  909. goto fail;
  910. }
  911. if (verbose != 0) {
  912. mbedtls_printf("passed\n");
  913. }
  914. }
  915. if (verbose != 0) {
  916. mbedtls_printf("\n");
  917. }
  918. goto exit;
  919. fail:
  920. if (verbose != 0) {
  921. mbedtls_printf("failed\n");
  922. }
  923. exit:
  924. mbedtls_sha512_free(&ctx);
  925. mbedtls_free(buf);
  926. return ret;
  927. }
  928. #if defined(MBEDTLS_SHA512_C)
  929. int mbedtls_sha512_self_test(int verbose)
  930. {
  931. return mbedtls_sha512_common_self_test(verbose, 0);
  932. }
  933. #endif /* MBEDTLS_SHA512_C */
  934. #if defined(MBEDTLS_SHA384_C)
  935. int mbedtls_sha384_self_test(int verbose)
  936. {
  937. return mbedtls_sha512_common_self_test(verbose, 1);
  938. }
  939. #endif /* MBEDTLS_SHA384_C */
  940. #undef ARRAY_LENGTH
  941. #endif /* MBEDTLS_SELF_TEST */
  942. #endif /* MBEDTLS_SHA512_C || MBEDTLS_SHA384_C */