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 8.2KB

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