您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. /*
  2. * Copyright (C) 2012 Michael Brown <mbrown@fensystems.co.uk>.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 of the
  7. * License, or any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301, USA.
  18. */
  19. FILE_LICENCE ( GPL2_OR_LATER );
  20. /** @file
  21. *
  22. * MD5 algorithm
  23. *
  24. */
  25. #include <stdint.h>
  26. #include <string.h>
  27. #include <byteswap.h>
  28. #include <assert.h>
  29. #include <ipxe/rotate.h>
  30. #include <ipxe/crypto.h>
  31. #include <ipxe/asn1.h>
  32. #include <ipxe/md5.h>
  33. /** MD5 variables */
  34. struct md5_variables {
  35. /* This layout matches that of struct md5_digest_data,
  36. * allowing for efficient endianness-conversion,
  37. */
  38. uint32_t a;
  39. uint32_t b;
  40. uint32_t c;
  41. uint32_t d;
  42. uint32_t w[16];
  43. } __attribute__ (( packed ));
  44. /** MD5 constants */
  45. static const uint32_t k[64] = {
  46. 0xd76aa478, 0xe8c7b756, 0x242070db, 0xc1bdceee, 0xf57c0faf, 0x4787c62a,
  47. 0xa8304613, 0xfd469501, 0x698098d8, 0x8b44f7af, 0xffff5bb1, 0x895cd7be,
  48. 0x6b901122, 0xfd987193, 0xa679438e, 0x49b40821, 0xf61e2562, 0xc040b340,
  49. 0x265e5a51, 0xe9b6c7aa, 0xd62f105d, 0x02441453, 0xd8a1e681, 0xe7d3fbc8,
  50. 0x21e1cde6, 0xc33707d6, 0xf4d50d87, 0x455a14ed, 0xa9e3e905, 0xfcefa3f8,
  51. 0x676f02d9, 0x8d2a4c8a, 0xfffa3942, 0x8771f681, 0x6d9d6122, 0xfde5380c,
  52. 0xa4beea44, 0x4bdecfa9, 0xf6bb4b60, 0xbebfbc70, 0x289b7ec6, 0xeaa127fa,
  53. 0xd4ef3085, 0x04881d05, 0xd9d4d039, 0xe6db99e5, 0x1fa27cf8, 0xc4ac5665,
  54. 0xf4292244, 0x432aff97, 0xab9423a7, 0xfc93a039, 0x655b59c3, 0x8f0ccc92,
  55. 0xffeff47d, 0x85845dd1, 0x6fa87e4f, 0xfe2ce6e0, 0xa3014314, 0x4e0811a1,
  56. 0xf7537e82, 0xbd3af235, 0x2ad7d2bb, 0xeb86d391
  57. };
  58. /** MD5 shift amounts */
  59. static const uint8_t r[64] = {
  60. 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22,
  61. 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20,
  62. 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23,
  63. 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21
  64. };
  65. /**
  66. * f(b,c,d) for steps 0 to 15
  67. *
  68. * @v v MD5 variables
  69. * @ret f f(b,c,d)
  70. */
  71. static uint32_t md5_f_0_15 ( struct md5_variables *v ) {
  72. return ( v->d ^ ( v->b & ( v->c ^ v->d ) ) );
  73. }
  74. /**
  75. * f(b,c,d) for steps 16 to 31
  76. *
  77. * @v v MD5 variables
  78. * @ret f f(b,c,d)
  79. */
  80. static uint32_t md5_f_16_31 ( struct md5_variables *v ) {
  81. return ( v->c ^ ( v->d & ( v->b ^ v->c ) ) );
  82. }
  83. /**
  84. * f(b,c,d) for steps 32 to 47
  85. *
  86. * @v v MD5 variables
  87. * @ret f f(b,c,d)
  88. */
  89. static uint32_t md5_f_32_47 ( struct md5_variables *v ) {
  90. return ( v->b ^ v->c ^ v->d );
  91. }
  92. /**
  93. * f(b,c,d) for steps 48 to 63
  94. *
  95. * @v v MD5 variables
  96. * @ret f f(b,c,d)
  97. */
  98. static uint32_t md5_f_48_63 ( struct md5_variables *v ) {
  99. return ( v->c ^ ( v->b | (~v->d) ) );
  100. }
  101. /** An MD5 step function */
  102. struct md5_step {
  103. /**
  104. * Calculate f(b,c,d)
  105. *
  106. * @v v MD5 variables
  107. * @ret f f(b,c,d)
  108. */
  109. uint32_t ( * f ) ( struct md5_variables *v );
  110. /** Coefficient of i in g=ni+m */
  111. uint8_t coefficient;
  112. /** Constant term in g=ni+m */
  113. uint8_t constant;
  114. };
  115. /** MD5 steps */
  116. static struct md5_step md5_steps[4] = {
  117. /** 0 to 15 */
  118. { .f = md5_f_0_15, .coefficient = 1, .constant = 0 },
  119. /** 16 to 31 */
  120. { .f = md5_f_16_31, .coefficient = 5, .constant = 1 },
  121. /** 32 to 47 */
  122. { .f = md5_f_32_47, .coefficient = 3, .constant = 5 },
  123. /** 48 to 63 */
  124. { .f = md5_f_48_63, .coefficient = 7, .constant = 0 },
  125. };
  126. /**
  127. * Initialise MD5 algorithm
  128. *
  129. * @v ctx MD5 context
  130. */
  131. static void md5_init ( void *ctx ) {
  132. struct md5_context *context = ctx;
  133. context->ddd.dd.digest.h[0] = cpu_to_le32 ( 0x67452301 );
  134. context->ddd.dd.digest.h[1] = cpu_to_le32 ( 0xefcdab89 );
  135. context->ddd.dd.digest.h[2] = cpu_to_le32 ( 0x98badcfe );
  136. context->ddd.dd.digest.h[3] = cpu_to_le32 ( 0x10325476 );
  137. context->len = 0;
  138. }
  139. /**
  140. * Calculate MD5 digest of accumulated data
  141. *
  142. * @v context MD5 context
  143. */
  144. static void md5_digest ( struct md5_context *context ) {
  145. union {
  146. union md5_digest_data_dwords ddd;
  147. struct md5_variables v;
  148. } u;
  149. uint32_t *a = &u.v.a;
  150. uint32_t *b = &u.v.b;
  151. uint32_t *c = &u.v.c;
  152. uint32_t *d = &u.v.d;
  153. uint32_t *w = u.v.w;
  154. uint32_t f;
  155. uint32_t g;
  156. uint32_t temp;
  157. struct md5_step *step;
  158. unsigned int i;
  159. /* Sanity checks */
  160. assert ( ( context->len % sizeof ( context->ddd.dd.data ) ) == 0 );
  161. linker_assert ( &u.ddd.dd.digest.h[0] == a, md5_bad_layout );
  162. linker_assert ( &u.ddd.dd.digest.h[1] == b, md5_bad_layout );
  163. linker_assert ( &u.ddd.dd.digest.h[2] == c, md5_bad_layout );
  164. linker_assert ( &u.ddd.dd.digest.h[3] == d, md5_bad_layout );
  165. linker_assert ( &u.ddd.dd.data.dword[0] == w, md5_bad_layout );
  166. DBGC ( context, "MD5 digesting:\n" );
  167. DBGC_HDA ( context, 0, &context->ddd.dd.digest,
  168. sizeof ( context->ddd.dd.digest ) );
  169. DBGC_HDA ( context, context->len, &context->ddd.dd.data,
  170. sizeof ( context->ddd.dd.data ) );
  171. /* Convert h[0..3] to host-endian, and initialise a, b, c, d,
  172. * and w[0..15]
  173. */
  174. for ( i = 0 ; i < ( sizeof ( u.ddd.dword ) /
  175. sizeof ( u.ddd.dword[0] ) ) ; i++ ) {
  176. le32_to_cpus ( &context->ddd.dword[i] );
  177. u.ddd.dword[i] = context->ddd.dword[i];
  178. }
  179. /* Main loop */
  180. for ( i = 0 ; i < 64 ; i++ ) {
  181. step = &md5_steps[ i / 16 ];
  182. f = step->f ( &u.v );
  183. g = ( ( ( step->coefficient * i ) + step->constant ) % 16 );
  184. temp = *d;
  185. *d = *c;
  186. *c = *b;
  187. *b = ( *b + rol32 ( ( *a + f + k[i] + w[g] ), r[i] ) );
  188. *a = temp;
  189. DBGC2 ( context, "%2d : %08x %08x %08x %08x\n",
  190. i, *a, *b, *c, *d );
  191. }
  192. /* Add chunk to hash and convert back to big-endian */
  193. for ( i = 0 ; i < 4 ; i++ ) {
  194. context->ddd.dd.digest.h[i] =
  195. cpu_to_le32 ( context->ddd.dd.digest.h[i] +
  196. u.ddd.dd.digest.h[i] );
  197. }
  198. DBGC ( context, "MD5 digested:\n" );
  199. DBGC_HDA ( context, 0, &context->ddd.dd.digest,
  200. sizeof ( context->ddd.dd.digest ) );
  201. }
  202. /**
  203. * Accumulate data with MD5 algorithm
  204. *
  205. * @v ctx MD5 context
  206. * @v data Data
  207. * @v len Length of data
  208. */
  209. static void md5_update ( void *ctx, const void *data, size_t len ) {
  210. struct md5_context *context = ctx;
  211. const uint8_t *byte = data;
  212. size_t offset;
  213. /* Accumulate data a byte at a time, performing the digest
  214. * whenever we fill the data buffer
  215. */
  216. while ( len-- ) {
  217. offset = ( context->len % sizeof ( context->ddd.dd.data ) );
  218. context->ddd.dd.data.byte[offset] = *(byte++);
  219. context->len++;
  220. if ( ( context->len % sizeof ( context->ddd.dd.data ) ) == 0 )
  221. md5_digest ( context );
  222. }
  223. }
  224. /**
  225. * Generate MD5 digest
  226. *
  227. * @v ctx MD5 context
  228. * @v out Output buffer
  229. */
  230. static void md5_final ( void *ctx, void *out ) {
  231. struct md5_context *context = ctx;
  232. uint64_t len_bits;
  233. uint8_t pad;
  234. /* Record length before pre-processing */
  235. len_bits = cpu_to_le64 ( ( ( uint64_t ) context->len ) * 8 );
  236. /* Pad with a single "1" bit followed by as many "0" bits as required */
  237. pad = 0x80;
  238. do {
  239. md5_update ( ctx, &pad, sizeof ( pad ) );
  240. pad = 0x00;
  241. } while ( ( context->len % sizeof ( context->ddd.dd.data ) ) !=
  242. offsetof ( typeof ( context->ddd.dd.data ), final.len ) );
  243. /* Append length (in bits) */
  244. md5_update ( ctx, &len_bits, sizeof ( len_bits ) );
  245. assert ( ( context->len % sizeof ( context->ddd.dd.data ) ) == 0 );
  246. /* Copy out final digest */
  247. memcpy ( out, &context->ddd.dd.digest,
  248. sizeof ( context->ddd.dd.digest ) );
  249. }
  250. /** MD5 algorithm */
  251. struct digest_algorithm md5_algorithm = {
  252. .name = "md5",
  253. .ctxsize = sizeof ( struct md5_context ),
  254. .blocksize = sizeof ( union md5_block ),
  255. .digestsize = sizeof ( struct md5_digest ),
  256. .init = md5_init,
  257. .update = md5_update,
  258. .final = md5_final,
  259. };
  260. /** "md5" object identifier */
  261. static uint8_t oid_md5[] = { ASN1_OID_MD5 };
  262. /** "md5" OID-identified algorithm */
  263. struct asn1_algorithm oid_md5_algorithm __asn1_algorithm = {
  264. .name = "md5",
  265. .digest = &md5_algorithm,
  266. .oid = ASN1_OID_CURSOR ( oid_md5 ),
  267. };