Fork of CyaSSL for my specific settings

Dependents:   CyaSSL_Example

Fork of CyaSSL by wolf SSL

Committer:
d0773d
Date:
Tue Mar 03 22:52:52 2015 +0000
Revision:
4:28ac50e1d49c
Parent:
0:1239e9b70ca2
CyaSSL example

Who changed what in which revision?

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