您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

sha1.c 7.0KB

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