This is a fork due to permission issues

Dependencies:   mbed Socket lwip-eth lwip-sys lwip

Fork of 6_songs-from-the-cloud by MakingMusicWorkshop

Committer:
timbeight
Date:
Thu May 19 16:02:10 2016 +0000
Revision:
1:0ddbe2d3319c
Parent:
0:f7c60d3e7b8a
This is my first commit while in the class.

Who changed what in which revision?

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