Hannes Tschofenig
/
aes-gcm-test-program
Example program to test AES-GCM functionality. Used for a workshop
SSL/library/md5.c@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 | * RFC 1321 compliant MD5 implementation |
HannesTschofenig | 0:796d0f61a05b | 3 | * |
HannesTschofenig | 0:796d0f61a05b | 4 | * Copyright (C) 2006-2014, Brainspark B.V. |
HannesTschofenig | 0:796d0f61a05b | 5 | * |
HannesTschofenig | 0:796d0f61a05b | 6 | * This file is part of PolarSSL (http://www.polarssl.org) |
HannesTschofenig | 0:796d0f61a05b | 7 | * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org> |
HannesTschofenig | 0:796d0f61a05b | 8 | * |
HannesTschofenig | 0:796d0f61a05b | 9 | * All rights reserved. |
HannesTschofenig | 0:796d0f61a05b | 10 | * |
HannesTschofenig | 0:796d0f61a05b | 11 | * This program is free software; you can redistribute it and/or modify |
HannesTschofenig | 0:796d0f61a05b | 12 | * it under the terms of the GNU General Public License as published by |
HannesTschofenig | 0:796d0f61a05b | 13 | * the Free Software Foundation; either version 2 of the License, or |
HannesTschofenig | 0:796d0f61a05b | 14 | * (at your option) any later version. |
HannesTschofenig | 0:796d0f61a05b | 15 | * |
HannesTschofenig | 0:796d0f61a05b | 16 | * This program is distributed in the hope that it will be useful, |
HannesTschofenig | 0:796d0f61a05b | 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
HannesTschofenig | 0:796d0f61a05b | 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
HannesTschofenig | 0:796d0f61a05b | 19 | * GNU General Public License for more details. |
HannesTschofenig | 0:796d0f61a05b | 20 | * |
HannesTschofenig | 0:796d0f61a05b | 21 | * You should have received a copy of the GNU General Public License along |
HannesTschofenig | 0:796d0f61a05b | 22 | * with this program; if not, write to the Free Software Foundation, Inc., |
HannesTschofenig | 0:796d0f61a05b | 23 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
HannesTschofenig | 0:796d0f61a05b | 24 | */ |
HannesTschofenig | 0:796d0f61a05b | 25 | /* |
HannesTschofenig | 0:796d0f61a05b | 26 | * The MD5 algorithm was designed by Ron Rivest in 1991. |
HannesTschofenig | 0:796d0f61a05b | 27 | * |
HannesTschofenig | 0:796d0f61a05b | 28 | * http://www.ietf.org/rfc/rfc1321.txt |
HannesTschofenig | 0:796d0f61a05b | 29 | */ |
HannesTschofenig | 0:796d0f61a05b | 30 | |
HannesTschofenig | 0:796d0f61a05b | 31 | #if !defined(POLARSSL_CONFIG_FILE) |
HannesTschofenig | 0:796d0f61a05b | 32 | #include "polarssl/config.h" |
HannesTschofenig | 0:796d0f61a05b | 33 | #else |
HannesTschofenig | 0:796d0f61a05b | 34 | #include POLARSSL_CONFIG_FILE |
HannesTschofenig | 0:796d0f61a05b | 35 | #endif |
HannesTschofenig | 0:796d0f61a05b | 36 | |
HannesTschofenig | 0:796d0f61a05b | 37 | #if defined(POLARSSL_MD5_C) |
HannesTschofenig | 0:796d0f61a05b | 38 | |
HannesTschofenig | 0:796d0f61a05b | 39 | #include "polarssl/md5.h" |
HannesTschofenig | 0:796d0f61a05b | 40 | |
HannesTschofenig | 0:796d0f61a05b | 41 | #if defined(POLARSSL_FS_IO) || defined(POLARSSL_SELF_TEST) |
HannesTschofenig | 0:796d0f61a05b | 42 | #include <stdio.h> |
HannesTschofenig | 0:796d0f61a05b | 43 | #endif |
HannesTschofenig | 0:796d0f61a05b | 44 | |
HannesTschofenig | 0:796d0f61a05b | 45 | #if defined(POLARSSL_PLATFORM_C) |
HannesTschofenig | 0:796d0f61a05b | 46 | #include "polarssl/platform.h" |
HannesTschofenig | 0:796d0f61a05b | 47 | #else |
HannesTschofenig | 0:796d0f61a05b | 48 | #define polarssl_printf printf |
HannesTschofenig | 0:796d0f61a05b | 49 | #endif |
HannesTschofenig | 0:796d0f61a05b | 50 | |
HannesTschofenig | 0:796d0f61a05b | 51 | #if !defined(POLARSSL_MD5_ALT) |
HannesTschofenig | 0:796d0f61a05b | 52 | |
HannesTschofenig | 0:796d0f61a05b | 53 | /* |
HannesTschofenig | 0:796d0f61a05b | 54 | * 32-bit integer manipulation macros (little endian) |
HannesTschofenig | 0:796d0f61a05b | 55 | */ |
HannesTschofenig | 0:796d0f61a05b | 56 | #ifndef GET_UINT32_LE |
HannesTschofenig | 0:796d0f61a05b | 57 | #define GET_UINT32_LE(n,b,i) \ |
HannesTschofenig | 0:796d0f61a05b | 58 | { \ |
HannesTschofenig | 0:796d0f61a05b | 59 | (n) = ( (uint32_t) (b)[(i) ] ) \ |
HannesTschofenig | 0:796d0f61a05b | 60 | | ( (uint32_t) (b)[(i) + 1] << 8 ) \ |
HannesTschofenig | 0:796d0f61a05b | 61 | | ( (uint32_t) (b)[(i) + 2] << 16 ) \ |
HannesTschofenig | 0:796d0f61a05b | 62 | | ( (uint32_t) (b)[(i) + 3] << 24 ); \ |
HannesTschofenig | 0:796d0f61a05b | 63 | } |
HannesTschofenig | 0:796d0f61a05b | 64 | #endif |
HannesTschofenig | 0:796d0f61a05b | 65 | |
HannesTschofenig | 0:796d0f61a05b | 66 | #ifndef PUT_UINT32_LE |
HannesTschofenig | 0:796d0f61a05b | 67 | #define PUT_UINT32_LE(n,b,i) \ |
HannesTschofenig | 0:796d0f61a05b | 68 | { \ |
HannesTschofenig | 0:796d0f61a05b | 69 | (b)[(i) ] = (unsigned char) ( (n) ); \ |
HannesTschofenig | 0:796d0f61a05b | 70 | (b)[(i) + 1] = (unsigned char) ( (n) >> 8 ); \ |
HannesTschofenig | 0:796d0f61a05b | 71 | (b)[(i) + 2] = (unsigned char) ( (n) >> 16 ); \ |
HannesTschofenig | 0:796d0f61a05b | 72 | (b)[(i) + 3] = (unsigned char) ( (n) >> 24 ); \ |
HannesTschofenig | 0:796d0f61a05b | 73 | } |
HannesTschofenig | 0:796d0f61a05b | 74 | #endif |
HannesTschofenig | 0:796d0f61a05b | 75 | |
HannesTschofenig | 0:796d0f61a05b | 76 | /* |
HannesTschofenig | 0:796d0f61a05b | 77 | * MD5 context setup |
HannesTschofenig | 0:796d0f61a05b | 78 | */ |
HannesTschofenig | 0:796d0f61a05b | 79 | void md5_starts( md5_context *ctx ) |
HannesTschofenig | 0:796d0f61a05b | 80 | { |
HannesTschofenig | 0:796d0f61a05b | 81 | ctx->total[0] = 0; |
HannesTschofenig | 0:796d0f61a05b | 82 | ctx->total[1] = 0; |
HannesTschofenig | 0:796d0f61a05b | 83 | |
HannesTschofenig | 0:796d0f61a05b | 84 | ctx->state[0] = 0x67452301; |
HannesTschofenig | 0:796d0f61a05b | 85 | ctx->state[1] = 0xEFCDAB89; |
HannesTschofenig | 0:796d0f61a05b | 86 | ctx->state[2] = 0x98BADCFE; |
HannesTschofenig | 0:796d0f61a05b | 87 | ctx->state[3] = 0x10325476; |
HannesTschofenig | 0:796d0f61a05b | 88 | } |
HannesTschofenig | 0:796d0f61a05b | 89 | |
HannesTschofenig | 0:796d0f61a05b | 90 | void md5_process( md5_context *ctx, const unsigned char data[64] ) |
HannesTschofenig | 0:796d0f61a05b | 91 | { |
HannesTschofenig | 0:796d0f61a05b | 92 | uint32_t X[16], A, B, C, D; |
HannesTschofenig | 0:796d0f61a05b | 93 | |
HannesTschofenig | 0:796d0f61a05b | 94 | GET_UINT32_LE( X[ 0], data, 0 ); |
HannesTschofenig | 0:796d0f61a05b | 95 | GET_UINT32_LE( X[ 1], data, 4 ); |
HannesTschofenig | 0:796d0f61a05b | 96 | GET_UINT32_LE( X[ 2], data, 8 ); |
HannesTschofenig | 0:796d0f61a05b | 97 | GET_UINT32_LE( X[ 3], data, 12 ); |
HannesTschofenig | 0:796d0f61a05b | 98 | GET_UINT32_LE( X[ 4], data, 16 ); |
HannesTschofenig | 0:796d0f61a05b | 99 | GET_UINT32_LE( X[ 5], data, 20 ); |
HannesTschofenig | 0:796d0f61a05b | 100 | GET_UINT32_LE( X[ 6], data, 24 ); |
HannesTschofenig | 0:796d0f61a05b | 101 | GET_UINT32_LE( X[ 7], data, 28 ); |
HannesTschofenig | 0:796d0f61a05b | 102 | GET_UINT32_LE( X[ 8], data, 32 ); |
HannesTschofenig | 0:796d0f61a05b | 103 | GET_UINT32_LE( X[ 9], data, 36 ); |
HannesTschofenig | 0:796d0f61a05b | 104 | GET_UINT32_LE( X[10], data, 40 ); |
HannesTschofenig | 0:796d0f61a05b | 105 | GET_UINT32_LE( X[11], data, 44 ); |
HannesTschofenig | 0:796d0f61a05b | 106 | GET_UINT32_LE( X[12], data, 48 ); |
HannesTschofenig | 0:796d0f61a05b | 107 | GET_UINT32_LE( X[13], data, 52 ); |
HannesTschofenig | 0:796d0f61a05b | 108 | GET_UINT32_LE( X[14], data, 56 ); |
HannesTschofenig | 0:796d0f61a05b | 109 | GET_UINT32_LE( X[15], data, 60 ); |
HannesTschofenig | 0:796d0f61a05b | 110 | |
HannesTschofenig | 0:796d0f61a05b | 111 | #define S(x,n) ((x << n) | ((x & 0xFFFFFFFF) >> (32 - n))) |
HannesTschofenig | 0:796d0f61a05b | 112 | |
HannesTschofenig | 0:796d0f61a05b | 113 | #define P(a,b,c,d,k,s,t) \ |
HannesTschofenig | 0:796d0f61a05b | 114 | { \ |
HannesTschofenig | 0:796d0f61a05b | 115 | a += F(b,c,d) + X[k] + t; a = S(a,s) + b; \ |
HannesTschofenig | 0:796d0f61a05b | 116 | } |
HannesTschofenig | 0:796d0f61a05b | 117 | |
HannesTschofenig | 0:796d0f61a05b | 118 | A = ctx->state[0]; |
HannesTschofenig | 0:796d0f61a05b | 119 | B = ctx->state[1]; |
HannesTschofenig | 0:796d0f61a05b | 120 | C = ctx->state[2]; |
HannesTschofenig | 0:796d0f61a05b | 121 | D = ctx->state[3]; |
HannesTschofenig | 0:796d0f61a05b | 122 | |
HannesTschofenig | 0:796d0f61a05b | 123 | #define F(x,y,z) (z ^ (x & (y ^ z))) |
HannesTschofenig | 0:796d0f61a05b | 124 | |
HannesTschofenig | 0:796d0f61a05b | 125 | P( A, B, C, D, 0, 7, 0xD76AA478 ); |
HannesTschofenig | 0:796d0f61a05b | 126 | P( D, A, B, C, 1, 12, 0xE8C7B756 ); |
HannesTschofenig | 0:796d0f61a05b | 127 | P( C, D, A, B, 2, 17, 0x242070DB ); |
HannesTschofenig | 0:796d0f61a05b | 128 | P( B, C, D, A, 3, 22, 0xC1BDCEEE ); |
HannesTschofenig | 0:796d0f61a05b | 129 | P( A, B, C, D, 4, 7, 0xF57C0FAF ); |
HannesTschofenig | 0:796d0f61a05b | 130 | P( D, A, B, C, 5, 12, 0x4787C62A ); |
HannesTschofenig | 0:796d0f61a05b | 131 | P( C, D, A, B, 6, 17, 0xA8304613 ); |
HannesTschofenig | 0:796d0f61a05b | 132 | P( B, C, D, A, 7, 22, 0xFD469501 ); |
HannesTschofenig | 0:796d0f61a05b | 133 | P( A, B, C, D, 8, 7, 0x698098D8 ); |
HannesTschofenig | 0:796d0f61a05b | 134 | P( D, A, B, C, 9, 12, 0x8B44F7AF ); |
HannesTschofenig | 0:796d0f61a05b | 135 | P( C, D, A, B, 10, 17, 0xFFFF5BB1 ); |
HannesTschofenig | 0:796d0f61a05b | 136 | P( B, C, D, A, 11, 22, 0x895CD7BE ); |
HannesTschofenig | 0:796d0f61a05b | 137 | P( A, B, C, D, 12, 7, 0x6B901122 ); |
HannesTschofenig | 0:796d0f61a05b | 138 | P( D, A, B, C, 13, 12, 0xFD987193 ); |
HannesTschofenig | 0:796d0f61a05b | 139 | P( C, D, A, B, 14, 17, 0xA679438E ); |
HannesTschofenig | 0:796d0f61a05b | 140 | P( B, C, D, A, 15, 22, 0x49B40821 ); |
HannesTschofenig | 0:796d0f61a05b | 141 | |
HannesTschofenig | 0:796d0f61a05b | 142 | #undef F |
HannesTschofenig | 0:796d0f61a05b | 143 | |
HannesTschofenig | 0:796d0f61a05b | 144 | #define F(x,y,z) (y ^ (z & (x ^ y))) |
HannesTschofenig | 0:796d0f61a05b | 145 | |
HannesTschofenig | 0:796d0f61a05b | 146 | P( A, B, C, D, 1, 5, 0xF61E2562 ); |
HannesTschofenig | 0:796d0f61a05b | 147 | P( D, A, B, C, 6, 9, 0xC040B340 ); |
HannesTschofenig | 0:796d0f61a05b | 148 | P( C, D, A, B, 11, 14, 0x265E5A51 ); |
HannesTschofenig | 0:796d0f61a05b | 149 | P( B, C, D, A, 0, 20, 0xE9B6C7AA ); |
HannesTschofenig | 0:796d0f61a05b | 150 | P( A, B, C, D, 5, 5, 0xD62F105D ); |
HannesTschofenig | 0:796d0f61a05b | 151 | P( D, A, B, C, 10, 9, 0x02441453 ); |
HannesTschofenig | 0:796d0f61a05b | 152 | P( C, D, A, B, 15, 14, 0xD8A1E681 ); |
HannesTschofenig | 0:796d0f61a05b | 153 | P( B, C, D, A, 4, 20, 0xE7D3FBC8 ); |
HannesTschofenig | 0:796d0f61a05b | 154 | P( A, B, C, D, 9, 5, 0x21E1CDE6 ); |
HannesTschofenig | 0:796d0f61a05b | 155 | P( D, A, B, C, 14, 9, 0xC33707D6 ); |
HannesTschofenig | 0:796d0f61a05b | 156 | P( C, D, A, B, 3, 14, 0xF4D50D87 ); |
HannesTschofenig | 0:796d0f61a05b | 157 | P( B, C, D, A, 8, 20, 0x455A14ED ); |
HannesTschofenig | 0:796d0f61a05b | 158 | P( A, B, C, D, 13, 5, 0xA9E3E905 ); |
HannesTschofenig | 0:796d0f61a05b | 159 | P( D, A, B, C, 2, 9, 0xFCEFA3F8 ); |
HannesTschofenig | 0:796d0f61a05b | 160 | P( C, D, A, B, 7, 14, 0x676F02D9 ); |
HannesTschofenig | 0:796d0f61a05b | 161 | P( B, C, D, A, 12, 20, 0x8D2A4C8A ); |
HannesTschofenig | 0:796d0f61a05b | 162 | |
HannesTschofenig | 0:796d0f61a05b | 163 | #undef F |
HannesTschofenig | 0:796d0f61a05b | 164 | |
HannesTschofenig | 0:796d0f61a05b | 165 | #define F(x,y,z) (x ^ y ^ z) |
HannesTschofenig | 0:796d0f61a05b | 166 | |
HannesTschofenig | 0:796d0f61a05b | 167 | P( A, B, C, D, 5, 4, 0xFFFA3942 ); |
HannesTschofenig | 0:796d0f61a05b | 168 | P( D, A, B, C, 8, 11, 0x8771F681 ); |
HannesTschofenig | 0:796d0f61a05b | 169 | P( C, D, A, B, 11, 16, 0x6D9D6122 ); |
HannesTschofenig | 0:796d0f61a05b | 170 | P( B, C, D, A, 14, 23, 0xFDE5380C ); |
HannesTschofenig | 0:796d0f61a05b | 171 | P( A, B, C, D, 1, 4, 0xA4BEEA44 ); |
HannesTschofenig | 0:796d0f61a05b | 172 | P( D, A, B, C, 4, 11, 0x4BDECFA9 ); |
HannesTschofenig | 0:796d0f61a05b | 173 | P( C, D, A, B, 7, 16, 0xF6BB4B60 ); |
HannesTschofenig | 0:796d0f61a05b | 174 | P( B, C, D, A, 10, 23, 0xBEBFBC70 ); |
HannesTschofenig | 0:796d0f61a05b | 175 | P( A, B, C, D, 13, 4, 0x289B7EC6 ); |
HannesTschofenig | 0:796d0f61a05b | 176 | P( D, A, B, C, 0, 11, 0xEAA127FA ); |
HannesTschofenig | 0:796d0f61a05b | 177 | P( C, D, A, B, 3, 16, 0xD4EF3085 ); |
HannesTschofenig | 0:796d0f61a05b | 178 | P( B, C, D, A, 6, 23, 0x04881D05 ); |
HannesTschofenig | 0:796d0f61a05b | 179 | P( A, B, C, D, 9, 4, 0xD9D4D039 ); |
HannesTschofenig | 0:796d0f61a05b | 180 | P( D, A, B, C, 12, 11, 0xE6DB99E5 ); |
HannesTschofenig | 0:796d0f61a05b | 181 | P( C, D, A, B, 15, 16, 0x1FA27CF8 ); |
HannesTschofenig | 0:796d0f61a05b | 182 | P( B, C, D, A, 2, 23, 0xC4AC5665 ); |
HannesTschofenig | 0:796d0f61a05b | 183 | |
HannesTschofenig | 0:796d0f61a05b | 184 | #undef F |
HannesTschofenig | 0:796d0f61a05b | 185 | |
HannesTschofenig | 0:796d0f61a05b | 186 | #define F(x,y,z) (y ^ (x | ~z)) |
HannesTschofenig | 0:796d0f61a05b | 187 | |
HannesTschofenig | 0:796d0f61a05b | 188 | P( A, B, C, D, 0, 6, 0xF4292244 ); |
HannesTschofenig | 0:796d0f61a05b | 189 | P( D, A, B, C, 7, 10, 0x432AFF97 ); |
HannesTschofenig | 0:796d0f61a05b | 190 | P( C, D, A, B, 14, 15, 0xAB9423A7 ); |
HannesTschofenig | 0:796d0f61a05b | 191 | P( B, C, D, A, 5, 21, 0xFC93A039 ); |
HannesTschofenig | 0:796d0f61a05b | 192 | P( A, B, C, D, 12, 6, 0x655B59C3 ); |
HannesTschofenig | 0:796d0f61a05b | 193 | P( D, A, B, C, 3, 10, 0x8F0CCC92 ); |
HannesTschofenig | 0:796d0f61a05b | 194 | P( C, D, A, B, 10, 15, 0xFFEFF47D ); |
HannesTschofenig | 0:796d0f61a05b | 195 | P( B, C, D, A, 1, 21, 0x85845DD1 ); |
HannesTschofenig | 0:796d0f61a05b | 196 | P( A, B, C, D, 8, 6, 0x6FA87E4F ); |
HannesTschofenig | 0:796d0f61a05b | 197 | P( D, A, B, C, 15, 10, 0xFE2CE6E0 ); |
HannesTschofenig | 0:796d0f61a05b | 198 | P( C, D, A, B, 6, 15, 0xA3014314 ); |
HannesTschofenig | 0:796d0f61a05b | 199 | P( B, C, D, A, 13, 21, 0x4E0811A1 ); |
HannesTschofenig | 0:796d0f61a05b | 200 | P( A, B, C, D, 4, 6, 0xF7537E82 ); |
HannesTschofenig | 0:796d0f61a05b | 201 | P( D, A, B, C, 11, 10, 0xBD3AF235 ); |
HannesTschofenig | 0:796d0f61a05b | 202 | P( C, D, A, B, 2, 15, 0x2AD7D2BB ); |
HannesTschofenig | 0:796d0f61a05b | 203 | P( B, C, D, A, 9, 21, 0xEB86D391 ); |
HannesTschofenig | 0:796d0f61a05b | 204 | |
HannesTschofenig | 0:796d0f61a05b | 205 | #undef F |
HannesTschofenig | 0:796d0f61a05b | 206 | |
HannesTschofenig | 0:796d0f61a05b | 207 | ctx->state[0] += A; |
HannesTschofenig | 0:796d0f61a05b | 208 | ctx->state[1] += B; |
HannesTschofenig | 0:796d0f61a05b | 209 | ctx->state[2] += C; |
HannesTschofenig | 0:796d0f61a05b | 210 | ctx->state[3] += D; |
HannesTschofenig | 0:796d0f61a05b | 211 | } |
HannesTschofenig | 0:796d0f61a05b | 212 | |
HannesTschofenig | 0:796d0f61a05b | 213 | /* |
HannesTschofenig | 0:796d0f61a05b | 214 | * MD5 process buffer |
HannesTschofenig | 0:796d0f61a05b | 215 | */ |
HannesTschofenig | 0:796d0f61a05b | 216 | void md5_update( md5_context *ctx, const unsigned char *input, size_t ilen ) |
HannesTschofenig | 0:796d0f61a05b | 217 | { |
HannesTschofenig | 0:796d0f61a05b | 218 | size_t fill; |
HannesTschofenig | 0:796d0f61a05b | 219 | uint32_t left; |
HannesTschofenig | 0:796d0f61a05b | 220 | |
HannesTschofenig | 0:796d0f61a05b | 221 | if( ilen <= 0 ) |
HannesTschofenig | 0:796d0f61a05b | 222 | return; |
HannesTschofenig | 0:796d0f61a05b | 223 | |
HannesTschofenig | 0:796d0f61a05b | 224 | left = ctx->total[0] & 0x3F; |
HannesTschofenig | 0:796d0f61a05b | 225 | fill = 64 - left; |
HannesTschofenig | 0:796d0f61a05b | 226 | |
HannesTschofenig | 0:796d0f61a05b | 227 | ctx->total[0] += (uint32_t) ilen; |
HannesTschofenig | 0:796d0f61a05b | 228 | ctx->total[0] &= 0xFFFFFFFF; |
HannesTschofenig | 0:796d0f61a05b | 229 | |
HannesTschofenig | 0:796d0f61a05b | 230 | if( ctx->total[0] < (uint32_t) ilen ) |
HannesTschofenig | 0:796d0f61a05b | 231 | ctx->total[1]++; |
HannesTschofenig | 0:796d0f61a05b | 232 | |
HannesTschofenig | 0:796d0f61a05b | 233 | if( left && ilen >= fill ) |
HannesTschofenig | 0:796d0f61a05b | 234 | { |
HannesTschofenig | 0:796d0f61a05b | 235 | memcpy( (void *) (ctx->buffer + left), input, fill ); |
HannesTschofenig | 0:796d0f61a05b | 236 | md5_process( ctx, ctx->buffer ); |
HannesTschofenig | 0:796d0f61a05b | 237 | input += fill; |
HannesTschofenig | 0:796d0f61a05b | 238 | ilen -= fill; |
HannesTschofenig | 0:796d0f61a05b | 239 | left = 0; |
HannesTschofenig | 0:796d0f61a05b | 240 | } |
HannesTschofenig | 0:796d0f61a05b | 241 | |
HannesTschofenig | 0:796d0f61a05b | 242 | while( ilen >= 64 ) |
HannesTschofenig | 0:796d0f61a05b | 243 | { |
HannesTschofenig | 0:796d0f61a05b | 244 | md5_process( ctx, input ); |
HannesTschofenig | 0:796d0f61a05b | 245 | input += 64; |
HannesTschofenig | 0:796d0f61a05b | 246 | ilen -= 64; |
HannesTschofenig | 0:796d0f61a05b | 247 | } |
HannesTschofenig | 0:796d0f61a05b | 248 | |
HannesTschofenig | 0:796d0f61a05b | 249 | if( ilen > 0 ) |
HannesTschofenig | 0:796d0f61a05b | 250 | { |
HannesTschofenig | 0:796d0f61a05b | 251 | memcpy( (void *) (ctx->buffer + left), input, ilen ); |
HannesTschofenig | 0:796d0f61a05b | 252 | } |
HannesTschofenig | 0:796d0f61a05b | 253 | } |
HannesTschofenig | 0:796d0f61a05b | 254 | |
HannesTschofenig | 0:796d0f61a05b | 255 | static const unsigned char md5_padding[64] = |
HannesTschofenig | 0:796d0f61a05b | 256 | { |
HannesTschofenig | 0:796d0f61a05b | 257 | 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
HannesTschofenig | 0:796d0f61a05b | 258 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
HannesTschofenig | 0:796d0f61a05b | 259 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
HannesTschofenig | 0:796d0f61a05b | 260 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 |
HannesTschofenig | 0:796d0f61a05b | 261 | }; |
HannesTschofenig | 0:796d0f61a05b | 262 | |
HannesTschofenig | 0:796d0f61a05b | 263 | /* |
HannesTschofenig | 0:796d0f61a05b | 264 | * MD5 final digest |
HannesTschofenig | 0:796d0f61a05b | 265 | */ |
HannesTschofenig | 0:796d0f61a05b | 266 | void md5_finish( md5_context *ctx, unsigned char output[16] ) |
HannesTschofenig | 0:796d0f61a05b | 267 | { |
HannesTschofenig | 0:796d0f61a05b | 268 | uint32_t last, padn; |
HannesTschofenig | 0:796d0f61a05b | 269 | uint32_t high, low; |
HannesTschofenig | 0:796d0f61a05b | 270 | unsigned char msglen[8]; |
HannesTschofenig | 0:796d0f61a05b | 271 | |
HannesTschofenig | 0:796d0f61a05b | 272 | high = ( ctx->total[0] >> 29 ) |
HannesTschofenig | 0:796d0f61a05b | 273 | | ( ctx->total[1] << 3 ); |
HannesTschofenig | 0:796d0f61a05b | 274 | low = ( ctx->total[0] << 3 ); |
HannesTschofenig | 0:796d0f61a05b | 275 | |
HannesTschofenig | 0:796d0f61a05b | 276 | PUT_UINT32_LE( low, msglen, 0 ); |
HannesTschofenig | 0:796d0f61a05b | 277 | PUT_UINT32_LE( high, msglen, 4 ); |
HannesTschofenig | 0:796d0f61a05b | 278 | |
HannesTschofenig | 0:796d0f61a05b | 279 | last = ctx->total[0] & 0x3F; |
HannesTschofenig | 0:796d0f61a05b | 280 | padn = ( last < 56 ) ? ( 56 - last ) : ( 120 - last ); |
HannesTschofenig | 0:796d0f61a05b | 281 | |
HannesTschofenig | 0:796d0f61a05b | 282 | md5_update( ctx, md5_padding, padn ); |
HannesTschofenig | 0:796d0f61a05b | 283 | md5_update( ctx, msglen, 8 ); |
HannesTschofenig | 0:796d0f61a05b | 284 | |
HannesTschofenig | 0:796d0f61a05b | 285 | PUT_UINT32_LE( ctx->state[0], output, 0 ); |
HannesTschofenig | 0:796d0f61a05b | 286 | PUT_UINT32_LE( ctx->state[1], output, 4 ); |
HannesTschofenig | 0:796d0f61a05b | 287 | PUT_UINT32_LE( ctx->state[2], output, 8 ); |
HannesTschofenig | 0:796d0f61a05b | 288 | PUT_UINT32_LE( ctx->state[3], output, 12 ); |
HannesTschofenig | 0:796d0f61a05b | 289 | } |
HannesTschofenig | 0:796d0f61a05b | 290 | |
HannesTschofenig | 0:796d0f61a05b | 291 | #endif /* !POLARSSL_MD5_ALT */ |
HannesTschofenig | 0:796d0f61a05b | 292 | |
HannesTschofenig | 0:796d0f61a05b | 293 | /* |
HannesTschofenig | 0:796d0f61a05b | 294 | * output = MD5( input buffer ) |
HannesTschofenig | 0:796d0f61a05b | 295 | */ |
HannesTschofenig | 0:796d0f61a05b | 296 | void md5( const unsigned char *input, size_t ilen, unsigned char output[16] ) |
HannesTschofenig | 0:796d0f61a05b | 297 | { |
HannesTschofenig | 0:796d0f61a05b | 298 | md5_context ctx; |
HannesTschofenig | 0:796d0f61a05b | 299 | |
HannesTschofenig | 0:796d0f61a05b | 300 | md5_starts( &ctx ); |
HannesTschofenig | 0:796d0f61a05b | 301 | md5_update( &ctx, input, ilen ); |
HannesTschofenig | 0:796d0f61a05b | 302 | md5_finish( &ctx, output ); |
HannesTschofenig | 0:796d0f61a05b | 303 | |
HannesTschofenig | 0:796d0f61a05b | 304 | memset( &ctx, 0, sizeof( md5_context ) ); |
HannesTschofenig | 0:796d0f61a05b | 305 | } |
HannesTschofenig | 0:796d0f61a05b | 306 | |
HannesTschofenig | 0:796d0f61a05b | 307 | #if defined(POLARSSL_FS_IO) |
HannesTschofenig | 0:796d0f61a05b | 308 | /* |
HannesTschofenig | 0:796d0f61a05b | 309 | * output = MD5( file contents ) |
HannesTschofenig | 0:796d0f61a05b | 310 | */ |
HannesTschofenig | 0:796d0f61a05b | 311 | int md5_file( const char *path, unsigned char output[16] ) |
HannesTschofenig | 0:796d0f61a05b | 312 | { |
HannesTschofenig | 0:796d0f61a05b | 313 | FILE *f; |
HannesTschofenig | 0:796d0f61a05b | 314 | size_t n; |
HannesTschofenig | 0:796d0f61a05b | 315 | md5_context ctx; |
HannesTschofenig | 0:796d0f61a05b | 316 | unsigned char buf[1024]; |
HannesTschofenig | 0:796d0f61a05b | 317 | |
HannesTschofenig | 0:796d0f61a05b | 318 | if( ( f = fopen( path, "rb" ) ) == NULL ) |
HannesTschofenig | 0:796d0f61a05b | 319 | return( POLARSSL_ERR_MD5_FILE_IO_ERROR ); |
HannesTschofenig | 0:796d0f61a05b | 320 | |
HannesTschofenig | 0:796d0f61a05b | 321 | md5_starts( &ctx ); |
HannesTschofenig | 0:796d0f61a05b | 322 | |
HannesTschofenig | 0:796d0f61a05b | 323 | while( ( n = fread( buf, 1, sizeof( buf ), f ) ) > 0 ) |
HannesTschofenig | 0:796d0f61a05b | 324 | md5_update( &ctx, buf, n ); |
HannesTschofenig | 0:796d0f61a05b | 325 | |
HannesTschofenig | 0:796d0f61a05b | 326 | md5_finish( &ctx, output ); |
HannesTschofenig | 0:796d0f61a05b | 327 | |
HannesTschofenig | 0:796d0f61a05b | 328 | memset( &ctx, 0, sizeof( md5_context ) ); |
HannesTschofenig | 0:796d0f61a05b | 329 | |
HannesTschofenig | 0:796d0f61a05b | 330 | if( ferror( f ) != 0 ) |
HannesTschofenig | 0:796d0f61a05b | 331 | { |
HannesTschofenig | 0:796d0f61a05b | 332 | fclose( f ); |
HannesTschofenig | 0:796d0f61a05b | 333 | return( POLARSSL_ERR_MD5_FILE_IO_ERROR ); |
HannesTschofenig | 0:796d0f61a05b | 334 | } |
HannesTschofenig | 0:796d0f61a05b | 335 | |
HannesTschofenig | 0:796d0f61a05b | 336 | fclose( f ); |
HannesTschofenig | 0:796d0f61a05b | 337 | return( 0 ); |
HannesTschofenig | 0:796d0f61a05b | 338 | } |
HannesTschofenig | 0:796d0f61a05b | 339 | #endif /* POLARSSL_FS_IO */ |
HannesTschofenig | 0:796d0f61a05b | 340 | |
HannesTschofenig | 0:796d0f61a05b | 341 | /* |
HannesTschofenig | 0:796d0f61a05b | 342 | * MD5 HMAC context setup |
HannesTschofenig | 0:796d0f61a05b | 343 | */ |
HannesTschofenig | 0:796d0f61a05b | 344 | void md5_hmac_starts( md5_context *ctx, const unsigned char *key, |
HannesTschofenig | 0:796d0f61a05b | 345 | size_t keylen ) |
HannesTschofenig | 0:796d0f61a05b | 346 | { |
HannesTschofenig | 0:796d0f61a05b | 347 | size_t i; |
HannesTschofenig | 0:796d0f61a05b | 348 | unsigned char sum[16]; |
HannesTschofenig | 0:796d0f61a05b | 349 | |
HannesTschofenig | 0:796d0f61a05b | 350 | if( keylen > 64 ) |
HannesTschofenig | 0:796d0f61a05b | 351 | { |
HannesTschofenig | 0:796d0f61a05b | 352 | md5( key, keylen, sum ); |
HannesTschofenig | 0:796d0f61a05b | 353 | keylen = 16; |
HannesTschofenig | 0:796d0f61a05b | 354 | key = sum; |
HannesTschofenig | 0:796d0f61a05b | 355 | } |
HannesTschofenig | 0:796d0f61a05b | 356 | |
HannesTschofenig | 0:796d0f61a05b | 357 | memset( ctx->ipad, 0x36, 64 ); |
HannesTschofenig | 0:796d0f61a05b | 358 | memset( ctx->opad, 0x5C, 64 ); |
HannesTschofenig | 0:796d0f61a05b | 359 | |
HannesTschofenig | 0:796d0f61a05b | 360 | for( i = 0; i < keylen; i++ ) |
HannesTschofenig | 0:796d0f61a05b | 361 | { |
HannesTschofenig | 0:796d0f61a05b | 362 | ctx->ipad[i] = (unsigned char)( ctx->ipad[i] ^ key[i] ); |
HannesTschofenig | 0:796d0f61a05b | 363 | ctx->opad[i] = (unsigned char)( ctx->opad[i] ^ key[i] ); |
HannesTschofenig | 0:796d0f61a05b | 364 | } |
HannesTschofenig | 0:796d0f61a05b | 365 | |
HannesTschofenig | 0:796d0f61a05b | 366 | md5_starts( ctx ); |
HannesTschofenig | 0:796d0f61a05b | 367 | md5_update( ctx, ctx->ipad, 64 ); |
HannesTschofenig | 0:796d0f61a05b | 368 | |
HannesTschofenig | 0:796d0f61a05b | 369 | memset( sum, 0, sizeof( sum ) ); |
HannesTschofenig | 0:796d0f61a05b | 370 | } |
HannesTschofenig | 0:796d0f61a05b | 371 | |
HannesTschofenig | 0:796d0f61a05b | 372 | /* |
HannesTschofenig | 0:796d0f61a05b | 373 | * MD5 HMAC process buffer |
HannesTschofenig | 0:796d0f61a05b | 374 | */ |
HannesTschofenig | 0:796d0f61a05b | 375 | void md5_hmac_update( md5_context *ctx, const unsigned char *input, |
HannesTschofenig | 0:796d0f61a05b | 376 | size_t ilen ) |
HannesTschofenig | 0:796d0f61a05b | 377 | { |
HannesTschofenig | 0:796d0f61a05b | 378 | md5_update( ctx, input, ilen ); |
HannesTschofenig | 0:796d0f61a05b | 379 | } |
HannesTschofenig | 0:796d0f61a05b | 380 | |
HannesTschofenig | 0:796d0f61a05b | 381 | /* |
HannesTschofenig | 0:796d0f61a05b | 382 | * MD5 HMAC final digest |
HannesTschofenig | 0:796d0f61a05b | 383 | */ |
HannesTschofenig | 0:796d0f61a05b | 384 | void md5_hmac_finish( md5_context *ctx, unsigned char output[16] ) |
HannesTschofenig | 0:796d0f61a05b | 385 | { |
HannesTschofenig | 0:796d0f61a05b | 386 | unsigned char tmpbuf[16]; |
HannesTschofenig | 0:796d0f61a05b | 387 | |
HannesTschofenig | 0:796d0f61a05b | 388 | md5_finish( ctx, tmpbuf ); |
HannesTschofenig | 0:796d0f61a05b | 389 | md5_starts( ctx ); |
HannesTschofenig | 0:796d0f61a05b | 390 | md5_update( ctx, ctx->opad, 64 ); |
HannesTschofenig | 0:796d0f61a05b | 391 | md5_update( ctx, tmpbuf, 16 ); |
HannesTschofenig | 0:796d0f61a05b | 392 | md5_finish( ctx, output ); |
HannesTschofenig | 0:796d0f61a05b | 393 | |
HannesTschofenig | 0:796d0f61a05b | 394 | memset( tmpbuf, 0, sizeof( tmpbuf ) ); |
HannesTschofenig | 0:796d0f61a05b | 395 | } |
HannesTschofenig | 0:796d0f61a05b | 396 | |
HannesTschofenig | 0:796d0f61a05b | 397 | /* |
HannesTschofenig | 0:796d0f61a05b | 398 | * MD5 HMAC context reset |
HannesTschofenig | 0:796d0f61a05b | 399 | */ |
HannesTschofenig | 0:796d0f61a05b | 400 | void md5_hmac_reset( md5_context *ctx ) |
HannesTschofenig | 0:796d0f61a05b | 401 | { |
HannesTschofenig | 0:796d0f61a05b | 402 | md5_starts( ctx ); |
HannesTschofenig | 0:796d0f61a05b | 403 | md5_update( ctx, ctx->ipad, 64 ); |
HannesTschofenig | 0:796d0f61a05b | 404 | } |
HannesTschofenig | 0:796d0f61a05b | 405 | |
HannesTschofenig | 0:796d0f61a05b | 406 | /* |
HannesTschofenig | 0:796d0f61a05b | 407 | * output = HMAC-MD5( hmac key, input buffer ) |
HannesTschofenig | 0:796d0f61a05b | 408 | */ |
HannesTschofenig | 0:796d0f61a05b | 409 | void md5_hmac( const unsigned char *key, size_t keylen, |
HannesTschofenig | 0:796d0f61a05b | 410 | const unsigned char *input, size_t ilen, |
HannesTschofenig | 0:796d0f61a05b | 411 | unsigned char output[16] ) |
HannesTschofenig | 0:796d0f61a05b | 412 | { |
HannesTschofenig | 0:796d0f61a05b | 413 | md5_context ctx; |
HannesTschofenig | 0:796d0f61a05b | 414 | |
HannesTschofenig | 0:796d0f61a05b | 415 | md5_hmac_starts( &ctx, key, keylen ); |
HannesTschofenig | 0:796d0f61a05b | 416 | md5_hmac_update( &ctx, input, ilen ); |
HannesTschofenig | 0:796d0f61a05b | 417 | md5_hmac_finish( &ctx, output ); |
HannesTschofenig | 0:796d0f61a05b | 418 | |
HannesTschofenig | 0:796d0f61a05b | 419 | memset( &ctx, 0, sizeof( md5_context ) ); |
HannesTschofenig | 0:796d0f61a05b | 420 | } |
HannesTschofenig | 0:796d0f61a05b | 421 | |
HannesTschofenig | 0:796d0f61a05b | 422 | #if defined(POLARSSL_SELF_TEST) |
HannesTschofenig | 0:796d0f61a05b | 423 | /* |
HannesTschofenig | 0:796d0f61a05b | 424 | * RFC 1321 test vectors |
HannesTschofenig | 0:796d0f61a05b | 425 | */ |
HannesTschofenig | 0:796d0f61a05b | 426 | static unsigned char md5_test_buf[7][81] = |
HannesTschofenig | 0:796d0f61a05b | 427 | { |
HannesTschofenig | 0:796d0f61a05b | 428 | { "" }, |
HannesTschofenig | 0:796d0f61a05b | 429 | { "a" }, |
HannesTschofenig | 0:796d0f61a05b | 430 | { "abc" }, |
HannesTschofenig | 0:796d0f61a05b | 431 | { "message digest" }, |
HannesTschofenig | 0:796d0f61a05b | 432 | { "abcdefghijklmnopqrstuvwxyz" }, |
HannesTschofenig | 0:796d0f61a05b | 433 | { "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789" }, |
HannesTschofenig | 0:796d0f61a05b | 434 | { "12345678901234567890123456789012345678901234567890123456789012" \ |
HannesTschofenig | 0:796d0f61a05b | 435 | "345678901234567890" } |
HannesTschofenig | 0:796d0f61a05b | 436 | }; |
HannesTschofenig | 0:796d0f61a05b | 437 | |
HannesTschofenig | 0:796d0f61a05b | 438 | static const int md5_test_buflen[7] = |
HannesTschofenig | 0:796d0f61a05b | 439 | { |
HannesTschofenig | 0:796d0f61a05b | 440 | 0, 1, 3, 14, 26, 62, 80 |
HannesTschofenig | 0:796d0f61a05b | 441 | }; |
HannesTschofenig | 0:796d0f61a05b | 442 | |
HannesTschofenig | 0:796d0f61a05b | 443 | static const unsigned char md5_test_sum[7][16] = |
HannesTschofenig | 0:796d0f61a05b | 444 | { |
HannesTschofenig | 0:796d0f61a05b | 445 | { 0xD4, 0x1D, 0x8C, 0xD9, 0x8F, 0x00, 0xB2, 0x04, |
HannesTschofenig | 0:796d0f61a05b | 446 | 0xE9, 0x80, 0x09, 0x98, 0xEC, 0xF8, 0x42, 0x7E }, |
HannesTschofenig | 0:796d0f61a05b | 447 | { 0x0C, 0xC1, 0x75, 0xB9, 0xC0, 0xF1, 0xB6, 0xA8, |
HannesTschofenig | 0:796d0f61a05b | 448 | 0x31, 0xC3, 0x99, 0xE2, 0x69, 0x77, 0x26, 0x61 }, |
HannesTschofenig | 0:796d0f61a05b | 449 | { 0x90, 0x01, 0x50, 0x98, 0x3C, 0xD2, 0x4F, 0xB0, |
HannesTschofenig | 0:796d0f61a05b | 450 | 0xD6, 0x96, 0x3F, 0x7D, 0x28, 0xE1, 0x7F, 0x72 }, |
HannesTschofenig | 0:796d0f61a05b | 451 | { 0xF9, 0x6B, 0x69, 0x7D, 0x7C, 0xB7, 0x93, 0x8D, |
HannesTschofenig | 0:796d0f61a05b | 452 | 0x52, 0x5A, 0x2F, 0x31, 0xAA, 0xF1, 0x61, 0xD0 }, |
HannesTschofenig | 0:796d0f61a05b | 453 | { 0xC3, 0xFC, 0xD3, 0xD7, 0x61, 0x92, 0xE4, 0x00, |
HannesTschofenig | 0:796d0f61a05b | 454 | 0x7D, 0xFB, 0x49, 0x6C, 0xCA, 0x67, 0xE1, 0x3B }, |
HannesTschofenig | 0:796d0f61a05b | 455 | { 0xD1, 0x74, 0xAB, 0x98, 0xD2, 0x77, 0xD9, 0xF5, |
HannesTschofenig | 0:796d0f61a05b | 456 | 0xA5, 0x61, 0x1C, 0x2C, 0x9F, 0x41, 0x9D, 0x9F }, |
HannesTschofenig | 0:796d0f61a05b | 457 | { 0x57, 0xED, 0xF4, 0xA2, 0x2B, 0xE3, 0xC9, 0x55, |
HannesTschofenig | 0:796d0f61a05b | 458 | 0xAC, 0x49, 0xDA, 0x2E, 0x21, 0x07, 0xB6, 0x7A } |
HannesTschofenig | 0:796d0f61a05b | 459 | }; |
HannesTschofenig | 0:796d0f61a05b | 460 | |
HannesTschofenig | 0:796d0f61a05b | 461 | /* |
HannesTschofenig | 0:796d0f61a05b | 462 | * RFC 2202 test vectors |
HannesTschofenig | 0:796d0f61a05b | 463 | */ |
HannesTschofenig | 0:796d0f61a05b | 464 | static unsigned char md5_hmac_test_key[7][26] = |
HannesTschofenig | 0:796d0f61a05b | 465 | { |
HannesTschofenig | 0:796d0f61a05b | 466 | { "\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B" }, |
HannesTschofenig | 0:796d0f61a05b | 467 | { "Jefe" }, |
HannesTschofenig | 0:796d0f61a05b | 468 | { "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA" }, |
HannesTschofenig | 0:796d0f61a05b | 469 | { "\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F\x10" |
HannesTschofenig | 0:796d0f61a05b | 470 | "\x11\x12\x13\x14\x15\x16\x17\x18\x19" }, |
HannesTschofenig | 0:796d0f61a05b | 471 | { "\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C" }, |
HannesTschofenig | 0:796d0f61a05b | 472 | { "" }, /* 0xAA 80 times */ |
HannesTschofenig | 0:796d0f61a05b | 473 | { "" } |
HannesTschofenig | 0:796d0f61a05b | 474 | }; |
HannesTschofenig | 0:796d0f61a05b | 475 | |
HannesTschofenig | 0:796d0f61a05b | 476 | static const int md5_hmac_test_keylen[7] = |
HannesTschofenig | 0:796d0f61a05b | 477 | { |
HannesTschofenig | 0:796d0f61a05b | 478 | 16, 4, 16, 25, 16, 80, 80 |
HannesTschofenig | 0:796d0f61a05b | 479 | }; |
HannesTschofenig | 0:796d0f61a05b | 480 | |
HannesTschofenig | 0:796d0f61a05b | 481 | static unsigned char md5_hmac_test_buf[7][74] = |
HannesTschofenig | 0:796d0f61a05b | 482 | { |
HannesTschofenig | 0:796d0f61a05b | 483 | { "Hi There" }, |
HannesTschofenig | 0:796d0f61a05b | 484 | { "what do ya want for nothing?" }, |
HannesTschofenig | 0:796d0f61a05b | 485 | { "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD" |
HannesTschofenig | 0:796d0f61a05b | 486 | "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD" |
HannesTschofenig | 0:796d0f61a05b | 487 | "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD" |
HannesTschofenig | 0:796d0f61a05b | 488 | "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD" |
HannesTschofenig | 0:796d0f61a05b | 489 | "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD" }, |
HannesTschofenig | 0:796d0f61a05b | 490 | { "\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD" |
HannesTschofenig | 0:796d0f61a05b | 491 | "\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD" |
HannesTschofenig | 0:796d0f61a05b | 492 | "\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD" |
HannesTschofenig | 0:796d0f61a05b | 493 | "\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD" |
HannesTschofenig | 0:796d0f61a05b | 494 | "\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD" }, |
HannesTschofenig | 0:796d0f61a05b | 495 | { "Test With Truncation" }, |
HannesTschofenig | 0:796d0f61a05b | 496 | { "Test Using Larger Than Block-Size Key - Hash Key First" }, |
HannesTschofenig | 0:796d0f61a05b | 497 | { "Test Using Larger Than Block-Size Key and Larger" |
HannesTschofenig | 0:796d0f61a05b | 498 | " Than One Block-Size Data" } |
HannesTschofenig | 0:796d0f61a05b | 499 | }; |
HannesTschofenig | 0:796d0f61a05b | 500 | |
HannesTschofenig | 0:796d0f61a05b | 501 | static const int md5_hmac_test_buflen[7] = |
HannesTschofenig | 0:796d0f61a05b | 502 | { |
HannesTschofenig | 0:796d0f61a05b | 503 | 8, 28, 50, 50, 20, 54, 73 |
HannesTschofenig | 0:796d0f61a05b | 504 | }; |
HannesTschofenig | 0:796d0f61a05b | 505 | |
HannesTschofenig | 0:796d0f61a05b | 506 | static const unsigned char md5_hmac_test_sum[7][16] = |
HannesTschofenig | 0:796d0f61a05b | 507 | { |
HannesTschofenig | 0:796d0f61a05b | 508 | { 0x92, 0x94, 0x72, 0x7A, 0x36, 0x38, 0xBB, 0x1C, |
HannesTschofenig | 0:796d0f61a05b | 509 | 0x13, 0xF4, 0x8E, 0xF8, 0x15, 0x8B, 0xFC, 0x9D }, |
HannesTschofenig | 0:796d0f61a05b | 510 | { 0x75, 0x0C, 0x78, 0x3E, 0x6A, 0xB0, 0xB5, 0x03, |
HannesTschofenig | 0:796d0f61a05b | 511 | 0xEA, 0xA8, 0x6E, 0x31, 0x0A, 0x5D, 0xB7, 0x38 }, |
HannesTschofenig | 0:796d0f61a05b | 512 | { 0x56, 0xBE, 0x34, 0x52, 0x1D, 0x14, 0x4C, 0x88, |
HannesTschofenig | 0:796d0f61a05b | 513 | 0xDB, 0xB8, 0xC7, 0x33, 0xF0, 0xE8, 0xB3, 0xF6 }, |
HannesTschofenig | 0:796d0f61a05b | 514 | { 0x69, 0x7E, 0xAF, 0x0A, 0xCA, 0x3A, 0x3A, 0xEA, |
HannesTschofenig | 0:796d0f61a05b | 515 | 0x3A, 0x75, 0x16, 0x47, 0x46, 0xFF, 0xAA, 0x79 }, |
HannesTschofenig | 0:796d0f61a05b | 516 | { 0x56, 0x46, 0x1E, 0xF2, 0x34, 0x2E, 0xDC, 0x00, |
HannesTschofenig | 0:796d0f61a05b | 517 | 0xF9, 0xBA, 0xB9, 0x95 }, |
HannesTschofenig | 0:796d0f61a05b | 518 | { 0x6B, 0x1A, 0xB7, 0xFE, 0x4B, 0xD7, 0xBF, 0x8F, |
HannesTschofenig | 0:796d0f61a05b | 519 | 0x0B, 0x62, 0xE6, 0xCE, 0x61, 0xB9, 0xD0, 0xCD }, |
HannesTschofenig | 0:796d0f61a05b | 520 | { 0x6F, 0x63, 0x0F, 0xAD, 0x67, 0xCD, 0xA0, 0xEE, |
HannesTschofenig | 0:796d0f61a05b | 521 | 0x1F, 0xB1, 0xF5, 0x62, 0xDB, 0x3A, 0xA5, 0x3E } |
HannesTschofenig | 0:796d0f61a05b | 522 | }; |
HannesTschofenig | 0:796d0f61a05b | 523 | |
HannesTschofenig | 0:796d0f61a05b | 524 | /* |
HannesTschofenig | 0:796d0f61a05b | 525 | * Checkup routine |
HannesTschofenig | 0:796d0f61a05b | 526 | */ |
HannesTschofenig | 0:796d0f61a05b | 527 | int md5_self_test( int verbose ) |
HannesTschofenig | 0:796d0f61a05b | 528 | { |
HannesTschofenig | 0:796d0f61a05b | 529 | int i, buflen; |
HannesTschofenig | 0:796d0f61a05b | 530 | unsigned char buf[1024]; |
HannesTschofenig | 0:796d0f61a05b | 531 | unsigned char md5sum[16]; |
HannesTschofenig | 0:796d0f61a05b | 532 | md5_context ctx; |
HannesTschofenig | 0:796d0f61a05b | 533 | |
HannesTschofenig | 0:796d0f61a05b | 534 | for( i = 0; i < 7; i++ ) |
HannesTschofenig | 0:796d0f61a05b | 535 | { |
HannesTschofenig | 0:796d0f61a05b | 536 | if( verbose != 0 ) |
HannesTschofenig | 0:796d0f61a05b | 537 | polarssl_printf( " MD5 test #%d: ", i + 1 ); |
HannesTschofenig | 0:796d0f61a05b | 538 | |
HannesTschofenig | 0:796d0f61a05b | 539 | md5( md5_test_buf[i], md5_test_buflen[i], md5sum ); |
HannesTschofenig | 0:796d0f61a05b | 540 | |
HannesTschofenig | 0:796d0f61a05b | 541 | if( memcmp( md5sum, md5_test_sum[i], 16 ) != 0 ) |
HannesTschofenig | 0:796d0f61a05b | 542 | { |
HannesTschofenig | 0:796d0f61a05b | 543 | if( verbose != 0 ) |
HannesTschofenig | 0:796d0f61a05b | 544 | polarssl_printf( "failed\n" ); |
HannesTschofenig | 0:796d0f61a05b | 545 | |
HannesTschofenig | 0:796d0f61a05b | 546 | return( 1 ); |
HannesTschofenig | 0:796d0f61a05b | 547 | } |
HannesTschofenig | 0:796d0f61a05b | 548 | |
HannesTschofenig | 0:796d0f61a05b | 549 | if( verbose != 0 ) |
HannesTschofenig | 0:796d0f61a05b | 550 | polarssl_printf( "passed\n" ); |
HannesTschofenig | 0:796d0f61a05b | 551 | } |
HannesTschofenig | 0:796d0f61a05b | 552 | |
HannesTschofenig | 0:796d0f61a05b | 553 | if( verbose != 0 ) |
HannesTschofenig | 0:796d0f61a05b | 554 | polarssl_printf( "\n" ); |
HannesTschofenig | 0:796d0f61a05b | 555 | |
HannesTschofenig | 0:796d0f61a05b | 556 | for( i = 0; i < 7; i++ ) |
HannesTschofenig | 0:796d0f61a05b | 557 | { |
HannesTschofenig | 0:796d0f61a05b | 558 | if( verbose != 0 ) |
HannesTschofenig | 0:796d0f61a05b | 559 | polarssl_printf( " HMAC-MD5 test #%d: ", i + 1 ); |
HannesTschofenig | 0:796d0f61a05b | 560 | |
HannesTschofenig | 0:796d0f61a05b | 561 | if( i == 5 || i == 6 ) |
HannesTschofenig | 0:796d0f61a05b | 562 | { |
HannesTschofenig | 0:796d0f61a05b | 563 | memset( buf, '\xAA', buflen = 80 ); |
HannesTschofenig | 0:796d0f61a05b | 564 | md5_hmac_starts( &ctx, buf, buflen ); |
HannesTschofenig | 0:796d0f61a05b | 565 | } |
HannesTschofenig | 0:796d0f61a05b | 566 | else |
HannesTschofenig | 0:796d0f61a05b | 567 | md5_hmac_starts( &ctx, md5_hmac_test_key[i], |
HannesTschofenig | 0:796d0f61a05b | 568 | md5_hmac_test_keylen[i] ); |
HannesTschofenig | 0:796d0f61a05b | 569 | |
HannesTschofenig | 0:796d0f61a05b | 570 | md5_hmac_update( &ctx, md5_hmac_test_buf[i], |
HannesTschofenig | 0:796d0f61a05b | 571 | md5_hmac_test_buflen[i] ); |
HannesTschofenig | 0:796d0f61a05b | 572 | |
HannesTschofenig | 0:796d0f61a05b | 573 | md5_hmac_finish( &ctx, md5sum ); |
HannesTschofenig | 0:796d0f61a05b | 574 | |
HannesTschofenig | 0:796d0f61a05b | 575 | buflen = ( i == 4 ) ? 12 : 16; |
HannesTschofenig | 0:796d0f61a05b | 576 | |
HannesTschofenig | 0:796d0f61a05b | 577 | if( memcmp( md5sum, md5_hmac_test_sum[i], buflen ) != 0 ) |
HannesTschofenig | 0:796d0f61a05b | 578 | { |
HannesTschofenig | 0:796d0f61a05b | 579 | if( verbose != 0 ) |
HannesTschofenig | 0:796d0f61a05b | 580 | polarssl_printf( "failed\n" ); |
HannesTschofenig | 0:796d0f61a05b | 581 | |
HannesTschofenig | 0:796d0f61a05b | 582 | return( 1 ); |
HannesTschofenig | 0:796d0f61a05b | 583 | } |
HannesTschofenig | 0:796d0f61a05b | 584 | |
HannesTschofenig | 0:796d0f61a05b | 585 | if( verbose != 0 ) |
HannesTschofenig | 0:796d0f61a05b | 586 | polarssl_printf( "passed\n" ); |
HannesTschofenig | 0:796d0f61a05b | 587 | } |
HannesTschofenig | 0:796d0f61a05b | 588 | |
HannesTschofenig | 0:796d0f61a05b | 589 | if( verbose != 0 ) |
HannesTschofenig | 0:796d0f61a05b | 590 | polarssl_printf( "\n" ); |
HannesTschofenig | 0:796d0f61a05b | 591 | |
HannesTschofenig | 0:796d0f61a05b | 592 | return( 0 ); |
HannesTschofenig | 0:796d0f61a05b | 593 | } |
HannesTschofenig | 0:796d0f61a05b | 594 | |
HannesTschofenig | 0:796d0f61a05b | 595 | #endif /* POLARSSL_SELF_TEST */ |
HannesTschofenig | 0:796d0f61a05b | 596 | |
HannesTschofenig | 0:796d0f61a05b | 597 | #endif /* POLARSSL_MD5_C */ |
HannesTschofenig | 0:796d0f61a05b | 598 | |
HannesTschofenig | 0:796d0f61a05b | 599 |