Debian Codegraph @

src/security.c

raw symbols SCIP
1 // SPDX-License-Identifier: BSD-2-Clausecxx . . $ `<file>/src/security.c`/
2 /*
3 * Copyright (C) 2023 Linutronix GmbH
4 * Author Kurt Kanzenbach <[email protected]>
5 */
6
7 #include <errno.h>
8
9 #include "security.h"cxx . . $ `<file>/src/security.h`/
10
11 /*
12 * Code inspired by https://wiki.openssl.org/index.php/EVP_Authenticated_Encryption_and_Decryption
13 * and https://github.com/openssl/openssl/discussions/22161
14 *
15 * Notes for the algorithms used: The IV size is 12 bytes. The tag len is always 16 bytes. And Input
16 * equals Output length. Furthermore, PROFINET supports Authentification Only. In that case
17 * plaintext respectively ciphertext is NULL.
18 */
19
20 struct security_contextcxx . . $ security_context# *security_initcxx . . $ security_init(5357658aae651c5a).(enum security_algorithmcxx . . $ security_algorithm# algorithm, const unsigned char *key)
21 {
22 struct security_contextcxx . . $ security_context# *context;
23
24 context = calloccxx . . $ calloc(63a7bf2941bfcd7a).(1, sizeof(*context));
25 if (!context)
26 return NULLcxx . . $ `<external>/15298297234472979974/stddef.h:419:9`!;
27
28 context->algorithmcxx . . $ security_context#algorithm. = algorithm;
29
30 context->ciphercxx . . $ security_context#cipher. = EVP_CIPHER_fetchcxx . . $ EVP_CIPHER_fetch(5217d8dd68ffeeb8).(NULLcxx . . $ `<external>/15298297234472979974/stddef.h:419:9`!, security_algorithm_to_stringcxx . . $ security_algorithm_to_string(82541b8b9ae66d0b).(algorithm), NULLcxx . . $ `<external>/15298297234472979974/stddef.h:419:9`!);
31 if (!context->ciphercxx . . $ security_context#cipher.)
32 goto err_cipher;
33
34 context->ctxcxx . . $ security_context#ctx. = EVP_CIPHER_CTX_newcxx . . $ EVP_CIPHER_CTX_new(ff07bd900d21220).();
35 if (!context->ctxcxx . . $ security_context#ctx.)
36 goto err_ctx;
37
38 /* Set key and cipher */
39 if (1 != EVP_EncryptInit_ex2cxx . . $ EVP_EncryptInit_ex2(c20cafd41fcfcbfe).(context->ctxcxx . . $ security_context#ctx., context->ciphercxx . . $ security_context#cipher., key, NULLcxx . . $ `<external>/15298297234472979974/stddef.h:419:9`!, NULLcxx . . $ `<external>/15298297234472979974/stddef.h:419:9`!))
40 goto err_ctx;
41
42 return context;
43
44 err_ctx:
45 EVP_CIPHER_freecxx . . $ EVP_CIPHER_free(62a7980511799b4a).(context->ciphercxx . . $ security_context#cipher.);
46 err_cipher:
47 freecxx . . $ free(5d22bdacc48458e8).(context);
48
49 return NULLcxx . . $ `<external>/15298297234472979974/stddef.h:419:9`!;
50 }
51
52 void security_exitcxx . . $ security_exit(180fdc1b6a791d79).(struct security_contextcxx . . $ security_context# *context)
53 {
54 if (!context)
55 return;
56
57 EVP_CIPHER_freecxx . . $ EVP_CIPHER_free(62a7980511799b4a).(context->ciphercxx . . $ security_context#cipher.);
58 EVP_CIPHER_CTX_freecxx . . $ EVP_CIPHER_CTX_free(2ce94ba550a584ce).(context->ctxcxx . . $ security_context#ctx.);
59 freecxx . . $ free(5d22bdacc48458e8).(context);
60 }
61
62 int security_encryptcxx . . $ security_encrypt(9b35fdd6c8552c6f).(struct security_contextcxx . . $ security_context# *context, const unsigned char *plaintext,
63 size_t plaintext_length, const unsigned char *associated_data,
64 size_t associated_data_length, const unsigned char *iv,
65 unsigned char *ciphertext, unsigned char *tag)
66 {
67 int ret = -EINVALcxx . . $ `<external>/8987895939074593727/errno-base.h:26:9`!, len;
68
69 /* Set IV */
70 if (1 != EVP_EncryptInit_ex2cxx . . $ EVP_EncryptInit_ex2(c20cafd41fcfcbfe).(context->ctxcxx . . $ security_context#ctx., NULLcxx . . $ `<external>/15298297234472979974/stddef.h:419:9`!, NULLcxx . . $ `<external>/15298297234472979974/stddef.h:419:9`!, iv, NULLcxx . . $ `<external>/15298297234472979974/stddef.h:419:9`!))
71 goto out;
72
73 /* Provide associatedData data. */
74 if (1 !=
75 EVP_EncryptUpdatecxx . . $ EVP_EncryptUpdate(e1042dfa3e10b205).(context->ctxcxx . . $ security_context#ctx., NULLcxx . . $ `<external>/15298297234472979974/stddef.h:419:9`!, &len, associated_data, associated_data_length))
76 goto out;
77
78 /* Provide the message to be encrypted, and obtain the encrypted output. */
79 if (plaintext) {
80 if (1 !=
81 EVP_EncryptUpdatecxx . . $ EVP_EncryptUpdate(e1042dfa3e10b205).(context->ctxcxx . . $ security_context#ctx., ciphertext, &len, plaintext, plaintext_length))
82 goto out;
83 }
84
85 if (1 != EVP_EncryptFinal_excxx . . $ EVP_EncryptFinal_ex(3738cdfa157dd9db).(context->ctxcxx . . $ security_context#ctx., plaintext ? ciphertext + len : NULLcxx . . $ `<external>/15298297234472979974/stddef.h:419:9`!, &len))
86 goto out;
87
88 if (1 != EVP_CIPHER_CTX_ctrlcxx . . $ EVP_CIPHER_CTX_ctrl(ca35871cd3111608).(context->ctxcxx . . $ security_context#ctx., EVP_CTRL_GCM_GET_TAGcxx . . $ `<external>/2497497914425334111/evp.h:393:9`!, 16, tag))
89 goto out;
90
91 ret = 0;
92
93 out:
94 return ret;
95 }
96
97 int security_decryptcxx . . $ security_decrypt(7be7b758f68f59b2).(struct security_contextcxx . . $ security_context# *context, const unsigned char *ciphertext,
98 size_t ciphertext_length, const unsigned char *associated_data,
99 size_t associated_data_length, unsigned char *tag, const unsigned char *iv,
100 unsigned char *plaintext)
101 {
102 int ret = -EINVALcxx . . $ `<external>/8987895939074593727/errno-base.h:26:9`!, len;
103
104 /* Set IV */
105 if (1 != EVP_DecryptInit_ex2cxx . . $ EVP_DecryptInit_ex2(c20cafd41fcfcbfe).(context->ctxcxx . . $ security_context#ctx., NULLcxx . . $ `<external>/15298297234472979974/stddef.h:419:9`!, NULLcxx . . $ `<external>/15298297234472979974/stddef.h:419:9`!, iv, NULLcxx . . $ `<external>/15298297234472979974/stddef.h:419:9`!))
106 goto out;
107
108 /* Provide associatedData data. */
109 if (!EVP_DecryptUpdatecxx . . $ EVP_DecryptUpdate(e1042dfa3e10b205).(context->ctxcxx . . $ security_context#ctx., NULLcxx . . $ `<external>/15298297234472979974/stddef.h:419:9`!, &len, associated_data, associated_data_length))
110 goto out;
111
112 /* Provide the message to be decrypted, and obtain the plaintext output. */
113 if (ciphertext) {
114 if (!EVP_DecryptUpdatecxx . . $ EVP_DecryptUpdate(e1042dfa3e10b205).(context->ctxcxx . . $ security_context#ctx., plaintext, &len, ciphertext,
115 ciphertext_length))
116 goto out;
117 }
118
119 if (!EVP_CIPHER_CTX_ctrlcxx . . $ EVP_CIPHER_CTX_ctrl(ca35871cd3111608).(context->ctxcxx . . $ security_context#ctx., EVP_CTRL_GCM_SET_TAGcxx . . $ `<external>/2497497914425334111/evp.h:394:9`!, 16, tag))
120 goto out;
121
122 ret = EVP_DecryptFinal_excxx . . $ EVP_DecryptFinal_ex(3738cdfa157dd9db).(context->ctxcxx . . $ security_context#ctx., ciphertext ? plaintext + len : NULLcxx . . $ `<external>/15298297234472979974/stddef.h:419:9`!, &len);
123
124 ret = ret > 0 ? 0 : -EPERMcxx . . $ `<external>/8987895939074593727/errno-base.h:5:9`!;
125
126 out:
127 return ret;
128 }