Anope IRC Services  Version 1.8
enc_md5.c
Go to the documentation of this file.
1 /* Module for encryption using MD5.
2  *
3  * Modified for Anope.
4  * (C) 2003-2014 Anope Team
5  * Contact us at team@anope.org
6  *
7  * Taken from IRC Services and is copyright (c) 1996-2002 Andrew Church.
8  * E-mail: <achurch@achurch.org>
9  * Parts written by Andrew Kempe and others.
10  * This program is free but copyrighted software; see the file COPYING for
11  * details.
12  */
13 
14 #include "module.h"
15 
16 
17 /*************************************************************************/
18 
19 /* Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All
20 rights reserved.
21 
22 License to copy and use this software is granted provided that it
23 is identified as the "RSA Data Security, Inc. MD5 Message-Digest
24 Algorithm" in all material mentioning or referencing this software
25 or this function.
26 
27 License is also granted to make and use derivative works provided
28 that such works are identified as "derived from the RSA Data
29 Security, Inc. MD5 Message-Digest Algorithm" in all material
30 mentioning or referencing the derived work.
31 
32 RSA Data Security, Inc. makes no representations concerning either
33 the merchantability of this software or the suitability of this
34 software for any particular purpose. It is provided "as is"
35 without express or implied warranty of any kind.
36 
37 These notices must be retained in any copies of any part of this
38 documentation and/or software.
39  */
40 
41 #include <string.h>
42 
43 typedef unsigned int UINT4;
44 
45 /* MD5 context. */
46 typedef struct {
47  UINT4 state[4]; /* state (ABCD) */
48  UINT4 count[2]; /* number of bits, modulo 2^64 (lsb first) */
49  unsigned char buffer[64]; /* input buffer */
50 } MD5_CTX;
51 
52 /* MD5C.C - RSA Data Security, Inc., MD5 message-digest algorithm
53  */
54 
55 typedef void *POINTER;
56 
57 /* Constants for MD5Transform routine.
58  */
59 #define S11 7
60 #define S12 12
61 #define S13 17
62 #define S14 22
63 #define S21 5
64 #define S22 9
65 #define S23 14
66 #define S24 20
67 #define S31 4
68 #define S32 11
69 #define S33 16
70 #define S34 23
71 #define S41 6
72 #define S42 10
73 #define S43 15
74 #define S44 21
75 
76 void Ano_MD5Transform (UINT4 [4], unsigned char [64]);
77 static void Encode (unsigned char *, UINT4 *, unsigned int);
78 static void Decode (UINT4 *, unsigned char *, unsigned int);
79 
80 static unsigned char PADDING[64] = {
81  0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
82  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
83  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
84 };
85 
86 /* F, G, H and I are basic MD5 functions.
87  */
88 #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
89 #define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
90 #define H(x, y, z) ((x) ^ (y) ^ (z))
91 #define I(x, y, z) ((y) ^ ((x) | (~z)))
92 
93 /* ROTATE_LEFT rotates x left n bits.
94  */
95 #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
96 
97 /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
98 Rotation is separate from addition to prevent recomputation.
99  */
100 #define FF(a, b, c, d, x, s, ac) { \
101  (a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
102  (a) = ROTATE_LEFT ((a), (s)); \
103  (a) += (b); \
104  }
105 #define GG(a, b, c, d, x, s, ac) { \
106  (a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
107  (a) = ROTATE_LEFT ((a), (s)); \
108  (a) += (b); \
109  }
110 #define HH(a, b, c, d, x, s, ac) { \
111  (a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
112  (a) = ROTATE_LEFT ((a), (s)); \
113  (a) += (b); \
114  }
115 #define II(a, b, c, d, x, s, ac) { \
116  (a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
117  (a) = ROTATE_LEFT ((a), (s)); \
118  (a) += (b); \
119  }
120 
121 /* MD5 initialization. Begins an MD5 operation, writing a new context.
122  */
123 void Ano_MD5Init (context)
124 MD5_CTX *context; /* context */
125 {
126  context->count[0] = context->count[1] = 0;
127  /* Load magic initialization constants.
128 */
129  context->state[0] = 0x67452301;
130  context->state[1] = 0xefcdab89;
131  context->state[2] = 0x98badcfe;
132  context->state[3] = 0x10325476;
133 }
134 
135 /* MD5 block update operation. Continues an MD5 message-digest
136  operation, processing another message block, and updating the
137  context.
138  */
139 void Ano_MD5Update (context, input, inputLen)
140 MD5_CTX *context; /* context */
141 unsigned char *input; /* input block */
142 unsigned int inputLen; /* length of input block */
143 {
144  unsigned int i, index, partLen;
145 
146  /* Compute number of bytes mod 64 */
147  index = (unsigned int)((context->count[0] >> 3) & 0x3F);
148 
149  /* Update number of bits */
150  if ((context->count[0] += ((UINT4)inputLen << 3))
151  < ((UINT4)inputLen << 3))
152  context->count[1]++;
153  context->count[1] += ((UINT4)inputLen >> 29);
154 
155  partLen = 64 - index;
156 
157  /* Transform as many times as possible.
158 */
159  if (inputLen >= partLen) {
160  memcpy
161  ((POINTER)&context->buffer[index], (POINTER)input, partLen);
162  Ano_MD5Transform (context->state, context->buffer);
163 
164  for (i = partLen; i + 63 < inputLen; i += 64)
165  Ano_MD5Transform (context->state, &input[i]);
166 
167  index = 0;
168  }
169  else
170  i = 0;
171 
172  /* Buffer remaining input */
173  memcpy
174  ((POINTER)&context->buffer[index], (POINTER)&input[i],
175  inputLen-i);
176 }
177 
178 /* MD5 finalization. Ends an MD5 message-digest operation, writing the
179  the message digest and zeroizing the context.
180  */
181 void Ano_MD5Final (digest, context)
182 unsigned char digest[16]; /* message digest */
183 MD5_CTX *context; /* context */
184 {
185  unsigned char bits[8];
186  unsigned int index, padLen;
187 
188  /* Save number of bits */
189  Encode (bits, context->count, 8);
190 
191  /* Pad out to 56 mod 64.
192 */
193  index = (unsigned int)((context->count[0] >> 3) & 0x3f);
194  padLen = (index < 56) ? (56 - index) : (120 - index);
195  Ano_MD5Update (context, PADDING, padLen);
196 
197  /* Append length (before padding) */
198  Ano_MD5Update (context, bits, 8);
199  /* Store state in digest */
200  Encode (digest, context->state, 16);
201 
202  /* Zeroize sensitive information.
203 */
204  memset ((POINTER)context, 0, sizeof (*context));
205 }
206 
207 /* MD5 basic transformation. Transforms state based on block.
208  */
209 void Ano_MD5Transform (state, block)
210 UINT4 state[4];
211 unsigned char block[64];
212 {
213  UINT4 a = state[0], b = state[1], c = state[2], d = state[3], x[16];
214 
215  Decode (x, block, 64);
216 
217  /* Round 1 */
218  FF (a, b, c, d, x[ 0], S11, 0xd76aa478); /* 1 */
219  FF (d, a, b, c, x[ 1], S12, 0xe8c7b756); /* 2 */
220  FF (c, d, a, b, x[ 2], S13, 0x242070db); /* 3 */
221  FF (b, c, d, a, x[ 3], S14, 0xc1bdceee); /* 4 */
222  FF (a, b, c, d, x[ 4], S11, 0xf57c0faf); /* 5 */
223  FF (d, a, b, c, x[ 5], S12, 0x4787c62a); /* 6 */
224  FF (c, d, a, b, x[ 6], S13, 0xa8304613); /* 7 */
225  FF (b, c, d, a, x[ 7], S14, 0xfd469501); /* 8 */
226  FF (a, b, c, d, x[ 8], S11, 0x698098d8); /* 9 */
227  FF (d, a, b, c, x[ 9], S12, 0x8b44f7af); /* 10 */
228  FF (c, d, a, b, x[10], S13, 0xffff5bb1); /* 11 */
229  FF (b, c, d, a, x[11], S14, 0x895cd7be); /* 12 */
230  FF (a, b, c, d, x[12], S11, 0x6b901122); /* 13 */
231  FF (d, a, b, c, x[13], S12, 0xfd987193); /* 14 */
232  FF (c, d, a, b, x[14], S13, 0xa679438e); /* 15 */
233  FF (b, c, d, a, x[15], S14, 0x49b40821); /* 16 */
234 
235  /* Round 2 */
236  GG (a, b, c, d, x[ 1], S21, 0xf61e2562); /* 17 */
237  GG (d, a, b, c, x[ 6], S22, 0xc040b340); /* 18 */
238  GG (c, d, a, b, x[11], S23, 0x265e5a51); /* 19 */
239  GG (b, c, d, a, x[ 0], S24, 0xe9b6c7aa); /* 20 */
240  GG (a, b, c, d, x[ 5], S21, 0xd62f105d); /* 21 */
241  GG (d, a, b, c, x[10], S22, 0x2441453); /* 22 */
242  GG (c, d, a, b, x[15], S23, 0xd8a1e681); /* 23 */
243  GG (b, c, d, a, x[ 4], S24, 0xe7d3fbc8); /* 24 */
244  GG (a, b, c, d, x[ 9], S21, 0x21e1cde6); /* 25 */
245  GG (d, a, b, c, x[14], S22, 0xc33707d6); /* 26 */
246  GG (c, d, a, b, x[ 3], S23, 0xf4d50d87); /* 27 */
247  GG (b, c, d, a, x[ 8], S24, 0x455a14ed); /* 28 */
248  GG (a, b, c, d, x[13], S21, 0xa9e3e905); /* 29 */
249  GG (d, a, b, c, x[ 2], S22, 0xfcefa3f8); /* 30 */
250  GG (c, d, a, b, x[ 7], S23, 0x676f02d9); /* 31 */
251  GG (b, c, d, a, x[12], S24, 0x8d2a4c8a); /* 32 */
252 
253  /* Round 3 */
254  HH (a, b, c, d, x[ 5], S31, 0xfffa3942); /* 33 */
255  HH (d, a, b, c, x[ 8], S32, 0x8771f681); /* 34 */
256  HH (c, d, a, b, x[11], S33, 0x6d9d6122); /* 35 */
257  HH (b, c, d, a, x[14], S34, 0xfde5380c); /* 36 */
258  HH (a, b, c, d, x[ 1], S31, 0xa4beea44); /* 37 */
259  HH (d, a, b, c, x[ 4], S32, 0x4bdecfa9); /* 38 */
260  HH (c, d, a, b, x[ 7], S33, 0xf6bb4b60); /* 39 */
261  HH (b, c, d, a, x[10], S34, 0xbebfbc70); /* 40 */
262  HH (a, b, c, d, x[13], S31, 0x289b7ec6); /* 41 */
263  HH (d, a, b, c, x[ 0], S32, 0xeaa127fa); /* 42 */
264  HH (c, d, a, b, x[ 3], S33, 0xd4ef3085); /* 43 */
265  HH (b, c, d, a, x[ 6], S34, 0x4881d05); /* 44 */
266  HH (a, b, c, d, x[ 9], S31, 0xd9d4d039); /* 45 */
267  HH (d, a, b, c, x[12], S32, 0xe6db99e5); /* 46 */
268  HH (c, d, a, b, x[15], S33, 0x1fa27cf8); /* 47 */
269  HH (b, c, d, a, x[ 2], S34, 0xc4ac5665); /* 48 */
270 
271  /* Round 4 */
272  II (a, b, c, d, x[ 0], S41, 0xf4292244); /* 49 */
273  II (d, a, b, c, x[ 7], S42, 0x432aff97); /* 50 */
274  II (c, d, a, b, x[14], S43, 0xab9423a7); /* 51 */
275  II (b, c, d, a, x[ 5], S44, 0xfc93a039); /* 52 */
276  II (a, b, c, d, x[12], S41, 0x655b59c3); /* 53 */
277  II (d, a, b, c, x[ 3], S42, 0x8f0ccc92); /* 54 */
278  II (c, d, a, b, x[10], S43, 0xffeff47d); /* 55 */
279  II (b, c, d, a, x[ 1], S44, 0x85845dd1); /* 56 */
280  II (a, b, c, d, x[ 8], S41, 0x6fa87e4f); /* 57 */
281  II (d, a, b, c, x[15], S42, 0xfe2ce6e0); /* 58 */
282  II (c, d, a, b, x[ 6], S43, 0xa3014314); /* 59 */
283  II (b, c, d, a, x[13], S44, 0x4e0811a1); /* 60 */
284  II (a, b, c, d, x[ 4], S41, 0xf7537e82); /* 61 */
285  II (d, a, b, c, x[11], S42, 0xbd3af235); /* 62 */
286  II (c, d, a, b, x[ 2], S43, 0x2ad7d2bb); /* 63 */
287  II (b, c, d, a, x[ 9], S44, 0xeb86d391); /* 64 */
288 
289  state[0] += a;
290  state[1] += b;
291  state[2] += c;
292  state[3] += d;
293 
294  /* Zeroize sensitive information.
295 */
296  memset ((POINTER)x, 0, sizeof (x));
297 }
298 
299 /* Encodes input (UINT4) into output (unsigned char). Assumes len is
300  a multiple of 4.
301  */
302 static void Encode (output, input, len)
303 unsigned char *output;
304 UINT4 *input;
305 unsigned int len;
306 {
307  unsigned int i, j;
308 
309  for (i = 0, j = 0; j < len; i++, j += 4) {
310  output[j] = (unsigned char)(input[i] & 0xff);
311  output[j+1] = (unsigned char)((input[i] >> 8) & 0xff);
312  output[j+2] = (unsigned char)((input[i] >> 16) & 0xff);
313  output[j+3] = (unsigned char)((input[i] >> 24) & 0xff);
314  }
315 }
316 
317 /* Decodes input (unsigned char) into output (UINT4). Assumes len is
318  a multiple of 4.
319  */
320 static void Decode (output, input, len)
321 UINT4 *output;
322 unsigned char *input;
323 unsigned int len;
324 {
325  unsigned int i, j;
326 
327  for (i = 0, j = 0; j < len; i++, j += 4)
328  output[i] = ((UINT4)input[j]) | (((UINT4)input[j+1]) << 8) |
329  (((UINT4)input[j+2]) << 16) | (((UINT4)input[j+3]) << 24);
330 }
331 
332 /*************************************************************************/
333 
334 /* Our own high-level routines. See encrypt.h for documentation. */
335 
336 #define XTOI(c) ((c)>9 ? (c)-'A'+10 : (c)-'0')
337 
338 static int md5_encrypt(const char *src, int len, char *dest, int size)
339 {
340  MD5_CTX context;
341  char tmp[33];
342 
343  if (size < 16)
344  return -1;
345 
346  Ano_MD5Init(&context);
347  Ano_MD5Update(&context, src, len);
348  Ano_MD5Final(dest, &context);
349 
350  if(debug) {
351  memset(tmp,0,33);
352  binary_to_hex((unsigned char *) dest,tmp,16);
353  /* Dont log source if we were encrypting in place :) */
354  if (memcmp(src, dest, 16) != 0) {
355  alog("enc_md5: hashed from [%s] to [%s]",src,tmp);
356  } else {
357  alog("enc_md5: hashed password to [%s]",tmp);
358  }
359  }
360 
361  return 0;
362 }
363 
364 
365 static int md5_encrypt_check_len(int passlen, int bufsize)
366 {
367  if (bufsize < 16)
368  fatal("enc_md5: md5_check_len(): buffer too small (%d)", bufsize);
369  return 0;
370 }
371 
372 
373 static int md5_decrypt(const char *src, char *dest, int size)
374 {
375  return 0;
376 }
377 
378 
379 static int md5_check_password(const char *plaintext, const char *password)
380 {
381  char buf[BUFSIZE];
382 
383  if (md5_encrypt(plaintext, strlen(plaintext), buf, sizeof(buf)) < 0)
384  return -1;
385  if (memcmp(buf, password, 16) == 0)
386  return 1;
387  return 0;
388 }
389 
390 /*************************************************************************/
391 
392 /* Module stuff. */
393 
394 int AnopeInit(int argc, char **argv) {
395 
396  moduleAddAuthor("Anope");
397  moduleAddVersion(VERSION_STRING);
399 
404 
405  return MOD_CONT;
406 }
407 
408 void AnopeFini(void) {
409  encmodule_encrypt(NULL);
411  encmodule_decrypt(NULL);
413 }
414 
415 
416 
417 
418 /*************************************************************************/
419 
E void encmodule_encrypt_check_len(int(*func)(int passlen, int bufsize))
Definition: encrypt.c:26
#define S22
Definition: enc_md5.c:64
#define S24
Definition: enc_md5.c:66
int AnopeInit(int argc, char **argv)
Definition: enc_md5.c:394
E void encmodule_encrypt(int(*func)(const char *src, int len, char *dest, int size))
Definition: encrypt.c:20
#define GG(a, b, c, d, x, s, ac)
Definition: enc_md5.c:105
void Ano_MD5Init(MD5_CTX *context)
Definition: enc_md5.c:123
#define S12
Definition: enc_md5.c:60
static int md5_encrypt_check_len(int passlen, int bufsize)
Definition: enc_md5.c:365
void * POINTER
Definition: enc_md5.c:55
UINT4 state[4]
Definition: enc_md5.c:47
E void encmodule_decrypt(int(*func)(const char *src, char *dest, int size))
Definition: encrypt.c:31
void Ano_MD5Final(digest, MD5_CTX *context)
Definition: enc_md5.c:181
#define FF(a, b, c, d, x, s, ac)
Definition: enc_md5.c:100
E void encmodule_check_password(int(*func)(const char *plaintext, const char *password))
Definition: encrypt.c:36
#define S31
Definition: enc_md5.c:67
#define S34
Definition: enc_md5.c:70
MDE void moduleAddAuthor(const char *author)
Definition: modules.c:1772
#define S14
Definition: enc_md5.c:62
MDE void moduleSetType(MODType type)
Definition: modules.c:818
#define S13
Definition: enc_md5.c:61
void Ano_MD5Update(MD5_CTX *context, unsigned char *input, unsigned int inputLen)
Definition: enc_md5.c:139
unsigned int UINT4
Definition: enc_md5.c:43
#define S42
Definition: enc_md5.c:72
#define S44
Definition: enc_md5.c:74
E void E void E void fatal(const char *fmt,...) FORMAT(printf
static int md5_encrypt(const char *src, int len, char *dest, int size)
Definition: enc_md5.c:338
MDE void moduleAddVersion(const char *version)
Definition: modules.c:1760
#define S21
Definition: enc_md5.c:63
Command * c
Definition: ns_recover.c:17
static void Decode(UINT4 *, unsigned char *, unsigned int)
Definition: enc_md5.c:320
#define HH(a, b, c, d, x, s, ac)
Definition: enc_md5.c:110
#define S33
Definition: enc_md5.c:69
#define S23
Definition: enc_md5.c:65
E void alog(const char *fmt,...) FORMAT(printf
#define MOD_CONT
Definition: modules.h:54
E int debug
Definition: extern.h:775
#define II(a, b, c, d, x, s, ac)
Definition: enc_md5.c:115
static void Encode(unsigned char *, UINT4 *, unsigned int)
Definition: enc_md5.c:302
UINT4 count[2]
Definition: enc_md5.c:48
#define S32
Definition: enc_md5.c:68
void AnopeFini(void)
Definition: enc_md5.c:408
#define S43
Definition: enc_md5.c:73
static unsigned char PADDING[64]
Definition: enc_md5.c:80
E void binary_to_hex(unsigned char *bin, char *hex, int length)
Definition: misc.c:67
#define S11
Definition: enc_md5.c:59
static int md5_decrypt(const char *src, char *dest, int size)
Definition: enc_md5.c:373
static int md5_check_password(const char *plaintext, const char *password)
Definition: enc_md5.c:379
#define BUFSIZE
Definition: config.h:47
void Ano_MD5Transform(UINT4[4], unsigned char[64])
#define S41
Definition: enc_md5.c:71