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