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