]> sourceware.org Git - systemtap.git/blame - mdfour.c
Merge branch 'master' of ssh://sources.redhat.com/git/systemtap
[systemtap.git] / mdfour.c
CommitLineData
dff50e09 1/*
1b78aef5
DS
2 a implementation of MD4 designed for use in the SMB authentication protocol
3 Copyright (C) Andrew Tridgell 1997-1998.
dff50e09 4
1b78aef5
DS
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
dff50e09 9
1b78aef5
DS
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
dff50e09 14
1b78aef5
DS
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18*/
19
20#include <stdio.h>
21#include <string.h>
22#include "mdfour.h"
23
dff50e09 24/* NOTE: This code makes no attempt to be fast!
1b78aef5
DS
25
26 It assumes that a int is at least 32 bits long
27*/
28
29static struct mdfour *m;
30
31#define MASK32 (0xffffffff)
32
33#define F(X,Y,Z) ((((X)&(Y)) | ((~(X))&(Z))))
34#define G(X,Y,Z) ((((X)&(Y)) | ((X)&(Z)) | ((Y)&(Z))))
35#define H(X,Y,Z) (((X)^(Y)^(Z)))
36#define lshift(x,s) (((((x)<<(s))&MASK32) | (((x)>>(32-(s)))&MASK32)))
37
38#define ROUND1(a,b,c,d,k,s) a = lshift((a + F(b,c,d) + M[k])&MASK32, s)
39#define ROUND2(a,b,c,d,k,s) a = lshift((a + G(b,c,d) + M[k] + 0x5A827999)&MASK32,s)
40#define ROUND3(a,b,c,d,k,s) a = lshift((a + H(b,c,d) + M[k] + 0x6ED9EBA1)&MASK32,s)
41
42/* this applies md4 to 64 byte chunks */
43static void
44mdfour64(uint32_t *M)
45{
46 uint32_t AA, BB, CC, DD;
47 uint32_t A,B,C,D;
48
dff50e09 49 A = m->A; B = m->B; C = m->C; D = m->D;
1b78aef5
DS
50 AA = A; BB = B; CC = C; DD = D;
51
dff50e09 52 ROUND1(A,B,C,D, 0, 3); ROUND1(D,A,B,C, 1, 7);
1b78aef5 53 ROUND1(C,D,A,B, 2, 11); ROUND1(B,C,D,A, 3, 19);
dff50e09 54 ROUND1(A,B,C,D, 4, 3); ROUND1(D,A,B,C, 5, 7);
1b78aef5 55 ROUND1(C,D,A,B, 6, 11); ROUND1(B,C,D,A, 7, 19);
dff50e09 56 ROUND1(A,B,C,D, 8, 3); ROUND1(D,A,B,C, 9, 7);
1b78aef5 57 ROUND1(C,D,A,B, 10, 11); ROUND1(B,C,D,A, 11, 19);
dff50e09
FCE
58 ROUND1(A,B,C,D, 12, 3); ROUND1(D,A,B,C, 13, 7);
59 ROUND1(C,D,A,B, 14, 11); ROUND1(B,C,D,A, 15, 19);
1b78aef5
DS
60
61
dff50e09 62 ROUND2(A,B,C,D, 0, 3); ROUND2(D,A,B,C, 4, 5);
1b78aef5 63 ROUND2(C,D,A,B, 8, 9); ROUND2(B,C,D,A, 12, 13);
dff50e09 64 ROUND2(A,B,C,D, 1, 3); ROUND2(D,A,B,C, 5, 5);
1b78aef5 65 ROUND2(C,D,A,B, 9, 9); ROUND2(B,C,D,A, 13, 13);
dff50e09 66 ROUND2(A,B,C,D, 2, 3); ROUND2(D,A,B,C, 6, 5);
1b78aef5 67 ROUND2(C,D,A,B, 10, 9); ROUND2(B,C,D,A, 14, 13);
dff50e09 68 ROUND2(A,B,C,D, 3, 3); ROUND2(D,A,B,C, 7, 5);
1b78aef5
DS
69 ROUND2(C,D,A,B, 11, 9); ROUND2(B,C,D,A, 15, 13);
70
dff50e09 71 ROUND3(A,B,C,D, 0, 3); ROUND3(D,A,B,C, 8, 9);
1b78aef5 72 ROUND3(C,D,A,B, 4, 11); ROUND3(B,C,D,A, 12, 15);
dff50e09 73 ROUND3(A,B,C,D, 2, 3); ROUND3(D,A,B,C, 10, 9);
1b78aef5 74 ROUND3(C,D,A,B, 6, 11); ROUND3(B,C,D,A, 14, 15);
dff50e09 75 ROUND3(A,B,C,D, 1, 3); ROUND3(D,A,B,C, 9, 9);
1b78aef5 76 ROUND3(C,D,A,B, 5, 11); ROUND3(B,C,D,A, 13, 15);
dff50e09 77 ROUND3(A,B,C,D, 3, 3); ROUND3(D,A,B,C, 11, 9);
1b78aef5
DS
78 ROUND3(C,D,A,B, 7, 11); ROUND3(B,C,D,A, 15, 15);
79
dff50e09 80 A += AA; B += BB;
1b78aef5 81 C += CC; D += DD;
dff50e09
FCE
82
83 A &= MASK32; B &= MASK32;
1b78aef5
DS
84 C &= MASK32; D &= MASK32;
85
86 m->A = A; m->B = B; m->C = C; m->D = D;
87}
88
89static void
90copy64(uint32_t *M, const unsigned char *in)
91{
92 int i;
93
94 for (i=0;i<16;i++)
95 M[i] = (in[i*4+3]<<24) | (in[i*4+2]<<16) |
96 (in[i*4+1]<<8) | (in[i*4+0]<<0);
97}
98
99static void
100copy4(unsigned char *out,uint32_t x)
101{
102 out[0] = x&0xFF;
103 out[1] = (x>>8)&0xFF;
104 out[2] = (x>>16)&0xFF;
105 out[3] = (x>>24)&0xFF;
106}
107
108void
109mdfour_begin(struct mdfour *md)
110{
111 md->A = 0x67452301;
112 md->B = 0xefcdab89;
113 md->C = 0x98badcfe;
114 md->D = 0x10325476;
115 md->totalN = 0;
116 md->tail_len = 0;
117}
118
119
120static void
121mdfour_tail(const unsigned char *in, int n)
122{
123 unsigned char buf[128];
124 uint32_t M[16];
125 uint32_t b;
126
127 m->totalN += n;
128
129 b = m->totalN * 8;
130
131 memset(buf, 0, 128);
132 if (n) memcpy(buf, in, n);
133 buf[n] = 0x80;
134
135 if (n <= 55)
136 {
137 copy4(buf+56, b);
138 copy64(M, buf);
139 mdfour64(M);
140 }
141 else
142 {
dff50e09 143 copy4(buf+120, b);
1b78aef5
DS
144 copy64(M, buf);
145 mdfour64(M);
146 copy64(M, buf+64);
147 mdfour64(M);
148 }
149}
150
151void
152mdfour_update(struct mdfour *md, const unsigned char *in, int n)
153{
154 uint32_t M[16];
155
156 m = md;
157
158 if (in == NULL)
159 {
160 mdfour_tail(md->tail, md->tail_len);
161 return;
162 }
163
164 if (md->tail_len)
165 {
166 int len = 64 - md->tail_len;
167 if (len > n) len = n;
168 memcpy(md->tail+md->tail_len, in, len);
169 md->tail_len += len;
170 n -= len;
171 in += len;
172 if (md->tail_len == 64)
173 {
174 copy64(M, md->tail);
175 mdfour64(M);
176 m->totalN += 64;
177 md->tail_len = 0;
178 }
179 }
180
181 while (n >= 64)
182 {
183 copy64(M, in);
184 mdfour64(M);
185 in += 64;
186 n -= 64;
187 m->totalN += 64;
188 }
189
190 if (n)
191 {
192 memcpy(md->tail, in, n);
193 md->tail_len = n;
194 }
195}
196
197
198void
199mdfour_result(struct mdfour *md, unsigned char *out)
200{
201 m = md;
202
203 copy4(out, m->A);
204 copy4(out+4, m->B);
205 copy4(out+8, m->C);
206 copy4(out+12, m->D);
207}
208
209
210void
211mdfour(unsigned char *out, const unsigned char *in, int n)
212{
213 struct mdfour md;
214 mdfour_begin(&md);
215 mdfour_update(&md, in, n);
216 mdfour_update(&md, NULL, 0);
217 mdfour_result(&md, out);
218}
0c5f5812
JS
219
220/* vim: set sw=2 ts=8 cino=>4,n-2,{2,^-2,t0,(0,u0,w1,M1 : */
This page took 0.070706 seconds and 5 git commands to generate.