core_h2c.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. #include <assert.h>
  2. #include <errno.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include "core_h2c.h"
  6. #include "crypto_hash_sha256.h"
  7. #include "crypto_hash_sha512.h"
  8. #include "private/common.h"
  9. #define HASH_BYTES crypto_hash_sha256_BYTES
  10. #define HASH_BLOCKBYTES 64U
  11. static int
  12. core_h2c_string_to_hash_sha256(unsigned char *h, const size_t h_len, const char *ctx,
  13. const unsigned char *msg, size_t msg_len)
  14. {
  15. crypto_hash_sha256_state st;
  16. const unsigned char empty_block[HASH_BLOCKBYTES] = { 0 };
  17. unsigned char u0[HASH_BYTES];
  18. unsigned char ux[HASH_BYTES] = { 0 };
  19. unsigned char t[3] = { 0U, (unsigned char) h_len, 0U};
  20. unsigned char ctx_len_u8;
  21. size_t ctx_len = ctx != NULL ? strlen(ctx) : 0U;
  22. size_t i, j;
  23. assert(h_len <= 0xff);
  24. if (ctx_len > (size_t) 0xff) {
  25. crypto_hash_sha256_init(&st);
  26. crypto_hash_sha256_update(&st,
  27. (const unsigned char *) "H2C-OVERSIZE-DST-",
  28. sizeof "H2C-OVERSIZE-DST-" - 1U);
  29. crypto_hash_sha256_update(&st, (const unsigned char *) ctx, ctx_len);
  30. crypto_hash_sha256_final(&st, u0);
  31. ctx = (const char *) u0;
  32. ctx_len = HASH_BYTES;
  33. COMPILER_ASSERT(HASH_BYTES <= (size_t) 0xff);
  34. }
  35. ctx_len_u8 = (unsigned char) ctx_len;
  36. crypto_hash_sha256_init(&st);
  37. crypto_hash_sha256_update(&st, empty_block, sizeof empty_block);
  38. crypto_hash_sha256_update(&st, msg, msg_len);
  39. crypto_hash_sha256_update(&st, t, 3U);
  40. crypto_hash_sha256_update(&st, (const unsigned char *) ctx, ctx_len);
  41. crypto_hash_sha256_update(&st, &ctx_len_u8, 1U);
  42. crypto_hash_sha256_final(&st, u0);
  43. for (i = 0U; i < h_len; i += HASH_BYTES) {
  44. for (j = 0U; j < HASH_BYTES; j++) {
  45. ux[j] ^= u0[j];
  46. }
  47. t[2]++;
  48. crypto_hash_sha256_init(&st);
  49. crypto_hash_sha256_update(&st, ux, HASH_BYTES);
  50. crypto_hash_sha256_update(&st, &t[2], 1U);
  51. crypto_hash_sha256_update(&st, (const unsigned char *) ctx, ctx_len);
  52. crypto_hash_sha256_update(&st, &ctx_len_u8, 1U);
  53. crypto_hash_sha256_final(&st, ux);
  54. memcpy(&h[i], ux, h_len - i >= (sizeof ux) ? (sizeof ux) : h_len - i);
  55. }
  56. return 0;
  57. }
  58. #undef HASH_BYTES
  59. #undef HASH_BLOCKBYTES
  60. #define HASH_BYTES crypto_hash_sha512_BYTES
  61. #define HASH_BLOCKBYTES 128U
  62. static int
  63. core_h2c_string_to_hash_sha512(unsigned char *h, const size_t h_len, const char *ctx,
  64. const unsigned char *msg, size_t msg_len)
  65. {
  66. crypto_hash_sha512_state st;
  67. const unsigned char empty_block[HASH_BLOCKBYTES] = { 0 };
  68. unsigned char u0[HASH_BYTES];
  69. unsigned char ux[HASH_BYTES] = { 0 };
  70. unsigned char t[3] = { 0U, (unsigned char) h_len, 0U};
  71. unsigned char ctx_len_u8;
  72. size_t ctx_len = ctx != NULL ? strlen(ctx) : 0U;
  73. size_t i, j;
  74. assert(h_len <= 0xff);
  75. if (ctx_len > (size_t) 0xff) {
  76. crypto_hash_sha512_init(&st);
  77. crypto_hash_sha512_update(&st,
  78. (const unsigned char *) "H2C-OVERSIZE-DST-",
  79. sizeof "H2C-OVERSIZE-DST-" - 1U);
  80. crypto_hash_sha512_update(&st, (const unsigned char *) ctx, ctx_len);
  81. crypto_hash_sha512_final(&st, u0);
  82. ctx = (const char *) u0;
  83. ctx_len = HASH_BYTES;
  84. COMPILER_ASSERT(HASH_BYTES <= (size_t) 0xff);
  85. }
  86. ctx_len_u8 = (unsigned char) ctx_len;
  87. crypto_hash_sha512_init(&st);
  88. crypto_hash_sha512_update(&st, empty_block, sizeof empty_block);
  89. crypto_hash_sha512_update(&st, msg, msg_len);
  90. crypto_hash_sha512_update(&st, t, 3U);
  91. crypto_hash_sha512_update(&st, (const unsigned char *) ctx, ctx_len);
  92. crypto_hash_sha512_update(&st, &ctx_len_u8, 1U);
  93. crypto_hash_sha512_final(&st, u0);
  94. for (i = 0U; i < h_len; i += HASH_BYTES) {
  95. for (j = 0U; j < HASH_BYTES; j++) {
  96. ux[j] ^= u0[j];
  97. }
  98. t[2]++;
  99. crypto_hash_sha512_init(&st);
  100. crypto_hash_sha512_update(&st, ux, HASH_BYTES);
  101. crypto_hash_sha512_update(&st, &t[2], 1U);
  102. crypto_hash_sha512_update(&st, (const unsigned char *) ctx, ctx_len);
  103. crypto_hash_sha512_update(&st, &ctx_len_u8, 1U);
  104. crypto_hash_sha512_final(&st, ux);
  105. memcpy(&h[i], ux, h_len - i >= (sizeof ux) ? (sizeof ux) : h_len - i);
  106. }
  107. return 0;
  108. }
  109. int
  110. core_h2c_string_to_hash(unsigned char *h, const size_t h_len, const char *ctx,
  111. const unsigned char *msg, size_t msg_len, int hash_alg)
  112. {
  113. switch (hash_alg) {
  114. case CORE_H2C_SHA256:
  115. return core_h2c_string_to_hash_sha256(h, h_len, ctx, msg, msg_len);
  116. case CORE_H2C_SHA512:
  117. return core_h2c_string_to_hash_sha512(h, h_len, ctx, msg, msg_len);
  118. default:
  119. errno = EINVAL;
  120. return -1;
  121. }
  122. }