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