Coverage Report

Created: 2024-01-20 12:36

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