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