1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
/*
* Copyright 2019-2023 Comcast Cable Communications Management, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
*/
/** @section Description
* @file symmetric.h
*
* This file contains the functions and structures implementing symmetric key and cipher operations.
*/
#ifndef SYMMETRIC_H
#define SYMMETRIC_H
#include "stored_key.h"
#ifdef __cplusplus
#include <cstdbool>
#include <cstddef>
#include <cstdint>
extern "C" {
#else
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#endif
typedef struct symmetric_context_s symmetric_context_t;
/**
* Generate a symmetric key.
* @param[out] stored_key_generated the generated AES key.
* @param[in] rights the key rights.
* @param[in] parameters the AES key parameters
* @return the status of the operation.
*/
sa_status symmetric_generate_key(
stored_key_t** stored_key_generated,
const sa_rights* rights,
sa_generate_parameters_symmetric* parameters);
/**
* Verifies that the symmetric cipher operation can be performed.
*
* @param cipher_algorithm the cipher algorithm.
* @param cipher_mode the cipher mode.
* @param stored_key the stored key to use in the cipher.
* @return the status of the cipher operation.
*/
sa_status symmetric_verify_cipher(
sa_cipher_algorithm cipher_algorithm,
sa_cipher_mode cipher_mode,
const stored_key_t* stored_key);
/**
* Create an AES ECB encryption context.
*
* @param[in] stored_key cipher key.
* @param[in] padded true if using PKCS7 padding.
* @return created AES context. NULL if the operation failed.
*/
symmetric_context_t* symmetric_create_aes_ecb_encrypt_context(
const stored_key_t* stored_key,
bool padded);
/**
* Create an AES CBC encryption context.
*
* @param[in] stored_key cipher key.
* @param[in] iv initialization vector.
* @param[in] iv_length initialization vector length. Has to be 16 bytes.
* @param[in] padded true if using PKCS7 padding.
* @return created AES context. NULL if the operation failed.
*/
symmetric_context_t* symmetric_create_aes_cbc_encrypt_context(
const stored_key_t* stored_key,
const void* iv,
size_t iv_length,
bool padded);
/**
* Create an AES CTR encryption context.
*
* @param[in] stored_key cipher key.
* @param[in] counter counter.
* @param[in] counter_length counter length. Has to be 16 bytes.
* @return created AES context. NULL if the operation failed.
*/
symmetric_context_t* symmetric_create_aes_ctr_encrypt_context(
const stored_key_t* stored_key,
const void* counter,
size_t counter_length);
/**
* Create an AES GCM encryption context.
*
* @param[in] stored_key cipher key.
* @param[in] iv initialization vector.
* @param[in] iv_length initialization vector length. Has to be 12 bytes.
* @param[in] aad additional authenticated data.
* @param[in] aad_length additional authenticated data length.
* @return created AES context. NULL if the operation failed.
*/
symmetric_context_t* symmetric_create_aes_gcm_encrypt_context(
const stored_key_t* stored_key,
const void* iv,
size_t iv_length,
const void* aad,
size_t aad_length);
/**
* Create an CHACHA20 encryption context.
*
* @param[in] stored_key cipher key.
* @param[in] nonce nonce.
* @param[in] nonce_length nonce length. Has to be 12 bytes.
* @param[in] counter counter.
* @param[in] counter_length counter length. Has to be 4 bytes.
* @return created CHACHA20 context. NULL if the operation failed.
*/
symmetric_context_t* symmetric_create_chacha20_encrypt_context(
const stored_key_t* stored_key,
const void* nonce,
size_t nonce_length,
const void* counter,
size_t counter_length);
/**
* Create an CHACHA20-POLY1305 encryption context.
*
* @param[in] stored_key cipher key.
* @param[in] nonce nonce.
* @param[in] nonce_length nonce length. Has to be 12 bytes.
* @param[in] aad additional authenticated data.
* @param[in] aad_length additional authenticated data length.
* @return created CHACHA20 context. NULL if the operation failed.
*/
symmetric_context_t* symmetric_create_chacha20_poly1305_encrypt_context(
const stored_key_t* stored_key,
const void* nonce,
size_t nonce_length,
const void* aad,
size_t aad_length);
/**
* Create an AES ECB decrypt context.
*
* @param[in] stored_key cipher key.
* @param[in] padded true if using PKCS7 padding.
* @return created AES context. NULL if the operation failed.
*/
symmetric_context_t* symmetric_create_aes_ecb_decrypt_context(
const stored_key_t* stored_key,
bool padded);
/**
* Create an AES CBC decrypt context.
*
* @param[in] stored_key cipher key.
* @param[in] iv initialization vector.
* @param[in] iv_length initialization vector length. Has to be 16 bytes.
* @param[in] padded true if using PKCS7 padding.
* @return created AES context. NULL if the operation failed.
*/
symmetric_context_t* symmetric_create_aes_cbc_decrypt_context(
const stored_key_t* stored_key,
const void* iv,
size_t iv_length,
bool padded);
/**
* Create an AES CTR decrypt context.
*
* @param[in] stored_key cipher key.
* @param[in] counter counter.
* @param[in] counter_length counter length. Has to be 16 bytes.
* @return created AES context. NULL if the operation failed.
*/
symmetric_context_t* symmetric_create_aes_ctr_decrypt_context(
const stored_key_t* stored_key,
const void* counter,
size_t counter_length);
/**
* Create an AES GCM decrypt context.
*
* @param[in] stored_key cipher key.
* @param[in] iv initialization vector.
* @param[in] iv_length initialization vector length. Has to be 12 bytes.
* @param[in] aad additional authenticated data.
* @param[in] aad_length additional authenticated data length.
* @return created AES context. NULL if the operation failed.
*/
symmetric_context_t* symmetric_create_aes_gcm_decrypt_context(
const stored_key_t* stored_key,
const void* iv,
size_t iv_length,
const void* aad,
size_t aad_length);
/**
* Create an CHACHA20 encryption context.
*
* @param[in] stored_key cipher key.
* @param[in] nonce nonce.
* @param[in] nonce_length nonce length. Has to be 12 bytes.
* @param[in] counter counter.
* @param[in] counter_length counter length. Has to be 4 bytes.
* @return created CHACHA20 context. NULL if the operation failed.
*/
symmetric_context_t* symmetric_create_chacha20_decrypt_context(
const stored_key_t* stored_key,
const void* nonce,
size_t nonce_length,
const void* counter,
size_t counter_length);
/**
* Create an CHACHA20-POLY1305 encryption context.
*
* @param[in] stored_key cipher key.
* @param[in] nonce nonce.
* @param[in] nonce_length nonce length. Has to be 12 bytes.
* @param[in] aad additional authenticated data.
* @param[in] aad_length additional authenticated data length.
* @return created CHACHA20 context. NULL if the operation failed.
*/
symmetric_context_t* symmetric_create_chacha20_poly1305_decrypt_context(
const stored_key_t* stored_key,
const void* nonce,
size_t nonce_length,
const void* aad,
size_t aad_length);
/**
* Encrypt a block of input data.
*
* @param[in] context AES context.
* @param[out] out output buffer.
* @param[in,out] out_length output buffer length. Set to bytes written on return.
* @param[in] in input buffer.
* @param[in] in_length input buffer length. Had to be a multiple of 16.
* @return status of the operation.
*/
sa_status symmetric_context_encrypt(
symmetric_context_t* context,
void* out,
size_t* out_length,
const void* in,
size_t in_length);
/**
* Encrypt last chunk of input data mode. Can only be called on CTR and GCM contexts.
*
* @param[in] context symmetric context.
* @param[out] out output buffer.
* @param[in,out] out_length output buffer length. Set to bytes written on return.
* @param[in] in input buffer.
* @param[in] in_length input buffer length. Had to be less then or equal to 16.
* @return status of the operation.
*/
sa_status symmetric_context_encrypt_last(
symmetric_context_t* context,
void* out,
size_t* out_length,
const void* in,
size_t in_length);
/**
* Decrypt input data.
*
* @param[in] context AES context.
* @param[out] out output buffer.
* @param[in,out] out_length output buffer length. Set to bytes written on return.
* @param[in] in input buffer.
* @param[in] in_length input buffer length. Had to be a multiple of 16.
* @return status of the operation.
*/
sa_status symmetric_context_decrypt(
symmetric_context_t* context,
void* out,
size_t* out_length,
const void* in,
size_t in_length);
/**
* Decrypt last chunk of input data mode. Can only be called on CTR and GCM contexts.
*
* @param[in] context symmetric context.
* @param[out] out output buffer.
* @param[in,out] out_length output buffer length. Set to bytes written on return.
* @param[in] in input buffer.
* @param[in] in_length input buffer length. Had to be less then or equal to 16.
* @return status of the operation.
*/
sa_status symmetric_context_decrypt_last(
symmetric_context_t* context,
void* out,
size_t* out_length,
const void* in,
size_t in_length);
/**
* Set the AES context IV to a new value.
*
* @param[in] context AES context.
* @param[in] iv initialization vector.
* @param[in] iv_length initialization vector length.
* @return status of the operation.
*/
sa_status symmetric_context_set_iv(
const symmetric_context_t* context,
const void* iv,
size_t iv_length);
/**
* Gets the authentication tag after the process_last call. Can only be called on AES GCM & ChaCha20-Poly1305 context.
*
* @param[in] context AES context.
* @param[out] tag authentication tag.
* @param[in] tag_length authentication tag length. Has to be less then or equal to 16.
*/
sa_status symmetric_context_get_tag(
const symmetric_context_t* context,
void* tag,
size_t tag_length);
/**
* Sets the authentication tag before the process_last call. Can only be called on AES GCM & ChaCha20-Poly1305 context.
*
* @param[in] context AES context.
* @param[in] tag authentication tag.
* @param[in] tag_length authentication tag length. Has to be less then or equal to 16.
* @return status of the operation.
*/
sa_status symmetric_context_set_tag(
symmetric_context_t* context,
const void* tag,
size_t tag_length);
/**
* Free the AES context. Operation is a NOOP if context is NULL.
*
* @param[in] context AES context.
*/
void symmetric_context_free(symmetric_context_t* context);
#ifdef __cplusplus
}
#endif
#endif // SYMMETRIC_H