25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 

149 satır
5.5 KiB

  1. #include <stdint.h>
  2. #include <string.h>
  3. #include "address.h"
  4. #include "hash.h"
  5. #include "params.h"
  6. #include "utils.h"
  7. #include "sha2.h"
  8. #include "sha256.h"
  9. /* For SHA256, there is no immediate reason to initialize at the start,
  10. so this function is an empty operation. */
  11. void PQCLEAN_SPHINCSSHA256128FROBUST_CLEAN_initialize_hash_function(
  12. const unsigned char *pub_seed, const unsigned char *sk_seed) {
  13. PQCLEAN_SPHINCSSHA256128FROBUST_CLEAN_seed_state(pub_seed);
  14. (void)sk_seed; /* Suppress an 'unused parameter' warning. */
  15. }
  16. /*
  17. * Computes PRF(key, addr), given a secret key of SPX_N bytes and an address
  18. */
  19. void PQCLEAN_SPHINCSSHA256128FROBUST_CLEAN_prf_addr(
  20. unsigned char *out, const unsigned char *key, const uint32_t addr[8]) {
  21. unsigned char buf[SPX_N + SPX_SHA256_ADDR_BYTES];
  22. unsigned char outbuf[SPX_SHA256_OUTPUT_BYTES];
  23. memcpy(buf, key, SPX_N);
  24. PQCLEAN_SPHINCSSHA256128FROBUST_CLEAN_compress_address(buf + SPX_N, addr);
  25. sha256(outbuf, buf, SPX_N + SPX_SHA256_ADDR_BYTES);
  26. memcpy(out, outbuf, SPX_N);
  27. }
  28. /**
  29. * Computes the message-dependent randomness R, using a secret seed as a key
  30. * for HMAC, and an optional randomization value prefixed to the message.
  31. * This requires m to have at least SPX_SHA256_BLOCK_BYTES + SPX_N space
  32. * available in front of the pointer, i.e. before the message to use for the
  33. * prefix. This is necessary to prevent having to move the message around (and
  34. * allocate memory for it).
  35. */
  36. void PQCLEAN_SPHINCSSHA256128FROBUST_CLEAN_gen_message_random(
  37. unsigned char *R,
  38. const unsigned char *sk_prf, const unsigned char *optrand,
  39. const unsigned char *m, size_t mlen) {
  40. unsigned char buf[SPX_SHA256_BLOCK_BYTES + SPX_SHA256_OUTPUT_BYTES];
  41. uint8_t state[40];
  42. int i;
  43. /* This implements HMAC-SHA256 */
  44. for (i = 0; i < SPX_N; i++) {
  45. buf[i] = 0x36 ^ sk_prf[i];
  46. }
  47. memset(buf + SPX_N, 0x36, SPX_SHA256_BLOCK_BYTES - SPX_N);
  48. sha256_inc_init(state);
  49. sha256_inc_blocks(state, buf, 1);
  50. memcpy(buf, optrand, SPX_N);
  51. /* If optrand + message cannot fill up an entire block */
  52. if (SPX_N + mlen < SPX_SHA256_BLOCK_BYTES) {
  53. memcpy(buf + SPX_N, m, mlen);
  54. sha256_inc_finalize(buf + SPX_SHA256_BLOCK_BYTES, state,
  55. buf, mlen + SPX_N);
  56. }
  57. /* Otherwise first fill a block, so that finalize only uses the message */
  58. else {
  59. memcpy(buf + SPX_N, m, SPX_SHA256_BLOCK_BYTES - SPX_N);
  60. sha256_inc_blocks(state, buf, 1);
  61. m += SPX_SHA256_BLOCK_BYTES - SPX_N;
  62. mlen -= SPX_SHA256_BLOCK_BYTES - SPX_N;
  63. sha256_inc_finalize(buf + SPX_SHA256_BLOCK_BYTES, state, m, mlen);
  64. }
  65. for (i = 0; i < SPX_N; i++) {
  66. buf[i] = 0x5c ^ sk_prf[i];
  67. }
  68. memset(buf + SPX_N, 0x5c, SPX_SHA256_BLOCK_BYTES - SPX_N);
  69. sha256(buf, buf, SPX_SHA256_BLOCK_BYTES + SPX_SHA256_OUTPUT_BYTES);
  70. memcpy(R, buf, SPX_N);
  71. }
  72. /**
  73. * Computes the message hash using R, the public key, and the message.
  74. * Outputs the message digest and the index of the leaf. The index is split in
  75. * the tree index and the leaf index, for convenient copying to an address.
  76. */
  77. void PQCLEAN_SPHINCSSHA256128FROBUST_CLEAN_hash_message(
  78. unsigned char *digest, uint64_t *tree, uint32_t *leaf_idx,
  79. const unsigned char *R, const unsigned char *pk,
  80. const unsigned char *m, size_t mlen) {
  81. #define SPX_TREE_BITS (SPX_TREE_HEIGHT * (SPX_D - 1))
  82. #define SPX_TREE_BYTES ((SPX_TREE_BITS + 7) / 8)
  83. #define SPX_LEAF_BITS SPX_TREE_HEIGHT
  84. #define SPX_LEAF_BYTES ((SPX_LEAF_BITS + 7) / 8)
  85. #define SPX_DGST_BYTES (SPX_FORS_MSG_BYTES + SPX_TREE_BYTES + SPX_LEAF_BYTES)
  86. unsigned char seed[SPX_SHA256_OUTPUT_BYTES];
  87. /* Round to nearest multiple of SPX_SHA256_BLOCK_BYTES */
  88. #define SPX_INBLOCKS (((SPX_N + SPX_PK_BYTES + SPX_SHA256_BLOCK_BYTES - 1) & \
  89. -SPX_SHA256_BLOCK_BYTES) / SPX_SHA256_BLOCK_BYTES)
  90. unsigned char inbuf[SPX_INBLOCKS * SPX_SHA256_BLOCK_BYTES];
  91. unsigned char buf[SPX_DGST_BYTES];
  92. unsigned char *bufp = buf;
  93. uint8_t state[40];
  94. sha256_inc_init(state);
  95. memcpy(inbuf, R, SPX_N);
  96. memcpy(inbuf + SPX_N, pk, SPX_PK_BYTES);
  97. /* If R + pk + message cannot fill up an entire block */
  98. if (SPX_N + SPX_PK_BYTES + mlen < SPX_INBLOCKS * SPX_SHA256_BLOCK_BYTES) {
  99. memcpy(inbuf + SPX_N + SPX_PK_BYTES, m, mlen);
  100. sha256_inc_finalize(seed, state, inbuf, SPX_N + SPX_PK_BYTES + mlen);
  101. }
  102. /* Otherwise first fill a block, so that finalize only uses the message */
  103. else {
  104. memcpy(inbuf + SPX_N + SPX_PK_BYTES, m,
  105. SPX_INBLOCKS * SPX_SHA256_BLOCK_BYTES - SPX_N - SPX_PK_BYTES);
  106. sha256_inc_blocks(state, inbuf, SPX_INBLOCKS);
  107. m += SPX_INBLOCKS * SPX_SHA256_BLOCK_BYTES - SPX_N - SPX_PK_BYTES;
  108. mlen -= SPX_INBLOCKS * SPX_SHA256_BLOCK_BYTES - SPX_N - SPX_PK_BYTES;
  109. sha256_inc_finalize(seed, state, m, mlen);
  110. }
  111. /* By doing this in two steps, we prevent hashing the message twice;
  112. otherwise each iteration in MGF1 would hash the message again. */
  113. PQCLEAN_SPHINCSSHA256128FROBUST_CLEAN_mgf1(bufp, SPX_DGST_BYTES, seed, SPX_SHA256_OUTPUT_BYTES);
  114. memcpy(digest, bufp, SPX_FORS_MSG_BYTES);
  115. bufp += SPX_FORS_MSG_BYTES;
  116. *tree = PQCLEAN_SPHINCSSHA256128FROBUST_CLEAN_bytes_to_ull(bufp, SPX_TREE_BYTES);
  117. *tree &= (~(uint64_t)0) >> (64 - SPX_TREE_BITS);
  118. bufp += SPX_TREE_BYTES;
  119. *leaf_idx = (uint32_t)PQCLEAN_SPHINCSSHA256128FROBUST_CLEAN_bytes_to_ull(
  120. bufp, SPX_LEAF_BYTES);
  121. *leaf_idx &= (~(uint32_t)0) >> (32 - SPX_LEAF_BITS);
  122. }