Includes library modifications to allow access to AIN_4 (AIN_0 / 5)

Committer:
bryantaylor
Date:
Tue Sep 20 21:26:12 2016 +0000
Revision:
0:eafc3fd41f75
hackathon

Who changed what in which revision?

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