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.1KB

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