Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

md5.c 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /*
  2. * Cryptographic API.
  3. *
  4. * MD5 Message Digest Algorithm (RFC1321).
  5. *
  6. * Derived from cryptoapi implementation, originally based on the
  7. * public domain implementation written by Colin Plumb in 1993.
  8. *
  9. * Reduced object size by around 50% compared to the original Linux
  10. * version for use in Etherboot by Michael Brown.
  11. *
  12. * Copyright (c) Cryptoapi developers.
  13. * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
  14. * Copyright (c) 2006 Michael Brown <mbrown@fensystems.co.uk>
  15. *
  16. * This program is free software; you can redistribute it and/or modify it
  17. * under the terms of the GNU General Public License as published by the Free
  18. * Software Foundation; either version 2 of the License, or (at your option)
  19. * any later version.
  20. *
  21. */
  22. #include <stdint.h>
  23. #include <string.h>
  24. #include <byteswap.h>
  25. #include <gpxe/crypto.h>
  26. #include <gpxe/md5.h>
  27. #define MD5_DIGEST_SIZE 16
  28. #define MD5_BLOCK_WORDS 16
  29. #define MD5_HASH_WORDS 4
  30. struct md5_ctx {
  31. u32 hash[MD5_HASH_WORDS];
  32. u32 block[MD5_BLOCK_WORDS];
  33. u64 byte_count;
  34. };
  35. #define __md5step __attribute__ (( regparm ( 3 ) ))
  36. struct md5_step {
  37. u32 __md5step ( * f ) ( u32 b, u32 c, u32 d );
  38. u8 coefficient;
  39. u8 constant;
  40. };
  41. static u32 __md5step f1(u32 b, u32 c, u32 d)
  42. {
  43. return ( d ^ ( b & ( c ^ d ) ) );
  44. }
  45. static u32 __md5step f2(u32 b, u32 c, u32 d)
  46. {
  47. return ( c ^ ( d & ( b ^ c ) ) );
  48. }
  49. static u32 __md5step f3(u32 b, u32 c, u32 d)
  50. {
  51. return ( b ^ c ^ d );
  52. }
  53. static u32 __md5step f4(u32 b, u32 c, u32 d)
  54. {
  55. return ( c ^ ( b | ~d ) );
  56. }
  57. struct md5_step md5_steps[4] = {
  58. {
  59. .f = f1,
  60. .coefficient = 1,
  61. .constant = 0,
  62. },
  63. {
  64. .f = f2,
  65. .coefficient = 5,
  66. .constant = 1,
  67. },
  68. {
  69. .f = f3,
  70. .coefficient = 3,
  71. .constant = 5,
  72. },
  73. {
  74. .f = f4,
  75. .coefficient = 7,
  76. .constant = 0,
  77. }
  78. };
  79. static const u8 r[64] = {
  80. 7,12,17,22,7,12,17,22,7,12,17,22,7,12,17,22,
  81. 5,9,14,20,5,9,14,20,5,9,14,20,5,9,14,20,
  82. 4,11,16,23,4,11,16,23,4,11,16,23,4,11,16,23,
  83. 6,10,15,21,6,10,15,21,6,10,15,21,6,10,15,21
  84. };
  85. static const u32 k[64] = {
  86. 0xd76aa478UL, 0xe8c7b756UL, 0x242070dbUL, 0xc1bdceeeUL,
  87. 0xf57c0fafUL, 0x4787c62aUL, 0xa8304613UL, 0xfd469501UL,
  88. 0x698098d8UL, 0x8b44f7afUL, 0xffff5bb1UL, 0x895cd7beUL,
  89. 0x6b901122UL, 0xfd987193UL, 0xa679438eUL, 0x49b40821UL,
  90. 0xf61e2562UL, 0xc040b340UL, 0x265e5a51UL, 0xe9b6c7aaUL,
  91. 0xd62f105dUL, 0x02441453UL, 0xd8a1e681UL, 0xe7d3fbc8UL,
  92. 0x21e1cde6UL, 0xc33707d6UL, 0xf4d50d87UL, 0x455a14edUL,
  93. 0xa9e3e905UL, 0xfcefa3f8UL, 0x676f02d9UL, 0x8d2a4c8aUL,
  94. 0xfffa3942UL, 0x8771f681UL, 0x6d9d6122UL, 0xfde5380cUL,
  95. 0xa4beea44UL, 0x4bdecfa9UL, 0xf6bb4b60UL, 0xbebfbc70UL,
  96. 0x289b7ec6UL, 0xeaa127faUL, 0xd4ef3085UL, 0x04881d05UL,
  97. 0xd9d4d039UL, 0xe6db99e5UL, 0x1fa27cf8UL, 0xc4ac5665UL,
  98. 0xf4292244UL, 0x432aff97UL, 0xab9423a7UL, 0xfc93a039UL,
  99. 0x655b59c3UL, 0x8f0ccc92UL, 0xffeff47dUL, 0x85845dd1UL,
  100. 0x6fa87e4fUL, 0xfe2ce6e0UL, 0xa3014314UL, 0x4e0811a1UL,
  101. 0xf7537e82UL, 0xbd3af235UL, 0x2ad7d2bbUL, 0xeb86d391UL,
  102. };
  103. static void md5_transform(u32 *hash, const u32 *in)
  104. {
  105. u32 a, b, c, d, f, g, temp;
  106. int i;
  107. struct md5_step *step;
  108. a = hash[0];
  109. b = hash[1];
  110. c = hash[2];
  111. d = hash[3];
  112. for ( i = 0 ; i < 64 ; i++ ) {
  113. step = &md5_steps[i >> 4];
  114. f = step->f ( b, c, d );
  115. g = ( ( i * step->coefficient + step->constant ) & 0xf );
  116. temp = d;
  117. d = c;
  118. c = b;
  119. a += ( f + k[i] + in[g] );
  120. a = ( ( a << r[i] ) | ( a >> ( 32-r[i] ) ) );
  121. b += a;
  122. a = temp;
  123. }
  124. hash[0] += a;
  125. hash[1] += b;
  126. hash[2] += c;
  127. hash[3] += d;
  128. }
  129. /* XXX: this stuff can be optimized */
  130. static inline void le32_to_cpu_array(u32 *buf, unsigned int words)
  131. {
  132. while (words--) {
  133. le32_to_cpus(buf);
  134. buf++;
  135. }
  136. }
  137. static inline void cpu_to_le32_array(u32 *buf, unsigned int words)
  138. {
  139. while (words--) {
  140. cpu_to_le32s(buf);
  141. buf++;
  142. }
  143. }
  144. static inline void md5_transform_helper(struct md5_ctx *ctx)
  145. {
  146. le32_to_cpu_array(ctx->block, sizeof(ctx->block) / sizeof(u32));
  147. md5_transform(ctx->hash, ctx->block);
  148. }
  149. static void md5_init(void *context)
  150. {
  151. struct md5_ctx *mctx = context;
  152. mctx->hash[0] = 0x67452301;
  153. mctx->hash[1] = 0xefcdab89;
  154. mctx->hash[2] = 0x98badcfe;
  155. mctx->hash[3] = 0x10325476;
  156. mctx->byte_count = 0;
  157. }
  158. static void md5_update(void *context, const void *data, size_t len)
  159. {
  160. struct md5_ctx *mctx = context;
  161. const u32 avail = sizeof(mctx->block) - (mctx->byte_count & 0x3f);
  162. mctx->byte_count += len;
  163. if (avail > len) {
  164. memcpy((char *)mctx->block + (sizeof(mctx->block) - avail),
  165. data, len);
  166. return;
  167. }
  168. memcpy((char *)mctx->block + (sizeof(mctx->block) - avail),
  169. data, avail);
  170. md5_transform_helper(mctx);
  171. data += avail;
  172. len -= avail;
  173. while (len >= sizeof(mctx->block)) {
  174. memcpy(mctx->block, data, sizeof(mctx->block));
  175. md5_transform_helper(mctx);
  176. data += sizeof(mctx->block);
  177. len -= sizeof(mctx->block);
  178. }
  179. memcpy(mctx->block, data, len);
  180. }
  181. static void md5_finish(void *context, void *out)
  182. {
  183. struct md5_ctx *mctx = context;
  184. const unsigned int offset = mctx->byte_count & 0x3f;
  185. char *p = (char *)mctx->block + offset;
  186. int padding = 56 - (offset + 1);
  187. *p++ = 0x80;
  188. if (padding < 0) {
  189. memset(p, 0x00, padding + sizeof (u64));
  190. md5_transform_helper(mctx);
  191. p = (char *)mctx->block;
  192. padding = 56;
  193. }
  194. memset(p, 0, padding);
  195. mctx->block[14] = mctx->byte_count << 3;
  196. mctx->block[15] = mctx->byte_count >> 29;
  197. le32_to_cpu_array(mctx->block, (sizeof(mctx->block) -
  198. sizeof(u64)) / sizeof(u32));
  199. md5_transform(mctx->hash, mctx->block);
  200. cpu_to_le32_array(mctx->hash, sizeof(mctx->hash) / sizeof(u32));
  201. memcpy(out, mctx->hash, sizeof(mctx->hash));
  202. memset(mctx, 0, sizeof(*mctx));
  203. }
  204. struct digest_algorithm md5_algorithm = {
  205. .name = "md5",
  206. .context_len = sizeof ( struct md5_ctx ),
  207. .digest_len = MD5_DIGEST_SIZE,
  208. .init = md5_init,
  209. .update = md5_update,
  210. .finish = md5_finish,
  211. };