首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >OpenSSL没有按照NIST实现AES?

OpenSSL没有按照NIST实现AES?
EN

Security用户
提问于 2015-04-10 07:04:45
回答 1查看 1.1K关注 0票数 2

我一直在使用来自https://wiki.openssl.org/index.php/EVP_对称_加密_和_解密的加密/解密代码。

用于AES的NIST测试向量(http://csrc.nist.gov/groups/STM/cavp/)不能产生预期的结果。我可以知道原因是什么,以及如何让它按照NIST测试向量工作?

为了提高可读性,我将添加代码below:#include

代码语言:javascript
复制
#include <openssl/evp.h>
#include <openssl/err.h>
#include <string.h>


int main(int arc, char *argv[])
{
/* Set up the key and iv. Do I need to say to not hard code these in a
* real application? :-)
*/

/* A 256 bit key */
unsigned char ***key** = "0000000000000000000000000000000000000000000000000000000000000000";

/* A 128 bit IV */
unsigned char ***iv** = "00000000000000000000000000000000";

/* Message to be encrypted */
unsigned char *plaintext =
"80000000000000000000000000000000";

/* Buffer for ciphertext. Ensure the buffer is long enough for the
 * ciphertext which may be longer than the plaintext, dependant on the
 * algorithm and mode
 */
unsigned char ciphertext[128];

/* Buffer for the decrypted text */
unsigned char decryptedtext[128];

int decryptedtext_len, ciphertext_len;

/* Initialise the library */
ERR_load_crypto_strings();
OpenSSL_add_all_algorithms();
OPENSSL_config(NULL);

/* Encrypt the plaintext */
ciphertext_len = encrypt(plaintext, strlen(plaintext), key, iv,
ciphertext);

/* Do something useful with the ciphertext here */
printf("Ciphertext is:\n");
BIO_dump_fp(stdout, ciphertext, ciphertext_len);

/* Decrypt the ciphertext */
decryptedtext_len = decrypt(ciphertext, ciphertext_len, key, iv,
decryptedtext);

/* Add a NULL terminator. We are expecting printable text */
decryptedtext[decryptedtext_len] = '\0';

/* Show the decrypted text */
printf("Decrypted text is:\n");
printf("%s\n", decryptedtext);

/* Clean up */
EVP_cleanup();
ERR_free_strings();

return 0;
}

void handleErrors(void)
{
ERR_print_errors_fp(stderr);
abort();
}

int encrypt(unsigned char *plaintext, int plaintext_len, unsigned char *key,
unsigned char *iv, unsigned char *ciphertext)
{
EVP_CIPHER_CTX *ctx;

int len;

int ciphertext_len;

/* Create and initialise the context */
if(!(ctx = EVP_CIPHER_CTX_new())) handleErrors();

/* Initialise the encryption operation. IMPORTANT - ensure you use a key
 * and IV size appropriate for your cipher
 * In this example we are using 256 bit AES (i.e. a 256 bit key). The
 * IV size for *most* modes is the same as the block size. For AES this
 * is 128 bits */
if(1 != EVP_EncryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, key, iv))
handleErrors();

/* Provide the message to be encrypted, and obtain the encrypted output.
 * EVP_EncryptUpdate can be called multiple times if necessary
 */
if(1 != EVP_EncryptUpdate(ctx, ciphertext, &len, plaintext, plaintext_len))
handleErrors();
ciphertext_len = len;

/* Finalise the encryption. Further ciphertext bytes may be written at
 * this stage.
 */
if(1 != EVP_EncryptFinal_ex(ctx, ciphertext + len, &len)) handleErrors();
ciphertext_len += len;

/* Clean up */
EVP_CIPHER_CTX_free(ctx);

return ciphertext_len;
}

int decrypt(unsigned char *ciphertext, int ciphertext_len, unsigned char *key,
unsigned char *iv, unsigned char *plaintext)
{
EVP_CIPHER_CTX *ctx;

int len;

int plaintext_len;

/* Create and initialise the context */
if(!(ctx = EVP_CIPHER_CTX_new())) handleErrors();

/* Initialise the decryption operation. IMPORTANT - ensure you use a key
 * and IV size appropriate for your cipher
 * In this example we are using 256 bit AES (i.e. a 256 bit key). The
 * IV size for *most* modes is the same as the block size. For AES this
 * is 128 bits */
if(1 != EVP_DecryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, key, iv))
handleErrors();

/* Provide the message to be decrypted, and obtain the plaintext output.
* EVP_DecryptUpdate can be called multiple times if necessary
*/
if(1 != EVP_DecryptUpdate(ctx, plaintext, &len, ciphertext, ciphertext_len))
handleErrors();
plaintext_len = len;

/* Finalise the decryption. Further plaintext bytes may be written at
 * this stage.
 */
if(1 != EVP_DecryptFinal_ex(ctx, plaintext + len, &len)) handleErrors();
plaintext_len += len;

/* Clean up */
EVP_CIPHER_CTX_free(ctx);

return plaintext_len;
}

我得到的输出是:

密文是:

0000 - 0d d2 52 e5 3c 69 ad 57-f2 93 1c 2b 0b 1b 84 a6 ..R.

采购产品名称: c8 b7 b7 c5 67 65 82-16 df ac 51 cc ae d7 20 >.

0020 - 6a 2a 39 fd 8f f9 2a b7-e5 6d 27 47 c5 36 ef 65 j*9.m‘G.6.e

解密文本是:

800000000000000000000000000000

但是根据NIST测试向量,输出应该是: ddc6bf790c15760d8d9aeb6f9a75fd4e

EN

回答 1

Security用户

发布于 2015-04-10 13:47:56

我还没有测试过OpenSSL,但我非常肯定它实现了AES-CBC的正确性。但是,您的程序显然使用了不同的数据,因此得到不同的结果并不奇怪。

测试向量以十六进制形式给出。例如

代码语言:javascript
复制
KEY = 0000000000000000000000000000000000000000000000000000000000000000
IV = 00000000000000000000000000000000
PLAINTEXT = 80000000000000000000000000000000
CIPHERTEXT = ddc6bf790c15760d8d9aeb6f9a75fd4e

有一个32字节的键(AES-256的正确大小),其中所有字节的值都为0。类似地,16字节IV包含所有字节0,明文包含一个字节,其值为0x80=128,后面为15个字节,值为0,而密文是给定的16字节字符串。

您的程序正在使用NIST符号中的数据

代码语言:javascript
复制
KEY = 3030303030303030303030303030303030303030303030303030303030303030
IV = 30303030303030303030303030303030
PLAINTEXT = 38303030303030303030303030303030

也就是说,键由64个字节组成,每个字节是字符0的ASCII代码,等等。

您的计算结果是正确的,顺便说一句(我指的是您发布的输出对于程序中的数据是正确的)。然而,有几件事,你应该注意的大小,这将在一个真正的程序。key变量包含一个65字节数组(64字节0x30=48='0' (数字0)加上一个终止空字节)- this工作,因为计算只使用数组的前32字节,但使程序混淆。同样适用于IV。对于明文,使用strlen计算长度;请记住,只有当数据不包含任何空字节时,这才能工作,因此,如果数据是C字符串,而对于任意二进制数据,则可以工作。您列出了48字节的输出,但是您输入了32字节,因此输出实际上只有32字节。

如果您对输出大小感到困惑: CBC加密中的每个输入块产生一个输出块。实际上,CBC加密的输出更大,因为它不只是包含CBC算法的输出,原因有两个。首先,IV通常作为加密文本的前缀发送,因此在加密文本的开头有一个额外的块,即IV。换句话说,“CBC加密消息”通常由IV和CBC算法的实际输出组成。其次,CBC只指定如何加密大小为块大小的倍数的数据(无论密钥大小,AES为16字节)。因此,要加密任意大小的数据,需要应用填充算法来获取大量的块,然后将CBC应用于填充算法的结果。您使用的OpenSSL函数应用了PKCS#7填充 (一种常见的选择),结果输出大小被舍入到整组块,或者,当输入大小是整组块时,额外的一个输出块。概括地说:

代码语言:javascript
复制
              CBC
32 bytes ------------> 32 bytes

           PKCS#7 padding                CBC                 preprend IV
32 bytes -----------------> 48 bytes -----------> 48 bytes ---------------> 64 bytes
票数 5
EN
页面原文内容由Security提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://security.stackexchange.com/questions/85672

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档