Skip to content

Commit 1bf2e71

Browse files
committed
Cygwin fix for SM4 (issue #663)
1 parent ab16280 commit 1bf2e71

File tree

1 file changed

+16
-14
lines changed

1 file changed

+16
-14
lines changed

src/ciphers/sm4.c

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ 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_sbox(sm4_u8_t a)
7171
{
7272
return sbox_table[(a >> 4) & 0x0f][a & 0x0f];
7373
}
@@ -80,35 +80,35 @@ 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_trans(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_sbox(a[0]);
91+
b[1] = s_sbox(a[1]);
92+
b[2] = s_sbox(a[2]);
93+
b[3] = s_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_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_T62(sm4_u32_t Z)
110110
{
111-
return L(t(Z));
111+
return s_L62(s_trans(Z));
112112
}
113113

114114
/*
@@ -137,17 +137,17 @@ 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_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_T73(sm4_u32_t Z)
149149
{
150-
return _L(t(Z));
150+
return s_L73(s_trans(Z));
151151
}
152152

153153
/*
@@ -167,7 +167,7 @@ LTC_INLINE static void mk2rk(sm4_u32_t rk[32], sm4_u8_t mk[16])
167167
for (i = 0; i < 4; ++i)
168168
K[i] = MK[i] ^ 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_T73(K[i+1] ^ K[i+2] ^ K[i+3] ^ CK[i]);
171171
for (i = 0; i < 32; ++i)
172172
rk[i] = K[i+4];
173173
}
@@ -177,7 +177,7 @@ LTC_INLINE static void mk2rk(sm4_u32_t rk[32], sm4_u8_t mk[16])
177177
*/
178178
LTC_INLINE static sm4_u32_t 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_T62(X[1] ^ X[2] ^ X[3] ^ rk);
181181
}
182182

183183
/*
@@ -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)