Dependents:   blinky_max32630fthr

Committer:
switches
Date:
Fri Dec 16 16:27:57 2016 +0000
Revision:
3:1198227e6421
Parent:
0:5c4d7b2438d3
Changed ADC scale for MAX32625 platforms to 1.2V full scale to match MAX32630 platforms

Who changed what in which revision?

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