s2n_hmac.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. /* this file is patched by sidetrail, clang-format invalidates patches */
  16. /* clang-format off */
  17. #pragma once
  18. #include <stdint.h>
  19. #include "crypto/s2n_hash.h"
  20. typedef enum {
  21. S2N_HMAC_NONE,
  22. S2N_HMAC_MD5,
  23. S2N_HMAC_SHA1,
  24. S2N_HMAC_SHA224,
  25. S2N_HMAC_SHA256,
  26. S2N_HMAC_SHA384,
  27. S2N_HMAC_SHA512,
  28. S2N_HMAC_SSLv3_MD5,
  29. S2N_HMAC_SSLv3_SHA1
  30. } s2n_hmac_algorithm;
  31. struct s2n_hmac_state {
  32. s2n_hmac_algorithm alg;
  33. uint16_t hash_block_size;
  34. uint32_t currently_in_hash_block;
  35. uint16_t xor_pad_size;
  36. uint8_t digest_size;
  37. struct s2n_hash_state inner;
  38. struct s2n_hash_state inner_just_key;
  39. struct s2n_hash_state outer;
  40. struct s2n_hash_state outer_just_key;
  41. /* key needs to be as large as the biggest block size */
  42. uint8_t xor_pad[128];
  43. /* For storing the inner digest */
  44. uint8_t digest_pad[SHA512_DIGEST_LENGTH];
  45. };
  46. struct s2n_hmac_evp_backup {
  47. struct s2n_hash_evp_digest inner;
  48. struct s2n_hash_evp_digest inner_just_key;
  49. struct s2n_hash_evp_digest outer;
  50. struct s2n_hash_evp_digest outer_just_key;
  51. };
  52. int s2n_hmac_digest_size(s2n_hmac_algorithm alg, uint8_t *out);
  53. bool s2n_hmac_is_available(s2n_hmac_algorithm alg);
  54. int s2n_hmac_hash_alg(s2n_hmac_algorithm hmac_alg, s2n_hash_algorithm *out);
  55. int s2n_hash_hmac_alg(s2n_hash_algorithm hash_alg, s2n_hmac_algorithm *out);
  56. int s2n_hmac_new(struct s2n_hmac_state *state);
  57. S2N_RESULT s2n_hmac_state_validate(struct s2n_hmac_state *state);
  58. int s2n_hmac_init(struct s2n_hmac_state *state, s2n_hmac_algorithm alg, const void *key, uint32_t klen);
  59. int s2n_hmac_update(struct s2n_hmac_state *state, const void *in, uint32_t size);
  60. int s2n_hmac_digest(struct s2n_hmac_state *state, void *out, uint32_t size);
  61. int s2n_hmac_digest_two_compression_rounds(struct s2n_hmac_state *state, void *out, uint32_t size);
  62. int s2n_hmac_digest_verify(const void *a, const void *b, uint32_t len);
  63. int s2n_hmac_free(struct s2n_hmac_state *state);
  64. int s2n_hmac_reset(struct s2n_hmac_state *state);
  65. int s2n_hmac_copy(struct s2n_hmac_state *to, struct s2n_hmac_state *from);
  66. int s2n_hmac_save_evp_hash_state(struct s2n_hmac_evp_backup* backup, struct s2n_hmac_state* hmac);
  67. int s2n_hmac_restore_evp_hash_state(struct s2n_hmac_evp_backup* backup, struct s2n_hmac_state* hmac);
  68. S2N_RESULT s2n_hmac_md_from_alg(s2n_hmac_algorithm alg, const EVP_MD **md);