2

次の例では、64 ビット デスクトップ Ubuntu 16.04 で同じ元の文字列を適切に暗号化および復号化しますが、同じコードをコンパイルして Raspberry Pi (ARM) (および別のカスタム Linux ARM ボード) で実行すると、復号化に失敗します。元の文字列。Raspberry Pi と他の ARM ボードの両方が、同じ、しかし正しくない値に復号化します。

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

/* AES key for Encryption and Decryption */
const static unsigned char aes_key[]={0x00,0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88,0x99,0xAA,0xBB,0xCC,0xDD,0xEE,0xFF};

/* Print Encrypted and Decrypted data packets */
void print_data(const char *tittle, const void* data, int len);

int main( )
{
    /* Input data to encrypt */
    unsigned char aes_input[]={0x0,0x1,0x2,0x3,0x4,0x5};

    fprintf(stderr,"%s\n",SSLeay_version(SSLEAY_VERSION));

    /* Init vector */
    unsigned char iv[AES_BLOCK_SIZE];
    memset(iv, 0x00, AES_BLOCK_SIZE);

    /* Buffers for Encryption and Decryption */
    unsigned char enc_out[sizeof(aes_input)];
    unsigned char dec_out[sizeof(aes_input)];

    /* AES-128 bit CBC Encryption */
    AES_KEY enc_key, dec_key;
    AES_set_encrypt_key(aes_key, sizeof(aes_key)*8, &enc_key);
    AES_cbc_encrypt(aes_input, enc_out, sizeof(aes_input), &enc_key, iv,         AES_ENCRYPT);
    /* AES-128 bit CBC Decryption */
    memset(iv, 0x00, AES_BLOCK_SIZE); // don't forget to set iv vector again, else you can't decrypt data properly
    AES_set_decrypt_key(aes_key, sizeof(aes_key)*8, &dec_key); // Size of key is in bits
    AES_cbc_encrypt(enc_out, dec_out, sizeof(aes_input), &dec_key, iv, AES_DECRYPT);

    /* Printing and Verifying */
    print_data("\n Original ",aes_input, sizeof(aes_input)); // you can not print data as a string, because after Encryption its not ASCII

    print_data("\n Encrypted",enc_out, sizeof(enc_out));

    print_data("\n Decrypted",dec_out, sizeof(dec_out));

    return 0;
}

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");
}

Ubuntu の結果:

 OpenSSL 1.0.1f 6 Jan 2014

 Original  : 00 01 02 03 04 05 

 Encrypted : D5 40 D0 BB 16 1D 

 Decrypted : 00 01 02 03 04 05 

ラズベリーパイの結果:

OpenSSL 1.0.2l  25 May 2017

Original  : 00 01 02 03 04 05 

Encrypted : D5 40 D0 BB 16 1D 

Decrypted : D3 87 81 20 2B B9

カスタム ボードの結果:

OpenSSL 1.1.0f  25 May 2017

Original  : 00 01 02 03 04 05 

Encrypted : D5 40 D0 BB 16 1D 

Decrypted : D3 87 81 20 2B B9 

カスタム ボード (OpenSSL を Ubuntu に合わせて更新):

 OpenSSL 1.0.1f 6 Jan 2014

 Original  : 00 01 02 03 04 05 

 Encrypted : D5 40 D0 BB 16 1D 

 Decrypted : D3 87 81 20 2B B9

オープンソースの libcrypto が Ubuntu と 2 つの異なる ARM マシンで同じように動作しないのはなぜですか?

4

1 に答える 1