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