gpt4 book ai didi

c - AES_cbc_encrypt 的内存分配

转载 作者:行者123 更新时间:2023-11-30 14:57:57 30 4
gpt4 key购买 nike

我正在尝试使用 openssl 库中的 AES_cbc_encrypt 函数加密和解密 keystore 。

我在内存分配方面遇到问题,如果有人能给我一些如何以正确的方式执行此操作的建议,那就太好了。

目前我不知道如何执行此操作,因此在调用 AES_cbc_encrypt 时遇到段错误

另一个问题是如何为 C 中的 char 表分配内存(干净的方式)。我想动态地执行此操作。此问题的代码中的位置标有 QUESTION 字符串。

unsigned char *input;
input = malloc(sizeof(unsigned char)*length);
unsigned char input_question[length];

输出:

[enc_dec_keystore] length: 82    
[enc_dec_keystore] QUESTION #1: why sizeof(input) != sizeof(input_question)
[enc_dec_keystore] input size: 8
[enc_dec_keystore] input_question size: 82

为什么sizeof(input)不同。因为我以这种方式获取 sizeof 指针?我也期望得到 82,因为 sizeof(unsigned char) 应该是 1,乘以 82 应该是 82,对吗?

这是我的程序中的代码:

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <openssl/aes.h>
#include <openssl/evp.h>
#include <openssl/sha.h>

#define DEBUG 1
#define KEYLEN 128

void print_data(const char *tittle, const void* data, int len)
{
printf("%s : ",tittle);
const unsigned char * p = (const unsigned char*)data;
int i = 0;

for (; i<len; ++i)
printf("%02X ", *p++);

printf("\n");
}

void PBKDF2_HMAC_SHA_512(const char *pass, const unsigned char *salt, int32_t iterations, uint32_t outputBytes, unsigned char *result) {
if (DEBUG) {
puts("[PBKDF2_HMAC_SHA_512] in");
}
unsigned char digest[outputBytes];
PKCS5_PBKDF2_HMAC(pass, strlen(pass), salt, strlen(salt), iterations, EVP_sha512(), outputBytes, digest);
for (int i = 0; i < sizeof(digest); i++)
{
result[i] = digest[i];
};
if (DEBUG) {
puts("[PBKDF2_HMAC_SHA_512] out");
}
}

int deriver_key(const char *pass, const char *salt, unsigned char *key) {
if (DEBUG) {
puts("[deriver_key] in");
}
/* allocate 16 bytes of memory for key (128 bits) */
key = (unsigned char *) malloc(sizeof(unsigned char)*16);
/* let's make 10 iteration for now */
PBKDF2_HMAC_SHA_512(pass, salt, 10, KEYLEN/8, key);
if (DEBUG) {
printf("[deriver_key] key(string): %s\n", key);
printf("[deriver_key] key(bytes): ");
for(int i=0; i<KEYLEN/8; i++) {
printf("%02x", key[i]);
}
printf("\n");
puts("[deriver_key] out");
}

return 0;
}

int enc_dec_keystore(char *keystore, char *key) {
/* length of keystore */
int length;
/* if (length % AES_BLOCK_SIZE) !=0 -> fill to 16 bytes block */
int pad_length;
int pad_counter = 0;

FILE *fp;
fp = fopen(keystore, "rb");
fseek(fp, 0L, SEEK_END);
length = ftell(fp);
fseek(fp, 0L, SEEK_SET);
if (DEBUG) {
printf("[enc_dec_keystore] keystore length: %i\n", length);
}

/* check if input fills blocks correctly */
pad_length = length;
while(pad_length % AES_BLOCK_SIZE !=0) {
pad_length++;
pad_counter++;
}
if (DEBUG) {
printf("[enc_dec_keystore] pad_length: %i\n", pad_length);
}

/* IV - fill with 0 for now */
unsigned char iv[AES_BLOCK_SIZE];
memset(iv, 0x00, AES_BLOCK_SIZE);

unsigned char *input;
input = malloc(sizeof(unsigned char)*length);
unsigned char input_question[length];
if (DEBUG) {
printf("\n[enc_dec_keystore] QUESTION #1: why sizeof(input) != sizeof(input_question)\n");
printf("[enc_dec_keystore] input size: %lu\n", sizeof(input));
printf("[enc_dec_keystore] input_question size: %lu\n\n", sizeof(input_question));
}

/* read data from file */
int ret = 0;
ret = fread(input, 1, length, fp);
if (ret == 0) {
puts("[enc_dec_keystore] file doesn't exist");
}

/* close the file after read */
fclose(fp);

if (DEBUG) {
printf("[enc_dec_keystore] sizeof input: %lu, input length: %lu\n", sizeof(input), strlen(input));
printf("[enc_dec_keystore] input data: \n%s\n", input);
}
/* allocate memory for aes's output */
unsigned char *enc_out;
enc_out = malloc(sizeof(unsigned char)*pad_length);
unsigned char enc_out_question[pad_length];
/* padding with 0 */
memset(enc_out, 0, sizeof(enc_out));
if (DEBUG) {
printf("\n[enc_dec_keystore] QUESTION #2: (again) why sizeof(enc_out) != sizeof(enc_out_question)\n");
printf("[enc_dec_keystore] enc_out size: %lu\n", sizeof(enc_out));
printf("[enc_dec_keystore] enc_out_question size: %lu\n\n", sizeof(enc_out_question));
}

/* AES-128 bit CBC Encryption */
/* set up a key */
/*
unsigned char key_again[KEYLEN/8];
for (int i=0; i<KEYLEN/8; i++) {
key_again[i] = key[i];
}
*/
AES_KEY enc_key;
AES_set_encrypt_key(key, 128, &enc_key);
/* encrypt input data to enc_out */
AES_cbc_encrypt(input, enc_out, sizeof(input), &enc_key, iv, AES_ENCRYPT);
if (DEBUG) {
printf("[enc_dec_keystore] encryption - aes output(string): %s\n", enc_out);
printf("[enc_dec_keystore] encryption - aes output(bytes): ");
for(int i=0; i<pad_length; i++) {
printf("%02x", enc_out[i]);
}
printf("\n");
}

/* AES-128 bit CBC Decryption */
/*
AES_KEY dec_key;
AES_set_encrypt_key(key, 128, &enc_key);
memset(iv, 0x00, AES_BLOCK_SIZE);
unsigned char *enc_out;
enc_out = malloc(sizeof(unsigned char)*pad_length);
AES_cbc_encrypt(enc_out, dec_out, sizeof(enc_out), &enc_key, iv, AES_ENCRYPT);
if (DEBUG) {
printf("[enc_dec_keystore] decryption - aes output(string): %s\n", enc_out);
printf("[enc_dec_keystore] decryption - aes output(bytes): ");
for(int i=0; i<pad_length; i++) {
printf("%02x", enc_out[i]);
}
printf("\n");
puts("[enc_dec_keystore] out");
}
*/

return 0;
}

int main(int argc, char **argv) {
char *user = "user";
char *key_id = "key1";
const char *pass = "temppass";
const char *salt = "tempsalt";
char *keystore = "keystore";

/* Deriver key from password and salt */
unsigned char *key;
if (deriver_key(pass, salt, key) != 0) {
puts("[main] error with key derivery");
}

/* Encrypt and decrypt keystore (for tests) */
if (enc_dec_keystore(keystore, key) != 0) {
puts("[main] error with encrypting/decrypting keystore");
}


return 0;
}

keystore 文件包含:

keyid1:01020304050607080900010203040506:
keyid2:06050403020100090807060504030201:

最佳答案

为什么sizeof(input)不是82,因为sizeof只返回指针变量的大小,不返回动态分配数据的大小。PBKDF2_HMAC_SHA_512 返回一个二进制 key ,该 key 可能不是由 '\0' 分隔的,这可能会导致打印 key 时出现段错误

关于c - AES_cbc_encrypt 的内存分配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43608537/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com