Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1 | /* |
| 2 | * FIPS-180-1 compliant SHA-1 implementation |
| 3 | * |
Manuel Pégourié-Gonnard | 6fb8187 | 2015-07-27 11:11:48 +0200 | [diff] [blame] | 4 | * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved |
Manuel Pégourié-Gonnard | 37ff140 | 2015-09-04 14:21:07 +0200 | [diff] [blame] | 5 | * SPDX-License-Identifier: Apache-2.0 |
| 6 | * |
| 7 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 8 | * not use this file except in compliance with the License. |
| 9 | * You may obtain a copy of the License at |
| 10 | * |
| 11 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | * |
| 13 | * Unless required by applicable law or agreed to in writing, software |
| 14 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 15 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | * See the License for the specific language governing permissions and |
| 17 | * limitations under the License. |
Paul Bakker | b96f154 | 2010-07-18 20:36:00 +0000 | [diff] [blame] | 18 | * |
Manuel Pégourié-Gonnard | fe44643 | 2015-03-06 13:17:10 +0000 | [diff] [blame] | 19 | * This file is part of mbed TLS (https://tls.mbed.org) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 20 | */ |
| 21 | /* |
| 22 | * The SHA-1 standard was published by NIST in 1993. |
| 23 | * |
| 24 | * http://www.itl.nist.gov/fipspubs/fip180-1.htm |
| 25 | */ |
| 26 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 27 | #if !defined(MBEDTLS_CONFIG_FILE) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 28 | #include "mbedtls/config.h" |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 29 | #else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 30 | #include MBEDTLS_CONFIG_FILE |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 31 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 32 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 33 | #if defined(MBEDTLS_SHA1_C) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 34 | |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 35 | #include "mbedtls/sha1.h" |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 36 | #include "mbedtls/platform_util.h" |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 37 | |
Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 38 | #include <string.h> |
| 39 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 40 | #if defined(MBEDTLS_SELF_TEST) |
| 41 | #if defined(MBEDTLS_PLATFORM_C) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 42 | #include "mbedtls/platform.h" |
Paul Bakker | 7dc4c44 | 2014-02-01 22:50:26 +0100 | [diff] [blame] | 43 | #else |
Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 44 | #include <stdio.h> |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 45 | #define mbedtls_printf printf |
| 46 | #endif /* MBEDTLS_PLATFORM_C */ |
| 47 | #endif /* MBEDTLS_SELF_TEST */ |
Paul Bakker | 7dc4c44 | 2014-02-01 22:50:26 +0100 | [diff] [blame] | 48 | |
Hanno Becker | b3c7023 | 2018-12-20 10:18:05 +0000 | [diff] [blame^] | 49 | #define SHA1_VALIDATE_RET(cond) \ |
| 50 | MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_SHA1_BAD_INPUT_DATA ) |
| 51 | |
| 52 | #define SHA1_VALIDATE(cond) MBEDTLS_INTERNAL_VALIDATE( cond ) |
| 53 | |
Manuel Pégourié-Gonnard | 8b2641d | 2015-08-27 20:03:46 +0200 | [diff] [blame] | 54 | #if !defined(MBEDTLS_SHA1_ALT) |
| 55 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 56 | /* |
| 57 | * 32-bit integer manipulation macros (big endian) |
| 58 | */ |
Paul Bakker | 5c2364c | 2012-10-01 14:41:15 +0000 | [diff] [blame] | 59 | #ifndef GET_UINT32_BE |
| 60 | #define GET_UINT32_BE(n,b,i) \ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 61 | { \ |
Paul Bakker | 5c2364c | 2012-10-01 14:41:15 +0000 | [diff] [blame] | 62 | (n) = ( (uint32_t) (b)[(i) ] << 24 ) \ |
| 63 | | ( (uint32_t) (b)[(i) + 1] << 16 ) \ |
| 64 | | ( (uint32_t) (b)[(i) + 2] << 8 ) \ |
| 65 | | ( (uint32_t) (b)[(i) + 3] ); \ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 66 | } |
| 67 | #endif |
| 68 | |
Paul Bakker | 5c2364c | 2012-10-01 14:41:15 +0000 | [diff] [blame] | 69 | #ifndef PUT_UINT32_BE |
| 70 | #define PUT_UINT32_BE(n,b,i) \ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 71 | { \ |
| 72 | (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \ |
| 73 | (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \ |
| 74 | (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \ |
| 75 | (b)[(i) + 3] = (unsigned char) ( (n) ); \ |
| 76 | } |
| 77 | #endif |
| 78 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 79 | void mbedtls_sha1_init( mbedtls_sha1_context *ctx ) |
Paul Bakker | 5b4af39 | 2014-06-26 12:09:34 +0200 | [diff] [blame] | 80 | { |
Hanno Becker | 039ccab | 2018-12-18 17:52:14 +0000 | [diff] [blame] | 81 | SHA1_VALIDATE( ctx != NULL ); |
Andres Amaya Garcia | f7c43b3 | 2018-12-09 19:12:19 +0000 | [diff] [blame] | 82 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 83 | memset( ctx, 0, sizeof( mbedtls_sha1_context ) ); |
Paul Bakker | 5b4af39 | 2014-06-26 12:09:34 +0200 | [diff] [blame] | 84 | } |
| 85 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 86 | void mbedtls_sha1_free( mbedtls_sha1_context *ctx ) |
Paul Bakker | 5b4af39 | 2014-06-26 12:09:34 +0200 | [diff] [blame] | 87 | { |
| 88 | if( ctx == NULL ) |
| 89 | return; |
| 90 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 91 | mbedtls_platform_zeroize( ctx, sizeof( mbedtls_sha1_context ) ); |
Paul Bakker | 5b4af39 | 2014-06-26 12:09:34 +0200 | [diff] [blame] | 92 | } |
| 93 | |
Manuel Pégourié-Gonnard | 16d412f | 2015-07-06 15:26:26 +0200 | [diff] [blame] | 94 | void mbedtls_sha1_clone( mbedtls_sha1_context *dst, |
| 95 | const mbedtls_sha1_context *src ) |
| 96 | { |
Hanno Becker | 039ccab | 2018-12-18 17:52:14 +0000 | [diff] [blame] | 97 | SHA1_VALIDATE( dst != NULL ); |
| 98 | SHA1_VALIDATE( src != NULL ); |
Andres Amaya Garcia | f7c43b3 | 2018-12-09 19:12:19 +0000 | [diff] [blame] | 99 | |
Manuel Pégourié-Gonnard | 16d412f | 2015-07-06 15:26:26 +0200 | [diff] [blame] | 100 | *dst = *src; |
| 101 | } |
| 102 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 103 | /* |
| 104 | * SHA-1 context setup |
| 105 | */ |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 106 | int mbedtls_sha1_starts_ret( mbedtls_sha1_context *ctx ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 107 | { |
Hanno Becker | 039ccab | 2018-12-18 17:52:14 +0000 | [diff] [blame] | 108 | SHA1_VALIDATE_RET( ctx != NULL ); |
Andres Amaya Garcia | f7c43b3 | 2018-12-09 19:12:19 +0000 | [diff] [blame] | 109 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 110 | ctx->total[0] = 0; |
| 111 | ctx->total[1] = 0; |
| 112 | |
| 113 | ctx->state[0] = 0x67452301; |
| 114 | ctx->state[1] = 0xEFCDAB89; |
| 115 | ctx->state[2] = 0x98BADCFE; |
| 116 | ctx->state[3] = 0x10325476; |
| 117 | ctx->state[4] = 0xC3D2E1F0; |
Andres Amaya Garcia | 034ea7e | 2017-04-28 15:14:50 +0100 | [diff] [blame] | 118 | |
| 119 | return( 0 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 120 | } |
| 121 | |
Jaeden Amero | 041039f | 2018-02-19 15:28:08 +0000 | [diff] [blame] | 122 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
| 123 | void mbedtls_sha1_starts( mbedtls_sha1_context *ctx ) |
| 124 | { |
| 125 | mbedtls_sha1_starts_ret( ctx ); |
| 126 | } |
| 127 | #endif |
| 128 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 129 | #if !defined(MBEDTLS_SHA1_PROCESS_ALT) |
Andres Amaya Garcia | cccfe08 | 2017-06-28 10:36:39 +0100 | [diff] [blame] | 130 | int mbedtls_internal_sha1_process( mbedtls_sha1_context *ctx, |
| 131 | const unsigned char data[64] ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 132 | { |
Paul Bakker | 5c2364c | 2012-10-01 14:41:15 +0000 | [diff] [blame] | 133 | uint32_t temp, W[16], A, B, C, D, E; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 134 | |
Hanno Becker | 039ccab | 2018-12-18 17:52:14 +0000 | [diff] [blame] | 135 | SHA1_VALIDATE_RET( ctx != NULL ); |
| 136 | SHA1_VALIDATE_RET( (const unsigned char *)data != NULL ); |
Andres Amaya Garcia | f7c43b3 | 2018-12-09 19:12:19 +0000 | [diff] [blame] | 137 | |
Paul Bakker | 5c2364c | 2012-10-01 14:41:15 +0000 | [diff] [blame] | 138 | GET_UINT32_BE( W[ 0], data, 0 ); |
| 139 | GET_UINT32_BE( W[ 1], data, 4 ); |
| 140 | GET_UINT32_BE( W[ 2], data, 8 ); |
| 141 | GET_UINT32_BE( W[ 3], data, 12 ); |
| 142 | GET_UINT32_BE( W[ 4], data, 16 ); |
| 143 | GET_UINT32_BE( W[ 5], data, 20 ); |
| 144 | GET_UINT32_BE( W[ 6], data, 24 ); |
| 145 | GET_UINT32_BE( W[ 7], data, 28 ); |
| 146 | GET_UINT32_BE( W[ 8], data, 32 ); |
| 147 | GET_UINT32_BE( W[ 9], data, 36 ); |
| 148 | GET_UINT32_BE( W[10], data, 40 ); |
| 149 | GET_UINT32_BE( W[11], data, 44 ); |
| 150 | GET_UINT32_BE( W[12], data, 48 ); |
| 151 | GET_UINT32_BE( W[13], data, 52 ); |
| 152 | GET_UINT32_BE( W[14], data, 56 ); |
| 153 | GET_UINT32_BE( W[15], data, 60 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 154 | |
| 155 | #define S(x,n) ((x << n) | ((x & 0xFFFFFFFF) >> (32 - n))) |
| 156 | |
| 157 | #define R(t) \ |
| 158 | ( \ |
Paul Bakker | 66d5d07 | 2014-06-17 16:39:18 +0200 | [diff] [blame] | 159 | temp = W[( t - 3 ) & 0x0F] ^ W[( t - 8 ) & 0x0F] ^ \ |
| 160 | W[( t - 14 ) & 0x0F] ^ W[ t & 0x0F], \ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 161 | ( W[t & 0x0F] = S(temp,1) ) \ |
| 162 | ) |
| 163 | |
| 164 | #define P(a,b,c,d,e,x) \ |
| 165 | { \ |
| 166 | e += S(a,5) + F(b,c,d) + K + x; b = S(b,30); \ |
| 167 | } |
| 168 | |
| 169 | A = ctx->state[0]; |
| 170 | B = ctx->state[1]; |
| 171 | C = ctx->state[2]; |
| 172 | D = ctx->state[3]; |
| 173 | E = ctx->state[4]; |
| 174 | |
| 175 | #define F(x,y,z) (z ^ (x & (y ^ z))) |
| 176 | #define K 0x5A827999 |
| 177 | |
| 178 | P( A, B, C, D, E, W[0] ); |
| 179 | P( E, A, B, C, D, W[1] ); |
| 180 | P( D, E, A, B, C, W[2] ); |
| 181 | P( C, D, E, A, B, W[3] ); |
| 182 | P( B, C, D, E, A, W[4] ); |
| 183 | P( A, B, C, D, E, W[5] ); |
| 184 | P( E, A, B, C, D, W[6] ); |
| 185 | P( D, E, A, B, C, W[7] ); |
| 186 | P( C, D, E, A, B, W[8] ); |
| 187 | P( B, C, D, E, A, W[9] ); |
| 188 | P( A, B, C, D, E, W[10] ); |
| 189 | P( E, A, B, C, D, W[11] ); |
| 190 | P( D, E, A, B, C, W[12] ); |
| 191 | P( C, D, E, A, B, W[13] ); |
| 192 | P( B, C, D, E, A, W[14] ); |
| 193 | P( A, B, C, D, E, W[15] ); |
| 194 | P( E, A, B, C, D, R(16) ); |
| 195 | P( D, E, A, B, C, R(17) ); |
| 196 | P( C, D, E, A, B, R(18) ); |
| 197 | P( B, C, D, E, A, R(19) ); |
| 198 | |
| 199 | #undef K |
| 200 | #undef F |
| 201 | |
| 202 | #define F(x,y,z) (x ^ y ^ z) |
| 203 | #define K 0x6ED9EBA1 |
| 204 | |
| 205 | P( A, B, C, D, E, R(20) ); |
| 206 | P( E, A, B, C, D, R(21) ); |
| 207 | P( D, E, A, B, C, R(22) ); |
| 208 | P( C, D, E, A, B, R(23) ); |
| 209 | P( B, C, D, E, A, R(24) ); |
| 210 | P( A, B, C, D, E, R(25) ); |
| 211 | P( E, A, B, C, D, R(26) ); |
| 212 | P( D, E, A, B, C, R(27) ); |
| 213 | P( C, D, E, A, B, R(28) ); |
| 214 | P( B, C, D, E, A, R(29) ); |
| 215 | P( A, B, C, D, E, R(30) ); |
| 216 | P( E, A, B, C, D, R(31) ); |
| 217 | P( D, E, A, B, C, R(32) ); |
| 218 | P( C, D, E, A, B, R(33) ); |
| 219 | P( B, C, D, E, A, R(34) ); |
| 220 | P( A, B, C, D, E, R(35) ); |
| 221 | P( E, A, B, C, D, R(36) ); |
| 222 | P( D, E, A, B, C, R(37) ); |
| 223 | P( C, D, E, A, B, R(38) ); |
| 224 | P( B, C, D, E, A, R(39) ); |
| 225 | |
| 226 | #undef K |
| 227 | #undef F |
| 228 | |
| 229 | #define F(x,y,z) ((x & y) | (z & (x | y))) |
| 230 | #define K 0x8F1BBCDC |
| 231 | |
| 232 | P( A, B, C, D, E, R(40) ); |
| 233 | P( E, A, B, C, D, R(41) ); |
| 234 | P( D, E, A, B, C, R(42) ); |
| 235 | P( C, D, E, A, B, R(43) ); |
| 236 | P( B, C, D, E, A, R(44) ); |
| 237 | P( A, B, C, D, E, R(45) ); |
| 238 | P( E, A, B, C, D, R(46) ); |
| 239 | P( D, E, A, B, C, R(47) ); |
| 240 | P( C, D, E, A, B, R(48) ); |
| 241 | P( B, C, D, E, A, R(49) ); |
| 242 | P( A, B, C, D, E, R(50) ); |
| 243 | P( E, A, B, C, D, R(51) ); |
| 244 | P( D, E, A, B, C, R(52) ); |
| 245 | P( C, D, E, A, B, R(53) ); |
| 246 | P( B, C, D, E, A, R(54) ); |
| 247 | P( A, B, C, D, E, R(55) ); |
| 248 | P( E, A, B, C, D, R(56) ); |
| 249 | P( D, E, A, B, C, R(57) ); |
| 250 | P( C, D, E, A, B, R(58) ); |
| 251 | P( B, C, D, E, A, R(59) ); |
| 252 | |
| 253 | #undef K |
| 254 | #undef F |
| 255 | |
| 256 | #define F(x,y,z) (x ^ y ^ z) |
| 257 | #define K 0xCA62C1D6 |
| 258 | |
| 259 | P( A, B, C, D, E, R(60) ); |
| 260 | P( E, A, B, C, D, R(61) ); |
| 261 | P( D, E, A, B, C, R(62) ); |
| 262 | P( C, D, E, A, B, R(63) ); |
| 263 | P( B, C, D, E, A, R(64) ); |
| 264 | P( A, B, C, D, E, R(65) ); |
| 265 | P( E, A, B, C, D, R(66) ); |
| 266 | P( D, E, A, B, C, R(67) ); |
| 267 | P( C, D, E, A, B, R(68) ); |
| 268 | P( B, C, D, E, A, R(69) ); |
| 269 | P( A, B, C, D, E, R(70) ); |
| 270 | P( E, A, B, C, D, R(71) ); |
| 271 | P( D, E, A, B, C, R(72) ); |
| 272 | P( C, D, E, A, B, R(73) ); |
| 273 | P( B, C, D, E, A, R(74) ); |
| 274 | P( A, B, C, D, E, R(75) ); |
| 275 | P( E, A, B, C, D, R(76) ); |
| 276 | P( D, E, A, B, C, R(77) ); |
| 277 | P( C, D, E, A, B, R(78) ); |
| 278 | P( B, C, D, E, A, R(79) ); |
| 279 | |
| 280 | #undef K |
| 281 | #undef F |
| 282 | |
| 283 | ctx->state[0] += A; |
| 284 | ctx->state[1] += B; |
| 285 | ctx->state[2] += C; |
| 286 | ctx->state[3] += D; |
| 287 | ctx->state[4] += E; |
Andres Amaya Garcia | 034ea7e | 2017-04-28 15:14:50 +0100 | [diff] [blame] | 288 | |
| 289 | return( 0 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 290 | } |
Jaeden Amero | 041039f | 2018-02-19 15:28:08 +0000 | [diff] [blame] | 291 | |
| 292 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
| 293 | void mbedtls_sha1_process( mbedtls_sha1_context *ctx, |
| 294 | const unsigned char data[64] ) |
| 295 | { |
| 296 | mbedtls_internal_sha1_process( ctx, data ); |
| 297 | } |
| 298 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 299 | #endif /* !MBEDTLS_SHA1_PROCESS_ALT */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 300 | |
| 301 | /* |
| 302 | * SHA-1 process buffer |
| 303 | */ |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 304 | int mbedtls_sha1_update_ret( mbedtls_sha1_context *ctx, |
Andres Amaya Garcia | 034ea7e | 2017-04-28 15:14:50 +0100 | [diff] [blame] | 305 | const unsigned char *input, |
| 306 | size_t ilen ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 307 | { |
Andres Amaya Garcia | 034ea7e | 2017-04-28 15:14:50 +0100 | [diff] [blame] | 308 | int ret; |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 309 | size_t fill; |
Paul Bakker | 5c2364c | 2012-10-01 14:41:15 +0000 | [diff] [blame] | 310 | uint32_t left; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 311 | |
Hanno Becker | 039ccab | 2018-12-18 17:52:14 +0000 | [diff] [blame] | 312 | SHA1_VALIDATE_RET( ctx != NULL ); |
| 313 | SHA1_VALIDATE_RET( ilen == 0 || input != NULL ); |
Hanno Becker | b3906d8 | 2018-12-18 11:35:00 +0000 | [diff] [blame] | 314 | |
Brian White | 12895d1 | 2014-04-11 11:29:42 -0400 | [diff] [blame] | 315 | if( ilen == 0 ) |
Andres Amaya Garcia | 034ea7e | 2017-04-28 15:14:50 +0100 | [diff] [blame] | 316 | return( 0 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 317 | |
| 318 | left = ctx->total[0] & 0x3F; |
| 319 | fill = 64 - left; |
| 320 | |
Paul Bakker | 5c2364c | 2012-10-01 14:41:15 +0000 | [diff] [blame] | 321 | ctx->total[0] += (uint32_t) ilen; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 322 | ctx->total[0] &= 0xFFFFFFFF; |
| 323 | |
Paul Bakker | 5c2364c | 2012-10-01 14:41:15 +0000 | [diff] [blame] | 324 | if( ctx->total[0] < (uint32_t) ilen ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 325 | ctx->total[1]++; |
| 326 | |
| 327 | if( left && ilen >= fill ) |
| 328 | { |
Paul Bakker | 3c2122f | 2013-06-24 19:03:14 +0200 | [diff] [blame] | 329 | memcpy( (void *) (ctx->buffer + left), input, fill ); |
Andres Amaya Garcia | 034ea7e | 2017-04-28 15:14:50 +0100 | [diff] [blame] | 330 | |
Andres Amaya Garcia | cccfe08 | 2017-06-28 10:36:39 +0100 | [diff] [blame] | 331 | if( ( ret = mbedtls_internal_sha1_process( ctx, ctx->buffer ) ) != 0 ) |
Andres Amaya Garcia | 034ea7e | 2017-04-28 15:14:50 +0100 | [diff] [blame] | 332 | return( ret ); |
| 333 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 334 | input += fill; |
| 335 | ilen -= fill; |
| 336 | left = 0; |
| 337 | } |
| 338 | |
| 339 | while( ilen >= 64 ) |
| 340 | { |
Andres Amaya Garcia | cccfe08 | 2017-06-28 10:36:39 +0100 | [diff] [blame] | 341 | if( ( ret = mbedtls_internal_sha1_process( ctx, input ) ) != 0 ) |
Andres Amaya Garcia | 034ea7e | 2017-04-28 15:14:50 +0100 | [diff] [blame] | 342 | return( ret ); |
| 343 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 344 | input += 64; |
| 345 | ilen -= 64; |
| 346 | } |
| 347 | |
| 348 | if( ilen > 0 ) |
Paul Bakker | 3c2122f | 2013-06-24 19:03:14 +0200 | [diff] [blame] | 349 | memcpy( (void *) (ctx->buffer + left), input, ilen ); |
Andres Amaya Garcia | 034ea7e | 2017-04-28 15:14:50 +0100 | [diff] [blame] | 350 | |
| 351 | return( 0 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 352 | } |
| 353 | |
Jaeden Amero | 041039f | 2018-02-19 15:28:08 +0000 | [diff] [blame] | 354 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
| 355 | void mbedtls_sha1_update( mbedtls_sha1_context *ctx, |
| 356 | const unsigned char *input, |
| 357 | size_t ilen ) |
| 358 | { |
| 359 | mbedtls_sha1_update_ret( ctx, input, ilen ); |
| 360 | } |
| 361 | #endif |
| 362 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 363 | /* |
| 364 | * SHA-1 final digest |
| 365 | */ |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 366 | int mbedtls_sha1_finish_ret( mbedtls_sha1_context *ctx, |
Andres Amaya Garcia | 034ea7e | 2017-04-28 15:14:50 +0100 | [diff] [blame] | 367 | unsigned char output[20] ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 368 | { |
Andres Amaya Garcia | 034ea7e | 2017-04-28 15:14:50 +0100 | [diff] [blame] | 369 | int ret; |
Manuel Pégourié-Gonnard | 1cc1fb0 | 2018-06-28 12:10:27 +0200 | [diff] [blame] | 370 | uint32_t used; |
Paul Bakker | 5c2364c | 2012-10-01 14:41:15 +0000 | [diff] [blame] | 371 | uint32_t high, low; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 372 | |
Hanno Becker | 039ccab | 2018-12-18 17:52:14 +0000 | [diff] [blame] | 373 | SHA1_VALIDATE_RET( ctx != NULL ); |
| 374 | SHA1_VALIDATE_RET( (unsigned char *)output != NULL ); |
Andres Amaya Garcia | f7c43b3 | 2018-12-09 19:12:19 +0000 | [diff] [blame] | 375 | |
Manuel Pégourié-Gonnard | 1cc1fb0 | 2018-06-28 12:10:27 +0200 | [diff] [blame] | 376 | /* |
| 377 | * Add padding: 0x80 then 0x00 until 8 bytes remain for the length |
| 378 | */ |
| 379 | used = ctx->total[0] & 0x3F; |
| 380 | |
| 381 | ctx->buffer[used++] = 0x80; |
| 382 | |
| 383 | if( used <= 56 ) |
| 384 | { |
| 385 | /* Enough room for padding + length in current block */ |
| 386 | memset( ctx->buffer + used, 0, 56 - used ); |
| 387 | } |
| 388 | else |
| 389 | { |
| 390 | /* We'll need an extra block */ |
| 391 | memset( ctx->buffer + used, 0, 64 - used ); |
| 392 | |
| 393 | if( ( ret = mbedtls_internal_sha1_process( ctx, ctx->buffer ) ) != 0 ) |
| 394 | return( ret ); |
| 395 | |
| 396 | memset( ctx->buffer, 0, 56 ); |
| 397 | } |
| 398 | |
| 399 | /* |
| 400 | * Add message length |
| 401 | */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 402 | high = ( ctx->total[0] >> 29 ) |
| 403 | | ( ctx->total[1] << 3 ); |
| 404 | low = ( ctx->total[0] << 3 ); |
| 405 | |
Manuel Pégourié-Gonnard | 1cc1fb0 | 2018-06-28 12:10:27 +0200 | [diff] [blame] | 406 | PUT_UINT32_BE( high, ctx->buffer, 56 ); |
| 407 | PUT_UINT32_BE( low, ctx->buffer, 60 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 408 | |
Manuel Pégourié-Gonnard | 1cc1fb0 | 2018-06-28 12:10:27 +0200 | [diff] [blame] | 409 | if( ( ret = mbedtls_internal_sha1_process( ctx, ctx->buffer ) ) != 0 ) |
Andres Amaya Garcia | 034ea7e | 2017-04-28 15:14:50 +0100 | [diff] [blame] | 410 | return( ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 411 | |
Manuel Pégourié-Gonnard | 1cc1fb0 | 2018-06-28 12:10:27 +0200 | [diff] [blame] | 412 | /* |
| 413 | * Output final state |
| 414 | */ |
Paul Bakker | 5c2364c | 2012-10-01 14:41:15 +0000 | [diff] [blame] | 415 | PUT_UINT32_BE( ctx->state[0], output, 0 ); |
| 416 | PUT_UINT32_BE( ctx->state[1], output, 4 ); |
| 417 | PUT_UINT32_BE( ctx->state[2], output, 8 ); |
| 418 | PUT_UINT32_BE( ctx->state[3], output, 12 ); |
| 419 | PUT_UINT32_BE( ctx->state[4], output, 16 ); |
Andres Amaya Garcia | 034ea7e | 2017-04-28 15:14:50 +0100 | [diff] [blame] | 420 | |
| 421 | return( 0 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 422 | } |
| 423 | |
Jaeden Amero | 041039f | 2018-02-19 15:28:08 +0000 | [diff] [blame] | 424 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
| 425 | void mbedtls_sha1_finish( mbedtls_sha1_context *ctx, |
| 426 | unsigned char output[20] ) |
| 427 | { |
| 428 | mbedtls_sha1_finish_ret( ctx, output ); |
| 429 | } |
| 430 | #endif |
| 431 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 432 | #endif /* !MBEDTLS_SHA1_ALT */ |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 433 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 434 | /* |
| 435 | * output = SHA-1( input buffer ) |
| 436 | */ |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 437 | int mbedtls_sha1_ret( const unsigned char *input, |
Andres Amaya Garcia | 034ea7e | 2017-04-28 15:14:50 +0100 | [diff] [blame] | 438 | size_t ilen, |
| 439 | unsigned char output[20] ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 440 | { |
Andres Amaya Garcia | 034ea7e | 2017-04-28 15:14:50 +0100 | [diff] [blame] | 441 | int ret; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 442 | mbedtls_sha1_context ctx; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 443 | |
Hanno Becker | 039ccab | 2018-12-18 17:52:14 +0000 | [diff] [blame] | 444 | SHA1_VALIDATE_RET( ilen == 0 || input != NULL ); |
| 445 | SHA1_VALIDATE_RET( (unsigned char *)output != NULL ); |
Andres Amaya Garcia | f7c43b3 | 2018-12-09 19:12:19 +0000 | [diff] [blame] | 446 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 447 | mbedtls_sha1_init( &ctx ); |
Andres Amaya Garcia | 034ea7e | 2017-04-28 15:14:50 +0100 | [diff] [blame] | 448 | |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 449 | if( ( ret = mbedtls_sha1_starts_ret( &ctx ) ) != 0 ) |
Andres Amaya Garcia | 0963e6c | 2017-07-20 14:34:08 +0100 | [diff] [blame] | 450 | goto exit; |
Andres Amaya Garcia | 034ea7e | 2017-04-28 15:14:50 +0100 | [diff] [blame] | 451 | |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 452 | if( ( ret = mbedtls_sha1_update_ret( &ctx, input, ilen ) ) != 0 ) |
Andres Amaya Garcia | 0963e6c | 2017-07-20 14:34:08 +0100 | [diff] [blame] | 453 | goto exit; |
Andres Amaya Garcia | 034ea7e | 2017-04-28 15:14:50 +0100 | [diff] [blame] | 454 | |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 455 | if( ( ret = mbedtls_sha1_finish_ret( &ctx, output ) ) != 0 ) |
Andres Amaya Garcia | 0963e6c | 2017-07-20 14:34:08 +0100 | [diff] [blame] | 456 | goto exit; |
Andres Amaya Garcia | 034ea7e | 2017-04-28 15:14:50 +0100 | [diff] [blame] | 457 | |
Andres Amaya Garcia | 0963e6c | 2017-07-20 14:34:08 +0100 | [diff] [blame] | 458 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 459 | mbedtls_sha1_free( &ctx ); |
Andres Amaya Garcia | 034ea7e | 2017-04-28 15:14:50 +0100 | [diff] [blame] | 460 | |
Andres Amaya Garcia | 0963e6c | 2017-07-20 14:34:08 +0100 | [diff] [blame] | 461 | return( ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 462 | } |
| 463 | |
Jaeden Amero | 041039f | 2018-02-19 15:28:08 +0000 | [diff] [blame] | 464 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
| 465 | void mbedtls_sha1( const unsigned char *input, |
| 466 | size_t ilen, |
| 467 | unsigned char output[20] ) |
| 468 | { |
| 469 | mbedtls_sha1_ret( input, ilen, output ); |
| 470 | } |
| 471 | #endif |
| 472 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 473 | #if defined(MBEDTLS_SELF_TEST) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 474 | /* |
| 475 | * FIPS-180-1 test vectors |
| 476 | */ |
Manuel Pégourié-Gonnard | 28122e4 | 2015-03-11 09:13:42 +0000 | [diff] [blame] | 477 | static const unsigned char sha1_test_buf[3][57] = |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 478 | { |
| 479 | { "abc" }, |
| 480 | { "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" }, |
| 481 | { "" } |
| 482 | }; |
| 483 | |
Andres Amaya Garcia | 2d0aa8b | 2017-07-21 14:57:26 +0100 | [diff] [blame] | 484 | static const size_t sha1_test_buflen[3] = |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 485 | { |
| 486 | 3, 56, 1000 |
| 487 | }; |
| 488 | |
| 489 | static const unsigned char sha1_test_sum[3][20] = |
| 490 | { |
| 491 | { 0xA9, 0x99, 0x3E, 0x36, 0x47, 0x06, 0x81, 0x6A, 0xBA, 0x3E, |
| 492 | 0x25, 0x71, 0x78, 0x50, 0xC2, 0x6C, 0x9C, 0xD0, 0xD8, 0x9D }, |
| 493 | { 0x84, 0x98, 0x3E, 0x44, 0x1C, 0x3B, 0xD2, 0x6E, 0xBA, 0xAE, |
| 494 | 0x4A, 0xA1, 0xF9, 0x51, 0x29, 0xE5, 0xE5, 0x46, 0x70, 0xF1 }, |
| 495 | { 0x34, 0xAA, 0x97, 0x3C, 0xD4, 0xC4, 0xDA, 0xA4, 0xF6, 0x1E, |
| 496 | 0xEB, 0x2B, 0xDB, 0xAD, 0x27, 0x31, 0x65, 0x34, 0x01, 0x6F } |
| 497 | }; |
| 498 | |
| 499 | /* |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 500 | * Checkup routine |
| 501 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 502 | int mbedtls_sha1_self_test( int verbose ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 503 | { |
Paul Bakker | 5b4af39 | 2014-06-26 12:09:34 +0200 | [diff] [blame] | 504 | int i, j, buflen, ret = 0; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 505 | unsigned char buf[1024]; |
| 506 | unsigned char sha1sum[20]; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 507 | mbedtls_sha1_context ctx; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 508 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 509 | mbedtls_sha1_init( &ctx ); |
Paul Bakker | 5b4af39 | 2014-06-26 12:09:34 +0200 | [diff] [blame] | 510 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 511 | /* |
| 512 | * SHA-1 |
| 513 | */ |
| 514 | for( i = 0; i < 3; i++ ) |
| 515 | { |
| 516 | if( verbose != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 517 | mbedtls_printf( " SHA-1 test #%d: ", i + 1 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 518 | |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 519 | if( ( ret = mbedtls_sha1_starts_ret( &ctx ) ) != 0 ) |
Andres Amaya Garcia | 034ea7e | 2017-04-28 15:14:50 +0100 | [diff] [blame] | 520 | goto fail; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 521 | |
| 522 | if( i == 2 ) |
| 523 | { |
| 524 | memset( buf, 'a', buflen = 1000 ); |
| 525 | |
| 526 | for( j = 0; j < 1000; j++ ) |
Andres Amaya Garcia | 034ea7e | 2017-04-28 15:14:50 +0100 | [diff] [blame] | 527 | { |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 528 | ret = mbedtls_sha1_update_ret( &ctx, buf, buflen ); |
Andres Amaya Garcia | 6a3f305 | 2017-07-20 14:18:54 +0100 | [diff] [blame] | 529 | if( ret != 0 ) |
Andres Amaya Garcia | 034ea7e | 2017-04-28 15:14:50 +0100 | [diff] [blame] | 530 | goto fail; |
| 531 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 532 | } |
| 533 | else |
Andres Amaya Garcia | 034ea7e | 2017-04-28 15:14:50 +0100 | [diff] [blame] | 534 | { |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 535 | ret = mbedtls_sha1_update_ret( &ctx, sha1_test_buf[i], |
Andres Amaya Garcia | 6a3f305 | 2017-07-20 14:18:54 +0100 | [diff] [blame] | 536 | sha1_test_buflen[i] ); |
| 537 | if( ret != 0 ) |
Andres Amaya Garcia | 034ea7e | 2017-04-28 15:14:50 +0100 | [diff] [blame] | 538 | goto fail; |
| 539 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 540 | |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 541 | if( ( ret = mbedtls_sha1_finish_ret( &ctx, sha1sum ) ) != 0 ) |
Andres Amaya Garcia | 6a3f305 | 2017-07-20 14:18:54 +0100 | [diff] [blame] | 542 | goto fail; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 543 | |
| 544 | if( memcmp( sha1sum, sha1_test_sum[i], 20 ) != 0 ) |
Andres Amaya Garcia | 6a3f305 | 2017-07-20 14:18:54 +0100 | [diff] [blame] | 545 | { |
| 546 | ret = 1; |
| 547 | goto fail; |
| 548 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 549 | |
| 550 | if( verbose != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 551 | mbedtls_printf( "passed\n" ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 552 | } |
| 553 | |
| 554 | if( verbose != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 555 | mbedtls_printf( "\n" ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 556 | |
Andres Amaya Garcia | 034ea7e | 2017-04-28 15:14:50 +0100 | [diff] [blame] | 557 | goto exit; |
| 558 | |
| 559 | fail: |
| 560 | if( verbose != 0 ) |
| 561 | mbedtls_printf( "failed\n" ); |
| 562 | |
Paul Bakker | 5b4af39 | 2014-06-26 12:09:34 +0200 | [diff] [blame] | 563 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 564 | mbedtls_sha1_free( &ctx ); |
Paul Bakker | 5b4af39 | 2014-06-26 12:09:34 +0200 | [diff] [blame] | 565 | |
| 566 | return( ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 567 | } |
| 568 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 569 | #endif /* MBEDTLS_SELF_TEST */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 570 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 571 | #endif /* MBEDTLS_SHA1_C */ |