Hannes Tschofenig
/
aes-gcm-test-program
Example program to test AES-GCM functionality. Used for a workshop
SSL/include/polarssl/md5.h@0:796d0f61a05b, 2018-09-27 (annotated)
- Committer:
- HannesTschofenig
- Date:
- Thu Sep 27 06:34:22 2018 +0000
- Revision:
- 0:796d0f61a05b
Example AES-GCM test program
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
HannesTschofenig | 0:796d0f61a05b | 1 | /** |
HannesTschofenig | 0:796d0f61a05b | 2 | * \file md5.h |
HannesTschofenig | 0:796d0f61a05b | 3 | * |
HannesTschofenig | 0:796d0f61a05b | 4 | * \brief MD5 message digest algorithm (hash function) |
HannesTschofenig | 0:796d0f61a05b | 5 | * |
HannesTschofenig | 0:796d0f61a05b | 6 | * Copyright (C) 2006-2013, Brainspark B.V. |
HannesTschofenig | 0:796d0f61a05b | 7 | * |
HannesTschofenig | 0:796d0f61a05b | 8 | * This file is part of PolarSSL (http://www.polarssl.org) |
HannesTschofenig | 0:796d0f61a05b | 9 | * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org> |
HannesTschofenig | 0:796d0f61a05b | 10 | * |
HannesTschofenig | 0:796d0f61a05b | 11 | * All rights reserved. |
HannesTschofenig | 0:796d0f61a05b | 12 | * |
HannesTschofenig | 0:796d0f61a05b | 13 | * This program is free software; you can redistribute it and/or modify |
HannesTschofenig | 0:796d0f61a05b | 14 | * it under the terms of the GNU General Public License as published by |
HannesTschofenig | 0:796d0f61a05b | 15 | * the Free Software Foundation; either version 2 of the License, or |
HannesTschofenig | 0:796d0f61a05b | 16 | * (at your option) any later version. |
HannesTschofenig | 0:796d0f61a05b | 17 | * |
HannesTschofenig | 0:796d0f61a05b | 18 | * This program is distributed in the hope that it will be useful, |
HannesTschofenig | 0:796d0f61a05b | 19 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
HannesTschofenig | 0:796d0f61a05b | 20 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
HannesTschofenig | 0:796d0f61a05b | 21 | * GNU General Public License for more details. |
HannesTschofenig | 0:796d0f61a05b | 22 | * |
HannesTschofenig | 0:796d0f61a05b | 23 | * You should have received a copy of the GNU General Public License along |
HannesTschofenig | 0:796d0f61a05b | 24 | * with this program; if not, write to the Free Software Foundation, Inc., |
HannesTschofenig | 0:796d0f61a05b | 25 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
HannesTschofenig | 0:796d0f61a05b | 26 | */ |
HannesTschofenig | 0:796d0f61a05b | 27 | #ifndef POLARSSL_MD5_H |
HannesTschofenig | 0:796d0f61a05b | 28 | #define POLARSSL_MD5_H |
HannesTschofenig | 0:796d0f61a05b | 29 | |
HannesTschofenig | 0:796d0f61a05b | 30 | #if !defined(POLARSSL_CONFIG_FILE) |
HannesTschofenig | 0:796d0f61a05b | 31 | #include "config.h" |
HannesTschofenig | 0:796d0f61a05b | 32 | #else |
HannesTschofenig | 0:796d0f61a05b | 33 | #include POLARSSL_CONFIG_FILE |
HannesTschofenig | 0:796d0f61a05b | 34 | #endif |
HannesTschofenig | 0:796d0f61a05b | 35 | |
HannesTschofenig | 0:796d0f61a05b | 36 | #include <string.h> |
HannesTschofenig | 0:796d0f61a05b | 37 | |
HannesTschofenig | 0:796d0f61a05b | 38 | #if defined(_MSC_VER) && !defined(EFIX64) && !defined(EFI32) |
HannesTschofenig | 0:796d0f61a05b | 39 | #include <basetsd.h> |
HannesTschofenig | 0:796d0f61a05b | 40 | typedef UINT32 uint32_t; |
HannesTschofenig | 0:796d0f61a05b | 41 | #else |
HannesTschofenig | 0:796d0f61a05b | 42 | #include <inttypes.h> |
HannesTschofenig | 0:796d0f61a05b | 43 | #endif |
HannesTschofenig | 0:796d0f61a05b | 44 | |
HannesTschofenig | 0:796d0f61a05b | 45 | #define POLARSSL_ERR_MD5_FILE_IO_ERROR -0x0074 /**< Read/write error in file. */ |
HannesTschofenig | 0:796d0f61a05b | 46 | |
HannesTschofenig | 0:796d0f61a05b | 47 | #if !defined(POLARSSL_MD5_ALT) |
HannesTschofenig | 0:796d0f61a05b | 48 | // Regular implementation |
HannesTschofenig | 0:796d0f61a05b | 49 | // |
HannesTschofenig | 0:796d0f61a05b | 50 | |
HannesTschofenig | 0:796d0f61a05b | 51 | #ifdef __cplusplus |
HannesTschofenig | 0:796d0f61a05b | 52 | extern "C" { |
HannesTschofenig | 0:796d0f61a05b | 53 | #endif |
HannesTschofenig | 0:796d0f61a05b | 54 | |
HannesTschofenig | 0:796d0f61a05b | 55 | /** |
HannesTschofenig | 0:796d0f61a05b | 56 | * \brief MD5 context structure |
HannesTschofenig | 0:796d0f61a05b | 57 | */ |
HannesTschofenig | 0:796d0f61a05b | 58 | typedef struct |
HannesTschofenig | 0:796d0f61a05b | 59 | { |
HannesTschofenig | 0:796d0f61a05b | 60 | uint32_t total[2]; /*!< number of bytes processed */ |
HannesTschofenig | 0:796d0f61a05b | 61 | uint32_t state[4]; /*!< intermediate digest state */ |
HannesTschofenig | 0:796d0f61a05b | 62 | unsigned char buffer[64]; /*!< data block being processed */ |
HannesTschofenig | 0:796d0f61a05b | 63 | |
HannesTschofenig | 0:796d0f61a05b | 64 | unsigned char ipad[64]; /*!< HMAC: inner padding */ |
HannesTschofenig | 0:796d0f61a05b | 65 | unsigned char opad[64]; /*!< HMAC: outer padding */ |
HannesTschofenig | 0:796d0f61a05b | 66 | } |
HannesTschofenig | 0:796d0f61a05b | 67 | md5_context; |
HannesTschofenig | 0:796d0f61a05b | 68 | |
HannesTschofenig | 0:796d0f61a05b | 69 | /** |
HannesTschofenig | 0:796d0f61a05b | 70 | * \brief MD5 context setup |
HannesTschofenig | 0:796d0f61a05b | 71 | * |
HannesTschofenig | 0:796d0f61a05b | 72 | * \param ctx context to be initialized |
HannesTschofenig | 0:796d0f61a05b | 73 | */ |
HannesTschofenig | 0:796d0f61a05b | 74 | void md5_starts( md5_context *ctx ); |
HannesTschofenig | 0:796d0f61a05b | 75 | |
HannesTschofenig | 0:796d0f61a05b | 76 | /** |
HannesTschofenig | 0:796d0f61a05b | 77 | * \brief MD5 process buffer |
HannesTschofenig | 0:796d0f61a05b | 78 | * |
HannesTschofenig | 0:796d0f61a05b | 79 | * \param ctx MD5 context |
HannesTschofenig | 0:796d0f61a05b | 80 | * \param input buffer holding the data |
HannesTschofenig | 0:796d0f61a05b | 81 | * \param ilen length of the input data |
HannesTschofenig | 0:796d0f61a05b | 82 | */ |
HannesTschofenig | 0:796d0f61a05b | 83 | void md5_update( md5_context *ctx, const unsigned char *input, size_t ilen ); |
HannesTschofenig | 0:796d0f61a05b | 84 | |
HannesTschofenig | 0:796d0f61a05b | 85 | /** |
HannesTschofenig | 0:796d0f61a05b | 86 | * \brief MD5 final digest |
HannesTschofenig | 0:796d0f61a05b | 87 | * |
HannesTschofenig | 0:796d0f61a05b | 88 | * \param ctx MD5 context |
HannesTschofenig | 0:796d0f61a05b | 89 | * \param output MD5 checksum result |
HannesTschofenig | 0:796d0f61a05b | 90 | */ |
HannesTschofenig | 0:796d0f61a05b | 91 | void md5_finish( md5_context *ctx, unsigned char output[16] ); |
HannesTschofenig | 0:796d0f61a05b | 92 | |
HannesTschofenig | 0:796d0f61a05b | 93 | /* Internal use */ |
HannesTschofenig | 0:796d0f61a05b | 94 | void md5_process( md5_context *ctx, const unsigned char data[64] ); |
HannesTschofenig | 0:796d0f61a05b | 95 | |
HannesTschofenig | 0:796d0f61a05b | 96 | #ifdef __cplusplus |
HannesTschofenig | 0:796d0f61a05b | 97 | } |
HannesTschofenig | 0:796d0f61a05b | 98 | #endif |
HannesTschofenig | 0:796d0f61a05b | 99 | |
HannesTschofenig | 0:796d0f61a05b | 100 | #else /* POLARSSL_MD5_ALT */ |
HannesTschofenig | 0:796d0f61a05b | 101 | #include "md5_alt.h" |
HannesTschofenig | 0:796d0f61a05b | 102 | #endif /* POLARSSL_MD5_ALT */ |
HannesTschofenig | 0:796d0f61a05b | 103 | |
HannesTschofenig | 0:796d0f61a05b | 104 | #ifdef __cplusplus |
HannesTschofenig | 0:796d0f61a05b | 105 | extern "C" { |
HannesTschofenig | 0:796d0f61a05b | 106 | #endif |
HannesTschofenig | 0:796d0f61a05b | 107 | |
HannesTschofenig | 0:796d0f61a05b | 108 | /** |
HannesTschofenig | 0:796d0f61a05b | 109 | * \brief Output = MD5( input buffer ) |
HannesTschofenig | 0:796d0f61a05b | 110 | * |
HannesTschofenig | 0:796d0f61a05b | 111 | * \param input buffer holding the data |
HannesTschofenig | 0:796d0f61a05b | 112 | * \param ilen length of the input data |
HannesTschofenig | 0:796d0f61a05b | 113 | * \param output MD5 checksum result |
HannesTschofenig | 0:796d0f61a05b | 114 | */ |
HannesTschofenig | 0:796d0f61a05b | 115 | void md5( const unsigned char *input, size_t ilen, unsigned char output[16] ); |
HannesTschofenig | 0:796d0f61a05b | 116 | |
HannesTschofenig | 0:796d0f61a05b | 117 | /** |
HannesTschofenig | 0:796d0f61a05b | 118 | * \brief Output = MD5( file contents ) |
HannesTschofenig | 0:796d0f61a05b | 119 | * |
HannesTschofenig | 0:796d0f61a05b | 120 | * \param path input file name |
HannesTschofenig | 0:796d0f61a05b | 121 | * \param output MD5 checksum result |
HannesTschofenig | 0:796d0f61a05b | 122 | * |
HannesTschofenig | 0:796d0f61a05b | 123 | * \return 0 if successful, or POLARSSL_ERR_MD5_FILE_IO_ERROR |
HannesTschofenig | 0:796d0f61a05b | 124 | */ |
HannesTschofenig | 0:796d0f61a05b | 125 | int md5_file( const char *path, unsigned char output[16] ); |
HannesTschofenig | 0:796d0f61a05b | 126 | |
HannesTschofenig | 0:796d0f61a05b | 127 | /** |
HannesTschofenig | 0:796d0f61a05b | 128 | * \brief MD5 HMAC context setup |
HannesTschofenig | 0:796d0f61a05b | 129 | * |
HannesTschofenig | 0:796d0f61a05b | 130 | * \param ctx HMAC context to be initialized |
HannesTschofenig | 0:796d0f61a05b | 131 | * \param key HMAC secret key |
HannesTschofenig | 0:796d0f61a05b | 132 | * \param keylen length of the HMAC key |
HannesTschofenig | 0:796d0f61a05b | 133 | */ |
HannesTschofenig | 0:796d0f61a05b | 134 | void md5_hmac_starts( md5_context *ctx, |
HannesTschofenig | 0:796d0f61a05b | 135 | const unsigned char *key, size_t keylen ); |
HannesTschofenig | 0:796d0f61a05b | 136 | |
HannesTschofenig | 0:796d0f61a05b | 137 | /** |
HannesTschofenig | 0:796d0f61a05b | 138 | * \brief MD5 HMAC process buffer |
HannesTschofenig | 0:796d0f61a05b | 139 | * |
HannesTschofenig | 0:796d0f61a05b | 140 | * \param ctx HMAC context |
HannesTschofenig | 0:796d0f61a05b | 141 | * \param input buffer holding the data |
HannesTschofenig | 0:796d0f61a05b | 142 | * \param ilen length of the input data |
HannesTschofenig | 0:796d0f61a05b | 143 | */ |
HannesTschofenig | 0:796d0f61a05b | 144 | void md5_hmac_update( md5_context *ctx, |
HannesTschofenig | 0:796d0f61a05b | 145 | const unsigned char *input, size_t ilen ); |
HannesTschofenig | 0:796d0f61a05b | 146 | |
HannesTschofenig | 0:796d0f61a05b | 147 | /** |
HannesTschofenig | 0:796d0f61a05b | 148 | * \brief MD5 HMAC final digest |
HannesTschofenig | 0:796d0f61a05b | 149 | * |
HannesTschofenig | 0:796d0f61a05b | 150 | * \param ctx HMAC context |
HannesTschofenig | 0:796d0f61a05b | 151 | * \param output MD5 HMAC checksum result |
HannesTschofenig | 0:796d0f61a05b | 152 | */ |
HannesTschofenig | 0:796d0f61a05b | 153 | void md5_hmac_finish( md5_context *ctx, unsigned char output[16] ); |
HannesTschofenig | 0:796d0f61a05b | 154 | |
HannesTschofenig | 0:796d0f61a05b | 155 | /** |
HannesTschofenig | 0:796d0f61a05b | 156 | * \brief MD5 HMAC context reset |
HannesTschofenig | 0:796d0f61a05b | 157 | * |
HannesTschofenig | 0:796d0f61a05b | 158 | * \param ctx HMAC context to be reset |
HannesTschofenig | 0:796d0f61a05b | 159 | */ |
HannesTschofenig | 0:796d0f61a05b | 160 | void md5_hmac_reset( md5_context *ctx ); |
HannesTschofenig | 0:796d0f61a05b | 161 | |
HannesTschofenig | 0:796d0f61a05b | 162 | /** |
HannesTschofenig | 0:796d0f61a05b | 163 | * \brief Output = HMAC-MD5( hmac key, input buffer ) |
HannesTschofenig | 0:796d0f61a05b | 164 | * |
HannesTschofenig | 0:796d0f61a05b | 165 | * \param key HMAC secret key |
HannesTschofenig | 0:796d0f61a05b | 166 | * \param keylen length of the HMAC key |
HannesTschofenig | 0:796d0f61a05b | 167 | * \param input buffer holding the data |
HannesTschofenig | 0:796d0f61a05b | 168 | * \param ilen length of the input data |
HannesTschofenig | 0:796d0f61a05b | 169 | * \param output HMAC-MD5 result |
HannesTschofenig | 0:796d0f61a05b | 170 | */ |
HannesTschofenig | 0:796d0f61a05b | 171 | void md5_hmac( const unsigned char *key, size_t keylen, |
HannesTschofenig | 0:796d0f61a05b | 172 | const unsigned char *input, size_t ilen, |
HannesTschofenig | 0:796d0f61a05b | 173 | unsigned char output[16] ); |
HannesTschofenig | 0:796d0f61a05b | 174 | |
HannesTschofenig | 0:796d0f61a05b | 175 | /** |
HannesTschofenig | 0:796d0f61a05b | 176 | * \brief Checkup routine |
HannesTschofenig | 0:796d0f61a05b | 177 | * |
HannesTschofenig | 0:796d0f61a05b | 178 | * \return 0 if successful, or 1 if the test failed |
HannesTschofenig | 0:796d0f61a05b | 179 | */ |
HannesTschofenig | 0:796d0f61a05b | 180 | int md5_self_test( int verbose ); |
HannesTschofenig | 0:796d0f61a05b | 181 | |
HannesTschofenig | 0:796d0f61a05b | 182 | #ifdef __cplusplus |
HannesTschofenig | 0:796d0f61a05b | 183 | } |
HannesTschofenig | 0:796d0f61a05b | 184 | #endif |
HannesTschofenig | 0:796d0f61a05b | 185 | |
HannesTschofenig | 0:796d0f61a05b | 186 | #endif /* md5.h */ |
HannesTschofenig | 0:796d0f61a05b | 187 | |
HannesTschofenig | 0:796d0f61a05b | 188 |