s2n_hash.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License").
  5. * You may not use this file except in compliance with the License.
  6. * A copy of the License is located at
  7. *
  8. * http://aws.amazon.com/apache2.0
  9. *
  10. * or in the "license" file accompanying this file. This file is distributed
  11. * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
  12. * express or implied. See the License for the specific language governing
  13. * permissions and limitations under the License.
  14. */
  15. #pragma once
  16. #include <openssl/md5.h>
  17. #include <openssl/sha.h>
  18. #include <stdbool.h>
  19. #include <stdint.h>
  20. #include "crypto/s2n_evp.h"
  21. #define S2N_MAX_DIGEST_LEN SHA512_DIGEST_LENGTH
  22. typedef enum {
  23. S2N_HASH_NONE = 0,
  24. S2N_HASH_MD5,
  25. S2N_HASH_SHA1,
  26. S2N_HASH_SHA224,
  27. S2N_HASH_SHA256,
  28. S2N_HASH_SHA384,
  29. S2N_HASH_SHA512,
  30. S2N_HASH_MD5_SHA1,
  31. /* Don't add any hash algorithms below S2N_HASH_SENTINEL */
  32. S2N_HASH_SENTINEL
  33. } s2n_hash_algorithm;
  34. /* The low_level_digest stores all OpenSSL structs that are alg-specific to be used with OpenSSL's low-level hash API's. */
  35. union s2n_hash_low_level_digest {
  36. MD5_CTX md5;
  37. SHA_CTX sha1;
  38. SHA256_CTX sha224;
  39. SHA256_CTX sha256;
  40. SHA512_CTX sha384;
  41. SHA512_CTX sha512;
  42. struct {
  43. MD5_CTX md5;
  44. SHA_CTX sha1;
  45. } md5_sha1;
  46. };
  47. /* The evp_digest stores all OpenSSL structs to be used with OpenSSL's EVP hash API's. */
  48. struct s2n_hash_evp_digest {
  49. struct s2n_evp_digest evp;
  50. /* Always store a secondary evp_digest to allow resetting a hash_state to MD5_SHA1 from another alg. */
  51. struct s2n_evp_digest evp_md5_secondary;
  52. };
  53. /* s2n_hash_state stores the s2n_hash implementation being used (low-level or EVP),
  54. * the hash algorithm being used at the time, and either low_level or high_level (EVP) OpenSSL digest structs.
  55. */
  56. struct s2n_hash_state {
  57. const struct s2n_hash *hash_impl;
  58. s2n_hash_algorithm alg;
  59. uint8_t is_ready_for_input;
  60. uint64_t currently_in_hash;
  61. union {
  62. union s2n_hash_low_level_digest low_level;
  63. struct s2n_hash_evp_digest high_level;
  64. } digest;
  65. };
  66. /* The s2n hash implementation is abstracted to allow for separate implementations, using
  67. * either OpenSSL's low-level algorithm-specific API's or OpenSSL's EVP API's.
  68. */
  69. struct s2n_hash {
  70. int (*alloc)(struct s2n_hash_state *state);
  71. int (*allow_md5_for_fips)(struct s2n_hash_state *state);
  72. int (*init)(struct s2n_hash_state *state, s2n_hash_algorithm alg);
  73. int (*update)(struct s2n_hash_state *state, const void *data, uint32_t size);
  74. int (*digest)(struct s2n_hash_state *state, void *out, uint32_t size);
  75. int (*copy)(struct s2n_hash_state *to, struct s2n_hash_state *from);
  76. int (*reset)(struct s2n_hash_state *state);
  77. int (*free)(struct s2n_hash_state *state);
  78. };
  79. bool s2n_hash_evp_fully_supported();
  80. const EVP_MD *s2n_hash_alg_to_evp_md(s2n_hash_algorithm alg);
  81. int s2n_hash_digest_size(s2n_hash_algorithm alg, uint8_t *out);
  82. int s2n_hash_block_size(s2n_hash_algorithm alg, uint64_t *block_size);
  83. bool s2n_hash_is_available(s2n_hash_algorithm alg);
  84. int s2n_hash_is_ready_for_input(struct s2n_hash_state *state);
  85. int s2n_hash_new(struct s2n_hash_state *state);
  86. S2N_RESULT s2n_hash_state_validate(struct s2n_hash_state *state);
  87. int s2n_hash_allow_md5_for_fips(struct s2n_hash_state *state);
  88. int s2n_hash_init(struct s2n_hash_state *state, s2n_hash_algorithm alg);
  89. int s2n_hash_update(struct s2n_hash_state *state, const void *data, uint32_t size);
  90. int s2n_hash_digest(struct s2n_hash_state *state, void *out, uint32_t size);
  91. int s2n_hash_copy(struct s2n_hash_state *to, struct s2n_hash_state *from);
  92. int s2n_hash_reset(struct s2n_hash_state *state);
  93. int s2n_hash_free(struct s2n_hash_state *state);
  94. int s2n_hash_get_currently_in_hash_total(struct s2n_hash_state *state, uint64_t *out);
  95. int s2n_hash_const_time_get_currently_in_hash_block(struct s2n_hash_state *state, uint64_t *out);