【发布时间】:2017-01-26 05:02:46
【问题描述】:
当我尝试通过EVP_CIPHER_CTX_set_key_length 将EVP_aes_128_cbc 信封的密钥长度设置为58 时,我收到以下错误:
3073369856:error:0607A082:digital envelope routines:EVP_CIPHER_CTX_set_key_length:invalid key length:crypto/evp/evp_enc.c:557:
这是有道理的,因为 EVP_aes_128_cbc 仅支持 128 位或 16 字节的密钥大小。
对我来说没有意义的是,当我为 EVP_bf_cbc 做同样的事情时,我根本没有收到任何错误。
根据wikipedia's entry on Blowfish,最大位大小为 448 位或 56 字节,小于 58。此外,引用http://etutorials.org/Programming/secure+programming/Chapter+5.+Symmetric+Encryption/5.18+Using+Variable+Key-Length+Ciphers+in+OpenSSL/,“OpenSSL 对密钥大小设置了 256 位的硬性限制 ”。
我的代码:
#include <openssl/conf.h>
#include <openssl/evp.h>
#include <openssl/err.h>
#include <openssl/crypto.h>
#include <string.h>
void handleErrors(void);
int main (void)
{
unsigned char *key = (unsigned char *)"0123456789012345678901234567890123456789012345678901234567";
unsigned char *iv = (unsigned char *)"01234567";
unsigned char *plaintext = (unsigned char *)"xxxxxxxx";
unsigned char ciphertext[128];
int plaintext_len = strlen((char *)plaintext);
int len;
EVP_CIPHER_CTX *ctx;
ERR_load_crypto_strings();
OpenSSL_add_all_algorithms();
OPENSSL_config(NULL);
if (!(ctx = EVP_CIPHER_CTX_new())) handleErrors();
if (!EVP_EncryptInit_ex(ctx, EVP_bf_cbc(), NULL, NULL, NULL)) handleErrors();
if (!EVP_CIPHER_CTX_set_key_length(ctx, 58)) handleErrors();
if (1 != EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv)) handleErrors();
if (1 != EVP_EncryptUpdate(ctx, ciphertext, &len, plaintext, plaintext_len)) handleErrors();
if (1 != EVP_EncryptFinal_ex(ctx, ciphertext + len, &len)) handleErrors();
EVP_CIPHER_CTX_free(ctx);
}
void handleErrors(void)
{
ERR_print_errors_fp(stderr);
abort();
}
将EVP_bf_cbc 替换为EVP_aes_128_cbc 以重现错误。如果我在EVP_EncryptInit_ex(与EVP_aes_128_cbc)中遇到错误,我不会感到惊讶,因为 iv 不等于块大小,但代码在此之前中止,无论如何,所以它似乎有点没有实际意义我。
【问题讨论】:
-
这可能是 OpenSSL 中的验证问题。我猜这个库在内部将密钥设置为 56 字节。也许你应该把它放在 OpenSSL 邮件列表中。