Debian Codegraph @

crt/aws-c-cal/include/aws/cal/rsa.h

raw symbols SCIP
1 #ifndef AWS_CAL_RSA_H
2 #define AWS_CAL_RSA_H
3 /**
4 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
5 * SPDX-License-Identifier: Apache-2.0.
6 */
7 #include <aws/cal/cal.h>
8 #include <aws/common/byte_buf.h>
9
10 AWS_PUSH_SANE_WARNING_LEVEL
11
12 struct aws_rsa_key_pair;
13
14 enum aws_rsa_encryption_algorithm {
15 AWS_CAL_RSA_ENCRYPTION_PKCS1_5,
16 AWS_CAL_RSA_ENCRYPTION_OAEP_SHA256,
17 AWS_CAL_RSA_ENCRYPTION_OAEP_SHA512,
18 };
19
20 enum aws_rsa_signature_algorithm {
21 AWS_CAL_RSA_SIGNATURE_PKCS1_5_SHA256,
22 AWS_CAL_RSA_SIGNATURE_PKCS1_5_SHA1,
23 AWS_CAL_RSA_SIGNATURE_PSS_SHA256,
24 };
25
26 /*
27 * Note: prefer using standard key sizes - 1024, 2048, 4096.
28 * Other key sizes will work, but which key sizes are supported may vary by
29 * platform. Typically, multiples of 64 should work on all platforms.
30 */
31 enum {
32 AWS_CAL_RSA_MIN_SUPPORTED_KEY_SIZE_IN_BITS = 1024,
33 AWS_CAL_RSA_MAX_SUPPORTED_KEY_SIZE_IN_BITS = 4096,
34 };
35
36 AWS_EXTERN_C_BEGIN
37
38 /**
39 * Creates an RSA public key from RSAPublicKey as defined in rfc 8017 (aka PKCS1).
40 * Returns a new instance of aws_rsa_key_pair if the key was successfully built.
41 * Otherwise returns NULL.
42 */
43 AWS_CAL_API struct aws_rsa_key_pair *aws_rsa_key_pair_new_from_public_key_pkcs1(
44 struct aws_allocator *allocator,
45 struct aws_byte_cursor key);
46
47 /**
48 * Creates an RSA private key from RSAPrivateKey as defined in rfc 8017 (aka PKCS1).
49 * Returns a new instance of aws_rsa_key_pair if the key was successfully built.
50 * Otherwise returns NULL.
51 */
52 AWS_CAL_API struct aws_rsa_key_pair *aws_rsa_key_pair_new_from_private_key_pkcs1(
53 struct aws_allocator *allocator,
54 struct aws_byte_cursor key);
55
56 /**
57 * Creates an RSA private key from PrivateKeyInfo as defined in rfc 5208 (aka PKCS8).
58 * Returns a new instance of aws_rsa_key_pair if the key was successfully built.
59 * Otherwise returns NULL.
60 */
61 AWS_CAL_API struct aws_rsa_key_pair *aws_rsa_key_pair_new_from_private_key_pkcs8(
62 struct aws_allocator *allocator,
63 struct aws_byte_cursor key);
64
65 /**
66 * Adds one to an RSA key pair's ref count.
67 * Returns key_pair pointer.
68 */
69 AWS_CAL_API struct aws_rsa_key_pair *aws_rsa_key_pair_acquire(struct aws_rsa_key_pair *key_pair);
70
71 /**
72 * Subtracts one from an RSA key pair's ref count. If ref count reaches zero, the key pair is destroyed.
73 * Always returns NULL.
74 */
75 AWS_CAL_API struct aws_rsa_key_pair *aws_rsa_key_pair_release(struct aws_rsa_key_pair *key_pair);
76
77 /**
78 * Max plaintext size that can be encrypted by the key (i.e. max data size
79 * supported by the key - bytes needed for padding).
80 */
81 AWS_CAL_API size_t aws_rsa_key_pair_max_encrypt_plaintext_size(
82 const struct aws_rsa_key_pair *key_pair,
83 enum aws_rsa_encryption_algorithm algorithm);
84
85 /*
86 * Uses the key_pair's private key to encrypt the plaintext. The output will be
87 * in out. out must be large enough to to hold the ciphertext. Check
88 * aws_rsa_key_pair_block_length() for output upper bound.
89 */
90 AWS_CAL_API int aws_rsa_key_pair_encrypt(
91 const struct aws_rsa_key_pair *key_pair,
92 enum aws_rsa_encryption_algorithm algorithm,
93 struct aws_byte_cursor plaintext,
94 struct aws_byte_buf *out);
95
96 /*
97 * Uses the key_pair's private key to decrypt the ciphertext. The output will be
98 * in out. out must be large enough to to hold the ciphertext. Check
99 * aws_rsa_key_pair_block_length() for output upper bound.
100 */
101 AWS_CAL_API int aws_rsa_key_pair_decrypt(
102 const struct aws_rsa_key_pair *key_pair,
103 enum aws_rsa_encryption_algorithm algorithm,
104 struct aws_byte_cursor ciphertext,
105 struct aws_byte_buf *out);
106
107 /*
108 * Max size for a block supported by a given key pair.
109 */
110 AWS_CAL_API size_t aws_rsa_key_pair_block_length(const struct aws_rsa_key_pair *key_pair);
111
112 /**
113 * Uses the key_pair's private key to sign message. The output will be in out. out must be large enough
114 * to hold the signature. Check aws_rsa_key_pair_signature_length() for the appropriate size.
115 *
116 * It is the callers job to make sure message is the appropriate cryptographic digest for this operation. It's usually
117 * something like a SHA256.
118 */
119 AWS_CAL_API int aws_rsa_key_pair_sign_message(
120 const struct aws_rsa_key_pair *key_pair,
121 enum aws_rsa_signature_algorithm algorithm,
122 struct aws_byte_cursor digest,
123 struct aws_byte_buf *out);
124
125 /**
126 * Uses the key_pair's public key to verify signature of message.
127 *
128 * It is the callers job to make sure message is the appropriate cryptographic digest for this operation. It's usually
129 * something like a SHA256.
130 *
131 * returns AWS_OP_SUCCESS if the signature is valid.
132 * raises AWS_ERROR_CAL_SIGNATURE_VALIDATION_FAILED if signature validation failed
133 */
134 AWS_CAL_API int aws_rsa_key_pair_verify_signature(
135 const struct aws_rsa_key_pair *key_pair,
136 enum aws_rsa_signature_algorithm algorithm,
137 struct aws_byte_cursor digest,
138 struct aws_byte_cursor signature);
139
140 /*
141 * Max size for a signature supported by a given key pair.
142 */
143 AWS_CAL_API size_t aws_rsa_key_pair_signature_length(const struct aws_rsa_key_pair *key_pair);
144
145 enum aws_rsa_key_export_format {
146 AWS_CAL_RSA_KEY_EXPORT_PKCS1,
147 };
148
149 /*
150 * Get public key for the key pair.
151 * Inits out to a copy of key.
152 * Any encoding on top of that (ex. b64) is left up to user.
153 * Note: this function is currently not supported on windows for generated keys.
154 */
155 AWS_CAL_API int aws_rsa_key_pair_get_public_key(
156 const struct aws_rsa_key_pair *key_pair,
157 enum aws_rsa_key_export_format format,
158 struct aws_byte_buf *out);
159
160 /*
161 * Get private key for the key pair.
162 * Inits out to a copy of key.
163 * Any encoding on top of that (ex. b64) is left up to user.
164 * Note: this function is currently not supported on Windows for generated keys.
165 */
166 AWS_CAL_API int aws_rsa_key_pair_get_private_key(
167 const struct aws_rsa_key_pair *key_pair,
168 enum aws_rsa_key_export_format format,
169 struct aws_byte_buf *out);
170
171 AWS_EXTERN_C_END
172
173 AWS_POP_SANE_WARNING_LEVEL
174
175 #endif /* AWS_CAL_RSA_H */