Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

sha1.c 6.9KB

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