nkjnm

Dependencies:   MAX44000 nexpaq_mdk

Fork of LED_Demo by Maxim nexpaq

Committer:
nitsshukla
Date:
Fri Nov 04 12:06:04 2016 +0000
Revision:
7:3a65ef12ba31
Parent:
1:55a6170b404f
kghj;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
nexpaq 1:55a6170b404f 1 /*
nexpaq 1:55a6170b404f 2 * FIPS-180-1 compliant SHA-1 implementation
nexpaq 1:55a6170b404f 3 *
nexpaq 1:55a6170b404f 4 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
nexpaq 1:55a6170b404f 5 * SPDX-License-Identifier: Apache-2.0
nexpaq 1:55a6170b404f 6 *
nexpaq 1:55a6170b404f 7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
nexpaq 1:55a6170b404f 8 * not use this file except in compliance with the License.
nexpaq 1:55a6170b404f 9 * You may obtain a copy of the License at
nexpaq 1:55a6170b404f 10 *
nexpaq 1:55a6170b404f 11 * http://www.apache.org/licenses/LICENSE-2.0
nexpaq 1:55a6170b404f 12 *
nexpaq 1:55a6170b404f 13 * Unless required by applicable law or agreed to in writing, software
nexpaq 1:55a6170b404f 14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
nexpaq 1:55a6170b404f 15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
nexpaq 1:55a6170b404f 16 * See the License for the specific language governing permissions and
nexpaq 1:55a6170b404f 17 * limitations under the License.
nexpaq 1:55a6170b404f 18 *
nexpaq 1:55a6170b404f 19 * This file is part of mbed TLS (https://tls.mbed.org)
nexpaq 1:55a6170b404f 20 */
nexpaq 1:55a6170b404f 21 /*
nexpaq 1:55a6170b404f 22 * The SHA-1 standard was published by NIST in 1993.
nexpaq 1:55a6170b404f 23 *
nexpaq 1:55a6170b404f 24 * http://www.itl.nist.gov/fipspubs/fip180-1.htm
nexpaq 1:55a6170b404f 25 */
nexpaq 1:55a6170b404f 26
nexpaq 1:55a6170b404f 27 #if !defined(MBEDTLS_CONFIG_FILE)
nexpaq 1:55a6170b404f 28 #include "mbedtls/config.h"
nexpaq 1:55a6170b404f 29 #else
nexpaq 1:55a6170b404f 30 #include MBEDTLS_CONFIG_FILE
nexpaq 1:55a6170b404f 31 #endif
nexpaq 1:55a6170b404f 32
nexpaq 1:55a6170b404f 33 #if defined(MBEDTLS_SHA1_C)
nexpaq 1:55a6170b404f 34
nexpaq 1:55a6170b404f 35 #include "mbedtls/sha1.h"
nexpaq 1:55a6170b404f 36
nexpaq 1:55a6170b404f 37 #include <string.h>
nexpaq 1:55a6170b404f 38
nexpaq 1:55a6170b404f 39 #if defined(MBEDTLS_SELF_TEST)
nexpaq 1:55a6170b404f 40 #if defined(MBEDTLS_PLATFORM_C)
nexpaq 1:55a6170b404f 41 #include "mbedtls/platform.h"
nexpaq 1:55a6170b404f 42 #else
nexpaq 1:55a6170b404f 43 #include <stdio.h>
nexpaq 1:55a6170b404f 44 #define mbedtls_printf printf
nexpaq 1:55a6170b404f 45 #endif /* MBEDTLS_PLATFORM_C */
nexpaq 1:55a6170b404f 46 #endif /* MBEDTLS_SELF_TEST */
nexpaq 1:55a6170b404f 47
nexpaq 1:55a6170b404f 48 #if !defined(MBEDTLS_SHA1_ALT)
nexpaq 1:55a6170b404f 49
nexpaq 1:55a6170b404f 50 /* Implementation that should never be optimized out by the compiler */
nexpaq 1:55a6170b404f 51 static void mbedtls_zeroize( void *v, size_t n ) {
nexpaq 1:55a6170b404f 52 volatile unsigned char *p = (unsigned char*)v; while( n-- ) *p++ = 0;
nexpaq 1:55a6170b404f 53 }
nexpaq 1:55a6170b404f 54
nexpaq 1:55a6170b404f 55 /*
nexpaq 1:55a6170b404f 56 * 32-bit integer manipulation macros (big endian)
nexpaq 1:55a6170b404f 57 */
nexpaq 1:55a6170b404f 58 #ifndef GET_UINT32_BE
nexpaq 1:55a6170b404f 59 #define GET_UINT32_BE(n,b,i) \
nexpaq 1:55a6170b404f 60 { \
nexpaq 1:55a6170b404f 61 (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
nexpaq 1:55a6170b404f 62 | ( (uint32_t) (b)[(i) + 1] << 16 ) \
nexpaq 1:55a6170b404f 63 | ( (uint32_t) (b)[(i) + 2] << 8 ) \
nexpaq 1:55a6170b404f 64 | ( (uint32_t) (b)[(i) + 3] ); \
nexpaq 1:55a6170b404f 65 }
nexpaq 1:55a6170b404f 66 #endif
nexpaq 1:55a6170b404f 67
nexpaq 1:55a6170b404f 68 #ifndef PUT_UINT32_BE
nexpaq 1:55a6170b404f 69 #define PUT_UINT32_BE(n,b,i) \
nexpaq 1:55a6170b404f 70 { \
nexpaq 1:55a6170b404f 71 (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
nexpaq 1:55a6170b404f 72 (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
nexpaq 1:55a6170b404f 73 (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
nexpaq 1:55a6170b404f 74 (b)[(i) + 3] = (unsigned char) ( (n) ); \
nexpaq 1:55a6170b404f 75 }
nexpaq 1:55a6170b404f 76 #endif
nexpaq 1:55a6170b404f 77
nexpaq 1:55a6170b404f 78 void mbedtls_sha1_init( mbedtls_sha1_context *ctx )
nexpaq 1:55a6170b404f 79 {
nexpaq 1:55a6170b404f 80 memset( ctx, 0, sizeof( mbedtls_sha1_context ) );
nexpaq 1:55a6170b404f 81 }
nexpaq 1:55a6170b404f 82
nexpaq 1:55a6170b404f 83 void mbedtls_sha1_free( mbedtls_sha1_context *ctx )
nexpaq 1:55a6170b404f 84 {
nexpaq 1:55a6170b404f 85 if( ctx == NULL )
nexpaq 1:55a6170b404f 86 return;
nexpaq 1:55a6170b404f 87
nexpaq 1:55a6170b404f 88 mbedtls_zeroize( ctx, sizeof( mbedtls_sha1_context ) );
nexpaq 1:55a6170b404f 89 }
nexpaq 1:55a6170b404f 90
nexpaq 1:55a6170b404f 91 void mbedtls_sha1_clone( mbedtls_sha1_context *dst,
nexpaq 1:55a6170b404f 92 const mbedtls_sha1_context *src )
nexpaq 1:55a6170b404f 93 {
nexpaq 1:55a6170b404f 94 *dst = *src;
nexpaq 1:55a6170b404f 95 }
nexpaq 1:55a6170b404f 96
nexpaq 1:55a6170b404f 97 /*
nexpaq 1:55a6170b404f 98 * SHA-1 context setup
nexpaq 1:55a6170b404f 99 */
nexpaq 1:55a6170b404f 100 void mbedtls_sha1_starts( mbedtls_sha1_context *ctx )
nexpaq 1:55a6170b404f 101 {
nexpaq 1:55a6170b404f 102 ctx->total[0] = 0;
nexpaq 1:55a6170b404f 103 ctx->total[1] = 0;
nexpaq 1:55a6170b404f 104
nexpaq 1:55a6170b404f 105 ctx->state[0] = 0x67452301;
nexpaq 1:55a6170b404f 106 ctx->state[1] = 0xEFCDAB89;
nexpaq 1:55a6170b404f 107 ctx->state[2] = 0x98BADCFE;
nexpaq 1:55a6170b404f 108 ctx->state[3] = 0x10325476;
nexpaq 1:55a6170b404f 109 ctx->state[4] = 0xC3D2E1F0;
nexpaq 1:55a6170b404f 110 }
nexpaq 1:55a6170b404f 111
nexpaq 1:55a6170b404f 112 #if !defined(MBEDTLS_SHA1_PROCESS_ALT)
nexpaq 1:55a6170b404f 113 void mbedtls_sha1_process( mbedtls_sha1_context *ctx, const unsigned char data[64] )
nexpaq 1:55a6170b404f 114 {
nexpaq 1:55a6170b404f 115 uint32_t temp, W[16], A, B, C, D, E;
nexpaq 1:55a6170b404f 116
nexpaq 1:55a6170b404f 117 GET_UINT32_BE( W[ 0], data, 0 );
nexpaq 1:55a6170b404f 118 GET_UINT32_BE( W[ 1], data, 4 );
nexpaq 1:55a6170b404f 119 GET_UINT32_BE( W[ 2], data, 8 );
nexpaq 1:55a6170b404f 120 GET_UINT32_BE( W[ 3], data, 12 );
nexpaq 1:55a6170b404f 121 GET_UINT32_BE( W[ 4], data, 16 );
nexpaq 1:55a6170b404f 122 GET_UINT32_BE( W[ 5], data, 20 );
nexpaq 1:55a6170b404f 123 GET_UINT32_BE( W[ 6], data, 24 );
nexpaq 1:55a6170b404f 124 GET_UINT32_BE( W[ 7], data, 28 );
nexpaq 1:55a6170b404f 125 GET_UINT32_BE( W[ 8], data, 32 );
nexpaq 1:55a6170b404f 126 GET_UINT32_BE( W[ 9], data, 36 );
nexpaq 1:55a6170b404f 127 GET_UINT32_BE( W[10], data, 40 );
nexpaq 1:55a6170b404f 128 GET_UINT32_BE( W[11], data, 44 );
nexpaq 1:55a6170b404f 129 GET_UINT32_BE( W[12], data, 48 );
nexpaq 1:55a6170b404f 130 GET_UINT32_BE( W[13], data, 52 );
nexpaq 1:55a6170b404f 131 GET_UINT32_BE( W[14], data, 56 );
nexpaq 1:55a6170b404f 132 GET_UINT32_BE( W[15], data, 60 );
nexpaq 1:55a6170b404f 133
nexpaq 1:55a6170b404f 134 #define S(x,n) ((x << n) | ((x & 0xFFFFFFFF) >> (32 - n)))
nexpaq 1:55a6170b404f 135
nexpaq 1:55a6170b404f 136 #define R(t) \
nexpaq 1:55a6170b404f 137 ( \
nexpaq 1:55a6170b404f 138 temp = W[( t - 3 ) & 0x0F] ^ W[( t - 8 ) & 0x0F] ^ \
nexpaq 1:55a6170b404f 139 W[( t - 14 ) & 0x0F] ^ W[ t & 0x0F], \
nexpaq 1:55a6170b404f 140 ( W[t & 0x0F] = S(temp,1) ) \
nexpaq 1:55a6170b404f 141 )
nexpaq 1:55a6170b404f 142
nexpaq 1:55a6170b404f 143 #define P(a,b,c,d,e,x) \
nexpaq 1:55a6170b404f 144 { \
nexpaq 1:55a6170b404f 145 e += S(a,5) + F(b,c,d) + K + x; b = S(b,30); \
nexpaq 1:55a6170b404f 146 }
nexpaq 1:55a6170b404f 147
nexpaq 1:55a6170b404f 148 A = ctx->state[0];
nexpaq 1:55a6170b404f 149 B = ctx->state[1];
nexpaq 1:55a6170b404f 150 C = ctx->state[2];
nexpaq 1:55a6170b404f 151 D = ctx->state[3];
nexpaq 1:55a6170b404f 152 E = ctx->state[4];
nexpaq 1:55a6170b404f 153
nexpaq 1:55a6170b404f 154 #define F(x,y,z) (z ^ (x & (y ^ z)))
nexpaq 1:55a6170b404f 155 #define K 0x5A827999
nexpaq 1:55a6170b404f 156
nexpaq 1:55a6170b404f 157 P( A, B, C, D, E, W[0] );
nexpaq 1:55a6170b404f 158 P( E, A, B, C, D, W[1] );
nexpaq 1:55a6170b404f 159 P( D, E, A, B, C, W[2] );
nexpaq 1:55a6170b404f 160 P( C, D, E, A, B, W[3] );
nexpaq 1:55a6170b404f 161 P( B, C, D, E, A, W[4] );
nexpaq 1:55a6170b404f 162 P( A, B, C, D, E, W[5] );
nexpaq 1:55a6170b404f 163 P( E, A, B, C, D, W[6] );
nexpaq 1:55a6170b404f 164 P( D, E, A, B, C, W[7] );
nexpaq 1:55a6170b404f 165 P( C, D, E, A, B, W[8] );
nexpaq 1:55a6170b404f 166 P( B, C, D, E, A, W[9] );
nexpaq 1:55a6170b404f 167 P( A, B, C, D, E, W[10] );
nexpaq 1:55a6170b404f 168 P( E, A, B, C, D, W[11] );
nexpaq 1:55a6170b404f 169 P( D, E, A, B, C, W[12] );
nexpaq 1:55a6170b404f 170 P( C, D, E, A, B, W[13] );
nexpaq 1:55a6170b404f 171 P( B, C, D, E, A, W[14] );
nexpaq 1:55a6170b404f 172 P( A, B, C, D, E, W[15] );
nexpaq 1:55a6170b404f 173 P( E, A, B, C, D, R(16) );
nexpaq 1:55a6170b404f 174 P( D, E, A, B, C, R(17) );
nexpaq 1:55a6170b404f 175 P( C, D, E, A, B, R(18) );
nexpaq 1:55a6170b404f 176 P( B, C, D, E, A, R(19) );
nexpaq 1:55a6170b404f 177
nexpaq 1:55a6170b404f 178 #undef K
nexpaq 1:55a6170b404f 179 #undef F
nexpaq 1:55a6170b404f 180
nexpaq 1:55a6170b404f 181 #define F(x,y,z) (x ^ y ^ z)
nexpaq 1:55a6170b404f 182 #define K 0x6ED9EBA1
nexpaq 1:55a6170b404f 183
nexpaq 1:55a6170b404f 184 P( A, B, C, D, E, R(20) );
nexpaq 1:55a6170b404f 185 P( E, A, B, C, D, R(21) );
nexpaq 1:55a6170b404f 186 P( D, E, A, B, C, R(22) );
nexpaq 1:55a6170b404f 187 P( C, D, E, A, B, R(23) );
nexpaq 1:55a6170b404f 188 P( B, C, D, E, A, R(24) );
nexpaq 1:55a6170b404f 189 P( A, B, C, D, E, R(25) );
nexpaq 1:55a6170b404f 190 P( E, A, B, C, D, R(26) );
nexpaq 1:55a6170b404f 191 P( D, E, A, B, C, R(27) );
nexpaq 1:55a6170b404f 192 P( C, D, E, A, B, R(28) );
nexpaq 1:55a6170b404f 193 P( B, C, D, E, A, R(29) );
nexpaq 1:55a6170b404f 194 P( A, B, C, D, E, R(30) );
nexpaq 1:55a6170b404f 195 P( E, A, B, C, D, R(31) );
nexpaq 1:55a6170b404f 196 P( D, E, A, B, C, R(32) );
nexpaq 1:55a6170b404f 197 P( C, D, E, A, B, R(33) );
nexpaq 1:55a6170b404f 198 P( B, C, D, E, A, R(34) );
nexpaq 1:55a6170b404f 199 P( A, B, C, D, E, R(35) );
nexpaq 1:55a6170b404f 200 P( E, A, B, C, D, R(36) );
nexpaq 1:55a6170b404f 201 P( D, E, A, B, C, R(37) );
nexpaq 1:55a6170b404f 202 P( C, D, E, A, B, R(38) );
nexpaq 1:55a6170b404f 203 P( B, C, D, E, A, R(39) );
nexpaq 1:55a6170b404f 204
nexpaq 1:55a6170b404f 205 #undef K
nexpaq 1:55a6170b404f 206 #undef F
nexpaq 1:55a6170b404f 207
nexpaq 1:55a6170b404f 208 #define F(x,y,z) ((x & y) | (z & (x | y)))
nexpaq 1:55a6170b404f 209 #define K 0x8F1BBCDC
nexpaq 1:55a6170b404f 210
nexpaq 1:55a6170b404f 211 P( A, B, C, D, E, R(40) );
nexpaq 1:55a6170b404f 212 P( E, A, B, C, D, R(41) );
nexpaq 1:55a6170b404f 213 P( D, E, A, B, C, R(42) );
nexpaq 1:55a6170b404f 214 P( C, D, E, A, B, R(43) );
nexpaq 1:55a6170b404f 215 P( B, C, D, E, A, R(44) );
nexpaq 1:55a6170b404f 216 P( A, B, C, D, E, R(45) );
nexpaq 1:55a6170b404f 217 P( E, A, B, C, D, R(46) );
nexpaq 1:55a6170b404f 218 P( D, E, A, B, C, R(47) );
nexpaq 1:55a6170b404f 219 P( C, D, E, A, B, R(48) );
nexpaq 1:55a6170b404f 220 P( B, C, D, E, A, R(49) );
nexpaq 1:55a6170b404f 221 P( A, B, C, D, E, R(50) );
nexpaq 1:55a6170b404f 222 P( E, A, B, C, D, R(51) );
nexpaq 1:55a6170b404f 223 P( D, E, A, B, C, R(52) );
nexpaq 1:55a6170b404f 224 P( C, D, E, A, B, R(53) );
nexpaq 1:55a6170b404f 225 P( B, C, D, E, A, R(54) );
nexpaq 1:55a6170b404f 226 P( A, B, C, D, E, R(55) );
nexpaq 1:55a6170b404f 227 P( E, A, B, C, D, R(56) );
nexpaq 1:55a6170b404f 228 P( D, E, A, B, C, R(57) );
nexpaq 1:55a6170b404f 229 P( C, D, E, A, B, R(58) );
nexpaq 1:55a6170b404f 230 P( B, C, D, E, A, R(59) );
nexpaq 1:55a6170b404f 231
nexpaq 1:55a6170b404f 232 #undef K
nexpaq 1:55a6170b404f 233 #undef F
nexpaq 1:55a6170b404f 234
nexpaq 1:55a6170b404f 235 #define F(x,y,z) (x ^ y ^ z)
nexpaq 1:55a6170b404f 236 #define K 0xCA62C1D6
nexpaq 1:55a6170b404f 237
nexpaq 1:55a6170b404f 238 P( A, B, C, D, E, R(60) );
nexpaq 1:55a6170b404f 239 P( E, A, B, C, D, R(61) );
nexpaq 1:55a6170b404f 240 P( D, E, A, B, C, R(62) );
nexpaq 1:55a6170b404f 241 P( C, D, E, A, B, R(63) );
nexpaq 1:55a6170b404f 242 P( B, C, D, E, A, R(64) );
nexpaq 1:55a6170b404f 243 P( A, B, C, D, E, R(65) );
nexpaq 1:55a6170b404f 244 P( E, A, B, C, D, R(66) );
nexpaq 1:55a6170b404f 245 P( D, E, A, B, C, R(67) );
nexpaq 1:55a6170b404f 246 P( C, D, E, A, B, R(68) );
nexpaq 1:55a6170b404f 247 P( B, C, D, E, A, R(69) );
nexpaq 1:55a6170b404f 248 P( A, B, C, D, E, R(70) );
nexpaq 1:55a6170b404f 249 P( E, A, B, C, D, R(71) );
nexpaq 1:55a6170b404f 250 P( D, E, A, B, C, R(72) );
nexpaq 1:55a6170b404f 251 P( C, D, E, A, B, R(73) );
nexpaq 1:55a6170b404f 252 P( B, C, D, E, A, R(74) );
nexpaq 1:55a6170b404f 253 P( A, B, C, D, E, R(75) );
nexpaq 1:55a6170b404f 254 P( E, A, B, C, D, R(76) );
nexpaq 1:55a6170b404f 255 P( D, E, A, B, C, R(77) );
nexpaq 1:55a6170b404f 256 P( C, D, E, A, B, R(78) );
nexpaq 1:55a6170b404f 257 P( B, C, D, E, A, R(79) );
nexpaq 1:55a6170b404f 258
nexpaq 1:55a6170b404f 259 #undef K
nexpaq 1:55a6170b404f 260 #undef F
nexpaq 1:55a6170b404f 261
nexpaq 1:55a6170b404f 262 ctx->state[0] += A;
nexpaq 1:55a6170b404f 263 ctx->state[1] += B;
nexpaq 1:55a6170b404f 264 ctx->state[2] += C;
nexpaq 1:55a6170b404f 265 ctx->state[3] += D;
nexpaq 1:55a6170b404f 266 ctx->state[4] += E;
nexpaq 1:55a6170b404f 267 }
nexpaq 1:55a6170b404f 268 #endif /* !MBEDTLS_SHA1_PROCESS_ALT */
nexpaq 1:55a6170b404f 269
nexpaq 1:55a6170b404f 270 /*
nexpaq 1:55a6170b404f 271 * SHA-1 process buffer
nexpaq 1:55a6170b404f 272 */
nexpaq 1:55a6170b404f 273 void mbedtls_sha1_update( mbedtls_sha1_context *ctx, const unsigned char *input, size_t ilen )
nexpaq 1:55a6170b404f 274 {
nexpaq 1:55a6170b404f 275 size_t fill;
nexpaq 1:55a6170b404f 276 uint32_t left;
nexpaq 1:55a6170b404f 277
nexpaq 1:55a6170b404f 278 if( ilen == 0 )
nexpaq 1:55a6170b404f 279 return;
nexpaq 1:55a6170b404f 280
nexpaq 1:55a6170b404f 281 left = ctx->total[0] & 0x3F;
nexpaq 1:55a6170b404f 282 fill = 64 - left;
nexpaq 1:55a6170b404f 283
nexpaq 1:55a6170b404f 284 ctx->total[0] += (uint32_t) ilen;
nexpaq 1:55a6170b404f 285 ctx->total[0] &= 0xFFFFFFFF;
nexpaq 1:55a6170b404f 286
nexpaq 1:55a6170b404f 287 if( ctx->total[0] < (uint32_t) ilen )
nexpaq 1:55a6170b404f 288 ctx->total[1]++;
nexpaq 1:55a6170b404f 289
nexpaq 1:55a6170b404f 290 if( left && ilen >= fill )
nexpaq 1:55a6170b404f 291 {
nexpaq 1:55a6170b404f 292 memcpy( (void *) (ctx->buffer + left), input, fill );
nexpaq 1:55a6170b404f 293 mbedtls_sha1_process( ctx, ctx->buffer );
nexpaq 1:55a6170b404f 294 input += fill;
nexpaq 1:55a6170b404f 295 ilen -= fill;
nexpaq 1:55a6170b404f 296 left = 0;
nexpaq 1:55a6170b404f 297 }
nexpaq 1:55a6170b404f 298
nexpaq 1:55a6170b404f 299 while( ilen >= 64 )
nexpaq 1:55a6170b404f 300 {
nexpaq 1:55a6170b404f 301 mbedtls_sha1_process( ctx, input );
nexpaq 1:55a6170b404f 302 input += 64;
nexpaq 1:55a6170b404f 303 ilen -= 64;
nexpaq 1:55a6170b404f 304 }
nexpaq 1:55a6170b404f 305
nexpaq 1:55a6170b404f 306 if( ilen > 0 )
nexpaq 1:55a6170b404f 307 memcpy( (void *) (ctx->buffer + left), input, ilen );
nexpaq 1:55a6170b404f 308 }
nexpaq 1:55a6170b404f 309
nexpaq 1:55a6170b404f 310 static const unsigned char sha1_padding[64] =
nexpaq 1:55a6170b404f 311 {
nexpaq 1:55a6170b404f 312 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
nexpaq 1:55a6170b404f 313 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
nexpaq 1:55a6170b404f 314 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
nexpaq 1:55a6170b404f 315 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
nexpaq 1:55a6170b404f 316 };
nexpaq 1:55a6170b404f 317
nexpaq 1:55a6170b404f 318 /*
nexpaq 1:55a6170b404f 319 * SHA-1 final digest
nexpaq 1:55a6170b404f 320 */
nexpaq 1:55a6170b404f 321 void mbedtls_sha1_finish( mbedtls_sha1_context *ctx, unsigned char output[20] )
nexpaq 1:55a6170b404f 322 {
nexpaq 1:55a6170b404f 323 uint32_t last, padn;
nexpaq 1:55a6170b404f 324 uint32_t high, low;
nexpaq 1:55a6170b404f 325 unsigned char msglen[8];
nexpaq 1:55a6170b404f 326
nexpaq 1:55a6170b404f 327 high = ( ctx->total[0] >> 29 )
nexpaq 1:55a6170b404f 328 | ( ctx->total[1] << 3 );
nexpaq 1:55a6170b404f 329 low = ( ctx->total[0] << 3 );
nexpaq 1:55a6170b404f 330
nexpaq 1:55a6170b404f 331 PUT_UINT32_BE( high, msglen, 0 );
nexpaq 1:55a6170b404f 332 PUT_UINT32_BE( low, msglen, 4 );
nexpaq 1:55a6170b404f 333
nexpaq 1:55a6170b404f 334 last = ctx->total[0] & 0x3F;
nexpaq 1:55a6170b404f 335 padn = ( last < 56 ) ? ( 56 - last ) : ( 120 - last );
nexpaq 1:55a6170b404f 336
nexpaq 1:55a6170b404f 337 mbedtls_sha1_update( ctx, sha1_padding, padn );
nexpaq 1:55a6170b404f 338 mbedtls_sha1_update( ctx, msglen, 8 );
nexpaq 1:55a6170b404f 339
nexpaq 1:55a6170b404f 340 PUT_UINT32_BE( ctx->state[0], output, 0 );
nexpaq 1:55a6170b404f 341 PUT_UINT32_BE( ctx->state[1], output, 4 );
nexpaq 1:55a6170b404f 342 PUT_UINT32_BE( ctx->state[2], output, 8 );
nexpaq 1:55a6170b404f 343 PUT_UINT32_BE( ctx->state[3], output, 12 );
nexpaq 1:55a6170b404f 344 PUT_UINT32_BE( ctx->state[4], output, 16 );
nexpaq 1:55a6170b404f 345 }
nexpaq 1:55a6170b404f 346
nexpaq 1:55a6170b404f 347 #endif /* !MBEDTLS_SHA1_ALT */
nexpaq 1:55a6170b404f 348
nexpaq 1:55a6170b404f 349 /*
nexpaq 1:55a6170b404f 350 * output = SHA-1( input buffer )
nexpaq 1:55a6170b404f 351 */
nexpaq 1:55a6170b404f 352 void mbedtls_sha1( const unsigned char *input, size_t ilen, unsigned char output[20] )
nexpaq 1:55a6170b404f 353 {
nexpaq 1:55a6170b404f 354 mbedtls_sha1_context ctx;
nexpaq 1:55a6170b404f 355
nexpaq 1:55a6170b404f 356 mbedtls_sha1_init( &ctx );
nexpaq 1:55a6170b404f 357 mbedtls_sha1_starts( &ctx );
nexpaq 1:55a6170b404f 358 mbedtls_sha1_update( &ctx, input, ilen );
nexpaq 1:55a6170b404f 359 mbedtls_sha1_finish( &ctx, output );
nexpaq 1:55a6170b404f 360 mbedtls_sha1_free( &ctx );
nexpaq 1:55a6170b404f 361 }
nexpaq 1:55a6170b404f 362
nexpaq 1:55a6170b404f 363 #if defined(MBEDTLS_SELF_TEST)
nexpaq 1:55a6170b404f 364 /*
nexpaq 1:55a6170b404f 365 * FIPS-180-1 test vectors
nexpaq 1:55a6170b404f 366 */
nexpaq 1:55a6170b404f 367 static const unsigned char sha1_test_buf[3][57] =
nexpaq 1:55a6170b404f 368 {
nexpaq 1:55a6170b404f 369 { "abc" },
nexpaq 1:55a6170b404f 370 { "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" },
nexpaq 1:55a6170b404f 371 { "" }
nexpaq 1:55a6170b404f 372 };
nexpaq 1:55a6170b404f 373
nexpaq 1:55a6170b404f 374 static const int sha1_test_buflen[3] =
nexpaq 1:55a6170b404f 375 {
nexpaq 1:55a6170b404f 376 3, 56, 1000
nexpaq 1:55a6170b404f 377 };
nexpaq 1:55a6170b404f 378
nexpaq 1:55a6170b404f 379 static const unsigned char sha1_test_sum[3][20] =
nexpaq 1:55a6170b404f 380 {
nexpaq 1:55a6170b404f 381 { 0xA9, 0x99, 0x3E, 0x36, 0x47, 0x06, 0x81, 0x6A, 0xBA, 0x3E,
nexpaq 1:55a6170b404f 382 0x25, 0x71, 0x78, 0x50, 0xC2, 0x6C, 0x9C, 0xD0, 0xD8, 0x9D },
nexpaq 1:55a6170b404f 383 { 0x84, 0x98, 0x3E, 0x44, 0x1C, 0x3B, 0xD2, 0x6E, 0xBA, 0xAE,
nexpaq 1:55a6170b404f 384 0x4A, 0xA1, 0xF9, 0x51, 0x29, 0xE5, 0xE5, 0x46, 0x70, 0xF1 },
nexpaq 1:55a6170b404f 385 { 0x34, 0xAA, 0x97, 0x3C, 0xD4, 0xC4, 0xDA, 0xA4, 0xF6, 0x1E,
nexpaq 1:55a6170b404f 386 0xEB, 0x2B, 0xDB, 0xAD, 0x27, 0x31, 0x65, 0x34, 0x01, 0x6F }
nexpaq 1:55a6170b404f 387 };
nexpaq 1:55a6170b404f 388
nexpaq 1:55a6170b404f 389 /*
nexpaq 1:55a6170b404f 390 * Checkup routine
nexpaq 1:55a6170b404f 391 */
nexpaq 1:55a6170b404f 392 int mbedtls_sha1_self_test( int verbose )
nexpaq 1:55a6170b404f 393 {
nexpaq 1:55a6170b404f 394 int i, j, buflen, ret = 0;
nexpaq 1:55a6170b404f 395 unsigned char buf[1024];
nexpaq 1:55a6170b404f 396 unsigned char sha1sum[20];
nexpaq 1:55a6170b404f 397 mbedtls_sha1_context ctx;
nexpaq 1:55a6170b404f 398
nexpaq 1:55a6170b404f 399 mbedtls_sha1_init( &ctx );
nexpaq 1:55a6170b404f 400
nexpaq 1:55a6170b404f 401 /*
nexpaq 1:55a6170b404f 402 * SHA-1
nexpaq 1:55a6170b404f 403 */
nexpaq 1:55a6170b404f 404 for( i = 0; i < 3; i++ )
nexpaq 1:55a6170b404f 405 {
nexpaq 1:55a6170b404f 406 if( verbose != 0 )
nexpaq 1:55a6170b404f 407 mbedtls_printf( " SHA-1 test #%d: ", i + 1 );
nexpaq 1:55a6170b404f 408
nexpaq 1:55a6170b404f 409 mbedtls_sha1_starts( &ctx );
nexpaq 1:55a6170b404f 410
nexpaq 1:55a6170b404f 411 if( i == 2 )
nexpaq 1:55a6170b404f 412 {
nexpaq 1:55a6170b404f 413 memset( buf, 'a', buflen = 1000 );
nexpaq 1:55a6170b404f 414
nexpaq 1:55a6170b404f 415 for( j = 0; j < 1000; j++ )
nexpaq 1:55a6170b404f 416 mbedtls_sha1_update( &ctx, buf, buflen );
nexpaq 1:55a6170b404f 417 }
nexpaq 1:55a6170b404f 418 else
nexpaq 1:55a6170b404f 419 mbedtls_sha1_update( &ctx, sha1_test_buf[i],
nexpaq 1:55a6170b404f 420 sha1_test_buflen[i] );
nexpaq 1:55a6170b404f 421
nexpaq 1:55a6170b404f 422 mbedtls_sha1_finish( &ctx, sha1sum );
nexpaq 1:55a6170b404f 423
nexpaq 1:55a6170b404f 424 if( memcmp( sha1sum, sha1_test_sum[i], 20 ) != 0 )
nexpaq 1:55a6170b404f 425 {
nexpaq 1:55a6170b404f 426 if( verbose != 0 )
nexpaq 1:55a6170b404f 427 mbedtls_printf( "failed\n" );
nexpaq 1:55a6170b404f 428
nexpaq 1:55a6170b404f 429 ret = 1;
nexpaq 1:55a6170b404f 430 goto exit;
nexpaq 1:55a6170b404f 431 }
nexpaq 1:55a6170b404f 432
nexpaq 1:55a6170b404f 433 if( verbose != 0 )
nexpaq 1:55a6170b404f 434 mbedtls_printf( "passed\n" );
nexpaq 1:55a6170b404f 435 }
nexpaq 1:55a6170b404f 436
nexpaq 1:55a6170b404f 437 if( verbose != 0 )
nexpaq 1:55a6170b404f 438 mbedtls_printf( "\n" );
nexpaq 1:55a6170b404f 439
nexpaq 1:55a6170b404f 440 exit:
nexpaq 1:55a6170b404f 441 mbedtls_sha1_free( &ctx );
nexpaq 1:55a6170b404f 442
nexpaq 1:55a6170b404f 443 return( ret );
nexpaq 1:55a6170b404f 444 }
nexpaq 1:55a6170b404f 445
nexpaq 1:55a6170b404f 446 #endif /* MBEDTLS_SELF_TEST */
nexpaq 1:55a6170b404f 447
nexpaq 1:55a6170b404f 448 #endif /* MBEDTLS_SHA1_C */