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