从/lib/x86_64-linux-gnu / libcrypto.so.1.0.0段错误

时间:2022-09-01 08:24:36

I am trying to write a little program to encrypt something with OpenSSL and AES in CBC mode. Here is my code :

我正在尝试编写一个小程序,用CBC模式的OpenSSL和AES加密一些东西。这是我的代码:

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

// main entrypoint
int main(int argc, char **argv)
{

    unsigned char *aes_key = malloc(32*sizeof(unsigned char));
        printf("Enter a 32 char key\n");
        scanf("%s", aes_key);
        if ((sizeof(aes_key)/sizeof(aes_key[0])) != 8) {
            fprintf(stderr,"you didn't write 32 char\n");
            return -1;
        }


    uint64_t msg = 30849384302932039;

    /* generate input with a given length */
    unsigned char *aes_input = malloc(100*sizeof(unsigned char));
    sprintf(aes_input, "%lu", msg);

    /* init vector */
    unsigned char *iv = (unsigned char*)malloc(sizeof(unsigned char) *AES_BLOCK_SIZE);
    RAND_bytes(iv, AES_BLOCK_SIZE);

    // buffers for encryption and decryption
    unsigned char *enc_out = malloc(sizeof(unsigned char)*16);
    sprintf(enc_out, "%d", 0);

    AES_KEY enc_key, dec_key;
    AES_set_encrypt_key(aes_key, 32, &enc_key);
    AES_cbc_encrypt(aes_input, enc_out, 16, &enc_key, iv, AES_ENCRYPT);

    printf("original:\t + %s\n",aes_input);
    printf("encrypt:\t + %s\n",enc_out);

    return 0;
}

I compile it with gcc -g test.c -lcrypto -o test but when I run it, I get a segmentation fault and gdb indicates me :

我用gcc -g测试来编译它。c -lcrypto -o测试,但是当我运行它时,我得到一个分割错误,gdb指示我:

Program received signal SIGSEGV, Segmentation fault.
0x00007ffff7a7b9a0 in ?? () from /lib/x86_64-linux-gnu/libcrypto.so.1.0.0

After trying to debug it, I found that the line AES_cbc_encrypt(aes_input, enc_out, 15, &enc_key, iv, AES_ENCRYPT); is responsible of the segfaut... But, all arguments seem initialized, I tried to print their values and I didn't get any problem ?

在尝试调试它之后,我发现line AES_cbc_encrypt(aes_input, enc_out, 15, &enc_key, iv, AES_ENCRYPT);负责segfaut…但是,所有的参数都是初始化的,我尝试打印它们的值,我没有遇到任何问题?

So I don't realy get what I am doing wrong, could someone help me ? Thank you very much :)

所以我不知道我做错了什么,有人能帮我吗?非常感谢:)

1 个解决方案

#1


1  

unsigned char *aes_key = malloc(32*sizeof(unsigned char));
...
if ((sizeof(aes_key)/sizeof(aes_key[0])) != 8) {

This is not getting the size of the array (32), but the size of a pointer to unsigned char.

这并没有得到数组的大小(32),而是一个指向无符号字符的指针的大小。

Same for

相同

const uint64_t encslength = ((sizeof(aes_input)/sizeof(aes_input[0]) + AES_BLOCK_SIZE) / AES_BLOCK_SIZE) * AES_BLOCK_SIZE;

Take a look to Question 7.28 of C FAQ

看一看问题7.28的C FAQ。

#1


1  

unsigned char *aes_key = malloc(32*sizeof(unsigned char));
...
if ((sizeof(aes_key)/sizeof(aes_key[0])) != 8) {

This is not getting the size of the array (32), but the size of a pointer to unsigned char.

这并没有得到数组的大小(32),而是一个指向无符号字符的指针的大小。

Same for

相同

const uint64_t encslength = ((sizeof(aes_input)/sizeof(aes_input[0]) + AES_BLOCK_SIZE) / AES_BLOCK_SIZE) * AES_BLOCK_SIZE;

Take a look to Question 7.28 of C FAQ

看一看问题7.28的C FAQ。