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 */
3241static const ulong32 K [64 ] = {
@@ -63,7 +72,12 @@ static int ss_sha256_compress(hash_state * md, const unsigned char *buf)
6372static 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 ++ ) {
0 commit comments