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