Preliminary main mbed library for nexpaq development
features/mbedtls/src/md.c@0:6c56fb4bc5f0, 2016-11-04 (annotated)
- Committer:
- nexpaq
- Date:
- Fri Nov 04 20:27:58 2016 +0000
- Revision:
- 0:6c56fb4bc5f0
Moving to library for sharing updates
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
nexpaq | 0:6c56fb4bc5f0 | 1 | /** |
nexpaq | 0:6c56fb4bc5f0 | 2 | * \file mbedtls_md.c |
nexpaq | 0:6c56fb4bc5f0 | 3 | * |
nexpaq | 0:6c56fb4bc5f0 | 4 | * \brief Generic message digest wrapper for mbed TLS |
nexpaq | 0:6c56fb4bc5f0 | 5 | * |
nexpaq | 0:6c56fb4bc5f0 | 6 | * \author Adriaan de Jong <dejong@fox-it.com> |
nexpaq | 0:6c56fb4bc5f0 | 7 | * |
nexpaq | 0:6c56fb4bc5f0 | 8 | * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved |
nexpaq | 0:6c56fb4bc5f0 | 9 | * SPDX-License-Identifier: Apache-2.0 |
nexpaq | 0:6c56fb4bc5f0 | 10 | * |
nexpaq | 0:6c56fb4bc5f0 | 11 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
nexpaq | 0:6c56fb4bc5f0 | 12 | * not use this file except in compliance with the License. |
nexpaq | 0:6c56fb4bc5f0 | 13 | * You may obtain a copy of the License at |
nexpaq | 0:6c56fb4bc5f0 | 14 | * |
nexpaq | 0:6c56fb4bc5f0 | 15 | * http://www.apache.org/licenses/LICENSE-2.0 |
nexpaq | 0:6c56fb4bc5f0 | 16 | * |
nexpaq | 0:6c56fb4bc5f0 | 17 | * Unless required by applicable law or agreed to in writing, software |
nexpaq | 0:6c56fb4bc5f0 | 18 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
nexpaq | 0:6c56fb4bc5f0 | 19 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
nexpaq | 0:6c56fb4bc5f0 | 20 | * See the License for the specific language governing permissions and |
nexpaq | 0:6c56fb4bc5f0 | 21 | * limitations under the License. |
nexpaq | 0:6c56fb4bc5f0 | 22 | * |
nexpaq | 0:6c56fb4bc5f0 | 23 | * This file is part of mbed TLS (https://tls.mbed.org) |
nexpaq | 0:6c56fb4bc5f0 | 24 | */ |
nexpaq | 0:6c56fb4bc5f0 | 25 | |
nexpaq | 0:6c56fb4bc5f0 | 26 | #if !defined(MBEDTLS_CONFIG_FILE) |
nexpaq | 0:6c56fb4bc5f0 | 27 | #include "mbedtls/config.h" |
nexpaq | 0:6c56fb4bc5f0 | 28 | #else |
nexpaq | 0:6c56fb4bc5f0 | 29 | #include MBEDTLS_CONFIG_FILE |
nexpaq | 0:6c56fb4bc5f0 | 30 | #endif |
nexpaq | 0:6c56fb4bc5f0 | 31 | |
nexpaq | 0:6c56fb4bc5f0 | 32 | #if defined(MBEDTLS_MD_C) |
nexpaq | 0:6c56fb4bc5f0 | 33 | |
nexpaq | 0:6c56fb4bc5f0 | 34 | #include "mbedtls/md.h" |
nexpaq | 0:6c56fb4bc5f0 | 35 | #include "mbedtls/md_internal.h" |
nexpaq | 0:6c56fb4bc5f0 | 36 | |
nexpaq | 0:6c56fb4bc5f0 | 37 | #if defined(MBEDTLS_PLATFORM_C) |
nexpaq | 0:6c56fb4bc5f0 | 38 | #include "mbedtls/platform.h" |
nexpaq | 0:6c56fb4bc5f0 | 39 | #else |
nexpaq | 0:6c56fb4bc5f0 | 40 | #include <stdlib.h> |
nexpaq | 0:6c56fb4bc5f0 | 41 | #define mbedtls_calloc calloc |
nexpaq | 0:6c56fb4bc5f0 | 42 | #define mbedtls_free free |
nexpaq | 0:6c56fb4bc5f0 | 43 | #endif |
nexpaq | 0:6c56fb4bc5f0 | 44 | |
nexpaq | 0:6c56fb4bc5f0 | 45 | #include <string.h> |
nexpaq | 0:6c56fb4bc5f0 | 46 | |
nexpaq | 0:6c56fb4bc5f0 | 47 | #if defined(MBEDTLS_FS_IO) |
nexpaq | 0:6c56fb4bc5f0 | 48 | #include <stdio.h> |
nexpaq | 0:6c56fb4bc5f0 | 49 | #endif |
nexpaq | 0:6c56fb4bc5f0 | 50 | |
nexpaq | 0:6c56fb4bc5f0 | 51 | /* Implementation that should never be optimized out by the compiler */ |
nexpaq | 0:6c56fb4bc5f0 | 52 | static void mbedtls_zeroize( void *v, size_t n ) { |
nexpaq | 0:6c56fb4bc5f0 | 53 | volatile unsigned char *p = v; while( n-- ) *p++ = 0; |
nexpaq | 0:6c56fb4bc5f0 | 54 | } |
nexpaq | 0:6c56fb4bc5f0 | 55 | |
nexpaq | 0:6c56fb4bc5f0 | 56 | /* |
nexpaq | 0:6c56fb4bc5f0 | 57 | * Reminder: update profiles in x509_crt.c when adding a new hash! |
nexpaq | 0:6c56fb4bc5f0 | 58 | */ |
nexpaq | 0:6c56fb4bc5f0 | 59 | static const int supported_digests[] = { |
nexpaq | 0:6c56fb4bc5f0 | 60 | |
nexpaq | 0:6c56fb4bc5f0 | 61 | #if defined(MBEDTLS_SHA512_C) |
nexpaq | 0:6c56fb4bc5f0 | 62 | MBEDTLS_MD_SHA512, |
nexpaq | 0:6c56fb4bc5f0 | 63 | MBEDTLS_MD_SHA384, |
nexpaq | 0:6c56fb4bc5f0 | 64 | #endif |
nexpaq | 0:6c56fb4bc5f0 | 65 | |
nexpaq | 0:6c56fb4bc5f0 | 66 | #if defined(MBEDTLS_SHA256_C) |
nexpaq | 0:6c56fb4bc5f0 | 67 | MBEDTLS_MD_SHA256, |
nexpaq | 0:6c56fb4bc5f0 | 68 | MBEDTLS_MD_SHA224, |
nexpaq | 0:6c56fb4bc5f0 | 69 | #endif |
nexpaq | 0:6c56fb4bc5f0 | 70 | |
nexpaq | 0:6c56fb4bc5f0 | 71 | #if defined(MBEDTLS_SHA1_C) |
nexpaq | 0:6c56fb4bc5f0 | 72 | MBEDTLS_MD_SHA1, |
nexpaq | 0:6c56fb4bc5f0 | 73 | #endif |
nexpaq | 0:6c56fb4bc5f0 | 74 | |
nexpaq | 0:6c56fb4bc5f0 | 75 | #if defined(MBEDTLS_RIPEMD160_C) |
nexpaq | 0:6c56fb4bc5f0 | 76 | MBEDTLS_MD_RIPEMD160, |
nexpaq | 0:6c56fb4bc5f0 | 77 | #endif |
nexpaq | 0:6c56fb4bc5f0 | 78 | |
nexpaq | 0:6c56fb4bc5f0 | 79 | #if defined(MBEDTLS_MD5_C) |
nexpaq | 0:6c56fb4bc5f0 | 80 | MBEDTLS_MD_MD5, |
nexpaq | 0:6c56fb4bc5f0 | 81 | #endif |
nexpaq | 0:6c56fb4bc5f0 | 82 | |
nexpaq | 0:6c56fb4bc5f0 | 83 | #if defined(MBEDTLS_MD4_C) |
nexpaq | 0:6c56fb4bc5f0 | 84 | MBEDTLS_MD_MD4, |
nexpaq | 0:6c56fb4bc5f0 | 85 | #endif |
nexpaq | 0:6c56fb4bc5f0 | 86 | |
nexpaq | 0:6c56fb4bc5f0 | 87 | #if defined(MBEDTLS_MD2_C) |
nexpaq | 0:6c56fb4bc5f0 | 88 | MBEDTLS_MD_MD2, |
nexpaq | 0:6c56fb4bc5f0 | 89 | #endif |
nexpaq | 0:6c56fb4bc5f0 | 90 | |
nexpaq | 0:6c56fb4bc5f0 | 91 | MBEDTLS_MD_NONE |
nexpaq | 0:6c56fb4bc5f0 | 92 | }; |
nexpaq | 0:6c56fb4bc5f0 | 93 | |
nexpaq | 0:6c56fb4bc5f0 | 94 | const int *mbedtls_md_list( void ) |
nexpaq | 0:6c56fb4bc5f0 | 95 | { |
nexpaq | 0:6c56fb4bc5f0 | 96 | return( supported_digests ); |
nexpaq | 0:6c56fb4bc5f0 | 97 | } |
nexpaq | 0:6c56fb4bc5f0 | 98 | |
nexpaq | 0:6c56fb4bc5f0 | 99 | const mbedtls_md_info_t *mbedtls_md_info_from_string( const char *md_name ) |
nexpaq | 0:6c56fb4bc5f0 | 100 | { |
nexpaq | 0:6c56fb4bc5f0 | 101 | if( NULL == md_name ) |
nexpaq | 0:6c56fb4bc5f0 | 102 | return( NULL ); |
nexpaq | 0:6c56fb4bc5f0 | 103 | |
nexpaq | 0:6c56fb4bc5f0 | 104 | /* Get the appropriate digest information */ |
nexpaq | 0:6c56fb4bc5f0 | 105 | #if defined(MBEDTLS_MD2_C) |
nexpaq | 0:6c56fb4bc5f0 | 106 | if( !strcmp( "MD2", md_name ) ) |
nexpaq | 0:6c56fb4bc5f0 | 107 | return mbedtls_md_info_from_type( MBEDTLS_MD_MD2 ); |
nexpaq | 0:6c56fb4bc5f0 | 108 | #endif |
nexpaq | 0:6c56fb4bc5f0 | 109 | #if defined(MBEDTLS_MD4_C) |
nexpaq | 0:6c56fb4bc5f0 | 110 | if( !strcmp( "MD4", md_name ) ) |
nexpaq | 0:6c56fb4bc5f0 | 111 | return mbedtls_md_info_from_type( MBEDTLS_MD_MD4 ); |
nexpaq | 0:6c56fb4bc5f0 | 112 | #endif |
nexpaq | 0:6c56fb4bc5f0 | 113 | #if defined(MBEDTLS_MD5_C) |
nexpaq | 0:6c56fb4bc5f0 | 114 | if( !strcmp( "MD5", md_name ) ) |
nexpaq | 0:6c56fb4bc5f0 | 115 | return mbedtls_md_info_from_type( MBEDTLS_MD_MD5 ); |
nexpaq | 0:6c56fb4bc5f0 | 116 | #endif |
nexpaq | 0:6c56fb4bc5f0 | 117 | #if defined(MBEDTLS_RIPEMD160_C) |
nexpaq | 0:6c56fb4bc5f0 | 118 | if( !strcmp( "RIPEMD160", md_name ) ) |
nexpaq | 0:6c56fb4bc5f0 | 119 | return mbedtls_md_info_from_type( MBEDTLS_MD_RIPEMD160 ); |
nexpaq | 0:6c56fb4bc5f0 | 120 | #endif |
nexpaq | 0:6c56fb4bc5f0 | 121 | #if defined(MBEDTLS_SHA1_C) |
nexpaq | 0:6c56fb4bc5f0 | 122 | if( !strcmp( "SHA1", md_name ) || !strcmp( "SHA", md_name ) ) |
nexpaq | 0:6c56fb4bc5f0 | 123 | return mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 ); |
nexpaq | 0:6c56fb4bc5f0 | 124 | #endif |
nexpaq | 0:6c56fb4bc5f0 | 125 | #if defined(MBEDTLS_SHA256_C) |
nexpaq | 0:6c56fb4bc5f0 | 126 | if( !strcmp( "SHA224", md_name ) ) |
nexpaq | 0:6c56fb4bc5f0 | 127 | return mbedtls_md_info_from_type( MBEDTLS_MD_SHA224 ); |
nexpaq | 0:6c56fb4bc5f0 | 128 | if( !strcmp( "SHA256", md_name ) ) |
nexpaq | 0:6c56fb4bc5f0 | 129 | return mbedtls_md_info_from_type( MBEDTLS_MD_SHA256 ); |
nexpaq | 0:6c56fb4bc5f0 | 130 | #endif |
nexpaq | 0:6c56fb4bc5f0 | 131 | #if defined(MBEDTLS_SHA512_C) |
nexpaq | 0:6c56fb4bc5f0 | 132 | if( !strcmp( "SHA384", md_name ) ) |
nexpaq | 0:6c56fb4bc5f0 | 133 | return mbedtls_md_info_from_type( MBEDTLS_MD_SHA384 ); |
nexpaq | 0:6c56fb4bc5f0 | 134 | if( !strcmp( "SHA512", md_name ) ) |
nexpaq | 0:6c56fb4bc5f0 | 135 | return mbedtls_md_info_from_type( MBEDTLS_MD_SHA512 ); |
nexpaq | 0:6c56fb4bc5f0 | 136 | #endif |
nexpaq | 0:6c56fb4bc5f0 | 137 | return( NULL ); |
nexpaq | 0:6c56fb4bc5f0 | 138 | } |
nexpaq | 0:6c56fb4bc5f0 | 139 | |
nexpaq | 0:6c56fb4bc5f0 | 140 | const mbedtls_md_info_t *mbedtls_md_info_from_type( mbedtls_md_type_t md_type ) |
nexpaq | 0:6c56fb4bc5f0 | 141 | { |
nexpaq | 0:6c56fb4bc5f0 | 142 | switch( md_type ) |
nexpaq | 0:6c56fb4bc5f0 | 143 | { |
nexpaq | 0:6c56fb4bc5f0 | 144 | #if defined(MBEDTLS_MD2_C) |
nexpaq | 0:6c56fb4bc5f0 | 145 | case MBEDTLS_MD_MD2: |
nexpaq | 0:6c56fb4bc5f0 | 146 | return( &mbedtls_md2_info ); |
nexpaq | 0:6c56fb4bc5f0 | 147 | #endif |
nexpaq | 0:6c56fb4bc5f0 | 148 | #if defined(MBEDTLS_MD4_C) |
nexpaq | 0:6c56fb4bc5f0 | 149 | case MBEDTLS_MD_MD4: |
nexpaq | 0:6c56fb4bc5f0 | 150 | return( &mbedtls_md4_info ); |
nexpaq | 0:6c56fb4bc5f0 | 151 | #endif |
nexpaq | 0:6c56fb4bc5f0 | 152 | #if defined(MBEDTLS_MD5_C) |
nexpaq | 0:6c56fb4bc5f0 | 153 | case MBEDTLS_MD_MD5: |
nexpaq | 0:6c56fb4bc5f0 | 154 | return( &mbedtls_md5_info ); |
nexpaq | 0:6c56fb4bc5f0 | 155 | #endif |
nexpaq | 0:6c56fb4bc5f0 | 156 | #if defined(MBEDTLS_RIPEMD160_C) |
nexpaq | 0:6c56fb4bc5f0 | 157 | case MBEDTLS_MD_RIPEMD160: |
nexpaq | 0:6c56fb4bc5f0 | 158 | return( &mbedtls_ripemd160_info ); |
nexpaq | 0:6c56fb4bc5f0 | 159 | #endif |
nexpaq | 0:6c56fb4bc5f0 | 160 | #if defined(MBEDTLS_SHA1_C) |
nexpaq | 0:6c56fb4bc5f0 | 161 | case MBEDTLS_MD_SHA1: |
nexpaq | 0:6c56fb4bc5f0 | 162 | return( &mbedtls_sha1_info ); |
nexpaq | 0:6c56fb4bc5f0 | 163 | #endif |
nexpaq | 0:6c56fb4bc5f0 | 164 | #if defined(MBEDTLS_SHA256_C) |
nexpaq | 0:6c56fb4bc5f0 | 165 | case MBEDTLS_MD_SHA224: |
nexpaq | 0:6c56fb4bc5f0 | 166 | return( &mbedtls_sha224_info ); |
nexpaq | 0:6c56fb4bc5f0 | 167 | case MBEDTLS_MD_SHA256: |
nexpaq | 0:6c56fb4bc5f0 | 168 | return( &mbedtls_sha256_info ); |
nexpaq | 0:6c56fb4bc5f0 | 169 | #endif |
nexpaq | 0:6c56fb4bc5f0 | 170 | #if defined(MBEDTLS_SHA512_C) |
nexpaq | 0:6c56fb4bc5f0 | 171 | case MBEDTLS_MD_SHA384: |
nexpaq | 0:6c56fb4bc5f0 | 172 | return( &mbedtls_sha384_info ); |
nexpaq | 0:6c56fb4bc5f0 | 173 | case MBEDTLS_MD_SHA512: |
nexpaq | 0:6c56fb4bc5f0 | 174 | return( &mbedtls_sha512_info ); |
nexpaq | 0:6c56fb4bc5f0 | 175 | #endif |
nexpaq | 0:6c56fb4bc5f0 | 176 | default: |
nexpaq | 0:6c56fb4bc5f0 | 177 | return( NULL ); |
nexpaq | 0:6c56fb4bc5f0 | 178 | } |
nexpaq | 0:6c56fb4bc5f0 | 179 | } |
nexpaq | 0:6c56fb4bc5f0 | 180 | |
nexpaq | 0:6c56fb4bc5f0 | 181 | void mbedtls_md_init( mbedtls_md_context_t *ctx ) |
nexpaq | 0:6c56fb4bc5f0 | 182 | { |
nexpaq | 0:6c56fb4bc5f0 | 183 | memset( ctx, 0, sizeof( mbedtls_md_context_t ) ); |
nexpaq | 0:6c56fb4bc5f0 | 184 | } |
nexpaq | 0:6c56fb4bc5f0 | 185 | |
nexpaq | 0:6c56fb4bc5f0 | 186 | void mbedtls_md_free( mbedtls_md_context_t *ctx ) |
nexpaq | 0:6c56fb4bc5f0 | 187 | { |
nexpaq | 0:6c56fb4bc5f0 | 188 | if( ctx == NULL || ctx->md_info == NULL ) |
nexpaq | 0:6c56fb4bc5f0 | 189 | return; |
nexpaq | 0:6c56fb4bc5f0 | 190 | |
nexpaq | 0:6c56fb4bc5f0 | 191 | if( ctx->md_ctx != NULL ) |
nexpaq | 0:6c56fb4bc5f0 | 192 | ctx->md_info->ctx_free_func( ctx->md_ctx ); |
nexpaq | 0:6c56fb4bc5f0 | 193 | |
nexpaq | 0:6c56fb4bc5f0 | 194 | if( ctx->hmac_ctx != NULL ) |
nexpaq | 0:6c56fb4bc5f0 | 195 | { |
nexpaq | 0:6c56fb4bc5f0 | 196 | mbedtls_zeroize( ctx->hmac_ctx, 2 * ctx->md_info->block_size ); |
nexpaq | 0:6c56fb4bc5f0 | 197 | mbedtls_free( ctx->hmac_ctx ); |
nexpaq | 0:6c56fb4bc5f0 | 198 | } |
nexpaq | 0:6c56fb4bc5f0 | 199 | |
nexpaq | 0:6c56fb4bc5f0 | 200 | mbedtls_zeroize( ctx, sizeof( mbedtls_md_context_t ) ); |
nexpaq | 0:6c56fb4bc5f0 | 201 | } |
nexpaq | 0:6c56fb4bc5f0 | 202 | |
nexpaq | 0:6c56fb4bc5f0 | 203 | int mbedtls_md_clone( mbedtls_md_context_t *dst, |
nexpaq | 0:6c56fb4bc5f0 | 204 | const mbedtls_md_context_t *src ) |
nexpaq | 0:6c56fb4bc5f0 | 205 | { |
nexpaq | 0:6c56fb4bc5f0 | 206 | if( dst == NULL || dst->md_info == NULL || |
nexpaq | 0:6c56fb4bc5f0 | 207 | src == NULL || src->md_info == NULL || |
nexpaq | 0:6c56fb4bc5f0 | 208 | dst->md_info != src->md_info ) |
nexpaq | 0:6c56fb4bc5f0 | 209 | { |
nexpaq | 0:6c56fb4bc5f0 | 210 | return( MBEDTLS_ERR_MD_BAD_INPUT_DATA ); |
nexpaq | 0:6c56fb4bc5f0 | 211 | } |
nexpaq | 0:6c56fb4bc5f0 | 212 | |
nexpaq | 0:6c56fb4bc5f0 | 213 | dst->md_info->clone_func( dst->md_ctx, src->md_ctx ); |
nexpaq | 0:6c56fb4bc5f0 | 214 | |
nexpaq | 0:6c56fb4bc5f0 | 215 | return( 0 ); |
nexpaq | 0:6c56fb4bc5f0 | 216 | } |
nexpaq | 0:6c56fb4bc5f0 | 217 | |
nexpaq | 0:6c56fb4bc5f0 | 218 | #if ! defined(MBEDTLS_DEPRECATED_REMOVED) |
nexpaq | 0:6c56fb4bc5f0 | 219 | int mbedtls_md_init_ctx( mbedtls_md_context_t *ctx, const mbedtls_md_info_t *md_info ) |
nexpaq | 0:6c56fb4bc5f0 | 220 | { |
nexpaq | 0:6c56fb4bc5f0 | 221 | return mbedtls_md_setup( ctx, md_info, 1 ); |
nexpaq | 0:6c56fb4bc5f0 | 222 | } |
nexpaq | 0:6c56fb4bc5f0 | 223 | #endif |
nexpaq | 0:6c56fb4bc5f0 | 224 | |
nexpaq | 0:6c56fb4bc5f0 | 225 | int mbedtls_md_setup( mbedtls_md_context_t *ctx, const mbedtls_md_info_t *md_info, int hmac ) |
nexpaq | 0:6c56fb4bc5f0 | 226 | { |
nexpaq | 0:6c56fb4bc5f0 | 227 | if( md_info == NULL || ctx == NULL ) |
nexpaq | 0:6c56fb4bc5f0 | 228 | return( MBEDTLS_ERR_MD_BAD_INPUT_DATA ); |
nexpaq | 0:6c56fb4bc5f0 | 229 | |
nexpaq | 0:6c56fb4bc5f0 | 230 | if( ( ctx->md_ctx = md_info->ctx_alloc_func() ) == NULL ) |
nexpaq | 0:6c56fb4bc5f0 | 231 | return( MBEDTLS_ERR_MD_ALLOC_FAILED ); |
nexpaq | 0:6c56fb4bc5f0 | 232 | |
nexpaq | 0:6c56fb4bc5f0 | 233 | if( hmac != 0 ) |
nexpaq | 0:6c56fb4bc5f0 | 234 | { |
nexpaq | 0:6c56fb4bc5f0 | 235 | ctx->hmac_ctx = mbedtls_calloc( 2, md_info->block_size ); |
nexpaq | 0:6c56fb4bc5f0 | 236 | if( ctx->hmac_ctx == NULL ) |
nexpaq | 0:6c56fb4bc5f0 | 237 | { |
nexpaq | 0:6c56fb4bc5f0 | 238 | md_info->ctx_free_func( ctx->md_ctx ); |
nexpaq | 0:6c56fb4bc5f0 | 239 | return( MBEDTLS_ERR_MD_ALLOC_FAILED ); |
nexpaq | 0:6c56fb4bc5f0 | 240 | } |
nexpaq | 0:6c56fb4bc5f0 | 241 | } |
nexpaq | 0:6c56fb4bc5f0 | 242 | |
nexpaq | 0:6c56fb4bc5f0 | 243 | ctx->md_info = md_info; |
nexpaq | 0:6c56fb4bc5f0 | 244 | |
nexpaq | 0:6c56fb4bc5f0 | 245 | return( 0 ); |
nexpaq | 0:6c56fb4bc5f0 | 246 | } |
nexpaq | 0:6c56fb4bc5f0 | 247 | |
nexpaq | 0:6c56fb4bc5f0 | 248 | int mbedtls_md_starts( mbedtls_md_context_t *ctx ) |
nexpaq | 0:6c56fb4bc5f0 | 249 | { |
nexpaq | 0:6c56fb4bc5f0 | 250 | if( ctx == NULL || ctx->md_info == NULL ) |
nexpaq | 0:6c56fb4bc5f0 | 251 | return( MBEDTLS_ERR_MD_BAD_INPUT_DATA ); |
nexpaq | 0:6c56fb4bc5f0 | 252 | |
nexpaq | 0:6c56fb4bc5f0 | 253 | ctx->md_info->starts_func( ctx->md_ctx ); |
nexpaq | 0:6c56fb4bc5f0 | 254 | |
nexpaq | 0:6c56fb4bc5f0 | 255 | return( 0 ); |
nexpaq | 0:6c56fb4bc5f0 | 256 | } |
nexpaq | 0:6c56fb4bc5f0 | 257 | |
nexpaq | 0:6c56fb4bc5f0 | 258 | int mbedtls_md_update( mbedtls_md_context_t *ctx, const unsigned char *input, size_t ilen ) |
nexpaq | 0:6c56fb4bc5f0 | 259 | { |
nexpaq | 0:6c56fb4bc5f0 | 260 | if( ctx == NULL || ctx->md_info == NULL ) |
nexpaq | 0:6c56fb4bc5f0 | 261 | return( MBEDTLS_ERR_MD_BAD_INPUT_DATA ); |
nexpaq | 0:6c56fb4bc5f0 | 262 | |
nexpaq | 0:6c56fb4bc5f0 | 263 | ctx->md_info->update_func( ctx->md_ctx, input, ilen ); |
nexpaq | 0:6c56fb4bc5f0 | 264 | |
nexpaq | 0:6c56fb4bc5f0 | 265 | return( 0 ); |
nexpaq | 0:6c56fb4bc5f0 | 266 | } |
nexpaq | 0:6c56fb4bc5f0 | 267 | |
nexpaq | 0:6c56fb4bc5f0 | 268 | int mbedtls_md_finish( mbedtls_md_context_t *ctx, unsigned char *output ) |
nexpaq | 0:6c56fb4bc5f0 | 269 | { |
nexpaq | 0:6c56fb4bc5f0 | 270 | if( ctx == NULL || ctx->md_info == NULL ) |
nexpaq | 0:6c56fb4bc5f0 | 271 | return( MBEDTLS_ERR_MD_BAD_INPUT_DATA ); |
nexpaq | 0:6c56fb4bc5f0 | 272 | |
nexpaq | 0:6c56fb4bc5f0 | 273 | ctx->md_info->finish_func( ctx->md_ctx, output ); |
nexpaq | 0:6c56fb4bc5f0 | 274 | |
nexpaq | 0:6c56fb4bc5f0 | 275 | return( 0 ); |
nexpaq | 0:6c56fb4bc5f0 | 276 | } |
nexpaq | 0:6c56fb4bc5f0 | 277 | |
nexpaq | 0:6c56fb4bc5f0 | 278 | int mbedtls_md( const mbedtls_md_info_t *md_info, const unsigned char *input, size_t ilen, |
nexpaq | 0:6c56fb4bc5f0 | 279 | unsigned char *output ) |
nexpaq | 0:6c56fb4bc5f0 | 280 | { |
nexpaq | 0:6c56fb4bc5f0 | 281 | if( md_info == NULL ) |
nexpaq | 0:6c56fb4bc5f0 | 282 | return( MBEDTLS_ERR_MD_BAD_INPUT_DATA ); |
nexpaq | 0:6c56fb4bc5f0 | 283 | |
nexpaq | 0:6c56fb4bc5f0 | 284 | md_info->digest_func( input, ilen, output ); |
nexpaq | 0:6c56fb4bc5f0 | 285 | |
nexpaq | 0:6c56fb4bc5f0 | 286 | return( 0 ); |
nexpaq | 0:6c56fb4bc5f0 | 287 | } |
nexpaq | 0:6c56fb4bc5f0 | 288 | |
nexpaq | 0:6c56fb4bc5f0 | 289 | #if defined(MBEDTLS_FS_IO) |
nexpaq | 0:6c56fb4bc5f0 | 290 | int mbedtls_md_file( const mbedtls_md_info_t *md_info, const char *path, unsigned char *output ) |
nexpaq | 0:6c56fb4bc5f0 | 291 | { |
nexpaq | 0:6c56fb4bc5f0 | 292 | int ret; |
nexpaq | 0:6c56fb4bc5f0 | 293 | FILE *f; |
nexpaq | 0:6c56fb4bc5f0 | 294 | size_t n; |
nexpaq | 0:6c56fb4bc5f0 | 295 | mbedtls_md_context_t ctx; |
nexpaq | 0:6c56fb4bc5f0 | 296 | unsigned char buf[1024]; |
nexpaq | 0:6c56fb4bc5f0 | 297 | |
nexpaq | 0:6c56fb4bc5f0 | 298 | if( md_info == NULL ) |
nexpaq | 0:6c56fb4bc5f0 | 299 | return( MBEDTLS_ERR_MD_BAD_INPUT_DATA ); |
nexpaq | 0:6c56fb4bc5f0 | 300 | |
nexpaq | 0:6c56fb4bc5f0 | 301 | if( ( f = fopen( path, "rb" ) ) == NULL ) |
nexpaq | 0:6c56fb4bc5f0 | 302 | return( MBEDTLS_ERR_MD_FILE_IO_ERROR ); |
nexpaq | 0:6c56fb4bc5f0 | 303 | |
nexpaq | 0:6c56fb4bc5f0 | 304 | mbedtls_md_init( &ctx ); |
nexpaq | 0:6c56fb4bc5f0 | 305 | |
nexpaq | 0:6c56fb4bc5f0 | 306 | if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 ) |
nexpaq | 0:6c56fb4bc5f0 | 307 | goto cleanup; |
nexpaq | 0:6c56fb4bc5f0 | 308 | |
nexpaq | 0:6c56fb4bc5f0 | 309 | md_info->starts_func( ctx.md_ctx ); |
nexpaq | 0:6c56fb4bc5f0 | 310 | |
nexpaq | 0:6c56fb4bc5f0 | 311 | while( ( n = fread( buf, 1, sizeof( buf ), f ) ) > 0 ) |
nexpaq | 0:6c56fb4bc5f0 | 312 | md_info->update_func( ctx.md_ctx, buf, n ); |
nexpaq | 0:6c56fb4bc5f0 | 313 | |
nexpaq | 0:6c56fb4bc5f0 | 314 | if( ferror( f ) != 0 ) |
nexpaq | 0:6c56fb4bc5f0 | 315 | { |
nexpaq | 0:6c56fb4bc5f0 | 316 | ret = MBEDTLS_ERR_MD_FILE_IO_ERROR; |
nexpaq | 0:6c56fb4bc5f0 | 317 | goto cleanup; |
nexpaq | 0:6c56fb4bc5f0 | 318 | } |
nexpaq | 0:6c56fb4bc5f0 | 319 | |
nexpaq | 0:6c56fb4bc5f0 | 320 | md_info->finish_func( ctx.md_ctx, output ); |
nexpaq | 0:6c56fb4bc5f0 | 321 | |
nexpaq | 0:6c56fb4bc5f0 | 322 | cleanup: |
nexpaq | 0:6c56fb4bc5f0 | 323 | fclose( f ); |
nexpaq | 0:6c56fb4bc5f0 | 324 | mbedtls_md_free( &ctx ); |
nexpaq | 0:6c56fb4bc5f0 | 325 | |
nexpaq | 0:6c56fb4bc5f0 | 326 | return( ret ); |
nexpaq | 0:6c56fb4bc5f0 | 327 | } |
nexpaq | 0:6c56fb4bc5f0 | 328 | #endif /* MBEDTLS_FS_IO */ |
nexpaq | 0:6c56fb4bc5f0 | 329 | |
nexpaq | 0:6c56fb4bc5f0 | 330 | int mbedtls_md_hmac_starts( mbedtls_md_context_t *ctx, const unsigned char *key, size_t keylen ) |
nexpaq | 0:6c56fb4bc5f0 | 331 | { |
nexpaq | 0:6c56fb4bc5f0 | 332 | unsigned char sum[MBEDTLS_MD_MAX_SIZE]; |
nexpaq | 0:6c56fb4bc5f0 | 333 | unsigned char *ipad, *opad; |
nexpaq | 0:6c56fb4bc5f0 | 334 | size_t i; |
nexpaq | 0:6c56fb4bc5f0 | 335 | |
nexpaq | 0:6c56fb4bc5f0 | 336 | if( ctx == NULL || ctx->md_info == NULL || ctx->hmac_ctx == NULL ) |
nexpaq | 0:6c56fb4bc5f0 | 337 | return( MBEDTLS_ERR_MD_BAD_INPUT_DATA ); |
nexpaq | 0:6c56fb4bc5f0 | 338 | |
nexpaq | 0:6c56fb4bc5f0 | 339 | if( keylen > (size_t) ctx->md_info->block_size ) |
nexpaq | 0:6c56fb4bc5f0 | 340 | { |
nexpaq | 0:6c56fb4bc5f0 | 341 | ctx->md_info->starts_func( ctx->md_ctx ); |
nexpaq | 0:6c56fb4bc5f0 | 342 | ctx->md_info->update_func( ctx->md_ctx, key, keylen ); |
nexpaq | 0:6c56fb4bc5f0 | 343 | ctx->md_info->finish_func( ctx->md_ctx, sum ); |
nexpaq | 0:6c56fb4bc5f0 | 344 | |
nexpaq | 0:6c56fb4bc5f0 | 345 | keylen = ctx->md_info->size; |
nexpaq | 0:6c56fb4bc5f0 | 346 | key = sum; |
nexpaq | 0:6c56fb4bc5f0 | 347 | } |
nexpaq | 0:6c56fb4bc5f0 | 348 | |
nexpaq | 0:6c56fb4bc5f0 | 349 | ipad = (unsigned char *) ctx->hmac_ctx; |
nexpaq | 0:6c56fb4bc5f0 | 350 | opad = (unsigned char *) ctx->hmac_ctx + ctx->md_info->block_size; |
nexpaq | 0:6c56fb4bc5f0 | 351 | |
nexpaq | 0:6c56fb4bc5f0 | 352 | memset( ipad, 0x36, ctx->md_info->block_size ); |
nexpaq | 0:6c56fb4bc5f0 | 353 | memset( opad, 0x5C, ctx->md_info->block_size ); |
nexpaq | 0:6c56fb4bc5f0 | 354 | |
nexpaq | 0:6c56fb4bc5f0 | 355 | for( i = 0; i < keylen; i++ ) |
nexpaq | 0:6c56fb4bc5f0 | 356 | { |
nexpaq | 0:6c56fb4bc5f0 | 357 | ipad[i] = (unsigned char)( ipad[i] ^ key[i] ); |
nexpaq | 0:6c56fb4bc5f0 | 358 | opad[i] = (unsigned char)( opad[i] ^ key[i] ); |
nexpaq | 0:6c56fb4bc5f0 | 359 | } |
nexpaq | 0:6c56fb4bc5f0 | 360 | |
nexpaq | 0:6c56fb4bc5f0 | 361 | mbedtls_zeroize( sum, sizeof( sum ) ); |
nexpaq | 0:6c56fb4bc5f0 | 362 | |
nexpaq | 0:6c56fb4bc5f0 | 363 | ctx->md_info->starts_func( ctx->md_ctx ); |
nexpaq | 0:6c56fb4bc5f0 | 364 | ctx->md_info->update_func( ctx->md_ctx, ipad, ctx->md_info->block_size ); |
nexpaq | 0:6c56fb4bc5f0 | 365 | |
nexpaq | 0:6c56fb4bc5f0 | 366 | return( 0 ); |
nexpaq | 0:6c56fb4bc5f0 | 367 | } |
nexpaq | 0:6c56fb4bc5f0 | 368 | |
nexpaq | 0:6c56fb4bc5f0 | 369 | int mbedtls_md_hmac_update( mbedtls_md_context_t *ctx, const unsigned char *input, size_t ilen ) |
nexpaq | 0:6c56fb4bc5f0 | 370 | { |
nexpaq | 0:6c56fb4bc5f0 | 371 | if( ctx == NULL || ctx->md_info == NULL || ctx->hmac_ctx == NULL ) |
nexpaq | 0:6c56fb4bc5f0 | 372 | return( MBEDTLS_ERR_MD_BAD_INPUT_DATA ); |
nexpaq | 0:6c56fb4bc5f0 | 373 | |
nexpaq | 0:6c56fb4bc5f0 | 374 | ctx->md_info->update_func( ctx->md_ctx, input, ilen ); |
nexpaq | 0:6c56fb4bc5f0 | 375 | |
nexpaq | 0:6c56fb4bc5f0 | 376 | return( 0 ); |
nexpaq | 0:6c56fb4bc5f0 | 377 | } |
nexpaq | 0:6c56fb4bc5f0 | 378 | |
nexpaq | 0:6c56fb4bc5f0 | 379 | int mbedtls_md_hmac_finish( mbedtls_md_context_t *ctx, unsigned char *output ) |
nexpaq | 0:6c56fb4bc5f0 | 380 | { |
nexpaq | 0:6c56fb4bc5f0 | 381 | unsigned char tmp[MBEDTLS_MD_MAX_SIZE]; |
nexpaq | 0:6c56fb4bc5f0 | 382 | unsigned char *opad; |
nexpaq | 0:6c56fb4bc5f0 | 383 | |
nexpaq | 0:6c56fb4bc5f0 | 384 | if( ctx == NULL || ctx->md_info == NULL || ctx->hmac_ctx == NULL ) |
nexpaq | 0:6c56fb4bc5f0 | 385 | return( MBEDTLS_ERR_MD_BAD_INPUT_DATA ); |
nexpaq | 0:6c56fb4bc5f0 | 386 | |
nexpaq | 0:6c56fb4bc5f0 | 387 | opad = (unsigned char *) ctx->hmac_ctx + ctx->md_info->block_size; |
nexpaq | 0:6c56fb4bc5f0 | 388 | |
nexpaq | 0:6c56fb4bc5f0 | 389 | ctx->md_info->finish_func( ctx->md_ctx, tmp ); |
nexpaq | 0:6c56fb4bc5f0 | 390 | ctx->md_info->starts_func( ctx->md_ctx ); |
nexpaq | 0:6c56fb4bc5f0 | 391 | ctx->md_info->update_func( ctx->md_ctx, opad, ctx->md_info->block_size ); |
nexpaq | 0:6c56fb4bc5f0 | 392 | ctx->md_info->update_func( ctx->md_ctx, tmp, ctx->md_info->size ); |
nexpaq | 0:6c56fb4bc5f0 | 393 | ctx->md_info->finish_func( ctx->md_ctx, output ); |
nexpaq | 0:6c56fb4bc5f0 | 394 | |
nexpaq | 0:6c56fb4bc5f0 | 395 | return( 0 ); |
nexpaq | 0:6c56fb4bc5f0 | 396 | } |
nexpaq | 0:6c56fb4bc5f0 | 397 | |
nexpaq | 0:6c56fb4bc5f0 | 398 | int mbedtls_md_hmac_reset( mbedtls_md_context_t *ctx ) |
nexpaq | 0:6c56fb4bc5f0 | 399 | { |
nexpaq | 0:6c56fb4bc5f0 | 400 | unsigned char *ipad; |
nexpaq | 0:6c56fb4bc5f0 | 401 | |
nexpaq | 0:6c56fb4bc5f0 | 402 | if( ctx == NULL || ctx->md_info == NULL || ctx->hmac_ctx == NULL ) |
nexpaq | 0:6c56fb4bc5f0 | 403 | return( MBEDTLS_ERR_MD_BAD_INPUT_DATA ); |
nexpaq | 0:6c56fb4bc5f0 | 404 | |
nexpaq | 0:6c56fb4bc5f0 | 405 | ipad = (unsigned char *) ctx->hmac_ctx; |
nexpaq | 0:6c56fb4bc5f0 | 406 | |
nexpaq | 0:6c56fb4bc5f0 | 407 | ctx->md_info->starts_func( ctx->md_ctx ); |
nexpaq | 0:6c56fb4bc5f0 | 408 | ctx->md_info->update_func( ctx->md_ctx, ipad, ctx->md_info->block_size ); |
nexpaq | 0:6c56fb4bc5f0 | 409 | |
nexpaq | 0:6c56fb4bc5f0 | 410 | return( 0 ); |
nexpaq | 0:6c56fb4bc5f0 | 411 | } |
nexpaq | 0:6c56fb4bc5f0 | 412 | |
nexpaq | 0:6c56fb4bc5f0 | 413 | int mbedtls_md_hmac( const mbedtls_md_info_t *md_info, const unsigned char *key, size_t keylen, |
nexpaq | 0:6c56fb4bc5f0 | 414 | const unsigned char *input, size_t ilen, |
nexpaq | 0:6c56fb4bc5f0 | 415 | unsigned char *output ) |
nexpaq | 0:6c56fb4bc5f0 | 416 | { |
nexpaq | 0:6c56fb4bc5f0 | 417 | mbedtls_md_context_t ctx; |
nexpaq | 0:6c56fb4bc5f0 | 418 | int ret; |
nexpaq | 0:6c56fb4bc5f0 | 419 | |
nexpaq | 0:6c56fb4bc5f0 | 420 | if( md_info == NULL ) |
nexpaq | 0:6c56fb4bc5f0 | 421 | return( MBEDTLS_ERR_MD_BAD_INPUT_DATA ); |
nexpaq | 0:6c56fb4bc5f0 | 422 | |
nexpaq | 0:6c56fb4bc5f0 | 423 | mbedtls_md_init( &ctx ); |
nexpaq | 0:6c56fb4bc5f0 | 424 | |
nexpaq | 0:6c56fb4bc5f0 | 425 | if( ( ret = mbedtls_md_setup( &ctx, md_info, 1 ) ) != 0 ) |
nexpaq | 0:6c56fb4bc5f0 | 426 | return( ret ); |
nexpaq | 0:6c56fb4bc5f0 | 427 | |
nexpaq | 0:6c56fb4bc5f0 | 428 | mbedtls_md_hmac_starts( &ctx, key, keylen ); |
nexpaq | 0:6c56fb4bc5f0 | 429 | mbedtls_md_hmac_update( &ctx, input, ilen ); |
nexpaq | 0:6c56fb4bc5f0 | 430 | mbedtls_md_hmac_finish( &ctx, output ); |
nexpaq | 0:6c56fb4bc5f0 | 431 | |
nexpaq | 0:6c56fb4bc5f0 | 432 | mbedtls_md_free( &ctx ); |
nexpaq | 0:6c56fb4bc5f0 | 433 | |
nexpaq | 0:6c56fb4bc5f0 | 434 | return( 0 ); |
nexpaq | 0:6c56fb4bc5f0 | 435 | } |
nexpaq | 0:6c56fb4bc5f0 | 436 | |
nexpaq | 0:6c56fb4bc5f0 | 437 | int mbedtls_md_process( mbedtls_md_context_t *ctx, const unsigned char *data ) |
nexpaq | 0:6c56fb4bc5f0 | 438 | { |
nexpaq | 0:6c56fb4bc5f0 | 439 | if( ctx == NULL || ctx->md_info == NULL ) |
nexpaq | 0:6c56fb4bc5f0 | 440 | return( MBEDTLS_ERR_MD_BAD_INPUT_DATA ); |
nexpaq | 0:6c56fb4bc5f0 | 441 | |
nexpaq | 0:6c56fb4bc5f0 | 442 | ctx->md_info->process_func( ctx->md_ctx, data ); |
nexpaq | 0:6c56fb4bc5f0 | 443 | |
nexpaq | 0:6c56fb4bc5f0 | 444 | return( 0 ); |
nexpaq | 0:6c56fb4bc5f0 | 445 | } |
nexpaq | 0:6c56fb4bc5f0 | 446 | |
nexpaq | 0:6c56fb4bc5f0 | 447 | unsigned char mbedtls_md_get_size( const mbedtls_md_info_t *md_info ) |
nexpaq | 0:6c56fb4bc5f0 | 448 | { |
nexpaq | 0:6c56fb4bc5f0 | 449 | if( md_info == NULL ) |
nexpaq | 0:6c56fb4bc5f0 | 450 | return( 0 ); |
nexpaq | 0:6c56fb4bc5f0 | 451 | |
nexpaq | 0:6c56fb4bc5f0 | 452 | return md_info->size; |
nexpaq | 0:6c56fb4bc5f0 | 453 | } |
nexpaq | 0:6c56fb4bc5f0 | 454 | |
nexpaq | 0:6c56fb4bc5f0 | 455 | mbedtls_md_type_t mbedtls_md_get_type( const mbedtls_md_info_t *md_info ) |
nexpaq | 0:6c56fb4bc5f0 | 456 | { |
nexpaq | 0:6c56fb4bc5f0 | 457 | if( md_info == NULL ) |
nexpaq | 0:6c56fb4bc5f0 | 458 | return( MBEDTLS_MD_NONE ); |
nexpaq | 0:6c56fb4bc5f0 | 459 | |
nexpaq | 0:6c56fb4bc5f0 | 460 | return md_info->type; |
nexpaq | 0:6c56fb4bc5f0 | 461 | } |
nexpaq | 0:6c56fb4bc5f0 | 462 | |
nexpaq | 0:6c56fb4bc5f0 | 463 | const char *mbedtls_md_get_name( const mbedtls_md_info_t *md_info ) |
nexpaq | 0:6c56fb4bc5f0 | 464 | { |
nexpaq | 0:6c56fb4bc5f0 | 465 | if( md_info == NULL ) |
nexpaq | 0:6c56fb4bc5f0 | 466 | return( NULL ); |
nexpaq | 0:6c56fb4bc5f0 | 467 | |
nexpaq | 0:6c56fb4bc5f0 | 468 | return md_info->name; |
nexpaq | 0:6c56fb4bc5f0 | 469 | } |
nexpaq | 0:6c56fb4bc5f0 | 470 | |
nexpaq | 0:6c56fb4bc5f0 | 471 | #endif /* MBEDTLS_MD_C */ |