md5.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. /* md5.c - MD5 Message-Digest Algorithm
  2. * Copyright (C) 1995,1996,1998,1999,2001,2002,
  3. * 2003 Free Software Foundation, Inc.
  4. *
  5. * This file is part of Libgcrypt.
  6. *
  7. * Libgcrypt is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser General Public License as
  9. * published by the Free Software Foundation; either version 2.1 of
  10. * the License, or (at your option) any later version.
  11. *
  12. * Libgcrypt 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 Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License 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. * According to the definition of MD5 in RFC 1321 from April 1992.
  22. * NOTE: This is *not* the same file as the one from glibc.
  23. * Written by Ulrich Drepper <drepper@gnu.ai.mit.edu>, 1995.
  24. * heavily modified for GnuPG by Werner Koch <wk@gnupg.org>
  25. */
  26. /* Test values:
  27. * "" D4 1D 8C D9 8F 00 B2 04 E9 80 09 98 EC F8 42 7E
  28. * "a" 0C C1 75 B9 C0 F1 B6 A8 31 C3 99 E2 69 77 26 61
  29. * "abc 90 01 50 98 3C D2 4F B0 D6 96 3F 7D 28 E1 7F 72
  30. * "message digest" F9 6B 69 7D 7C B7 93 8D 52 5A 2F 31 AA F1 61 D0
  31. */
  32. #include <config.h>
  33. #include <stdio.h>
  34. #include <stdlib.h>
  35. #include <string.h>
  36. #include "g10lib.h"
  37. #include "cipher.h"
  38. #include "bithelp.h"
  39. typedef struct {
  40. u32 A,B,C,D; /* chaining variables */
  41. u32 nblocks;
  42. byte buf[64];
  43. int count;
  44. } MD5_CONTEXT;
  45. static void
  46. md5_init( void *context )
  47. {
  48. MD5_CONTEXT *ctx = context;
  49. ctx->A = 0x67452301;
  50. ctx->B = 0xefcdab89;
  51. ctx->C = 0x98badcfe;
  52. ctx->D = 0x10325476;
  53. ctx->nblocks = 0;
  54. ctx->count = 0;
  55. }
  56. /* These are the four functions used in the four steps of the MD5 algorithm
  57. and defined in the RFC 1321. The first function is a little bit optimized
  58. (as found in Colin Plumbs public domain implementation). */
  59. /* #define FF(b, c, d) ((b & c) | (~b & d)) */
  60. #define FF(b, c, d) (d ^ (b & (c ^ d)))
  61. #define FG(b, c, d) FF (d, b, c)
  62. #define FH(b, c, d) (b ^ c ^ d)
  63. #define FI(b, c, d) (c ^ (b | ~d))
  64. /****************
  65. * transform n*64 bytes
  66. */
  67. static void
  68. transform ( MD5_CONTEXT *ctx, const unsigned char *data )
  69. {
  70. u32 correct_words[16];
  71. register u32 A = ctx->A;
  72. register u32 B = ctx->B;
  73. register u32 C = ctx->C;
  74. register u32 D = ctx->D;
  75. u32 *cwp = correct_words;
  76. #ifdef WORDS_BIGENDIAN
  77. {
  78. int i;
  79. byte *p2;
  80. const byte *p1;
  81. for(i=0, p1=data, p2=(byte*)correct_words; i < 16; i++, p2 += 4 )
  82. {
  83. p2[3] = *p1++;
  84. p2[2] = *p1++;
  85. p2[1] = *p1++;
  86. p2[0] = *p1++;
  87. }
  88. }
  89. #else
  90. memcpy( correct_words, data, 64 );
  91. #endif
  92. #define OP(a, b, c, d, s, T) \
  93. do \
  94. { \
  95. a += FF (b, c, d) + (*cwp++) + T; \
  96. a = rol(a, s); \
  97. a += b; \
  98. } \
  99. while (0)
  100. /* Before we start, one word about the strange constants.
  101. They are defined in RFC 1321 as
  102. T[i] = (int) (4294967296.0 * fabs (sin (i))), i=1..64
  103. */
  104. /* Round 1. */
  105. OP (A, B, C, D, 7, 0xd76aa478);
  106. OP (D, A, B, C, 12, 0xe8c7b756);
  107. OP (C, D, A, B, 17, 0x242070db);
  108. OP (B, C, D, A, 22, 0xc1bdceee);
  109. OP (A, B, C, D, 7, 0xf57c0faf);
  110. OP (D, A, B, C, 12, 0x4787c62a);
  111. OP (C, D, A, B, 17, 0xa8304613);
  112. OP (B, C, D, A, 22, 0xfd469501);
  113. OP (A, B, C, D, 7, 0x698098d8);
  114. OP (D, A, B, C, 12, 0x8b44f7af);
  115. OP (C, D, A, B, 17, 0xffff5bb1);
  116. OP (B, C, D, A, 22, 0x895cd7be);
  117. OP (A, B, C, D, 7, 0x6b901122);
  118. OP (D, A, B, C, 12, 0xfd987193);
  119. OP (C, D, A, B, 17, 0xa679438e);
  120. OP (B, C, D, A, 22, 0x49b40821);
  121. #undef OP
  122. #define OP(f, a, b, c, d, k, s, T) \
  123. do \
  124. { \
  125. a += f (b, c, d) + correct_words[k] + T; \
  126. a = rol(a, s); \
  127. a += b; \
  128. } \
  129. while (0)
  130. /* Round 2. */
  131. OP (FG, A, B, C, D, 1, 5, 0xf61e2562);
  132. OP (FG, D, A, B, C, 6, 9, 0xc040b340);
  133. OP (FG, C, D, A, B, 11, 14, 0x265e5a51);
  134. OP (FG, B, C, D, A, 0, 20, 0xe9b6c7aa);
  135. OP (FG, A, B, C, D, 5, 5, 0xd62f105d);
  136. OP (FG, D, A, B, C, 10, 9, 0x02441453);
  137. OP (FG, C, D, A, B, 15, 14, 0xd8a1e681);
  138. OP (FG, B, C, D, A, 4, 20, 0xe7d3fbc8);
  139. OP (FG, A, B, C, D, 9, 5, 0x21e1cde6);
  140. OP (FG, D, A, B, C, 14, 9, 0xc33707d6);
  141. OP (FG, C, D, A, B, 3, 14, 0xf4d50d87);
  142. OP (FG, B, C, D, A, 8, 20, 0x455a14ed);
  143. OP (FG, A, B, C, D, 13, 5, 0xa9e3e905);
  144. OP (FG, D, A, B, C, 2, 9, 0xfcefa3f8);
  145. OP (FG, C, D, A, B, 7, 14, 0x676f02d9);
  146. OP (FG, B, C, D, A, 12, 20, 0x8d2a4c8a);
  147. /* Round 3. */
  148. OP (FH, A, B, C, D, 5, 4, 0xfffa3942);
  149. OP (FH, D, A, B, C, 8, 11, 0x8771f681);
  150. OP (FH, C, D, A, B, 11, 16, 0x6d9d6122);
  151. OP (FH, B, C, D, A, 14, 23, 0xfde5380c);
  152. OP (FH, A, B, C, D, 1, 4, 0xa4beea44);
  153. OP (FH, D, A, B, C, 4, 11, 0x4bdecfa9);
  154. OP (FH, C, D, A, B, 7, 16, 0xf6bb4b60);
  155. OP (FH, B, C, D, A, 10, 23, 0xbebfbc70);
  156. OP (FH, A, B, C, D, 13, 4, 0x289b7ec6);
  157. OP (FH, D, A, B, C, 0, 11, 0xeaa127fa);
  158. OP (FH, C, D, A, B, 3, 16, 0xd4ef3085);
  159. OP (FH, B, C, D, A, 6, 23, 0x04881d05);
  160. OP (FH, A, B, C, D, 9, 4, 0xd9d4d039);
  161. OP (FH, D, A, B, C, 12, 11, 0xe6db99e5);
  162. OP (FH, C, D, A, B, 15, 16, 0x1fa27cf8);
  163. OP (FH, B, C, D, A, 2, 23, 0xc4ac5665);
  164. /* Round 4. */
  165. OP (FI, A, B, C, D, 0, 6, 0xf4292244);
  166. OP (FI, D, A, B, C, 7, 10, 0x432aff97);
  167. OP (FI, C, D, A, B, 14, 15, 0xab9423a7);
  168. OP (FI, B, C, D, A, 5, 21, 0xfc93a039);
  169. OP (FI, A, B, C, D, 12, 6, 0x655b59c3);
  170. OP (FI, D, A, B, C, 3, 10, 0x8f0ccc92);
  171. OP (FI, C, D, A, B, 10, 15, 0xffeff47d);
  172. OP (FI, B, C, D, A, 1, 21, 0x85845dd1);
  173. OP (FI, A, B, C, D, 8, 6, 0x6fa87e4f);
  174. OP (FI, D, A, B, C, 15, 10, 0xfe2ce6e0);
  175. OP (FI, C, D, A, B, 6, 15, 0xa3014314);
  176. OP (FI, B, C, D, A, 13, 21, 0x4e0811a1);
  177. OP (FI, A, B, C, D, 4, 6, 0xf7537e82);
  178. OP (FI, D, A, B, C, 11, 10, 0xbd3af235);
  179. OP (FI, C, D, A, B, 2, 15, 0x2ad7d2bb);
  180. OP (FI, B, C, D, A, 9, 21, 0xeb86d391);
  181. /* Put checksum in context given as argument. */
  182. ctx->A += A;
  183. ctx->B += B;
  184. ctx->C += C;
  185. ctx->D += D;
  186. }
  187. /* The routine updates the message-digest context to
  188. * account for the presence of each of the characters inBuf[0..inLen-1]
  189. * in the message whose digest is being computed.
  190. */
  191. static void
  192. md5_write( void *context, const void *inbuf_arg , size_t inlen)
  193. {
  194. const unsigned char *inbuf = inbuf_arg;
  195. MD5_CONTEXT *hd = context;
  196. if( hd->count == 64 ) /* flush the buffer */
  197. {
  198. transform( hd, hd->buf );
  199. _gcry_burn_stack (80+6*sizeof(void*));
  200. hd->count = 0;
  201. hd->nblocks++;
  202. }
  203. if( !inbuf )
  204. return;
  205. if( hd->count )
  206. {
  207. for( ; inlen && hd->count < 64; inlen-- )
  208. hd->buf[hd->count++] = *inbuf++;
  209. md5_write( hd, NULL, 0 );
  210. if( !inlen )
  211. return;
  212. }
  213. _gcry_burn_stack (80+6*sizeof(void*));
  214. while( inlen >= 64 )
  215. {
  216. transform( hd, inbuf );
  217. hd->count = 0;
  218. hd->nblocks++;
  219. inlen -= 64;
  220. inbuf += 64;
  221. }
  222. for( ; inlen && hd->count < 64; inlen-- )
  223. hd->buf[hd->count++] = *inbuf++;
  224. }
  225. /* The routine final terminates the message-digest computation and
  226. * ends with the desired message digest in mdContext->digest[0...15].
  227. * The handle is prepared for a new MD5 cycle.
  228. * Returns 16 bytes representing the digest.
  229. */
  230. static void
  231. md5_final( void *context)
  232. {
  233. MD5_CONTEXT *hd = context;
  234. u32 t, msb, lsb;
  235. byte *p;
  236. md5_write(hd, NULL, 0); /* flush */;
  237. t = hd->nblocks;
  238. /* multiply by 64 to make a byte count */
  239. lsb = t << 6;
  240. msb = t >> 26;
  241. /* add the count */
  242. t = lsb;
  243. if( (lsb += hd->count) < t )
  244. msb++;
  245. /* multiply by 8 to make a bit count */
  246. t = lsb;
  247. lsb <<= 3;
  248. msb <<= 3;
  249. msb |= t >> 29;
  250. if( hd->count < 56 ) /* enough room */
  251. {
  252. hd->buf[hd->count++] = 0x80; /* pad */
  253. while( hd->count < 56 )
  254. hd->buf[hd->count++] = 0; /* pad */
  255. }
  256. else /* need one extra block */
  257. {
  258. hd->buf[hd->count++] = 0x80; /* pad character */
  259. while( hd->count < 64 )
  260. hd->buf[hd->count++] = 0;
  261. md5_write(hd, NULL, 0); /* flush */;
  262. memset(hd->buf, 0, 56 ); /* fill next block with zeroes */
  263. }
  264. /* append the 64 bit count */
  265. hd->buf[56] = lsb ;
  266. hd->buf[57] = lsb >> 8;
  267. hd->buf[58] = lsb >> 16;
  268. hd->buf[59] = lsb >> 24;
  269. hd->buf[60] = msb ;
  270. hd->buf[61] = msb >> 8;
  271. hd->buf[62] = msb >> 16;
  272. hd->buf[63] = msb >> 24;
  273. transform( hd, hd->buf );
  274. _gcry_burn_stack (80+6*sizeof(void*));
  275. p = hd->buf;
  276. #ifdef WORDS_BIGENDIAN
  277. #define X(a) do { *p++ = hd->a ; *p++ = hd->a >> 8; \
  278. *p++ = hd->a >> 16; *p++ = hd->a >> 24; } while(0)
  279. #else /* little endian */
  280. #define X(a) do { *(u32*)p = (*hd).a ; p += 4; } while(0)
  281. #endif
  282. X(A);
  283. X(B);
  284. X(C);
  285. X(D);
  286. #undef X
  287. }
  288. static byte *
  289. md5_read( void *context )
  290. {
  291. MD5_CONTEXT *hd = (MD5_CONTEXT *) context;
  292. return hd->buf;
  293. }
  294. static byte asn[18] = /* Object ID is 1.2.840.113549.2.5 */
  295. { 0x30, 0x20, 0x30, 0x0c, 0x06, 0x08, 0x2a, 0x86,0x48,
  296. 0x86, 0xf7, 0x0d, 0x02, 0x05, 0x05, 0x00, 0x04, 0x10 };
  297. static gcry_md_oid_spec_t oid_spec_md5[] =
  298. {
  299. /* iso.member-body.us.rsadsi.pkcs.pkcs-1.4 (md5WithRSAEncryption) */
  300. { "1.2.840.113549.1.1.4" },
  301. /* RSADSI digestAlgorithm MD5 */
  302. { "1.2.840.113549.2.5" },
  303. { NULL },
  304. };
  305. gcry_md_spec_t _gcry_digest_spec_md5 =
  306. {
  307. "MD5", asn, DIM (asn), oid_spec_md5, 16,
  308. md5_init, md5_write, md5_final, md5_read,
  309. sizeof (MD5_CONTEXT)
  310. };