Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. /*
  2. * Copyright (C) 2015 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-512 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/sha512.h>
  37. /** SHA-512 variables */
  38. struct sha512_variables {
  39. /* This layout matches that of struct sha512_digest_data,
  40. * allowing for efficient endianness-conversion,
  41. */
  42. uint64_t a;
  43. uint64_t b;
  44. uint64_t c;
  45. uint64_t d;
  46. uint64_t e;
  47. uint64_t f;
  48. uint64_t g;
  49. uint64_t h;
  50. uint64_t w[SHA512_ROUNDS];
  51. } __attribute__ (( packed ));
  52. /** SHA-512 constants */
  53. static const uint64_t k[SHA512_ROUNDS] = {
  54. 0x428a2f98d728ae22ULL, 0x7137449123ef65cdULL, 0xb5c0fbcfec4d3b2fULL,
  55. 0xe9b5dba58189dbbcULL, 0x3956c25bf348b538ULL, 0x59f111f1b605d019ULL,
  56. 0x923f82a4af194f9bULL, 0xab1c5ed5da6d8118ULL, 0xd807aa98a3030242ULL,
  57. 0x12835b0145706fbeULL, 0x243185be4ee4b28cULL, 0x550c7dc3d5ffb4e2ULL,
  58. 0x72be5d74f27b896fULL, 0x80deb1fe3b1696b1ULL, 0x9bdc06a725c71235ULL,
  59. 0xc19bf174cf692694ULL, 0xe49b69c19ef14ad2ULL, 0xefbe4786384f25e3ULL,
  60. 0x0fc19dc68b8cd5b5ULL, 0x240ca1cc77ac9c65ULL, 0x2de92c6f592b0275ULL,
  61. 0x4a7484aa6ea6e483ULL, 0x5cb0a9dcbd41fbd4ULL, 0x76f988da831153b5ULL,
  62. 0x983e5152ee66dfabULL, 0xa831c66d2db43210ULL, 0xb00327c898fb213fULL,
  63. 0xbf597fc7beef0ee4ULL, 0xc6e00bf33da88fc2ULL, 0xd5a79147930aa725ULL,
  64. 0x06ca6351e003826fULL, 0x142929670a0e6e70ULL, 0x27b70a8546d22ffcULL,
  65. 0x2e1b21385c26c926ULL, 0x4d2c6dfc5ac42aedULL, 0x53380d139d95b3dfULL,
  66. 0x650a73548baf63deULL, 0x766a0abb3c77b2a8ULL, 0x81c2c92e47edaee6ULL,
  67. 0x92722c851482353bULL, 0xa2bfe8a14cf10364ULL, 0xa81a664bbc423001ULL,
  68. 0xc24b8b70d0f89791ULL, 0xc76c51a30654be30ULL, 0xd192e819d6ef5218ULL,
  69. 0xd69906245565a910ULL, 0xf40e35855771202aULL, 0x106aa07032bbd1b8ULL,
  70. 0x19a4c116b8d2d0c8ULL, 0x1e376c085141ab53ULL, 0x2748774cdf8eeb99ULL,
  71. 0x34b0bcb5e19b48a8ULL, 0x391c0cb3c5c95a63ULL, 0x4ed8aa4ae3418acbULL,
  72. 0x5b9cca4f7763e373ULL, 0x682e6ff3d6b2b8a3ULL, 0x748f82ee5defb2fcULL,
  73. 0x78a5636f43172f60ULL, 0x84c87814a1f0ab72ULL, 0x8cc702081a6439ecULL,
  74. 0x90befffa23631e28ULL, 0xa4506cebde82bde9ULL, 0xbef9a3f7b2c67915ULL,
  75. 0xc67178f2e372532bULL, 0xca273eceea26619cULL, 0xd186b8c721c0c207ULL,
  76. 0xeada7dd6cde0eb1eULL, 0xf57d4f7fee6ed178ULL, 0x06f067aa72176fbaULL,
  77. 0x0a637dc5a2c898a6ULL, 0x113f9804bef90daeULL, 0x1b710b35131c471bULL,
  78. 0x28db77f523047d84ULL, 0x32caab7b40c72493ULL, 0x3c9ebe0a15c9bebcULL,
  79. 0x431d67c49c100d4cULL, 0x4cc5d4becb3e42b6ULL, 0x597f299cfc657e2aULL,
  80. 0x5fcb6fab3ad6faecULL, 0x6c44198c4a475817ULL
  81. };
  82. /** SHA-512 initial digest values */
  83. static const struct sha512_digest sha512_init_digest = {
  84. .h = {
  85. cpu_to_be64 ( 0x6a09e667f3bcc908ULL ),
  86. cpu_to_be64 ( 0xbb67ae8584caa73bULL ),
  87. cpu_to_be64 ( 0x3c6ef372fe94f82bULL ),
  88. cpu_to_be64 ( 0xa54ff53a5f1d36f1ULL ),
  89. cpu_to_be64 ( 0x510e527fade682d1ULL ),
  90. cpu_to_be64 ( 0x9b05688c2b3e6c1fULL ),
  91. cpu_to_be64 ( 0x1f83d9abfb41bd6bULL ),
  92. cpu_to_be64 ( 0x5be0cd19137e2179ULL ),
  93. },
  94. };
  95. /**
  96. * Initialise SHA-512 family algorithm
  97. *
  98. * @v context SHA-512 context
  99. * @v init Initial digest values
  100. * @v digestsize Digest size
  101. */
  102. void sha512_family_init ( struct sha512_context *context,
  103. const struct sha512_digest *init,
  104. size_t digestsize ) {
  105. context->len = 0;
  106. context->digestsize = digestsize;
  107. memcpy ( &context->ddq.dd.digest, init,
  108. sizeof ( context->ddq.dd.digest ) );
  109. }
  110. /**
  111. * Initialise SHA-512 algorithm
  112. *
  113. * @v ctx SHA-512 context
  114. */
  115. static void sha512_init ( void *ctx ) {
  116. struct sha512_context *context = ctx;
  117. sha512_family_init ( context, &sha512_init_digest,
  118. sizeof ( struct sha512_digest ) );
  119. }
  120. /**
  121. * Calculate SHA-512 digest of accumulated data
  122. *
  123. * @v context SHA-512 context
  124. */
  125. static void sha512_digest ( struct sha512_context *context ) {
  126. union {
  127. union sha512_digest_data_qwords ddq;
  128. struct sha512_variables v;
  129. } u;
  130. uint64_t *a = &u.v.a;
  131. uint64_t *b = &u.v.b;
  132. uint64_t *c = &u.v.c;
  133. uint64_t *d = &u.v.d;
  134. uint64_t *e = &u.v.e;
  135. uint64_t *f = &u.v.f;
  136. uint64_t *g = &u.v.g;
  137. uint64_t *h = &u.v.h;
  138. uint64_t *w = u.v.w;
  139. uint64_t s0;
  140. uint64_t s1;
  141. uint64_t maj;
  142. uint64_t t1;
  143. uint64_t t2;
  144. uint64_t ch;
  145. unsigned int i;
  146. /* Sanity checks */
  147. assert ( ( context->len % sizeof ( context->ddq.dd.data ) ) == 0 );
  148. linker_assert ( &u.ddq.dd.digest.h[0] == a, sha512_bad_layout );
  149. linker_assert ( &u.ddq.dd.digest.h[1] == b, sha512_bad_layout );
  150. linker_assert ( &u.ddq.dd.digest.h[2] == c, sha512_bad_layout );
  151. linker_assert ( &u.ddq.dd.digest.h[3] == d, sha512_bad_layout );
  152. linker_assert ( &u.ddq.dd.digest.h[4] == e, sha512_bad_layout );
  153. linker_assert ( &u.ddq.dd.digest.h[5] == f, sha512_bad_layout );
  154. linker_assert ( &u.ddq.dd.digest.h[6] == g, sha512_bad_layout );
  155. linker_assert ( &u.ddq.dd.digest.h[7] == h, sha512_bad_layout );
  156. linker_assert ( &u.ddq.dd.data.qword[0] == w, sha512_bad_layout );
  157. DBGC ( context, "SHA512 digesting:\n" );
  158. DBGC_HDA ( context, 0, &context->ddq.dd.digest,
  159. sizeof ( context->ddq.dd.digest ) );
  160. DBGC_HDA ( context, context->len, &context->ddq.dd.data,
  161. sizeof ( context->ddq.dd.data ) );
  162. /* Convert h[0..7] to host-endian, and initialise a, b, c, d,
  163. * e, f, g, h, and w[0..15]
  164. */
  165. for ( i = 0 ; i < ( sizeof ( u.ddq.qword ) /
  166. sizeof ( u.ddq.qword[0] ) ) ; i++ ) {
  167. be64_to_cpus ( &context->ddq.qword[i] );
  168. u.ddq.qword[i] = context->ddq.qword[i];
  169. }
  170. /* Initialise w[16..79] */
  171. for ( i = 16 ; i < SHA512_ROUNDS ; i++ ) {
  172. s0 = ( ror64 ( w[i-15], 1 ) ^ ror64 ( w[i-15], 8 ) ^
  173. ( w[i-15] >> 7 ) );
  174. s1 = ( ror64 ( w[i-2], 19 ) ^ ror64 ( w[i-2], 61 ) ^
  175. ( w[i-2] >> 6 ) );
  176. w[i] = ( w[i-16] + s0 + w[i-7] + s1 );
  177. }
  178. /* Main loop */
  179. for ( i = 0 ; i < SHA512_ROUNDS ; i++ ) {
  180. s0 = ( ror64 ( *a, 28 ) ^ ror64 ( *a, 34 ) ^ ror64 ( *a, 39 ) );
  181. maj = ( ( *a & *b ) ^ ( *a & *c ) ^ ( *b & *c ) );
  182. t2 = ( s0 + maj );
  183. s1 = ( ror64 ( *e, 14 ) ^ ror64 ( *e, 18 ) ^ ror64 ( *e, 41 ) );
  184. ch = ( ( *e & *f ) ^ ( (~*e) & *g ) );
  185. t1 = ( *h + s1 + ch + k[i] + w[i] );
  186. *h = *g;
  187. *g = *f;
  188. *f = *e;
  189. *e = ( *d + t1 );
  190. *d = *c;
  191. *c = *b;
  192. *b = *a;
  193. *a = ( t1 + t2 );
  194. DBGC2 ( context, "%2d : %016llx %016llx %016llx %016llx "
  195. "%016llx %016llx %016llx %016llx\n",
  196. i, *a, *b, *c, *d, *e, *f, *g, *h );
  197. }
  198. /* Add chunk to hash and convert back to big-endian */
  199. for ( i = 0 ; i < 8 ; i++ ) {
  200. context->ddq.dd.digest.h[i] =
  201. cpu_to_be64 ( context->ddq.dd.digest.h[i] +
  202. u.ddq.dd.digest.h[i] );
  203. }
  204. DBGC ( context, "SHA512 digested:\n" );
  205. DBGC_HDA ( context, 0, &context->ddq.dd.digest,
  206. sizeof ( context->ddq.dd.digest ) );
  207. }
  208. /**
  209. * Accumulate data with SHA-512 algorithm
  210. *
  211. * @v ctx SHA-512 context
  212. * @v data Data
  213. * @v len Length of data
  214. */
  215. void sha512_update ( void *ctx, const void *data, size_t len ) {
  216. struct sha512_context *context = ctx;
  217. const uint8_t *byte = data;
  218. size_t offset;
  219. /* Accumulate data a byte at a time, performing the digest
  220. * whenever we fill the data buffer
  221. */
  222. while ( len-- ) {
  223. offset = ( context->len % sizeof ( context->ddq.dd.data ) );
  224. context->ddq.dd.data.byte[offset] = *(byte++);
  225. context->len++;
  226. if ( ( context->len % sizeof ( context->ddq.dd.data ) ) == 0 )
  227. sha512_digest ( context );
  228. }
  229. }
  230. /**
  231. * Generate SHA-512 digest
  232. *
  233. * @v ctx SHA-512 context
  234. * @v out Output buffer
  235. */
  236. void sha512_final ( void *ctx, void *out ) {
  237. struct sha512_context *context = ctx;
  238. uint64_t len_bits_hi;
  239. uint64_t len_bits_lo;
  240. uint8_t pad;
  241. /* Record length before pre-processing */
  242. len_bits_hi = 0;
  243. len_bits_lo = cpu_to_be64 ( ( ( uint64_t ) context->len ) * 8 );
  244. /* Pad with a single "1" bit followed by as many "0" bits as required */
  245. pad = 0x80;
  246. do {
  247. sha512_update ( ctx, &pad, sizeof ( pad ) );
  248. pad = 0x00;
  249. } while ( ( context->len % sizeof ( context->ddq.dd.data ) ) !=
  250. offsetof ( typeof ( context->ddq.dd.data ), final.len_hi ) );
  251. /* Append length (in bits) */
  252. sha512_update ( ctx, &len_bits_hi, sizeof ( len_bits_hi ) );
  253. sha512_update ( ctx, &len_bits_lo, sizeof ( len_bits_lo ) );
  254. assert ( ( context->len % sizeof ( context->ddq.dd.data ) ) == 0 );
  255. /* Copy out final digest */
  256. memcpy ( out, &context->ddq.dd.digest, context->digestsize );
  257. }
  258. /** SHA-512 algorithm */
  259. struct digest_algorithm sha512_algorithm = {
  260. .name = "sha512",
  261. .ctxsize = sizeof ( struct sha512_context ),
  262. .blocksize = sizeof ( union sha512_block ),
  263. .digestsize = sizeof ( struct sha512_digest ),
  264. .init = sha512_init,
  265. .update = sha512_update,
  266. .final = sha512_final,
  267. };
  268. /** "sha512" object identifier */
  269. static uint8_t oid_sha512[] = { ASN1_OID_SHA512 };
  270. /** "sha512" OID-identified algorithm */
  271. struct asn1_algorithm oid_sha512_algorithm __asn1_algorithm = {
  272. .name = "sha512",
  273. .digest = &sha512_algorithm,
  274. .oid = ASN1_OID_CURSOR ( oid_sha512 ),
  275. };