keccak-fast.c

Minimal SIMD keccak implementation
git clone git://git.finwo.net/lib/keccak-fast.c
Log | Files | Refs | README | LICENSE

arm_sha3.c (9899B)


      1 /* ARMv8.2 SHA3 backend. Self-contained: own permutation (arm-sha3-perm.h)
      2  * and own copy of the FIPS 202 sponge. Nothing here is shared with, or leaks
      3  * into, the scalar backend. Availability is decided at runtime only. */
      4 
      5 #include <stddef.h>
      6 #include <stdint.h>
      7 #include <string.h>
      8 
      9 #include "../keccak-fast-internal.h"
     10 
     11 #if defined(__aarch64__) && !defined(KECCAK_FAST_NO_ARM_SHA3)
     12 
     13 #if defined(__APPLE__)
     14 #include <sys/sysctl.h>
     15 #elif defined(__linux__)
     16 #include <sys/auxv.h>
     17 #include <asm/hwcap.h>
     18 #ifndef HWCAP_SHA3
     19 #define HWCAP_SHA3 (1 << 17)
     20 #endif
     21 #endif
     22 
     23 /* The sha3 target is scoped to the permutation, sponge and batch kernels so
     24  * the runtime gate below compiles at the x86-64 baseline with no SHA3
     25  * instructions of its own. */
     26 #if defined(__clang__)
     27 #pragma clang attribute push(__attribute__((target("sha3"))), apply_to = function)
     28 #elif defined(__GNUC__)
     29 #pragma GCC push_options
     30 #pragma GCC target("+sha3")
     31 #endif
     32 
     33 #include <arm_neon.h>
     34 
     35 static const uint64_t RC[24] = {
     36   1ULL, 0x8082ULL, 0x800000000000808aULL, 0x8000000080008000ULL,
     37   0x808bULL, 0x80000001ULL, 0x8000000080008081ULL, 0x8000000000008009ULL,
     38   0x8aULL, 0x88ULL, 0x80008009ULL, 0x8000000aULL,
     39   0x8000808bULL, 0x800000000000008bULL, 0x8000000000008089ULL, 0x8000000000008003ULL,
     40   0x8000000000008002ULL, 0x8000000000000080ULL, 0x800aULL, 0x800000008000000aULL,
     41   0x8000000080008081ULL, 0x8000000000008080ULL, 0x80000001ULL, 0x8000000080008008ULL};
     42 
     43 #include "arm-sha3-perm.h"
     44 
     45 #define KF_CAT2(a, b) a##b
     46 #define KF_CAT(a, b) KF_CAT2(a, b)
     47 #define KF_FN(name) KF_CAT(KF_PREFIX, name)
     48 
     49 #define _(S) do { S } while (0)
     50 #define FOR(i, ST, L, S) \
     51   _(for (size_t i = 0; i < L; i += ST) { S; })
     52 #define mkapply_ds(NAME, S) \
     53   static inline void NAME(uint8_t *dst, const uint8_t *src, size_t len) { \
     54     FOR(i, 1, len, S);                                                    \
     55   }
     56 #define mkapply_sd(NAME, S) \
     57   static inline void NAME(const uint8_t *src, uint8_t *dst, size_t len) { \
     58     FOR(i, 1, len, S);                                                    \
     59   }
     60 
     61 mkapply_ds(xorin, dst[i] ^= src[i])
     62 mkapply_sd(setout, dst[i] = src[i])
     63 
     64 #define Plen 200
     65 
     66 #define foldP(I, L, F, PERM) \
     67   while (L >= rate) {      \
     68     F(a, I, rate);         \
     69     PERM(a);               \
     70     I += rate;             \
     71     L -= rate;             \
     72   }
     73 
     74 #define KF_HASH_BODY(PERM)                                                     \
     75   uint8_t a[Plen] = {0};                                                       \
     76   foldP(in, inlen, xorin, PERM);                                               \
     77   a[inlen] ^= delim;                                                           \
     78   a[rate - 1] ^= 0x80;                                                         \
     79   xorin(a, in, inlen);                                                         \
     80   PERM(a);                                                                     \
     81   foldP(out, outlen, setout, PERM);                                            \
     82   setout(a, out, outlen);                                                      \
     83   memset(a, 0, 200);                                                           \
     84   return 0
     85 
     86 static inline int kf_hash24(uint8_t *out, size_t outlen, const uint8_t *in,
     87                             size_t inlen, size_t rate, uint8_t delim) {
     88   if ((out == NULL) || ((in == NULL) && inlen != 0) || (rate >= Plen)) {
     89     return -1;
     90   }
     91   KF_HASH_BODY(keccakf_armsha3);
     92 }
     93 
     94 static inline int kf_hash12(uint8_t *out, size_t outlen, const uint8_t *in,
     95                             size_t inlen, size_t rate, uint8_t delim) {
     96   if ((out == NULL) || ((in == NULL) && inlen != 0) || (rate >= Plen)) {
     97     return -1;
     98   }
     99   KF_HASH_BODY(keccak12_armsha3);
    100 }
    101 
    102 #define defshake(bits)                                                         \
    103   int KF_FN(shake##bits)(uint8_t *out, size_t outlen,                          \
    104                          const uint8_t *in, size_t inlen) {                    \
    105     return kf_hash24(out, outlen, in, inlen, 200 - (bits / 4), 0x1f);          \
    106   }
    107 #define defsha3(bits)                                                          \
    108   int KF_FN(sha3_##bits)(uint8_t *out, size_t outlen,                          \
    109                          const uint8_t *in, size_t inlen) {                    \
    110     if (outlen > (bits / 8)) {                                                 \
    111       return -1;                                                               \
    112     }                                                                          \
    113     return kf_hash24(out, outlen, in, inlen, 200 - (bits / 4), 0x06);          \
    114   }
    115 #define defturboshake(bits)                                                    \
    116   int KF_FN(turboshake##bits)(uint8_t *out, size_t outlen,                     \
    117                               const uint8_t *in, size_t inlen) {               \
    118     return kf_hash12(out, outlen, in, inlen, 200 - (bits / 4), 0x1f);          \
    119   }
    120 
    121 #define KF_PREFIX kf_armsha3_
    122 defshake(128)
    123 defshake(256)
    124 defsha3(224)
    125 defsha3(256)
    126 defsha3(384)
    127 defsha3(512)
    128 defturboshake(128)
    129 defturboshake(256)
    130 #undef defshake
    131 #undef defsha3
    132 #undef defturboshake
    133 
    134 void kf_armsha3_perm(uint64_t st[25]) {
    135   keccakf_armsha3(st);
    136 }
    137 
    138 void kf_armsha3_perm12(uint64_t st[25]) {
    139   keccak12_armsha3(st);
    140 }
    141 
    142 void kf_armsha3_batch(uint64_t states[][25], size_t count) {
    143   size_t i = 0;
    144   for (; i + 2 <= count; i += 2) {
    145     keccakf2_armsha3(&states[i]);
    146   }
    147   for (; i < count; i++) {
    148     keccakf_armsha3(states[i]);
    149   }
    150 }
    151 
    152 void kf_armsha3_batch12(uint64_t states[][25], size_t count) {
    153   size_t i = 0;
    154   for (; i + 2 <= count; i += 2) {
    155     keccak12_2_armsha3(&states[i]);
    156   }
    157   for (; i < count; i++) {
    158     keccak12_armsha3(states[i]);
    159   }
    160 }
    161 
    162 #define KF_DEFARMCHAIN(NAME, PERM2, PERM1)                                     \
    163   void NAME(size_t count, uint8_t *x, size_t outlen, const uint8_t *tail,      \
    164             size_t tail_len, size_t ctr_len, uint8_t ctr0, uint32_t iters,     \
    165             size_t rate, uint8_t delim) {                                      \
    166     uint64_t st[2][25];                                                        \
    167     size_t   i = 0;                                                            \
    168     for (; i + 2 <= count; i += 2) {                                           \
    169       for (uint32_t it = 0; it < iters; it++) {                                \
    170         for (size_t l = 0; l < 2; l++) {                                       \
    171           const uint8_t *tr = tail_len ? tail + (i + l) * tail_len : NULL;     \
    172           kf_chain_fill(st[l], x + (i + l) * outlen, outlen, tr, tail_len,     \
    173                         ctr_len, (uint8_t)(ctr0 + it), rate, delim);           \
    174         }                                                                      \
    175         PERM2(st);                                                             \
    176         for (size_t l = 0; l < 2; l++) {                                       \
    177           memcpy(x + (i + l) * outlen, st[l], outlen);                         \
    178         }                                                                      \
    179       }                                                                        \
    180     }                                                                          \
    181     for (; i < count; i++) {                                                   \
    182       uint8_t       *xr = x + i * outlen;                                      \
    183       const uint8_t *tr = tail_len ? tail + i * tail_len : NULL;               \
    184       for (uint32_t it = 0; it < iters; it++) {                                \
    185         kf_chain_fill(st[0], xr, outlen, tr, tail_len, ctr_len,                \
    186                       (uint8_t)(ctr0 + it), rate, delim);                      \
    187         PERM1(st[0]);                                                          \
    188         memcpy(xr, st[0], outlen);                                             \
    189       }                                                                        \
    190     }                                                                          \
    191   }
    192 
    193 KF_DEFARMCHAIN(kf_armsha3_chain, keccakf2_armsha3, keccakf_armsha3)
    194 KF_DEFARMCHAIN(kf_armsha3_chain12, keccak12_2_armsha3, keccak12_armsha3)
    195 
    196 #undef KF_DEFARMCHAIN
    197 
    198 #if defined(__clang__)
    199 #pragma clang attribute pop
    200 #elif defined(__GNUC__)
    201 #pragma GCC pop_options
    202 #endif
    203 
    204 int kf_armsha3_available(void) {
    205 #if defined(__APPLE__)
    206   int    v = 0;
    207   size_t n = sizeof v;
    208   if (sysctlbyname("hw.optional.arm.FEAT_SHA3", &v, &n, NULL, 0) != 0) {
    209     return 0;
    210   }
    211   return v != 0;
    212 #elif defined(__linux__)
    213   return (getauxval(AT_HWCAP) & HWCAP_SHA3) != 0;
    214 #else
    215   return 0;
    216 #endif
    217 }
    218 
    219 __attribute__((constructor)) static void kf_armsha3_register(void) {
    220   if (!kf_armsha3_available()) {
    221     return;
    222   }
    223   if (kf_backend_priority <= 101) {
    224     kf_shake128      = kf_armsha3_shake128;
    225     kf_shake256      = kf_armsha3_shake256;
    226     kf_sha3_224      = kf_armsha3_sha3_224;
    227     kf_sha3_256      = kf_armsha3_sha3_256;
    228     kf_sha3_384      = kf_armsha3_sha3_384;
    229     kf_sha3_512      = kf_armsha3_sha3_512;
    230     kf_turboshake128 = kf_armsha3_turboshake128;
    231     kf_turboshake256 = kf_armsha3_turboshake256;
    232     kf_backend_priority = 101;
    233     kf_backend_label    = "arm-sha3";
    234   }
    235   if (kf_batch_priority <= 182) {
    236     kf_batch_perm     = kf_armsha3_batch;
    237     kf_batch_perm12   = kf_armsha3_batch12;
    238     kf_batch_priority = 182;
    239     kf_batch_lanes    = 2;
    240     kf_batch_label    = "arm-sha3";
    241   }
    242   if (kf_chain_priority <= 194) {
    243     kf_chain_kernel   = kf_armsha3_chain;
    244     kf_chain_kernel12 = kf_armsha3_chain12;
    245     kf_chain_priority = 194;
    246     kf_chain_lanes    = 2;
    247     kf_chain_label    = "arm-sha3";
    248   }
    249   if (kf_stream_priority <= 101) {
    250     kf_stream_perm     = kf_armsha3_perm;
    251     kf_stream_perm12   = kf_armsha3_perm12;
    252     kf_stream_priority = 101;
    253     kf_stream_label    = "arm-sha3";
    254   }
    255 }
    256 
    257 #endif /* aarch64 && !KECCAK_FAST_NO_ARM_SHA3 */