The WDCInterface is is a drop-in replacement for an EthernetInterface class that allows the user to connect to the Internet with a Wistron NeWeb Corporation (WNC) M14A2A Series data module using the standard network Socket API's. This interface class is used in the AT&T Cellular IoT Starter Kit which is sold by Avnet (http://cloudconnectkits.org/product/att-cellular-iot-starter-kit).

Dependencies:   WncControllerK64F

Dependents:   WNCProximityMqtt Pubnub_ATT_IoT_SK_WNC_sync BluemixDemo BluemixQS ... more

See the WNCInterface README in the Wiki tab for detailed information on this library.

Committer:
JMF
Date:
Fri Mar 24 22:26:23 2017 +0000
Revision:
29:b278b745fb4f
Parent:
12:0071cb144c7a
updated Class name of TCPSocketConnection to WncTCPSocketConnection;

Who changed what in which revision?

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