s2n_pkey.h 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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/evp.h>
  17. #include "crypto/s2n_ecdsa.h"
  18. #include "crypto/s2n_hash.h"
  19. #include "crypto/s2n_rsa.h"
  20. #include "crypto/s2n_signature.h"
  21. #include "utils/s2n_blob.h"
  22. #include "utils/s2n_result.h"
  23. /* Public/Private Key Type */
  24. typedef enum {
  25. S2N_PKEY_TYPE_UNKNOWN = -1,
  26. S2N_PKEY_TYPE_RSA = 0,
  27. S2N_PKEY_TYPE_ECDSA,
  28. S2N_PKEY_TYPE_RSA_PSS,
  29. S2N_PKEY_TYPE_SENTINEL
  30. } s2n_pkey_type;
  31. /* Structure that models a public or private key and type-specific operations */
  32. struct s2n_pkey {
  33. /* Legacy OpenSSL APIs operate on specific keys, but the more recent
  34. * APIs all operate on EVP_PKEY. Let's store both for backwards compatibility. */
  35. union {
  36. struct s2n_rsa_key rsa_key;
  37. struct s2n_ecdsa_key ecdsa_key;
  38. } key;
  39. EVP_PKEY *pkey;
  40. S2N_RESULT (*size)(const struct s2n_pkey *key, uint32_t *size_out);
  41. int (*sign)(const struct s2n_pkey *priv_key, s2n_signature_algorithm sig_alg,
  42. struct s2n_hash_state *digest, struct s2n_blob *signature);
  43. int (*verify)(const struct s2n_pkey *pub_key, s2n_signature_algorithm sig_alg,
  44. struct s2n_hash_state *digest, struct s2n_blob *signature);
  45. int (*encrypt)(const struct s2n_pkey *key, struct s2n_blob *in, struct s2n_blob *out);
  46. int (*decrypt)(const struct s2n_pkey *key, struct s2n_blob *in, struct s2n_blob *out);
  47. int (*match)(const struct s2n_pkey *pub_key, const struct s2n_pkey *priv_key);
  48. int (*free)(struct s2n_pkey *key);
  49. int (*check_key)(const struct s2n_pkey *key);
  50. };
  51. int s2n_pkey_zero_init(struct s2n_pkey *pkey);
  52. int s2n_pkey_setup_for_type(struct s2n_pkey *pkey, s2n_pkey_type pkey_type);
  53. int s2n_pkey_check_key_exists(const struct s2n_pkey *pkey);
  54. S2N_RESULT s2n_pkey_size(const struct s2n_pkey *pkey, uint32_t *size_out);
  55. int s2n_pkey_sign(const struct s2n_pkey *pkey, s2n_signature_algorithm sig_alg,
  56. struct s2n_hash_state *digest, struct s2n_blob *signature);
  57. int s2n_pkey_verify(const struct s2n_pkey *pkey, s2n_signature_algorithm sig_alg,
  58. struct s2n_hash_state *digest, struct s2n_blob *signature);
  59. int s2n_pkey_encrypt(const struct s2n_pkey *pkey, struct s2n_blob *in, struct s2n_blob *out);
  60. int s2n_pkey_decrypt(const struct s2n_pkey *pkey, struct s2n_blob *in, struct s2n_blob *out);
  61. int s2n_pkey_match(const struct s2n_pkey *pub_key, const struct s2n_pkey *priv_key);
  62. int s2n_pkey_free(struct s2n_pkey *pkey);
  63. int s2n_asn1der_to_private_key(struct s2n_pkey *priv_key, struct s2n_blob *asn1der, int type_hint);
  64. int s2n_asn1der_to_public_key_and_type(struct s2n_pkey *pub_key, s2n_pkey_type *pkey_type, struct s2n_blob *asn1der);