Skip to content

Commit 77afa82

Browse files
committed
Add option LTC_SMALL_STACK.
Signed-off-by: Steffen Jaeckel <s@jaeckel.eu>
1 parent a9cd3cd commit 77afa82

5 files changed

Lines changed: 111 additions & 27 deletions

File tree

.github/workflows/main.yml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,12 @@ jobs:
4747
- { BUILDNAME: 'STOCK', BUILDOPTIONS: '', BUILDSCRIPT: '.ci/run.sh' }
4848
- { BUILDNAME: 'STOCK-MPI', BUILDOPTIONS: '-ULTM_DESC -UTFM_DESC -UUSE_LTM -UUSE_TFM', BUILDSCRIPT: '.ci/run.sh' }
4949
- { BUILDNAME: 'EASY', BUILDOPTIONS: '-DLTC_EASY', BUILDSCRIPT: '.ci/run.sh' }
50-
- { BUILDNAME: 'SMALL', BUILDOPTIONS: '-DLTC_SMALL_CODE', BUILDSCRIPT: '.ci/run.sh' }
50+
- { BUILDNAME: 'SMALL_CODE', BUILDOPTIONS: '-DLTC_SMALL_CODE', BUILDSCRIPT: '.ci/run.sh' }
51+
- { BUILDNAME: 'SMALL_STACK', BUILDOPTIONS: '-DLTC_SMALL_STACK', BUILDSCRIPT: '.ci/run.sh' }
52+
- { BUILDNAME: 'SMALL', BUILDOPTIONS: '-DLTC_SMALL_CODE -DLTC_SMALL_STACK', BUILDSCRIPT: '.ci/run.sh' }
5153
- { BUILDNAME: 'NO_TABLES', BUILDOPTIONS: '-DLTC_NO_TABLES', BUILDSCRIPT: '.ci/run.sh' }
5254
- { BUILDNAME: 'NO_FAST', BUILDOPTIONS: '-DLTC_NO_FAST', BUILDSCRIPT: '.ci/run.sh' }
53-
- { BUILDNAME: 'NO_FAST+SMALL+NO_TABLES', BUILDOPTIONS: '-DLTC_NO_FAST -DLTC_SMALL_CODE -DLTC_NO_TABLES', BUILDSCRIPT: '.ci/run.sh' }
55+
- { BUILDNAME: 'NO_FAST+SMALL+NO_TABLES', BUILDOPTIONS: '-DLTC_NO_FAST -DLTC_SMALL_CODE -DLTC_SMALL_STACK -DLTC_NO_TABLES', BUILDSCRIPT: '.ci/run.sh' }
5456
- { BUILDNAME: 'NO_ASM', BUILDOPTIONS: '-DLTC_NO_ASM', BUILDSCRIPT: '.ci/run.sh' }
5557
- { BUILDNAME: 'NO_DEPRECATED_APIS', BUILDOPTIONS: '-DLTC_NO_DEPRECATED_APIS', BUILDSCRIPT: '.ci/run.sh' }
5658
- { BUILDNAME: 'NO_TIMING_RESISTANCE', BUILDOPTIONS: '-DLTC_NO_ECC_TIMING_RESISTANT -DLTC_NO_RSA_BLINDING', BUILDSCRIPT: '.ci/run.sh' }

src/hashes/sha1.c

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,20 @@
44

55
/**
66
@file sha1.c
7-
LTC_SHA1 code by Tom St Denis
7+
SHA1 code by Tom St Denis
88
*/
99

1010

1111
#ifdef LTC_SHA1
1212

13+
/* While implementing the SMALL STACK option in https://github.com/libtom/libtomcrypt/pull/709
14+
* we came to the conclusion that SHA1 profits from the SMALL STACK option when the SMALL CODE
15+
* option is enabled, so let's do that.
16+
*/
17+
#if defined(LTC_SMALL_STACK) || defined(LTC_SMALL_CODE)
18+
#define LTC_SMALL_STACK_SHA1
19+
#endif
20+
1321
const struct ltc_hash_descriptor sha1_desc =
1422
{
1523
"sha1",
@@ -39,7 +47,12 @@ static int ss_sha1_compress(hash_state *md, const unsigned char *buf)
3947
static int s_sha1_compress(hash_state *md, const unsigned char *buf)
4048
#endif
4149
{
42-
ulong32 a,b,c,d,e,W[16],i;
50+
ulong32 a,b,c,d,e,i;
51+
#ifdef LTC_SMALL_STACK_SHA1
52+
ulong32 W[16];
53+
#else
54+
ulong32 W[80];
55+
#endif
4356
#ifdef LTC_SMALL_CODE
4457
ulong32 t;
4558
#endif
@@ -48,7 +61,6 @@ static int s_sha1_compress(hash_state *md, const unsigned char *buf)
4861
for (i = 0; i < 16; i++) {
4962
LOAD32H(W[i], buf + (4*i));
5063
}
51-
#define Wi(i) W[(i) % 16] = ROL(W[((i) - 3) % 16] ^ W[((i) - 8) % 16] ^ W[((i) - 14) % 16] ^ W[((i) - 16) % 16], 1);
5264

5365
/* copy state */
5466
a = md->sha1.state[0];
@@ -57,12 +69,24 @@ static int s_sha1_compress(hash_state *md, const unsigned char *buf)
5769
d = md->sha1.state[3];
5870
e = md->sha1.state[4];
5971

72+
#ifdef LTC_SMALL_STACK_SHA1
73+
#define Wi(i) do { W[(i) % 16] = ROL(W[((i) - 3) % 16] ^ W[((i) - 8) % 16] ^ W[((i) - 14) % 16] ^ W[((i) - 16) % 16], 1); } while(0)
74+
#define Windex(i) ((i) % 16)
75+
#else
76+
#define Wi(i) do { } while(0)
77+
#define Windex(i) (i)
78+
/* expand it */
79+
for (i = 16; i < 80; i++) {
80+
W[i] = ROL(W[i-3] ^ W[i-8] ^ W[i-14] ^ W[i-16], 1);
81+
}
82+
#endif
83+
6084
/* compress */
6185
/* round one */
62-
#define FF0(a,b,c,d,e,i) e = (ROLc(a, 5) + F0(b,c,d) + e + W[(i) % 16] + 0x5a827999UL); b = ROLc(b, 30);
63-
#define FF1(a,b,c,d,e,i) e = (ROLc(a, 5) + F1(b,c,d) + e + W[(i) % 16] + 0x6ed9eba1UL); b = ROLc(b, 30);
64-
#define FF2(a,b,c,d,e,i) e = (ROLc(a, 5) + F2(b,c,d) + e + W[(i) % 16] + 0x8f1bbcdcUL); b = ROLc(b, 30);
65-
#define FF3(a,b,c,d,e,i) e = (ROLc(a, 5) + F3(b,c,d) + e + W[(i) % 16] + 0xca62c1d6UL); b = ROLc(b, 30);
86+
#define FF0(a,b,c,d,e,i) e = (ROLc(a, 5) + F0(b,c,d) + e + W[Windex(i)] + 0x5a827999UL); b = ROLc(b, 30);
87+
#define FF1(a,b,c,d,e,i) e = (ROLc(a, 5) + F1(b,c,d) + e + W[Windex(i)] + 0x6ed9eba1UL); b = ROLc(b, 30);
88+
#define FF2(a,b,c,d,e,i) e = (ROLc(a, 5) + F2(b,c,d) + e + W[Windex(i)] + 0x8f1bbcdcUL); b = ROLc(b, 30);
89+
#define FF3(a,b,c,d,e,i) e = (ROLc(a, 5) + F3(b,c,d) + e + W[Windex(i)] + 0xca62c1d6UL); b = ROLc(b, 30);
6690

6791
#ifdef LTC_SMALL_CODE
6892

@@ -133,6 +157,7 @@ static int s_sha1_compress(hash_state *md, const unsigned char *buf)
133157
#undef FF2
134158
#undef FF3
135159
#undef Wi
160+
#undef Windex
136161

137162
/* store */
138163
md->sha1.state[0] = md->sha1.state[0] + a;

src/hashes/sha2/sha256.c

Lines changed: 45 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
/**
66
@file sha256.c
7-
LTC_SHA256 by Tom St Denis
7+
SHA256 by Tom St Denis
88
*/
99

1010
#ifdef LTC_SHA256
@@ -27,6 +27,15 @@ const struct ltc_hash_descriptor sha256_desc =
2727
NULL
2828
};
2929

30+
/* While implementing the SMALL STACK option in https://github.com/libtom/libtomcrypt/pull/709
31+
* we came to the conclusion that SHA256 profits from the SMALL STACK option when the SMALL CODE
32+
* option is disabled.
33+
* So enable it either when it's enabled explicitly, or when SMALL CODE is disabled.
34+
*/
35+
#if !defined(LTC_SMALL_CODE) || defined(LTC_SMALL_STACK)
36+
#define LTC_SMALL_STACK_SHA256
37+
#endif
38+
3039
#ifdef LTC_SMALL_CODE
3140
/* the K array */
3241
static const ulong32 K[64] = {
@@ -63,7 +72,12 @@ static int ss_sha256_compress(hash_state * md, const unsigned char *buf)
6372
static int s_sha256_compress(hash_state * md, const unsigned char *buf)
6473
#endif
6574
{
66-
ulong32 S[8], W[16], t0, t1;
75+
ulong32 S[8], t0, t1;
76+
#ifdef LTC_SMALL_STACK_SHA256
77+
ulong32 W[16];
78+
#else
79+
ulong32 W[64];
80+
#endif
6781
#ifdef LTC_SMALL_CODE
6882
ulong32 t;
6983
#endif
@@ -78,16 +92,29 @@ static int s_sha256_compress(hash_state * md, const unsigned char *buf)
7892
for (i = 0; i < 16; i++) {
7993
LOAD32H(W[i], buf + (4*i));
8094
}
95+
96+
#ifdef LTC_SMALL_STACK_SHA256
8197
#define Wi(i) W[(i) % 16] = Gamma1(W[((i) - 2) % 16]) + W[((i) - 7) % 16] + Gamma0(W[((i) - 15) % 16]) + W[((i) - 16) % 16]
98+
#define Windex(i) ((i) % 16)
99+
#else
100+
#define Wi(i) do { } while(0)
101+
#define Windex(i) (i)
102+
103+
/* fill W[16..63] */
104+
for (i = 16; i < 64; i++) {
105+
W[i] = Gamma1(W[i - 2]) + W[i - 7] + Gamma0(W[i - 15]) + W[i - 16];
106+
}
107+
#endif
82108

83109
/* Compress */
84110
#ifdef LTC_SMALL_CODE
85-
#define RND(a,b,c,d,e,f,g,h,i) \
86-
t0 = h + Sigma1(e) + Ch(e, f, g) + K[i] + W[(i) % 16]; \
87-
t1 = Sigma0(a) + Maj(a, b, c); \
88-
d += t0; \
111+
#define RND(a,b,c,d,e,f,g,h,i) \
112+
t0 = h + Sigma1(e) + Ch(e, f, g) + K[i] + W[Windex(i)]; \
113+
t1 = Sigma0(a) + Maj(a, b, c); \
114+
d += t0; \
89115
h = t0 + t1;
90116

117+
#ifdef LTC_SMALL_STACK_SHA256
91118
for (i = 0; i < 16; ++i) {
92119
RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],i);
93120
t = S[7]; S[7] = S[6]; S[6] = S[5]; S[5] = S[4];
@@ -100,10 +127,17 @@ static int s_sha256_compress(hash_state * md, const unsigned char *buf)
100127
S[4] = S[3]; S[3] = S[2]; S[2] = S[1]; S[1] = S[0]; S[0] = t;
101128
}
102129
#else
103-
#define RND(a,b,c,d,e,f,g,h,i,ki) \
104-
t0 = h + Sigma1(e) + Ch(e, f, g) + ki + W[(i) % 16]; \
105-
t1 = Sigma0(a) + Maj(a, b, c); \
106-
d += t0; \
130+
for (i = 0; i < 64; ++i) {
131+
RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],i);
132+
t = S[7]; S[7] = S[6]; S[6] = S[5]; S[5] = S[4];
133+
S[4] = S[3]; S[3] = S[2]; S[2] = S[1]; S[1] = S[0]; S[0] = t;
134+
}
135+
#endif /* LTC_SMALL_STACK_SHA256 */
136+
#else
137+
#define RND(a,b,c,d,e,f,g,h,i,ki) \
138+
t0 = h + Sigma1(e) + Ch(e, f, g) + ki + W[Windex(i)]; \
139+
t1 = Sigma0(a) + Maj(a, b, c); \
140+
d += t0; \
107141
h = t0 + t1;
108142

109143
RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],0,0x428a2f98);
@@ -173,6 +207,7 @@ static int s_sha256_compress(hash_state * md, const unsigned char *buf)
173207
#endif
174208
#undef RND
175209
#undef Wi
210+
#undef Windex
176211

177212
/* feedback */
178213
for (i = 0; i < 8; i++) {

src/hashes/sha2/sha512.c

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
/**
66
@param sha512.c
7-
LTC_SHA512 by Tom St Denis
7+
SHA512 by Tom St Denis
88
*/
99

1010
#ifdef LTC_SHA512
@@ -88,7 +88,12 @@ static int ss_sha512_compress(hash_state * md, const unsigned char *buf)
8888
static int s_sha512_compress(hash_state * md, const unsigned char *buf)
8989
#endif
9090
{
91-
ulong64 S[8], W[16], t0, t1;
91+
ulong64 S[8], t0, t1;
92+
#ifdef LTC_SMALL_STACK
93+
ulong64 W[16];
94+
#else
95+
ulong64 W[80];
96+
#endif
9297
int i;
9398

9499
/* copy state into S */
@@ -100,12 +105,24 @@ static int s_sha512_compress(hash_state * md, const unsigned char *buf)
100105
for (i = 0; i < 16; i++) {
101106
LOAD64H(W[i], buf + (8*i));
102107
}
108+
109+
#ifdef LTC_SMALL_STACK
103110
#define Wi(i) W[(i) % 16] = Gamma1(W[((i) - 2) % 16]) + W[((i) - 7) % 16] + Gamma0(W[((i) - 15) % 16]) + W[((i) - 16) % 16];
111+
#define Windex(i) ((i) % 16)
112+
#else
113+
#define Wi(i) do { } while(0)
114+
#define Windex(i) (i)
115+
116+
/* fill W[16..79] */
117+
for (i = 16; i < 80; i++) {
118+
W[i] = Gamma1(W[i - 2]) + W[i - 7] + Gamma0(W[i - 15]) + W[i - 16];
119+
}
120+
#endif
104121

105122
/* Compress */
106123
#ifdef LTC_SMALL_CODE
107124
for (i = 0; i < 16; i++) {
108-
t0 = S[7] + Sigma1(S[4]) + Ch(S[4], S[5], S[6]) + K[i] + W[i % 16];
125+
t0 = S[7] + Sigma1(S[4]) + Ch(S[4], S[5], S[6]) + K[i] + W[Windex(i)];
109126
t1 = Sigma0(S[0]) + Maj(S[0], S[1], S[2]);
110127
S[7] = S[6];
111128
S[6] = S[5];
@@ -118,7 +135,7 @@ static int s_sha512_compress(hash_state * md, const unsigned char *buf)
118135
}
119136
for (; i < 80; i++) {
120137
Wi(i);
121-
t0 = S[7] + Sigma1(S[4]) + Ch(S[4], S[5], S[6]) + K[i] + W[i % 16];
138+
t0 = S[7] + Sigma1(S[4]) + Ch(S[4], S[5], S[6]) + K[i] + W[Windex(i)];
122139
t1 = Sigma0(S[0]) + Maj(S[0], S[1], S[2]);
123140
S[7] = S[6];
124141
S[6] = S[5];
@@ -130,10 +147,10 @@ static int s_sha512_compress(hash_state * md, const unsigned char *buf)
130147
S[0] = t0 + t1;
131148
}
132149
#else
133-
#define RND(a,b,c,d,e,f,g,h,i) \
134-
t0 = h + Sigma1(e) + Ch(e, f, g) + K[i] + W[(i) % 16]; \
135-
t1 = Sigma0(a) + Maj(a, b, c); \
136-
d += t0; \
150+
#define RND(a,b,c,d,e,f,g,h,i) \
151+
t0 = h + Sigma1(e) + Ch(e, f, g) + K[i] + W[Windex(i)]; \
152+
t1 = Sigma0(a) + Maj(a, b, c); \
153+
d += t0; \
137154
h = t0 + t1;
138155

139156
for (i = 0; i < 16; i += 8) {
@@ -156,8 +173,10 @@ static int s_sha512_compress(hash_state * md, const unsigned char *buf)
156173
Wi(i+6); RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],i+6);
157174
Wi(i+7); RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],i+7);
158175
}
176+
#undef RND
159177
#endif
160178
#undef Wi
179+
#undef Windex
161180

162181

163182
/* feedback */

src/misc/crypt/crypt.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -528,6 +528,9 @@ const char *crypt_build_settings =
528528
#if defined(LTC_SMALL_CODE)
529529
" LTC_SMALL_CODE "
530530
#endif
531+
#if defined(LTC_SMALL_STACK)
532+
" LTC_SMALL_STACK "
533+
#endif
531534
#if defined(LTC_NO_FILE)
532535
" LTC_NO_FILE "
533536
#endif

0 commit comments

Comments
 (0)