Debian Codegraph @

src/security.h

raw symbols SCIP
1 /* SPDX-License-Identifier: BSD-2-Clause */
2 /*
3 * Copyright (C) 2023 Linutronix GmbH
4 * Author Kurt Kanzenbach <[email protected]>
5 */
6
7 #ifndef _SECURITY_H_
8 #define _SECURITY_H_
9
10 #include <openssl/conf.h>
11 #include <openssl/err.h>
12 #include <openssl/evp.h>
13
14 #include <stddef.h>
15 #include <stdint.h>
16
17 enum security_mode {
18 SECURITY_MODE_NONE, /* No authentification or enryption */
19 SECURITY_MODE_AO, /* Authentification only */
20 SECURITY_MODE_AE, /* Authentification and encryption */
21 };
22
23 enum security_algorithm {
24 SECURITY_ALGORITHM_AES256_GCM,
25 SECURITY_ALGORITHM_AES128_GCM,
26 SECURITY_ALGORITHM_CHACHA20_POLY1305,
27 };
28
29 static inline const char *security_mode_to_string(enum security_mode mode)
30 {
31 switch (mode) {
32 case SECURITY_MODE_NONE:
33 return "None";
34 case SECURITY_MODE_AO:
35 return "Authentication";
36 case SECURITY_MODE_AE:
37 return "Authentication and Encryption";
38 default:
39 return "Unknown";
40 }
41 }
42
43 static inline const char *security_algorithm_to_string(enum security_algorithm algorithm)
44 {
45 switch (algorithm) {
46 case SECURITY_ALGORITHM_AES256_GCM:
47 return "AES-256-GCM";
48 case SECURITY_ALGORITHM_AES128_GCM:
49 return "AES-128-GCM";
50 case SECURITY_ALGORITHM_CHACHA20_POLY1305:
51 return "CHACHA20-POLY1305";
52 default:
53 return "Unknown";
54 }
55 }
56
57 #define SECURITY_IV_LEN 12
58 #define SECURITY_IV_PREFIX_LEN 6
59 #define SECURITY_IV_COUNTER_LEN 6
60
61 struct security_iv {
62 unsigned char iv_prefix[SECURITY_IV_PREFIX_LEN];
63 uint64_t counter;
64 } __attribute__((packed));
65
66 struct security_context {
67 enum security_algorithm algorithm;
68 EVP_CIPHER_CTX *ctx;
69 EVP_CIPHER *cipher;
70 };
71
72 struct security_context *security_init(enum security_algorithm algorithm, const unsigned char *key);
73 void security_exit(struct security_context *context);
74
75 int security_encrypt(struct security_context *context, const unsigned char *plaintext,
76 size_t plaintext_length, const unsigned char *associated_data,
77 size_t associated_data_length, const unsigned char *iv,
78 unsigned char *ciphertext, unsigned char *tag);
79
80 int security_decrypt(struct security_context *context, const unsigned char *ciphertext,
81 size_t ciphertext_length, const unsigned char *associated_data,
82 size_t associated_data_length, unsigned char *tag, const unsigned char *iv,
83 unsigned char *plaintext);
84
85 #endif /* _SECURITY_H_ */