1e1051a39Sopenharmony_ci/*
2e1051a39Sopenharmony_ci * Copyright 2017-2021 The OpenSSL Project Authors. All Rights Reserved.
3e1051a39Sopenharmony_ci * Copyright 2017 Ribose Inc. All Rights Reserved.
4e1051a39Sopenharmony_ci * Ported from Ribose contributions from Botan.
5e1051a39Sopenharmony_ci *
6e1051a39Sopenharmony_ci * Licensed under the Apache License 2.0 (the "License").  You may not use
7e1051a39Sopenharmony_ci * this file except in compliance with the License.  You can obtain a copy
8e1051a39Sopenharmony_ci * in the file LICENSE in the source distribution or at
9e1051a39Sopenharmony_ci * https://www.openssl.org/source/license.html
10e1051a39Sopenharmony_ci */
11e1051a39Sopenharmony_ci
12e1051a39Sopenharmony_ci#include <openssl/e_os2.h>
13e1051a39Sopenharmony_ci#include "sm3_local.h"
14e1051a39Sopenharmony_ci
15e1051a39Sopenharmony_ciint ossl_sm3_init(SM3_CTX *c)
16e1051a39Sopenharmony_ci{
17e1051a39Sopenharmony_ci    memset(c, 0, sizeof(*c));
18e1051a39Sopenharmony_ci    c->A = SM3_A;
19e1051a39Sopenharmony_ci    c->B = SM3_B;
20e1051a39Sopenharmony_ci    c->C = SM3_C;
21e1051a39Sopenharmony_ci    c->D = SM3_D;
22e1051a39Sopenharmony_ci    c->E = SM3_E;
23e1051a39Sopenharmony_ci    c->F = SM3_F;
24e1051a39Sopenharmony_ci    c->G = SM3_G;
25e1051a39Sopenharmony_ci    c->H = SM3_H;
26e1051a39Sopenharmony_ci    return 1;
27e1051a39Sopenharmony_ci}
28e1051a39Sopenharmony_ci
29e1051a39Sopenharmony_civoid ossl_sm3_block_data_order(SM3_CTX *ctx, const void *p, size_t num)
30e1051a39Sopenharmony_ci{
31e1051a39Sopenharmony_ci    const unsigned char *data = p;
32e1051a39Sopenharmony_ci    register unsigned MD32_REG_T A, B, C, D, E, F, G, H;
33e1051a39Sopenharmony_ci
34e1051a39Sopenharmony_ci    unsigned MD32_REG_T W00, W01, W02, W03, W04, W05, W06, W07,
35e1051a39Sopenharmony_ci        W08, W09, W10, W11, W12, W13, W14, W15;
36e1051a39Sopenharmony_ci
37e1051a39Sopenharmony_ci    for (; num--;) {
38e1051a39Sopenharmony_ci
39e1051a39Sopenharmony_ci        A = ctx->A;
40e1051a39Sopenharmony_ci        B = ctx->B;
41e1051a39Sopenharmony_ci        C = ctx->C;
42e1051a39Sopenharmony_ci        D = ctx->D;
43e1051a39Sopenharmony_ci        E = ctx->E;
44e1051a39Sopenharmony_ci        F = ctx->F;
45e1051a39Sopenharmony_ci        G = ctx->G;
46e1051a39Sopenharmony_ci        H = ctx->H;
47e1051a39Sopenharmony_ci
48e1051a39Sopenharmony_ci        /*
49e1051a39Sopenharmony_ci        * We have to load all message bytes immediately since SM3 reads
50e1051a39Sopenharmony_ci        * them slightly out of order.
51e1051a39Sopenharmony_ci        */
52e1051a39Sopenharmony_ci        (void)HOST_c2l(data, W00);
53e1051a39Sopenharmony_ci        (void)HOST_c2l(data, W01);
54e1051a39Sopenharmony_ci        (void)HOST_c2l(data, W02);
55e1051a39Sopenharmony_ci        (void)HOST_c2l(data, W03);
56e1051a39Sopenharmony_ci        (void)HOST_c2l(data, W04);
57e1051a39Sopenharmony_ci        (void)HOST_c2l(data, W05);
58e1051a39Sopenharmony_ci        (void)HOST_c2l(data, W06);
59e1051a39Sopenharmony_ci        (void)HOST_c2l(data, W07);
60e1051a39Sopenharmony_ci        (void)HOST_c2l(data, W08);
61e1051a39Sopenharmony_ci        (void)HOST_c2l(data, W09);
62e1051a39Sopenharmony_ci        (void)HOST_c2l(data, W10);
63e1051a39Sopenharmony_ci        (void)HOST_c2l(data, W11);
64e1051a39Sopenharmony_ci        (void)HOST_c2l(data, W12);
65e1051a39Sopenharmony_ci        (void)HOST_c2l(data, W13);
66e1051a39Sopenharmony_ci        (void)HOST_c2l(data, W14);
67e1051a39Sopenharmony_ci        (void)HOST_c2l(data, W15);
68e1051a39Sopenharmony_ci
69e1051a39Sopenharmony_ci        R1(A, B, C, D, E, F, G, H, 0x79CC4519, W00, W00 ^ W04);
70e1051a39Sopenharmony_ci        W00 = EXPAND(W00, W07, W13, W03, W10);
71e1051a39Sopenharmony_ci        R1(D, A, B, C, H, E, F, G, 0xF3988A32, W01, W01 ^ W05);
72e1051a39Sopenharmony_ci        W01 = EXPAND(W01, W08, W14, W04, W11);
73e1051a39Sopenharmony_ci        R1(C, D, A, B, G, H, E, F, 0xE7311465, W02, W02 ^ W06);
74e1051a39Sopenharmony_ci        W02 = EXPAND(W02, W09, W15, W05, W12);
75e1051a39Sopenharmony_ci        R1(B, C, D, A, F, G, H, E, 0xCE6228CB, W03, W03 ^ W07);
76e1051a39Sopenharmony_ci        W03 = EXPAND(W03, W10, W00, W06, W13);
77e1051a39Sopenharmony_ci        R1(A, B, C, D, E, F, G, H, 0x9CC45197, W04, W04 ^ W08);
78e1051a39Sopenharmony_ci        W04 = EXPAND(W04, W11, W01, W07, W14);
79e1051a39Sopenharmony_ci        R1(D, A, B, C, H, E, F, G, 0x3988A32F, W05, W05 ^ W09);
80e1051a39Sopenharmony_ci        W05 = EXPAND(W05, W12, W02, W08, W15);
81e1051a39Sopenharmony_ci        R1(C, D, A, B, G, H, E, F, 0x7311465E, W06, W06 ^ W10);
82e1051a39Sopenharmony_ci        W06 = EXPAND(W06, W13, W03, W09, W00);
83e1051a39Sopenharmony_ci        R1(B, C, D, A, F, G, H, E, 0xE6228CBC, W07, W07 ^ W11);
84e1051a39Sopenharmony_ci        W07 = EXPAND(W07, W14, W04, W10, W01);
85e1051a39Sopenharmony_ci        R1(A, B, C, D, E, F, G, H, 0xCC451979, W08, W08 ^ W12);
86e1051a39Sopenharmony_ci        W08 = EXPAND(W08, W15, W05, W11, W02);
87e1051a39Sopenharmony_ci        R1(D, A, B, C, H, E, F, G, 0x988A32F3, W09, W09 ^ W13);
88e1051a39Sopenharmony_ci        W09 = EXPAND(W09, W00, W06, W12, W03);
89e1051a39Sopenharmony_ci        R1(C, D, A, B, G, H, E, F, 0x311465E7, W10, W10 ^ W14);
90e1051a39Sopenharmony_ci        W10 = EXPAND(W10, W01, W07, W13, W04);
91e1051a39Sopenharmony_ci        R1(B, C, D, A, F, G, H, E, 0x6228CBCE, W11, W11 ^ W15);
92e1051a39Sopenharmony_ci        W11 = EXPAND(W11, W02, W08, W14, W05);
93e1051a39Sopenharmony_ci        R1(A, B, C, D, E, F, G, H, 0xC451979C, W12, W12 ^ W00);
94e1051a39Sopenharmony_ci        W12 = EXPAND(W12, W03, W09, W15, W06);
95e1051a39Sopenharmony_ci        R1(D, A, B, C, H, E, F, G, 0x88A32F39, W13, W13 ^ W01);
96e1051a39Sopenharmony_ci        W13 = EXPAND(W13, W04, W10, W00, W07);
97e1051a39Sopenharmony_ci        R1(C, D, A, B, G, H, E, F, 0x11465E73, W14, W14 ^ W02);
98e1051a39Sopenharmony_ci        W14 = EXPAND(W14, W05, W11, W01, W08);
99e1051a39Sopenharmony_ci        R1(B, C, D, A, F, G, H, E, 0x228CBCE6, W15, W15 ^ W03);
100e1051a39Sopenharmony_ci        W15 = EXPAND(W15, W06, W12, W02, W09);
101e1051a39Sopenharmony_ci        R2(A, B, C, D, E, F, G, H, 0x9D8A7A87, W00, W00 ^ W04);
102e1051a39Sopenharmony_ci        W00 = EXPAND(W00, W07, W13, W03, W10);
103e1051a39Sopenharmony_ci        R2(D, A, B, C, H, E, F, G, 0x3B14F50F, W01, W01 ^ W05);
104e1051a39Sopenharmony_ci        W01 = EXPAND(W01, W08, W14, W04, W11);
105e1051a39Sopenharmony_ci        R2(C, D, A, B, G, H, E, F, 0x7629EA1E, W02, W02 ^ W06);
106e1051a39Sopenharmony_ci        W02 = EXPAND(W02, W09, W15, W05, W12);
107e1051a39Sopenharmony_ci        R2(B, C, D, A, F, G, H, E, 0xEC53D43C, W03, W03 ^ W07);
108e1051a39Sopenharmony_ci        W03 = EXPAND(W03, W10, W00, W06, W13);
109e1051a39Sopenharmony_ci        R2(A, B, C, D, E, F, G, H, 0xD8A7A879, W04, W04 ^ W08);
110e1051a39Sopenharmony_ci        W04 = EXPAND(W04, W11, W01, W07, W14);
111e1051a39Sopenharmony_ci        R2(D, A, B, C, H, E, F, G, 0xB14F50F3, W05, W05 ^ W09);
112e1051a39Sopenharmony_ci        W05 = EXPAND(W05, W12, W02, W08, W15);
113e1051a39Sopenharmony_ci        R2(C, D, A, B, G, H, E, F, 0x629EA1E7, W06, W06 ^ W10);
114e1051a39Sopenharmony_ci        W06 = EXPAND(W06, W13, W03, W09, W00);
115e1051a39Sopenharmony_ci        R2(B, C, D, A, F, G, H, E, 0xC53D43CE, W07, W07 ^ W11);
116e1051a39Sopenharmony_ci        W07 = EXPAND(W07, W14, W04, W10, W01);
117e1051a39Sopenharmony_ci        R2(A, B, C, D, E, F, G, H, 0x8A7A879D, W08, W08 ^ W12);
118e1051a39Sopenharmony_ci        W08 = EXPAND(W08, W15, W05, W11, W02);
119e1051a39Sopenharmony_ci        R2(D, A, B, C, H, E, F, G, 0x14F50F3B, W09, W09 ^ W13);
120e1051a39Sopenharmony_ci        W09 = EXPAND(W09, W00, W06, W12, W03);
121e1051a39Sopenharmony_ci        R2(C, D, A, B, G, H, E, F, 0x29EA1E76, W10, W10 ^ W14);
122e1051a39Sopenharmony_ci        W10 = EXPAND(W10, W01, W07, W13, W04);
123e1051a39Sopenharmony_ci        R2(B, C, D, A, F, G, H, E, 0x53D43CEC, W11, W11 ^ W15);
124e1051a39Sopenharmony_ci        W11 = EXPAND(W11, W02, W08, W14, W05);
125e1051a39Sopenharmony_ci        R2(A, B, C, D, E, F, G, H, 0xA7A879D8, W12, W12 ^ W00);
126e1051a39Sopenharmony_ci        W12 = EXPAND(W12, W03, W09, W15, W06);
127e1051a39Sopenharmony_ci        R2(D, A, B, C, H, E, F, G, 0x4F50F3B1, W13, W13 ^ W01);
128e1051a39Sopenharmony_ci        W13 = EXPAND(W13, W04, W10, W00, W07);
129e1051a39Sopenharmony_ci        R2(C, D, A, B, G, H, E, F, 0x9EA1E762, W14, W14 ^ W02);
130e1051a39Sopenharmony_ci        W14 = EXPAND(W14, W05, W11, W01, W08);
131e1051a39Sopenharmony_ci        R2(B, C, D, A, F, G, H, E, 0x3D43CEC5, W15, W15 ^ W03);
132e1051a39Sopenharmony_ci        W15 = EXPAND(W15, W06, W12, W02, W09);
133e1051a39Sopenharmony_ci        R2(A, B, C, D, E, F, G, H, 0x7A879D8A, W00, W00 ^ W04);
134e1051a39Sopenharmony_ci        W00 = EXPAND(W00, W07, W13, W03, W10);
135e1051a39Sopenharmony_ci        R2(D, A, B, C, H, E, F, G, 0xF50F3B14, W01, W01 ^ W05);
136e1051a39Sopenharmony_ci        W01 = EXPAND(W01, W08, W14, W04, W11);
137e1051a39Sopenharmony_ci        R2(C, D, A, B, G, H, E, F, 0xEA1E7629, W02, W02 ^ W06);
138e1051a39Sopenharmony_ci        W02 = EXPAND(W02, W09, W15, W05, W12);
139e1051a39Sopenharmony_ci        R2(B, C, D, A, F, G, H, E, 0xD43CEC53, W03, W03 ^ W07);
140e1051a39Sopenharmony_ci        W03 = EXPAND(W03, W10, W00, W06, W13);
141e1051a39Sopenharmony_ci        R2(A, B, C, D, E, F, G, H, 0xA879D8A7, W04, W04 ^ W08);
142e1051a39Sopenharmony_ci        W04 = EXPAND(W04, W11, W01, W07, W14);
143e1051a39Sopenharmony_ci        R2(D, A, B, C, H, E, F, G, 0x50F3B14F, W05, W05 ^ W09);
144e1051a39Sopenharmony_ci        W05 = EXPAND(W05, W12, W02, W08, W15);
145e1051a39Sopenharmony_ci        R2(C, D, A, B, G, H, E, F, 0xA1E7629E, W06, W06 ^ W10);
146e1051a39Sopenharmony_ci        W06 = EXPAND(W06, W13, W03, W09, W00);
147e1051a39Sopenharmony_ci        R2(B, C, D, A, F, G, H, E, 0x43CEC53D, W07, W07 ^ W11);
148e1051a39Sopenharmony_ci        W07 = EXPAND(W07, W14, W04, W10, W01);
149e1051a39Sopenharmony_ci        R2(A, B, C, D, E, F, G, H, 0x879D8A7A, W08, W08 ^ W12);
150e1051a39Sopenharmony_ci        W08 = EXPAND(W08, W15, W05, W11, W02);
151e1051a39Sopenharmony_ci        R2(D, A, B, C, H, E, F, G, 0x0F3B14F5, W09, W09 ^ W13);
152e1051a39Sopenharmony_ci        W09 = EXPAND(W09, W00, W06, W12, W03);
153e1051a39Sopenharmony_ci        R2(C, D, A, B, G, H, E, F, 0x1E7629EA, W10, W10 ^ W14);
154e1051a39Sopenharmony_ci        W10 = EXPAND(W10, W01, W07, W13, W04);
155e1051a39Sopenharmony_ci        R2(B, C, D, A, F, G, H, E, 0x3CEC53D4, W11, W11 ^ W15);
156e1051a39Sopenharmony_ci        W11 = EXPAND(W11, W02, W08, W14, W05);
157e1051a39Sopenharmony_ci        R2(A, B, C, D, E, F, G, H, 0x79D8A7A8, W12, W12 ^ W00);
158e1051a39Sopenharmony_ci        W12 = EXPAND(W12, W03, W09, W15, W06);
159e1051a39Sopenharmony_ci        R2(D, A, B, C, H, E, F, G, 0xF3B14F50, W13, W13 ^ W01);
160e1051a39Sopenharmony_ci        W13 = EXPAND(W13, W04, W10, W00, W07);
161e1051a39Sopenharmony_ci        R2(C, D, A, B, G, H, E, F, 0xE7629EA1, W14, W14 ^ W02);
162e1051a39Sopenharmony_ci        W14 = EXPAND(W14, W05, W11, W01, W08);
163e1051a39Sopenharmony_ci        R2(B, C, D, A, F, G, H, E, 0xCEC53D43, W15, W15 ^ W03);
164e1051a39Sopenharmony_ci        W15 = EXPAND(W15, W06, W12, W02, W09);
165e1051a39Sopenharmony_ci        R2(A, B, C, D, E, F, G, H, 0x9D8A7A87, W00, W00 ^ W04);
166e1051a39Sopenharmony_ci        W00 = EXPAND(W00, W07, W13, W03, W10);
167e1051a39Sopenharmony_ci        R2(D, A, B, C, H, E, F, G, 0x3B14F50F, W01, W01 ^ W05);
168e1051a39Sopenharmony_ci        W01 = EXPAND(W01, W08, W14, W04, W11);
169e1051a39Sopenharmony_ci        R2(C, D, A, B, G, H, E, F, 0x7629EA1E, W02, W02 ^ W06);
170e1051a39Sopenharmony_ci        W02 = EXPAND(W02, W09, W15, W05, W12);
171e1051a39Sopenharmony_ci        R2(B, C, D, A, F, G, H, E, 0xEC53D43C, W03, W03 ^ W07);
172e1051a39Sopenharmony_ci        W03 = EXPAND(W03, W10, W00, W06, W13);
173e1051a39Sopenharmony_ci        R2(A, B, C, D, E, F, G, H, 0xD8A7A879, W04, W04 ^ W08);
174e1051a39Sopenharmony_ci        R2(D, A, B, C, H, E, F, G, 0xB14F50F3, W05, W05 ^ W09);
175e1051a39Sopenharmony_ci        R2(C, D, A, B, G, H, E, F, 0x629EA1E7, W06, W06 ^ W10);
176e1051a39Sopenharmony_ci        R2(B, C, D, A, F, G, H, E, 0xC53D43CE, W07, W07 ^ W11);
177e1051a39Sopenharmony_ci        R2(A, B, C, D, E, F, G, H, 0x8A7A879D, W08, W08 ^ W12);
178e1051a39Sopenharmony_ci        R2(D, A, B, C, H, E, F, G, 0x14F50F3B, W09, W09 ^ W13);
179e1051a39Sopenharmony_ci        R2(C, D, A, B, G, H, E, F, 0x29EA1E76, W10, W10 ^ W14);
180e1051a39Sopenharmony_ci        R2(B, C, D, A, F, G, H, E, 0x53D43CEC, W11, W11 ^ W15);
181e1051a39Sopenharmony_ci        R2(A, B, C, D, E, F, G, H, 0xA7A879D8, W12, W12 ^ W00);
182e1051a39Sopenharmony_ci        R2(D, A, B, C, H, E, F, G, 0x4F50F3B1, W13, W13 ^ W01);
183e1051a39Sopenharmony_ci        R2(C, D, A, B, G, H, E, F, 0x9EA1E762, W14, W14 ^ W02);
184e1051a39Sopenharmony_ci        R2(B, C, D, A, F, G, H, E, 0x3D43CEC5, W15, W15 ^ W03);
185e1051a39Sopenharmony_ci
186e1051a39Sopenharmony_ci        ctx->A ^= A;
187e1051a39Sopenharmony_ci        ctx->B ^= B;
188e1051a39Sopenharmony_ci        ctx->C ^= C;
189e1051a39Sopenharmony_ci        ctx->D ^= D;
190e1051a39Sopenharmony_ci        ctx->E ^= E;
191e1051a39Sopenharmony_ci        ctx->F ^= F;
192e1051a39Sopenharmony_ci        ctx->G ^= G;
193e1051a39Sopenharmony_ci        ctx->H ^= H;
194e1051a39Sopenharmony_ci    }
195e1051a39Sopenharmony_ci}
196