1 /* $OpenBSD: sm3.c,v 1.6 2023/07/08 06:36:55 jsing Exp $ */ 2 /* 3 * Copyright (c) 2018, Ribose Inc 4 * 5 * Permission to use, copy, modify, and/or distribute this software for any 6 * purpose with or without fee is hereby granted, provided that the above 7 * copyright notice and this permission notice appear in all copies. 8 * 9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16 */ 17 18 #ifndef OPENSSL_NO_SM3 19 20 #include <openssl/sm3.h> 21 22 #include <string.h> 23 24 #include <openssl/opensslconf.h> 25 26 #define DATA_ORDER_IS_BIG_ENDIAN 27 28 #define HASH_LONG SM3_WORD 29 #define HASH_CTX SM3_CTX 30 #define HASH_CBLOCK SM3_CBLOCK 31 #define HASH_UPDATE SM3_Update 32 #define HASH_TRANSFORM SM3_Transform 33 #define HASH_FINAL SM3_Final 34 #define HASH_MAKE_STRING(c, s) do { \ 35 unsigned long ll; \ 36 ll = (c)->A; HOST_l2c(ll, (s)); \ 37 ll = (c)->B; HOST_l2c(ll, (s)); \ 38 ll = (c)->C; HOST_l2c(ll, (s)); \ 39 ll = (c)->D; HOST_l2c(ll, (s)); \ 40 ll = (c)->E; HOST_l2c(ll, (s)); \ 41 ll = (c)->F; HOST_l2c(ll, (s)); \ 42 ll = (c)->G; HOST_l2c(ll, (s)); \ 43 ll = (c)->H; HOST_l2c(ll, (s)); \ 44 } while (0) 45 #define HASH_BLOCK_DATA_ORDER SM3_block_data_order 46 47 void SM3_block_data_order(SM3_CTX *c, const void *p, size_t num); 48 void SM3_transform(SM3_CTX *c, const unsigned char *data); 49 50 #include "md32_common.h" 51 52 #define P0(X) (X ^ ROTATE(X, 9) ^ ROTATE(X, 17)) 53 #define P1(X) (X ^ ROTATE(X, 15) ^ ROTATE(X, 23)) 54 55 #define FF0(X, Y, Z) (X ^ Y ^ Z) 56 #define GG0(X, Y, Z) (X ^ Y ^ Z) 57 58 #define FF1(X, Y, Z) ((X & Y) | ((X | Y) & Z)) 59 #define GG1(X, Y, Z) ((Z ^ (X & (Y ^ Z)))) 60 61 #define EXPAND(W0, W7, W13, W3, W10) \ 62 (P1(W0 ^ W7 ^ ROTATE(W13, 15)) ^ ROTATE(W3, 7) ^ W10) 63 64 #define ROUND(A, B, C, D, E, F, G, H, TJ, Wi, Wj, FF, GG) do { \ 65 const SM3_WORD A12 = ROTATE(A, 12); \ 66 const SM3_WORD A12_SM = A12 + E + TJ; \ 67 const SM3_WORD SS1 = ROTATE(A12_SM, 7); \ 68 const SM3_WORD TT1 = FF(A, B, C) + D + (SS1 ^ A12) + (Wj); \ 69 const SM3_WORD TT2 = GG(E, F, G) + H + SS1 + Wi; \ 70 B = ROTATE(B, 9); \ 71 D = TT1; \ 72 F = ROTATE(F, 19); \ 73 H = P0(TT2); \ 74 } while(0) 75 76 #define R1(A, B, C, D, E, F, G, H, TJ, Wi, Wj) \ 77 ROUND(A, B, C, D, E, F, G, H, TJ, Wi, Wj, FF0, GG0) 78 79 #define R2(A, B, C, D, E, F, G, H, TJ, Wi, Wj) \ 80 ROUND(A, B, C, D, E, F, G, H, TJ, Wi, Wj, FF1, GG1) 81 82 #define SM3_A 0x7380166fUL 83 #define SM3_B 0x4914b2b9UL 84 #define SM3_C 0x172442d7UL 85 #define SM3_D 0xda8a0600UL 86 #define SM3_E 0xa96f30bcUL 87 #define SM3_F 0x163138aaUL 88 #define SM3_G 0xe38dee4dUL 89 #define SM3_H 0xb0fb0e4eUL 90 91 LCRYPTO_ALIAS(SM3_Update); 92 LCRYPTO_ALIAS(SM3_Final); 93 94 int 95 SM3_Init(SM3_CTX *c) 96 { 97 memset(c, 0, sizeof(*c)); 98 c->A = SM3_A; 99 c->B = SM3_B; 100 c->C = SM3_C; 101 c->D = SM3_D; 102 c->E = SM3_E; 103 c->F = SM3_F; 104 c->G = SM3_G; 105 c->H = SM3_H; 106 return 1; 107 } 108 LCRYPTO_ALIAS(SM3_Init); 109 110 void 111 SM3_block_data_order(SM3_CTX *ctx, const void *p, size_t num) 112 { 113 const unsigned char *data = p; 114 SM3_WORD A, B, C, D, E, F, G, H; 115 SM3_WORD W00, W01, W02, W03, W04, W05, W06, W07; 116 SM3_WORD W08, W09, W10, W11, W12, W13, W14, W15; 117 118 while (num-- != 0) { 119 A = ctx->A; 120 B = ctx->B; 121 C = ctx->C; 122 D = ctx->D; 123 E = ctx->E; 124 F = ctx->F; 125 G = ctx->G; 126 H = ctx->H; 127 128 /* 129 * We have to load all message bytes immediately since SM3 reads 130 * them slightly out of order. 131 */ 132 HOST_c2l(data, W00); 133 HOST_c2l(data, W01); 134 HOST_c2l(data, W02); 135 HOST_c2l(data, W03); 136 HOST_c2l(data, W04); 137 HOST_c2l(data, W05); 138 HOST_c2l(data, W06); 139 HOST_c2l(data, W07); 140 HOST_c2l(data, W08); 141 HOST_c2l(data, W09); 142 HOST_c2l(data, W10); 143 HOST_c2l(data, W11); 144 HOST_c2l(data, W12); 145 HOST_c2l(data, W13); 146 HOST_c2l(data, W14); 147 HOST_c2l(data, W15); 148 149 R1(A, B, C, D, E, F, G, H, 0x79cc4519, W00, W00 ^ W04); 150 W00 = EXPAND(W00, W07, W13, W03, W10); 151 R1(D, A, B, C, H, E, F, G, 0xf3988a32, W01, W01 ^ W05); 152 W01 = EXPAND(W01, W08, W14, W04, W11); 153 R1(C, D, A, B, G, H, E, F, 0xe7311465, W02, W02 ^ W06); 154 W02 = EXPAND(W02, W09, W15, W05, W12); 155 R1(B, C, D, A, F, G, H, E, 0xce6228cb, W03, W03 ^ W07); 156 W03 = EXPAND(W03, W10, W00, W06, W13); 157 R1(A, B, C, D, E, F, G, H, 0x9cc45197, W04, W04 ^ W08); 158 W04 = EXPAND(W04, W11, W01, W07, W14); 159 R1(D, A, B, C, H, E, F, G, 0x3988a32f, W05, W05 ^ W09); 160 W05 = EXPAND(W05, W12, W02, W08, W15); 161 R1(C, D, A, B, G, H, E, F, 0x7311465e, W06, W06 ^ W10); 162 W06 = EXPAND(W06, W13, W03, W09, W00); 163 R1(B, C, D, A, F, G, H, E, 0xe6228cbc, W07, W07 ^ W11); 164 W07 = EXPAND(W07, W14, W04, W10, W01); 165 R1(A, B, C, D, E, F, G, H, 0xcc451979, W08, W08 ^ W12); 166 W08 = EXPAND(W08, W15, W05, W11, W02); 167 R1(D, A, B, C, H, E, F, G, 0x988a32f3, W09, W09 ^ W13); 168 W09 = EXPAND(W09, W00, W06, W12, W03); 169 R1(C, D, A, B, G, H, E, F, 0x311465e7, W10, W10 ^ W14); 170 W10 = EXPAND(W10, W01, W07, W13, W04); 171 R1(B, C, D, A, F, G, H, E, 0x6228cbce, W11, W11 ^ W15); 172 W11 = EXPAND(W11, W02, W08, W14, W05); 173 R1(A, B, C, D, E, F, G, H, 0xc451979c, W12, W12 ^ W00); 174 W12 = EXPAND(W12, W03, W09, W15, W06); 175 R1(D, A, B, C, H, E, F, G, 0x88a32f39, W13, W13 ^ W01); 176 W13 = EXPAND(W13, W04, W10, W00, W07); 177 R1(C, D, A, B, G, H, E, F, 0x11465e73, W14, W14 ^ W02); 178 W14 = EXPAND(W14, W05, W11, W01, W08); 179 R1(B, C, D, A, F, G, H, E, 0x228cbce6, W15, W15 ^ W03); 180 W15 = EXPAND(W15, W06, W12, W02, W09); 181 R2(A, B, C, D, E, F, G, H, 0x9d8a7a87, W00, W00 ^ W04); 182 W00 = EXPAND(W00, W07, W13, W03, W10); 183 R2(D, A, B, C, H, E, F, G, 0x3b14f50f, W01, W01 ^ W05); 184 W01 = EXPAND(W01, W08, W14, W04, W11); 185 R2(C, D, A, B, G, H, E, F, 0x7629ea1e, W02, W02 ^ W06); 186 W02 = EXPAND(W02, W09, W15, W05, W12); 187 R2(B, C, D, A, F, G, H, E, 0xec53d43c, W03, W03 ^ W07); 188 W03 = EXPAND(W03, W10, W00, W06, W13); 189 R2(A, B, C, D, E, F, G, H, 0xd8a7a879, W04, W04 ^ W08); 190 W04 = EXPAND(W04, W11, W01, W07, W14); 191 R2(D, A, B, C, H, E, F, G, 0xb14f50f3, W05, W05 ^ W09); 192 W05 = EXPAND(W05, W12, W02, W08, W15); 193 R2(C, D, A, B, G, H, E, F, 0x629ea1e7, W06, W06 ^ W10); 194 W06 = EXPAND(W06, W13, W03, W09, W00); 195 R2(B, C, D, A, F, G, H, E, 0xc53d43ce, W07, W07 ^ W11); 196 W07 = EXPAND(W07, W14, W04, W10, W01); 197 R2(A, B, C, D, E, F, G, H, 0x8a7a879d, W08, W08 ^ W12); 198 W08 = EXPAND(W08, W15, W05, W11, W02); 199 R2(D, A, B, C, H, E, F, G, 0x14f50f3b, W09, W09 ^ W13); 200 W09 = EXPAND(W09, W00, W06, W12, W03); 201 R2(C, D, A, B, G, H, E, F, 0x29ea1e76, W10, W10 ^ W14); 202 W10 = EXPAND(W10, W01, W07, W13, W04); 203 R2(B, C, D, A, F, G, H, E, 0x53d43cec, W11, W11 ^ W15); 204 W11 = EXPAND(W11, W02, W08, W14, W05); 205 R2(A, B, C, D, E, F, G, H, 0xa7a879d8, W12, W12 ^ W00); 206 W12 = EXPAND(W12, W03, W09, W15, W06); 207 R2(D, A, B, C, H, E, F, G, 0x4f50f3b1, W13, W13 ^ W01); 208 W13 = EXPAND(W13, W04, W10, W00, W07); 209 R2(C, D, A, B, G, H, E, F, 0x9ea1e762, W14, W14 ^ W02); 210 W14 = EXPAND(W14, W05, W11, W01, W08); 211 R2(B, C, D, A, F, G, H, E, 0x3d43cec5, W15, W15 ^ W03); 212 W15 = EXPAND(W15, W06, W12, W02, W09); 213 R2(A, B, C, D, E, F, G, H, 0x7a879d8a, W00, W00 ^ W04); 214 W00 = EXPAND(W00, W07, W13, W03, W10); 215 R2(D, A, B, C, H, E, F, G, 0xf50f3b14, W01, W01 ^ W05); 216 W01 = EXPAND(W01, W08, W14, W04, W11); 217 R2(C, D, A, B, G, H, E, F, 0xea1e7629, W02, W02 ^ W06); 218 W02 = EXPAND(W02, W09, W15, W05, W12); 219 R2(B, C, D, A, F, G, H, E, 0xd43cec53, W03, W03 ^ W07); 220 W03 = EXPAND(W03, W10, W00, W06, W13); 221 R2(A, B, C, D, E, F, G, H, 0xa879d8a7, W04, W04 ^ W08); 222 W04 = EXPAND(W04, W11, W01, W07, W14); 223 R2(D, A, B, C, H, E, F, G, 0x50f3b14f, W05, W05 ^ W09); 224 W05 = EXPAND(W05, W12, W02, W08, W15); 225 R2(C, D, A, B, G, H, E, F, 0xa1e7629e, W06, W06 ^ W10); 226 W06 = EXPAND(W06, W13, W03, W09, W00); 227 R2(B, C, D, A, F, G, H, E, 0x43cec53d, W07, W07 ^ W11); 228 W07 = EXPAND(W07, W14, W04, W10, W01); 229 R2(A, B, C, D, E, F, G, H, 0x879d8a7a, W08, W08 ^ W12); 230 W08 = EXPAND(W08, W15, W05, W11, W02); 231 R2(D, A, B, C, H, E, F, G, 0x0f3b14f5, W09, W09 ^ W13); 232 W09 = EXPAND(W09, W00, W06, W12, W03); 233 R2(C, D, A, B, G, H, E, F, 0x1e7629ea, W10, W10 ^ W14); 234 W10 = EXPAND(W10, W01, W07, W13, W04); 235 R2(B, C, D, A, F, G, H, E, 0x3cec53d4, W11, W11 ^ W15); 236 W11 = EXPAND(W11, W02, W08, W14, W05); 237 R2(A, B, C, D, E, F, G, H, 0x79d8a7a8, W12, W12 ^ W00); 238 W12 = EXPAND(W12, W03, W09, W15, W06); 239 R2(D, A, B, C, H, E, F, G, 0xf3b14f50, W13, W13 ^ W01); 240 W13 = EXPAND(W13, W04, W10, W00, W07); 241 R2(C, D, A, B, G, H, E, F, 0xe7629ea1, W14, W14 ^ W02); 242 W14 = EXPAND(W14, W05, W11, W01, W08); 243 R2(B, C, D, A, F, G, H, E, 0xcec53d43, W15, W15 ^ W03); 244 W15 = EXPAND(W15, W06, W12, W02, W09); 245 R2(A, B, C, D, E, F, G, H, 0x9d8a7a87, W00, W00 ^ W04); 246 W00 = EXPAND(W00, W07, W13, W03, W10); 247 R2(D, A, B, C, H, E, F, G, 0x3b14f50f, W01, W01 ^ W05); 248 W01 = EXPAND(W01, W08, W14, W04, W11); 249 R2(C, D, A, B, G, H, E, F, 0x7629ea1e, W02, W02 ^ W06); 250 W02 = EXPAND(W02, W09, W15, W05, W12); 251 R2(B, C, D, A, F, G, H, E, 0xec53d43c, W03, W03 ^ W07); 252 W03 = EXPAND(W03, W10, W00, W06, W13); 253 R2(A, B, C, D, E, F, G, H, 0xd8a7a879, W04, W04 ^ W08); 254 R2(D, A, B, C, H, E, F, G, 0xb14f50f3, W05, W05 ^ W09); 255 R2(C, D, A, B, G, H, E, F, 0x629ea1e7, W06, W06 ^ W10); 256 R2(B, C, D, A, F, G, H, E, 0xc53d43ce, W07, W07 ^ W11); 257 R2(A, B, C, D, E, F, G, H, 0x8a7a879d, W08, W08 ^ W12); 258 R2(D, A, B, C, H, E, F, G, 0x14f50f3b, W09, W09 ^ W13); 259 R2(C, D, A, B, G, H, E, F, 0x29ea1e76, W10, W10 ^ W14); 260 R2(B, C, D, A, F, G, H, E, 0x53d43cec, W11, W11 ^ W15); 261 R2(A, B, C, D, E, F, G, H, 0xa7a879d8, W12, W12 ^ W00); 262 R2(D, A, B, C, H, E, F, G, 0x4f50f3b1, W13, W13 ^ W01); 263 R2(C, D, A, B, G, H, E, F, 0x9ea1e762, W14, W14 ^ W02); 264 R2(B, C, D, A, F, G, H, E, 0x3d43cec5, W15, W15 ^ W03); 265 266 ctx->A ^= A; 267 ctx->B ^= B; 268 ctx->C ^= C; 269 ctx->D ^= D; 270 ctx->E ^= E; 271 ctx->F ^= F; 272 ctx->G ^= G; 273 ctx->H ^= H; 274 } 275 } 276 277 #endif /* !OPENSSL_NO_SM3 */ 278