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