Skip to content

Commit e8eb4a0

Browse files
authored
Merge pull request #664 from libtom/pr/fix-sm4-on-cygwin
Cygwin fix for SM4 (issue #663)
2 parents fc6b420 + 24e31ef commit e8eb4a0

File tree

1 file changed

+34
-32
lines changed

1 file changed

+34
-32
lines changed

src/ciphers/sm4.c

Lines changed: 34 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ typedef ulong32 sm4_u32_t;
2828
* S-box defined in section 6.2
2929
* (1) Nonlinear transformation
3030
*/
31-
static const sm4_u8_t sbox_table[16][16] = {
31+
static const sm4_u8_t sm4_sbox_table[16][16] = {
3232
{0xd6, 0x90, 0xe9, 0xfe, 0xcc, 0xe1, 0x3d, 0xb7,
3333
0x16, 0xb6, 0x14, 0xc2, 0x28, 0xfb, 0x2c, 0x05},
3434
{0x2b, 0x67, 0x9a, 0x76, 0x2a, 0xbe, 0x04, 0xc3,
@@ -67,9 +67,9 @@ static const sm4_u8_t sbox_table[16][16] = {
6767
* S-box
6868
* defined in section 2.6 S-box
6969
*/
70-
LTC_INLINE static sm4_u8_t sbox(sm4_u8_t a)
70+
LTC_INLINE static sm4_u8_t s_sm4_sbox(sm4_u8_t a)
7171
{
72-
return sbox_table[(a >> 4) & 0x0f][a & 0x0f];
72+
return sm4_sbox_table[(a >> 4) & 0x0f][a & 0x0f];
7373
}
7474

7575
/*
@@ -80,49 +80,49 @@ LTC_INLINE static sm4_u8_t sbox(sm4_u8_t a)
8080
* But we just convert a 32bit word byte by byte.
8181
* So it's OK if we don't convert the endian order
8282
*/
83-
LTC_INLINE static sm4_u32_t t(sm4_u32_t A)
83+
LTC_INLINE static sm4_u32_t s_sm4_t(sm4_u32_t A)
8484
{
8585
sm4_u8_t a[4];
8686
sm4_u8_t b[4];
8787
sm4_u32_t B;
8888

8989
STORE32H(A, a);
90-
b[0] = sbox(a[0]);
91-
b[1] = sbox(a[1]);
92-
b[2] = sbox(a[2]);
93-
b[3] = sbox(a[3]);
90+
b[0] = s_sm4_sbox(a[0]);
91+
b[1] = s_sm4_sbox(a[1]);
92+
b[2] = s_sm4_sbox(a[2]);
93+
b[3] = s_sm4_sbox(a[3]);
9494
LOAD32H(B, b);
9595
return B;
9696
}
9797

9898
/*
9999
* defined in section 6.2 (2) Linear transformation L
100100
*/
101-
LTC_INLINE static sm4_u32_t L(sm4_u32_t B)
101+
LTC_INLINE static sm4_u32_t s_sm4_L62(sm4_u32_t B)
102102
{
103103
return B ^ ROLc(B, 2) ^ ROLc(B, 10) ^ ROLc(B, 18) ^ ROLc(B, 24);
104104
}
105105

106106
/*
107107
* defined in section 6.2 Permutation T
108108
*/
109-
LTC_INLINE static sm4_u32_t T(sm4_u32_t Z)
109+
LTC_INLINE static sm4_u32_t s_sm4_T62(sm4_u32_t Z)
110110
{
111-
return L(t(Z));
111+
return s_sm4_L62(s_sm4_t(Z));
112112
}
113113

114114
/*
115115
* defined in section 7.3 (2) The system parameter FK
116116
*/
117-
static const sm4_u32_t FK[4] = {
117+
static const sm4_u32_t sm4_FK[4] = {
118118
0xa3b1bac6, 0x56aa3350, 0x677d9197, 0xb27022dc
119119
};
120120

121121
/*
122122
* defined in section 7.3 (3) The fixed parameter CK
123123
* The fixed parameter CK is used in the key expansion algorithm
124124
*/
125-
static const sm4_u32_t CK[32] =
125+
static const sm4_u32_t sm4_CK[32] =
126126
{
127127
0x00070e15, 0x1c232a31, 0x383f464d, 0x545b6269,
128128
0x70777e85, 0x8c939aa1, 0xa8afb6bd, 0xc4cbd2d9,
@@ -137,23 +137,23 @@ static const sm4_u32_t CK[32] =
137137
/*
138138
* defined in section 7.3 (1) L'
139139
*/
140-
LTC_INLINE static sm4_u32_t _L(sm4_u32_t B)
140+
LTC_INLINE static sm4_u32_t s_sm4_L73(sm4_u32_t B)
141141
{
142142
return B ^ ROLc(B, 13) ^ ROLc(B, 23);
143143
}
144144

145145
/*
146146
* defined in section 7.3 (1) T'
147147
*/
148-
LTC_INLINE static sm4_u32_t _T(sm4_u32_t Z)
148+
LTC_INLINE static sm4_u32_t s_sm4_T73(sm4_u32_t Z)
149149
{
150-
return _L(t(Z));
150+
return s_sm4_L73(s_sm4_t(Z));
151151
}
152152

153153
/*
154154
* defined in section 7.3 Key Expansion
155155
*/
156-
LTC_INLINE static void mk2rk(sm4_u32_t rk[32], sm4_u8_t mk[16])
156+
LTC_INLINE static void s_sm4_mk2rk(sm4_u32_t rk[32], sm4_u8_t mk[16])
157157
{
158158
sm4_u32_t MK[4] = { 0 };
159159
sm4_u32_t K[4+32] = { 0 };
@@ -165,25 +165,25 @@ LTC_INLINE static void mk2rk(sm4_u32_t rk[32], sm4_u8_t mk[16])
165165
LOAD32H(MK[3], mk + 12);
166166

167167
for (i = 0; i < 4; ++i)
168-
K[i] = MK[i] ^ FK[i];
168+
K[i] = MK[i] ^ sm4_FK[i];
169169
for (i = 0; i < 32; ++i)
170-
K[i+4] = K[i] ^ _T(K[i+1] ^ K[i+2] ^ K[i+3] ^ CK[i]);
170+
K[i+4] = K[i] ^ s_sm4_T73(K[i+1] ^ K[i+2] ^ K[i+3] ^ sm4_CK[i]);
171171
for (i = 0; i < 32; ++i)
172172
rk[i] = K[i+4];
173173
}
174174

175175
/*
176176
* defined in section 6 Round Function F
177177
*/
178-
LTC_INLINE static sm4_u32_t F(sm4_u32_t X[4], sm4_u32_t rk)
178+
LTC_INLINE static sm4_u32_t s_sm4_F(sm4_u32_t X[4], sm4_u32_t rk)
179179
{
180-
return X[0] ^ T(X[1] ^ X[2] ^ X[3] ^ rk);
180+
return X[0] ^ s_sm4_T62(X[1] ^ X[2] ^ X[3] ^ rk);
181181
}
182182

183183
/*
184184
* defined in section 7.1 (2) The reverse transformation
185185
*/
186-
LTC_INLINE static void R(sm4_u32_t Y[4], sm4_u32_t X[32+4])
186+
LTC_INLINE static void s_sm4_R(sm4_u32_t Y[4], sm4_u32_t X[32+4])
187187
{
188188
Y[0] = X[35];
189189
Y[1] = X[34];
@@ -194,20 +194,20 @@ LTC_INLINE static void R(sm4_u32_t Y[4], sm4_u32_t X[32+4])
194194
/*
195195
* defined in section 7.1 (En)cryption
196196
*/
197-
LTC_INLINE static void sm4_crypt(sm4_u32_t Y[4], sm4_u32_t X[4+32], const sm4_u32_t rk[32])
197+
LTC_INLINE static void s_sm4_crypt(sm4_u32_t Y[4], sm4_u32_t X[4+32], const sm4_u32_t rk[32])
198198
{
199199
int i;
200200

201201
for (i = 0; i < 32; ++i)
202-
X[i+4] = F(X+i, rk[i]);
203-
R(Y, X);
202+
X[i+4] = s_sm4_F(X+i, rk[i]);
203+
s_sm4_R(Y, X);
204204
}
205205

206-
LTC_INLINE static void sm4_setkey(struct sm4_key *sm4, const unsigned char *key)
206+
LTC_INLINE static void s_sm4_setkey(struct sm4_key *sm4, const unsigned char *key)
207207
{
208208
int i;
209209

210-
mk2rk(sm4->ek,(void*)key);
210+
s_sm4_mk2rk(sm4->ek,(void*)key);
211211
/*swap key sequence when decrypt cipher*/
212212
for (i = 0; i < 32; ++i)
213213
sm4->dk[i] = sm4->ek[32 - 1 - i];
@@ -222,14 +222,14 @@ int sm4_setup(const unsigned char *key, int keylen,
222222
return CRYPT_INVALID_ROUNDS;
223223
if (keylen != 16)
224224
return CRYPT_INVALID_KEYSIZE;
225-
sm4_setkey(&(skey->sm4), key);
225+
s_sm4_setkey(&(skey->sm4), key);
226226
return CRYPT_OK;
227227
}
228228

229229
/*
230230
* SM4 encryption.
231231
*/
232-
LTC_INLINE static void sm4_do(void *output, const void *input, const sm4_u32_t rk[32])
232+
LTC_INLINE static void s_sm4_do(void *output, const void *input, const sm4_u32_t rk[32])
233233
{
234234
sm4_u32_t Y[4];
235235
sm4_u32_t X[32+4];
@@ -239,7 +239,7 @@ LTC_INLINE static void sm4_do(void *output, const void *input, const sm4_u32_t r
239239
LOAD32H(X[2], (sm4_u8_t *)input + 8);
240240
LOAD32H(X[3], (sm4_u8_t *)input + 12);
241241

242-
sm4_crypt(Y, X, rk);
242+
s_sm4_crypt(Y, X, rk);
243243

244244
STORE32H(Y[0], (sm4_u8_t *)output );
245245
STORE32H(Y[1], (sm4_u8_t *)output + 4);
@@ -253,7 +253,7 @@ int sm4_ecb_encrypt(const unsigned char *pt, unsigned char *ct,
253253
LTC_ARGCHK(pt != NULL);
254254
LTC_ARGCHK(ct != NULL);
255255
LTC_ARGCHK(skey != NULL);
256-
sm4_do(ct, pt, skey->sm4.ek);
256+
s_sm4_do(ct, pt, skey->sm4.ek);
257257
return CRYPT_OK;
258258
}
259259
int sm4_ecb_decrypt(const unsigned char *ct, unsigned char *pt,
@@ -262,7 +262,7 @@ int sm4_ecb_decrypt(const unsigned char *ct, unsigned char *pt,
262262
LTC_ARGCHK(pt != NULL);
263263
LTC_ARGCHK(ct != NULL);
264264
LTC_ARGCHK(skey != NULL);
265-
sm4_do(pt, ct, skey->sm4.dk);
265+
s_sm4_do(pt, ct, skey->sm4.dk);
266266
return CRYPT_OK;
267267
}
268268

@@ -284,6 +284,7 @@ int sm4_keysize(int *keysize)
284284
* libtomcrypt interface is used
285285
*/
286286

287+
#ifdef LTC_TEST
287288
static int sm4_self_test_ltc(void)
288289
{
289290
int result;
@@ -348,6 +349,7 @@ static int sm4_self_test_ltc(void)
348349

349350
return result;
350351
}
352+
#endif
351353

352354
int sm4_test(void)
353355
{

0 commit comments

Comments
 (0)