When trying to make a decryption subroutine for some luks header reader I got some problems, but later finalizing this minimal reproducible example here,
I compiled it as
Code:
#include <assert.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <openssl/core_names.h>
#include <openssl/params.h>
#include <openssl/evp.h>
#include <openssl/bio.h>
#include <openssl/err.h>
int main(int argc, char **argv) {
int ret = 0, len;
const uint64_t size = 258048;
uint8_t *input_buffer = malloc(size);
uint8_t *output_buffer = malloc(size);
(void) memset(input_buffer, 0, size);
(void) memset(output_buffer, 0, size);
int32_t outlen = 0;
uint8_t iv[1];
uint32_t iv_length = 0;
uint8_t key[64];
uint32_t key_length = sizeof(key);
static_assert(sizeof(key) == 64, "");
(void) memset(key, 0, key_length);
EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
EVP_CIPHER *cipher = EVP_CIPHER_fetch(NULL, "AES-128-XTS", NULL);
if (ctx == NULL || cipher == NULL) {
goto err;
}
OSSL_PARAM params[3] = {
OSSL_PARAM_construct_uint32(OSSL_CIPHER_PARAM_KEYLEN, &key_length),
OSSL_PARAM_construct_uint32(OSSL_CIPHER_PARAM_IVLEN, &iv_length),
OSSL_PARAM_construct_end()
};
if (!EVP_CipherInit_ex2(ctx, cipher, key, iv, 0, params)) {
printf("LINE: %i\n", __LINE__);
goto err;
}
if (!EVP_CipherUpdate(ctx, output_buffer, &outlen, input_buffer, size)) {
printf("LINE: %i\n", __LINE__);
goto err;
}
if (!EVP_CipherFinal_ex(ctx, output_buffer + outlen, &len)) {
printf("LINE: %i\n", __LINE__);
goto err;
}
ret = EXIT_SUCCESS;
for (int x = 0; x < 16; x++) {
for (int y = 0; y < 16; y++) {
printf("%02x ", output_buffer[(x * 16) + y]);
}
printf("\n");
}
err:
EVP_CIPHER_free(cipher);
EVP_CIPHER_CTX_free(ctx);
return ret;
}
cc -g -O0 -lcrypto example.c -o example, and got the error code for an error in EVP_CipherInit_ex2 invocation when I ran the program.[/code]