Botan 3.14.1
Crypto and TLS for C&
sha2_32_armv8.cpp
Go to the documentation of this file.
1/*
2* SHA-256 using CPU instructions in ARMv8
3*
4* Contributed by Jeffrey Walton. Based on public domain code by
5* Johannes Schneiders, Skip Hovsmith and Barry O'Rourke.
6*
7* Further changes (C) 2020,2025 Jack Lloyd
8*
9* Botan is released under the Simplified BSD License (see license.txt)
10*/
11
12#include <botan/internal/sha2_32.h>
13
14#include <botan/internal/isa_extn.h>
15#include <botan/internal/sha2_32_f.h>
16#include <botan/internal/simd_4x32.h>
17#include <botan/internal/stack_scrubbing.h>
18#include <arm_neon.h>
19
20namespace Botan {
21
22namespace {
23
24inline BOTAN_FN_ISA_SHA2 SIMD_4x32 aarch64_sha256_expand_w(const SIMD_4x32 w0,
25 const SIMD_4x32 w1,
26 const SIMD_4x32 w2,
27 const SIMD_4x32 w3) {
28 return SIMD_4x32(vsha256su1q_u32(vsha256su0q_u32(w0.raw(), w1.raw()), w2.raw(), w3.raw()));
29}
30
31inline BOTAN_FN_ISA_SHA2 void aarch64_sha256_update(SIMD_4x32& s0,
32 SIMD_4x32& s1,
33 const SIMD_4x32 w,
34 const uint32_t K[4]) {
35 auto w_k = w + SIMD_4x32::load_le(K);
36 auto t = vsha256hq_u32(s0.raw(), s1.raw(), w_k.raw());
37 s1 = SIMD_4x32(vsha256h2q_u32(s1.raw(), s0.raw(), w_k.raw()));
38 s0 = SIMD_4x32(t);
39}
40
41} // namespace
42
43/*
44* SHA-256 using CPU instructions in ARMv8
45*/
46//static
48 std::span<const uint8_t> input8,
49 size_t blocks) {
50 // Load initial values
51 SIMD_4x32 s0 = SIMD_4x32::load_le(&digest[0]); // NOLINT(*-container-data-pointer)
52 SIMD_4x32 s1 = SIMD_4x32::load_le(&digest[4]);
53
54 const uint32_t* input32 = reinterpret_cast<const uint32_t*>(input8.data());
55
56 while(blocks > 0) {
57 const auto s0_save = s0;
58 const auto s1_save = s1;
59
60 auto w0 = SIMD_4x32::load_be(input32);
61 auto w1 = SIMD_4x32::load_be(input32 + 4);
62 auto w2 = SIMD_4x32::load_be(input32 + 8);
63 auto w3 = SIMD_4x32::load_be(input32 + 12);
64
65 for(size_t r = 0; r != 48; r += 16) {
66 aarch64_sha256_update(s0, s1, w0, &SHA256_K[r]);
67 w0 = aarch64_sha256_expand_w(w0, w1, w2, w3);
68
69 aarch64_sha256_update(s0, s1, w1, &SHA256_K[r + 4 * 1]);
70 w1 = aarch64_sha256_expand_w(w1, w2, w3, w0);
71
72 aarch64_sha256_update(s0, s1, w2, &SHA256_K[r + 4 * 2]);
73 w2 = aarch64_sha256_expand_w(w2, w3, w0, w1);
74
75 aarch64_sha256_update(s0, s1, w3, &SHA256_K[r + 4 * 3]);
76 w3 = aarch64_sha256_expand_w(w3, w0, w1, w2);
77 }
78
79 aarch64_sha256_update(s0, s1, w0, &SHA256_K[4 * 12]);
80 aarch64_sha256_update(s0, s1, w1, &SHA256_K[4 * 13]);
81 aarch64_sha256_update(s0, s1, w2, &SHA256_K[4 * 14]);
82 aarch64_sha256_update(s0, s1, w3, &SHA256_K[4 * 15]);
83
84 s0 += s0_save;
85 s1 += s1_save;
86
87 input32 += 64 / 4;
88 blocks--;
89 }
90
91 s0.store_le(&digest[0]); // NOLINT(*-container-data-pointer)
92 s1.store_le(&digest[4]);
93}
94
95} // namespace Botan
secure_vector< uint32_t > digest_type
Definition sha2_32.h:61
static void compress_digest_armv8(digest_type &digest, std::span< const uint8_t > input, size_t blocks)
void BOTAN_FN_ISA_SIMD_4X32 store_le(uint32_t out[4]) const noexcept
Definition simd_4x32.h:210
static SIMD_4x32 BOTAN_FN_ISA_SIMD_4X32 load_be(const void *in) noexcept
Definition simd_4x32.h:194
static SIMD_4x32 BOTAN_FN_ISA_SIMD_4X32 load_le(const void *in) noexcept
Definition simd_4x32.h:183
constexpr uint32_t SHA256_K[64]
Definition sha2_32_f.h:19
#define BOTAN_SCRUB_STACK_AFTER_RETURN