有人可以使用OpenSSL AES CCM提供加密/解密示例吗?

时间:2011-11-01 19:02:12

标签: openssl aes

我正在寻找使用OpenSSL的AES CCM加密的示例。显然,这是两种“自定义”密码模式之一,需要在EVP_CipherInit_ex()之外进行额外设置。任何建议将不胜感激。

3 个答案:

答案 0 :(得分:2)

简而言之:

// Tell the alg we will encrypt Psize bytes
int outl = 0;
EVP_EncryptUpdate(ctx, 0, &outl, 0, Psize);

// Add the AAD
EVP_EncryptUpdate(ctx, 0, &outl, A, Asize);

// Now we encrypt the data in P, placing the output in CT
EVP_EncryptUpdate(ctx, CT, &outl, P, Psize);

有关详细信息,请参阅我的博文http://www.fredriks.se/?p=23

答案 1 :(得分:0)

要查看使用某些NIST数据的完整简单AES CCM加密/解密示例,我强烈建议您访问OpenSSL Github Demos存储库,此处:

https://github.com/openssl/openssl/blob/master/demos/evp/aesccm.c

该示例运行良好,包含注释和调试输出。

答案 2 :(得分:0)

以下是使用OpenSSL CCM-AES-128进行加密和解密的示例

#include <stdio.h>
#include <string.h>
#include <openssl/evp.h>

void str2hex(char *, char*, int);
void printBytes(unsigned char *, size_t );


int main() {

    unsigned char *aad, *pt, *key, *nonce;
    int Klen, Alen, Nlen, Plen, Tlen, Clen;
    int outl = 0;

    key = "5a33980e71e7d67fd6cf171454dc96e5";
    aad = "eca622a37570df619e10ebb18bebadb2f2b49c4d2b2ff715873bb672e30fc0ff";
    nonce = "33ae68ebb8010c6b3da6b9cb29";
    pt = "a34dfa24847c365291ce1b54bcf8d9a75d861e5133cc3a74";

    Klen = strlen(key) / 2;
    Alen = strlen(aad) / 2;
    Nlen = strlen(nonce) / 2;
    Plen = strlen(pt) / 2;
    Tlen = 16;
    Clen = Plen + Tlen;

    unsigned char keyy[Klen], aadd[Alen], noncee[Nlen], ptt[Plen];
    unsigned char ct[Clen], dt[Plen];

    str2hex(key, keyy, Klen);
    str2hex(pt, ptt, Plen);
    str2hex(aad, aadd, Alen);
    str2hex(nonce, noncee, Nlen);

    EVP_CIPHER_CTX* ctx = EVP_CIPHER_CTX_new();
    EVP_CIPHER_CTX_init(ctx);
    EVP_EncryptInit(ctx, EVP_aes_128_ccm(), 0, 0);
    EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_CCM_SET_IVLEN, Nlen, 0);
    EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_CCM_SET_TAG, Tlen, 0);
    EVP_EncryptInit(ctx, 0, keyy, noncee);
    EVP_EncryptUpdate(ctx, 0, &outl, 0, Plen);
    EVP_EncryptUpdate(ctx, 0, &outl, aadd, Alen);
    EVP_EncryptUpdate(ctx, ct, &outl, ptt, Plen);
    EVP_EncryptFinal(ctx, &ct[outl], &outl);
    EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_CCM_GET_TAG, Tlen, ct + Plen);

    printf("plaintext' = %s", pt);
    // printf("\nciphertext' = 7a60fa7ee8859e283cce378fb6b95522ab8b70efcdb0265f7c4b4fa597666b86dd1353e400f28864");
    printf("\n");

    printf("\nciphertext : ");
    printBytes(ct, Clen);

    EVP_DecryptInit(ctx, EVP_aes_128_ccm(), 0, 0);
    EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_CCM_SET_IVLEN, Nlen, 0);
    EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_CCM_SET_TAG, Tlen, ct + Plen);
    EVP_DecryptInit(ctx, 0, keyy, noncee);
    EVP_DecryptUpdate(ctx, 0, &outl, 0, Plen);
    EVP_DecryptUpdate(ctx, 0, &outl, aadd, Alen);
    EVP_DecryptUpdate(ctx, dt, &outl, ct, Plen);
    EVP_DecryptFinal(ctx, &dt[outl], &outl);
    printf("plaintext : ");
    printBytes(dt, Plen);

    return 0;
}

void str2hex(char *str, char *hex, int len) {
    int tt, ss;
    unsigned char temp[4];
    for (tt = 0, ss = 0; tt < len, ss < 2 * len; tt++, ss += 2) {
        temp[0] = '0';
        temp[1] = 'x';
        temp[2] = str[ss];
        temp[3] = str[ss + 1];

        hex[tt] = (int) strtol(temp, NULL, 0);
    }
}

void printBytes(unsigned char *buf, size_t len) {
    int i;
    for (i = 0; i < len; i++) {
        printf("%02x", buf[i]);
    }
    printf("\n");
}