xref: /src/crypto/openssl/crypto/sm3/sm3_local.h (revision f25b8c9fb4f58cf61adb47d7570abe7caa6d385d)
1 /*
2  * Copyright 2017-2024 The OpenSSL Project Authors. All Rights Reserved.
3  * Copyright 2017 Ribose Inc. All Rights Reserved.
4  * Ported from Ribose contributions from Botan.
5  *
6  * Licensed under the Apache License 2.0 (the "License").  You may not use
7  * this file except in compliance with the License.  You can obtain a copy
8  * in the file LICENSE in the source distribution or at
9  * https://www.openssl.org/source/license.html
10  */
11 
12 #include <string.h>
13 #include "internal/sm3.h"
14 
15 #define DATA_ORDER_IS_BIG_ENDIAN
16 
17 #define HASH_LONG SM3_WORD
18 #define HASH_CTX SM3_CTX
19 #define HASH_CBLOCK SM3_CBLOCK
20 #define HASH_UPDATE ossl_sm3_update
21 #define HASH_TRANSFORM ossl_sm3_transform
22 #define HASH_FINAL ossl_sm3_final
23 #define HASH_MAKE_STRING(c, s)   \
24     do {                         \
25         unsigned long ll;        \
26         ll = (c)->A;             \
27         (void)HOST_l2c(ll, (s)); \
28         ll = (c)->B;             \
29         (void)HOST_l2c(ll, (s)); \
30         ll = (c)->C;             \
31         (void)HOST_l2c(ll, (s)); \
32         ll = (c)->D;             \
33         (void)HOST_l2c(ll, (s)); \
34         ll = (c)->E;             \
35         (void)HOST_l2c(ll, (s)); \
36         ll = (c)->F;             \
37         (void)HOST_l2c(ll, (s)); \
38         ll = (c)->G;             \
39         (void)HOST_l2c(ll, (s)); \
40         ll = (c)->H;             \
41         (void)HOST_l2c(ll, (s)); \
42     } while (0)
43 
44 #if defined(OPENSSL_SM3_ASM)
45 #if defined(__aarch64__) || defined(_M_ARM64)
46 #include "crypto/arm_arch.h"
47 #define HWSM3_CAPABLE (OPENSSL_armcap_P & ARMV8_SM3)
48 void ossl_hwsm3_block_data_order(SM3_CTX *c, const void *p, size_t num);
49 #endif
50 #if defined(OPENSSL_CPUID_OBJ) && defined(__riscv) && __riscv_xlen == 64
51 #include "crypto/riscv_arch.h"
52 #define HWSM3_CAPABLE 1
53 void ossl_hwsm3_block_data_order(SM3_CTX *c, const void *p, size_t num);
54 #endif
55 #endif
56 
57 #if defined(HWSM3_CAPABLE)
58 #define HASH_BLOCK_DATA_ORDER (HWSM3_CAPABLE ? ossl_hwsm3_block_data_order \
59                                              : ossl_sm3_block_data_order)
60 #else
61 #define HASH_BLOCK_DATA_ORDER ossl_sm3_block_data_order
62 #endif
63 
64 void ossl_sm3_block_data_order(SM3_CTX *c, const void *p, size_t num);
65 void ossl_sm3_transform(SM3_CTX *c, const unsigned char *data);
66 
67 #include "crypto/md32_common.h"
68 
69 #ifndef PEDANTIC
70 #if defined(__GNUC__) && __GNUC__ >= 2 && !defined(OPENSSL_NO_ASM) && !defined(OPENSSL_NO_INLINE_ASM)
71 #if defined(__riscv_zksh)
72 #define P0(x) ({ MD32_REG_T ret;        \
73                        asm ("sm3p0 %0, %1" \
74                        : "=r"(ret)         \
75                        : "r"(x)); ret; })
76 #define P1(x) ({ MD32_REG_T ret;        \
77                        asm ("sm3p1 %0, %1" \
78                        : "=r"(ret)         \
79                        : "r"(x)); ret; })
80 #endif
81 #endif
82 #endif
83 
84 #ifndef P0
85 #define P0(X) (X ^ ROTATE(X, 9) ^ ROTATE(X, 17))
86 #endif
87 #ifndef P1
88 #define P1(X) (X ^ ROTATE(X, 15) ^ ROTATE(X, 23))
89 #endif
90 
91 #define FF0(X, Y, Z) (X ^ Y ^ Z)
92 #define GG0(X, Y, Z) (X ^ Y ^ Z)
93 
94 #define FF1(X, Y, Z) ((X & Y) | ((X | Y) & Z))
95 #define GG1(X, Y, Z) ((Z ^ (X & (Y ^ Z))))
96 
97 #define EXPAND(W0, W7, W13, W3, W10) \
98     (P1(W0 ^ W7 ^ ROTATE(W13, 15)) ^ ROTATE(W3, 7) ^ W10)
99 
100 #define RND(A, B, C, D, E, F, G, H, TJ, Wi, Wj, FF, GG)            \
101     do {                                                           \
102         const SM3_WORD A12 = ROTATE(A, 12);                        \
103         const SM3_WORD A12_SM = A12 + E + TJ;                      \
104         const SM3_WORD SS1 = ROTATE(A12_SM, 7);                    \
105         const SM3_WORD TT1 = FF(A, B, C) + D + (SS1 ^ A12) + (Wj); \
106         const SM3_WORD TT2 = GG(E, F, G) + H + SS1 + Wi;           \
107         B = ROTATE(B, 9);                                          \
108         D = TT1;                                                   \
109         F = ROTATE(F, 19);                                         \
110         H = P0(TT2);                                               \
111     } while (0)
112 
113 #define R1(A, B, C, D, E, F, G, H, TJ, Wi, Wj) \
114     RND(A, B, C, D, E, F, G, H, TJ, Wi, Wj, FF0, GG0)
115 
116 #define R2(A, B, C, D, E, F, G, H, TJ, Wi, Wj) \
117     RND(A, B, C, D, E, F, G, H, TJ, Wi, Wj, FF1, GG1)
118 
119 #define SM3_A 0x7380166fUL
120 #define SM3_B 0x4914b2b9UL
121 #define SM3_C 0x172442d7UL
122 #define SM3_D 0xda8a0600UL
123 #define SM3_E 0xa96f30bcUL
124 #define SM3_F 0x163138aaUL
125 #define SM3_G 0xe38dee4dUL
126 #define SM3_H 0xb0fb0e4eUL
127