Debian Codegraph @

crt/aws-c-cal/source/symmetric_cipher.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 #include <aws/cal/private/symmetric_cipher_priv.h>
6 #include <aws/cal/symmetric_cipher.h>
7 #include <aws/common/device_random.h>
8
9 #ifndef BYO_CRYPTO
10
11 extern struct aws_symmetric_cipher *aws_aes_cbc_256_new_impl(
12 struct aws_allocator *allocator,
13 const struct aws_byte_cursor *key,
14 const struct aws_byte_cursor *iv);
15
16 extern struct aws_symmetric_cipher *aws_aes_ctr_256_new_impl(
17 struct aws_allocator *allocator,
18 const struct aws_byte_cursor *key,
19 const struct aws_byte_cursor *iv);
20
21 extern struct aws_symmetric_cipher *aws_aes_gcm_256_new_impl(
22 struct aws_allocator *allocator,
23 const struct aws_byte_cursor *key,
24 const struct aws_byte_cursor *iv,
25 const struct aws_byte_cursor *aad);
26
27 extern struct aws_symmetric_cipher *aws_aes_keywrap_256_new_impl(
28 struct aws_allocator *allocator,
29 const struct aws_byte_cursor *key);
30
31 #else /* BYO_CRYPTO */
32 struct aws_symmetric_cipher *aws_aes_cbc_256_new_impl(
33 struct aws_allocator *allocator,
34 const struct aws_byte_cursor *key,
35 const struct aws_byte_cursor *iv) {
36 (void)allocator;
37 (void)key;
38 (void)iv;
39 abort();
40 }
41
42 struct aws_symmetric_cipher *aws_aes_ctr_256_new_impl(
43 struct aws_allocator *allocator,
44 const struct aws_byte_cursor *key,
45 const struct aws_byte_cursor *iv) {
46 (void)allocator;
47 (void)key;
48 (void)iv;
49 abort();
50 }
51
52 struct aws_symmetric_cipher *aws_aes_gcm_256_new_impl(
53 struct aws_allocator *allocator,
54 const struct aws_byte_cursor *key,
55 const struct aws_byte_cursor *iv,
56 const struct aws_byte_cursor *aad) {
57 (void)allocator;
58 (void)key;
59 (void)iv;
60 (void)aad;
61 abort();
62 }
63
64 struct aws_symmetric_cipher *aws_aes_keywrap_256_new_impl(
65 struct aws_allocator *allocator,
66 const struct aws_byte_cursor *key) {
67 (void)allocator;
68 (void)key;
69 abort();
70 }
71
72 #endif /* BYO_CRYPTO */
73
74 static aws_aes_cbc_256_new_fn *s_aes_cbc_new_fn = aws_aes_cbc_256_new_impl;
75 static aws_aes_ctr_256_new_fn *s_aes_ctr_new_fn = aws_aes_ctr_256_new_impl;
76 static aws_aes_gcm_256_new_fn *s_aes_gcm_new_fn = aws_aes_gcm_256_new_impl;
77 static aws_aes_keywrap_256_new_fn *s_aes_keywrap_new_fn = aws_aes_keywrap_256_new_impl;
78
79 static int s_check_input_size_limits(const struct aws_symmetric_cipher *cipher, const struct aws_byte_cursor *input) {
80 /* libcrypto uses int, not size_t, so this is the limit.
81 * For simplicity, enforce the same rules on all platforms. */
82 return input->len <= INT_MAX - cipher->block_size ? AWS_OP_SUCCESS
83 : aws_raise_error(AWS_ERROR_CAL_BUFFER_TOO_LARGE_FOR_ALGORITHM);
84 }
85
86 static int s_validate_key_materials(
87 const struct aws_byte_cursor *key,
88 size_t expected_key_size,
89 const struct aws_byte_cursor *iv,
90 size_t expected_iv_size) {
91 if (key && key->len != expected_key_size) {
92 return aws_raise_error(AWS_ERROR_CAL_INVALID_KEY_LENGTH_FOR_ALGORITHM);
93 }
94
95 if (iv && iv->len != expected_iv_size) {
96 return aws_raise_error(AWS_ERROR_CAL_INVALID_CIPHER_MATERIAL_SIZE_FOR_ALGORITHM);
97 }
98
99 return AWS_OP_SUCCESS;
100 }
101
102 struct aws_symmetric_cipher *aws_aes_cbc_256_new(
103 struct aws_allocator *allocator,
104 const struct aws_byte_cursor *key,
105 const struct aws_byte_cursor *iv) {
106
107 if (s_validate_key_materials(key, AWS_AES_256_KEY_BYTE_LEN, iv, AWS_AES_256_CIPHER_BLOCK_SIZE) != AWS_OP_SUCCESS) {
108 return NULL;
109 }
110 return s_aes_cbc_new_fn(allocator, key, iv);
111 }
112
113 struct aws_symmetric_cipher *aws_aes_ctr_256_new(
114 struct aws_allocator *allocator,
115 const struct aws_byte_cursor *key,
116 const struct aws_byte_cursor *iv) {
117 if (s_validate_key_materials(key, AWS_AES_256_KEY_BYTE_LEN, iv, AWS_AES_256_CIPHER_BLOCK_SIZE) != AWS_OP_SUCCESS) {
118 return NULL;
119 }
120 return s_aes_ctr_new_fn(allocator, key, iv);
121 }
122
123 struct aws_symmetric_cipher *aws_aes_gcm_256_new(
124 struct aws_allocator *allocator,
125 const struct aws_byte_cursor *key,
126 const struct aws_byte_cursor *iv,
127 const struct aws_byte_cursor *aad) {
128 if (s_validate_key_materials(key, AWS_AES_256_KEY_BYTE_LEN, iv, AWS_AES_256_CIPHER_BLOCK_SIZE - sizeof(uint32_t)) !=
129 AWS_OP_SUCCESS) {
130 return NULL;
131 }
132 return s_aes_gcm_new_fn(allocator, key, iv, aad);
133 }
134
135 struct aws_symmetric_cipher *aws_aes_keywrap_256_new(
136 struct aws_allocator *allocator,
137 const struct aws_byte_cursor *key) {
138 if (s_validate_key_materials(key, AWS_AES_256_KEY_BYTE_LEN, NULL, 0) != AWS_OP_SUCCESS) {
139 return NULL;
140 }
141 return s_aes_keywrap_new_fn(allocator, key);
142 }
143
144 void aws_symmetric_cipher_destroy(struct aws_symmetric_cipher *cipher) {
145 if (cipher) {
146 cipher->vtable->destroy(cipher);
147 }
148 }
149
150 int aws_symmetric_cipher_encrypt(
151 struct aws_symmetric_cipher *cipher,
152 struct aws_byte_cursor to_encrypt,
153 struct aws_byte_buf *out) {
154
155 AWS_PRECONDITION(aws_byte_cursor_is_valid(&to_encrypt));
156
157 if (AWS_UNLIKELY(s_check_input_size_limits(cipher, &to_encrypt) != AWS_OP_SUCCESS)) {
158 return AWS_OP_ERR;
159 }
160
161 if (cipher->state == AWS_SYMMETRIC_CIPHER_READY) {
162 return cipher->vtable->encrypt(cipher, to_encrypt, out);
163 }
164
165 return aws_raise_error(AWS_ERROR_INVALID_STATE);
166 }
167
168 int aws_symmetric_cipher_decrypt(
169 struct aws_symmetric_cipher *cipher,
170 struct aws_byte_cursor to_decrypt,
171 struct aws_byte_buf *out) {
172
173 AWS_PRECONDITION(aws_byte_cursor_is_valid(&to_decrypt));
174
175 if (AWS_UNLIKELY(s_check_input_size_limits(cipher, &to_decrypt) != AWS_OP_SUCCESS)) {
176 return AWS_OP_ERR;
177 }
178
179 if (cipher->state == AWS_SYMMETRIC_CIPHER_READY) {
180 return cipher->vtable->decrypt(cipher, to_decrypt, out);
181 }
182
183 return aws_raise_error(AWS_ERROR_INVALID_STATE);
184 }
185
186 int aws_symmetric_cipher_finalize_encryption(struct aws_symmetric_cipher *cipher, struct aws_byte_buf *out) {
187 if (cipher->state == AWS_SYMMETRIC_CIPHER_READY) {
188 int ret_val = cipher->vtable->finalize_encryption(cipher, out);
189 if (cipher->state != AWS_SYMMETRIC_CIPHER_ERROR) {
190 cipher->state = AWS_SYMMETRIC_CIPHER_FINALIZED;
191 }
192 return ret_val;
193 }
194
195 return aws_raise_error(AWS_ERROR_INVALID_STATE);
196 }
197
198 int aws_symmetric_cipher_finalize_decryption(struct aws_symmetric_cipher *cipher, struct aws_byte_buf *out) {
199 if (cipher->state == AWS_SYMMETRIC_CIPHER_READY) {
200 int ret_val = cipher->vtable->finalize_decryption(cipher, out);
201 if (cipher->state != AWS_SYMMETRIC_CIPHER_ERROR) {
202 cipher->state = AWS_SYMMETRIC_CIPHER_FINALIZED;
203 }
204 return ret_val;
205 }
206 return aws_raise_error(AWS_ERROR_INVALID_STATE);
207 }
208
209 int aws_symmetric_cipher_reset(struct aws_symmetric_cipher *cipher) {
210 int ret_val = cipher->vtable->reset(cipher);
211 if (ret_val == AWS_OP_SUCCESS) {
212 cipher->state = AWS_SYMMETRIC_CIPHER_READY;
213 }
214
215 return ret_val;
216 }
217
218 struct aws_byte_cursor aws_symmetric_cipher_get_tag(const struct aws_symmetric_cipher *cipher) {
219 return aws_byte_cursor_from_buf(&cipher->tag);
220 }
221
222 void aws_symmetric_cipher_set_tag(struct aws_symmetric_cipher *cipher, struct aws_byte_cursor tag) {
223 AWS_PRECONDITION(aws_byte_cursor_is_valid(&tag));
224 aws_byte_buf_clean_up_secure(&cipher->tag);
225 aws_byte_buf_init_copy_from_cursor(&cipher->tag, cipher->allocator, tag);
226 }
227
228 struct aws_byte_cursor aws_symmetric_cipher_get_initialization_vector(const struct aws_symmetric_cipher *cipher) {
229 return aws_byte_cursor_from_buf(&cipher->iv);
230 }
231
232 struct aws_byte_cursor aws_symmetric_cipher_get_key(const struct aws_symmetric_cipher *cipher) {
233 return aws_byte_cursor_from_buf(&cipher->key);
234 }
235
236 bool aws_symmetric_cipher_is_good(const struct aws_symmetric_cipher *cipher) {
237 return cipher->state == AWS_SYMMETRIC_CIPHER_READY;
238 }
239
240 enum aws_symmetric_cipher_state aws_symmetric_cipher_get_state(const struct aws_symmetric_cipher *cipher) {
241 return cipher->state;
242 }
243
244 void aws_symmetric_cipher_generate_initialization_vector(
245 size_t len_bytes,
246 bool is_counter_mode,
247 struct aws_byte_buf *out) {
248 size_t counter_len = is_counter_mode ? sizeof(uint32_t) : 0;
249 AWS_ASSERT(len_bytes > counter_len);
250 size_t rand_len = len_bytes - counter_len;
251
252 AWS_FATAL_ASSERT(aws_device_random_buffer_append(out, rand_len) == AWS_OP_SUCCESS);
253
254 if (is_counter_mode) {
255 /* put counter at the end, initialized to 1 */
256 aws_byte_buf_write_be32(out, 1);
257 }
258 }
259
260 void aws_symmetric_cipher_generate_key(size_t key_len_bytes, struct aws_byte_buf *out) {
261 AWS_FATAL_ASSERT(aws_device_random_buffer_append(out, key_len_bytes) == AWS_OP_SUCCESS);
262 }
263
264 int aws_symmetric_cipher_try_ensure_sufficient_buffer_space(struct aws_byte_buf *buf, size_t size) {
265 if (buf->capacity - buf->len < size) {
266 return aws_byte_buf_reserve_relative(buf, size);
267 }
268
269 return AWS_OP_SUCCESS;
270 }