【问题标题】:AES (aes-ige-128, aes-ige-192, aes-ige-256) encryption/decryption with openssl CAES (aes-ige-128, aes-ige-192, aes-ige-256) 使用 openssl C 加密/解密
【发布时间】:2013-08-12 21:04:30
【问题描述】:

最近,我终于(在 stackoverflow 的用户 @WhozCraig 的帮助下)开始在 CBC 模式下使用 AES。现在,我想做完全相同的事情,但使用 AES IGE。我查看了openssl-1.0.1e/test/igetest.c 并尝试构建自己的测试。但是再一次,我在输入和输出正确大小方面遇到了问题。其他一切都很好,因为我从我之前的代码中复制了它:AES (aes-cbc-128, aes-cbc-192, aes-cbc-256) encryption/decryption with openssl C

现在,当我传递一个小于 32 的输入长度时,它会显示:

Give a key length [only 128 or 192 or 256!]:
256
Give an input's length:
3
aes_ige.c(88): OpenSSL internal error, assertion failed: (length%AES_BLOCK_SIZE) == 0
 (core dumped)

但是当长度大于 32 时,我也不确定是否一切正常:

Give a key length [only 128 or 192 or 256!]:
256
Give an input's length:
48
original:       58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 
encrypt:        A4 1F C4 E8 42 5E E5 62 1A B6 C1 47 D2 2F 8D 98 D0 0B 32 77 4E 36 84 25 15 5B BA 60 EA A9 64 D2 53 D1 98 78 83 21 90 90 74 44 C7 AA 3E AD 9B 26 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
decrypt:        58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 

代码如下:

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

// a simple hex-print routine. could be modified to print 16 bytes-per-line
static void hex_print(const void* pv, size_t len)
{
    const unsigned char * p = (const unsigned char*)pv;
    if (NULL == pv)
        printf("NULL");
    else
    {
        size_t i = 0;
        for (; i<len;++i)
            printf("%02X ", *p++);
    }
    printf("\n");
}

// main entrypoint
int main(int argc, char **argv)
{
    int keylength;
    printf("Give a key length [only 128 or 192 or 256!]:\n");
    scanf("%d", &keylength);

    /* generate a key with a given length */
    unsigned char aes_key[keylength/8];
    memset(aes_key, 0, keylength/8);
    if (!RAND_bytes(aes_key, keylength/8))
        exit(-1);

    size_t inputslength = 0;
    printf("Give an input's length:\n");
    scanf("%lu", &inputslength);

    /* generate input with a given length */
    unsigned char aes_input[inputslength];
    memset(aes_input, 'X', inputslength);

    const size_t ivsize = AES_BLOCK_SIZE*2;

    /* init vector */
    unsigned char iv_enc[ivsize], iv_dec[ivsize];
    RAND_bytes(iv_enc, ivsize);
    memcpy(iv_dec, iv_enc, ivsize);

    // buffers for encryption and decryption
    const size_t encslength = (inputslength/AES_BLOCK_SIZE + 1) * AES_BLOCK_SIZE;//((inputslength + AES_BLOCK_SIZE) / AES_BLOCK_SIZE) * AES_BLOCK_SIZE;
    unsigned char enc_out[encslength];
    unsigned char dec_out[inputslength];
    memset(enc_out, 0, sizeof(enc_out));
    memset(dec_out, 0, sizeof(dec_out));

    AES_KEY enc_key, dec_key;
    AES_set_encrypt_key(aes_key, keylength, &enc_key);
    AES_set_decrypt_key(aes_key, keylength, &dec_key);

    AES_ige_encrypt(aes_input, enc_out, inputslength, &enc_key, iv_enc, AES_ENCRYPT);

    AES_ige_encrypt(enc_out, dec_out, encslength, &dec_key, iv_dec, AES_DECRYPT);

    printf("original:\t");
    hex_print(aes_input, sizeof(aes_input));

    printf("encrypt:\t");
    hex_print(enc_out, sizeof(enc_out));

    printf("decrypt:\t");
    hex_print(dec_out, sizeof(dec_out));

    return 0;
}

终于!得到它的工作(希望如此)。但如果有人能说下面的代码是 100% 好的,我会非常感激;)

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

// a simple hex-print routine. could be modified to print 16 bytes-per-line
static void hex_print(const void* pv, size_t len)
{
    const unsigned char * p = (const unsigned char*)pv;
    if (NULL == pv)
        printf("NULL");
    else
    {
        size_t i = 0;
        for (; i<len;++i)
            printf("%02X ", *p++);
    }
    printf("\n");
}

// main entrypoint
int main(int argc, char **argv)
{
    int keylength = 256;
    //printf("Give a key length [only 128 or 192 or 256!]:\n");
    //scanf("%d", &keylength);

    /* generate a key with a given length */
    unsigned char aes_key[keylength/8];
    memset(aes_key, 0, keylength/8);
    if (!RAND_bytes(aes_key, keylength/8))
        exit(-1);

    size_t inputslength = 0;
    printf("Give an input's length:\n");
    scanf("%lu", &inputslength);

    /* generate input with a given length */
    unsigned char aes_input[inputslength];
    memset(aes_input, 'X', inputslength);

    const size_t ivsize = AES_BLOCK_SIZE*2;
    /* init vector */
    unsigned char iv_enc[ivsize], iv_dec[ivsize];
    RAND_bytes(iv_enc, ivsize);
    memcpy(iv_dec, iv_enc, ivsize);

    // buffers for encryption and decryption
    const size_t encslength = ((inputslength + AES_BLOCK_SIZE) / AES_BLOCK_SIZE) * AES_BLOCK_SIZE;
    unsigned char enc_out[encslength];
    unsigned char dec_out[inputslength];
    memset(enc_out, 0, sizeof(enc_out));
    memset(dec_out, 0, sizeof(dec_out));

    // so i can do with this aes-cbc-128 aes-cbc-192 aes-cbc-256
    AES_KEY enc_key, dec_key;
    AES_set_encrypt_key(aes_key, keylength, &enc_key);
    AES_ige_encrypt(aes_input, enc_out, encslength, &enc_key, iv_enc, AES_ENCRYPT);

    AES_set_decrypt_key(aes_key, keylength, &dec_key);
    AES_ige_encrypt(enc_out, dec_out, encslength, &dec_key, iv_dec, AES_DECRYPT);

    printf("original:\t");
    hex_print(aes_input, sizeof(aes_input));

    printf("encrypt:\t");
    hex_print(enc_out, sizeof(enc_out));

    printf("decrypt:\t");
    hex_print(dec_out, sizeof(dec_out));

    return 0;
}

我只是改变了这个:

AES_ige_encrypt(aes_input, enc_out, **inputslength**, &amp;enc_key, iv_enc, AES_ENCRYPT);

进入那个:

AES_ige_encrypt(aes_input, enc_out, **encslength**, &amp;enc_key, iv_enc, AES_ENCRYPT);

对吗?

编辑 No.2 ;)

好的,伙计们,又做了一个例子,你的建议在输入中添加了一些填充。希望现在没事吧?

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

// a simple hex-print routine. could be modified to print 16 bytes-per-line
static void hex_print(const void* pv, size_t len)
{
    const unsigned char * p = (const unsigned char*)pv;
    if (NULL == pv)
        printf("NULL");
    else
    {
        size_t i = 0;
        for (; i<len;++i)
            printf("%02X ", *p++);
    }
    printf("\n");
}

// main entrypoint
int main(int argc, char **argv)
{
    int keylength = 256;
    //printf("Give a key length [only 128 or 192 or 256!]:\n");
    //scanf("%d", &keylength);

    /* generate a key with a given length */
    unsigned char aes_key[keylength/8];
    memset(aes_key, 0, keylength/8);
    if (!RAND_bytes(aes_key, keylength/8))
        exit(-1);

    size_t inputslength = 0;
    printf("Give an input's length:\n");
    scanf("%lu", &inputslength);

    /* generate input with a given length */
    unsigned char aes_input[inputslength];
    memset(aes_input, 'X', inputslength);

    const size_t ivsize = AES_BLOCK_SIZE*2;
    /* init vector */
    unsigned char iv_enc[ivsize], iv_dec[ivsize];
    RAND_bytes(iv_enc, ivsize);
    memcpy(iv_dec, iv_enc, ivsize);

    // buffers for encryption and decryption
    const size_t encslength = ((inputslength + AES_BLOCK_SIZE) / AES_BLOCK_SIZE) * AES_BLOCK_SIZE;

    unsigned char paddedinput[encslength];
    memset(paddedinput, 0, encslength);
    memcpy(paddedinput, aes_input, inputslength);

    unsigned char enc_out[encslength];
    unsigned char dec_out[inputslength];
    memset(enc_out, 0, sizeof(enc_out));
    memset(dec_out, 0, sizeof(dec_out));

    AES_KEY enc_key, dec_key;
    AES_set_encrypt_key(aes_key, keylength, &enc_key);
    AES_ige_encrypt(paddedinput, enc_out, encslength, &enc_key, iv_enc, AES_ENCRYPT);

    AES_set_decrypt_key(aes_key, keylength, &dec_key);
    AES_ige_encrypt(enc_out, dec_out, encslength, &dec_key, iv_dec, AES_DECRYPT);

    printf("original:\t");
    hex_print(aes_input, sizeof(aes_input));

    printf("encrypt:\t");
    hex_print(enc_out, sizeof(enc_out));

    printf("decrypt:\t");
    hex_print(dec_out, sizeof(dec_out));

    return 0;
}

【问题讨论】:

  • @WhozCraig:我可以再打扰你一会儿吗?你能看看我粘贴在这里的第二个代码,只说它是否正确吗? AES_ige_encrypt 的主要问题 - 当我通过 inputslength 而不是 encslength 时,我遇到了一个奇怪的错误:aes_ige.c(88): OpenSSL internal error, assertion failed: (length%AES_BLOCK_SIZE) == 0
  • 如果你不执行任何填充(零字节填充或其他),那么你需要你的纯文本正好是块大小的 N 倍。您不清楚这其中的哪一部分? IGE 在这方面与 CBC 相同。
  • @owlstead:感谢您的回答。是的,我昨天读了很多关于它的内容,我认为现在对我来说很清楚了。但是还有一件事我不能真正理解。在我的previous code 中,我没有做任何填充,一切都很好。我的意思是,我向AES_cbc_encrypt 传递了用户给定长度的输入(没有填充!),那么为什么现在它不起作用?
  • 请注意,如果您想使用二进制数据(可能以零值字节结尾),最好应用 PKCS#7 填充。另请注意,IGE 不是 身份验证标签的替代品。完整性/真实性可能无法保留 - 中间人攻击可能适用。 IGE 是一种很少使用的加密模式,只能防止非常特定的攻击。

标签: c openssl aes


【解决方案1】:

错误消息说明了一切。

aes_ige.c(88): OpenSSL internal error, assertion failed: (length%AES_BLOCK_SIZE) == 0

这基本上是一个运行时检查(断言),由于向 line 88 of aes_ige.c 源中的函数 AES_ige_encrypt() 提供的无效输入而失败。

OPENSSL_assert((length%AES_BLOCK_SIZE) == 0);

断言主要检查 length(传递给函数的第三个参数)是否是 AES_BLOCK_SIZE 的整数倍。如果是,则继续执行,否则程序将停止并打印有关断言失败的警告。

  1. 所以要确保传递给AES_ige_encrypt()的数据大小是AES_BLOCK_SIZE的倍数。

  2. 如果数据大小不是整数倍,则附加 NUL 个字节,使总大小最接近 AES_BLOCK_SIZE 的倍数>.

  3. 另外,始终将“integral-multiple-of-AES_BLOCK_SIZE”值作为 length 参数传递给 AES_ige_encrypt()

【讨论】:

  • 感谢您的解释。我知道错误来自哪里,但我不明白为什么。在我的previous code 中,我没有做任何填充,它起作用了。我向AES_cbc_encrypt 传递了用户给定长度的输入(没有填充!),那么为什么现在它不起作用?我做了另一个示例代码:pastie.org/private/yzlkcudvpwwfczvpvo8gw,现在似乎没问题。有什么想法为什么我不能像以前一样做吗?
  • 之前调用AES_cbc_encrypt(),内部调用CRYPTO_cbc128_encrypt(),对要加密的payload数据长度没有限制。因此,您没有收到任何错误。
  • 关于您的块大小查询,答案取决于库/框架的实现。顾名思义,BLOCK 密码需要标准的块大小。当传递非标准尺寸时,是否 payload 自动填充引发错误 取决于代码实现加密框架/库。
  • 最初 zero-ing a buffer of proper-length 然后将有效负载数据复制到其中对我来说看起来不错。修改encslength的计算如下:encslength = ((inputslength + AES_BLOCK_SIZE -1) / AES_BLOCK_SIZE) * AES_BLOCK_SIZE;。如果负载已经AES_BLOCK_SIZE的精确倍数,旧的逻辑会不必要地使用更大的缓冲区。
  • 是的,是的。数学是一样的。关于为什么某个库以特定方式工作 - 答案几乎总是在文档中的“某处”可用。此外,只要源代码可用,这是检查为什么会发生这种情况的最佳方式。我已将我之前碰巧提到的所有 openssl 函数链接到在线 openssl 源代码库。我鼓励您单击链接并花几分钟浏览代码以更好地理解。
猜你喜欢
  • 2013-08-11
  • 1970-01-01
  • 1970-01-01
  • 2011-05-23
  • 1970-01-01
  • 2016-06-20
  • 1970-01-01
  • 1970-01-01
  • 2019-08-17
相关资源
最近更新 更多