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.

md4.c 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. /*
  2. * Copyright (C) 2017 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. * MD4 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/md4.h>
  37. /** MD4 variables */
  38. struct md4_variables {
  39. /* This layout matches that of struct md4_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. /** MD4 shift amounts */
  49. static const uint8_t r[3][4] = {
  50. { 3, 7, 11, 19 },
  51. { 3, 5, 9, 13 },
  52. { 3, 9, 11, 15 },
  53. };
  54. /**
  55. * f(b,c,d,w) for steps 0 to 15
  56. *
  57. * @v v MD4 variables
  58. * @v i Index within round
  59. * @ret f f(b,c,d,w)
  60. */
  61. static uint32_t md4_f_0_15 ( struct md4_variables *v, unsigned int i ) {
  62. return ( ( ( v->b & v->c ) | ( ~v->b & v->d ) ) + v->w[i] );
  63. }
  64. /**
  65. * f(b,c,d,w) for steps 16 to 31
  66. *
  67. * @v v MD4 variables
  68. * @v i Index within round
  69. * @ret f f(b,c,d,w)
  70. */
  71. static uint32_t md4_f_16_31 ( struct md4_variables *v, unsigned int i ) {
  72. return ( ( ( v->b & v->c ) | ( v->b & v->d ) | ( v->c & v->d ) ) +
  73. v->w[ ( ( i << 2 ) | ( i >> 2 ) ) % 16 ] );
  74. }
  75. /**
  76. * f(b,c,d,w) for steps 32 to 47
  77. *
  78. * @v v MD4 variables
  79. * @v i Index within round
  80. * @ret f f(b,c,d,w)
  81. */
  82. static uint32_t md4_f_32_47 ( struct md4_variables *v, unsigned int i ) {
  83. static const uint8_t reverse[16] = {
  84. 0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 15
  85. };
  86. return ( ( v->b ^ v->c ^ v->d ) + v->w[reverse[i]] );
  87. }
  88. /** An MD4 step function */
  89. struct md4_step {
  90. /**
  91. * Calculate f(b,c,d,w)
  92. *
  93. * @v v MD4 variables
  94. * @v i Index within round
  95. * @ret f f(b,c,d,w)
  96. */
  97. uint32_t ( * f ) ( struct md4_variables *v, unsigned int i );
  98. /** Constant */
  99. uint32_t constant;
  100. };
  101. /** MD4 steps */
  102. static struct md4_step md4_steps[4] = {
  103. /** 0 to 15 */
  104. { .f = md4_f_0_15, .constant = 0x00000000UL },
  105. /** 16 to 31 */
  106. { .f = md4_f_16_31, .constant = 0x5a827999UL },
  107. /** 32 to 47 */
  108. { .f = md4_f_32_47, .constant = 0x6ed9eba1UL },
  109. };
  110. /**
  111. * Initialise MD4 algorithm
  112. *
  113. * @v ctx MD4 context
  114. */
  115. static void md4_init ( void *ctx ) {
  116. struct md4_context *context = ctx;
  117. context->ddd.dd.digest.h[0] = cpu_to_le32 ( 0x67452301 );
  118. context->ddd.dd.digest.h[1] = cpu_to_le32 ( 0xefcdab89 );
  119. context->ddd.dd.digest.h[2] = cpu_to_le32 ( 0x98badcfe );
  120. context->ddd.dd.digest.h[3] = cpu_to_le32 ( 0x10325476 );
  121. context->len = 0;
  122. }
  123. /**
  124. * Calculate MD4 digest of accumulated data
  125. *
  126. * @v context MD4 context
  127. */
  128. static void md4_digest ( struct md4_context *context ) {
  129. union {
  130. union md4_digest_data_dwords ddd;
  131. struct md4_variables v;
  132. } u;
  133. uint32_t *a = &u.v.a;
  134. uint32_t *b = &u.v.b;
  135. uint32_t *c = &u.v.c;
  136. uint32_t *d = &u.v.d;
  137. uint32_t *w = u.v.w;
  138. uint32_t f;
  139. uint32_t temp;
  140. struct md4_step *step;
  141. unsigned int round;
  142. unsigned int i;
  143. /* Sanity checks */
  144. assert ( ( context->len % sizeof ( context->ddd.dd.data ) ) == 0 );
  145. linker_assert ( &u.ddd.dd.digest.h[0] == a, md4_bad_layout );
  146. linker_assert ( &u.ddd.dd.digest.h[1] == b, md4_bad_layout );
  147. linker_assert ( &u.ddd.dd.digest.h[2] == c, md4_bad_layout );
  148. linker_assert ( &u.ddd.dd.digest.h[3] == d, md4_bad_layout );
  149. linker_assert ( &u.ddd.dd.data.dword[0] == w, md4_bad_layout );
  150. DBGC ( context, "MD4 digesting:\n" );
  151. DBGC_HDA ( context, 0, &context->ddd.dd.digest,
  152. sizeof ( context->ddd.dd.digest ) );
  153. DBGC_HDA ( context, context->len, &context->ddd.dd.data,
  154. sizeof ( context->ddd.dd.data ) );
  155. /* Convert h[0..3] to host-endian, and initialise a, b, c, d,
  156. * and x[0..15]
  157. */
  158. for ( i = 0 ; i < ( sizeof ( u.ddd.dword ) /
  159. sizeof ( u.ddd.dword[0] ) ) ; i++ ) {
  160. le32_to_cpus ( &context->ddd.dword[i] );
  161. u.ddd.dword[i] = context->ddd.dword[i];
  162. }
  163. /* Main loop */
  164. for ( i = 0 ; i < 48 ; i++ ) {
  165. round = ( i / 16 );
  166. step = &md4_steps[round];
  167. f = step->f ( &u.v, ( i % 16 ) );
  168. temp = *d;
  169. *d = *c;
  170. *c = *b;
  171. *b = rol32 ( ( *a + f + step->constant ), r[round][ i % 4 ] );
  172. *a = temp;
  173. DBGC2 ( context, "%2d : %08x %08x %08x %08x\n",
  174. i, *a, *b, *c, *d );
  175. }
  176. /* Add chunk to hash and convert back to little-endian */
  177. for ( i = 0 ; i < 4 ; i++ ) {
  178. context->ddd.dd.digest.h[i] =
  179. cpu_to_le32 ( context->ddd.dd.digest.h[i] +
  180. u.ddd.dd.digest.h[i] );
  181. }
  182. DBGC ( context, "MD4 digested:\n" );
  183. DBGC_HDA ( context, 0, &context->ddd.dd.digest,
  184. sizeof ( context->ddd.dd.digest ) );
  185. }
  186. /**
  187. * Accumulate data with MD4 algorithm
  188. *
  189. * @v ctx MD4 context
  190. * @v data Data
  191. * @v len Length of data
  192. */
  193. static void md4_update ( void *ctx, const void *data, size_t len ) {
  194. struct md4_context *context = ctx;
  195. const uint8_t *byte = data;
  196. size_t offset;
  197. /* Accumulate data a byte at a time, performing the digest
  198. * whenever we fill the data buffer
  199. */
  200. while ( len-- ) {
  201. offset = ( context->len % sizeof ( context->ddd.dd.data ) );
  202. context->ddd.dd.data.byte[offset] = *(byte++);
  203. context->len++;
  204. if ( ( context->len % sizeof ( context->ddd.dd.data ) ) == 0 )
  205. md4_digest ( context );
  206. }
  207. }
  208. /**
  209. * Generate MD4 digest
  210. *
  211. * @v ctx MD4 context
  212. * @v out Output buffer
  213. */
  214. static void md4_final ( void *ctx, void *out ) {
  215. struct md4_context *context = ctx;
  216. uint64_t len_bits;
  217. uint8_t pad;
  218. /* Record length before pre-processing */
  219. len_bits = cpu_to_le64 ( ( ( uint64_t ) context->len ) * 8 );
  220. /* Pad with a single "1" bit followed by as many "0" bits as required */
  221. pad = 0x80;
  222. do {
  223. md4_update ( ctx, &pad, sizeof ( pad ) );
  224. pad = 0x00;
  225. } while ( ( context->len % sizeof ( context->ddd.dd.data ) ) !=
  226. offsetof ( typeof ( context->ddd.dd.data ), final.len ) );
  227. /* Append length (in bits) */
  228. md4_update ( ctx, &len_bits, sizeof ( len_bits ) );
  229. assert ( ( context->len % sizeof ( context->ddd.dd.data ) ) == 0 );
  230. /* Copy out final digest */
  231. memcpy ( out, &context->ddd.dd.digest,
  232. sizeof ( context->ddd.dd.digest ) );
  233. }
  234. /** MD4 algorithm */
  235. struct digest_algorithm md4_algorithm = {
  236. .name = "md4",
  237. .ctxsize = sizeof ( struct md4_context ),
  238. .blocksize = sizeof ( union md4_block ),
  239. .digestsize = sizeof ( struct md4_digest ),
  240. .init = md4_init,
  241. .update = md4_update,
  242. .final = md4_final,
  243. };
  244. /** "md4" object identifier */
  245. static uint8_t oid_md4[] = { ASN1_OID_MD4 };
  246. /** "md4" OID-identified algorithm */
  247. struct asn1_algorithm oid_md4_algorithm __asn1_algorithm = {
  248. .name = "md4",
  249. .digest = &md4_algorithm,
  250. .oid = ASN1_OID_CURSOR ( oid_md4 ),
  251. };