rfc1319.txt 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955
  1. Network Working Group B. Kaliski
  2. Request for Comments: 1319 RSA Laboratories
  3. Updates: RFC 1115 April 1992
  4. The MD2 Message-Digest Algorithm
  5. Status of this Memo
  6. This memo provides information for the Internet community. It does
  7. not specify an Internet standard. Distribution of this memo is
  8. unlimited.
  9. Acknowlegements
  10. The description of MD2 is based on material prepared by John Linn and
  11. Ron Rivest. Their permission to incorporate that material is greatly
  12. appreciated.
  13. Table of Contents
  14. 1. Executive Summary 1
  15. 2. Terminology and Notation 2
  16. 3. MD2 Algorithm Description 2
  17. 4. Summary 4
  18. References 5
  19. APPENDIX A - Reference Implementation 5
  20. Security Considerations 17
  21. Author's Address 17
  22. 1. Executive Summary
  23. This document describes the MD2 message-digest algorithm. The
  24. algorithm takes as input a message of arbitrary length and produces
  25. as output a 128-bit "fingerprint" or "message digest" of the input.
  26. It is conjectured that it is computationally infeasible to produce
  27. two messages having the same message digest, or to produce any
  28. message having a given prespecified target message digest. The MD2
  29. algorithm is intended for digital signature applications, where a
  30. large file must be "compressed" in a secure manner before being
  31. signed with a private (secret) key under a public-key cryptosystem
  32. such as RSA.
  33. License to use MD2 is granted for non-commerical Internet Privacy-
  34. Enhanced Mail [1-3].
  35. This document is an update to the August 1989 RFC 1115 [3], which
  36. also gives a reference implementation of MD2. The main differences
  37. Kaliski [Page 1]
  38. RFC 1319 MD2 Message-Digest Algorithm April 1992
  39. are that a textual description of MD2 is included, and that the
  40. reference implementation of MD2 is more portable.
  41. For OSI-based applications, MD2's object identifier is
  42. md2 OBJECT IDENTIFIER ::=
  43. iso(1) member-body(2) US(840) rsadsi(113549) digestAlgorithm(2) 2}
  44. In the X.509 type AlgorithmIdentifier [4], the parameters for MD2
  45. should have type NULL.
  46. 2. Terminology and Notation
  47. In this document, a "byte" is an eight-bit quantity.
  48. Let x_i denote "x sub i". If the subscript is an expression, we
  49. surround it in braces, as in x_{i+1}. Similarly, we use ^ for
  50. superscripts (exponentiation), so that x^i denotes x to the i-th
  51. power.
  52. Let X xor Y denote the bit-wise XOR of X and Y.
  53. 3. MD2 Algorithm Description
  54. We begin by supposing that we have a b-byte message as input, and
  55. that we wish to find its message digest. Here b is an arbitrary
  56. nonnegative integer; b may be zero, and it may be arbitrarily large.
  57. We imagine the bytes of the message written down as follows:
  58. m_0 m_1 ... m_{b-1}
  59. The following five steps are performed to compute the message digest
  60. of the message.
  61. 3.1 Step 1. Append Padding Bytes
  62. The message is "padded" (extended) so that its length (in bytes) is
  63. congruent to 0, modulo 16. That is, the message is extended so that
  64. it is a multiple of 16 bytes long. Padding is always performed, even
  65. if the length of the message is already congruent to 0, modulo 16.
  66. Padding is performed as follows: "i" bytes of value "i" are appended
  67. to the message so that the length in bytes of the padded message
  68. becomes congruent to 0, modulo 16. At least one byte and at most 16
  69. 16 bytes are appended.
  70. At this point the resulting message (after padding with bytes) has a
  71. length that is an exact multiple of 16 bytes. Let M[0 ... N-1] denote
  72. Kaliski [Page 2]
  73. RFC 1319 MD2 Message-Digest Algorithm April 1992
  74. the bytes of the resulting message, where N is a multiple of 16.
  75. 3.2 Step 2. Append Checksum
  76. A 16-byte checksum of the message is appended to the result of the
  77. previous step.
  78. This step uses a 256-byte "random" permutation constructed from the
  79. digits of pi. Let S[i] denote the i-th element of this table. The
  80. table is given in the appendix.
  81. Do the following:
  82. /* Clear checksum. */
  83. For i = 0 to 15 do:
  84. Set C[i] to 0.
  85. end /* of loop on i */
  86. Set L to 0.
  87. /* Process each 16-word block. */
  88. For i = 0 to N/16-1 do
  89. /* Checksum block i. */
  90. For j = 0 to 15 do
  91. Set c to M[i*16+j].
  92. Set C[j] to S[c xor L].
  93. Set L to C[j].
  94. end /* of loop on j */
  95. end /* of loop on i */
  96. The 16-byte checksum C[0 ... 15] is appended to the message. Let M[0
  97. with checksum), where N' = N + 16.
  98. 3.3 Step 3. Initialize MD Buffer
  99. A 48-byte buffer X is used to compute the message digest. The buffer
  100. is initialized to zero.
  101. Kaliski [Page 3]
  102. RFC 1319 MD2 Message-Digest Algorithm April 1992
  103. 3.4 Step 4. Process Message in 16-Byte Blocks
  104. This step uses the same 256-byte permutation S as step 2 does.
  105. Do the following:
  106. /* Process each 16-word block. */
  107. For i = 0 to N'/16-1 do
  108. /* Copy block i into X. */
  109. For j = 0 to 15 do
  110. Set X[16+j] to M[i*16+j].
  111. Set X[32+j] to (X[16+j] xor X[j]).
  112. end /* of loop on j */
  113. Set t to 0.
  114. /* Do 18 rounds. */
  115. For j = 0 to 17 do
  116. /* Round j. */
  117. For k = 0 to 47 do
  118. Set t and X[k] to (X[k] xor S[t]).
  119. end /* of loop on k */
  120. Set t to (t+j) modulo 256.
  121. end /* of loop on j */
  122. end /* of loop on i */
  123. 3.5 Step 5. Output
  124. The message digest produced as output is X[0 ... 15]. That is, we
  125. begin with X[0], and end with X[15].
  126. This completes the description of MD2. A reference implementation in
  127. C is given in the appendix.
  128. 4. Summary
  129. The MD2 message-digest algorithm is simple to implement, and provides
  130. a "fingerprint" or message digest of a message of arbitrary length.
  131. It is conjectured that the difficulty of coming up with two messages
  132. having the same message digest is on the order of 2^64 operations,
  133. and that the difficulty of coming up with any message having a given
  134. message digest is on the order of 2^128 operations. The MD2 algorithm
  135. has been carefully scrutinized for weaknesses. It is, however, a
  136. relatively new algorithm and further security analysis is of course
  137. Kaliski [Page 4]
  138. RFC 1319 MD2 Message-Digest Algorithm April 1992
  139. justified, as is the case with any new proposal of this sort.
  140. References
  141. [1] Linn, J., "Privacy Enhancement for Internet Electronic Mail: Part
  142. I -- Message Encipherment and Authentication Procedures", RFC
  143. 1113, DEC, IAB Privacy Task Force, August 1989.
  144. [2] Kent, S., and J. Linn, "Privacy Enhancement for Internet
  145. Electronic Mail: Part II -- Certificate-Based Key Management",
  146. RFC 1114, BBNCC, DEC, IAB Privacy Task Force, August 1989.
  147. [3] Linn, J., "Privacy Enhancement for Internet Electronic Mail: Part
  148. III -- Algorithms, Modes, and Identifiers", RFC 1115 DEC, IAB
  149. Privacy Task Force, August 1989.
  150. [4] CCITT Recommendation X.509 (1988), "The Directory -
  151. Authentication Framework".
  152. APPENDIX A - Reference Implementation
  153. This appendix contains the following files taken from RSAREF: A
  154. Cryptographic Toolkit for Privacy-Enhanced Mail:
  155. global.h -- global header file
  156. md2.h -- header file for MD2
  157. md2c.c -- source code for MD2
  158. For more information on RSAREF, send email to <rsaref@rsa.com>.
  159. The appendix also includes the following file:
  160. mddriver.c -- test driver for MD2, MD4 and MD5
  161. The driver compiles for MD5 by default but can compile for MD2 or MD4 if
  162. the symbol MD is defined on the C compiler command line as 2 or 4.
  163. A.1 global.h
  164. /* GLOBAL.H - RSAREF types and constants
  165. */
  166. /* PROTOTYPES should be set to one if and only if the compiler supports
  167. function argument prototyping.
  168. The following makes PROTOTYPES default to 0 if it has not already
  169. been defined with C compiler flags.
  170. Kaliski [Page 5]
  171. RFC 1319 MD2 Message-Digest Algorithm April 1992
  172. */
  173. #ifndef PROTOTYPES
  174. #define PROTOTYPES 0
  175. #endif
  176. /* POINTER defines a generic pointer type */
  177. typedef unsigned char *POINTER;
  178. /* UINT2 defines a two byte word */
  179. typedef unsigned short int UINT2;
  180. /* UINT4 defines a four byte word */
  181. typedef unsigned long int UINT4;
  182. /* PROTO_LIST is defined depending on how PROTOTYPES is defined above.
  183. If using PROTOTYPES, then PROTO_LIST returns the list, otherwise it
  184. returns an empty list.
  185. */
  186. #if PROTOTYPES
  187. #define PROTO_LIST(list) list
  188. #else
  189. #define PROTO_LIST(list) ()
  190. #endif
  191. A.2 md2.h
  192. /* MD2.H - header file for MD2C.C
  193. */
  194. /* Copyright (C) 1990-2, RSA Data Security, Inc. Created 1990. All
  195. rights reserved.
  196. License to copy and use this software is granted for
  197. non-commercial Internet Privacy-Enhanced Mail provided that it is
  198. identified as the "RSA Data Security, Inc. MD2 Message Digest
  199. Algorithm" in all material mentioning or referencing this software
  200. or this function.
  201. RSA Data Security, Inc. makes no representations concerning either
  202. the merchantability of this software or the suitability of this
  203. software for any particular purpose. It is provided "as is"
  204. without express or implied warranty of any kind.
  205. These notices must be retained in any copies of any part of this
  206. documentation and/or software.
  207. */
  208. Kaliski [Page 6]
  209. RFC 1319 MD2 Message-Digest Algorithm April 1992
  210. typedef struct {
  211. unsigned char state[16]; /* state */
  212. unsigned char checksum[16]; /* checksum */
  213. unsigned int count; /* number of bytes, modulo 16 */
  214. unsigned char buffer[16]; /* input buffer */
  215. } MD2_CTX;
  216. void MD2Init PROTO_LIST ((MD2_CTX *));
  217. void MD2Update PROTO_LIST
  218. ((MD2_CTX *, unsigned char *, unsigned int));
  219. void MD2Final PROTO_LIST ((unsigned char [16], MD2_CTX *));
  220. A.3 md2c.c
  221. /* MD2C.C - RSA Data Security, Inc., MD2 message-digest algorithm
  222. */
  223. /* Copyright (C) 1990-2, RSA Data Security, Inc. Created 1990. All
  224. rights reserved.
  225. License to copy and use this software is granted for
  226. non-commercial Internet Privacy-Enhanced Mail provided that it is
  227. identified as the "RSA Data Security, Inc. MD2 Message Digest
  228. Algorithm" in all material mentioning or referencing this software
  229. or this function.
  230. RSA Data Security, Inc. makes no representations concerning either
  231. the merchantability of this software or the suitability of this
  232. software for any particular purpose. It is provided "as is"
  233. without express or implied warranty of any kind.
  234. These notices must be retained in any copies of any part of this
  235. documentation and/or software.
  236. */
  237. #include "global.h"
  238. #include "md2.h"
  239. static void MD2Transform PROTO_LIST
  240. ((unsigned char [16], unsigned char [16], unsigned char [16]));
  241. static void MD2_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
  242. static void MD2_memset PROTO_LIST ((POINTER, int, unsigned int));
  243. /* Permutation of 0..255 constructed from the digits of pi. It gives a
  244. "random" nonlinear byte substitution operation.
  245. */
  246. static unsigned char PI_SUBST[256] = {
  247. 41, 46, 67, 201, 162, 216, 124, 1, 61, 54, 84, 161, 236, 240, 6,
  248. Kaliski [Page 7]
  249. RFC 1319 MD2 Message-Digest Algorithm April 1992
  250. 19, 98, 167, 5, 243, 192, 199, 115, 140, 152, 147, 43, 217, 188,
  251. 76, 130, 202, 30, 155, 87, 60, 253, 212, 224, 22, 103, 66, 111, 24,
  252. 138, 23, 229, 18, 190, 78, 196, 214, 218, 158, 222, 73, 160, 251,
  253. 245, 142, 187, 47, 238, 122, 169, 104, 121, 145, 21, 178, 7, 63,
  254. 148, 194, 16, 137, 11, 34, 95, 33, 128, 127, 93, 154, 90, 144, 50,
  255. 39, 53, 62, 204, 231, 191, 247, 151, 3, 255, 25, 48, 179, 72, 165,
  256. 181, 209, 215, 94, 146, 42, 172, 86, 170, 198, 79, 184, 56, 210,
  257. 150, 164, 125, 182, 118, 252, 107, 226, 156, 116, 4, 241, 69, 157,
  258. 112, 89, 100, 113, 135, 32, 134, 91, 207, 101, 230, 45, 168, 2, 27,
  259. 96, 37, 173, 174, 176, 185, 246, 28, 70, 97, 105, 52, 64, 126, 15,
  260. 85, 71, 163, 35, 221, 81, 175, 58, 195, 92, 249, 206, 186, 197,
  261. 234, 38, 44, 83, 13, 110, 133, 40, 132, 9, 211, 223, 205, 244, 65,
  262. 129, 77, 82, 106, 220, 55, 200, 108, 193, 171, 250, 36, 225, 123,
  263. 8, 12, 189, 177, 74, 120, 136, 149, 139, 227, 99, 232, 109, 233,
  264. 203, 213, 254, 59, 0, 29, 57, 242, 239, 183, 14, 102, 88, 208, 228,
  265. 166, 119, 114, 248, 235, 117, 75, 10, 49, 68, 80, 180, 143, 237,
  266. 31, 26, 219, 153, 141, 51, 159, 17, 131, 20
  267. };
  268. static unsigned char *PADDING[] = {
  269. (unsigned char *)"",
  270. (unsigned char *)"\001",
  271. (unsigned char *)"\002\002",
  272. (unsigned char *)"\003\003\003",
  273. (unsigned char *)"\004\004\004\004",
  274. (unsigned char *)"\005\005\005\005\005",
  275. (unsigned char *)"\006\006\006\006\006\006",
  276. (unsigned char *)"\007\007\007\007\007\007\007",
  277. (unsigned char *)"\010\010\010\010\010\010\010\010",
  278. (unsigned char *)"\011\011\011\011\011\011\011\011\011",
  279. (unsigned char *)"\012\012\012\012\012\012\012\012\012\012",
  280. (unsigned char *)"\013\013\013\013\013\013\013\013\013\013\013",
  281. (unsigned char *)"\014\014\014\014\014\014\014\014\014\014\014\014",
  282. (unsigned char *)
  283. "\015\015\015\015\015\015\015\015\015\015\015\015\015",
  284. (unsigned char *)
  285. "\016\016\016\016\016\016\016\016\016\016\016\016\016\016",
  286. (unsigned char *)
  287. "\017\017\017\017\017\017\017\017\017\017\017\017\017\017\017",
  288. (unsigned char *)
  289. "\020\020\020\020\020\020\020\020\020\020\020\020\020\020\020\020"
  290. };
  291. /* MD2 initialization. Begins an MD2 operation, writing a new context.
  292. */
  293. void MD2Init (context)
  294. MD2_CTX *context; /* context */
  295. {
  296. Kaliski [Page 8]
  297. RFC 1319 MD2 Message-Digest Algorithm April 1992
  298. context->count = 0;
  299. MD2_memset ((POINTER)context->state, 0, sizeof (context->state));
  300. MD2_memset
  301. ((POINTER)context->checksum, 0, sizeof (context->checksum));
  302. }
  303. /* MD2 block update operation. Continues an MD2 message-digest
  304. operation, processing another message block, and updating the
  305. context.
  306. */
  307. void MD2Update (context, input, inputLen)
  308. MD2_CTX *context; /* context */
  309. unsigned char *input; /* input block */
  310. unsigned int inputLen; /* length of input block */
  311. {
  312. unsigned int i, index, partLen;
  313. /* Update number of bytes mod 16 */
  314. index = context->count;
  315. context->count = (index + inputLen) & 0xf;
  316. partLen = 16 - index;
  317. /* Transform as many times as possible.
  318. */
  319. if (inputLen >= partLen) {
  320. MD2_memcpy
  321. ((POINTER)&context->buffer[index], (POINTER)input, partLen);
  322. MD2Transform (context->state, context->checksum, context->buffer);
  323. for (i = partLen; i + 15 < inputLen; i += 16)
  324. MD2Transform (context->state, context->checksum, &input[i]);
  325. index = 0;
  326. }
  327. else
  328. i = 0;
  329. /* Buffer remaining input */
  330. MD2_memcpy
  331. ((POINTER)&context->buffer[index], (POINTER)&input[i],
  332. inputLen-i);
  333. }
  334. /* MD2 finalization. Ends an MD2 message-digest operation, writing the
  335. message digest and zeroizing the context.
  336. */
  337. void MD2Final (digest, context)
  338. Kaliski [Page 9]
  339. RFC 1319 MD2 Message-Digest Algorithm April 1992
  340. unsigned char digest[16]; /* message digest */
  341. MD2_CTX *context; /* context */
  342. {
  343. unsigned int index, padLen;
  344. /* Pad out to multiple of 16.
  345. */
  346. index = context->count;
  347. padLen = 16 - index;
  348. MD2Update (context, PADDING[padLen], padLen);
  349. /* Extend with checksum */
  350. MD2Update (context, context->checksum, 16);
  351. /* Store state in digest */
  352. MD2_memcpy ((POINTER)digest, (POINTER)context->state, 16);
  353. /* Zeroize sensitive information.
  354. */
  355. MD2_memset ((POINTER)context, 0, sizeof (*context));
  356. }
  357. /* MD2 basic transformation. Transforms state and updates checksum
  358. based on block.
  359. */
  360. static void MD2Transform (state, checksum, block)
  361. unsigned char state[16];
  362. unsigned char checksum[16];
  363. unsigned char block[16];
  364. {
  365. unsigned int i, j, t;
  366. unsigned char x[48];
  367. /* Form encryption block from state, block, state ^ block.
  368. */
  369. MD2_memcpy ((POINTER)x, (POINTER)state, 16);
  370. MD2_memcpy ((POINTER)x+16, (POINTER)block, 16);
  371. for (i = 0; i < 16; i++)
  372. x[i+32] = state[i] ^ block[i];
  373. /* Encrypt block (18 rounds).
  374. */
  375. t = 0;
  376. for (i = 0; i < 18; i++) {
  377. for (j = 0; j < 48; j++)
  378. t = x[j] ^= PI_SUBST[t];
  379. t = (t + i) & 0xff;
  380. }
  381. Kaliski [Page 10]
  382. RFC 1319 MD2 Message-Digest Algorithm April 1992
  383. /* Save new state */
  384. MD2_memcpy ((POINTER)state, (POINTER)x, 16);
  385. /* Update checksum.
  386. */
  387. t = checksum[15];
  388. for (i = 0; i < 16; i++)
  389. t = checksum[i] ^= PI_SUBST[block[i] ^ t];
  390. /* Zeroize sensitive information.
  391. */
  392. MD2_memset ((POINTER)x, 0, sizeof (x));
  393. }
  394. /* Note: Replace "for loop" with standard memcpy if possible.
  395. */
  396. static void MD2_memcpy (output, input, len)
  397. POINTER output;
  398. POINTER input;
  399. unsigned int len;
  400. {
  401. unsigned int i;
  402. for (i = 0; i < len; i++)
  403. output[i] = input[i];
  404. }
  405. /* Note: Replace "for loop" with standard memset if possible.
  406. */
  407. static void MD2_memset (output, value, len)
  408. POINTER output;
  409. int value;
  410. unsigned int len;
  411. {
  412. unsigned int i;
  413. for (i = 0; i < len; i++)
  414. ((char *)output)[i] = (char)value;
  415. }
  416. A.4 mddriver.c
  417. /* MDDRIVER.C - test driver for MD2, MD4 and MD5
  418. */
  419. /* Copyright (C) 1990-2, RSA Data Security, Inc. Created 1990. All
  420. rights reserved.
  421. Kaliski [Page 11]
  422. RFC 1319 MD2 Message-Digest Algorithm April 1992
  423. RSA Data Security, Inc. makes no representations concerning either
  424. the merchantability of this software or the suitability of this
  425. software for any particular purpose. It is provided "as is"
  426. without express or implied warranty of any kind.
  427. These notices must be retained in any copies of any part of this
  428. documentation and/or software.
  429. */
  430. /* The following makes MD default to MD5 if it has not already been
  431. defined with C compiler flags.
  432. */
  433. #ifndef MD
  434. #define MD MD5
  435. #endif
  436. #include <stdio.h>
  437. #include <time.h>
  438. #include <string.h>
  439. #include "global.h"
  440. #if MD == 2
  441. #include "md2.h"
  442. #endif
  443. #if MD == 4
  444. #include "md4.h"
  445. #endif
  446. #if MD == 5
  447. #include "md5.h"
  448. #endif
  449. /* Length of test block, number of test blocks.
  450. */
  451. #define TEST_BLOCK_LEN 1000
  452. #define TEST_BLOCK_COUNT 1000
  453. static void MDString PROTO_LIST ((char *));
  454. static void MDTimeTrial PROTO_LIST ((void));
  455. static void MDTestSuite PROTO_LIST ((void));
  456. static void MDFile PROTO_LIST ((char *));
  457. static void MDFilter PROTO_LIST ((void));
  458. static void MDPrint PROTO_LIST ((unsigned char [16]));
  459. #if MD == 2
  460. #define MD_CTX MD2_CTX
  461. #define MDInit MD2Init
  462. #define MDUpdate MD2Update
  463. #define MDFinal MD2Final
  464. #endif
  465. Kaliski [Page 12]
  466. RFC 1319 MD2 Message-Digest Algorithm April 1992
  467. #if MD == 4
  468. #define MD_CTX MD4_CTX
  469. #define MDInit MD4Init
  470. #define MDUpdate MD4Update
  471. #define MDFinal MD4Final
  472. #endif
  473. #if MD == 5
  474. #define MD_CTX MD5_CTX
  475. #define MDInit MD5Init
  476. #define MDUpdate MD5Update
  477. #define MDFinal MD5Final
  478. #endif
  479. /* Main driver.
  480. Arguments (may be any combination):
  481. -sstring - digests string
  482. -t - runs time trial
  483. -x - runs test script
  484. filename - digests file
  485. (none) - digests standard input
  486. */
  487. int main (argc, argv)
  488. int argc;
  489. char *argv[];
  490. {
  491. int i;
  492. if (argc > 1)
  493. for (i = 1; i < argc; i++)
  494. if (argv[i][0] == '-' && argv[i][1] == 's')
  495. MDString (argv[i] + 2);
  496. else if (strcmp (argv[i], "-t") == 0)
  497. MDTimeTrial ();
  498. else if (strcmp (argv[i], "-x") == 0)
  499. MDTestSuite ();
  500. else
  501. MDFile (argv[i]);
  502. else
  503. MDFilter ();
  504. return (0);
  505. }
  506. /* Digests a string and prints the result.
  507. */
  508. static void MDString (string)
  509. char *string;
  510. Kaliski [Page 13]
  511. RFC 1319 MD2 Message-Digest Algorithm April 1992
  512. {
  513. MD_CTX context;
  514. unsigned char digest[16];
  515. unsigned int len = strlen (string);
  516. MDInit (&context);
  517. MDUpdate (&context, string, len);
  518. MDFinal (digest, &context);
  519. printf ("MD%d (\"%s\") = ", MD, string);
  520. MDPrint (digest);
  521. printf ("\n");
  522. }
  523. /* Measures the time to digest TEST_BLOCK_COUNT TEST_BLOCK_LEN-byte
  524. blocks.
  525. */
  526. static void MDTimeTrial ()
  527. {
  528. MD_CTX context;
  529. time_t endTime, startTime;
  530. unsigned char block[TEST_BLOCK_LEN], digest[16];
  531. unsigned int i;
  532. printf
  533. ("MD%d time trial. Digesting %d %d-byte blocks ...", MD,
  534. TEST_BLOCK_LEN, TEST_BLOCK_COUNT);
  535. /* Initialize block */
  536. for (i = 0; i < TEST_BLOCK_LEN; i++)
  537. block[i] = (unsigned char)(i & 0xff);
  538. /* Start timer */
  539. time (&startTime);
  540. /* Digest blocks */
  541. MDInit (&context);
  542. for (i = 0; i < TEST_BLOCK_COUNT; i++)
  543. MDUpdate (&context, block, TEST_BLOCK_LEN);
  544. MDFinal (digest, &context);
  545. /* Stop timer */
  546. time (&endTime);
  547. printf (" done\n");
  548. printf ("Digest = ");
  549. MDPrint (digest);
  550. printf ("\nTime = %ld seconds\n", (long)(endTime-startTime));
  551. Kaliski [Page 14]
  552. RFC 1319 MD2 Message-Digest Algorithm April 1992
  553. printf
  554. ("Speed = %ld bytes/second\n",
  555. (long)TEST_BLOCK_LEN * (long)TEST_BLOCK_COUNT/(endTime-startTime));
  556. }
  557. /* Digests a reference suite of strings and prints the results.
  558. */
  559. static void MDTestSuite ()
  560. {
  561. printf ("MD%d test suite:\n", MD);
  562. MDString ("");
  563. MDString ("a");
  564. MDString ("abc");
  565. MDString ("message digest");
  566. MDString ("abcdefghijklmnopqrstuvwxyz");
  567. MDString
  568. ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789");
  569. MDString
  570. ("1234567890123456789012345678901234567890\
  571. 1234567890123456789012345678901234567890");
  572. }
  573. /* Digests a file and prints the result.
  574. */
  575. static void MDFile (filename)
  576. char *filename;
  577. {
  578. FILE *file;
  579. MD_CTX context;
  580. int len;
  581. unsigned char buffer[1024], digest[16];
  582. if ((file = fopen (filename, "rb")) == NULL)
  583. printf ("%s can't be opened\n", filename);
  584. else {
  585. MDInit (&context);
  586. while (len = fread (buffer, 1, 1024, file))
  587. MDUpdate (&context, buffer, len);
  588. MDFinal (digest, &context);
  589. fclose (file);
  590. printf ("MD%d (%s) = ", MD, filename);
  591. MDPrint (digest);
  592. printf ("\n");
  593. }
  594. }
  595. Kaliski [Page 15]
  596. RFC 1319 MD2 Message-Digest Algorithm April 1992
  597. /* Digests the standard input and prints the result.
  598. */
  599. static void MDFilter ()
  600. {
  601. MD_CTX context;
  602. int len;
  603. unsigned char buffer[16], digest[16];
  604. MDInit (&context);
  605. while (len = fread (buffer, 1, 16, stdin))
  606. MDUpdate (&context, buffer, len);
  607. MDFinal (digest, &context);
  608. MDPrint (digest);
  609. printf ("\n");
  610. }
  611. /* Prints a message digest in hexadecimal.
  612. */
  613. static void MDPrint (digest)
  614. unsigned char digest[16];
  615. {
  616. unsigned int i;
  617. for (i = 0; i < 16; i++)
  618. printf ("%02x", digest[i]);
  619. }
  620. A.5 Test suite
  621. The MD2 test suite (driver option "-x") should print the following
  622. results:
  623. MD2 test suite:
  624. MD2 ("") = 8350e5a3e24c153df2275c9f80692773
  625. MD2 ("a") = 32ec01ec4a6dac72c0ab96fb34c0b5d1
  626. MD2 ("abc") = da853b0d3f88d99b30283a69e6ded6bb
  627. MD2 ("message digest") = ab4f496bfb2a530b219ff33031fe06b0
  628. MD2 ("abcdefghijklmnopqrstuvwxyz") = 4e8ddff3650292ab5a4108c3aa47940b
  629. MD2 ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789") =
  630. da33def2a42df13975352846c30338cd
  631. MD2 ("123456789012345678901234567890123456789012345678901234567890123456
  632. 78901234567890") = d5976f79d83d3a0dc9806c3c66f3efd8
  633. Kaliski [Page 16]
  634. RFC 1319 MD2 Message-Digest Algorithm April 1992
  635. Security Considerations
  636. The level of security discussed in this memo is considered to be
  637. sufficient for implementing very high security hybrid digital
  638. signature schemes based on MD2 and a public-key cryptosystem.
  639. Author's Address
  640. Burton S. Kaliski Jr.
  641. RSA Laboratories (a division of RSA Data Security, Inc.)
  642. 10 Twin Dolphin Drive
  643. Redwood City, CA 94065
  644. Phone: (415) 595-8782
  645. FAX: (415) 595-4126
  646. EMail: burt@rsa.com
  647. Kaliski [Page 17]