41 #include "netif/ppp/ppp_opts.h" 42 #if PPP_SUPPORT && LWIP_INCLUDED_POLARSSL_SHA1 44 #include "netif/ppp/polarssl/sha1.h" 52 #define GET_ULONG_BE(n,b,i) \ 54 (n) = ( (unsigned long) (b)[(i) ] << 24 ) \ 55 | ( (unsigned long) (b)[(i) + 1] << 16 ) \ 56 | ( (unsigned long) (b)[(i) + 2] << 8 ) \ 57 | ( (unsigned long) (b)[(i) + 3] ); \ 62 #define PUT_ULONG_BE(n,b,i) \ 64 (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \ 65 (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \ 66 (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \ 67 (b)[(i) + 3] = (unsigned char) ( (n) ); \ 74 void sha1_starts( sha1_context *ctx )
79 ctx->state[0] = 0x67452301;
80 ctx->state[1] = 0xEFCDAB89;
81 ctx->state[2] = 0x98BADCFE;
82 ctx->state[3] = 0x10325476;
83 ctx->state[4] = 0xC3D2E1F0;
86 static void sha1_process( sha1_context *ctx,
const unsigned char data[64] )
88 unsigned long temp, W[16], A, B, C, D, E;
90 GET_ULONG_BE( W[ 0], data, 0 );
91 GET_ULONG_BE( W[ 1], data, 4 );
92 GET_ULONG_BE( W[ 2], data, 8 );
93 GET_ULONG_BE( W[ 3], data, 12 );
94 GET_ULONG_BE( W[ 4], data, 16 );
95 GET_ULONG_BE( W[ 5], data, 20 );
96 GET_ULONG_BE( W[ 6], data, 24 );
97 GET_ULONG_BE( W[ 7], data, 28 );
98 GET_ULONG_BE( W[ 8], data, 32 );
99 GET_ULONG_BE( W[ 9], data, 36 );
100 GET_ULONG_BE( W[10], data, 40 );
101 GET_ULONG_BE( W[11], data, 44 );
102 GET_ULONG_BE( W[12], data, 48 );
103 GET_ULONG_BE( W[13], data, 52 );
104 GET_ULONG_BE( W[14], data, 56 );
105 GET_ULONG_BE( W[15], data, 60 );
107 #define S(x,n) ((x << n) | ((x & 0xFFFFFFFF) >> (32 - n))) 111 temp = W[(t - 3) & 0x0F] ^ W[(t - 8) & 0x0F] ^ \ 112 W[(t - 14) & 0x0F] ^ W[ t & 0x0F], \ 113 ( W[t & 0x0F] = S(temp,1) ) \ 116 #define P(a,b,c,d,e,x) \ 118 e += S(a,5) + F(b,c,d) + K + x; b = S(b,30); \ 127 #define F(x,y,z) (z ^ (x & (y ^ z))) 130 P( A, B, C, D, E, W[0] );
131 P( E, A, B, C, D, W[1] );
132 P( D, E, A, B, C, W[2] );
133 P( C, D, E, A, B, W[3] );
134 P( B, C, D, E, A, W[4] );
135 P( A, B, C, D, E, W[5] );
136 P( E, A, B, C, D, W[6] );
137 P( D, E, A, B, C, W[7] );
138 P( C, D, E, A, B, W[8] );
139 P( B, C, D, E, A, W[9] );
140 P( A, B, C, D, E, W[10] );
141 P( E, A, B, C, D, W[11] );
142 P( D, E, A, B, C, W[12] );
143 P( C, D, E, A, B, W[13] );
144 P( B, C, D, E, A, W[14] );
145 P( A, B, C, D, E, W[15] );
146 P( E, A, B, C, D, R(16) );
147 P( D, E, A, B, C, R(17) );
148 P( C, D, E, A, B, R(18) );
149 P( B, C, D, E, A, R(19) );
154 #define F(x,y,z) (x ^ y ^ z) 157 P( A, B, C, D, E, R(20) );
158 P( E, A, B, C, D, R(21) );
159 P( D, E, A, B, C, R(22) );
160 P( C, D, E, A, B, R(23) );
161 P( B, C, D, E, A, R(24) );
162 P( A, B, C, D, E, R(25) );
163 P( E, A, B, C, D, R(26) );
164 P( D, E, A, B, C, R(27) );
165 P( C, D, E, A, B, R(28) );
166 P( B, C, D, E, A, R(29) );
167 P( A, B, C, D, E, R(30) );
168 P( E, A, B, C, D, R(31) );
169 P( D, E, A, B, C, R(32) );
170 P( C, D, E, A, B, R(33) );
171 P( B, C, D, E, A, R(34) );
172 P( A, B, C, D, E, R(35) );
173 P( E, A, B, C, D, R(36) );
174 P( D, E, A, B, C, R(37) );
175 P( C, D, E, A, B, R(38) );
176 P( B, C, D, E, A, R(39) );
181 #define F(x,y,z) ((x & y) | (z & (x | y))) 184 P( A, B, C, D, E, R(40) );
185 P( E, A, B, C, D, R(41) );
186 P( D, E, A, B, C, R(42) );
187 P( C, D, E, A, B, R(43) );
188 P( B, C, D, E, A, R(44) );
189 P( A, B, C, D, E, R(45) );
190 P( E, A, B, C, D, R(46) );
191 P( D, E, A, B, C, R(47) );
192 P( C, D, E, A, B, R(48) );
193 P( B, C, D, E, A, R(49) );
194 P( A, B, C, D, E, R(50) );
195 P( E, A, B, C, D, R(51) );
196 P( D, E, A, B, C, R(52) );
197 P( C, D, E, A, B, R(53) );
198 P( B, C, D, E, A, R(54) );
199 P( A, B, C, D, E, R(55) );
200 P( E, A, B, C, D, R(56) );
201 P( D, E, A, B, C, R(57) );
202 P( C, D, E, A, B, R(58) );
203 P( B, C, D, E, A, R(59) );
208 #define F(x,y,z) (x ^ y ^ z) 211 P( A, B, C, D, E, R(60) );
212 P( E, A, B, C, D, R(61) );
213 P( D, E, A, B, C, R(62) );
214 P( C, D, E, A, B, R(63) );
215 P( B, C, D, E, A, R(64) );
216 P( A, B, C, D, E, R(65) );
217 P( E, A, B, C, D, R(66) );
218 P( D, E, A, B, C, R(67) );
219 P( C, D, E, A, B, R(68) );
220 P( B, C, D, E, A, R(69) );
221 P( A, B, C, D, E, R(70) );
222 P( E, A, B, C, D, R(71) );
223 P( D, E, A, B, C, R(72) );
224 P( C, D, E, A, B, R(73) );
225 P( B, C, D, E, A, R(74) );
226 P( A, B, C, D, E, R(75) );
227 P( E, A, B, C, D, R(76) );
228 P( D, E, A, B, C, R(77) );
229 P( C, D, E, A, B, R(78) );
230 P( B, C, D, E, A, R(79) );
245 void sha1_update( sha1_context *ctx,
const unsigned char *input,
int ilen )
253 left = ctx->total[0] & 0x3F;
256 ctx->total[0] += ilen;
257 ctx->total[0] &= 0xFFFFFFFF;
259 if( ctx->total[0] < (
unsigned long) ilen )
262 if( left && ilen >= fill )
264 MEMCPY( (
void *) (ctx->buffer + left),
266 sha1_process( ctx, ctx->buffer );
274 sha1_process( ctx, input );
281 MEMCPY( (
void *) (ctx->buffer + left),
286 static const unsigned char sha1_padding[64] =
288 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
289 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
290 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
291 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
297 void sha1_finish( sha1_context *ctx,
unsigned char output[20] )
299 unsigned long last, padn;
300 unsigned long high, low;
301 unsigned char msglen[8];
303 high = ( ctx->total[0] >> 29 )
304 | ( ctx->total[1] << 3 );
305 low = ( ctx->total[0] << 3 );
307 PUT_ULONG_BE( high, msglen, 0 );
308 PUT_ULONG_BE( low, msglen, 4 );
310 last = ctx->total[0] & 0x3F;
311 padn = ( last < 56 ) ? ( 56 - last ) : ( 120 - last );
313 sha1_update( ctx, sha1_padding, padn );
314 sha1_update( ctx, msglen, 8 );
316 PUT_ULONG_BE( ctx->state[0], output, 0 );
317 PUT_ULONG_BE( ctx->state[1], output, 4 );
318 PUT_ULONG_BE( ctx->state[2], output, 8 );
319 PUT_ULONG_BE( ctx->state[3], output, 12 );
320 PUT_ULONG_BE( ctx->state[4], output, 16 );
326 void sha1(
unsigned char *input,
int ilen,
unsigned char output[20] )
331 sha1_update( &ctx, input, ilen );
332 sha1_finish( &ctx, output );