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.

sha256.c 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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-256 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/sha256.h>
  30. /**
  31. * Rotate dword right
  32. *
  33. * @v dword Dword
  34. * @v rotate Amount of rotation
  35. */
  36. static inline __attribute__ (( always_inline )) uint32_t
  37. ror32 ( uint32_t dword, unsigned int rotate ) {
  38. return ( ( dword >> rotate ) | ( dword << ( 32 - rotate ) ) );
  39. }
  40. /** SHA-256 variables */
  41. struct sha256_variables {
  42. /* This layout matches that of struct sha256_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 f;
  51. uint32_t g;
  52. uint32_t h;
  53. uint32_t w[64];
  54. } __attribute__ (( packed ));
  55. /** SHA-256 constants */
  56. static const uint32_t k[64] = {
  57. 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1,
  58. 0x923f82a4, 0xab1c5ed5, 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
  59. 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, 0xe49b69c1, 0xefbe4786,
  60. 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
  61. 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147,
  62. 0x06ca6351, 0x14292967, 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
  63. 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, 0xa2bfe8a1, 0xa81a664b,
  64. 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
  65. 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a,
  66. 0x5b9cca4f, 0x682e6ff3, 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
  67. 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
  68. };
  69. /**
  70. * Initialise SHA-256 algorithm
  71. *
  72. * @v ctx SHA-256 context
  73. */
  74. static void sha256_init ( void *ctx ) {
  75. struct sha256_context *context = ctx;
  76. context->ddd.dd.digest.h[0] = cpu_to_be32 ( 0x6a09e667 );
  77. context->ddd.dd.digest.h[1] = cpu_to_be32 ( 0xbb67ae85 );
  78. context->ddd.dd.digest.h[2] = cpu_to_be32 ( 0x3c6ef372 );
  79. context->ddd.dd.digest.h[3] = cpu_to_be32 ( 0xa54ff53a );
  80. context->ddd.dd.digest.h[4] = cpu_to_be32 ( 0x510e527f );
  81. context->ddd.dd.digest.h[5] = cpu_to_be32 ( 0x9b05688c );
  82. context->ddd.dd.digest.h[6] = cpu_to_be32 ( 0x1f83d9ab );
  83. context->ddd.dd.digest.h[7] = cpu_to_be32 ( 0x5be0cd19 );
  84. context->len = 0;
  85. }
  86. /**
  87. * Calculate SHA-256 digest of accumulated data
  88. *
  89. * @v context SHA-256 context
  90. */
  91. static void sha256_digest ( struct sha256_context *context ) {
  92. union {
  93. union sha256_digest_data_dwords ddd;
  94. struct sha256_variables v;
  95. } u;
  96. uint32_t *a = &u.v.a;
  97. uint32_t *b = &u.v.b;
  98. uint32_t *c = &u.v.c;
  99. uint32_t *d = &u.v.d;
  100. uint32_t *e = &u.v.e;
  101. uint32_t *f = &u.v.f;
  102. uint32_t *g = &u.v.g;
  103. uint32_t *h = &u.v.h;
  104. uint32_t *w = u.v.w;
  105. uint32_t s0;
  106. uint32_t s1;
  107. uint32_t maj;
  108. uint32_t t1;
  109. uint32_t t2;
  110. uint32_t ch;
  111. unsigned int i;
  112. /* Sanity checks */
  113. assert ( ( context->len % sizeof ( context->ddd.dd.data ) ) == 0 );
  114. linker_assert ( &u.ddd.dd.digest.h[0] == a, sha256_bad_layout );
  115. linker_assert ( &u.ddd.dd.digest.h[1] == b, sha256_bad_layout );
  116. linker_assert ( &u.ddd.dd.digest.h[2] == c, sha256_bad_layout );
  117. linker_assert ( &u.ddd.dd.digest.h[3] == d, sha256_bad_layout );
  118. linker_assert ( &u.ddd.dd.digest.h[4] == e, sha256_bad_layout );
  119. linker_assert ( &u.ddd.dd.digest.h[5] == f, sha256_bad_layout );
  120. linker_assert ( &u.ddd.dd.digest.h[6] == g, sha256_bad_layout );
  121. linker_assert ( &u.ddd.dd.digest.h[7] == h, sha256_bad_layout );
  122. linker_assert ( &u.ddd.dd.data.dword[0] == w, sha256_bad_layout );
  123. DBGC ( context, "SHA256 digesting:\n" );
  124. DBGC_HDA ( context, 0, &context->ddd.dd.digest,
  125. sizeof ( context->ddd.dd.digest ) );
  126. DBGC_HDA ( context, context->len, &context->ddd.dd.data,
  127. sizeof ( context->ddd.dd.data ) );
  128. /* Convert h[0..7] to host-endian, and initialise a, b, c, d,
  129. * e, f, g, h, and w[0..15]
  130. */
  131. for ( i = 0 ; i < ( sizeof ( u.ddd.dword ) /
  132. sizeof ( u.ddd.dword[0] ) ) ; i++ ) {
  133. be32_to_cpus ( &context->ddd.dword[i] );
  134. u.ddd.dword[i] = context->ddd.dword[i];
  135. }
  136. /* Initialise w[16..63] */
  137. for ( i = 16 ; i < 64 ; i++ ) {
  138. s0 = ( ror32 ( w[i-15], 7 ) ^ ror32 ( w[i-15], 18 ) ^
  139. ( w[i-15] >> 3 ) );
  140. s1 = ( ror32 ( w[i-2], 17 ) ^ ror32 ( w[i-2], 19 ) ^
  141. ( w[i-2] >> 10 ) );
  142. w[i] = ( w[i-16] + s0 + w[i-7] + s1 );
  143. }
  144. /* Main loop */
  145. for ( i = 0 ; i < 64 ; i++ ) {
  146. s0 = ( ror32 ( *a, 2 ) ^ ror32 ( *a, 13 ) ^ ror32 ( *a, 22 ) );
  147. maj = ( ( *a & *b ) ^ ( *a & *c ) ^ ( *b & *c ) );
  148. t2 = ( s0 + maj );
  149. s1 = ( ror32 ( *e, 6 ) ^ ror32 ( *e, 11 ) ^ ror32 ( *e, 25 ) );
  150. ch = ( ( *e & *f ) ^ ( (~*e) & *g ) );
  151. t1 = ( *h + s1 + ch + k[i] + w[i] );
  152. *h = *g;
  153. *g = *f;
  154. *f = *e;
  155. *e = ( *d + t1 );
  156. *d = *c;
  157. *c = *b;
  158. *b = *a;
  159. *a = ( t1 + t2 );
  160. DBGC2 ( context, "%2d : %08x %08x %08x %08x %08x %08x %08x "
  161. "%08x\n", i, *a, *b, *c, *d, *e, *f, *g, *h );
  162. }
  163. /* Add chunk to hash and convert back to big-endian */
  164. for ( i = 0 ; i < 8 ; i++ ) {
  165. context->ddd.dd.digest.h[i] =
  166. cpu_to_be32 ( context->ddd.dd.digest.h[i] +
  167. u.ddd.dd.digest.h[i] );
  168. }
  169. DBGC ( context, "SHA256 digested:\n" );
  170. DBGC_HDA ( context, 0, &context->ddd.dd.digest,
  171. sizeof ( context->ddd.dd.digest ) );
  172. }
  173. /**
  174. * Accumulate data with SHA-256 algorithm
  175. *
  176. * @v ctx SHA-256 context
  177. * @v data Data
  178. * @v len Length of data
  179. */
  180. static void sha256_update ( void *ctx, const void *data, size_t len ) {
  181. struct sha256_context *context = ctx;
  182. const uint8_t *byte = data;
  183. size_t offset;
  184. /* Accumulate data a byte at a time, performing the digest
  185. * whenever we fill the data buffer
  186. */
  187. while ( len-- ) {
  188. offset = ( context->len % sizeof ( context->ddd.dd.data ) );
  189. context->ddd.dd.data.byte[offset] = *(byte++);
  190. context->len++;
  191. if ( ( context->len % sizeof ( context->ddd.dd.data ) ) == 0 )
  192. sha256_digest ( context );
  193. }
  194. }
  195. /**
  196. * Generate SHA-256 digest
  197. *
  198. * @v ctx SHA-256 context
  199. * @v out Output buffer
  200. */
  201. static void sha256_final ( void *ctx, void *out ) {
  202. struct sha256_context *context = ctx;
  203. uint64_t len_bits;
  204. uint8_t pad;
  205. /* Record length before pre-processing */
  206. len_bits = cpu_to_be64 ( ( ( uint64_t ) context->len ) * 8 );
  207. /* Pad with a single "1" bit followed by as many "0" bits as required */
  208. pad = 0x80;
  209. do {
  210. sha256_update ( ctx, &pad, sizeof ( pad ) );
  211. pad = 0x00;
  212. } while ( ( context->len % sizeof ( context->ddd.dd.data ) ) !=
  213. offsetof ( typeof ( context->ddd.dd.data ), final.len ) );
  214. /* Append length (in bits) */
  215. sha256_update ( ctx, &len_bits, sizeof ( len_bits ) );
  216. assert ( ( context->len % sizeof ( context->ddd.dd.data ) ) == 0 );
  217. /* Copy out final digest */
  218. memcpy ( out, &context->ddd.dd.digest,
  219. sizeof ( context->ddd.dd.digest ) );
  220. }
  221. /** SHA-256 algorithm */
  222. struct digest_algorithm sha256_algorithm = {
  223. .name = "sha256",
  224. .ctxsize = sizeof ( struct sha256_context ),
  225. .blocksize = sizeof ( union sha256_block ),
  226. .digestsize = sizeof ( struct sha256_digest ),
  227. .init = sha256_init,
  228. .update = sha256_update,
  229. .final = sha256_final,
  230. };