Debian Codegraph @

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

raw symbols SCIP
1 #ifndef AWS_C_CAL_PRIVATE_RSA_H
2 #define AWS_C_CAL_PRIVATE_RSA_H
3 /**
4 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
5 * SPDX-License-Identifier: Apache-2.0.
6 */
7
8 #include <aws/cal/rsa.h>
9
10 #include <aws/common/byte_buf.h>
11 #include <aws/common/ref_count.h>
12
13 struct aws_rsa_key_pair;
14 struct aws_der_decoder;
15
16 struct aws_rsa_key_vtable {
17 int (*encrypt)(
18 const struct aws_rsa_key_pair *key_pair,
19 enum aws_rsa_encryption_algorithm algorithm,
20 struct aws_byte_cursor plaintext,
21 struct aws_byte_buf *out);
22 int (*decrypt)(
23 const struct aws_rsa_key_pair *key_pair,
24 enum aws_rsa_encryption_algorithm algorithm,
25 struct aws_byte_cursor ciphertext,
26 struct aws_byte_buf *out);
27
28 int (*sign)(
29 const struct aws_rsa_key_pair *key_pair,
30 enum aws_rsa_signature_algorithm algorithm,
31 struct aws_byte_cursor digest,
32 struct aws_byte_buf *out);
33
34 int (*verify)(
35 const struct aws_rsa_key_pair *key_pair,
36 enum aws_rsa_signature_algorithm algorithm,
37 struct aws_byte_cursor digest,
38 struct aws_byte_cursor signature);
39 };
40
41 struct aws_rsa_key_pair {
42 struct aws_allocator *allocator;
43 struct aws_rsa_key_vtable *vtable;
44 struct aws_ref_count ref_count;
45
46 size_t key_size_in_bits;
47 struct aws_byte_buf priv;
48 struct aws_byte_buf pub;
49
50 void *impl;
51 };
52
53 void aws_rsa_key_pair_base_clean_up(struct aws_rsa_key_pair *key_pair);
54
55 /*
56 * RSAPrivateKey as defined in RFC 8017 (aka PKCS1 format):
57 * version Version,
58 * modulus INTEGER, -- n
59 * publicExponent INTEGER, -- e
60 * privateExponent INTEGER, -- d
61 * prime1 INTEGER, -- p
62 * prime2 INTEGER, -- q
63 * exponent1 INTEGER, -- d mod (p-1)
64 * exponent2 INTEGER, -- d mod (q-1)
65 * coefficient INTEGER, -- (inverse of q) mod p
66 * otherPrimeInfos OtherPrimeInfos OPTIONAL
67 * Note: otherPrimeInfos is used for >2 primes RSA cases, which are not very
68 * common and currently not supported by CRT. Version == 0 indicates 2 prime
69 * case and version == 1 indicates >2 prime case, hence in practice it will
70 * always be 0.
71 */
72 struct aws_rsa_private_key_pkcs1 {
73 /*
74 * Note: all cursors here point to bignum data for underlying RSA numbers.
75 * Struct itself does not own the data and points to where ever the data was
76 * decoded from.
77 */
78 int version;
79 struct aws_byte_cursor modulus;
80 struct aws_byte_cursor publicExponent;
81 struct aws_byte_cursor privateExponent;
82 struct aws_byte_cursor prime1;
83 struct aws_byte_cursor prime2;
84 struct aws_byte_cursor exponent1;
85 struct aws_byte_cursor exponent2;
86 struct aws_byte_cursor coefficient;
87 };
88
89 AWS_CAL_API int aws_der_decoder_load_private_rsa_pkcs1(
90 struct aws_der_decoder *decoder,
91 struct aws_rsa_private_key_pkcs1 *out);
92
93 /*
94 * RSAPublicKey as defined in RFC 8017 (aka PKCS1 format):
95 modulus INTEGER, -- n
96 publicExponent INTEGER -- e
97 */
98 struct aws_rsa_public_key_pkcs1 {
99 /*
100 * Note: all cursors here point to bignum data for underlying RSA numbers.
101 * Struct itself does not own the data and points to where ever the data was
102 * decoded from.
103 */
104 struct aws_byte_cursor modulus;
105 struct aws_byte_cursor publicExponent;
106 };
107
108 AWS_CAL_API int aws_der_decoder_load_public_rsa_pkcs1(
109 struct aws_der_decoder *decoder,
110 struct aws_rsa_public_key_pkcs1 *out);
111
112 /*
113 * Returns AWS_OP_SUCCESS if key size is supported and raises
114 * AWS_ERROR_INVALID_ARGUMENT otherwise.
115 */
116 int is_valid_rsa_key_size(size_t key_size_in_bits);
117
118 #endif /* AWS_C_CAL_PRIVATE_RSA_H */