Modified mbed TLS headers for AES functionality only to reduce build size

Dependents:   BLE_Gateway_Linker_fix BLE_Gateway

Fork of mbedtls by sandbox

Committer:
electronichamsters
Date:
Mon Jul 10 04:00:25 2017 +0000
Revision:
5:f09f5ed830ca
Parent:
1:24750b9ad5ef
working gateway

Who changed what in which revision?

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