Debian Codegraph @

src/MD4.c

raw symbols SCIP
1 /*
2 * md4.c : MD4 hash algorithm.
3 *
4 * Part of the Python Cryptography Toolkit
5 *
6 * Originally written by: A.M. Kuchling
7 *
8 * ===================================================================
9 * The contents of this file are dedicated to the public domain. To
10 * the extent that dedication to the public domain is not available,
11 * everyone is granted a worldwide, perpetual, royalty-free,
12 * non-exclusive license to exercise all rights associated with the
13 * contents of this file for any purpose whatsoever.
14 * No rights are reserved.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
20 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
21 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 * SOFTWARE.
24 * ===================================================================
25 *
26 */
27
28 #include "common.h"
29 #include "endianess.h"
30
31 FAKE_INIT(MD4)
32
33 typedef struct {
34 uint32_t A,B,C,D;
35 uint64_t bitlen;
36 uint8_t buf[64];
37 unsigned count;
38 } hash_state;
39
40 #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
41 #define G(x, y, z) (((x) & (y)) | ((x) & (z)) | ((y) & (z)))
42 #define H(x, y, z) ((x) ^ (y) ^ (z))
43
44 /* ROTATE_LEFT rotates x left n bits */
45 #define ROL(x, n) (((x) << n) | ((x) >> (32-n) ))
46
47 EXPORT_SYM int md4_init (hash_state **md4State)
48 {
49 hash_state *hs;
50
51 if (NULL == md4State) {
52 return ERR_NULL;
53 }
54
55 *md4State = hs = (hash_state*) calloc(1, sizeof(hash_state));
56 if (NULL == hs)
57 return ERR_MEMORY;
58
59 hs->A=0x67452301;
60 hs->B=0xefcdab89;
61 hs->C=0x98badcfe;
62 hs->D=0x10325476;
63
64 return 0;
65 }
66
67 EXPORT_SYM int md4_destroy(hash_state *hs)
68 {
69 free(hs);
70 return 0;
71 }
72
73 EXPORT_SYM int md4_copy(const hash_state *src, hash_state *dst)
74 {
75 if (NULL == src || NULL == dst) {
76 return ERR_NULL;
77 }
78
79 *dst = *src;
80 return 0;
81 }
82
83 EXPORT_SYM int md4_update(hash_state *hs, const uint8_t *buf, size_t len)
84 {
85 if (NULL == hs || NULL == buf) {
86 return ERR_NULL;
87 }
88
89 assert(hs->count < 64);
90
91 hs->bitlen += (uint64_t)len * 8;
92
93 while (len>0) {
94 unsigned left, tc;
95
96 left = 64 - hs->count;
97 tc = (unsigned)MIN(left, len);
98 memcpy(hs->buf+hs->count, buf, tc);
99 hs->count += tc;
100 buf += tc;
101 len -= tc;
102
103 if (hs->count==64) {
104 uint32_t X[16], A, B, C, D;
105 unsigned j;
106
107 hs->count=0;
108 for(j=0; j<16; j++) {
109 X[j] = LOAD_U32_LITTLE(&hs->buf[j*4]);
110 }
111
112 A=hs->A; B=hs->B; C=hs->C; D=hs->D;
113
114 #define function(a,b,c,d,k,s) a=ROL(a+F(b,c,d)+X[k],s);
115 function(A,B,C,D, 0, 3);
116 function(D,A,B,C, 1, 7);
117 function(C,D,A,B, 2,11);
118 function(B,C,D,A, 3,19);
119 function(A,B,C,D, 4, 3);
120 function(D,A,B,C, 5, 7);
121 function(C,D,A,B, 6,11);
122 function(B,C,D,A, 7,19);
123 function(A,B,C,D, 8, 3);
124 function(D,A,B,C, 9, 7);
125 function(C,D,A,B,10,11);
126 function(B,C,D,A,11,19);
127 function(A,B,C,D,12, 3);
128 function(D,A,B,C,13, 7);
129 function(C,D,A,B,14,11);
130 function(B,C,D,A,15,19);
131
132 #undef function
133 #define function(a,b,c,d,k,s) a=ROL(a+G(b,c,d)+X[k]+(uint32_t)0x5a827999,s);
134 function(A,B,C,D, 0, 3);
135 function(D,A,B,C, 4, 5);
136 function(C,D,A,B, 8, 9);
137 function(B,C,D,A,12,13);
138 function(A,B,C,D, 1, 3);
139 function(D,A,B,C, 5, 5);
140 function(C,D,A,B, 9, 9);
141 function(B,C,D,A,13,13);
142 function(A,B,C,D, 2, 3);
143 function(D,A,B,C, 6, 5);
144 function(C,D,A,B,10, 9);
145 function(B,C,D,A,14,13);
146 function(A,B,C,D, 3, 3);
147 function(D,A,B,C, 7, 5);
148 function(C,D,A,B,11, 9);
149 function(B,C,D,A,15,13);
150
151 #undef function
152 #define function(a,b,c,d,k,s) a=ROL(a+H(b,c,d)+X[k]+(uint32_t)0x6ed9eba1,s);
153 function(A,B,C,D, 0, 3);
154 function(D,A,B,C, 8, 9);
155 function(C,D,A,B, 4,11);
156 function(B,C,D,A,12,15);
157 function(A,B,C,D, 2, 3);
158 function(D,A,B,C,10, 9);
159 function(C,D,A,B, 6,11);
160 function(B,C,D,A,14,15);
161 function(A,B,C,D, 1, 3);
162 function(D,A,B,C, 9, 9);
163 function(C,D,A,B, 5,11);
164 function(B,C,D,A,13,15);
165 function(A,B,C,D, 3, 3);
166 function(D,A,B,C,11, 9);
167 function(C,D,A,B, 7,11);
168 function(B,C,D,A,15,15);
169
170 hs->A+=A; hs->B+=B; hs->C+=C; hs->D+=D;
171 }
172 }
173
174 return 0;
175 }
176
177 EXPORT_SYM int md4_digest(const hash_state *hs, uint8_t digest[16])
178 {
179 static uint8_t s[8];
180 uint32_t padlen;
181 hash_state temp;
182 unsigned i;
183 uint64_t bitlen;
184
185 static const uint8_t padding[64] = {
186 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
187 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
188 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
189 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
190 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
191 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
192 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
193 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
194 };
195
196 if (NULL==hs || NULL==digest)
197 return ERR_NULL;
198
199 temp = *hs;
200
201 bitlen = temp.bitlen; /* Save current length */
202
203 padlen= (56<=hs->count) ? 56-hs->count+64: 56-hs->count;
204 md4_update(&temp, padding, padlen);
205
206 for (i=0; i<8; i++)
207 s[i] = (uint8_t)(bitlen >> (i*8));
208 md4_update(&temp, s, 8);
209
210 STORE_U32_LITTLE(&digest[0], temp.A);
211 STORE_U32_LITTLE(&digest[4], temp.B);
212 STORE_U32_LITTLE(&digest[8], temp.C);
213 STORE_U32_LITTLE(&digest[12], temp.D);
214
215 return 0;
216 }