598c80aa1dd971eb0bf9ce0589e256e1d4a95135
[openssl.git] / crypto / sm3 / sm3_locl.h
1 /*
2  * Copyright 2017 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 OpenSSL license (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 <stdlib.h>
13 #include <string.h>
14 #include <openssl/e_os2.h>
15 #include <openssl/sm3.h>
16
17 void sm3_block_data_order(SM3_CTX *c, const void *p, size_t num);
18
19 #define DATA_ORDER_IS_BIG_ENDIAN
20
21 #define HASH_LONG               SM3_WORD
22 #define HASH_CTX                SM3_CTX
23 #define HASH_CBLOCK             SM3_CBLOCK
24 #define HASH_UPDATE             SM3_Update
25 #define HASH_TRANSFORM          SM3_Transform
26 #define HASH_FINAL              SM3_Final
27 #define HASH_MAKE_STRING(c,s)   do {    \
28         unsigned long ll;               \
29         ll=(c)->A; (void)HOST_l2c(ll,(s));      \
30         ll=(c)->B; (void)HOST_l2c(ll,(s));      \
31         ll=(c)->C; (void)HOST_l2c(ll,(s));      \
32         ll=(c)->D; (void)HOST_l2c(ll,(s));      \
33         ll=(c)->E; (void)HOST_l2c(ll,(s));      \
34         ll=(c)->F; (void)HOST_l2c(ll,(s));      \
35         ll=(c)->G; (void)HOST_l2c(ll,(s));      \
36         ll=(c)->H; (void)HOST_l2c(ll,(s));      \
37         } while (0)
38 #define HASH_BLOCK_DATA_ORDER   sm3_block_data_order
39
40 #include "internal/md32_common.h"
41
42 #define P0(X) (X ^ ROTATE(X, 9) ^ ROTATE(X, 17))
43 #define P1(X) (X ^ ROTATE(X, 15) ^ ROTATE(X, 23))
44
45 #define FF0(X,Y,Z) (X ^ Y ^ Z)
46 #define GG0(X,Y,Z) (X ^ Y ^ Z)
47
48 #define FF1(X,Y,Z) ((X & Y) | ((X | Y) & Z))
49 #define GG1(X,Y,Z) ((Z ^ (X & (Y ^ Z))))
50
51 #define EXPAND(W0,W7,W13,W3,W10) \
52    (P1(W0 ^ W7 ^ ROTATE(W13, 15)) ^ ROTATE(W3, 7) ^ W10)
53
54 #define RND(A,B,C,D,E,F,G,H,TJ,Wi,Wj,FF,GG) do {                      \
55    const SM3_WORD A12 = ROTATE(A, 12);                                \
56    const SM3_WORD A12_SM = A12 + E + TJ;                              \
57    const SM3_WORD SS1 = ROTATE(A12_SM, 7);                            \
58    const SM3_WORD TT1 = FF(A,B,C) + D + (SS1 ^ A12) + (Wj);           \
59    const SM3_WORD TT2 = GG(E,F,G) + H + SS1 + Wi;                     \
60    B = ROTATE(B, 9);                                                  \
61    D = TT1;                                                           \
62    F = ROTATE(F, 19);                                                 \
63    H = P0(TT2);                                                       \
64    } while(0);
65
66 #define R1(A,B,C,D,E,F,G,H,TJ,Wi,Wj) \
67    RND(A,B,C,D,E,F,G,H,TJ,Wi,Wj,FF0,GG0)
68
69 #define R2(A,B,C,D,E,F,G,H,TJ,Wi,Wj) \
70    RND(A,B,C,D,E,F,G,H,TJ,Wi,Wj,FF1,GG1)
71
72 #define SM3_A 0x7380166fUL
73 #define SM3_B 0x4914b2b9UL
74 #define SM3_C 0x172442d7UL
75 #define SM3_D 0xda8a0600UL
76 #define SM3_E 0xa96f30bcUL
77 #define SM3_F 0x163138aaUL
78 #define SM3_G 0xe38dee4dUL
79 #define SM3_H 0xb0fb0e4eUL