Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 1 | /** |
| 2 | * \file md.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.h" |
| 35 | #include "polarssl/md_wrap.h" |
| 36 | |
| 37 | #include <string.h> |
| 38 | #include <stdlib.h> |
| 39 | |
| 40 | const md_info_t *md_info_from_string( const char *md_name ) |
| 41 | { |
| 42 | if( NULL == md_name ) |
| 43 | return NULL; |
| 44 | |
| 45 | /* Get the appropriate digest information */ |
| 46 | #if defined(POLARSSL_MD2_C) |
| 47 | if( !strcasecmp( "MD2", md_name ) ) |
| 48 | return md_info_from_type( POLARSSL_MD_MD2 ); |
| 49 | #endif |
| 50 | #if defined(POLARSSL_MD4_C) |
| 51 | if( !strcasecmp( "MD4", md_name ) ) |
| 52 | return md_info_from_type( POLARSSL_MD_MD4 ); |
| 53 | #endif |
| 54 | #if defined(POLARSSL_MD5_C) |
| 55 | if( !strcasecmp( "MD5", md_name ) ) |
| 56 | return md_info_from_type( POLARSSL_MD_MD5 ); |
| 57 | #endif |
| 58 | #if defined(POLARSSL_SHA1_C) |
| 59 | if( !strcasecmp( "SHA1", md_name ) || !strcasecmp( "SHA", md_name ) ) |
| 60 | return md_info_from_type( POLARSSL_MD_SHA1 ); |
| 61 | #endif |
| 62 | #if defined(POLARSSL_SHA2_C) |
| 63 | if( !strcasecmp( "SHA224", md_name ) ) |
| 64 | return md_info_from_type( POLARSSL_MD_SHA224 ); |
| 65 | if( !strcasecmp( "SHA256", md_name ) ) |
| 66 | return md_info_from_type( POLARSSL_MD_SHA256 ); |
| 67 | #endif |
| 68 | #if defined(POLARSSL_SHA4_C) |
| 69 | if( !strcasecmp( "SHA384", md_name ) ) |
| 70 | return md_info_from_type( POLARSSL_MD_SHA384 ); |
| 71 | if( !strcasecmp( "SHA512", md_name ) ) |
| 72 | return md_info_from_type( POLARSSL_MD_SHA512 ); |
| 73 | #endif |
| 74 | return NULL; |
| 75 | } |
| 76 | |
| 77 | const md_info_t *md_info_from_type( md_type_t md_type ) |
| 78 | { |
| 79 | switch( md_type ) |
| 80 | { |
| 81 | #if defined(POLARSSL_MD2_C) |
| 82 | case POLARSSL_MD_MD2: |
| 83 | return &md2_info; |
| 84 | #endif |
| 85 | #if defined(POLARSSL_MD4_C) |
| 86 | case POLARSSL_MD_MD4: |
| 87 | return &md4_info; |
| 88 | #endif |
| 89 | #if defined(POLARSSL_MD5_C) |
| 90 | case POLARSSL_MD_MD5: |
| 91 | return &md5_info; |
| 92 | #endif |
| 93 | #if defined(POLARSSL_SHA1_C) |
| 94 | case POLARSSL_MD_SHA1: |
| 95 | return &sha1_info; |
| 96 | #endif |
| 97 | #if defined(POLARSSL_SHA2_C) |
| 98 | case POLARSSL_MD_SHA224: |
| 99 | return &sha224_info; |
| 100 | case POLARSSL_MD_SHA256: |
| 101 | return &sha256_info; |
| 102 | #endif |
| 103 | #if defined(POLARSSL_SHA4_C) |
| 104 | case POLARSSL_MD_SHA384: |
| 105 | return &sha384_info; |
| 106 | case POLARSSL_MD_SHA512: |
| 107 | return &sha512_info; |
| 108 | #endif |
| 109 | default: |
| 110 | return NULL; |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | int md_starts( const md_info_t *md_info, md_context_t *ctx ) |
| 115 | { |
| 116 | if( md_info == NULL ) |
| 117 | return 1; |
| 118 | |
| 119 | if( ctx == NULL || ctx->md_ctx != NULL ) |
| 120 | return 1; |
| 121 | |
| 122 | if( ( ctx->md_ctx = md_info->ctx_alloc_func() ) == NULL ) |
| 123 | return 1; |
| 124 | |
| 125 | ctx->md_info = md_info; |
| 126 | |
| 127 | md_info->starts_func( ctx->md_ctx ); |
| 128 | |
| 129 | return 0; |
| 130 | } |
| 131 | |
| 132 | int md_update( md_context_t *ctx, const unsigned char *input, int ilen ) |
| 133 | { |
| 134 | if( ctx == NULL || ctx->md_info == NULL ) |
| 135 | return 1; |
| 136 | |
| 137 | ctx->md_info->update_func( ctx->md_ctx, input, ilen ); |
| 138 | |
| 139 | return 0; |
| 140 | } |
| 141 | |
| 142 | int md_finish( md_context_t *ctx, unsigned char *output ) |
| 143 | { |
| 144 | if( ctx == NULL || ctx->md_info == NULL ) |
| 145 | return 1; |
| 146 | |
| 147 | ctx->md_info->finish_func( ctx->md_ctx, output ); |
| 148 | |
| 149 | return 0; |
| 150 | } |
| 151 | |
| 152 | int md_free_ctx( md_context_t *ctx ) |
| 153 | { |
| 154 | if( ctx == NULL || ctx->md_info == NULL ) |
| 155 | return 1; |
| 156 | |
| 157 | ctx->md_info->ctx_free_func( ctx->md_ctx ); |
| 158 | |
| 159 | return 0; |
| 160 | } |
| 161 | |
| 162 | int md( const md_info_t *md_info, const unsigned char *input, int ilen, |
| 163 | unsigned char *output ) |
| 164 | { |
| 165 | if ( md_info == NULL ) |
| 166 | return 1; |
| 167 | |
| 168 | md_info->digest_func( input, ilen, output ); |
| 169 | |
| 170 | return 0; |
| 171 | } |
| 172 | |
| 173 | int md_file( const md_info_t *md_info, const char *path, unsigned char *output ) |
| 174 | { |
| 175 | if( md_info == NULL ) |
| 176 | return 3; |
| 177 | |
| 178 | return md_info->file_func( path, output ); |
| 179 | } |
| 180 | |
| 181 | int md_hmac_starts( const md_info_t *md_info, md_context_t *ctx, |
| 182 | const unsigned char *key, int keylen ) |
| 183 | { |
| 184 | if(md_info == NULL ) |
| 185 | return 1; |
| 186 | |
| 187 | if( ctx == NULL || ctx->md_ctx != NULL ) |
| 188 | return 1; |
| 189 | |
| 190 | if( ( ctx->md_ctx = md_info->ctx_alloc_func() ) == NULL ) |
| 191 | return 1; |
| 192 | |
| 193 | ctx->md_info = md_info; |
| 194 | |
| 195 | md_info->hmac_starts_func( ctx->md_ctx, key, keylen); |
| 196 | |
| 197 | return 0; |
| 198 | } |
| 199 | |
| 200 | int md_hmac_update( md_context_t *ctx, const unsigned char *input, int ilen ) |
| 201 | { |
| 202 | if( ctx == NULL || ctx->md_info == NULL ) |
| 203 | return 1; |
| 204 | |
| 205 | ctx->md_info->hmac_update_func( ctx->md_ctx, input, ilen ); |
| 206 | |
| 207 | return 0; |
| 208 | } |
| 209 | |
| 210 | int md_hmac_finish( md_context_t *ctx, unsigned char *output) |
| 211 | { |
| 212 | if( ctx == NULL || ctx->md_info == NULL ) |
| 213 | return 1; |
| 214 | |
| 215 | ctx->md_info->hmac_finish_func( ctx->md_ctx, output); |
| 216 | |
| 217 | return 0; |
| 218 | } |
| 219 | |
| 220 | int md_hmac_reset( md_context_t *ctx ) |
| 221 | { |
| 222 | if( ctx == NULL || ctx->md_info == NULL ) |
| 223 | return 1; |
| 224 | |
| 225 | ctx->md_info->hmac_reset_func( ctx->md_ctx); |
| 226 | |
| 227 | return 0; |
| 228 | } |
| 229 | |
| 230 | int md_hmac( const md_info_t *md_info, const unsigned char *key, int keylen, |
| 231 | const unsigned char *input, int ilen, |
| 232 | unsigned char *output ) |
| 233 | { |
| 234 | if( md_info == NULL ) |
| 235 | return 1; |
| 236 | |
| 237 | md_info->hmac_func( key, keylen, input, ilen, output ); |
| 238 | |
| 239 | return 0; |
| 240 | } |
| 241 | |
| 242 | #endif |