mbed client on ethernet with LWIP

Dependencies:   mbed Socket lwip-eth lwip-sys lwip

Fork of mbed-client-classic-example-lwip by sandbox

Committer:
mbedAustin
Date:
Thu Jun 09 17:08:36 2016 +0000
Revision:
11:cada08fc8a70
Commit for public Consumption

Who changed what in which revision?

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