You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

md5.c 8.0KB

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