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