![](/img/trans.png)
[英]AES-256 Encryption with OpenSSL library using ECB mode of operation
[英]AES in ECB mode using WolfCrypt
我在桌面軟件中使用OpenSSL,在其嵌入式版本中使用WolfCrypt(也是開源的)。 對於此項目,即使我知道ECB不是AES的最安全的操作模式,我也必須在ECB模式下使用AES。 根據此線程(斷開的鏈接),即使沒有正確記錄,WolfCrypt也支持ECB模式。
我可以毫無問題地在OpenSSL中對數據進行編碼和解碼,但是在WolfCrypt中卻不能。 在帶有192位和256位長密鑰的ECB模式下,wolfCrypt似乎是錯誤的(但似乎可以與128位長的密鑰一起工作)。 我使用以下代碼注意到了此行為。 該代碼對數據塊進行加密,解密並將結果與原始數據進行比較。 如果數據匹配,則顯示成功消息。 只有128位長的密鑰似乎可以產生正確的結果。
我使用WolfSSL 3.8.0在VS 2013(Windows 7)中測試了此代碼。
我在這里做錯什么了嗎?或者WolfCrypt真的是越野車嗎?
#include <stdlib.h>
#include <stdio.h>
#include <wolfssl/wolfcrypt/aes.h>
#define POINTER_TO_INDEX(v, i) ( &( ( v )[ i ] ) )
#define BITS_TO_BYTES(x) ( ( x ) / 8 )
#define MAX_KEY_BITS ( 256 )
#define MAX_KEY_LENGTH BITS_TO_BYTES( MAX_KEY_BITS )
#define DATA_LENGTH ( 768 )
byte aes_key[MAX_KEY_LENGTH];
byte aes_iv[MAX_KEY_LENGTH];
byte original_data[DATA_LENGTH];
byte encrypted_data[DATA_LENGTH];
byte decrypted_data[DATA_LENGTH];
Aes aes_encrypt;
Aes aes_decrypt;
void wait_before_exit(void)
{
printf("\nPress 'q' to quit.\n");
while (1)
{
char c = getchar();
if (c == 'q' || c == 'Q') return;
}
}
int main(int argc, char* argv[])
{
int actual_key_length = 0;
printf("Choose key length:\n ( A ) 128 bits\n ( B ) 192 bits\n ( C ) 256 bits\n");
while (actual_key_length == 0)
{
char c = getchar();
switch (c)
{
case 'A': case 'a':
actual_key_length = BITS_TO_BYTES(128);
break;
case 'B': case 'b':
actual_key_length = BITS_TO_BYTES(192);
break;
case 'C': case 'c':
actual_key_length = BITS_TO_BYTES(256);
break;
}
}
// generate aes_key and aes_iv.
for (int i = 0; i < actual_key_length; i++)
{
aes_key[i] = (byte)rand();
aes_iv[i] = (byte)rand();
}
// initialize AES engines.
if (wc_AesSetKeyDirect(&aes_encrypt, (const byte *)aes_key, actual_key_length, (const byte *)aes_iv, AES_ENCRYPTION))
{
printf("Cannot create AES engine for encryption.\n");
wait_before_exit();
return 0;
}
if (wc_AesSetKeyDirect(&aes_decrypt, (const byte *)aes_key, actual_key_length, (const byte *)aes_iv, AES_DECRYPTION))
{
printf("Cannot create AES engine for decryption.\n");
wait_before_exit();
return 0;
}
// generate original data.
for (int i = 0; i < DATA_LENGTH; i++)
{
original_data[i] = (byte)rand();
}
// encrypt data.
for (int i = 0; i < DATA_LENGTH; i += actual_key_length)
{
wc_AesEncryptDirect(&aes_encrypt, POINTER_TO_INDEX(encrypted_data, i), (const byte*)POINTER_TO_INDEX(original_data, i));
}
// decrypt data.
for (int i = 0; i < DATA_LENGTH; i += actual_key_length)
{
wc_AesDecryptDirect(&aes_decrypt, POINTER_TO_INDEX(decrypted_data, i), (const byte*)POINTER_TO_INDEX(encrypted_data, i));
}
// check data.
for (int i = 0; i < DATA_LENGTH; i++)
{
if (original_data[i] != decrypted_data[i])
{
printf("Data mismatch at index %i: original value was %i but decrypted value is %i.\n", i, original_data[i], decrypted_data[i]);
wait_before_exit();
return 0;
}
}
printf("Decrypted data matches original data.\n");
wait_before_exit();
return 0;
}
不要混淆密鑰大小和塊大小。 AES支持128、192和256位的密鑰,但是具有128位的固定塊大小。 塊大小也是CBC模式的IV大小,但是ECB模式沒有IV,這部分是它如此糟糕的原因。
您需要按塊大小而不是密鑰大小來推進循環:
int block_length = BITS_TO_BYTES(128);
for (int i = 0; i < DATA_LENGTH; i += block_length) {...}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.