| 1 | // SPDX-License-Identifier: BSD-2-Clause |
| 2 | /* |
| 3 | * Copyright (C) 2023 Linutronix GmbH |
| 4 | * Author Kurt Kanzenbach <[email protected]> |
| 5 | */ |
| 6 | |
| 7 | #include <errno.h> |
| 8 | |
| 9 | #include "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_context *security_init(enum security_algorithm algorithm, const unsigned char *key) |
| 21 | { |
| 22 | struct security_context *context; |
| 23 | |
| 24 | context = calloc(1, sizeof(*context)); |
| 25 | if (!context) |
| 26 | return NULL; |
| 27 | |
| 28 | context->algorithm = algorithm; |
| 29 | |
| 30 | context->cipher = EVP_CIPHER_fetch(NULL, security_algorithm_to_string(algorithm), NULL); |
| 31 | if (!context->cipher) |
| 32 | goto err_cipher; |
| 33 | |
| 34 | context->ctx = EVP_CIPHER_CTX_new(); |
| 35 | if (!context->ctx) |
| 36 | goto err_ctx; |
| 37 | |
| 38 | /* Set key and cipher */ |
| 39 | if (1 != EVP_EncryptInit_ex2(context->ctx, context->cipher, key, NULL, NULL)) |
| 40 | goto err_ctx; |
| 41 | |
| 42 | return context; |
| 43 | |
| 44 | err_ctx: |
| 45 | EVP_CIPHER_free(context->cipher); |
| 46 | err_cipher: |
| 47 | free(context); |
| 48 | |
| 49 | return NULL; |
| 50 | } |
| 51 | |
| 52 | void security_exit(struct security_context *context) |
| 53 | { |
| 54 | if (!context) |
| 55 | return; |
| 56 | |
| 57 | EVP_CIPHER_free(context->cipher); |
| 58 | EVP_CIPHER_CTX_free(context->ctx); |
| 59 | free(context); |
| 60 | } |
| 61 | |
| 62 | int security_encrypt(struct 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 = -EINVAL, len; |
| 68 | |
| 69 | /* Set IV */ |
| 70 | if (1 != EVP_EncryptInit_ex2(context->ctx, NULL, NULL, iv, NULL)) |
| 71 | goto out; |
| 72 | |
| 73 | /* Provide associatedData data. */ |
| 74 | if (1 != |
| 75 | EVP_EncryptUpdate(context->ctx, NULL, &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_EncryptUpdate(context->ctx, ciphertext, &len, plaintext, plaintext_length)) |
| 82 | goto out; |
| 83 | } |
| 84 | |
| 85 | if (1 != EVP_EncryptFinal_ex(context->ctx, plaintext ? ciphertext + len : NULL, &len)) |
| 86 | goto out; |
| 87 | |
| 88 | if (1 != EVP_CIPHER_CTX_ctrl(context->ctx, EVP_CTRL_GCM_GET_TAG, 16, tag)) |
| 89 | goto out; |
| 90 | |
| 91 | ret = 0; |
| 92 | |
| 93 | out: |
| 94 | return ret; |
| 95 | } |
| 96 | |
| 97 | int security_decrypt(struct 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 = -EINVAL, len; |
| 103 | |
| 104 | /* Set IV */ |
| 105 | if (1 != EVP_DecryptInit_ex2(context->ctx, NULL, NULL, iv, NULL)) |
| 106 | goto out; |
| 107 | |
| 108 | /* Provide associatedData data. */ |
| 109 | if (!EVP_DecryptUpdate(context->ctx, NULL, &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_DecryptUpdate(context->ctx, plaintext, &len, ciphertext, |
| 115 | ciphertext_length)) |
| 116 | goto out; |
| 117 | } |
| 118 | |
| 119 | if (!EVP_CIPHER_CTX_ctrl(context->ctx, EVP_CTRL_GCM_SET_TAG, 16, tag)) |
| 120 | goto out; |
| 121 | |
| 122 | ret = EVP_DecryptFinal_ex(context->ctx, ciphertext ? plaintext + len : NULL, &len); |
| 123 | |
| 124 | ret = ret > 0 ? 0 : -EPERM; |
| 125 | |
| 126 | out: |
| 127 | return ret; |
| 128 | } |