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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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. * SHA-1 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/sha1.h>
  33. /** SHA-1 variables */
  34. struct sha1_variables {
  35. /* This layout matches that of struct sha1_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 e;
  43. uint32_t w[80];
  44. } __attribute__ (( packed ));
  45. /**
  46. * f(a,b,c,d) for steps 0 to 19
  47. *
  48. * @v v SHA-1 variables
  49. * @ret f f(a,b,c,d)
  50. */
  51. static uint32_t sha1_f_0_19 ( struct sha1_variables *v ) {
  52. return ( ( v->b & v->c ) | ( (~v->b) & v->d ) );
  53. }
  54. /**
  55. * f(a,b,c,d) for steps 20 to 39 and 60 to 79
  56. *
  57. * @v v SHA-1 variables
  58. * @ret f f(a,b,c,d)
  59. */
  60. static uint32_t sha1_f_20_39_60_79 ( struct sha1_variables *v ) {
  61. return ( v->b ^ v->c ^ v->d );
  62. }
  63. /**
  64. * f(a,b,c,d) for steps 40 to 59
  65. *
  66. * @v v SHA-1 variables
  67. * @ret f f(a,b,c,d)
  68. */
  69. static uint32_t sha1_f_40_59 ( struct sha1_variables *v ) {
  70. return ( ( v->b & v->c ) | ( v->b & v->d ) | ( v->c & v->d ) );
  71. }
  72. /** An SHA-1 step function */
  73. struct sha1_step {
  74. /**
  75. * Calculate f(a,b,c,d)
  76. *
  77. * @v v SHA-1 variables
  78. * @ret f f(a,b,c,d)
  79. */
  80. uint32_t ( * f ) ( struct sha1_variables *v );
  81. /** Constant k */
  82. uint32_t k;
  83. };
  84. /** SHA-1 steps */
  85. static struct sha1_step sha1_steps[4] = {
  86. /** 0 to 19 */
  87. { .f = sha1_f_0_19, .k = 0x5a827999 },
  88. /** 20 to 39 */
  89. { .f = sha1_f_20_39_60_79, .k = 0x6ed9eba1 },
  90. /** 40 to 59 */
  91. { .f = sha1_f_40_59, .k = 0x8f1bbcdc },
  92. /** 60 to 79 */
  93. { .f = sha1_f_20_39_60_79, .k = 0xca62c1d6 },
  94. };
  95. /**
  96. * Initialise SHA-1 algorithm
  97. *
  98. * @v ctx SHA-1 context
  99. */
  100. static void sha1_init ( void *ctx ) {
  101. struct sha1_context *context = ctx;
  102. context->ddd.dd.digest.h[0] = cpu_to_be32 ( 0x67452301 );
  103. context->ddd.dd.digest.h[1] = cpu_to_be32 ( 0xefcdab89 );
  104. context->ddd.dd.digest.h[2] = cpu_to_be32 ( 0x98badcfe );
  105. context->ddd.dd.digest.h[3] = cpu_to_be32 ( 0x10325476 );
  106. context->ddd.dd.digest.h[4] = cpu_to_be32 ( 0xc3d2e1f0 );
  107. context->len = 0;
  108. }
  109. /**
  110. * Calculate SHA-1 digest of accumulated data
  111. *
  112. * @v context SHA-1 context
  113. */
  114. static void sha1_digest ( struct sha1_context *context ) {
  115. union {
  116. union sha1_digest_data_dwords ddd;
  117. struct sha1_variables v;
  118. } u;
  119. uint32_t *a = &u.v.a;
  120. uint32_t *b = &u.v.b;
  121. uint32_t *c = &u.v.c;
  122. uint32_t *d = &u.v.d;
  123. uint32_t *e = &u.v.e;
  124. uint32_t *w = u.v.w;
  125. uint32_t f;
  126. uint32_t k;
  127. uint32_t temp;
  128. struct sha1_step *step;
  129. unsigned int i;
  130. /* Sanity checks */
  131. assert ( ( context->len % sizeof ( context->ddd.dd.data ) ) == 0 );
  132. linker_assert ( &u.ddd.dd.digest.h[0] == a, sha1_bad_layout );
  133. linker_assert ( &u.ddd.dd.digest.h[1] == b, sha1_bad_layout );
  134. linker_assert ( &u.ddd.dd.digest.h[2] == c, sha1_bad_layout );
  135. linker_assert ( &u.ddd.dd.digest.h[3] == d, sha1_bad_layout );
  136. linker_assert ( &u.ddd.dd.digest.h[4] == e, sha1_bad_layout );
  137. linker_assert ( &u.ddd.dd.data.dword[0] == w, sha1_bad_layout );
  138. DBGC ( context, "SHA1 digesting:\n" );
  139. DBGC_HDA ( context, 0, &context->ddd.dd.digest,
  140. sizeof ( context->ddd.dd.digest ) );
  141. DBGC_HDA ( context, context->len, &context->ddd.dd.data,
  142. sizeof ( context->ddd.dd.data ) );
  143. /* Convert h[0..4] to host-endian, and initialise a, b, c, d,
  144. * e, and w[0..15]
  145. */
  146. for ( i = 0 ; i < ( sizeof ( u.ddd.dword ) /
  147. sizeof ( u.ddd.dword[0] ) ) ; i++ ) {
  148. be32_to_cpus ( &context->ddd.dword[i] );
  149. u.ddd.dword[i] = context->ddd.dword[i];
  150. }
  151. /* Initialise w[16..79] */
  152. for ( i = 16 ; i < 80 ; i++ )
  153. w[i] = rol32 ( ( w[i-3] ^ w[i-8] ^ w[i-14] ^ w[i-16] ), 1 );
  154. /* Main loop */
  155. for ( i = 0 ; i < 80 ; i++ ) {
  156. step = &sha1_steps[ i / 20 ];
  157. f = step->f ( &u.v );
  158. k = step->k;
  159. temp = ( rol32 ( *a, 5 ) + f + *e + k + w[i] );
  160. *e = *d;
  161. *d = *c;
  162. *c = rol32 ( *b, 30 );
  163. *b = *a;
  164. *a = temp;
  165. DBGC2 ( context, "%2d : %08x %08x %08x %08x %08x\n",
  166. i, *a, *b, *c, *d, *e );
  167. }
  168. /* Add chunk to hash and convert back to big-endian */
  169. for ( i = 0 ; i < 5 ; i++ ) {
  170. context->ddd.dd.digest.h[i] =
  171. cpu_to_be32 ( context->ddd.dd.digest.h[i] +
  172. u.ddd.dd.digest.h[i] );
  173. }
  174. DBGC ( context, "SHA1 digested:\n" );
  175. DBGC_HDA ( context, 0, &context->ddd.dd.digest,
  176. sizeof ( context->ddd.dd.digest ) );
  177. }
  178. /**
  179. * Accumulate data with SHA-1 algorithm
  180. *
  181. * @v ctx SHA-1 context
  182. * @v data Data
  183. * @v len Length of data
  184. */
  185. static void sha1_update ( void *ctx, const void *data, size_t len ) {
  186. struct sha1_context *context = ctx;
  187. const uint8_t *byte = data;
  188. size_t offset;
  189. /* Accumulate data a byte at a time, performing the digest
  190. * whenever we fill the data buffer
  191. */
  192. while ( len-- ) {
  193. offset = ( context->len % sizeof ( context->ddd.dd.data ) );
  194. context->ddd.dd.data.byte[offset] = *(byte++);
  195. context->len++;
  196. if ( ( context->len % sizeof ( context->ddd.dd.data ) ) == 0 )
  197. sha1_digest ( context );
  198. }
  199. }
  200. /**
  201. * Generate SHA-1 digest
  202. *
  203. * @v ctx SHA-1 context
  204. * @v out Output buffer
  205. */
  206. static void sha1_final ( void *ctx, void *out ) {
  207. struct sha1_context *context = ctx;
  208. uint64_t len_bits;
  209. uint8_t pad;
  210. /* Record length before pre-processing */
  211. len_bits = cpu_to_be64 ( ( ( uint64_t ) context->len ) * 8 );
  212. /* Pad with a single "1" bit followed by as many "0" bits as required */
  213. pad = 0x80;
  214. do {
  215. sha1_update ( ctx, &pad, sizeof ( pad ) );
  216. pad = 0x00;
  217. } while ( ( context->len % sizeof ( context->ddd.dd.data ) ) !=
  218. offsetof ( typeof ( context->ddd.dd.data ), final.len ) );
  219. /* Append length (in bits) */
  220. sha1_update ( ctx, &len_bits, sizeof ( len_bits ) );
  221. assert ( ( context->len % sizeof ( context->ddd.dd.data ) ) == 0 );
  222. /* Copy out final digest */
  223. memcpy ( out, &context->ddd.dd.digest,
  224. sizeof ( context->ddd.dd.digest ) );
  225. }
  226. /** SHA-1 algorithm */
  227. struct digest_algorithm sha1_algorithm = {
  228. .name = "sha1",
  229. .ctxsize = sizeof ( struct sha1_context ),
  230. .blocksize = sizeof ( union sha1_block ),
  231. .digestsize = sizeof ( struct sha1_digest ),
  232. .init = sha1_init,
  233. .update = sha1_update,
  234. .final = sha1_final,
  235. };
  236. /** "sha1" object identifier */
  237. static uint8_t oid_sha1[] = { ASN1_OID_SHA1 };
  238. /** "sha1" OID-identified algorithm */
  239. struct asn1_algorithm oid_sha1_algorithm __asn1_algorithm = {
  240. .name = "sha1",
  241. .digest = &sha1_algorithm,
  242. .oid = ASN1_OID_CURSOR ( oid_sha1 ),
  243. };