source: azure_iot_hub_mbedtls/trunk/mbedtls-2.16.1/library/sha1.c@ 398

Last change on this file since 398 was 398, checked in by coas-nagasima, 5 years ago

mbedTLS版Azure IoT Hub接続サンプルのソースコードを追加

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/x-csrc;charset=UTF-8
File size: 14.1 KB
Line 
1/*
2 * FIPS-180-1 compliant SHA-1 implementation
3 *
4 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
5 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 *
19 * This file is part of mbed TLS (https://tls.mbed.org)
20 */
21/*
22 * The SHA-1 standard was published by NIST in 1993.
23 *
24 * http://www.itl.nist.gov/fipspubs/fip180-1.htm
25 */
26
27#if !defined(MBEDTLS_CONFIG_FILE)
28#include "mbedtls/config.h"
29#else
30#include MBEDTLS_CONFIG_FILE
31#endif
32
33#if defined(MBEDTLS_SHA1_C)
34
35#include "mbedtls/sha1.h"
36#include "mbedtls/platform_util.h"
37
38#include <string.h>
39
40#if defined(MBEDTLS_SELF_TEST)
41#if defined(MBEDTLS_PLATFORM_C)
42#include "mbedtls/platform.h"
43#else
44#include <stdio.h>
45#define mbedtls_printf printf
46#endif /* MBEDTLS_PLATFORM_C */
47#endif /* MBEDTLS_SELF_TEST */
48
49#define SHA1_VALIDATE_RET(cond) \
50 MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_SHA1_BAD_INPUT_DATA )
51
52#define SHA1_VALIDATE(cond) MBEDTLS_INTERNAL_VALIDATE( cond )
53
54#if !defined(MBEDTLS_SHA1_ALT)
55
56/*
57 * 32-bit integer manipulation macros (big endian)
58 */
59#ifndef GET_UINT32_BE
60#define GET_UINT32_BE(n,b,i) \
61{ \
62 (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
63 | ( (uint32_t) (b)[(i) + 1] << 16 ) \
64 | ( (uint32_t) (b)[(i) + 2] << 8 ) \
65 | ( (uint32_t) (b)[(i) + 3] ); \
66}
67#endif
68
69#ifndef PUT_UINT32_BE
70#define PUT_UINT32_BE(n,b,i) \
71{ \
72 (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
73 (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
74 (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
75 (b)[(i) + 3] = (unsigned char) ( (n) ); \
76}
77#endif
78
79void mbedtls_sha1_init( mbedtls_sha1_context *ctx )
80{
81 SHA1_VALIDATE( ctx != NULL );
82
83 memset( ctx, 0, sizeof( mbedtls_sha1_context ) );
84}
85
86void mbedtls_sha1_free( mbedtls_sha1_context *ctx )
87{
88 if( ctx == NULL )
89 return;
90
91 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_sha1_context ) );
92}
93
94void mbedtls_sha1_clone( mbedtls_sha1_context *dst,
95 const mbedtls_sha1_context *src )
96{
97 SHA1_VALIDATE( dst != NULL );
98 SHA1_VALIDATE( src != NULL );
99
100 *dst = *src;
101}
102
103/*
104 * SHA-1 context setup
105 */
106int mbedtls_sha1_starts_ret( mbedtls_sha1_context *ctx )
107{
108 SHA1_VALIDATE_RET( ctx != NULL );
109
110 ctx->total[0] = 0;
111 ctx->total[1] = 0;
112
113 ctx->state[0] = 0x67452301;
114 ctx->state[1] = 0xEFCDAB89;
115 ctx->state[2] = 0x98BADCFE;
116 ctx->state[3] = 0x10325476;
117 ctx->state[4] = 0xC3D2E1F0;
118
119 return( 0 );
120}
121
122#if !defined(MBEDTLS_DEPRECATED_REMOVED)
123void mbedtls_sha1_starts( mbedtls_sha1_context *ctx )
124{
125 mbedtls_sha1_starts_ret( ctx );
126}
127#endif
128
129#if !defined(MBEDTLS_SHA1_PROCESS_ALT)
130int mbedtls_internal_sha1_process( mbedtls_sha1_context *ctx,
131 const unsigned char data[64] )
132{
133 uint32_t temp, W[16], A, B, C, D, E;
134
135 SHA1_VALIDATE_RET( ctx != NULL );
136 SHA1_VALIDATE_RET( (const unsigned char *)data != NULL );
137
138 GET_UINT32_BE( W[ 0], data, 0 );
139 GET_UINT32_BE( W[ 1], data, 4 );
140 GET_UINT32_BE( W[ 2], data, 8 );
141 GET_UINT32_BE( W[ 3], data, 12 );
142 GET_UINT32_BE( W[ 4], data, 16 );
143 GET_UINT32_BE( W[ 5], data, 20 );
144 GET_UINT32_BE( W[ 6], data, 24 );
145 GET_UINT32_BE( W[ 7], data, 28 );
146 GET_UINT32_BE( W[ 8], data, 32 );
147 GET_UINT32_BE( W[ 9], data, 36 );
148 GET_UINT32_BE( W[10], data, 40 );
149 GET_UINT32_BE( W[11], data, 44 );
150 GET_UINT32_BE( W[12], data, 48 );
151 GET_UINT32_BE( W[13], data, 52 );
152 GET_UINT32_BE( W[14], data, 56 );
153 GET_UINT32_BE( W[15], data, 60 );
154
155#define S(x,n) ((x << n) | ((x & 0xFFFFFFFF) >> (32 - n)))
156
157#define R(t) \
158( \
159 temp = W[( t - 3 ) & 0x0F] ^ W[( t - 8 ) & 0x0F] ^ \
160 W[( t - 14 ) & 0x0F] ^ W[ t & 0x0F], \
161 ( W[t & 0x0F] = S(temp,1) ) \
162)
163
164#define P(a,b,c,d,e,x) \
165{ \
166 e += S(a,5) + F(b,c,d) + K + x; b = S(b,30); \
167}
168
169 A = ctx->state[0];
170 B = ctx->state[1];
171 C = ctx->state[2];
172 D = ctx->state[3];
173 E = ctx->state[4];
174
175#define F(x,y,z) (z ^ (x & (y ^ z)))
176#define K 0x5A827999
177
178 P( A, B, C, D, E, W[0] );
179 P( E, A, B, C, D, W[1] );
180 P( D, E, A, B, C, W[2] );
181 P( C, D, E, A, B, W[3] );
182 P( B, C, D, E, A, W[4] );
183 P( A, B, C, D, E, W[5] );
184 P( E, A, B, C, D, W[6] );
185 P( D, E, A, B, C, W[7] );
186 P( C, D, E, A, B, W[8] );
187 P( B, C, D, E, A, W[9] );
188 P( A, B, C, D, E, W[10] );
189 P( E, A, B, C, D, W[11] );
190 P( D, E, A, B, C, W[12] );
191 P( C, D, E, A, B, W[13] );
192 P( B, C, D, E, A, W[14] );
193 P( A, B, C, D, E, W[15] );
194 P( E, A, B, C, D, R(16) );
195 P( D, E, A, B, C, R(17) );
196 P( C, D, E, A, B, R(18) );
197 P( B, C, D, E, A, R(19) );
198
199#undef K
200#undef F
201
202#define F(x,y,z) (x ^ y ^ z)
203#define K 0x6ED9EBA1
204
205 P( A, B, C, D, E, R(20) );
206 P( E, A, B, C, D, R(21) );
207 P( D, E, A, B, C, R(22) );
208 P( C, D, E, A, B, R(23) );
209 P( B, C, D, E, A, R(24) );
210 P( A, B, C, D, E, R(25) );
211 P( E, A, B, C, D, R(26) );
212 P( D, E, A, B, C, R(27) );
213 P( C, D, E, A, B, R(28) );
214 P( B, C, D, E, A, R(29) );
215 P( A, B, C, D, E, R(30) );
216 P( E, A, B, C, D, R(31) );
217 P( D, E, A, B, C, R(32) );
218 P( C, D, E, A, B, R(33) );
219 P( B, C, D, E, A, R(34) );
220 P( A, B, C, D, E, R(35) );
221 P( E, A, B, C, D, R(36) );
222 P( D, E, A, B, C, R(37) );
223 P( C, D, E, A, B, R(38) );
224 P( B, C, D, E, A, R(39) );
225
226#undef K
227#undef F
228
229#define F(x,y,z) ((x & y) | (z & (x | y)))
230#define K 0x8F1BBCDC
231
232 P( A, B, C, D, E, R(40) );
233 P( E, A, B, C, D, R(41) );
234 P( D, E, A, B, C, R(42) );
235 P( C, D, E, A, B, R(43) );
236 P( B, C, D, E, A, R(44) );
237 P( A, B, C, D, E, R(45) );
238 P( E, A, B, C, D, R(46) );
239 P( D, E, A, B, C, R(47) );
240 P( C, D, E, A, B, R(48) );
241 P( B, C, D, E, A, R(49) );
242 P( A, B, C, D, E, R(50) );
243 P( E, A, B, C, D, R(51) );
244 P( D, E, A, B, C, R(52) );
245 P( C, D, E, A, B, R(53) );
246 P( B, C, D, E, A, R(54) );
247 P( A, B, C, D, E, R(55) );
248 P( E, A, B, C, D, R(56) );
249 P( D, E, A, B, C, R(57) );
250 P( C, D, E, A, B, R(58) );
251 P( B, C, D, E, A, R(59) );
252
253#undef K
254#undef F
255
256#define F(x,y,z) (x ^ y ^ z)
257#define K 0xCA62C1D6
258
259 P( A, B, C, D, E, R(60) );
260 P( E, A, B, C, D, R(61) );
261 P( D, E, A, B, C, R(62) );
262 P( C, D, E, A, B, R(63) );
263 P( B, C, D, E, A, R(64) );
264 P( A, B, C, D, E, R(65) );
265 P( E, A, B, C, D, R(66) );
266 P( D, E, A, B, C, R(67) );
267 P( C, D, E, A, B, R(68) );
268 P( B, C, D, E, A, R(69) );
269 P( A, B, C, D, E, R(70) );
270 P( E, A, B, C, D, R(71) );
271 P( D, E, A, B, C, R(72) );
272 P( C, D, E, A, B, R(73) );
273 P( B, C, D, E, A, R(74) );
274 P( A, B, C, D, E, R(75) );
275 P( E, A, B, C, D, R(76) );
276 P( D, E, A, B, C, R(77) );
277 P( C, D, E, A, B, R(78) );
278 P( B, C, D, E, A, R(79) );
279
280#undef K
281#undef F
282
283 ctx->state[0] += A;
284 ctx->state[1] += B;
285 ctx->state[2] += C;
286 ctx->state[3] += D;
287 ctx->state[4] += E;
288
289 return( 0 );
290}
291
292#if !defined(MBEDTLS_DEPRECATED_REMOVED)
293void mbedtls_sha1_process( mbedtls_sha1_context *ctx,
294 const unsigned char data[64] )
295{
296 mbedtls_internal_sha1_process( ctx, data );
297}
298#endif
299#endif /* !MBEDTLS_SHA1_PROCESS_ALT */
300
301/*
302 * SHA-1 process buffer
303 */
304int mbedtls_sha1_update_ret( mbedtls_sha1_context *ctx,
305 const unsigned char *input,
306 size_t ilen )
307{
308 int ret;
309 size_t fill;
310 uint32_t left;
311
312 SHA1_VALIDATE_RET( ctx != NULL );
313 SHA1_VALIDATE_RET( ilen == 0 || input != NULL );
314
315 if( ilen == 0 )
316 return( 0 );
317
318 left = ctx->total[0] & 0x3F;
319 fill = 64 - left;
320
321 ctx->total[0] += (uint32_t) ilen;
322 ctx->total[0] &= 0xFFFFFFFF;
323
324 if( ctx->total[0] < (uint32_t) ilen )
325 ctx->total[1]++;
326
327 if( left && ilen >= fill )
328 {
329 memcpy( (void *) (ctx->buffer + left), input, fill );
330
331 if( ( ret = mbedtls_internal_sha1_process( ctx, ctx->buffer ) ) != 0 )
332 return( ret );
333
334 input += fill;
335 ilen -= fill;
336 left = 0;
337 }
338
339 while( ilen >= 64 )
340 {
341 if( ( ret = mbedtls_internal_sha1_process( ctx, input ) ) != 0 )
342 return( ret );
343
344 input += 64;
345 ilen -= 64;
346 }
347
348 if( ilen > 0 )
349 memcpy( (void *) (ctx->buffer + left), input, ilen );
350
351 return( 0 );
352}
353
354#if !defined(MBEDTLS_DEPRECATED_REMOVED)
355void mbedtls_sha1_update( mbedtls_sha1_context *ctx,
356 const unsigned char *input,
357 size_t ilen )
358{
359 mbedtls_sha1_update_ret( ctx, input, ilen );
360}
361#endif
362
363/*
364 * SHA-1 final digest
365 */
366int mbedtls_sha1_finish_ret( mbedtls_sha1_context *ctx,
367 unsigned char output[20] )
368{
369 int ret;
370 uint32_t used;
371 uint32_t high, low;
372
373 SHA1_VALIDATE_RET( ctx != NULL );
374 SHA1_VALIDATE_RET( (unsigned char *)output != NULL );
375
376 /*
377 * Add padding: 0x80 then 0x00 until 8 bytes remain for the length
378 */
379 used = ctx->total[0] & 0x3F;
380
381 ctx->buffer[used++] = 0x80;
382
383 if( used <= 56 )
384 {
385 /* Enough room for padding + length in current block */
386 memset( ctx->buffer + used, 0, 56 - used );
387 }
388 else
389 {
390 /* We'll need an extra block */
391 memset( ctx->buffer + used, 0, 64 - used );
392
393 if( ( ret = mbedtls_internal_sha1_process( ctx, ctx->buffer ) ) != 0 )
394 return( ret );
395
396 memset( ctx->buffer, 0, 56 );
397 }
398
399 /*
400 * Add message length
401 */
402 high = ( ctx->total[0] >> 29 )
403 | ( ctx->total[1] << 3 );
404 low = ( ctx->total[0] << 3 );
405
406 PUT_UINT32_BE( high, ctx->buffer, 56 );
407 PUT_UINT32_BE( low, ctx->buffer, 60 );
408
409 if( ( ret = mbedtls_internal_sha1_process( ctx, ctx->buffer ) ) != 0 )
410 return( ret );
411
412 /*
413 * Output final state
414 */
415 PUT_UINT32_BE( ctx->state[0], output, 0 );
416 PUT_UINT32_BE( ctx->state[1], output, 4 );
417 PUT_UINT32_BE( ctx->state[2], output, 8 );
418 PUT_UINT32_BE( ctx->state[3], output, 12 );
419 PUT_UINT32_BE( ctx->state[4], output, 16 );
420
421 return( 0 );
422}
423
424#if !defined(MBEDTLS_DEPRECATED_REMOVED)
425void mbedtls_sha1_finish( mbedtls_sha1_context *ctx,
426 unsigned char output[20] )
427{
428 mbedtls_sha1_finish_ret( ctx, output );
429}
430#endif
431
432#endif /* !MBEDTLS_SHA1_ALT */
433
434/*
435 * output = SHA-1( input buffer )
436 */
437int mbedtls_sha1_ret( const unsigned char *input,
438 size_t ilen,
439 unsigned char output[20] )
440{
441 int ret;
442 mbedtls_sha1_context ctx;
443
444 SHA1_VALIDATE_RET( ilen == 0 || input != NULL );
445 SHA1_VALIDATE_RET( (unsigned char *)output != NULL );
446
447 mbedtls_sha1_init( &ctx );
448
449 if( ( ret = mbedtls_sha1_starts_ret( &ctx ) ) != 0 )
450 goto exit;
451
452 if( ( ret = mbedtls_sha1_update_ret( &ctx, input, ilen ) ) != 0 )
453 goto exit;
454
455 if( ( ret = mbedtls_sha1_finish_ret( &ctx, output ) ) != 0 )
456 goto exit;
457
458exit:
459 mbedtls_sha1_free( &ctx );
460
461 return( ret );
462}
463
464#if !defined(MBEDTLS_DEPRECATED_REMOVED)
465void mbedtls_sha1( const unsigned char *input,
466 size_t ilen,
467 unsigned char output[20] )
468{
469 mbedtls_sha1_ret( input, ilen, output );
470}
471#endif
472
473#if defined(MBEDTLS_SELF_TEST)
474/*
475 * FIPS-180-1 test vectors
476 */
477static const unsigned char sha1_test_buf[3][57] =
478{
479 { "abc" },
480 { "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" },
481 { "" }
482};
483
484static const size_t sha1_test_buflen[3] =
485{
486 3, 56, 1000
487};
488
489static const unsigned char sha1_test_sum[3][20] =
490{
491 { 0xA9, 0x99, 0x3E, 0x36, 0x47, 0x06, 0x81, 0x6A, 0xBA, 0x3E,
492 0x25, 0x71, 0x78, 0x50, 0xC2, 0x6C, 0x9C, 0xD0, 0xD8, 0x9D },
493 { 0x84, 0x98, 0x3E, 0x44, 0x1C, 0x3B, 0xD2, 0x6E, 0xBA, 0xAE,
494 0x4A, 0xA1, 0xF9, 0x51, 0x29, 0xE5, 0xE5, 0x46, 0x70, 0xF1 },
495 { 0x34, 0xAA, 0x97, 0x3C, 0xD4, 0xC4, 0xDA, 0xA4, 0xF6, 0x1E,
496 0xEB, 0x2B, 0xDB, 0xAD, 0x27, 0x31, 0x65, 0x34, 0x01, 0x6F }
497};
498
499/*
500 * Checkup routine
501 */
502int mbedtls_sha1_self_test( int verbose )
503{
504 int i, j, buflen, ret = 0;
505 unsigned char buf[1024];
506 unsigned char sha1sum[20];
507 mbedtls_sha1_context ctx;
508
509 mbedtls_sha1_init( &ctx );
510
511 /*
512 * SHA-1
513 */
514 for( i = 0; i < 3; i++ )
515 {
516 if( verbose != 0 )
517 mbedtls_printf( " SHA-1 test #%d: ", i + 1 );
518
519 if( ( ret = mbedtls_sha1_starts_ret( &ctx ) ) != 0 )
520 goto fail;
521
522 if( i == 2 )
523 {
524 memset( buf, 'a', buflen = 1000 );
525
526 for( j = 0; j < 1000; j++ )
527 {
528 ret = mbedtls_sha1_update_ret( &ctx, buf, buflen );
529 if( ret != 0 )
530 goto fail;
531 }
532 }
533 else
534 {
535 ret = mbedtls_sha1_update_ret( &ctx, sha1_test_buf[i],
536 sha1_test_buflen[i] );
537 if( ret != 0 )
538 goto fail;
539 }
540
541 if( ( ret = mbedtls_sha1_finish_ret( &ctx, sha1sum ) ) != 0 )
542 goto fail;
543
544 if( memcmp( sha1sum, sha1_test_sum[i], 20 ) != 0 )
545 {
546 ret = 1;
547 goto fail;
548 }
549
550 if( verbose != 0 )
551 mbedtls_printf( "passed\n" );
552 }
553
554 if( verbose != 0 )
555 mbedtls_printf( "\n" );
556
557 goto exit;
558
559fail:
560 if( verbose != 0 )
561 mbedtls_printf( "failed\n" );
562
563exit:
564 mbedtls_sha1_free( &ctx );
565
566 return( ret );
567}
568
569#endif /* MBEDTLS_SELF_TEST */
570
571#endif /* MBEDTLS_SHA1_C */
Note: See TracBrowser for help on using the repository browser.