wolfSSL 3.11.1 for TLS1.3 beta

Fork of wolfSSL by wolf SSL

Committer:
wolfSSL
Date:
Fri Jun 26 00:39:20 2015 +0000
Revision:
0:d92f9d21154c
wolfSSL 3.6.0

Who changed what in which revision?

UserRevisionLine numberNew contents of line
wolfSSL 0:d92f9d21154c 1 /* md2.c
wolfSSL 0:d92f9d21154c 2 *
wolfSSL 0:d92f9d21154c 3 * Copyright (C) 2006-2015 wolfSSL Inc.
wolfSSL 0:d92f9d21154c 4 *
wolfSSL 0:d92f9d21154c 5 * This file is part of wolfSSL. (formerly known as CyaSSL)
wolfSSL 0:d92f9d21154c 6 *
wolfSSL 0:d92f9d21154c 7 * wolfSSL is free software; you can redistribute it and/or modify
wolfSSL 0:d92f9d21154c 8 * it under the terms of the GNU General Public License as published by
wolfSSL 0:d92f9d21154c 9 * the Free Software Foundation; either version 2 of the License, or
wolfSSL 0:d92f9d21154c 10 * (at your option) any later version.
wolfSSL 0:d92f9d21154c 11 *
wolfSSL 0:d92f9d21154c 12 * wolfSSL is distributed in the hope that it will be useful,
wolfSSL 0:d92f9d21154c 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
wolfSSL 0:d92f9d21154c 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
wolfSSL 0:d92f9d21154c 15 * GNU General Public License for more details.
wolfSSL 0:d92f9d21154c 16 *
wolfSSL 0:d92f9d21154c 17 * You should have received a copy of the GNU General Public License
wolfSSL 0:d92f9d21154c 18 * along with this program; if not, write to the Free Software
wolfSSL 0:d92f9d21154c 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
wolfSSL 0:d92f9d21154c 20 */
wolfSSL 0:d92f9d21154c 21
wolfSSL 0:d92f9d21154c 22
wolfSSL 0:d92f9d21154c 23 #ifdef HAVE_CONFIG_H
wolfSSL 0:d92f9d21154c 24 #include <config.h>
wolfSSL 0:d92f9d21154c 25 #endif
wolfSSL 0:d92f9d21154c 26
wolfSSL 0:d92f9d21154c 27 #include <wolfssl/wolfcrypt/settings.h>
wolfSSL 0:d92f9d21154c 28
wolfSSL 0:d92f9d21154c 29 #ifdef WOLFSSL_MD2
wolfSSL 0:d92f9d21154c 30
wolfSSL 0:d92f9d21154c 31 #include <wolfssl/wolfcrypt/md2.h>
wolfSSL 0:d92f9d21154c 32 #include <wolfssl/wolfcrypt/error-crypt.h>
wolfSSL 0:d92f9d21154c 33
wolfSSL 0:d92f9d21154c 34 #ifdef NO_INLINE
wolfSSL 0:d92f9d21154c 35 #include <wolfssl/wolfcrypt/misc.h>
wolfSSL 0:d92f9d21154c 36 #else
wolfSSL 0:d92f9d21154c 37 #include <wolfcrypt/src/misc.c>
wolfSSL 0:d92f9d21154c 38 #endif
wolfSSL 0:d92f9d21154c 39
wolfSSL 0:d92f9d21154c 40
wolfSSL 0:d92f9d21154c 41 void wc_InitMd2(Md2* md2)
wolfSSL 0:d92f9d21154c 42 {
wolfSSL 0:d92f9d21154c 43 XMEMSET(md2->X, 0, MD2_X_SIZE);
wolfSSL 0:d92f9d21154c 44 XMEMSET(md2->C, 0, MD2_BLOCK_SIZE);
wolfSSL 0:d92f9d21154c 45 XMEMSET(md2->buffer, 0, MD2_BLOCK_SIZE);
wolfSSL 0:d92f9d21154c 46 md2->count = 0;
wolfSSL 0:d92f9d21154c 47 }
wolfSSL 0:d92f9d21154c 48
wolfSSL 0:d92f9d21154c 49
wolfSSL 0:d92f9d21154c 50 void wc_Md2Update(Md2* md2, const byte* data, word32 len)
wolfSSL 0:d92f9d21154c 51 {
wolfSSL 0:d92f9d21154c 52 static const byte S[256] =
wolfSSL 0:d92f9d21154c 53 {
wolfSSL 0:d92f9d21154c 54 41, 46, 67, 201, 162, 216, 124, 1, 61, 54, 84, 161, 236, 240, 6,
wolfSSL 0:d92f9d21154c 55 19, 98, 167, 5, 243, 192, 199, 115, 140, 152, 147, 43, 217, 188,
wolfSSL 0:d92f9d21154c 56 76, 130, 202, 30, 155, 87, 60, 253, 212, 224, 22, 103, 66, 111, 24,
wolfSSL 0:d92f9d21154c 57 138, 23, 229, 18, 190, 78, 196, 214, 218, 158, 222, 73, 160, 251,
wolfSSL 0:d92f9d21154c 58 245, 142, 187, 47, 238, 122, 169, 104, 121, 145, 21, 178, 7, 63,
wolfSSL 0:d92f9d21154c 59 148, 194, 16, 137, 11, 34, 95, 33, 128, 127, 93, 154, 90, 144, 50,
wolfSSL 0:d92f9d21154c 60 39, 53, 62, 204, 231, 191, 247, 151, 3, 255, 25, 48, 179, 72, 165,
wolfSSL 0:d92f9d21154c 61 181, 209, 215, 94, 146, 42, 172, 86, 170, 198, 79, 184, 56, 210,
wolfSSL 0:d92f9d21154c 62 150, 164, 125, 182, 118, 252, 107, 226, 156, 116, 4, 241, 69, 157,
wolfSSL 0:d92f9d21154c 63 112, 89, 100, 113, 135, 32, 134, 91, 207, 101, 230, 45, 168, 2, 27,
wolfSSL 0:d92f9d21154c 64 96, 37, 173, 174, 176, 185, 246, 28, 70, 97, 105, 52, 64, 126, 15,
wolfSSL 0:d92f9d21154c 65 85, 71, 163, 35, 221, 81, 175, 58, 195, 92, 249, 206, 186, 197,
wolfSSL 0:d92f9d21154c 66 234, 38, 44, 83, 13, 110, 133, 40, 132, 9, 211, 223, 205, 244, 65,
wolfSSL 0:d92f9d21154c 67 129, 77, 82, 106, 220, 55, 200, 108, 193, 171, 250, 36, 225, 123,
wolfSSL 0:d92f9d21154c 68 8, 12, 189, 177, 74, 120, 136, 149, 139, 227, 99, 232, 109, 233,
wolfSSL 0:d92f9d21154c 69 203, 213, 254, 59, 0, 29, 57, 242, 239, 183, 14, 102, 88, 208, 228,
wolfSSL 0:d92f9d21154c 70 166, 119, 114, 248, 235, 117, 75, 10, 49, 68, 80, 180, 143, 237,
wolfSSL 0:d92f9d21154c 71 31, 26, 219, 153, 141, 51, 159, 17, 131, 20
wolfSSL 0:d92f9d21154c 72 };
wolfSSL 0:d92f9d21154c 73
wolfSSL 0:d92f9d21154c 74 while (len) {
wolfSSL 0:d92f9d21154c 75 word32 L = (MD2_PAD_SIZE - md2->count) < len ?
wolfSSL 0:d92f9d21154c 76 (MD2_PAD_SIZE - md2->count) : len;
wolfSSL 0:d92f9d21154c 77 XMEMCPY(md2->buffer + md2->count, data, L);
wolfSSL 0:d92f9d21154c 78 md2->count += L;
wolfSSL 0:d92f9d21154c 79 data += L;
wolfSSL 0:d92f9d21154c 80 len -= L;
wolfSSL 0:d92f9d21154c 81
wolfSSL 0:d92f9d21154c 82 if (md2->count == MD2_PAD_SIZE) {
wolfSSL 0:d92f9d21154c 83 int i;
wolfSSL 0:d92f9d21154c 84 byte t;
wolfSSL 0:d92f9d21154c 85
wolfSSL 0:d92f9d21154c 86 md2->count = 0;
wolfSSL 0:d92f9d21154c 87 XMEMCPY(md2->X + MD2_PAD_SIZE, md2->buffer, MD2_PAD_SIZE);
wolfSSL 0:d92f9d21154c 88 t = md2->C[15];
wolfSSL 0:d92f9d21154c 89
wolfSSL 0:d92f9d21154c 90 for(i = 0; i < MD2_PAD_SIZE; i++) {
wolfSSL 0:d92f9d21154c 91 md2->X[32 + i] = md2->X[MD2_PAD_SIZE + i] ^ md2->X[i];
wolfSSL 0:d92f9d21154c 92 t = md2->C[i] ^= S[md2->buffer[i] ^ t];
wolfSSL 0:d92f9d21154c 93 }
wolfSSL 0:d92f9d21154c 94
wolfSSL 0:d92f9d21154c 95 t=0;
wolfSSL 0:d92f9d21154c 96 for(i = 0; i < 18; i++) {
wolfSSL 0:d92f9d21154c 97 int j;
wolfSSL 0:d92f9d21154c 98 for(j = 0; j < MD2_X_SIZE; j += 8) {
wolfSSL 0:d92f9d21154c 99 t = md2->X[j+0] ^= S[t];
wolfSSL 0:d92f9d21154c 100 t = md2->X[j+1] ^= S[t];
wolfSSL 0:d92f9d21154c 101 t = md2->X[j+2] ^= S[t];
wolfSSL 0:d92f9d21154c 102 t = md2->X[j+3] ^= S[t];
wolfSSL 0:d92f9d21154c 103 t = md2->X[j+4] ^= S[t];
wolfSSL 0:d92f9d21154c 104 t = md2->X[j+5] ^= S[t];
wolfSSL 0:d92f9d21154c 105 t = md2->X[j+6] ^= S[t];
wolfSSL 0:d92f9d21154c 106 t = md2->X[j+7] ^= S[t];
wolfSSL 0:d92f9d21154c 107 }
wolfSSL 0:d92f9d21154c 108 t = (t + i) & 0xFF;
wolfSSL 0:d92f9d21154c 109 }
wolfSSL 0:d92f9d21154c 110 }
wolfSSL 0:d92f9d21154c 111 }
wolfSSL 0:d92f9d21154c 112 }
wolfSSL 0:d92f9d21154c 113
wolfSSL 0:d92f9d21154c 114
wolfSSL 0:d92f9d21154c 115 void wc_Md2Final(Md2* md2, byte* hash)
wolfSSL 0:d92f9d21154c 116 {
wolfSSL 0:d92f9d21154c 117 byte padding[MD2_BLOCK_SIZE];
wolfSSL 0:d92f9d21154c 118 word32 padLen = MD2_PAD_SIZE - md2->count;
wolfSSL 0:d92f9d21154c 119 word32 i;
wolfSSL 0:d92f9d21154c 120
wolfSSL 0:d92f9d21154c 121 for (i = 0; i < padLen; i++)
wolfSSL 0:d92f9d21154c 122 padding[i] = (byte)padLen;
wolfSSL 0:d92f9d21154c 123
wolfSSL 0:d92f9d21154c 124 wc_Md2Update(md2, padding, padLen);
wolfSSL 0:d92f9d21154c 125 wc_Md2Update(md2, md2->C, MD2_BLOCK_SIZE);
wolfSSL 0:d92f9d21154c 126
wolfSSL 0:d92f9d21154c 127 XMEMCPY(hash, md2->X, MD2_DIGEST_SIZE);
wolfSSL 0:d92f9d21154c 128
wolfSSL 0:d92f9d21154c 129 wc_InitMd2(md2);
wolfSSL 0:d92f9d21154c 130 }
wolfSSL 0:d92f9d21154c 131
wolfSSL 0:d92f9d21154c 132
wolfSSL 0:d92f9d21154c 133 int wc_Md2Hash(const byte* data, word32 len, byte* hash)
wolfSSL 0:d92f9d21154c 134 {
wolfSSL 0:d92f9d21154c 135 #ifdef WOLFSSL_SMALL_STACK
wolfSSL 0:d92f9d21154c 136 Md2* md2;
wolfSSL 0:d92f9d21154c 137 #else
wolfSSL 0:d92f9d21154c 138 Md2 md2[1];
wolfSSL 0:d92f9d21154c 139 #endif
wolfSSL 0:d92f9d21154c 140
wolfSSL 0:d92f9d21154c 141 #ifdef WOLFSSL_SMALL_STACK
wolfSSL 0:d92f9d21154c 142 md2 = (Md2*)XMALLOC(sizeof(Md2), NULL, DYNAMIC_TYPE_TMP_BUFFER);
wolfSSL 0:d92f9d21154c 143 if (md2 == NULL)
wolfSSL 0:d92f9d21154c 144 return MEMORY_E;
wolfSSL 0:d92f9d21154c 145 #endif
wolfSSL 0:d92f9d21154c 146
wolfSSL 0:d92f9d21154c 147 wc_InitMd2(md2);
wolfSSL 0:d92f9d21154c 148 wc_Md2Update(md2, data, len);
wolfSSL 0:d92f9d21154c 149 wc_Md2Final(md2, hash);
wolfSSL 0:d92f9d21154c 150
wolfSSL 0:d92f9d21154c 151 #ifdef WOLFSSL_SMALL_STACK
wolfSSL 0:d92f9d21154c 152 XFREE(md2, NULL, DYNAMIC_TYPE_TMP_BUFFER);
wolfSSL 0:d92f9d21154c 153 #endif
wolfSSL 0:d92f9d21154c 154
wolfSSL 0:d92f9d21154c 155 return 0;
wolfSSL 0:d92f9d21154c 156 }
wolfSSL 0:d92f9d21154c 157
wolfSSL 0:d92f9d21154c 158
wolfSSL 0:d92f9d21154c 159 #endif /* WOLFSSL_MD2 */
wolfSSL 0:d92f9d21154c 160
wolfSSL 0:d92f9d21154c 161