当前位置: 首页 > 知识库问答 >
问题:

mbed TLS AES-CTR的正确实施?

华景同
2023-03-14

我正在使用这个库:https://tls.mbed.org/download用ESPRESIF ESP32。目标是使用AES-CTR加密一些数据,然后将密码文本解密回原始纯文本。我解密后得到的结果不正确。

因为我们使用的是CTR模式,所以不需要单独的“解密”功能;我们可以只调用一次encrypt函数进行加密,然后再调用同一个函数一次,它就会解密。至少,大多数消息来源都是这么说的,其他实现也是如此:https://github.com/kokke/tiny-AES-c

我尝试使用相同的随机数、相同的流块、不同的非随机数、不同的流块,在每个函数调用之前设置键,等等。

    mbedtls_aes_context aes;
    unsigned char key[16];
    size_t plainText_len = 64;
    unsigned int nc_off = 0;
    unsigned char nonce_counter[16] = {0};
    unsigned char stream_block[16] = {0};
    unsigned char plainText[64] = {0x48, 0x45, 0x4c, 0x4c, 0x4f};
    unsigned char encryptText[64];
    unsigned char decryptText[64];
    memcpy(key, key_128, 16); //key_128 comes from a different file

    //Print Key
    printf("aes Key: \n");
    for(int i = 0; i < 16; i++){
        printf("%x",key[i]);
    }
    printf("\n");

    //Print plain-text
    printf("aes plainText: \n");
    for(int i = 0; i < 5; i++){
        printf("%x",plainText[i]);
    }
    printf("\n");

    esp_aes_init(&aes); //context is initialized
    esp_aes_setkey(&aes, key, 128); //key is associated to context
    esp_aes_crypt_ctr(&aes, plainText_len, &nc_off, nonce_counter, stream_block, plainText, encryptText); //encrypt

    //Print encrypt-text
    printf("aes encryptText: \n");
    for(int i = 0; i < 5; i++){
        printf("%x",encryptText[i]);
    }
    printf("\n");

    esp_aes_crypt_ctr(&aes, plainText_len, &nc_off, nonce_counter, stream_block, encryptText, decryptText); //decrypt

    //Print decrypt-text
    printf("aes decrypt: \n");
    for(int i = 0; i < 5; i++){
        printf("%x",decryptText[i]);
    }
    printf("\n");

在加密函数被第二次调用后,生成的decryptText应该与原始纯文本相同,但是,在目前的纯文本=/=decryptText。

这是我的显示器正在打印的内容:

aes key: 7d3043fb95355e6ccd850ad8debc279
aes plainText: 48454c4c4f
aes encryptText: 852b97da59
aes decryptText: 814268329f

如你所见,我错过了一些可以正确解密加密文本的东西!

共有1个答案

裴凯定
2023-03-14

由于我们使用CTR模式,我们不需要单独的“解密”函数;我们可以调用加密函数...

我认为你把话题混为一谈了。CTR模式确实在加密和解密的前进方向上操作密码。但是,在更高级别的mbedTLS对象中可能不存在相同的对称性。

esp_aes_init(&aes); //context is initialized
esp_aes_setkey(&aes, key, 128); //key is associated to context

esp_aes_crypt_ctr(&aes, plainText_len, &nc_off, nonce_counter, stream_block, plainText, encryptText); //encrypt

// ...

esp_aes_crypt_ctr(&aes, plainText_len, &nc_off, nonce_counter, stream_block, encryptText, decryptText); //decrypt

// ...

esp\u aes\u crypt\u ctr是对mbedtls\u aes\u crypt\u ctr的定义mbedtls\u aes\u crypt\u ctr文档称该函数同时更新nonce\u计数器流块。我相信,当您想要执行解密时,您需要重新启动这两个程序。

以下是文档中提到的stream\u block。你没有继续加密。您需要重新启动参数。

stream_block-保存的用于恢复的流块。这被函数覆盖。它必须是16字节的可读可写缓冲区。

但从更大的角度来看,正如@James所评论的那样。。。您可能应该按照设计的方式使用mbedTLS,而不是尝试走捷径。

 类似资料:
  • m3diaLib A fast, extensive and powerful allround media and game library for the Nintendo 3DS, written in C++. This library is deprecated. I will still merge PRs and fix critical bugs, but I'm not plan

  • 我有一个结构如下的项目。 是我的主要应用程序,而和是导入到Project中的两个库。使用了的一些类,使用了的一些类。 在的文件中,我使用了。一切正常。但是,如果我用替换,它就不能从导入类。它给出错误。

  • 我有个案子需要 对象,如果映射函数抛出异常,我将其映射为。 对象的映射流,如果为null,则抛出异常,否则收集到列表。 我该如何做到这一点呢? 现在我的问题是,我应该如何调整/重构上面的lambda以使on或其他。

  • 问题内容: 我有一个名为User的域对象。用户的属性包括ssoId,名称,电子邮件,createdBy,createdDate和userRole。其中,ssoId必须是唯一的,因为两个用户不能具有相同的sso id。因此,我的equals方法检查sso id并返回true或false。 我认为这是一个错误的实现,尽管就业务规则而言是正确的。对于具有相同sso id但名称或电子邮件或两者具有不同值的

  • 我最近开始使用weka,我正试图使用朴素贝叶斯将推特分类为正面或负面。所以我有一个训练集,上面有我给的标签,还有一个测试集,上面有所有标签都是“肯定的”。当我运行Naive Bayes时,我得到以下结果: 正确分类实例:69 92%错误分类实例:6 8% 然后,如果我将测试集中推文的标签更改为“否定”并再次运行朴素贝叶斯,结果会颠倒: 分类正确的实例:6.8%分类错误的实例:69.92% 我认为正