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