Debian Codegraph @

crt/aws-c-cal/source/ed25519.c

raw symbols SCIP
1 /**
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 * SPDX-License-Identifier: Apache-2.0.
4 */
5
6 #include <aws/cal/ed25519.h>
7 #include <aws/common/ref_count.h>
8
9 struct aws_ed25519_key_pair_impl;
10
11 #ifndef BYO_CRYPTO
12
13 extern struct aws_ed25519_key_pair_impl *aws_ed25519_key_pair_new_generate_impl(struct aws_allocator *allocator);
14
15 extern void aws_ed25519_key_pair_destroy_impl(struct aws_ed25519_key_pair_impl *key_pair_impl);
16
17 extern int aws_ed25519_key_pair_get_public_key_impl(
18 const struct aws_ed25519_key_pair_impl *key_pair,
19 enum aws_ed25519_key_export_format format,
20 struct aws_byte_buf *out);
21
22 extern size_t aws_ed25519_key_pair_get_public_key_size_impl(enum aws_ed25519_key_export_format format);
23
24 extern int aws_ed25519_key_pair_get_private_key_impl(
25 const struct aws_ed25519_key_pair_impl *key_pair,
26 enum aws_ed25519_key_export_format format,
27 struct aws_byte_buf *out);
28
29 extern size_t aws_ed25519_key_pair_get_private_key_size_impl(enum aws_ed25519_key_export_format format);
30
31 #else /* BYO_CRYPTO */
32
33 struct aws_ed25519_key_pair_impl *aws_ed25519_key_pair_new_generate_impl(struct aws_allocator *allocator) {
34 (void)allocator;
35 abort();
36 }
37
38 void aws_ed25519_key_pair_destroy_impl(struct aws_ed25519_key_pair_impl *key_pair_impl) {
39 (void)key_pair_impl;
40 abort();
41 }
42
43 int aws_ed25519_key_pair_get_public_key_impl(
44 const struct aws_ed25519_key_pair_impl *key_pair,
45 enum aws_ed25519_key_export_format format,
46 struct aws_byte_buf *out) {
47 (void)key_pair;
48 (void)format;
49 (void)out;
50 abort();
51 }
52
53 size_t aws_ed25519_key_pair_get_public_key_size_impl(enum aws_ed25519_key_export_format format) {
54 (void)format;
55 abort();
56 }
57
58 int aws_ed25519_key_pair_get_private_key_impl(
59 const struct aws_ed25519_key_pair_impl *key_pair,
60 enum aws_ed25519_key_export_format format,
61 struct aws_byte_buf *out) {
62 (void)key_pair;
63 (void)format;
64 (void)out;
65 abort();
66 }
67
68 size_t aws_ed25519_key_pair_get_private_key_size_impl(enum aws_ed25519_key_export_format format) {
69 (void)format;
70 abort();
71 }
72
73 #endif /* BYO_CRYPTO */
74
75 struct aws_ed25519_key_pair {
76 struct aws_allocator *allocator;
77 struct aws_ref_count ref_count;
78
79 struct aws_ed25519_key_pair_impl *key;
80 };
81
82 static void s_ed25519_destroy_key(void *key_pair) {
83 if (key_pair == NULL) {
84 return;
85 }
86
87 struct aws_ed25519_key_pair *ed25519_key_pair = (struct aws_ed25519_key_pair *)(key_pair);
88
89 if (ed25519_key_pair->key != NULL) {
90 aws_ed25519_key_pair_destroy_impl(ed25519_key_pair->key);
91 }
92
93 aws_mem_release(ed25519_key_pair->allocator, ed25519_key_pair);
94 }
95
96 struct aws_ed25519_key_pair *aws_ed25519_key_pair_new_generate(struct aws_allocator *allocator) {
97
98 struct aws_ed25519_key_pair_impl *key_impl = aws_ed25519_key_pair_new_generate_impl(allocator);
99
100 if (key_impl == NULL) {
101 return NULL;
102 }
103
104 struct aws_ed25519_key_pair *key_pair = aws_mem_calloc(allocator, 1, sizeof(struct aws_ed25519_key_pair));
105
106 aws_ref_count_init(&key_pair->ref_count, key_pair, s_ed25519_destroy_key);
107 key_pair->allocator = allocator;
108 key_pair->key = key_impl;
109
110 return key_pair;
111 }
112
113 struct aws_ed25519_key_pair *aws_ed25519_key_pair_acquire(struct aws_ed25519_key_pair *key_pair) {
114 if (key_pair != NULL) {
115 aws_ref_count_acquire(&key_pair->ref_count);
116 }
117 return key_pair;
118 }
119
120 struct aws_ed25519_key_pair *aws_ed25519_key_pair_release(struct aws_ed25519_key_pair *key_pair) {
121 if (key_pair != NULL) {
122 aws_ref_count_release(&key_pair->ref_count);
123 }
124 return NULL;
125 }
126
127 int aws_ed25519_key_pair_get_public_key(
128 const struct aws_ed25519_key_pair *key_pair,
129 enum aws_ed25519_key_export_format format,
130 struct aws_byte_buf *out) {
131 return aws_ed25519_key_pair_get_public_key_impl(key_pair->key, format, out);
132 }
133
134 size_t aws_ed25519_key_pair_get_public_key_size(enum aws_ed25519_key_export_format format) {
135 return aws_ed25519_key_pair_get_public_key_size_impl(format);
136 }
137
138 int aws_ed25519_key_pair_get_private_key(
139 const struct aws_ed25519_key_pair *key_pair,
140 enum aws_ed25519_key_export_format format,
141 struct aws_byte_buf *out) {
142 return aws_ed25519_key_pair_get_private_key_impl(key_pair->key, format, out);
143 }
144
145 size_t aws_ed25519_key_pair_get_private_key_size(enum aws_ed25519_key_export_format format) {
146 return aws_ed25519_key_pair_get_private_key_size_impl(format);
147 }