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