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