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