m_sha3.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /*
  2. * Copyright 2017 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <stdio.h>
  10. #include <string.h>
  11. #include <openssl/evp.h>
  12. #include <openssl/objects.h>
  13. #include "internal/evp_int.h"
  14. #include "evp_locl.h"
  15. size_t SHA3_absorb(uint64_t A[5][5], const unsigned char *inp, size_t len,
  16. size_t r);
  17. void SHA3_squeeze(uint64_t A[5][5], unsigned char *out, size_t len, size_t r);
  18. #define KECCAK1600_WIDTH 1600
  19. typedef struct {
  20. uint64_t A[5][5];
  21. size_t block_size; /* cached ctx->digest->block_size */
  22. size_t md_size; /* output length, variable in XOF */
  23. size_t num; /* used bytes in below buffer */
  24. unsigned char buf[KECCAK1600_WIDTH / 8 - 32];
  25. unsigned char pad;
  26. } KECCAK1600_CTX;
  27. static int init(EVP_MD_CTX *evp_ctx, unsigned char pad)
  28. {
  29. KECCAK1600_CTX *ctx = evp_ctx->md_data;
  30. size_t bsz = evp_ctx->digest->block_size;
  31. if (bsz <= sizeof(ctx->buf)) {
  32. memset(ctx->A, 0, sizeof(ctx->A));
  33. ctx->num = 0;
  34. ctx->block_size = bsz;
  35. ctx->md_size = evp_ctx->digest->md_size;
  36. ctx->pad = pad;
  37. return 1;
  38. }
  39. return 0;
  40. }
  41. static int sha3_init(EVP_MD_CTX *evp_ctx)
  42. {
  43. return init(evp_ctx, '\x06');
  44. }
  45. static int shake_init(EVP_MD_CTX *evp_ctx)
  46. {
  47. return init(evp_ctx, '\x1f');
  48. }
  49. static int sha3_update(EVP_MD_CTX *evp_ctx, const void *_inp, size_t len)
  50. {
  51. KECCAK1600_CTX *ctx = evp_ctx->md_data;
  52. const unsigned char *inp = _inp;
  53. size_t bsz = ctx->block_size;
  54. size_t num, rem;
  55. if ((num = ctx->num) != 0) { /* process intermediate buffer? */
  56. rem = bsz - num;
  57. if (len < rem) {
  58. memcpy(ctx->buf + num, inp, len);
  59. ctx->num += len;
  60. return 1;
  61. }
  62. /*
  63. * We have enough data to fill or overflow the intermediate
  64. * buffer. So we append |rem| bytes and process the block,
  65. * leaving the rest for later processing...
  66. */
  67. memcpy(ctx->buf + num, inp, rem);
  68. inp += rem, len -= rem;
  69. (void)SHA3_absorb(ctx->A, ctx->buf, bsz, bsz);
  70. ctx->num = 0;
  71. /* ctx->buf is processed, ctx->num is guaranteed to be zero */
  72. }
  73. if (len >= bsz)
  74. rem = SHA3_absorb(ctx->A, inp, len, bsz);
  75. else
  76. rem = len;
  77. if (rem) {
  78. memcpy(ctx->buf, inp + len - rem, rem);
  79. ctx->num = rem;
  80. }
  81. return 1;
  82. }
  83. static int sha3_final(EVP_MD_CTX *evp_ctx, unsigned char *md)
  84. {
  85. KECCAK1600_CTX *ctx = evp_ctx->md_data;
  86. size_t bsz = ctx->block_size;
  87. size_t num = ctx->num;
  88. /*
  89. * Pad the data with 10*1. Note that |num| can be |bsz - 1|
  90. * in which case both byte operations below are performed on
  91. * same byte...
  92. */
  93. memset(ctx->buf + num, 0, bsz - num);
  94. ctx->buf[num] = ctx->pad;
  95. ctx->buf[bsz - 1] |= 0x80;
  96. (void)SHA3_absorb(ctx->A, ctx->buf, bsz, bsz);
  97. SHA3_squeeze(ctx->A, md, ctx->md_size, bsz);
  98. return 1;
  99. }
  100. static int shake_ctrl(EVP_MD_CTX *evp_ctx, int cmd, int p1, void *p2)
  101. {
  102. KECCAK1600_CTX *ctx = evp_ctx->md_data;
  103. switch (cmd) {
  104. case EVP_MD_CTRL_XOF_LEN:
  105. ctx->md_size = p1;
  106. return 1;
  107. default:
  108. return 0;
  109. }
  110. }
  111. #define EVP_MD_SHA3(bitlen) \
  112. const EVP_MD *EVP_sha3_##bitlen(void) \
  113. { \
  114. static const EVP_MD sha3_##bitlen##_md = { \
  115. NID_sha3_##bitlen, \
  116. NID_RSA_SHA3_##bitlen, \
  117. bitlen / 8, \
  118. EVP_MD_FLAG_DIGALGID_ABSENT, \
  119. sha3_init, \
  120. sha3_update, \
  121. sha3_final, \
  122. NULL, \
  123. NULL, \
  124. (KECCAK1600_WIDTH - bitlen * 2) / 8, \
  125. sizeof(KECCAK1600_CTX), \
  126. }; \
  127. return &sha3_##bitlen##_md; \
  128. }
  129. EVP_MD_SHA3(224)
  130. EVP_MD_SHA3(256)
  131. EVP_MD_SHA3(384)
  132. EVP_MD_SHA3(512)
  133. #define EVP_MD_SHAKE(bitlen) \
  134. const EVP_MD *EVP_shake##bitlen(void) \
  135. { \
  136. static const EVP_MD shake##bitlen##_md = { \
  137. NID_shake##bitlen, \
  138. 0, \
  139. bitlen / 8, \
  140. EVP_MD_FLAG_XOF, \
  141. shake_init, \
  142. sha3_update, \
  143. sha3_final, \
  144. NULL, \
  145. NULL, \
  146. (KECCAK1600_WIDTH - bitlen * 2) / 8, \
  147. sizeof(KECCAK1600_CTX), \
  148. shake_ctrl \
  149. }; \
  150. return &shake##bitlen##_md; \
  151. }
  152. EVP_MD_SHAKE(128)
  153. EVP_MD_SHAKE(256)