Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 1 | /** |
| 2 | * \file md_wrap.c |
| 3 | |
| 4 | * \brief Generic message digest wrapper for PolarSSL |
| 5 | * |
| 6 | * \author Adriaan de Jong <dejong@fox-it.com> |
| 7 | * |
Paul Bakker | 530927b | 2015-02-13 14:24:10 +0100 | [diff] [blame] | 8 | * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 9 | * |
Manuel Pégourié-Gonnard | e12abf9 | 2015-01-28 17:13:45 +0000 | [diff] [blame] | 10 | * This file is part of mbed TLS (https://polarssl.org) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 11 | * |
| 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 | |
| 27 | #include "polarssl/config.h" |
| 28 | |
| 29 | #if defined(POLARSSL_MD_C) |
| 30 | |
| 31 | #include "polarssl/md_wrap.h" |
Paul Bakker | f654371 | 2012-03-05 14:01:29 +0000 | [diff] [blame] | 32 | |
| 33 | #if defined(POLARSSL_MD2_C) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 34 | #include "polarssl/md2.h" |
Paul Bakker | f654371 | 2012-03-05 14:01:29 +0000 | [diff] [blame] | 35 | #endif |
| 36 | |
| 37 | #if defined(POLARSSL_MD4_C) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 38 | #include "polarssl/md4.h" |
Paul Bakker | f654371 | 2012-03-05 14:01:29 +0000 | [diff] [blame] | 39 | #endif |
| 40 | |
| 41 | #if defined(POLARSSL_MD5_C) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 42 | #include "polarssl/md5.h" |
Paul Bakker | f654371 | 2012-03-05 14:01:29 +0000 | [diff] [blame] | 43 | #endif |
| 44 | |
| 45 | #if defined(POLARSSL_SHA1_C) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 46 | #include "polarssl/sha1.h" |
Paul Bakker | f654371 | 2012-03-05 14:01:29 +0000 | [diff] [blame] | 47 | #endif |
| 48 | |
| 49 | #if defined(POLARSSL_SHA2_C) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 50 | #include "polarssl/sha2.h" |
Paul Bakker | f654371 | 2012-03-05 14:01:29 +0000 | [diff] [blame] | 51 | #endif |
| 52 | |
| 53 | #if defined(POLARSSL_SHA4_C) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 54 | #include "polarssl/sha4.h" |
Paul Bakker | f654371 | 2012-03-05 14:01:29 +0000 | [diff] [blame] | 55 | #endif |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 56 | |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 57 | #include <stdlib.h> |
| 58 | |
Paul Bakker | 312da33 | 2014-06-13 17:20:13 +0200 | [diff] [blame] | 59 | /* Implementation that should never be optimized out by the compiler */ |
| 60 | static void polarssl_zeroize( void *v, size_t n ) { |
| 61 | volatile unsigned char *p = v; while( n-- ) *p++ = 0; |
| 62 | } |
| 63 | |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 64 | #if defined(POLARSSL_MD2_C) |
| 65 | |
| 66 | static void md2_starts_wrap( void *ctx ) |
| 67 | { |
| 68 | md2_starts( (md2_context *) ctx ); |
| 69 | } |
| 70 | |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 71 | static void md2_update_wrap( void *ctx, const unsigned char *input, size_t ilen ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 72 | { |
| 73 | md2_update( (md2_context *) ctx, input, ilen ); |
| 74 | } |
| 75 | |
| 76 | static void md2_finish_wrap( void *ctx, unsigned char *output ) |
| 77 | { |
| 78 | md2_finish( (md2_context *) ctx, output ); |
| 79 | } |
| 80 | |
Paul Bakker | 1d073c5 | 2014-07-08 20:15:51 +0200 | [diff] [blame] | 81 | static int md2_file_wrap( const char *path, unsigned char *output ) |
Paul Bakker | 335db3f | 2011-04-25 15:28:35 +0000 | [diff] [blame] | 82 | { |
| 83 | #if defined(POLARSSL_FS_IO) |
| 84 | return md2_file( path, output ); |
| 85 | #else |
| 86 | ((void) path); |
| 87 | ((void) output); |
| 88 | return POLARSSL_ERR_MD_FEATURE_UNAVAILABLE; |
| 89 | #endif |
| 90 | } |
| 91 | |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 92 | static void md2_hmac_starts_wrap( void *ctx, const unsigned char *key, size_t keylen ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 93 | { |
| 94 | md2_hmac_starts( (md2_context *) ctx, key, keylen ); |
| 95 | } |
| 96 | |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 97 | static void md2_hmac_update_wrap( void *ctx, const unsigned char *input, size_t ilen ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 98 | { |
| 99 | md2_hmac_update( (md2_context *) ctx, input, ilen ); |
| 100 | } |
| 101 | |
| 102 | static void md2_hmac_finish_wrap( void *ctx, unsigned char *output ) |
| 103 | { |
| 104 | md2_hmac_finish( (md2_context *) ctx, output ); |
| 105 | } |
| 106 | |
| 107 | static void md2_hmac_reset_wrap( void *ctx ) |
| 108 | { |
| 109 | md2_hmac_reset( (md2_context *) ctx ); |
| 110 | } |
| 111 | |
| 112 | static void * md2_ctx_alloc( void ) |
| 113 | { |
| 114 | return malloc( sizeof( md2_context ) ); |
| 115 | } |
| 116 | |
| 117 | static void md2_ctx_free( void *ctx ) |
| 118 | { |
Paul Bakker | 312da33 | 2014-06-13 17:20:13 +0200 | [diff] [blame] | 119 | polarssl_zeroize( ctx, sizeof( md2_context ) ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 120 | free( ctx ); |
| 121 | } |
| 122 | |
| 123 | const md_info_t md2_info = { |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 124 | POLARSSL_MD_MD2, |
| 125 | "MD2", |
| 126 | 16, |
| 127 | md2_starts_wrap, |
| 128 | md2_update_wrap, |
| 129 | md2_finish_wrap, |
| 130 | md2, |
Paul Bakker | 335db3f | 2011-04-25 15:28:35 +0000 | [diff] [blame] | 131 | md2_file_wrap, |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 132 | md2_hmac_starts_wrap, |
| 133 | md2_hmac_update_wrap, |
| 134 | md2_hmac_finish_wrap, |
| 135 | md2_hmac_reset_wrap, |
| 136 | md2_hmac, |
| 137 | md2_ctx_alloc, |
| 138 | md2_ctx_free, |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 139 | }; |
| 140 | |
| 141 | #endif |
| 142 | |
| 143 | #if defined(POLARSSL_MD4_C) |
| 144 | |
Paul Bakker | 1d073c5 | 2014-07-08 20:15:51 +0200 | [diff] [blame] | 145 | static void md4_starts_wrap( void *ctx ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 146 | { |
| 147 | md4_starts( (md4_context *) ctx ); |
| 148 | } |
| 149 | |
Paul Bakker | 1d073c5 | 2014-07-08 20:15:51 +0200 | [diff] [blame] | 150 | static void md4_update_wrap( void *ctx, const unsigned char *input, size_t ilen ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 151 | { |
| 152 | md4_update( (md4_context *) ctx, input, ilen ); |
| 153 | } |
| 154 | |
Paul Bakker | 1d073c5 | 2014-07-08 20:15:51 +0200 | [diff] [blame] | 155 | static void md4_finish_wrap( void *ctx, unsigned char *output ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 156 | { |
| 157 | md4_finish( (md4_context *) ctx, output ); |
| 158 | } |
| 159 | |
Paul Bakker | 1d073c5 | 2014-07-08 20:15:51 +0200 | [diff] [blame] | 160 | static int md4_file_wrap( const char *path, unsigned char *output ) |
Paul Bakker | 335db3f | 2011-04-25 15:28:35 +0000 | [diff] [blame] | 161 | { |
| 162 | #if defined(POLARSSL_FS_IO) |
| 163 | return md4_file( path, output ); |
| 164 | #else |
| 165 | ((void) path); |
| 166 | ((void) output); |
| 167 | return POLARSSL_ERR_MD_FEATURE_UNAVAILABLE; |
| 168 | #endif |
| 169 | } |
| 170 | |
Paul Bakker | 1d073c5 | 2014-07-08 20:15:51 +0200 | [diff] [blame] | 171 | static void md4_hmac_starts_wrap( void *ctx, const unsigned char *key, size_t keylen ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 172 | { |
| 173 | md4_hmac_starts( (md4_context *) ctx, key, keylen ); |
| 174 | } |
| 175 | |
Paul Bakker | 1d073c5 | 2014-07-08 20:15:51 +0200 | [diff] [blame] | 176 | static void md4_hmac_update_wrap( void *ctx, const unsigned char *input, size_t ilen ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 177 | { |
| 178 | md4_hmac_update( (md4_context *) ctx, input, ilen ); |
| 179 | } |
| 180 | |
Paul Bakker | 1d073c5 | 2014-07-08 20:15:51 +0200 | [diff] [blame] | 181 | static void md4_hmac_finish_wrap( void *ctx, unsigned char *output ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 182 | { |
| 183 | md4_hmac_finish( (md4_context *) ctx, output ); |
| 184 | } |
| 185 | |
Paul Bakker | 1d073c5 | 2014-07-08 20:15:51 +0200 | [diff] [blame] | 186 | static void md4_hmac_reset_wrap( void *ctx ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 187 | { |
| 188 | md4_hmac_reset( (md4_context *) ctx ); |
| 189 | } |
| 190 | |
Paul Bakker | 1d073c5 | 2014-07-08 20:15:51 +0200 | [diff] [blame] | 191 | static void *md4_ctx_alloc( void ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 192 | { |
| 193 | return malloc( sizeof( md4_context ) ); |
| 194 | } |
| 195 | |
Paul Bakker | 1d073c5 | 2014-07-08 20:15:51 +0200 | [diff] [blame] | 196 | static void md4_ctx_free( void *ctx ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 197 | { |
Paul Bakker | 312da33 | 2014-06-13 17:20:13 +0200 | [diff] [blame] | 198 | polarssl_zeroize( ctx, sizeof( md4_context ) ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 199 | free( ctx ); |
| 200 | } |
| 201 | |
| 202 | const md_info_t md4_info = { |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 203 | POLARSSL_MD_MD4, |
| 204 | "MD4", |
| 205 | 16, |
| 206 | md4_starts_wrap, |
| 207 | md4_update_wrap, |
| 208 | md4_finish_wrap, |
| 209 | md4, |
Paul Bakker | 335db3f | 2011-04-25 15:28:35 +0000 | [diff] [blame] | 210 | md4_file_wrap, |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 211 | md4_hmac_starts_wrap, |
| 212 | md4_hmac_update_wrap, |
| 213 | md4_hmac_finish_wrap, |
| 214 | md4_hmac_reset_wrap, |
| 215 | md4_hmac, |
| 216 | md4_ctx_alloc, |
| 217 | md4_ctx_free, |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 218 | }; |
| 219 | |
| 220 | #endif |
| 221 | |
| 222 | #if defined(POLARSSL_MD5_C) |
| 223 | |
| 224 | static void md5_starts_wrap( void *ctx ) |
| 225 | { |
| 226 | md5_starts( (md5_context *) ctx ); |
| 227 | } |
| 228 | |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 229 | static void md5_update_wrap( void *ctx, const unsigned char *input, size_t ilen ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 230 | { |
| 231 | md5_update( (md5_context *) ctx, input, ilen ); |
| 232 | } |
| 233 | |
| 234 | static void md5_finish_wrap( void *ctx, unsigned char *output ) |
| 235 | { |
| 236 | md5_finish( (md5_context *) ctx, output ); |
| 237 | } |
| 238 | |
Paul Bakker | 1d073c5 | 2014-07-08 20:15:51 +0200 | [diff] [blame] | 239 | static int md5_file_wrap( const char *path, unsigned char *output ) |
Paul Bakker | 335db3f | 2011-04-25 15:28:35 +0000 | [diff] [blame] | 240 | { |
| 241 | #if defined(POLARSSL_FS_IO) |
| 242 | return md5_file( path, output ); |
| 243 | #else |
| 244 | ((void) path); |
| 245 | ((void) output); |
| 246 | return POLARSSL_ERR_MD_FEATURE_UNAVAILABLE; |
| 247 | #endif |
| 248 | } |
| 249 | |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 250 | static void md5_hmac_starts_wrap( void *ctx, const unsigned char *key, size_t keylen ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 251 | { |
| 252 | md5_hmac_starts( (md5_context *) ctx, key, keylen ); |
| 253 | } |
| 254 | |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 255 | static void md5_hmac_update_wrap( void *ctx, const unsigned char *input, size_t ilen ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 256 | { |
| 257 | md5_hmac_update( (md5_context *) ctx, input, ilen ); |
| 258 | } |
| 259 | |
| 260 | static void md5_hmac_finish_wrap( void *ctx, unsigned char *output ) |
| 261 | { |
| 262 | md5_hmac_finish( (md5_context *) ctx, output ); |
| 263 | } |
| 264 | |
| 265 | static void md5_hmac_reset_wrap( void *ctx ) |
| 266 | { |
| 267 | md5_hmac_reset( (md5_context *) ctx ); |
| 268 | } |
| 269 | |
| 270 | static void * md5_ctx_alloc( void ) |
| 271 | { |
| 272 | return malloc( sizeof( md5_context ) ); |
| 273 | } |
| 274 | |
| 275 | static void md5_ctx_free( void *ctx ) |
| 276 | { |
Paul Bakker | 312da33 | 2014-06-13 17:20:13 +0200 | [diff] [blame] | 277 | polarssl_zeroize( ctx, sizeof( md5_context ) ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 278 | free( ctx ); |
| 279 | } |
| 280 | |
| 281 | const md_info_t md5_info = { |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 282 | POLARSSL_MD_MD5, |
| 283 | "MD5", |
| 284 | 16, |
| 285 | md5_starts_wrap, |
| 286 | md5_update_wrap, |
| 287 | md5_finish_wrap, |
| 288 | md5, |
Paul Bakker | 335db3f | 2011-04-25 15:28:35 +0000 | [diff] [blame] | 289 | md5_file_wrap, |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 290 | md5_hmac_starts_wrap, |
| 291 | md5_hmac_update_wrap, |
| 292 | md5_hmac_finish_wrap, |
| 293 | md5_hmac_reset_wrap, |
| 294 | md5_hmac, |
| 295 | md5_ctx_alloc, |
| 296 | md5_ctx_free, |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 297 | }; |
| 298 | |
| 299 | #endif |
| 300 | |
| 301 | #if defined(POLARSSL_SHA1_C) |
| 302 | |
Paul Bakker | 1d073c5 | 2014-07-08 20:15:51 +0200 | [diff] [blame] | 303 | static void sha1_starts_wrap( void *ctx ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 304 | { |
| 305 | sha1_starts( (sha1_context *) ctx ); |
| 306 | } |
| 307 | |
Paul Bakker | 1d073c5 | 2014-07-08 20:15:51 +0200 | [diff] [blame] | 308 | static void sha1_update_wrap( void *ctx, const unsigned char *input, size_t ilen ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 309 | { |
| 310 | sha1_update( (sha1_context *) ctx, input, ilen ); |
| 311 | } |
| 312 | |
Paul Bakker | 1d073c5 | 2014-07-08 20:15:51 +0200 | [diff] [blame] | 313 | static void sha1_finish_wrap( void *ctx, unsigned char *output ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 314 | { |
| 315 | sha1_finish( (sha1_context *) ctx, output ); |
| 316 | } |
| 317 | |
Paul Bakker | 1d073c5 | 2014-07-08 20:15:51 +0200 | [diff] [blame] | 318 | static int sha1_file_wrap( const char *path, unsigned char *output ) |
Paul Bakker | 335db3f | 2011-04-25 15:28:35 +0000 | [diff] [blame] | 319 | { |
| 320 | #if defined(POLARSSL_FS_IO) |
| 321 | return sha1_file( path, output ); |
| 322 | #else |
| 323 | ((void) path); |
| 324 | ((void) output); |
| 325 | return POLARSSL_ERR_MD_FEATURE_UNAVAILABLE; |
| 326 | #endif |
| 327 | } |
| 328 | |
Paul Bakker | 1d073c5 | 2014-07-08 20:15:51 +0200 | [diff] [blame] | 329 | static void sha1_hmac_starts_wrap( void *ctx, const unsigned char *key, size_t keylen ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 330 | { |
| 331 | sha1_hmac_starts( (sha1_context *) ctx, key, keylen ); |
| 332 | } |
| 333 | |
Paul Bakker | 1d073c5 | 2014-07-08 20:15:51 +0200 | [diff] [blame] | 334 | static void sha1_hmac_update_wrap( void *ctx, const unsigned char *input, size_t ilen ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 335 | { |
| 336 | sha1_hmac_update( (sha1_context *) ctx, input, ilen ); |
| 337 | } |
| 338 | |
Paul Bakker | 1d073c5 | 2014-07-08 20:15:51 +0200 | [diff] [blame] | 339 | static void sha1_hmac_finish_wrap( void *ctx, unsigned char *output ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 340 | { |
| 341 | sha1_hmac_finish( (sha1_context *) ctx, output ); |
| 342 | } |
| 343 | |
Paul Bakker | 1d073c5 | 2014-07-08 20:15:51 +0200 | [diff] [blame] | 344 | static void sha1_hmac_reset_wrap( void *ctx ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 345 | { |
| 346 | sha1_hmac_reset( (sha1_context *) ctx ); |
| 347 | } |
| 348 | |
Paul Bakker | 1d073c5 | 2014-07-08 20:15:51 +0200 | [diff] [blame] | 349 | static void * sha1_ctx_alloc( void ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 350 | { |
| 351 | return malloc( sizeof( sha1_context ) ); |
| 352 | } |
| 353 | |
Paul Bakker | 1d073c5 | 2014-07-08 20:15:51 +0200 | [diff] [blame] | 354 | static void sha1_ctx_free( void *ctx ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 355 | { |
Paul Bakker | 312da33 | 2014-06-13 17:20:13 +0200 | [diff] [blame] | 356 | polarssl_zeroize( ctx, sizeof( sha1_context ) ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 357 | free( ctx ); |
| 358 | } |
| 359 | |
| 360 | const md_info_t sha1_info = { |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 361 | POLARSSL_MD_SHA1, |
| 362 | "SHA1", |
| 363 | 20, |
| 364 | sha1_starts_wrap, |
| 365 | sha1_update_wrap, |
| 366 | sha1_finish_wrap, |
| 367 | sha1, |
Paul Bakker | 335db3f | 2011-04-25 15:28:35 +0000 | [diff] [blame] | 368 | sha1_file_wrap, |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 369 | sha1_hmac_starts_wrap, |
| 370 | sha1_hmac_update_wrap, |
| 371 | sha1_hmac_finish_wrap, |
| 372 | sha1_hmac_reset_wrap, |
| 373 | sha1_hmac, |
| 374 | sha1_ctx_alloc, |
| 375 | sha1_ctx_free, |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 376 | }; |
| 377 | |
| 378 | #endif |
| 379 | |
| 380 | /* |
| 381 | * Wrappers for generic message digests |
| 382 | */ |
| 383 | #if defined(POLARSSL_SHA2_C) |
| 384 | |
Paul Bakker | 1d073c5 | 2014-07-08 20:15:51 +0200 | [diff] [blame] | 385 | static void sha224_starts_wrap( void *ctx ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 386 | { |
| 387 | sha2_starts( (sha2_context *) ctx, 1 ); |
| 388 | } |
| 389 | |
Paul Bakker | 1d073c5 | 2014-07-08 20:15:51 +0200 | [diff] [blame] | 390 | static void sha224_update_wrap( void *ctx, const unsigned char *input, size_t ilen ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 391 | { |
| 392 | sha2_update( (sha2_context *) ctx, input, ilen ); |
| 393 | } |
| 394 | |
Paul Bakker | 1d073c5 | 2014-07-08 20:15:51 +0200 | [diff] [blame] | 395 | static void sha224_finish_wrap( void *ctx, unsigned char *output ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 396 | { |
| 397 | sha2_finish( (sha2_context *) ctx, output ); |
| 398 | } |
| 399 | |
Paul Bakker | 1d073c5 | 2014-07-08 20:15:51 +0200 | [diff] [blame] | 400 | static void sha224_wrap( const unsigned char *input, size_t ilen, |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 401 | unsigned char *output ) |
| 402 | { |
| 403 | sha2( input, ilen, output, 1 ); |
| 404 | } |
| 405 | |
Paul Bakker | 1d073c5 | 2014-07-08 20:15:51 +0200 | [diff] [blame] | 406 | static int sha224_file_wrap( const char *path, unsigned char *output ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 407 | { |
Paul Bakker | 335db3f | 2011-04-25 15:28:35 +0000 | [diff] [blame] | 408 | #if defined(POLARSSL_FS_IO) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 409 | return sha2_file( path, output, 1 ); |
Paul Bakker | 335db3f | 2011-04-25 15:28:35 +0000 | [diff] [blame] | 410 | #else |
| 411 | ((void) path); |
| 412 | ((void) output); |
| 413 | return POLARSSL_ERR_MD_FEATURE_UNAVAILABLE; |
| 414 | #endif |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 415 | } |
| 416 | |
Paul Bakker | 1d073c5 | 2014-07-08 20:15:51 +0200 | [diff] [blame] | 417 | static void sha224_hmac_starts_wrap( void *ctx, const unsigned char *key, size_t keylen ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 418 | { |
| 419 | sha2_hmac_starts( (sha2_context *) ctx, key, keylen, 1 ); |
| 420 | } |
| 421 | |
Paul Bakker | 1d073c5 | 2014-07-08 20:15:51 +0200 | [diff] [blame] | 422 | static void sha224_hmac_update_wrap( void *ctx, const unsigned char *input, size_t ilen ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 423 | { |
| 424 | sha2_hmac_update( (sha2_context *) ctx, input, ilen ); |
| 425 | } |
| 426 | |
Paul Bakker | 1d073c5 | 2014-07-08 20:15:51 +0200 | [diff] [blame] | 427 | static void sha224_hmac_finish_wrap( void *ctx, unsigned char *output ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 428 | { |
| 429 | sha2_hmac_finish( (sha2_context *) ctx, output ); |
| 430 | } |
| 431 | |
Paul Bakker | 1d073c5 | 2014-07-08 20:15:51 +0200 | [diff] [blame] | 432 | static void sha224_hmac_reset_wrap( void *ctx ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 433 | { |
| 434 | sha2_hmac_reset( (sha2_context *) ctx ); |
| 435 | } |
| 436 | |
Paul Bakker | 1d073c5 | 2014-07-08 20:15:51 +0200 | [diff] [blame] | 437 | static void sha224_hmac_wrap( const unsigned char *key, size_t keylen, |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 438 | const unsigned char *input, size_t ilen, |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 439 | unsigned char *output ) |
| 440 | { |
| 441 | sha2_hmac( key, keylen, input, ilen, output, 1 ); |
| 442 | } |
| 443 | |
Paul Bakker | 1d073c5 | 2014-07-08 20:15:51 +0200 | [diff] [blame] | 444 | static void * sha224_ctx_alloc( void ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 445 | { |
| 446 | return malloc( sizeof( sha2_context ) ); |
| 447 | } |
| 448 | |
Paul Bakker | 1d073c5 | 2014-07-08 20:15:51 +0200 | [diff] [blame] | 449 | static void sha224_ctx_free( void *ctx ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 450 | { |
Paul Bakker | 312da33 | 2014-06-13 17:20:13 +0200 | [diff] [blame] | 451 | polarssl_zeroize( ctx, sizeof( sha2_context ) ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 452 | free( ctx ); |
| 453 | } |
| 454 | |
| 455 | const md_info_t sha224_info = { |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 456 | POLARSSL_MD_SHA224, |
| 457 | "SHA224", |
| 458 | 28, |
| 459 | sha224_starts_wrap, |
| 460 | sha224_update_wrap, |
| 461 | sha224_finish_wrap, |
| 462 | sha224_wrap, |
| 463 | sha224_file_wrap, |
| 464 | sha224_hmac_starts_wrap, |
| 465 | sha224_hmac_update_wrap, |
| 466 | sha224_hmac_finish_wrap, |
| 467 | sha224_hmac_reset_wrap, |
| 468 | sha224_hmac_wrap, |
| 469 | sha224_ctx_alloc, |
| 470 | sha224_ctx_free, |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 471 | }; |
| 472 | |
Paul Bakker | 1d073c5 | 2014-07-08 20:15:51 +0200 | [diff] [blame] | 473 | static void sha256_starts_wrap( void *ctx ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 474 | { |
| 475 | sha2_starts( (sha2_context *) ctx, 0 ); |
| 476 | } |
| 477 | |
Paul Bakker | 1d073c5 | 2014-07-08 20:15:51 +0200 | [diff] [blame] | 478 | static void sha256_update_wrap( void *ctx, const unsigned char *input, size_t ilen ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 479 | { |
| 480 | sha2_update( (sha2_context *) ctx, input, ilen ); |
| 481 | } |
| 482 | |
Paul Bakker | 1d073c5 | 2014-07-08 20:15:51 +0200 | [diff] [blame] | 483 | static void sha256_finish_wrap( void *ctx, unsigned char *output ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 484 | { |
| 485 | sha2_finish( (sha2_context *) ctx, output ); |
| 486 | } |
| 487 | |
Paul Bakker | 1d073c5 | 2014-07-08 20:15:51 +0200 | [diff] [blame] | 488 | static void sha256_wrap( const unsigned char *input, size_t ilen, |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 489 | unsigned char *output ) |
| 490 | { |
| 491 | sha2( input, ilen, output, 0 ); |
| 492 | } |
| 493 | |
Paul Bakker | 1d073c5 | 2014-07-08 20:15:51 +0200 | [diff] [blame] | 494 | static int sha256_file_wrap( const char *path, unsigned char *output ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 495 | { |
Paul Bakker | 335db3f | 2011-04-25 15:28:35 +0000 | [diff] [blame] | 496 | #if defined(POLARSSL_FS_IO) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 497 | return sha2_file( path, output, 0 ); |
Paul Bakker | 335db3f | 2011-04-25 15:28:35 +0000 | [diff] [blame] | 498 | #else |
| 499 | ((void) path); |
| 500 | ((void) output); |
| 501 | return POLARSSL_ERR_MD_FEATURE_UNAVAILABLE; |
| 502 | #endif |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 503 | } |
| 504 | |
Paul Bakker | 1d073c5 | 2014-07-08 20:15:51 +0200 | [diff] [blame] | 505 | static void sha256_hmac_starts_wrap( void *ctx, const unsigned char *key, size_t keylen ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 506 | { |
| 507 | sha2_hmac_starts( (sha2_context *) ctx, key, keylen, 0 ); |
| 508 | } |
| 509 | |
Paul Bakker | 1d073c5 | 2014-07-08 20:15:51 +0200 | [diff] [blame] | 510 | static void sha256_hmac_update_wrap( void *ctx, const unsigned char *input, size_t ilen ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 511 | { |
| 512 | sha2_hmac_update( (sha2_context *) ctx, input, ilen ); |
| 513 | } |
| 514 | |
Paul Bakker | 1d073c5 | 2014-07-08 20:15:51 +0200 | [diff] [blame] | 515 | static void sha256_hmac_finish_wrap( void *ctx, unsigned char *output ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 516 | { |
| 517 | sha2_hmac_finish( (sha2_context *) ctx, output ); |
| 518 | } |
| 519 | |
Paul Bakker | 1d073c5 | 2014-07-08 20:15:51 +0200 | [diff] [blame] | 520 | static void sha256_hmac_reset_wrap( void *ctx ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 521 | { |
| 522 | sha2_hmac_reset( (sha2_context *) ctx ); |
| 523 | } |
| 524 | |
Paul Bakker | 1d073c5 | 2014-07-08 20:15:51 +0200 | [diff] [blame] | 525 | static void sha256_hmac_wrap( const unsigned char *key, size_t keylen, |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 526 | const unsigned char *input, size_t ilen, |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 527 | unsigned char *output ) |
| 528 | { |
| 529 | sha2_hmac( key, keylen, input, ilen, output, 0 ); |
| 530 | } |
| 531 | |
Paul Bakker | 1d073c5 | 2014-07-08 20:15:51 +0200 | [diff] [blame] | 532 | static void * sha256_ctx_alloc( void ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 533 | { |
Paul Bakker | 6d46812 | 2011-01-06 15:35:45 +0000 | [diff] [blame] | 534 | return malloc( sizeof( sha2_context ) ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 535 | } |
| 536 | |
Paul Bakker | 1d073c5 | 2014-07-08 20:15:51 +0200 | [diff] [blame] | 537 | static void sha256_ctx_free( void *ctx ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 538 | { |
Paul Bakker | 312da33 | 2014-06-13 17:20:13 +0200 | [diff] [blame] | 539 | polarssl_zeroize( ctx, sizeof( sha2_context ) ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 540 | free( ctx ); |
| 541 | } |
| 542 | |
| 543 | const md_info_t sha256_info = { |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 544 | POLARSSL_MD_SHA256, |
| 545 | "SHA256", |
| 546 | 32, |
| 547 | sha256_starts_wrap, |
| 548 | sha256_update_wrap, |
| 549 | sha256_finish_wrap, |
| 550 | sha256_wrap, |
| 551 | sha256_file_wrap, |
| 552 | sha256_hmac_starts_wrap, |
| 553 | sha256_hmac_update_wrap, |
| 554 | sha256_hmac_finish_wrap, |
| 555 | sha256_hmac_reset_wrap, |
| 556 | sha256_hmac_wrap, |
| 557 | sha256_ctx_alloc, |
| 558 | sha256_ctx_free, |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 559 | }; |
| 560 | |
| 561 | #endif |
| 562 | |
| 563 | #if defined(POLARSSL_SHA4_C) |
| 564 | |
Paul Bakker | 1d073c5 | 2014-07-08 20:15:51 +0200 | [diff] [blame] | 565 | static void sha384_starts_wrap( void *ctx ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 566 | { |
| 567 | sha4_starts( (sha4_context *) ctx, 1 ); |
| 568 | } |
| 569 | |
Paul Bakker | 1d073c5 | 2014-07-08 20:15:51 +0200 | [diff] [blame] | 570 | static void sha384_update_wrap( void *ctx, const unsigned char *input, size_t ilen ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 571 | { |
| 572 | sha4_update( (sha4_context *) ctx, input, ilen ); |
| 573 | } |
| 574 | |
Paul Bakker | 1d073c5 | 2014-07-08 20:15:51 +0200 | [diff] [blame] | 575 | static void sha384_finish_wrap( void *ctx, unsigned char *output ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 576 | { |
| 577 | sha4_finish( (sha4_context *) ctx, output ); |
| 578 | } |
| 579 | |
Paul Bakker | 1d073c5 | 2014-07-08 20:15:51 +0200 | [diff] [blame] | 580 | static void sha384_wrap( const unsigned char *input, size_t ilen, |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 581 | unsigned char *output ) |
| 582 | { |
| 583 | sha4( input, ilen, output, 1 ); |
| 584 | } |
| 585 | |
Paul Bakker | 1d073c5 | 2014-07-08 20:15:51 +0200 | [diff] [blame] | 586 | static int sha384_file_wrap( const char *path, unsigned char *output ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 587 | { |
Paul Bakker | 335db3f | 2011-04-25 15:28:35 +0000 | [diff] [blame] | 588 | #if defined(POLARSSL_FS_IO) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 589 | return sha4_file( path, output, 1 ); |
Paul Bakker | 335db3f | 2011-04-25 15:28:35 +0000 | [diff] [blame] | 590 | #else |
| 591 | ((void) path); |
| 592 | ((void) output); |
| 593 | return POLARSSL_ERR_MD_FEATURE_UNAVAILABLE; |
| 594 | #endif |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 595 | } |
| 596 | |
Paul Bakker | 1d073c5 | 2014-07-08 20:15:51 +0200 | [diff] [blame] | 597 | static void sha384_hmac_starts_wrap( void *ctx, const unsigned char *key, size_t keylen ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 598 | { |
| 599 | sha4_hmac_starts( (sha4_context *) ctx, key, keylen, 1 ); |
| 600 | } |
| 601 | |
Paul Bakker | 1d073c5 | 2014-07-08 20:15:51 +0200 | [diff] [blame] | 602 | static void sha384_hmac_update_wrap( void *ctx, const unsigned char *input, size_t ilen ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 603 | { |
| 604 | sha4_hmac_update( (sha4_context *) ctx, input, ilen ); |
| 605 | } |
| 606 | |
Paul Bakker | 1d073c5 | 2014-07-08 20:15:51 +0200 | [diff] [blame] | 607 | static void sha384_hmac_finish_wrap( void *ctx, unsigned char *output ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 608 | { |
| 609 | sha4_hmac_finish( (sha4_context *) ctx, output ); |
| 610 | } |
| 611 | |
Paul Bakker | 1d073c5 | 2014-07-08 20:15:51 +0200 | [diff] [blame] | 612 | static void sha384_hmac_reset_wrap( void *ctx ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 613 | { |
| 614 | sha4_hmac_reset( (sha4_context *) ctx ); |
| 615 | } |
| 616 | |
Paul Bakker | 1d073c5 | 2014-07-08 20:15:51 +0200 | [diff] [blame] | 617 | static void sha384_hmac_wrap( const unsigned char *key, size_t keylen, |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 618 | const unsigned char *input, size_t ilen, |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 619 | unsigned char *output ) |
| 620 | { |
| 621 | sha4_hmac( key, keylen, input, ilen, output, 1 ); |
| 622 | } |
| 623 | |
Paul Bakker | 1d073c5 | 2014-07-08 20:15:51 +0200 | [diff] [blame] | 624 | static void * sha384_ctx_alloc( void ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 625 | { |
| 626 | return malloc( sizeof( sha4_context ) ); |
| 627 | } |
| 628 | |
Paul Bakker | 1d073c5 | 2014-07-08 20:15:51 +0200 | [diff] [blame] | 629 | static void sha384_ctx_free( void *ctx ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 630 | { |
Paul Bakker | 312da33 | 2014-06-13 17:20:13 +0200 | [diff] [blame] | 631 | polarssl_zeroize( ctx, sizeof( sha4_context ) ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 632 | free( ctx ); |
| 633 | } |
| 634 | |
| 635 | const md_info_t sha384_info = { |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 636 | POLARSSL_MD_SHA384, |
| 637 | "SHA384", |
| 638 | 48, |
| 639 | sha384_starts_wrap, |
| 640 | sha384_update_wrap, |
| 641 | sha384_finish_wrap, |
| 642 | sha384_wrap, |
| 643 | sha384_file_wrap, |
| 644 | sha384_hmac_starts_wrap, |
| 645 | sha384_hmac_update_wrap, |
| 646 | sha384_hmac_finish_wrap, |
| 647 | sha384_hmac_reset_wrap, |
| 648 | sha384_hmac_wrap, |
| 649 | sha384_ctx_alloc, |
| 650 | sha384_ctx_free, |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 651 | }; |
| 652 | |
Paul Bakker | 1d073c5 | 2014-07-08 20:15:51 +0200 | [diff] [blame] | 653 | static void sha512_starts_wrap( void *ctx ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 654 | { |
| 655 | sha4_starts( (sha4_context *) ctx, 0 ); |
| 656 | } |
| 657 | |
Paul Bakker | 1d073c5 | 2014-07-08 20:15:51 +0200 | [diff] [blame] | 658 | static void sha512_update_wrap( void *ctx, const unsigned char *input, size_t ilen ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 659 | { |
| 660 | sha4_update( (sha4_context *) ctx, input, ilen ); |
| 661 | } |
| 662 | |
Paul Bakker | 1d073c5 | 2014-07-08 20:15:51 +0200 | [diff] [blame] | 663 | static void sha512_finish_wrap( void *ctx, unsigned char *output ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 664 | { |
| 665 | sha4_finish( (sha4_context *) ctx, output ); |
| 666 | } |
| 667 | |
Paul Bakker | 1d073c5 | 2014-07-08 20:15:51 +0200 | [diff] [blame] | 668 | static void sha512_wrap( const unsigned char *input, size_t ilen, |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 669 | unsigned char *output ) |
| 670 | { |
| 671 | sha4( input, ilen, output, 0 ); |
| 672 | } |
| 673 | |
Paul Bakker | 1d073c5 | 2014-07-08 20:15:51 +0200 | [diff] [blame] | 674 | static int sha512_file_wrap( const char *path, unsigned char *output ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 675 | { |
Paul Bakker | 335db3f | 2011-04-25 15:28:35 +0000 | [diff] [blame] | 676 | #if defined(POLARSSL_FS_IO) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 677 | return sha4_file( path, output, 0 ); |
Paul Bakker | 335db3f | 2011-04-25 15:28:35 +0000 | [diff] [blame] | 678 | #else |
| 679 | ((void) path); |
| 680 | ((void) output); |
| 681 | return POLARSSL_ERR_MD_FEATURE_UNAVAILABLE; |
| 682 | #endif |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 683 | } |
| 684 | |
Paul Bakker | 1d073c5 | 2014-07-08 20:15:51 +0200 | [diff] [blame] | 685 | static void sha512_hmac_starts_wrap( void *ctx, const unsigned char *key, size_t keylen ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 686 | { |
| 687 | sha4_hmac_starts( (sha4_context *) ctx, key, keylen, 0 ); |
| 688 | } |
| 689 | |
Paul Bakker | 1d073c5 | 2014-07-08 20:15:51 +0200 | [diff] [blame] | 690 | static void sha512_hmac_update_wrap( void *ctx, const unsigned char *input, size_t ilen ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 691 | { |
| 692 | sha4_hmac_update( (sha4_context *) ctx, input, ilen ); |
| 693 | } |
| 694 | |
Paul Bakker | 1d073c5 | 2014-07-08 20:15:51 +0200 | [diff] [blame] | 695 | static void sha512_hmac_finish_wrap( void *ctx, unsigned char *output ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 696 | { |
| 697 | sha4_hmac_finish( (sha4_context *) ctx, output ); |
| 698 | } |
| 699 | |
Paul Bakker | 1d073c5 | 2014-07-08 20:15:51 +0200 | [diff] [blame] | 700 | static void sha512_hmac_reset_wrap( void *ctx ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 701 | { |
| 702 | sha4_hmac_reset( (sha4_context *) ctx ); |
| 703 | } |
| 704 | |
Paul Bakker | 1d073c5 | 2014-07-08 20:15:51 +0200 | [diff] [blame] | 705 | static void sha512_hmac_wrap( const unsigned char *key, size_t keylen, |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 706 | const unsigned char *input, size_t ilen, |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 707 | unsigned char *output ) |
| 708 | { |
| 709 | sha4_hmac( key, keylen, input, ilen, output, 0 ); |
| 710 | } |
| 711 | |
Paul Bakker | 1d073c5 | 2014-07-08 20:15:51 +0200 | [diff] [blame] | 712 | static void * sha512_ctx_alloc( void ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 713 | { |
| 714 | return malloc( sizeof( sha4_context ) ); |
| 715 | } |
| 716 | |
Paul Bakker | 1d073c5 | 2014-07-08 20:15:51 +0200 | [diff] [blame] | 717 | static void sha512_ctx_free( void *ctx ) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 718 | { |
Paul Bakker | 312da33 | 2014-06-13 17:20:13 +0200 | [diff] [blame] | 719 | polarssl_zeroize( ctx, sizeof( sha4_context ) ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 720 | free( ctx ); |
| 721 | } |
| 722 | |
| 723 | const md_info_t sha512_info = { |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 724 | POLARSSL_MD_SHA512, |
| 725 | "SHA512", |
| 726 | 64, |
| 727 | sha512_starts_wrap, |
| 728 | sha512_update_wrap, |
| 729 | sha512_finish_wrap, |
| 730 | sha512_wrap, |
| 731 | sha512_file_wrap, |
| 732 | sha512_hmac_starts_wrap, |
| 733 | sha512_hmac_update_wrap, |
| 734 | sha512_hmac_finish_wrap, |
| 735 | sha512_hmac_reset_wrap, |
| 736 | sha512_hmac_wrap, |
| 737 | sha512_ctx_alloc, |
| 738 | sha512_ctx_free, |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 739 | }; |
| 740 | |
| 741 | #endif |
| 742 | |
| 743 | #endif |