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) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 28 | #include "polarssl/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 | |
| 35 | #include "polarssl/md_wrap.h" |
Paul Bakker | f654371 | 2012-03-05 14:01:29 +0000 | [diff] [blame] | 36 | |
| 37 | #if defined(POLARSSL_MD2_C) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 38 | #include "polarssl/md2.h" |
Paul Bakker | f654371 | 2012-03-05 14:01:29 +0000 | [diff] [blame] | 39 | #endif |
| 40 | |
| 41 | #if defined(POLARSSL_MD4_C) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 42 | #include "polarssl/md4.h" |
Paul Bakker | f654371 | 2012-03-05 14:01:29 +0000 | [diff] [blame] | 43 | #endif |
| 44 | |
| 45 | #if defined(POLARSSL_MD5_C) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 46 | #include "polarssl/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) |
| 50 | #include "polarssl/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) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 54 | #include "polarssl/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) |
Paul Bakker | d2681d8 | 2013-06-30 14:49:12 +0200 | [diff] [blame] | 58 | #include "polarssl/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) |
Paul Bakker | d2681d8 | 2013-06-30 14:49:12 +0200 | [diff] [blame] | 62 | #include "polarssl/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) |
| 66 | #include "polarssl/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 |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 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, |
| 151 | md2_starts_wrap, |
| 152 | md2_update_wrap, |
| 153 | md2_finish_wrap, |
| 154 | md2, |
Paul Bakker | 335db3f | 2011-04-25 15:28:35 +0000 | [diff] [blame] | 155 | md2_file_wrap, |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 156 | md2_hmac_starts_wrap, |
| 157 | md2_hmac_update_wrap, |
| 158 | md2_hmac_finish_wrap, |
| 159 | md2_hmac_reset_wrap, |
| 160 | md2_hmac, |
| 161 | md2_ctx_alloc, |
| 162 | md2_ctx_free, |
Paul Bakker | 1bd3ae8 | 2013-03-13 10:26:44 +0100 | [diff] [blame] | 163 | md2_process_wrap, |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 164 | }; |
| 165 | |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 166 | #endif /* POLARSSL_MD2_C */ |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 167 | |
| 168 | #if defined(POLARSSL_MD4_C) |
| 169 | |
Paul Bakker | d1df02a | 2013-03-13 10:31:31 +0100 | [diff] [blame] | 170 | static void md4_starts_wrap( void *ctx ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 171 | { |
| 172 | md4_starts( (md4_context *) ctx ); |
| 173 | } |
| 174 | |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 175 | static void md4_update_wrap( void *ctx, const unsigned char *input, |
| 176 | size_t ilen ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 177 | { |
| 178 | md4_update( (md4_context *) ctx, input, ilen ); |
| 179 | } |
| 180 | |
Paul Bakker | d1df02a | 2013-03-13 10:31:31 +0100 | [diff] [blame] | 181 | static void md4_finish_wrap( void *ctx, unsigned char *output ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 182 | { |
| 183 | md4_finish( (md4_context *) ctx, output ); |
Paul Bakker | 335db3f | 2011-04-25 15:28:35 +0000 | [diff] [blame] | 184 | } |
| 185 | |
Paul Bakker | d1df02a | 2013-03-13 10:31:31 +0100 | [diff] [blame] | 186 | static int md4_file_wrap( const char *path, unsigned char *output ) |
Paul Bakker | 335db3f | 2011-04-25 15:28:35 +0000 | [diff] [blame] | 187 | { |
| 188 | #if defined(POLARSSL_FS_IO) |
| 189 | return md4_file( path, output ); |
| 190 | #else |
| 191 | ((void) path); |
| 192 | ((void) output); |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 193 | return( POLARSSL_ERR_MD_FEATURE_UNAVAILABLE ); |
Paul Bakker | 335db3f | 2011-04-25 15:28:35 +0000 | [diff] [blame] | 194 | #endif |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 195 | } |
| 196 | |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 197 | static void md4_hmac_starts_wrap( void *ctx, const unsigned char *key, |
| 198 | size_t keylen ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 199 | { |
| 200 | md4_hmac_starts( (md4_context *) ctx, key, keylen ); |
| 201 | } |
| 202 | |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 203 | static void md4_hmac_update_wrap( void *ctx, const unsigned char *input, |
| 204 | size_t ilen ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 205 | { |
| 206 | md4_hmac_update( (md4_context *) ctx, input, ilen ); |
| 207 | } |
| 208 | |
Paul Bakker | d1df02a | 2013-03-13 10:31:31 +0100 | [diff] [blame] | 209 | static void md4_hmac_finish_wrap( void *ctx, unsigned char *output ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 210 | { |
| 211 | md4_hmac_finish( (md4_context *) ctx, output ); |
| 212 | } |
| 213 | |
Paul Bakker | d1df02a | 2013-03-13 10:31:31 +0100 | [diff] [blame] | 214 | static void md4_hmac_reset_wrap( void *ctx ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 215 | { |
| 216 | md4_hmac_reset( (md4_context *) ctx ); |
| 217 | } |
| 218 | |
Paul Bakker | d1df02a | 2013-03-13 10:31:31 +0100 | [diff] [blame] | 219 | static void *md4_ctx_alloc( void ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 220 | { |
Paul Bakker | 6e339b5 | 2013-07-03 13:37:05 +0200 | [diff] [blame] | 221 | return polarssl_malloc( sizeof( md4_context ) ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 222 | } |
| 223 | |
Paul Bakker | d1df02a | 2013-03-13 10:31:31 +0100 | [diff] [blame] | 224 | static void md4_ctx_free( void *ctx ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 225 | { |
Paul Bakker | 3461772 | 2014-06-13 17:20:13 +0200 | [diff] [blame] | 226 | polarssl_zeroize( ctx, sizeof( md4_context ) ); |
Paul Bakker | 6e339b5 | 2013-07-03 13:37:05 +0200 | [diff] [blame] | 227 | polarssl_free( ctx ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 228 | } |
| 229 | |
Paul Bakker | d1df02a | 2013-03-13 10:31:31 +0100 | [diff] [blame] | 230 | static void md4_process_wrap( void *ctx, const unsigned char *data ) |
Paul Bakker | 1bd3ae8 | 2013-03-13 10:26:44 +0100 | [diff] [blame] | 231 | { |
| 232 | md4_process( (md4_context *) ctx, data ); |
| 233 | } |
| 234 | |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 235 | const md_info_t md4_info = { |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 236 | POLARSSL_MD_MD4, |
| 237 | "MD4", |
| 238 | 16, |
| 239 | md4_starts_wrap, |
| 240 | md4_update_wrap, |
| 241 | md4_finish_wrap, |
| 242 | md4, |
Paul Bakker | 335db3f | 2011-04-25 15:28:35 +0000 | [diff] [blame] | 243 | md4_file_wrap, |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 244 | md4_hmac_starts_wrap, |
| 245 | md4_hmac_update_wrap, |
| 246 | md4_hmac_finish_wrap, |
| 247 | md4_hmac_reset_wrap, |
| 248 | md4_hmac, |
| 249 | md4_ctx_alloc, |
| 250 | md4_ctx_free, |
Paul Bakker | 1bd3ae8 | 2013-03-13 10:26:44 +0100 | [diff] [blame] | 251 | md4_process_wrap, |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 252 | }; |
| 253 | |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 254 | #endif /* POLARSSL_MD4_C */ |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 255 | |
| 256 | #if defined(POLARSSL_MD5_C) |
| 257 | |
| 258 | static void md5_starts_wrap( void *ctx ) |
| 259 | { |
| 260 | md5_starts( (md5_context *) ctx ); |
| 261 | } |
| 262 | |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 263 | static void md5_update_wrap( void *ctx, const unsigned char *input, |
| 264 | size_t ilen ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 265 | { |
| 266 | md5_update( (md5_context *) ctx, input, ilen ); |
| 267 | } |
| 268 | |
| 269 | static void md5_finish_wrap( void *ctx, unsigned char *output ) |
| 270 | { |
| 271 | md5_finish( (md5_context *) ctx, output ); |
Paul Bakker | 335db3f | 2011-04-25 15:28:35 +0000 | [diff] [blame] | 272 | } |
| 273 | |
Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 274 | static int md5_file_wrap( const char *path, unsigned char *output ) |
Paul Bakker | 335db3f | 2011-04-25 15:28:35 +0000 | [diff] [blame] | 275 | { |
| 276 | #if defined(POLARSSL_FS_IO) |
| 277 | return md5_file( path, output ); |
| 278 | #else |
| 279 | ((void) path); |
| 280 | ((void) output); |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 281 | return( POLARSSL_ERR_MD_FEATURE_UNAVAILABLE ); |
Paul Bakker | 335db3f | 2011-04-25 15:28:35 +0000 | [diff] [blame] | 282 | #endif |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 283 | } |
| 284 | |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 285 | static void md5_hmac_starts_wrap( void *ctx, const unsigned char *key, |
| 286 | size_t keylen ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 287 | { |
| 288 | md5_hmac_starts( (md5_context *) ctx, key, keylen ); |
| 289 | } |
| 290 | |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 291 | static void md5_hmac_update_wrap( void *ctx, const unsigned char *input, |
| 292 | size_t ilen ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 293 | { |
| 294 | md5_hmac_update( (md5_context *) ctx, input, ilen ); |
| 295 | } |
| 296 | |
| 297 | static void md5_hmac_finish_wrap( void *ctx, unsigned char *output ) |
| 298 | { |
| 299 | md5_hmac_finish( (md5_context *) ctx, output ); |
| 300 | } |
| 301 | |
| 302 | static void md5_hmac_reset_wrap( void *ctx ) |
| 303 | { |
| 304 | md5_hmac_reset( (md5_context *) ctx ); |
| 305 | } |
| 306 | |
| 307 | static void * md5_ctx_alloc( void ) |
| 308 | { |
Paul Bakker | 6e339b5 | 2013-07-03 13:37:05 +0200 | [diff] [blame] | 309 | return polarssl_malloc( sizeof( md5_context ) ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | static void md5_ctx_free( void *ctx ) |
| 313 | { |
Paul Bakker | 3461772 | 2014-06-13 17:20:13 +0200 | [diff] [blame] | 314 | polarssl_zeroize( ctx, sizeof( md5_context ) ); |
Paul Bakker | 6e339b5 | 2013-07-03 13:37:05 +0200 | [diff] [blame] | 315 | polarssl_free( ctx ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 316 | } |
| 317 | |
Paul Bakker | 1bd3ae8 | 2013-03-13 10:26:44 +0100 | [diff] [blame] | 318 | static void md5_process_wrap( void *ctx, const unsigned char *data ) |
| 319 | { |
| 320 | md5_process( (md5_context *) ctx, data ); |
| 321 | } |
| 322 | |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 323 | const md_info_t md5_info = { |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 324 | POLARSSL_MD_MD5, |
| 325 | "MD5", |
| 326 | 16, |
| 327 | md5_starts_wrap, |
| 328 | md5_update_wrap, |
| 329 | md5_finish_wrap, |
| 330 | md5, |
Paul Bakker | 335db3f | 2011-04-25 15:28:35 +0000 | [diff] [blame] | 331 | md5_file_wrap, |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 332 | md5_hmac_starts_wrap, |
| 333 | md5_hmac_update_wrap, |
| 334 | md5_hmac_finish_wrap, |
| 335 | md5_hmac_reset_wrap, |
| 336 | md5_hmac, |
| 337 | md5_ctx_alloc, |
| 338 | md5_ctx_free, |
Paul Bakker | 1bd3ae8 | 2013-03-13 10:26:44 +0100 | [diff] [blame] | 339 | md5_process_wrap, |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 340 | }; |
| 341 | |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 342 | #endif /* POLARSSL_MD5_C */ |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 343 | |
Paul Bakker | 61b699e | 2014-01-22 13:35:29 +0100 | [diff] [blame] | 344 | #if defined(POLARSSL_RIPEMD160_C) |
Manuel Pégourié-Gonnard | e4d47a6 | 2014-01-17 20:41:32 +0100 | [diff] [blame] | 345 | |
Paul Bakker | 61b699e | 2014-01-22 13:35:29 +0100 | [diff] [blame] | 346 | static void ripemd160_starts_wrap( void *ctx ) |
Manuel Pégourié-Gonnard | e4d47a6 | 2014-01-17 20:41:32 +0100 | [diff] [blame] | 347 | { |
Paul Bakker | 61b699e | 2014-01-22 13:35:29 +0100 | [diff] [blame] | 348 | ripemd160_starts( (ripemd160_context *) ctx ); |
Manuel Pégourié-Gonnard | e4d47a6 | 2014-01-17 20:41:32 +0100 | [diff] [blame] | 349 | } |
| 350 | |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 351 | static void ripemd160_update_wrap( void *ctx, const unsigned char *input, |
| 352 | size_t ilen ) |
Manuel Pégourié-Gonnard | e4d47a6 | 2014-01-17 20:41:32 +0100 | [diff] [blame] | 353 | { |
Paul Bakker | 61b699e | 2014-01-22 13:35:29 +0100 | [diff] [blame] | 354 | ripemd160_update( (ripemd160_context *) ctx, input, ilen ); |
Manuel Pégourié-Gonnard | e4d47a6 | 2014-01-17 20:41:32 +0100 | [diff] [blame] | 355 | } |
| 356 | |
Paul Bakker | 61b699e | 2014-01-22 13:35:29 +0100 | [diff] [blame] | 357 | static void ripemd160_finish_wrap( void *ctx, unsigned char *output ) |
Manuel Pégourié-Gonnard | e4d47a6 | 2014-01-17 20:41:32 +0100 | [diff] [blame] | 358 | { |
Paul Bakker | 61b699e | 2014-01-22 13:35:29 +0100 | [diff] [blame] | 359 | ripemd160_finish( (ripemd160_context *) ctx, output ); |
Manuel Pégourié-Gonnard | e4d47a6 | 2014-01-17 20:41:32 +0100 | [diff] [blame] | 360 | } |
| 361 | |
Paul Bakker | 61b699e | 2014-01-22 13:35:29 +0100 | [diff] [blame] | 362 | 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] | 363 | { |
| 364 | #if defined(POLARSSL_FS_IO) |
Paul Bakker | 61b699e | 2014-01-22 13:35:29 +0100 | [diff] [blame] | 365 | return ripemd160_file( path, output ); |
Manuel Pégourié-Gonnard | e4d47a6 | 2014-01-17 20:41:32 +0100 | [diff] [blame] | 366 | #else |
| 367 | ((void) path); |
| 368 | ((void) output); |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 369 | return( POLARSSL_ERR_MD_FEATURE_UNAVAILABLE ); |
Manuel Pégourié-Gonnard | e4d47a6 | 2014-01-17 20:41:32 +0100 | [diff] [blame] | 370 | #endif |
| 371 | } |
| 372 | |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 373 | static void ripemd160_hmac_starts_wrap( void *ctx, const unsigned char *key, |
| 374 | size_t keylen ) |
Manuel Pégourié-Gonnard | e4d47a6 | 2014-01-17 20:41:32 +0100 | [diff] [blame] | 375 | { |
Paul Bakker | 61b699e | 2014-01-22 13:35:29 +0100 | [diff] [blame] | 376 | ripemd160_hmac_starts( (ripemd160_context *) ctx, key, keylen ); |
Manuel Pégourié-Gonnard | e4d47a6 | 2014-01-17 20:41:32 +0100 | [diff] [blame] | 377 | } |
| 378 | |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 379 | static void ripemd160_hmac_update_wrap( void *ctx, const unsigned char *input, |
| 380 | size_t ilen ) |
Manuel Pégourié-Gonnard | e4d47a6 | 2014-01-17 20:41:32 +0100 | [diff] [blame] | 381 | { |
Paul Bakker | 61b699e | 2014-01-22 13:35:29 +0100 | [diff] [blame] | 382 | ripemd160_hmac_update( (ripemd160_context *) ctx, input, ilen ); |
Manuel Pégourié-Gonnard | e4d47a6 | 2014-01-17 20:41:32 +0100 | [diff] [blame] | 383 | } |
| 384 | |
Paul Bakker | 61b699e | 2014-01-22 13:35:29 +0100 | [diff] [blame] | 385 | 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] | 386 | { |
Paul Bakker | 61b699e | 2014-01-22 13:35:29 +0100 | [diff] [blame] | 387 | ripemd160_hmac_finish( (ripemd160_context *) ctx, output ); |
Manuel Pégourié-Gonnard | e4d47a6 | 2014-01-17 20:41:32 +0100 | [diff] [blame] | 388 | } |
| 389 | |
Paul Bakker | 61b699e | 2014-01-22 13:35:29 +0100 | [diff] [blame] | 390 | static void ripemd160_hmac_reset_wrap( void *ctx ) |
Manuel Pégourié-Gonnard | e4d47a6 | 2014-01-17 20:41:32 +0100 | [diff] [blame] | 391 | { |
Paul Bakker | 61b699e | 2014-01-22 13:35:29 +0100 | [diff] [blame] | 392 | ripemd160_hmac_reset( (ripemd160_context *) ctx ); |
Manuel Pégourié-Gonnard | e4d47a6 | 2014-01-17 20:41:32 +0100 | [diff] [blame] | 393 | } |
| 394 | |
Paul Bakker | 61b699e | 2014-01-22 13:35:29 +0100 | [diff] [blame] | 395 | static void * ripemd160_ctx_alloc( void ) |
Manuel Pégourié-Gonnard | e4d47a6 | 2014-01-17 20:41:32 +0100 | [diff] [blame] | 396 | { |
Paul Bakker | 5b4af39 | 2014-06-26 12:09:34 +0200 | [diff] [blame] | 397 | ripemd160_context *ctx; |
Mansour Moufid | c531b4a | 2015-02-15 17:35:38 -0500 | [diff] [blame] | 398 | ctx = polarssl_malloc( sizeof( ripemd160_context ) ); |
Paul Bakker | 5b4af39 | 2014-06-26 12:09:34 +0200 | [diff] [blame] | 399 | |
| 400 | if( ctx == NULL ) |
| 401 | return( NULL ); |
| 402 | |
| 403 | ripemd160_init( ctx ); |
| 404 | |
| 405 | return( ctx ); |
Manuel Pégourié-Gonnard | e4d47a6 | 2014-01-17 20:41:32 +0100 | [diff] [blame] | 406 | } |
| 407 | |
Paul Bakker | 61b699e | 2014-01-22 13:35:29 +0100 | [diff] [blame] | 408 | static void ripemd160_ctx_free( void *ctx ) |
Manuel Pégourié-Gonnard | e4d47a6 | 2014-01-17 20:41:32 +0100 | [diff] [blame] | 409 | { |
Paul Bakker | 5b4af39 | 2014-06-26 12:09:34 +0200 | [diff] [blame] | 410 | ripemd160_free( (ripemd160_context *) ctx ); |
Manuel Pégourié-Gonnard | e4d47a6 | 2014-01-17 20:41:32 +0100 | [diff] [blame] | 411 | polarssl_free( ctx ); |
| 412 | } |
| 413 | |
Paul Bakker | 61b699e | 2014-01-22 13:35:29 +0100 | [diff] [blame] | 414 | 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] | 415 | { |
Paul Bakker | 61b699e | 2014-01-22 13:35:29 +0100 | [diff] [blame] | 416 | ripemd160_process( (ripemd160_context *) ctx, data ); |
Manuel Pégourié-Gonnard | e4d47a6 | 2014-01-17 20:41:32 +0100 | [diff] [blame] | 417 | } |
| 418 | |
Paul Bakker | 61b699e | 2014-01-22 13:35:29 +0100 | [diff] [blame] | 419 | const md_info_t ripemd160_info = { |
| 420 | POLARSSL_MD_RIPEMD160, |
| 421 | "RIPEMD160", |
Manuel Pégourié-Gonnard | e4d47a6 | 2014-01-17 20:41:32 +0100 | [diff] [blame] | 422 | 20, |
Paul Bakker | 61b699e | 2014-01-22 13:35:29 +0100 | [diff] [blame] | 423 | ripemd160_starts_wrap, |
| 424 | ripemd160_update_wrap, |
| 425 | ripemd160_finish_wrap, |
| 426 | ripemd160, |
| 427 | ripemd160_file_wrap, |
| 428 | ripemd160_hmac_starts_wrap, |
| 429 | ripemd160_hmac_update_wrap, |
| 430 | ripemd160_hmac_finish_wrap, |
| 431 | ripemd160_hmac_reset_wrap, |
| 432 | ripemd160_hmac, |
| 433 | ripemd160_ctx_alloc, |
| 434 | ripemd160_ctx_free, |
| 435 | ripemd160_process_wrap, |
Manuel Pégourié-Gonnard | e4d47a6 | 2014-01-17 20:41:32 +0100 | [diff] [blame] | 436 | }; |
| 437 | |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 438 | #endif /* POLARSSL_RIPEMD160_C */ |
Manuel Pégourié-Gonnard | e4d47a6 | 2014-01-17 20:41:32 +0100 | [diff] [blame] | 439 | |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 440 | #if defined(POLARSSL_SHA1_C) |
| 441 | |
Paul Bakker | d1df02a | 2013-03-13 10:31:31 +0100 | [diff] [blame] | 442 | static void sha1_starts_wrap( void *ctx ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 443 | { |
| 444 | sha1_starts( (sha1_context *) ctx ); |
| 445 | } |
| 446 | |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 447 | static void sha1_update_wrap( void *ctx, const unsigned char *input, |
| 448 | size_t ilen ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 449 | { |
| 450 | sha1_update( (sha1_context *) ctx, input, ilen ); |
| 451 | } |
| 452 | |
Paul Bakker | d1df02a | 2013-03-13 10:31:31 +0100 | [diff] [blame] | 453 | static void sha1_finish_wrap( void *ctx, unsigned char *output ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 454 | { |
| 455 | sha1_finish( (sha1_context *) ctx, output ); |
Paul Bakker | 335db3f | 2011-04-25 15:28:35 +0000 | [diff] [blame] | 456 | } |
| 457 | |
Paul Bakker | d1df02a | 2013-03-13 10:31:31 +0100 | [diff] [blame] | 458 | static int sha1_file_wrap( const char *path, unsigned char *output ) |
Paul Bakker | 335db3f | 2011-04-25 15:28:35 +0000 | [diff] [blame] | 459 | { |
| 460 | #if defined(POLARSSL_FS_IO) |
| 461 | return sha1_file( path, output ); |
| 462 | #else |
| 463 | ((void) path); |
| 464 | ((void) output); |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 465 | return( POLARSSL_ERR_MD_FEATURE_UNAVAILABLE ); |
Paul Bakker | 335db3f | 2011-04-25 15:28:35 +0000 | [diff] [blame] | 466 | #endif |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 467 | } |
| 468 | |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 469 | static void sha1_hmac_starts_wrap( void *ctx, const unsigned char *key, |
| 470 | size_t keylen ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 471 | { |
| 472 | sha1_hmac_starts( (sha1_context *) ctx, key, keylen ); |
| 473 | } |
| 474 | |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 475 | static void sha1_hmac_update_wrap( void *ctx, const unsigned char *input, |
| 476 | size_t ilen ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 477 | { |
| 478 | sha1_hmac_update( (sha1_context *) ctx, input, ilen ); |
| 479 | } |
| 480 | |
Paul Bakker | d1df02a | 2013-03-13 10:31:31 +0100 | [diff] [blame] | 481 | static void sha1_hmac_finish_wrap( void *ctx, unsigned char *output ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 482 | { |
| 483 | sha1_hmac_finish( (sha1_context *) ctx, output ); |
| 484 | } |
| 485 | |
Paul Bakker | d1df02a | 2013-03-13 10:31:31 +0100 | [diff] [blame] | 486 | static void sha1_hmac_reset_wrap( void *ctx ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 487 | { |
| 488 | sha1_hmac_reset( (sha1_context *) ctx ); |
| 489 | } |
| 490 | |
Paul Bakker | d1df02a | 2013-03-13 10:31:31 +0100 | [diff] [blame] | 491 | static void * sha1_ctx_alloc( void ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 492 | { |
Paul Bakker | 5b4af39 | 2014-06-26 12:09:34 +0200 | [diff] [blame] | 493 | sha1_context *ctx; |
Mansour Moufid | c531b4a | 2015-02-15 17:35:38 -0500 | [diff] [blame] | 494 | ctx = polarssl_malloc( sizeof( sha1_context ) ); |
Paul Bakker | 5b4af39 | 2014-06-26 12:09:34 +0200 | [diff] [blame] | 495 | |
| 496 | if( ctx == NULL ) |
| 497 | return( NULL ); |
| 498 | |
| 499 | sha1_init( ctx ); |
| 500 | |
| 501 | return( ctx ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 502 | } |
| 503 | |
Paul Bakker | d1df02a | 2013-03-13 10:31:31 +0100 | [diff] [blame] | 504 | static void sha1_ctx_free( void *ctx ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 505 | { |
Paul Bakker | 5b4af39 | 2014-06-26 12:09:34 +0200 | [diff] [blame] | 506 | sha1_free( (sha1_context *) ctx ); |
Paul Bakker | 6e339b5 | 2013-07-03 13:37:05 +0200 | [diff] [blame] | 507 | polarssl_free( ctx ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 508 | } |
| 509 | |
Paul Bakker | d1df02a | 2013-03-13 10:31:31 +0100 | [diff] [blame] | 510 | static void sha1_process_wrap( void *ctx, const unsigned char *data ) |
Paul Bakker | 1bd3ae8 | 2013-03-13 10:26:44 +0100 | [diff] [blame] | 511 | { |
| 512 | sha1_process( (sha1_context *) ctx, data ); |
| 513 | } |
| 514 | |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 515 | const md_info_t sha1_info = { |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 516 | POLARSSL_MD_SHA1, |
| 517 | "SHA1", |
| 518 | 20, |
| 519 | sha1_starts_wrap, |
| 520 | sha1_update_wrap, |
| 521 | sha1_finish_wrap, |
| 522 | sha1, |
Paul Bakker | 335db3f | 2011-04-25 15:28:35 +0000 | [diff] [blame] | 523 | sha1_file_wrap, |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 524 | sha1_hmac_starts_wrap, |
| 525 | sha1_hmac_update_wrap, |
| 526 | sha1_hmac_finish_wrap, |
| 527 | sha1_hmac_reset_wrap, |
| 528 | sha1_hmac, |
| 529 | sha1_ctx_alloc, |
| 530 | sha1_ctx_free, |
Paul Bakker | 1bd3ae8 | 2013-03-13 10:26:44 +0100 | [diff] [blame] | 531 | sha1_process_wrap, |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 532 | }; |
| 533 | |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 534 | #endif /* POLARSSL_SHA1_C */ |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 535 | |
| 536 | /* |
| 537 | * Wrappers for generic message digests |
| 538 | */ |
Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 539 | #if defined(POLARSSL_SHA256_C) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 540 | |
Paul Bakker | d1df02a | 2013-03-13 10:31:31 +0100 | [diff] [blame] | 541 | static void sha224_starts_wrap( void *ctx ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 542 | { |
Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 543 | sha256_starts( (sha256_context *) ctx, 1 ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 544 | } |
| 545 | |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 546 | static void sha224_update_wrap( void *ctx, const unsigned char *input, |
| 547 | size_t ilen ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 548 | { |
Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 549 | sha256_update( (sha256_context *) ctx, input, ilen ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 550 | } |
| 551 | |
Paul Bakker | d1df02a | 2013-03-13 10:31:31 +0100 | [diff] [blame] | 552 | static void sha224_finish_wrap( void *ctx, unsigned char *output ) |
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_finish( (sha256_context *) ctx, output ); |
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_wrap( const unsigned char *input, size_t ilen, |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 558 | unsigned char *output ) |
| 559 | { |
Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 560 | sha256( input, ilen, output, 1 ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 561 | } |
| 562 | |
Paul Bakker | d1df02a | 2013-03-13 10:31:31 +0100 | [diff] [blame] | 563 | static int sha224_file_wrap( const char *path, unsigned char *output ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 564 | { |
Paul Bakker | 335db3f | 2011-04-25 15:28:35 +0000 | [diff] [blame] | 565 | #if defined(POLARSSL_FS_IO) |
Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 566 | return sha256_file( path, output, 1 ); |
Paul Bakker | 335db3f | 2011-04-25 15:28:35 +0000 | [diff] [blame] | 567 | #else |
| 568 | ((void) path); |
| 569 | ((void) output); |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 570 | return( POLARSSL_ERR_MD_FEATURE_UNAVAILABLE ); |
Paul Bakker | 335db3f | 2011-04-25 15:28:35 +0000 | [diff] [blame] | 571 | #endif |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 572 | } |
| 573 | |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 574 | static void sha224_hmac_starts_wrap( void *ctx, const unsigned char *key, |
| 575 | size_t keylen ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 576 | { |
Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 577 | sha256_hmac_starts( (sha256_context *) ctx, key, keylen, 1 ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 578 | } |
| 579 | |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 580 | static void sha224_hmac_update_wrap( void *ctx, const unsigned char *input, |
| 581 | size_t ilen ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 582 | { |
Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 583 | sha256_hmac_update( (sha256_context *) ctx, input, ilen ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 584 | } |
| 585 | |
Paul Bakker | d1df02a | 2013-03-13 10:31:31 +0100 | [diff] [blame] | 586 | static void sha224_hmac_finish_wrap( void *ctx, unsigned char *output ) |
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_finish( (sha256_context *) ctx, output ); |
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_reset_wrap( void *ctx ) |
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_reset( (sha256_context *) ctx ); |
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_wrap( const unsigned char *key, size_t keylen, |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 597 | const unsigned char *input, size_t ilen, |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 598 | unsigned char *output ) |
| 599 | { |
Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 600 | sha256_hmac( key, keylen, input, ilen, output, 1 ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 601 | } |
| 602 | |
Paul Bakker | d1df02a | 2013-03-13 10:31:31 +0100 | [diff] [blame] | 603 | static void * sha224_ctx_alloc( void ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 604 | { |
Paul Bakker | 6e339b5 | 2013-07-03 13:37:05 +0200 | [diff] [blame] | 605 | return polarssl_malloc( sizeof( sha256_context ) ); |
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_free( void *ctx ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 609 | { |
Paul Bakker | 3461772 | 2014-06-13 17:20:13 +0200 | [diff] [blame] | 610 | polarssl_zeroize( ctx, sizeof( sha256_context ) ); |
Paul Bakker | 6e339b5 | 2013-07-03 13:37:05 +0200 | [diff] [blame] | 611 | polarssl_free( ctx ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 612 | } |
| 613 | |
Paul Bakker | d1df02a | 2013-03-13 10:31:31 +0100 | [diff] [blame] | 614 | static void sha224_process_wrap( void *ctx, const unsigned char *data ) |
Paul Bakker | 1bd3ae8 | 2013-03-13 10:26:44 +0100 | [diff] [blame] | 615 | { |
Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 616 | sha256_process( (sha256_context *) ctx, data ); |
Paul Bakker | 1bd3ae8 | 2013-03-13 10:26:44 +0100 | [diff] [blame] | 617 | } |
| 618 | |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 619 | const md_info_t sha224_info = { |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 620 | POLARSSL_MD_SHA224, |
| 621 | "SHA224", |
| 622 | 28, |
| 623 | sha224_starts_wrap, |
| 624 | sha224_update_wrap, |
| 625 | sha224_finish_wrap, |
| 626 | sha224_wrap, |
| 627 | sha224_file_wrap, |
| 628 | sha224_hmac_starts_wrap, |
| 629 | sha224_hmac_update_wrap, |
| 630 | sha224_hmac_finish_wrap, |
| 631 | sha224_hmac_reset_wrap, |
| 632 | sha224_hmac_wrap, |
| 633 | sha224_ctx_alloc, |
| 634 | sha224_ctx_free, |
Paul Bakker | 1bd3ae8 | 2013-03-13 10:26:44 +0100 | [diff] [blame] | 635 | sha224_process_wrap, |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 636 | }; |
| 637 | |
Paul Bakker | d1df02a | 2013-03-13 10:31:31 +0100 | [diff] [blame] | 638 | static void sha256_starts_wrap( void *ctx ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 639 | { |
Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 640 | sha256_starts( (sha256_context *) ctx, 0 ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 641 | } |
| 642 | |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 643 | static void sha256_update_wrap( void *ctx, const unsigned char *input, |
| 644 | size_t ilen ) |
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_update( (sha256_context *) ctx, input, ilen ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 647 | } |
| 648 | |
Paul Bakker | d1df02a | 2013-03-13 10:31:31 +0100 | [diff] [blame] | 649 | static void sha256_finish_wrap( void *ctx, unsigned char *output ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 650 | { |
Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 651 | sha256_finish( (sha256_context *) ctx, output ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 652 | } |
| 653 | |
Paul Bakker | d1df02a | 2013-03-13 10:31:31 +0100 | [diff] [blame] | 654 | static void sha256_wrap( const unsigned char *input, size_t ilen, |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 655 | unsigned char *output ) |
| 656 | { |
Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 657 | sha256( input, ilen, output, 0 ); |
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 int sha256_file_wrap( const char *path, unsigned char *output ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 661 | { |
Paul Bakker | 335db3f | 2011-04-25 15:28:35 +0000 | [diff] [blame] | 662 | #if defined(POLARSSL_FS_IO) |
Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 663 | return sha256_file( path, output, 0 ); |
Paul Bakker | 335db3f | 2011-04-25 15:28:35 +0000 | [diff] [blame] | 664 | #else |
| 665 | ((void) path); |
| 666 | ((void) output); |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 667 | return( POLARSSL_ERR_MD_FEATURE_UNAVAILABLE ); |
Paul Bakker | 335db3f | 2011-04-25 15:28:35 +0000 | [diff] [blame] | 668 | #endif |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 669 | } |
| 670 | |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 671 | static void sha256_hmac_starts_wrap( void *ctx, const unsigned char *key, |
| 672 | size_t keylen ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 673 | { |
Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 674 | sha256_hmac_starts( (sha256_context *) ctx, key, keylen, 0 ); |
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_update_wrap( void *ctx, const unsigned char *input, |
| 678 | size_t ilen ) |
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_update( (sha256_context *) ctx, input, ilen ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 681 | } |
| 682 | |
Paul Bakker | d1df02a | 2013-03-13 10:31:31 +0100 | [diff] [blame] | 683 | static void sha256_hmac_finish_wrap( void *ctx, unsigned char *output ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 684 | { |
Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 685 | sha256_hmac_finish( (sha256_context *) ctx, output ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 686 | } |
| 687 | |
Paul Bakker | d1df02a | 2013-03-13 10:31:31 +0100 | [diff] [blame] | 688 | static void sha256_hmac_reset_wrap( void *ctx ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 689 | { |
Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 690 | sha256_hmac_reset( (sha256_context *) ctx ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 691 | } |
| 692 | |
Paul Bakker | d1df02a | 2013-03-13 10:31:31 +0100 | [diff] [blame] | 693 | static void sha256_hmac_wrap( const unsigned char *key, size_t keylen, |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 694 | const unsigned char *input, size_t ilen, |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 695 | unsigned char *output ) |
| 696 | { |
Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 697 | sha256_hmac( key, keylen, input, ilen, output, 0 ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 698 | } |
| 699 | |
Paul Bakker | d1df02a | 2013-03-13 10:31:31 +0100 | [diff] [blame] | 700 | static void * sha256_ctx_alloc( void ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 701 | { |
Paul Bakker | 5b4af39 | 2014-06-26 12:09:34 +0200 | [diff] [blame] | 702 | sha256_context *ctx; |
Mansour Moufid | c531b4a | 2015-02-15 17:35:38 -0500 | [diff] [blame] | 703 | ctx = polarssl_malloc( sizeof( sha256_context ) ); |
Paul Bakker | 5b4af39 | 2014-06-26 12:09:34 +0200 | [diff] [blame] | 704 | |
| 705 | if( ctx == NULL ) |
| 706 | return( NULL ); |
| 707 | |
| 708 | sha256_init( ctx ); |
| 709 | |
| 710 | return( ctx ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 711 | } |
| 712 | |
Paul Bakker | d1df02a | 2013-03-13 10:31:31 +0100 | [diff] [blame] | 713 | static void sha256_ctx_free( void *ctx ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 714 | { |
Paul Bakker | 5b4af39 | 2014-06-26 12:09:34 +0200 | [diff] [blame] | 715 | sha256_free( (sha256_context *) ctx ); |
Paul Bakker | 6e339b5 | 2013-07-03 13:37:05 +0200 | [diff] [blame] | 716 | polarssl_free( 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_process_wrap( void *ctx, const unsigned char *data ) |
Paul Bakker | 1bd3ae8 | 2013-03-13 10:26:44 +0100 | [diff] [blame] | 720 | { |
Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 721 | sha256_process( (sha256_context *) ctx, data ); |
Paul Bakker | 1bd3ae8 | 2013-03-13 10:26:44 +0100 | [diff] [blame] | 722 | } |
| 723 | |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 724 | const md_info_t sha256_info = { |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 725 | POLARSSL_MD_SHA256, |
| 726 | "SHA256", |
| 727 | 32, |
| 728 | sha256_starts_wrap, |
| 729 | sha256_update_wrap, |
| 730 | sha256_finish_wrap, |
| 731 | sha256_wrap, |
| 732 | sha256_file_wrap, |
| 733 | sha256_hmac_starts_wrap, |
| 734 | sha256_hmac_update_wrap, |
| 735 | sha256_hmac_finish_wrap, |
| 736 | sha256_hmac_reset_wrap, |
| 737 | sha256_hmac_wrap, |
| 738 | sha256_ctx_alloc, |
| 739 | sha256_ctx_free, |
Paul Bakker | 1bd3ae8 | 2013-03-13 10:26:44 +0100 | [diff] [blame] | 740 | sha256_process_wrap, |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 741 | }; |
| 742 | |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 743 | #endif /* POLARSSL_SHA256_C */ |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 744 | |
Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 745 | #if defined(POLARSSL_SHA512_C) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 746 | |
Paul Bakker | d1df02a | 2013-03-13 10:31:31 +0100 | [diff] [blame] | 747 | static void sha384_starts_wrap( void *ctx ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 748 | { |
Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 749 | sha512_starts( (sha512_context *) ctx, 1 ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 750 | } |
| 751 | |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 752 | static void sha384_update_wrap( void *ctx, const unsigned char *input, |
| 753 | size_t ilen ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 754 | { |
Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 755 | sha512_update( (sha512_context *) ctx, input, ilen ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 756 | } |
| 757 | |
Paul Bakker | d1df02a | 2013-03-13 10:31:31 +0100 | [diff] [blame] | 758 | static void sha384_finish_wrap( void *ctx, unsigned char *output ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 759 | { |
Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 760 | sha512_finish( (sha512_context *) ctx, output ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 761 | } |
| 762 | |
Paul Bakker | d1df02a | 2013-03-13 10:31:31 +0100 | [diff] [blame] | 763 | static void sha384_wrap( const unsigned char *input, size_t ilen, |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 764 | unsigned char *output ) |
| 765 | { |
Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 766 | sha512( input, ilen, output, 1 ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 767 | } |
| 768 | |
Paul Bakker | d1df02a | 2013-03-13 10:31:31 +0100 | [diff] [blame] | 769 | static int sha384_file_wrap( const char *path, unsigned char *output ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 770 | { |
Paul Bakker | 335db3f | 2011-04-25 15:28:35 +0000 | [diff] [blame] | 771 | #if defined(POLARSSL_FS_IO) |
Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 772 | return sha512_file( path, output, 1 ); |
Paul Bakker | 335db3f | 2011-04-25 15:28:35 +0000 | [diff] [blame] | 773 | #else |
| 774 | ((void) path); |
| 775 | ((void) output); |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 776 | return( POLARSSL_ERR_MD_FEATURE_UNAVAILABLE ); |
Paul Bakker | 335db3f | 2011-04-25 15:28:35 +0000 | [diff] [blame] | 777 | #endif |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 778 | } |
| 779 | |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 780 | static void sha384_hmac_starts_wrap( void *ctx, const unsigned char *key, |
| 781 | size_t keylen ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 782 | { |
Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 783 | sha512_hmac_starts( (sha512_context *) ctx, key, keylen, 1 ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 784 | } |
| 785 | |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 786 | static void sha384_hmac_update_wrap( void *ctx, const unsigned char *input, |
| 787 | size_t ilen ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 788 | { |
Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 789 | sha512_hmac_update( (sha512_context *) ctx, input, ilen ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 790 | } |
| 791 | |
Paul Bakker | d1df02a | 2013-03-13 10:31:31 +0100 | [diff] [blame] | 792 | static void sha384_hmac_finish_wrap( void *ctx, unsigned char *output ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 793 | { |
Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 794 | sha512_hmac_finish( (sha512_context *) ctx, output ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 795 | } |
| 796 | |
Paul Bakker | d1df02a | 2013-03-13 10:31:31 +0100 | [diff] [blame] | 797 | static void sha384_hmac_reset_wrap( void *ctx ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 798 | { |
Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 799 | sha512_hmac_reset( (sha512_context *) ctx ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 800 | } |
| 801 | |
Paul Bakker | d1df02a | 2013-03-13 10:31:31 +0100 | [diff] [blame] | 802 | static void sha384_hmac_wrap( const unsigned char *key, size_t keylen, |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 803 | const unsigned char *input, size_t ilen, |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 804 | unsigned char *output ) |
| 805 | { |
Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 806 | sha512_hmac( key, keylen, input, ilen, output, 1 ); |
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_ctx_alloc( void ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 810 | { |
Paul Bakker | 6e339b5 | 2013-07-03 13:37:05 +0200 | [diff] [blame] | 811 | return polarssl_malloc( sizeof( sha512_context ) ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 812 | } |
| 813 | |
Paul Bakker | d1df02a | 2013-03-13 10:31:31 +0100 | [diff] [blame] | 814 | static void sha384_ctx_free( void *ctx ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 815 | { |
Paul Bakker | 3461772 | 2014-06-13 17:20:13 +0200 | [diff] [blame] | 816 | polarssl_zeroize( ctx, sizeof( sha512_context ) ); |
Paul Bakker | 6e339b5 | 2013-07-03 13:37:05 +0200 | [diff] [blame] | 817 | polarssl_free( ctx ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 818 | } |
| 819 | |
Paul Bakker | d1df02a | 2013-03-13 10:31:31 +0100 | [diff] [blame] | 820 | static void sha384_process_wrap( void *ctx, const unsigned char *data ) |
Paul Bakker | 1bd3ae8 | 2013-03-13 10:26:44 +0100 | [diff] [blame] | 821 | { |
Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 822 | sha512_process( (sha512_context *) ctx, data ); |
Paul Bakker | 1bd3ae8 | 2013-03-13 10:26:44 +0100 | [diff] [blame] | 823 | } |
| 824 | |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 825 | const md_info_t sha384_info = { |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 826 | POLARSSL_MD_SHA384, |
| 827 | "SHA384", |
| 828 | 48, |
| 829 | sha384_starts_wrap, |
| 830 | sha384_update_wrap, |
| 831 | sha384_finish_wrap, |
| 832 | sha384_wrap, |
| 833 | sha384_file_wrap, |
| 834 | sha384_hmac_starts_wrap, |
| 835 | sha384_hmac_update_wrap, |
| 836 | sha384_hmac_finish_wrap, |
| 837 | sha384_hmac_reset_wrap, |
| 838 | sha384_hmac_wrap, |
| 839 | sha384_ctx_alloc, |
| 840 | sha384_ctx_free, |
Paul Bakker | 1bd3ae8 | 2013-03-13 10:26:44 +0100 | [diff] [blame] | 841 | sha384_process_wrap, |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 842 | }; |
| 843 | |
Paul Bakker | d1df02a | 2013-03-13 10:31:31 +0100 | [diff] [blame] | 844 | static void sha512_starts_wrap( void *ctx ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 845 | { |
Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 846 | sha512_starts( (sha512_context *) ctx, 0 ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 847 | } |
| 848 | |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 849 | static void sha512_update_wrap( void *ctx, const unsigned char *input, |
| 850 | size_t ilen ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 851 | { |
Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 852 | sha512_update( (sha512_context *) ctx, input, ilen ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 853 | } |
| 854 | |
Paul Bakker | d1df02a | 2013-03-13 10:31:31 +0100 | [diff] [blame] | 855 | static void sha512_finish_wrap( void *ctx, unsigned char *output ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 856 | { |
Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 857 | sha512_finish( (sha512_context *) ctx, output ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 858 | } |
| 859 | |
Paul Bakker | d1df02a | 2013-03-13 10:31:31 +0100 | [diff] [blame] | 860 | static void sha512_wrap( const unsigned char *input, size_t ilen, |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 861 | unsigned char *output ) |
| 862 | { |
Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 863 | sha512( input, ilen, output, 0 ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 864 | } |
| 865 | |
Paul Bakker | d1df02a | 2013-03-13 10:31:31 +0100 | [diff] [blame] | 866 | static int sha512_file_wrap( const char *path, unsigned char *output ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 867 | { |
Paul Bakker | 335db3f | 2011-04-25 15:28:35 +0000 | [diff] [blame] | 868 | #if defined(POLARSSL_FS_IO) |
Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 869 | return sha512_file( path, output, 0 ); |
Paul Bakker | 335db3f | 2011-04-25 15:28:35 +0000 | [diff] [blame] | 870 | #else |
| 871 | ((void) path); |
| 872 | ((void) output); |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 873 | return( POLARSSL_ERR_MD_FEATURE_UNAVAILABLE ); |
Paul Bakker | 335db3f | 2011-04-25 15:28:35 +0000 | [diff] [blame] | 874 | #endif |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 875 | } |
| 876 | |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 877 | static void sha512_hmac_starts_wrap( void *ctx, const unsigned char *key, |
| 878 | size_t keylen ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 879 | { |
Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 880 | sha512_hmac_starts( (sha512_context *) ctx, key, keylen, 0 ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 881 | } |
| 882 | |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 883 | static void sha512_hmac_update_wrap( void *ctx, const unsigned char *input, |
| 884 | size_t ilen ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 885 | { |
Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 886 | sha512_hmac_update( (sha512_context *) ctx, input, ilen ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 887 | } |
| 888 | |
Paul Bakker | d1df02a | 2013-03-13 10:31:31 +0100 | [diff] [blame] | 889 | static void sha512_hmac_finish_wrap( void *ctx, unsigned char *output ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 890 | { |
Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 891 | sha512_hmac_finish( (sha512_context *) ctx, output ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 892 | } |
| 893 | |
Paul Bakker | d1df02a | 2013-03-13 10:31:31 +0100 | [diff] [blame] | 894 | static void sha512_hmac_reset_wrap( void *ctx ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 895 | { |
Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 896 | sha512_hmac_reset( (sha512_context *) ctx ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 897 | } |
| 898 | |
Paul Bakker | d1df02a | 2013-03-13 10:31:31 +0100 | [diff] [blame] | 899 | static void sha512_hmac_wrap( const unsigned char *key, size_t keylen, |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 900 | const unsigned char *input, size_t ilen, |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 901 | unsigned char *output ) |
| 902 | { |
Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 903 | sha512_hmac( key, keylen, input, ilen, output, 0 ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 904 | } |
| 905 | |
Paul Bakker | d1df02a | 2013-03-13 10:31:31 +0100 | [diff] [blame] | 906 | static void * sha512_ctx_alloc( void ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 907 | { |
Paul Bakker | 5b4af39 | 2014-06-26 12:09:34 +0200 | [diff] [blame] | 908 | sha512_context *ctx; |
Mansour Moufid | c531b4a | 2015-02-15 17:35:38 -0500 | [diff] [blame] | 909 | ctx = polarssl_malloc( sizeof( sha512_context ) ); |
Paul Bakker | 5b4af39 | 2014-06-26 12:09:34 +0200 | [diff] [blame] | 910 | |
| 911 | if( ctx == NULL ) |
| 912 | return( NULL ); |
| 913 | |
| 914 | sha512_init( ctx ); |
| 915 | |
| 916 | return( ctx ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 917 | } |
| 918 | |
Paul Bakker | d1df02a | 2013-03-13 10:31:31 +0100 | [diff] [blame] | 919 | static void sha512_ctx_free( void *ctx ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 920 | { |
Paul Bakker | 5b4af39 | 2014-06-26 12:09:34 +0200 | [diff] [blame] | 921 | sha512_free( (sha512_context *) ctx ); |
Paul Bakker | 6e339b5 | 2013-07-03 13:37:05 +0200 | [diff] [blame] | 922 | polarssl_free( ctx ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 923 | } |
| 924 | |
Paul Bakker | d1df02a | 2013-03-13 10:31:31 +0100 | [diff] [blame] | 925 | static void sha512_process_wrap( void *ctx, const unsigned char *data ) |
Paul Bakker | 1bd3ae8 | 2013-03-13 10:26:44 +0100 | [diff] [blame] | 926 | { |
Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 927 | sha512_process( (sha512_context *) ctx, data ); |
Paul Bakker | 1bd3ae8 | 2013-03-13 10:26:44 +0100 | [diff] [blame] | 928 | } |
| 929 | |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 930 | const md_info_t sha512_info = { |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 931 | POLARSSL_MD_SHA512, |
| 932 | "SHA512", |
| 933 | 64, |
| 934 | sha512_starts_wrap, |
| 935 | sha512_update_wrap, |
| 936 | sha512_finish_wrap, |
| 937 | sha512_wrap, |
| 938 | sha512_file_wrap, |
| 939 | sha512_hmac_starts_wrap, |
| 940 | sha512_hmac_update_wrap, |
| 941 | sha512_hmac_finish_wrap, |
| 942 | sha512_hmac_reset_wrap, |
| 943 | sha512_hmac_wrap, |
| 944 | sha512_ctx_alloc, |
| 945 | sha512_ctx_free, |
Paul Bakker | 1bd3ae8 | 2013-03-13 10:26:44 +0100 | [diff] [blame] | 946 | sha512_process_wrap, |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 947 | }; |
| 948 | |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 949 | #endif /* POLARSSL_SHA512_C */ |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 950 | |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 951 | #endif /* POLARSSL_MD_C */ |