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+
1321const 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)
3947static 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 ;
0 commit comments