Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 1 | /** |
| 2 | * \file md_wrap.c |
| 3 | |
Manuel Pégourié-Gonnard | b4fe3cb | 2015-01-22 16:11:05 +0000 | [diff] [blame] | 4 | * \brief Generic message digest wrapper for mbed TLS |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 5 | * |
| 6 | * \author Adriaan de Jong <dejong@fox-it.com> |
| 7 | * |
Manuel Pégourié-Gonnard | a658a40 | 2015-01-23 09:45:19 +0000 | [diff] [blame] | 8 | * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 9 | * |
Manuel Pégourié-Gonnard | fe44643 | 2015-03-06 13:17:10 +0000 | [diff] [blame] | 10 | * This file is part of mbed TLS (https://tls.mbed.org) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 11 | * |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 12 | * This program is free software; you can redistribute it and/or modify |
| 13 | * it under the terms of the GNU General Public License as published by |
| 14 | * the Free Software Foundation; either version 2 of the License, or |
| 15 | * (at your option) any later version. |
| 16 | * |
| 17 | * This program is distributed in the hope that it will be useful, |
| 18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 20 | * GNU General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU General Public License along |
| 23 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 24 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 25 | */ |
| 26 | |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 27 | #if !defined(POLARSSL_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 |
| 30 | #include POLARSSL_CONFIG_FILE |
| 31 | #endif |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 32 | |
| 33 | #if defined(POLARSSL_MD_C) |
| 34 | |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 35 | #include "mbedtls/md_wrap.h" |
Paul Bakker | f654371 | 2012-03-05 14:01:29 +0000 | [diff] [blame] | 36 | |
| 37 | #if defined(POLARSSL_MD2_C) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 38 | #include "mbedtls/md2.h" |
Paul Bakker | f654371 | 2012-03-05 14:01:29 +0000 | [diff] [blame] | 39 | #endif |
| 40 | |
| 41 | #if defined(POLARSSL_MD4_C) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 42 | #include "mbedtls/md4.h" |
Paul Bakker | f654371 | 2012-03-05 14:01:29 +0000 | [diff] [blame] | 43 | #endif |
| 44 | |
| 45 | #if defined(POLARSSL_MD5_C) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 46 | #include "mbedtls/md5.h" |
Paul Bakker | f654371 | 2012-03-05 14:01:29 +0000 | [diff] [blame] | 47 | #endif |
| 48 | |
Paul Bakker | 61b699e | 2014-01-22 13:35:29 +0100 | [diff] [blame] | 49 | #if defined(POLARSSL_RIPEMD160_C) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 50 | #include "mbedtls/ripemd160.h" |
Manuel Pégourié-Gonnard | e4d47a6 | 2014-01-17 20:41:32 +0100 | [diff] [blame] | 51 | #endif |
| 52 | |
Paul Bakker | f654371 | 2012-03-05 14:01:29 +0000 | [diff] [blame] | 53 | #if defined(POLARSSL_SHA1_C) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 54 | #include "mbedtls/sha1.h" |
Paul Bakker | f654371 | 2012-03-05 14:01:29 +0000 | [diff] [blame] | 55 | #endif |
| 56 | |
Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 57 | #if defined(POLARSSL_SHA256_C) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 58 | #include "mbedtls/sha256.h" |
Paul Bakker | f654371 | 2012-03-05 14:01:29 +0000 | [diff] [blame] | 59 | #endif |
| 60 | |
Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 61 | #if defined(POLARSSL_SHA512_C) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 62 | #include "mbedtls/sha512.h" |
Paul Bakker | f654371 | 2012-03-05 14:01:29 +0000 | [diff] [blame] | 63 | #endif |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 64 | |
Paul Bakker | 7dc4c44 | 2014-02-01 22:50:26 +0100 | [diff] [blame] | 65 | #if defined(POLARSSL_PLATFORM_C) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 66 | #include "mbedtls/platform.h" |
Paul Bakker | 6e339b5 | 2013-07-03 13:37:05 +0200 | [diff] [blame] | 67 | #else |
Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 68 | #include <stdlib.h> |
Paul Bakker | 6e339b5 | 2013-07-03 13:37:05 +0200 | [diff] [blame] | 69 | #define polarssl_malloc malloc |
| 70 | #define polarssl_free free |
| 71 | #endif |
| 72 | |
Paul Bakker | 3461772 | 2014-06-13 17:20:13 +0200 | [diff] [blame] | 73 | /* Implementation that should never be optimized out by the compiler */ |
| 74 | static void polarssl_zeroize( void *v, size_t n ) { |
| 75 | volatile unsigned char *p = v; while( n-- ) *p++ = 0; |
| 76 | } |
| 77 | |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 78 | #if defined(POLARSSL_MD2_C) |
| 79 | |
| 80 | static void md2_starts_wrap( void *ctx ) |
| 81 | { |
| 82 | md2_starts( (md2_context *) ctx ); |
| 83 | } |
| 84 | |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 85 | static void md2_update_wrap( void *ctx, const unsigned char *input, |
| 86 | size_t ilen ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 87 | { |
| 88 | md2_update( (md2_context *) ctx, input, ilen ); |
| 89 | } |
| 90 | |
| 91 | static void md2_finish_wrap( void *ctx, unsigned char *output ) |
| 92 | { |
| 93 | md2_finish( (md2_context *) ctx, output ); |
| 94 | } |
| 95 | |
Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 96 | static int md2_file_wrap( const char *path, unsigned char *output ) |
Paul Bakker | 335db3f | 2011-04-25 15:28:35 +0000 | [diff] [blame] | 97 | { |
| 98 | #if defined(POLARSSL_FS_IO) |
| 99 | return md2_file( path, output ); |
| 100 | #else |
| 101 | ((void) path); |
| 102 | ((void) output); |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 103 | return( POLARSSL_ERR_MD_FEATURE_UNAVAILABLE ); |
Paul Bakker | 335db3f | 2011-04-25 15:28:35 +0000 | [diff] [blame] | 104 | #endif |
| 105 | } |
| 106 | |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 107 | static void md2_hmac_starts_wrap( void *ctx, const unsigned char *key, |
| 108 | size_t keylen ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 109 | { |
| 110 | md2_hmac_starts( (md2_context *) ctx, key, keylen ); |
| 111 | } |
| 112 | |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 113 | static void md2_hmac_update_wrap( void *ctx, const unsigned char *input, |
| 114 | size_t ilen ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 115 | { |
| 116 | md2_hmac_update( (md2_context *) ctx, input, ilen ); |
| 117 | } |
| 118 | |
| 119 | static void md2_hmac_finish_wrap( void *ctx, unsigned char *output ) |
| 120 | { |
| 121 | md2_hmac_finish( (md2_context *) ctx, output ); |
| 122 | } |
| 123 | |
| 124 | static void md2_hmac_reset_wrap( void *ctx ) |
| 125 | { |
| 126 | md2_hmac_reset( (md2_context *) ctx ); |
| 127 | } |
| 128 | |
| 129 | static void * md2_ctx_alloc( void ) |
| 130 | { |
Paul Bakker | 6e339b5 | 2013-07-03 13:37:05 +0200 | [diff] [blame] | 131 | return polarssl_malloc( sizeof( md2_context ) ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | static void md2_ctx_free( void *ctx ) |
| 135 | { |
Paul Bakker | 3461772 | 2014-06-13 17:20:13 +0200 | [diff] [blame] | 136 | polarssl_zeroize( ctx, sizeof( md2_context ) ); |
Paul Bakker | 6e339b5 | 2013-07-03 13:37:05 +0200 | [diff] [blame] | 137 | polarssl_free( ctx ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 138 | } |
| 139 | |
Paul Bakker | 1bd3ae8 | 2013-03-13 10:26:44 +0100 | [diff] [blame] | 140 | static void md2_process_wrap( void *ctx, const unsigned char *data ) |
| 141 | { |
| 142 | ((void) data); |
| 143 | |
| 144 | md2_process( (md2_context *) ctx ); |
| 145 | } |
| 146 | |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 147 | const md_info_t md2_info = { |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 148 | POLARSSL_MD_MD2, |
| 149 | "MD2", |
| 150 | 16, |
Manuel Pégourié-Gonnard | 8379a82 | 2015-03-24 16:48:22 +0100 | [diff] [blame^] | 151 | 16, |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 152 | md2_starts_wrap, |
| 153 | md2_update_wrap, |
| 154 | md2_finish_wrap, |
| 155 | md2, |
Paul Bakker | 335db3f | 2011-04-25 15:28:35 +0000 | [diff] [blame] | 156 | md2_file_wrap, |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 157 | md2_hmac_starts_wrap, |
| 158 | md2_hmac_update_wrap, |
| 159 | md2_hmac_finish_wrap, |
| 160 | md2_hmac_reset_wrap, |
| 161 | md2_hmac, |
| 162 | md2_ctx_alloc, |
| 163 | md2_ctx_free, |
Paul Bakker | 1bd3ae8 | 2013-03-13 10:26:44 +0100 | [diff] [blame] | 164 | md2_process_wrap, |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 165 | }; |
| 166 | |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 167 | #endif /* POLARSSL_MD2_C */ |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 168 | |
| 169 | #if defined(POLARSSL_MD4_C) |
| 170 | |
Paul Bakker | d1df02a | 2013-03-13 10:31:31 +0100 | [diff] [blame] | 171 | static void md4_starts_wrap( void *ctx ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 172 | { |
| 173 | md4_starts( (md4_context *) ctx ); |
| 174 | } |
| 175 | |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 176 | static void md4_update_wrap( void *ctx, const unsigned char *input, |
| 177 | size_t ilen ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 178 | { |
| 179 | md4_update( (md4_context *) ctx, input, ilen ); |
| 180 | } |
| 181 | |
Paul Bakker | d1df02a | 2013-03-13 10:31:31 +0100 | [diff] [blame] | 182 | static void md4_finish_wrap( void *ctx, unsigned char *output ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 183 | { |
| 184 | md4_finish( (md4_context *) ctx, output ); |
| 185 | } |
| 186 | |
Paul Bakker | d1df02a | 2013-03-13 10:31:31 +0100 | [diff] [blame] | 187 | static int md4_file_wrap( const char *path, unsigned char *output ) |
Paul Bakker | 335db3f | 2011-04-25 15:28:35 +0000 | [diff] [blame] | 188 | { |
| 189 | #if defined(POLARSSL_FS_IO) |
| 190 | return md4_file( path, output ); |
| 191 | #else |
| 192 | ((void) path); |
| 193 | ((void) output); |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 194 | return( POLARSSL_ERR_MD_FEATURE_UNAVAILABLE ); |
Paul Bakker | 335db3f | 2011-04-25 15:28:35 +0000 | [diff] [blame] | 195 | #endif |
| 196 | } |
| 197 | |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 198 | static void md4_hmac_starts_wrap( void *ctx, const unsigned char *key, |
| 199 | size_t keylen ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 200 | { |
| 201 | md4_hmac_starts( (md4_context *) ctx, key, keylen ); |
| 202 | } |
| 203 | |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 204 | static void md4_hmac_update_wrap( void *ctx, const unsigned char *input, |
| 205 | size_t ilen ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 206 | { |
| 207 | md4_hmac_update( (md4_context *) ctx, input, ilen ); |
| 208 | } |
| 209 | |
Paul Bakker | d1df02a | 2013-03-13 10:31:31 +0100 | [diff] [blame] | 210 | static void md4_hmac_finish_wrap( void *ctx, unsigned char *output ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 211 | { |
| 212 | md4_hmac_finish( (md4_context *) ctx, output ); |
| 213 | } |
| 214 | |
Paul Bakker | d1df02a | 2013-03-13 10:31:31 +0100 | [diff] [blame] | 215 | static void md4_hmac_reset_wrap( void *ctx ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 216 | { |
| 217 | md4_hmac_reset( (md4_context *) ctx ); |
| 218 | } |
| 219 | |
Paul Bakker | d1df02a | 2013-03-13 10:31:31 +0100 | [diff] [blame] | 220 | static void *md4_ctx_alloc( void ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 221 | { |
Paul Bakker | 6e339b5 | 2013-07-03 13:37:05 +0200 | [diff] [blame] | 222 | return polarssl_malloc( sizeof( md4_context ) ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 223 | } |
| 224 | |
Paul Bakker | d1df02a | 2013-03-13 10:31:31 +0100 | [diff] [blame] | 225 | static void md4_ctx_free( void *ctx ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 226 | { |
Paul Bakker | 3461772 | 2014-06-13 17:20:13 +0200 | [diff] [blame] | 227 | polarssl_zeroize( ctx, sizeof( md4_context ) ); |
Paul Bakker | 6e339b5 | 2013-07-03 13:37:05 +0200 | [diff] [blame] | 228 | polarssl_free( ctx ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 229 | } |
| 230 | |
Paul Bakker | d1df02a | 2013-03-13 10:31:31 +0100 | [diff] [blame] | 231 | static void md4_process_wrap( void *ctx, const unsigned char *data ) |
Paul Bakker | 1bd3ae8 | 2013-03-13 10:26:44 +0100 | [diff] [blame] | 232 | { |
| 233 | md4_process( (md4_context *) ctx, data ); |
| 234 | } |
| 235 | |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 236 | const md_info_t md4_info = { |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 237 | POLARSSL_MD_MD4, |
| 238 | "MD4", |
| 239 | 16, |
Manuel Pégourié-Gonnard | 8379a82 | 2015-03-24 16:48:22 +0100 | [diff] [blame^] | 240 | 64, |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 241 | md4_starts_wrap, |
| 242 | md4_update_wrap, |
| 243 | md4_finish_wrap, |
| 244 | md4, |
Paul Bakker | 335db3f | 2011-04-25 15:28:35 +0000 | [diff] [blame] | 245 | md4_file_wrap, |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 246 | md4_hmac_starts_wrap, |
| 247 | md4_hmac_update_wrap, |
| 248 | md4_hmac_finish_wrap, |
| 249 | md4_hmac_reset_wrap, |
| 250 | md4_hmac, |
| 251 | md4_ctx_alloc, |
| 252 | md4_ctx_free, |
Paul Bakker | 1bd3ae8 | 2013-03-13 10:26:44 +0100 | [diff] [blame] | 253 | md4_process_wrap, |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 254 | }; |
| 255 | |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 256 | #endif /* POLARSSL_MD4_C */ |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 257 | |
| 258 | #if defined(POLARSSL_MD5_C) |
| 259 | |
| 260 | static void md5_starts_wrap( void *ctx ) |
| 261 | { |
| 262 | md5_starts( (md5_context *) ctx ); |
| 263 | } |
| 264 | |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 265 | static void md5_update_wrap( void *ctx, const unsigned char *input, |
| 266 | size_t ilen ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 267 | { |
| 268 | md5_update( (md5_context *) ctx, input, ilen ); |
| 269 | } |
| 270 | |
| 271 | static void md5_finish_wrap( void *ctx, unsigned char *output ) |
| 272 | { |
| 273 | md5_finish( (md5_context *) ctx, output ); |
| 274 | } |
| 275 | |
Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 276 | static int md5_file_wrap( const char *path, unsigned char *output ) |
Paul Bakker | 335db3f | 2011-04-25 15:28:35 +0000 | [diff] [blame] | 277 | { |
| 278 | #if defined(POLARSSL_FS_IO) |
| 279 | return md5_file( path, output ); |
| 280 | #else |
| 281 | ((void) path); |
| 282 | ((void) output); |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 283 | return( POLARSSL_ERR_MD_FEATURE_UNAVAILABLE ); |
Paul Bakker | 335db3f | 2011-04-25 15:28:35 +0000 | [diff] [blame] | 284 | #endif |
| 285 | } |
| 286 | |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 287 | static void md5_hmac_starts_wrap( void *ctx, const unsigned char *key, |
| 288 | size_t keylen ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 289 | { |
| 290 | md5_hmac_starts( (md5_context *) ctx, key, keylen ); |
| 291 | } |
| 292 | |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 293 | static void md5_hmac_update_wrap( void *ctx, const unsigned char *input, |
| 294 | size_t ilen ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 295 | { |
| 296 | md5_hmac_update( (md5_context *) ctx, input, ilen ); |
| 297 | } |
| 298 | |
| 299 | static void md5_hmac_finish_wrap( void *ctx, unsigned char *output ) |
| 300 | { |
| 301 | md5_hmac_finish( (md5_context *) ctx, output ); |
| 302 | } |
| 303 | |
| 304 | static void md5_hmac_reset_wrap( void *ctx ) |
| 305 | { |
| 306 | md5_hmac_reset( (md5_context *) ctx ); |
| 307 | } |
| 308 | |
| 309 | static void * md5_ctx_alloc( void ) |
| 310 | { |
Paul Bakker | 6e339b5 | 2013-07-03 13:37:05 +0200 | [diff] [blame] | 311 | return polarssl_malloc( sizeof( md5_context ) ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | static void md5_ctx_free( void *ctx ) |
| 315 | { |
Paul Bakker | 3461772 | 2014-06-13 17:20:13 +0200 | [diff] [blame] | 316 | polarssl_zeroize( ctx, sizeof( md5_context ) ); |
Paul Bakker | 6e339b5 | 2013-07-03 13:37:05 +0200 | [diff] [blame] | 317 | polarssl_free( ctx ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 318 | } |
| 319 | |
Paul Bakker | 1bd3ae8 | 2013-03-13 10:26:44 +0100 | [diff] [blame] | 320 | static void md5_process_wrap( void *ctx, const unsigned char *data ) |
| 321 | { |
| 322 | md5_process( (md5_context *) ctx, data ); |
| 323 | } |
| 324 | |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 325 | const md_info_t md5_info = { |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 326 | POLARSSL_MD_MD5, |
| 327 | "MD5", |
| 328 | 16, |
Manuel Pégourié-Gonnard | 8379a82 | 2015-03-24 16:48:22 +0100 | [diff] [blame^] | 329 | 64, |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 330 | md5_starts_wrap, |
| 331 | md5_update_wrap, |
| 332 | md5_finish_wrap, |
| 333 | md5, |
Paul Bakker | 335db3f | 2011-04-25 15:28:35 +0000 | [diff] [blame] | 334 | md5_file_wrap, |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 335 | md5_hmac_starts_wrap, |
| 336 | md5_hmac_update_wrap, |
| 337 | md5_hmac_finish_wrap, |
| 338 | md5_hmac_reset_wrap, |
| 339 | md5_hmac, |
| 340 | md5_ctx_alloc, |
| 341 | md5_ctx_free, |
Paul Bakker | 1bd3ae8 | 2013-03-13 10:26:44 +0100 | [diff] [blame] | 342 | md5_process_wrap, |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 343 | }; |
| 344 | |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 345 | #endif /* POLARSSL_MD5_C */ |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 346 | |
Paul Bakker | 61b699e | 2014-01-22 13:35:29 +0100 | [diff] [blame] | 347 | #if defined(POLARSSL_RIPEMD160_C) |
Manuel Pégourié-Gonnard | e4d47a6 | 2014-01-17 20:41:32 +0100 | [diff] [blame] | 348 | |
Paul Bakker | 61b699e | 2014-01-22 13:35:29 +0100 | [diff] [blame] | 349 | static void ripemd160_starts_wrap( void *ctx ) |
Manuel Pégourié-Gonnard | e4d47a6 | 2014-01-17 20:41:32 +0100 | [diff] [blame] | 350 | { |
Paul Bakker | 61b699e | 2014-01-22 13:35:29 +0100 | [diff] [blame] | 351 | ripemd160_starts( (ripemd160_context *) ctx ); |
Manuel Pégourié-Gonnard | e4d47a6 | 2014-01-17 20:41:32 +0100 | [diff] [blame] | 352 | } |
| 353 | |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 354 | static void ripemd160_update_wrap( void *ctx, const unsigned char *input, |
| 355 | size_t ilen ) |
Manuel Pégourié-Gonnard | e4d47a6 | 2014-01-17 20:41:32 +0100 | [diff] [blame] | 356 | { |
Paul Bakker | 61b699e | 2014-01-22 13:35:29 +0100 | [diff] [blame] | 357 | ripemd160_update( (ripemd160_context *) ctx, input, ilen ); |
Manuel Pégourié-Gonnard | e4d47a6 | 2014-01-17 20:41:32 +0100 | [diff] [blame] | 358 | } |
| 359 | |
Paul Bakker | 61b699e | 2014-01-22 13:35:29 +0100 | [diff] [blame] | 360 | static void ripemd160_finish_wrap( void *ctx, unsigned char *output ) |
Manuel Pégourié-Gonnard | e4d47a6 | 2014-01-17 20:41:32 +0100 | [diff] [blame] | 361 | { |
Paul Bakker | 61b699e | 2014-01-22 13:35:29 +0100 | [diff] [blame] | 362 | ripemd160_finish( (ripemd160_context *) ctx, output ); |
Manuel Pégourié-Gonnard | e4d47a6 | 2014-01-17 20:41:32 +0100 | [diff] [blame] | 363 | } |
| 364 | |
Paul Bakker | 61b699e | 2014-01-22 13:35:29 +0100 | [diff] [blame] | 365 | static int ripemd160_file_wrap( const char *path, unsigned char *output ) |
Manuel Pégourié-Gonnard | e4d47a6 | 2014-01-17 20:41:32 +0100 | [diff] [blame] | 366 | { |
| 367 | #if defined(POLARSSL_FS_IO) |
Paul Bakker | 61b699e | 2014-01-22 13:35:29 +0100 | [diff] [blame] | 368 | return ripemd160_file( path, output ); |
Manuel Pégourié-Gonnard | e4d47a6 | 2014-01-17 20:41:32 +0100 | [diff] [blame] | 369 | #else |
| 370 | ((void) path); |
| 371 | ((void) output); |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 372 | return( POLARSSL_ERR_MD_FEATURE_UNAVAILABLE ); |
Manuel Pégourié-Gonnard | e4d47a6 | 2014-01-17 20:41:32 +0100 | [diff] [blame] | 373 | #endif |
| 374 | } |
| 375 | |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 376 | static void ripemd160_hmac_starts_wrap( void *ctx, const unsigned char *key, |
| 377 | size_t keylen ) |
Manuel Pégourié-Gonnard | e4d47a6 | 2014-01-17 20:41:32 +0100 | [diff] [blame] | 378 | { |
Paul Bakker | 61b699e | 2014-01-22 13:35:29 +0100 | [diff] [blame] | 379 | ripemd160_hmac_starts( (ripemd160_context *) ctx, key, keylen ); |
Manuel Pégourié-Gonnard | e4d47a6 | 2014-01-17 20:41:32 +0100 | [diff] [blame] | 380 | } |
| 381 | |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 382 | static void ripemd160_hmac_update_wrap( void *ctx, const unsigned char *input, |
| 383 | size_t ilen ) |
Manuel Pégourié-Gonnard | e4d47a6 | 2014-01-17 20:41:32 +0100 | [diff] [blame] | 384 | { |
Paul Bakker | 61b699e | 2014-01-22 13:35:29 +0100 | [diff] [blame] | 385 | ripemd160_hmac_update( (ripemd160_context *) ctx, input, ilen ); |
Manuel Pégourié-Gonnard | e4d47a6 | 2014-01-17 20:41:32 +0100 | [diff] [blame] | 386 | } |
| 387 | |
Paul Bakker | 61b699e | 2014-01-22 13:35:29 +0100 | [diff] [blame] | 388 | static void ripemd160_hmac_finish_wrap( void *ctx, unsigned char *output ) |
Manuel Pégourié-Gonnard | e4d47a6 | 2014-01-17 20:41:32 +0100 | [diff] [blame] | 389 | { |
Paul Bakker | 61b699e | 2014-01-22 13:35:29 +0100 | [diff] [blame] | 390 | ripemd160_hmac_finish( (ripemd160_context *) ctx, output ); |
Manuel Pégourié-Gonnard | e4d47a6 | 2014-01-17 20:41:32 +0100 | [diff] [blame] | 391 | } |
| 392 | |
Paul Bakker | 61b699e | 2014-01-22 13:35:29 +0100 | [diff] [blame] | 393 | static void ripemd160_hmac_reset_wrap( void *ctx ) |
Manuel Pégourié-Gonnard | e4d47a6 | 2014-01-17 20:41:32 +0100 | [diff] [blame] | 394 | { |
Paul Bakker | 61b699e | 2014-01-22 13:35:29 +0100 | [diff] [blame] | 395 | ripemd160_hmac_reset( (ripemd160_context *) ctx ); |
Manuel Pégourié-Gonnard | e4d47a6 | 2014-01-17 20:41:32 +0100 | [diff] [blame] | 396 | } |
| 397 | |
Paul Bakker | 61b699e | 2014-01-22 13:35:29 +0100 | [diff] [blame] | 398 | static void * ripemd160_ctx_alloc( void ) |
Manuel Pégourié-Gonnard | e4d47a6 | 2014-01-17 20:41:32 +0100 | [diff] [blame] | 399 | { |
Paul Bakker | 5b4af39 | 2014-06-26 12:09:34 +0200 | [diff] [blame] | 400 | ripemd160_context *ctx; |
Mansour Moufid | c531b4a | 2015-02-15 17:35:38 -0500 | [diff] [blame] | 401 | ctx = polarssl_malloc( sizeof( ripemd160_context ) ); |
Paul Bakker | 5b4af39 | 2014-06-26 12:09:34 +0200 | [diff] [blame] | 402 | |
| 403 | if( ctx == NULL ) |
| 404 | return( NULL ); |
| 405 | |
| 406 | ripemd160_init( ctx ); |
| 407 | |
| 408 | return( ctx ); |
Manuel Pégourié-Gonnard | e4d47a6 | 2014-01-17 20:41:32 +0100 | [diff] [blame] | 409 | } |
| 410 | |
Paul Bakker | 61b699e | 2014-01-22 13:35:29 +0100 | [diff] [blame] | 411 | static void ripemd160_ctx_free( void *ctx ) |
Manuel Pégourié-Gonnard | e4d47a6 | 2014-01-17 20:41:32 +0100 | [diff] [blame] | 412 | { |
Paul Bakker | 5b4af39 | 2014-06-26 12:09:34 +0200 | [diff] [blame] | 413 | ripemd160_free( (ripemd160_context *) ctx ); |
Manuel Pégourié-Gonnard | e4d47a6 | 2014-01-17 20:41:32 +0100 | [diff] [blame] | 414 | polarssl_free( ctx ); |
| 415 | } |
| 416 | |
Paul Bakker | 61b699e | 2014-01-22 13:35:29 +0100 | [diff] [blame] | 417 | static void ripemd160_process_wrap( void *ctx, const unsigned char *data ) |
Manuel Pégourié-Gonnard | e4d47a6 | 2014-01-17 20:41:32 +0100 | [diff] [blame] | 418 | { |
Paul Bakker | 61b699e | 2014-01-22 13:35:29 +0100 | [diff] [blame] | 419 | ripemd160_process( (ripemd160_context *) ctx, data ); |
Manuel Pégourié-Gonnard | e4d47a6 | 2014-01-17 20:41:32 +0100 | [diff] [blame] | 420 | } |
| 421 | |
Paul Bakker | 61b699e | 2014-01-22 13:35:29 +0100 | [diff] [blame] | 422 | const md_info_t ripemd160_info = { |
| 423 | POLARSSL_MD_RIPEMD160, |
| 424 | "RIPEMD160", |
Manuel Pégourié-Gonnard | e4d47a6 | 2014-01-17 20:41:32 +0100 | [diff] [blame] | 425 | 20, |
Manuel Pégourié-Gonnard | 8379a82 | 2015-03-24 16:48:22 +0100 | [diff] [blame^] | 426 | 64, |
Paul Bakker | 61b699e | 2014-01-22 13:35:29 +0100 | [diff] [blame] | 427 | ripemd160_starts_wrap, |
| 428 | ripemd160_update_wrap, |
| 429 | ripemd160_finish_wrap, |
| 430 | ripemd160, |
| 431 | ripemd160_file_wrap, |
| 432 | ripemd160_hmac_starts_wrap, |
| 433 | ripemd160_hmac_update_wrap, |
| 434 | ripemd160_hmac_finish_wrap, |
| 435 | ripemd160_hmac_reset_wrap, |
| 436 | ripemd160_hmac, |
| 437 | ripemd160_ctx_alloc, |
| 438 | ripemd160_ctx_free, |
| 439 | ripemd160_process_wrap, |
Manuel Pégourié-Gonnard | e4d47a6 | 2014-01-17 20:41:32 +0100 | [diff] [blame] | 440 | }; |
| 441 | |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 442 | #endif /* POLARSSL_RIPEMD160_C */ |
Manuel Pégourié-Gonnard | e4d47a6 | 2014-01-17 20:41:32 +0100 | [diff] [blame] | 443 | |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 444 | #if defined(POLARSSL_SHA1_C) |
| 445 | |
Paul Bakker | d1df02a | 2013-03-13 10:31:31 +0100 | [diff] [blame] | 446 | static void sha1_starts_wrap( void *ctx ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 447 | { |
| 448 | sha1_starts( (sha1_context *) ctx ); |
| 449 | } |
| 450 | |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 451 | static void sha1_update_wrap( void *ctx, const unsigned char *input, |
| 452 | size_t ilen ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 453 | { |
| 454 | sha1_update( (sha1_context *) ctx, input, ilen ); |
| 455 | } |
| 456 | |
Paul Bakker | d1df02a | 2013-03-13 10:31:31 +0100 | [diff] [blame] | 457 | static void sha1_finish_wrap( void *ctx, unsigned char *output ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 458 | { |
| 459 | sha1_finish( (sha1_context *) ctx, output ); |
| 460 | } |
| 461 | |
Paul Bakker | d1df02a | 2013-03-13 10:31:31 +0100 | [diff] [blame] | 462 | static int sha1_file_wrap( const char *path, unsigned char *output ) |
Paul Bakker | 335db3f | 2011-04-25 15:28:35 +0000 | [diff] [blame] | 463 | { |
| 464 | #if defined(POLARSSL_FS_IO) |
| 465 | return sha1_file( path, output ); |
| 466 | #else |
| 467 | ((void) path); |
| 468 | ((void) output); |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 469 | return( POLARSSL_ERR_MD_FEATURE_UNAVAILABLE ); |
Paul Bakker | 335db3f | 2011-04-25 15:28:35 +0000 | [diff] [blame] | 470 | #endif |
| 471 | } |
| 472 | |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 473 | static void sha1_hmac_starts_wrap( void *ctx, const unsigned char *key, |
| 474 | size_t keylen ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 475 | { |
| 476 | sha1_hmac_starts( (sha1_context *) ctx, key, keylen ); |
| 477 | } |
| 478 | |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 479 | static void sha1_hmac_update_wrap( void *ctx, const unsigned char *input, |
| 480 | size_t ilen ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 481 | { |
| 482 | sha1_hmac_update( (sha1_context *) ctx, input, ilen ); |
| 483 | } |
| 484 | |
Paul Bakker | d1df02a | 2013-03-13 10:31:31 +0100 | [diff] [blame] | 485 | static void sha1_hmac_finish_wrap( void *ctx, unsigned char *output ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 486 | { |
| 487 | sha1_hmac_finish( (sha1_context *) ctx, output ); |
| 488 | } |
| 489 | |
Paul Bakker | d1df02a | 2013-03-13 10:31:31 +0100 | [diff] [blame] | 490 | static void sha1_hmac_reset_wrap( void *ctx ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 491 | { |
| 492 | sha1_hmac_reset( (sha1_context *) ctx ); |
| 493 | } |
| 494 | |
Paul Bakker | d1df02a | 2013-03-13 10:31:31 +0100 | [diff] [blame] | 495 | static void * sha1_ctx_alloc( void ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 496 | { |
Paul Bakker | 5b4af39 | 2014-06-26 12:09:34 +0200 | [diff] [blame] | 497 | sha1_context *ctx; |
Mansour Moufid | c531b4a | 2015-02-15 17:35:38 -0500 | [diff] [blame] | 498 | ctx = polarssl_malloc( sizeof( sha1_context ) ); |
Paul Bakker | 5b4af39 | 2014-06-26 12:09:34 +0200 | [diff] [blame] | 499 | |
| 500 | if( ctx == NULL ) |
| 501 | return( NULL ); |
| 502 | |
| 503 | sha1_init( ctx ); |
| 504 | |
| 505 | return( ctx ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 506 | } |
| 507 | |
Paul Bakker | d1df02a | 2013-03-13 10:31:31 +0100 | [diff] [blame] | 508 | static void sha1_ctx_free( void *ctx ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 509 | { |
Paul Bakker | 5b4af39 | 2014-06-26 12:09:34 +0200 | [diff] [blame] | 510 | sha1_free( (sha1_context *) ctx ); |
Paul Bakker | 6e339b5 | 2013-07-03 13:37:05 +0200 | [diff] [blame] | 511 | polarssl_free( ctx ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 512 | } |
| 513 | |
Paul Bakker | d1df02a | 2013-03-13 10:31:31 +0100 | [diff] [blame] | 514 | static void sha1_process_wrap( void *ctx, const unsigned char *data ) |
Paul Bakker | 1bd3ae8 | 2013-03-13 10:26:44 +0100 | [diff] [blame] | 515 | { |
| 516 | sha1_process( (sha1_context *) ctx, data ); |
| 517 | } |
| 518 | |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 519 | const md_info_t sha1_info = { |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 520 | POLARSSL_MD_SHA1, |
| 521 | "SHA1", |
| 522 | 20, |
Manuel Pégourié-Gonnard | 8379a82 | 2015-03-24 16:48:22 +0100 | [diff] [blame^] | 523 | 64, |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 524 | sha1_starts_wrap, |
| 525 | sha1_update_wrap, |
| 526 | sha1_finish_wrap, |
| 527 | sha1, |
Paul Bakker | 335db3f | 2011-04-25 15:28:35 +0000 | [diff] [blame] | 528 | sha1_file_wrap, |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 529 | sha1_hmac_starts_wrap, |
| 530 | sha1_hmac_update_wrap, |
| 531 | sha1_hmac_finish_wrap, |
| 532 | sha1_hmac_reset_wrap, |
| 533 | sha1_hmac, |
| 534 | sha1_ctx_alloc, |
| 535 | sha1_ctx_free, |
Paul Bakker | 1bd3ae8 | 2013-03-13 10:26:44 +0100 | [diff] [blame] | 536 | sha1_process_wrap, |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 537 | }; |
| 538 | |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 539 | #endif /* POLARSSL_SHA1_C */ |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 540 | |
| 541 | /* |
| 542 | * Wrappers for generic message digests |
| 543 | */ |
Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 544 | #if defined(POLARSSL_SHA256_C) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 545 | |
Paul Bakker | d1df02a | 2013-03-13 10:31:31 +0100 | [diff] [blame] | 546 | static void sha224_starts_wrap( void *ctx ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 547 | { |
Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 548 | sha256_starts( (sha256_context *) ctx, 1 ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 549 | } |
| 550 | |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 551 | static void sha224_update_wrap( void *ctx, const unsigned char *input, |
| 552 | size_t ilen ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 553 | { |
Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 554 | sha256_update( (sha256_context *) ctx, input, ilen ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 555 | } |
| 556 | |
Paul Bakker | d1df02a | 2013-03-13 10:31:31 +0100 | [diff] [blame] | 557 | static void sha224_finish_wrap( void *ctx, unsigned char *output ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 558 | { |
Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 559 | sha256_finish( (sha256_context *) ctx, output ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 560 | } |
| 561 | |
Paul Bakker | d1df02a | 2013-03-13 10:31:31 +0100 | [diff] [blame] | 562 | static void sha224_wrap( const unsigned char *input, size_t ilen, |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 563 | unsigned char *output ) |
| 564 | { |
Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 565 | sha256( input, ilen, output, 1 ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 566 | } |
| 567 | |
Paul Bakker | d1df02a | 2013-03-13 10:31:31 +0100 | [diff] [blame] | 568 | static int sha224_file_wrap( const char *path, unsigned char *output ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 569 | { |
Paul Bakker | 335db3f | 2011-04-25 15:28:35 +0000 | [diff] [blame] | 570 | #if defined(POLARSSL_FS_IO) |
Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 571 | return sha256_file( path, output, 1 ); |
Paul Bakker | 335db3f | 2011-04-25 15:28:35 +0000 | [diff] [blame] | 572 | #else |
| 573 | ((void) path); |
| 574 | ((void) output); |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 575 | return( POLARSSL_ERR_MD_FEATURE_UNAVAILABLE ); |
Paul Bakker | 335db3f | 2011-04-25 15:28:35 +0000 | [diff] [blame] | 576 | #endif |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 577 | } |
| 578 | |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 579 | static void sha224_hmac_starts_wrap( void *ctx, const unsigned char *key, |
| 580 | size_t keylen ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 581 | { |
Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 582 | sha256_hmac_starts( (sha256_context *) ctx, key, keylen, 1 ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 583 | } |
| 584 | |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 585 | static void sha224_hmac_update_wrap( void *ctx, const unsigned char *input, |
| 586 | size_t ilen ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 587 | { |
Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 588 | sha256_hmac_update( (sha256_context *) ctx, input, ilen ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 589 | } |
| 590 | |
Paul Bakker | d1df02a | 2013-03-13 10:31:31 +0100 | [diff] [blame] | 591 | static void sha224_hmac_finish_wrap( void *ctx, unsigned char *output ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 592 | { |
Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 593 | sha256_hmac_finish( (sha256_context *) ctx, output ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 594 | } |
| 595 | |
Paul Bakker | d1df02a | 2013-03-13 10:31:31 +0100 | [diff] [blame] | 596 | static void sha224_hmac_reset_wrap( void *ctx ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 597 | { |
Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 598 | sha256_hmac_reset( (sha256_context *) ctx ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 599 | } |
| 600 | |
Paul Bakker | d1df02a | 2013-03-13 10:31:31 +0100 | [diff] [blame] | 601 | static void sha224_hmac_wrap( const unsigned char *key, size_t keylen, |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 602 | const unsigned char *input, size_t ilen, |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 603 | unsigned char *output ) |
| 604 | { |
Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 605 | sha256_hmac( key, keylen, input, ilen, output, 1 ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 606 | } |
| 607 | |
Paul Bakker | d1df02a | 2013-03-13 10:31:31 +0100 | [diff] [blame] | 608 | static void * sha224_ctx_alloc( void ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 609 | { |
Paul Bakker | 6e339b5 | 2013-07-03 13:37:05 +0200 | [diff] [blame] | 610 | return polarssl_malloc( sizeof( sha256_context ) ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 611 | } |
| 612 | |
Paul Bakker | d1df02a | 2013-03-13 10:31:31 +0100 | [diff] [blame] | 613 | static void sha224_ctx_free( void *ctx ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 614 | { |
Paul Bakker | 3461772 | 2014-06-13 17:20:13 +0200 | [diff] [blame] | 615 | polarssl_zeroize( ctx, sizeof( sha256_context ) ); |
Paul Bakker | 6e339b5 | 2013-07-03 13:37:05 +0200 | [diff] [blame] | 616 | polarssl_free( ctx ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 617 | } |
| 618 | |
Paul Bakker | d1df02a | 2013-03-13 10:31:31 +0100 | [diff] [blame] | 619 | static void sha224_process_wrap( void *ctx, const unsigned char *data ) |
Paul Bakker | 1bd3ae8 | 2013-03-13 10:26:44 +0100 | [diff] [blame] | 620 | { |
Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 621 | sha256_process( (sha256_context *) ctx, data ); |
Paul Bakker | 1bd3ae8 | 2013-03-13 10:26:44 +0100 | [diff] [blame] | 622 | } |
| 623 | |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 624 | const md_info_t sha224_info = { |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 625 | POLARSSL_MD_SHA224, |
| 626 | "SHA224", |
| 627 | 28, |
Manuel Pégourié-Gonnard | 8379a82 | 2015-03-24 16:48:22 +0100 | [diff] [blame^] | 628 | 64, |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 629 | sha224_starts_wrap, |
| 630 | sha224_update_wrap, |
| 631 | sha224_finish_wrap, |
| 632 | sha224_wrap, |
| 633 | sha224_file_wrap, |
| 634 | sha224_hmac_starts_wrap, |
| 635 | sha224_hmac_update_wrap, |
| 636 | sha224_hmac_finish_wrap, |
| 637 | sha224_hmac_reset_wrap, |
| 638 | sha224_hmac_wrap, |
| 639 | sha224_ctx_alloc, |
| 640 | sha224_ctx_free, |
Paul Bakker | 1bd3ae8 | 2013-03-13 10:26:44 +0100 | [diff] [blame] | 641 | sha224_process_wrap, |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 642 | }; |
| 643 | |
Paul Bakker | d1df02a | 2013-03-13 10:31:31 +0100 | [diff] [blame] | 644 | static void sha256_starts_wrap( void *ctx ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 645 | { |
Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 646 | sha256_starts( (sha256_context *) ctx, 0 ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 647 | } |
| 648 | |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 649 | static void sha256_update_wrap( void *ctx, const unsigned char *input, |
| 650 | size_t ilen ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 651 | { |
Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 652 | sha256_update( (sha256_context *) ctx, input, ilen ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 653 | } |
| 654 | |
Paul Bakker | d1df02a | 2013-03-13 10:31:31 +0100 | [diff] [blame] | 655 | static void sha256_finish_wrap( void *ctx, unsigned char *output ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 656 | { |
Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 657 | sha256_finish( (sha256_context *) ctx, output ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 658 | } |
| 659 | |
Paul Bakker | d1df02a | 2013-03-13 10:31:31 +0100 | [diff] [blame] | 660 | static void sha256_wrap( const unsigned char *input, size_t ilen, |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 661 | unsigned char *output ) |
| 662 | { |
Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 663 | sha256( input, ilen, output, 0 ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 664 | } |
| 665 | |
Paul Bakker | d1df02a | 2013-03-13 10:31:31 +0100 | [diff] [blame] | 666 | static int sha256_file_wrap( const char *path, unsigned char *output ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 667 | { |
Paul Bakker | 335db3f | 2011-04-25 15:28:35 +0000 | [diff] [blame] | 668 | #if defined(POLARSSL_FS_IO) |
Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 669 | return sha256_file( path, output, 0 ); |
Paul Bakker | 335db3f | 2011-04-25 15:28:35 +0000 | [diff] [blame] | 670 | #else |
| 671 | ((void) path); |
| 672 | ((void) output); |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 673 | return( POLARSSL_ERR_MD_FEATURE_UNAVAILABLE ); |
Paul Bakker | 335db3f | 2011-04-25 15:28:35 +0000 | [diff] [blame] | 674 | #endif |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 675 | } |
| 676 | |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 677 | static void sha256_hmac_starts_wrap( void *ctx, const unsigned char *key, |
| 678 | size_t keylen ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 679 | { |
Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 680 | sha256_hmac_starts( (sha256_context *) ctx, key, keylen, 0 ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 681 | } |
| 682 | |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 683 | static void sha256_hmac_update_wrap( void *ctx, const unsigned char *input, |
| 684 | size_t ilen ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 685 | { |
Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 686 | sha256_hmac_update( (sha256_context *) ctx, input, ilen ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 687 | } |
| 688 | |
Paul Bakker | d1df02a | 2013-03-13 10:31:31 +0100 | [diff] [blame] | 689 | static void sha256_hmac_finish_wrap( void *ctx, unsigned char *output ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 690 | { |
Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 691 | sha256_hmac_finish( (sha256_context *) ctx, output ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 692 | } |
| 693 | |
Paul Bakker | d1df02a | 2013-03-13 10:31:31 +0100 | [diff] [blame] | 694 | static void sha256_hmac_reset_wrap( void *ctx ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 695 | { |
Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 696 | sha256_hmac_reset( (sha256_context *) ctx ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 697 | } |
| 698 | |
Paul Bakker | d1df02a | 2013-03-13 10:31:31 +0100 | [diff] [blame] | 699 | static void sha256_hmac_wrap( const unsigned char *key, size_t keylen, |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 700 | const unsigned char *input, size_t ilen, |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 701 | unsigned char *output ) |
| 702 | { |
Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 703 | sha256_hmac( key, keylen, input, ilen, output, 0 ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 704 | } |
| 705 | |
Paul Bakker | d1df02a | 2013-03-13 10:31:31 +0100 | [diff] [blame] | 706 | static void * sha256_ctx_alloc( void ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 707 | { |
Paul Bakker | 5b4af39 | 2014-06-26 12:09:34 +0200 | [diff] [blame] | 708 | sha256_context *ctx; |
Mansour Moufid | c531b4a | 2015-02-15 17:35:38 -0500 | [diff] [blame] | 709 | ctx = polarssl_malloc( sizeof( sha256_context ) ); |
Paul Bakker | 5b4af39 | 2014-06-26 12:09:34 +0200 | [diff] [blame] | 710 | |
| 711 | if( ctx == NULL ) |
| 712 | return( NULL ); |
| 713 | |
| 714 | sha256_init( ctx ); |
| 715 | |
| 716 | return( ctx ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 717 | } |
| 718 | |
Paul Bakker | d1df02a | 2013-03-13 10:31:31 +0100 | [diff] [blame] | 719 | static void sha256_ctx_free( void *ctx ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 720 | { |
Paul Bakker | 5b4af39 | 2014-06-26 12:09:34 +0200 | [diff] [blame] | 721 | sha256_free( (sha256_context *) ctx ); |
Paul Bakker | 6e339b5 | 2013-07-03 13:37:05 +0200 | [diff] [blame] | 722 | polarssl_free( ctx ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 723 | } |
| 724 | |
Paul Bakker | d1df02a | 2013-03-13 10:31:31 +0100 | [diff] [blame] | 725 | static void sha256_process_wrap( void *ctx, const unsigned char *data ) |
Paul Bakker | 1bd3ae8 | 2013-03-13 10:26:44 +0100 | [diff] [blame] | 726 | { |
Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 727 | sha256_process( (sha256_context *) ctx, data ); |
Paul Bakker | 1bd3ae8 | 2013-03-13 10:26:44 +0100 | [diff] [blame] | 728 | } |
| 729 | |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 730 | const md_info_t sha256_info = { |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 731 | POLARSSL_MD_SHA256, |
| 732 | "SHA256", |
| 733 | 32, |
Manuel Pégourié-Gonnard | 8379a82 | 2015-03-24 16:48:22 +0100 | [diff] [blame^] | 734 | 64, |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 735 | sha256_starts_wrap, |
| 736 | sha256_update_wrap, |
| 737 | sha256_finish_wrap, |
| 738 | sha256_wrap, |
| 739 | sha256_file_wrap, |
| 740 | sha256_hmac_starts_wrap, |
| 741 | sha256_hmac_update_wrap, |
| 742 | sha256_hmac_finish_wrap, |
| 743 | sha256_hmac_reset_wrap, |
| 744 | sha256_hmac_wrap, |
| 745 | sha256_ctx_alloc, |
| 746 | sha256_ctx_free, |
Paul Bakker | 1bd3ae8 | 2013-03-13 10:26:44 +0100 | [diff] [blame] | 747 | sha256_process_wrap, |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 748 | }; |
| 749 | |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 750 | #endif /* POLARSSL_SHA256_C */ |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 751 | |
Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 752 | #if defined(POLARSSL_SHA512_C) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 753 | |
Paul Bakker | d1df02a | 2013-03-13 10:31:31 +0100 | [diff] [blame] | 754 | static void sha384_starts_wrap( void *ctx ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 755 | { |
Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 756 | sha512_starts( (sha512_context *) ctx, 1 ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 757 | } |
| 758 | |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 759 | static void sha384_update_wrap( void *ctx, const unsigned char *input, |
| 760 | size_t ilen ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 761 | { |
Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 762 | sha512_update( (sha512_context *) ctx, input, ilen ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 763 | } |
| 764 | |
Paul Bakker | d1df02a | 2013-03-13 10:31:31 +0100 | [diff] [blame] | 765 | static void sha384_finish_wrap( void *ctx, unsigned char *output ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 766 | { |
Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 767 | sha512_finish( (sha512_context *) ctx, output ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 768 | } |
| 769 | |
Paul Bakker | d1df02a | 2013-03-13 10:31:31 +0100 | [diff] [blame] | 770 | static void sha384_wrap( const unsigned char *input, size_t ilen, |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 771 | unsigned char *output ) |
| 772 | { |
Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 773 | sha512( input, ilen, output, 1 ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 774 | } |
| 775 | |
Paul Bakker | d1df02a | 2013-03-13 10:31:31 +0100 | [diff] [blame] | 776 | static int sha384_file_wrap( const char *path, unsigned char *output ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 777 | { |
Paul Bakker | 335db3f | 2011-04-25 15:28:35 +0000 | [diff] [blame] | 778 | #if defined(POLARSSL_FS_IO) |
Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 779 | return sha512_file( path, output, 1 ); |
Paul Bakker | 335db3f | 2011-04-25 15:28:35 +0000 | [diff] [blame] | 780 | #else |
| 781 | ((void) path); |
| 782 | ((void) output); |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 783 | return( POLARSSL_ERR_MD_FEATURE_UNAVAILABLE ); |
Paul Bakker | 335db3f | 2011-04-25 15:28:35 +0000 | [diff] [blame] | 784 | #endif |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 785 | } |
| 786 | |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 787 | static void sha384_hmac_starts_wrap( void *ctx, const unsigned char *key, |
| 788 | size_t keylen ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 789 | { |
Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 790 | sha512_hmac_starts( (sha512_context *) ctx, key, keylen, 1 ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 791 | } |
| 792 | |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 793 | static void sha384_hmac_update_wrap( void *ctx, const unsigned char *input, |
| 794 | size_t ilen ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 795 | { |
Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 796 | sha512_hmac_update( (sha512_context *) ctx, input, ilen ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 797 | } |
| 798 | |
Paul Bakker | d1df02a | 2013-03-13 10:31:31 +0100 | [diff] [blame] | 799 | static void sha384_hmac_finish_wrap( void *ctx, unsigned char *output ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 800 | { |
Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 801 | sha512_hmac_finish( (sha512_context *) ctx, output ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 802 | } |
| 803 | |
Paul Bakker | d1df02a | 2013-03-13 10:31:31 +0100 | [diff] [blame] | 804 | static void sha384_hmac_reset_wrap( void *ctx ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 805 | { |
Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 806 | sha512_hmac_reset( (sha512_context *) ctx ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 807 | } |
| 808 | |
Paul Bakker | d1df02a | 2013-03-13 10:31:31 +0100 | [diff] [blame] | 809 | static void sha384_hmac_wrap( const unsigned char *key, size_t keylen, |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 810 | const unsigned char *input, size_t ilen, |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 811 | unsigned char *output ) |
| 812 | { |
Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 813 | sha512_hmac( key, keylen, input, ilen, output, 1 ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 814 | } |
| 815 | |
Paul Bakker | d1df02a | 2013-03-13 10:31:31 +0100 | [diff] [blame] | 816 | static void * sha384_ctx_alloc( void ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 817 | { |
Paul Bakker | 6e339b5 | 2013-07-03 13:37:05 +0200 | [diff] [blame] | 818 | return polarssl_malloc( sizeof( sha512_context ) ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 819 | } |
| 820 | |
Paul Bakker | d1df02a | 2013-03-13 10:31:31 +0100 | [diff] [blame] | 821 | static void sha384_ctx_free( void *ctx ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 822 | { |
Paul Bakker | 3461772 | 2014-06-13 17:20:13 +0200 | [diff] [blame] | 823 | polarssl_zeroize( ctx, sizeof( sha512_context ) ); |
Paul Bakker | 6e339b5 | 2013-07-03 13:37:05 +0200 | [diff] [blame] | 824 | polarssl_free( ctx ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 825 | } |
| 826 | |
Paul Bakker | d1df02a | 2013-03-13 10:31:31 +0100 | [diff] [blame] | 827 | static void sha384_process_wrap( void *ctx, const unsigned char *data ) |
Paul Bakker | 1bd3ae8 | 2013-03-13 10:26:44 +0100 | [diff] [blame] | 828 | { |
Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 829 | sha512_process( (sha512_context *) ctx, data ); |
Paul Bakker | 1bd3ae8 | 2013-03-13 10:26:44 +0100 | [diff] [blame] | 830 | } |
| 831 | |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 832 | const md_info_t sha384_info = { |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 833 | POLARSSL_MD_SHA384, |
| 834 | "SHA384", |
| 835 | 48, |
Manuel Pégourié-Gonnard | 8379a82 | 2015-03-24 16:48:22 +0100 | [diff] [blame^] | 836 | 128, |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 837 | sha384_starts_wrap, |
| 838 | sha384_update_wrap, |
| 839 | sha384_finish_wrap, |
| 840 | sha384_wrap, |
| 841 | sha384_file_wrap, |
| 842 | sha384_hmac_starts_wrap, |
| 843 | sha384_hmac_update_wrap, |
| 844 | sha384_hmac_finish_wrap, |
| 845 | sha384_hmac_reset_wrap, |
| 846 | sha384_hmac_wrap, |
| 847 | sha384_ctx_alloc, |
| 848 | sha384_ctx_free, |
Paul Bakker | 1bd3ae8 | 2013-03-13 10:26:44 +0100 | [diff] [blame] | 849 | sha384_process_wrap, |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 850 | }; |
| 851 | |
Paul Bakker | d1df02a | 2013-03-13 10:31:31 +0100 | [diff] [blame] | 852 | static void sha512_starts_wrap( void *ctx ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 853 | { |
Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 854 | sha512_starts( (sha512_context *) ctx, 0 ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 855 | } |
| 856 | |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 857 | static void sha512_update_wrap( void *ctx, const unsigned char *input, |
| 858 | size_t ilen ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 859 | { |
Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 860 | sha512_update( (sha512_context *) ctx, input, ilen ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 861 | } |
| 862 | |
Paul Bakker | d1df02a | 2013-03-13 10:31:31 +0100 | [diff] [blame] | 863 | static void sha512_finish_wrap( void *ctx, unsigned char *output ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 864 | { |
Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 865 | sha512_finish( (sha512_context *) ctx, output ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 866 | } |
| 867 | |
Paul Bakker | d1df02a | 2013-03-13 10:31:31 +0100 | [diff] [blame] | 868 | static void sha512_wrap( const unsigned char *input, size_t ilen, |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 869 | unsigned char *output ) |
| 870 | { |
Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 871 | sha512( input, ilen, output, 0 ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 872 | } |
| 873 | |
Paul Bakker | d1df02a | 2013-03-13 10:31:31 +0100 | [diff] [blame] | 874 | static int sha512_file_wrap( const char *path, unsigned char *output ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 875 | { |
Paul Bakker | 335db3f | 2011-04-25 15:28:35 +0000 | [diff] [blame] | 876 | #if defined(POLARSSL_FS_IO) |
Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 877 | return sha512_file( path, output, 0 ); |
Paul Bakker | 335db3f | 2011-04-25 15:28:35 +0000 | [diff] [blame] | 878 | #else |
| 879 | ((void) path); |
| 880 | ((void) output); |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 881 | return( POLARSSL_ERR_MD_FEATURE_UNAVAILABLE ); |
Paul Bakker | 335db3f | 2011-04-25 15:28:35 +0000 | [diff] [blame] | 882 | #endif |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 883 | } |
| 884 | |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 885 | static void sha512_hmac_starts_wrap( void *ctx, const unsigned char *key, |
| 886 | size_t keylen ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 887 | { |
Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 888 | sha512_hmac_starts( (sha512_context *) ctx, key, keylen, 0 ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 889 | } |
| 890 | |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 891 | static void sha512_hmac_update_wrap( void *ctx, const unsigned char *input, |
| 892 | size_t ilen ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 893 | { |
Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 894 | sha512_hmac_update( (sha512_context *) ctx, input, ilen ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 895 | } |
| 896 | |
Paul Bakker | d1df02a | 2013-03-13 10:31:31 +0100 | [diff] [blame] | 897 | static void sha512_hmac_finish_wrap( void *ctx, unsigned char *output ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 898 | { |
Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 899 | sha512_hmac_finish( (sha512_context *) ctx, output ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 900 | } |
| 901 | |
Paul Bakker | d1df02a | 2013-03-13 10:31:31 +0100 | [diff] [blame] | 902 | static void sha512_hmac_reset_wrap( void *ctx ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 903 | { |
Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 904 | sha512_hmac_reset( (sha512_context *) ctx ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 905 | } |
| 906 | |
Paul Bakker | d1df02a | 2013-03-13 10:31:31 +0100 | [diff] [blame] | 907 | static void sha512_hmac_wrap( const unsigned char *key, size_t keylen, |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 908 | const unsigned char *input, size_t ilen, |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 909 | unsigned char *output ) |
| 910 | { |
Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 911 | sha512_hmac( key, keylen, input, ilen, output, 0 ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 912 | } |
| 913 | |
Paul Bakker | d1df02a | 2013-03-13 10:31:31 +0100 | [diff] [blame] | 914 | static void * sha512_ctx_alloc( void ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 915 | { |
Paul Bakker | 5b4af39 | 2014-06-26 12:09:34 +0200 | [diff] [blame] | 916 | sha512_context *ctx; |
Mansour Moufid | c531b4a | 2015-02-15 17:35:38 -0500 | [diff] [blame] | 917 | ctx = polarssl_malloc( sizeof( sha512_context ) ); |
Paul Bakker | 5b4af39 | 2014-06-26 12:09:34 +0200 | [diff] [blame] | 918 | |
| 919 | if( ctx == NULL ) |
| 920 | return( NULL ); |
| 921 | |
| 922 | sha512_init( ctx ); |
| 923 | |
| 924 | return( ctx ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 925 | } |
| 926 | |
Paul Bakker | d1df02a | 2013-03-13 10:31:31 +0100 | [diff] [blame] | 927 | static void sha512_ctx_free( void *ctx ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 928 | { |
Paul Bakker | 5b4af39 | 2014-06-26 12:09:34 +0200 | [diff] [blame] | 929 | sha512_free( (sha512_context *) ctx ); |
Paul Bakker | 6e339b5 | 2013-07-03 13:37:05 +0200 | [diff] [blame] | 930 | polarssl_free( ctx ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 931 | } |
| 932 | |
Paul Bakker | d1df02a | 2013-03-13 10:31:31 +0100 | [diff] [blame] | 933 | static void sha512_process_wrap( void *ctx, const unsigned char *data ) |
Paul Bakker | 1bd3ae8 | 2013-03-13 10:26:44 +0100 | [diff] [blame] | 934 | { |
Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 935 | sha512_process( (sha512_context *) ctx, data ); |
Paul Bakker | 1bd3ae8 | 2013-03-13 10:26:44 +0100 | [diff] [blame] | 936 | } |
| 937 | |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 938 | const md_info_t sha512_info = { |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 939 | POLARSSL_MD_SHA512, |
| 940 | "SHA512", |
| 941 | 64, |
Manuel Pégourié-Gonnard | 8379a82 | 2015-03-24 16:48:22 +0100 | [diff] [blame^] | 942 | 128, |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 943 | sha512_starts_wrap, |
| 944 | sha512_update_wrap, |
| 945 | sha512_finish_wrap, |
| 946 | sha512_wrap, |
| 947 | sha512_file_wrap, |
| 948 | sha512_hmac_starts_wrap, |
| 949 | sha512_hmac_update_wrap, |
| 950 | sha512_hmac_finish_wrap, |
| 951 | sha512_hmac_reset_wrap, |
| 952 | sha512_hmac_wrap, |
| 953 | sha512_ctx_alloc, |
| 954 | sha512_ctx_free, |
Paul Bakker | 1bd3ae8 | 2013-03-13 10:26:44 +0100 | [diff] [blame] | 955 | sha512_process_wrap, |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 956 | }; |
| 957 | |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 958 | #endif /* POLARSSL_SHA512_C */ |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 959 | |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 960 | #endif /* POLARSSL_MD_C */ |