s2n_cipher.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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/aes.h>
  17. #include <openssl/des.h>
  18. #include <openssl/dh.h>
  19. #include <openssl/evp.h>
  20. #include <openssl/rc4.h>
  21. #include <openssl/rsa.h>
  22. #include "crypto/s2n_crypto.h"
  23. #include "crypto/s2n_ktls_crypto.h"
  24. #include "utils/s2n_blob.h"
  25. #if defined(OPENSSL_IS_BORINGSSL) || defined(OPENSSL_IS_AWSLC)
  26. #define S2N_CIPHER_AEAD_API_AVAILABLE
  27. #endif
  28. struct s2n_session_key {
  29. EVP_CIPHER_CTX *evp_cipher_ctx;
  30. #if defined(S2N_CIPHER_AEAD_API_AVAILABLE)
  31. EVP_AEAD_CTX *evp_aead_ctx;
  32. #endif
  33. };
  34. struct s2n_stream_cipher {
  35. int (*decrypt)(struct s2n_session_key *key, struct s2n_blob *in, struct s2n_blob *out);
  36. int (*encrypt)(struct s2n_session_key *key, struct s2n_blob *in, struct s2n_blob *out);
  37. };
  38. struct s2n_cbc_cipher {
  39. uint8_t block_size;
  40. uint8_t record_iv_size;
  41. int (*decrypt)(struct s2n_session_key *key, struct s2n_blob *iv, struct s2n_blob *in, struct s2n_blob *out);
  42. int (*encrypt)(struct s2n_session_key *key, struct s2n_blob *iv, struct s2n_blob *in, struct s2n_blob *out);
  43. };
  44. struct s2n_aead_cipher {
  45. uint8_t fixed_iv_size;
  46. uint8_t record_iv_size;
  47. uint8_t tag_size;
  48. int (*decrypt)(struct s2n_session_key *key, struct s2n_blob *iv, struct s2n_blob *add, struct s2n_blob *in, struct s2n_blob *out);
  49. int (*encrypt)(struct s2n_session_key *key, struct s2n_blob *iv, struct s2n_blob *add, struct s2n_blob *in, struct s2n_blob *out);
  50. };
  51. struct s2n_composite_cipher {
  52. uint8_t block_size;
  53. uint8_t record_iv_size;
  54. uint8_t mac_key_size;
  55. int (*decrypt)(struct s2n_session_key *key, struct s2n_blob *iv, struct s2n_blob *in, struct s2n_blob *out);
  56. int (*encrypt)(struct s2n_session_key *key, struct s2n_blob *iv, struct s2n_blob *in, struct s2n_blob *out);
  57. int (*set_mac_write_key)(struct s2n_session_key *key, uint8_t *mac_key, uint32_t mac_size);
  58. int (*initial_hmac)(struct s2n_session_key *key, uint8_t *sequence_number, uint8_t content_type, uint16_t protocol_version,
  59. uint16_t payload_and_eiv_len, int *extra);
  60. };
  61. struct s2n_cipher {
  62. enum {
  63. S2N_STREAM,
  64. S2N_CBC,
  65. S2N_AEAD,
  66. S2N_COMPOSITE
  67. } type;
  68. union {
  69. struct s2n_stream_cipher stream;
  70. struct s2n_aead_cipher aead;
  71. struct s2n_cbc_cipher cbc;
  72. struct s2n_composite_cipher comp;
  73. } io;
  74. uint8_t key_material_size;
  75. uint8_t (*is_available)(void);
  76. int (*init)(struct s2n_session_key *key);
  77. int (*set_decryption_key)(struct s2n_session_key *key, struct s2n_blob *in);
  78. int (*set_encryption_key)(struct s2n_session_key *key, struct s2n_blob *in);
  79. int (*destroy_key)(struct s2n_session_key *key);
  80. S2N_RESULT (*set_ktls_info)(struct s2n_ktls_crypto_info_inputs *inputs,
  81. struct s2n_ktls_crypto_info *crypto_info);
  82. };
  83. int s2n_session_key_alloc(struct s2n_session_key *key);
  84. int s2n_session_key_free(struct s2n_session_key *key);
  85. extern const struct s2n_cipher s2n_null_cipher;
  86. extern const struct s2n_cipher s2n_rc4;
  87. extern const struct s2n_cipher s2n_aes128;
  88. extern const struct s2n_cipher s2n_aes256;
  89. extern const struct s2n_cipher s2n_3des;
  90. extern const struct s2n_cipher s2n_aes128_gcm;
  91. extern const struct s2n_cipher s2n_aes256_gcm;
  92. extern const struct s2n_cipher s2n_aes128_sha;
  93. extern const struct s2n_cipher s2n_aes256_sha;
  94. extern const struct s2n_cipher s2n_aes128_sha256;
  95. extern const struct s2n_cipher s2n_aes256_sha256;
  96. extern const struct s2n_cipher s2n_chacha20_poly1305;
  97. extern const struct s2n_cipher s2n_tls13_aes128_gcm;
  98. extern const struct s2n_cipher s2n_tls13_aes256_gcm;
  99. S2N_RESULT s2n_rc4_init();
  100. S2N_RESULT s2n_rc4_cleanup();