使用OpenSSL EVP_DigestSign*生成CMAC密钥失败

使用OpenSSL EVP_DigestSign*生成CMAC密钥失败,openssl,cmac,Openssl,Cmac,我正在尝试生成用于使用OpenSSL计算CMAC的密钥 但是,这些操作似乎失败了,下面复制了错误消息。有人能指出问题出在哪里吗? 有人用EVP\u DigestSign*电话进行过CMAC吗 以下是根据以下示例构建的部分代码: 在pmeth_lib.c中的第390行: EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_COMMAND_NOT_SUPPORTED); 我正在使用OpenSSL 1.0.1e 另请参见由于不受支持,目前不可能这样做 唯一可以尝试的是使用最新

我正在尝试生成用于使用OpenSSL计算CMAC的密钥

但是,这些操作似乎失败了,下面复制了错误消息。有人能指出问题出在哪里吗? 有人用
EVP\u DigestSign*
电话进行过CMAC吗

以下是根据以下示例构建的部分代码:

在pmeth_lib.c中的第390行:

EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_COMMAND_NOT_SUPPORTED);
我正在使用OpenSSL 1.0.1e


另请参见

由于不受支持,目前不可能这样做


唯一可以尝试的是使用最新的OpenSSL并尝试相同的方法,但我怀疑它是否有效。尝试不使用EVP。

我设法使用EVP接口让CMAC工作。先前失败的密钥生成部分也可以工作。这是代码。如您所见,我在这里发布了一个示例:它使用OpenSSL的CMAC_Init/Update/Final接口,并尝试了各种NIST值来检查EVP接口是否适用于CMAC:下面是代码片段。密钥生成现在也可以工作。如果有什么我忽略了,请告诉我。在使用EVP时,我有一句话要说。请参见下面的备注部分

/*
 * CMACSiging.c
 */

#include <stdio.h>
#include <openssl/cmac.h>
#include <openssl/err.h>
#include <stdio.h>
#include <stdlib.h>
#include <openssl/evp.h>
#include <string.h>

typedef signed char       int8_t;
typedef signed short      int16_t;
typedef signed int        int32_t;
typedef unsigned char     uint8_t;
typedef unsigned short    uint16_t;
typedef unsigned int      uint32_t;

void printBytes(unsigned char *buf, size_t len) {
    int i;
    for(i=0; i<len; i++) {
        printf("%02x", buf[i]);
    }
    printf("\n");
}
EVP_PKEY *generate_key(int type)
{
    EVP_PKEY_CTX *pctx = NULL, *kctx = NULL;
    EVP_PKEY *params = NULL, *key = NULL;

    unsigned char k[] = {0x60, 0x3d, 0xeb, 0x10, 0x15, 0xca, 0x71, 0xbe, 0x2b, 0x73, 0xae, 0xf0, 0x85, 0x7d, 0x77, 0x81, 0x1f, 0x35, 0x2c, 0x07, 0x3b, 0x61, 0x08, 0xd7, 0x2d, 0x98, 0x10, 0xa3, 0x09, 0x14, 0xdf, 0xf4};
    /* Create context for the key generation */
    if(!(kctx = EVP_PKEY_CTX_new_id(type, NULL))) goto err;

    /* Generate the key */

    if(!EVP_PKEY_keygen_init(kctx)) goto err;

    if(type == EVP_PKEY_CMAC)
    {
        if (EVP_PKEY_CTX_ctrl(kctx, -1, EVP_PKEY_OP_KEYGEN,
            EVP_PKEY_CTRL_CIPHER,
            0, (void *)EVP_aes_256_cbc()) <= 0)
            goto err;

        if (EVP_PKEY_CTX_ctrl(kctx, -1, EVP_PKEY_OP_KEYGEN,
            EVP_PKEY_CTRL_SET_MAC_KEY, sizeof(k), k) <= 0)
            goto err;
    }

    if (!EVP_PKEY_keygen(kctx, &key)) goto err;

   goto end;
err:

end:

    if(pctx) EVP_PKEY_CTX_free(pctx);
    if(params) EVP_PKEY_free(params);
    if(kctx) EVP_PKEY_CTX_free(kctx);

    return key;
}

void trial(uint8_t *msg, uint8_t mlen, uint8_t *key, uint8_t keylen, uint8_t **sig, uint8_t *slen)
{
//16 byte msg with 32 byte key with aes 256 cbc based CMAC
    if(!msg || !mlen || !key) {
        //handleError
    }

    if(*sig)
        OPENSSL_free(*sig);

    *sig = NULL;
    *slen = 0;

    EVP_MD_CTX* ctx = NULL;
    EVP_PKEY *pkey = NULL;
    const EVP_MD* md = NULL;
    OpenSSL_add_all_digests();

    do
    {
        ctx = EVP_MD_CTX_create();
        if(ctx == NULL) {
            printf("EVP_MD_CTX_create failed\n");
            break; // failed
        }
        if(!(md = EVP_get_digestbyname("SHA256")))
                printf("EVP_get_digestbyname failed\n");

        printf("Over to EVP calls \n");
        if(!(pkey = generate_key(EVP_PKEY_CMAC))) printf("Error 5 \n");
        int rc ;
        rc = EVP_DigestSignInit(ctx, NULL, md, NULL, pkey);
        if(rc != 1) {
            printf("EVP_DigestSignInit failed\n");
            ERR_print_errors_fp(stdout);
            break;
        }

        rc = EVP_DigestSignUpdate(ctx, msg, mlen);
        if(rc != 1) {
            printf("EVP_DigestSignUpdate failed\n");
            ERR_print_errors_fp(stdout);
            break;
        }

        size_t req = 0;
        rc = EVP_DigestSignFinal(ctx, NULL, &req);

        if(rc != 1) {
            printf("EVP_DigestSignFinal failed\n");
            ERR_print_errors_fp(stdout);
            break;
        }

        if(!(req > 0)) {
            printf("EVP_DigestSignFinal failed (2)\n");
            break;
        }

        *sig = OPENSSL_malloc(req);
        if(*sig == NULL) {
            printf("OPENSSL_malloc failed, error \n");
            break;
        }

        *slen = req;
        rc = EVP_DigestSignFinal(ctx, *sig, slen);
        if(rc != 1) {
            printf("EVP_DigestSignFinal failed (3)\n");
            ERR_print_errors_fp(stdout);
            break;
        }

    } while(0);

    if(ctx) {
        EVP_MD_CTX_destroy(ctx);
        ctx = NULL;
    }
}

int main(int argc, char *argv[])
{
    // https://tools.ietf.org/html/rfc4493

    // K, M and T from
    // http://csrc.nist.gov/publications/nistpubs/800-38B/Updated_CMAC_Examples.pdf
    // D.1 AES-128

    // K: 2b7e1516 28aed2a6 abf71588 09cf4f3c

    unsigned char key[] = {0x60, 0x3d, 0xeb, 0x10, 0x15, 0xca, 0x71, 0xbe, 0x2b, 0x73, 0xae, 0xf0, 0x85, 0x7d, 0x77, 0x81, 0x1f, 0x35, 0x2c, 0x07, 0x3b, 0x61, 0x08, 0xd7, 0x2d, 0x98, 0x10, 0xa3, 0x09, 0x14, 0xdf, 0xf4};

    // M: 6bc1bee2 2e409f96 e93d7e11 7393172a Mlen: 128
    unsigned char message[] = { 0x6b,0xc1,0xbe,0xe2,
            0x2e,0x40,0x9f,0x96,
            0xe9,0x3d,0x7e,0x11,
            0x73,0x93,0x17,0x2a };

    unsigned char mact[16] = {0};
    size_t mactlen;

    CMAC_CTX *ctx = CMAC_CTX_new();
    CMAC_Init(ctx, key, 32, EVP_aes_256_cbc(), NULL);
    printf("message length = %lu bytes (%lu bits)\n",sizeof(message), sizeof(message)*8);

    CMAC_Update(ctx, message, sizeof(message));
    CMAC_Final(ctx, mact, &mactlen);

    printBytes(mact, mactlen);
    //expected result T = 070a16b4 6b4d4144 f79bdd9d d04a287c

    CMAC_CTX_free(ctx);


    uint8_t key_len = sizeof(key);
    uint8_t mlen = sizeof(message);
    uint8_t *dgst = NULL;
    size_t dlen;
    trial( message, mlen, key, key_len, &dgst, &dlen);
    printf("length of sig = %d\n", dlen);
    printf("CMAC returned from trial is: ");
    int i;
    for(i = 0; i < dlen; i++)
        printf("%02x", dgst[i]);
    printf("\n");

    return 0;
}
/*
*c.c
*/
#包括
#包括
#包括
#包括
#包括
#包括
#包括
typedef签名字符int8;
typedef签名短整数;
typedef签名int int32_t;
typedef无符号字符uint8;
typedef无符号短uint16_t;
typedef unsigned int uint32\u t;
无效打印字节(无符号字符*buf,大小\u t len){
int i;
对于(i=0;iAt,在本节末尾,它提到CMAC仅在1.1.0 OpenSSL版本中受支持:“请注意,CMAC仅在(尚未发布的)OpenSSL 1.1.0版本中受支持”。但是,如果您不使用EVP API,您应该仍然能够使用CMAC;请参阅以获取可能的示例。
EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_COMMAND_NOT_SUPPORTED);
/*
 * CMACSiging.c
 */

#include <stdio.h>
#include <openssl/cmac.h>
#include <openssl/err.h>
#include <stdio.h>
#include <stdlib.h>
#include <openssl/evp.h>
#include <string.h>

typedef signed char       int8_t;
typedef signed short      int16_t;
typedef signed int        int32_t;
typedef unsigned char     uint8_t;
typedef unsigned short    uint16_t;
typedef unsigned int      uint32_t;

void printBytes(unsigned char *buf, size_t len) {
    int i;
    for(i=0; i<len; i++) {
        printf("%02x", buf[i]);
    }
    printf("\n");
}
EVP_PKEY *generate_key(int type)
{
    EVP_PKEY_CTX *pctx = NULL, *kctx = NULL;
    EVP_PKEY *params = NULL, *key = NULL;

    unsigned char k[] = {0x60, 0x3d, 0xeb, 0x10, 0x15, 0xca, 0x71, 0xbe, 0x2b, 0x73, 0xae, 0xf0, 0x85, 0x7d, 0x77, 0x81, 0x1f, 0x35, 0x2c, 0x07, 0x3b, 0x61, 0x08, 0xd7, 0x2d, 0x98, 0x10, 0xa3, 0x09, 0x14, 0xdf, 0xf4};
    /* Create context for the key generation */
    if(!(kctx = EVP_PKEY_CTX_new_id(type, NULL))) goto err;

    /* Generate the key */

    if(!EVP_PKEY_keygen_init(kctx)) goto err;

    if(type == EVP_PKEY_CMAC)
    {
        if (EVP_PKEY_CTX_ctrl(kctx, -1, EVP_PKEY_OP_KEYGEN,
            EVP_PKEY_CTRL_CIPHER,
            0, (void *)EVP_aes_256_cbc()) <= 0)
            goto err;

        if (EVP_PKEY_CTX_ctrl(kctx, -1, EVP_PKEY_OP_KEYGEN,
            EVP_PKEY_CTRL_SET_MAC_KEY, sizeof(k), k) <= 0)
            goto err;
    }

    if (!EVP_PKEY_keygen(kctx, &key)) goto err;

   goto end;
err:

end:

    if(pctx) EVP_PKEY_CTX_free(pctx);
    if(params) EVP_PKEY_free(params);
    if(kctx) EVP_PKEY_CTX_free(kctx);

    return key;
}

void trial(uint8_t *msg, uint8_t mlen, uint8_t *key, uint8_t keylen, uint8_t **sig, uint8_t *slen)
{
//16 byte msg with 32 byte key with aes 256 cbc based CMAC
    if(!msg || !mlen || !key) {
        //handleError
    }

    if(*sig)
        OPENSSL_free(*sig);

    *sig = NULL;
    *slen = 0;

    EVP_MD_CTX* ctx = NULL;
    EVP_PKEY *pkey = NULL;
    const EVP_MD* md = NULL;
    OpenSSL_add_all_digests();

    do
    {
        ctx = EVP_MD_CTX_create();
        if(ctx == NULL) {
            printf("EVP_MD_CTX_create failed\n");
            break; // failed
        }
        if(!(md = EVP_get_digestbyname("SHA256")))
                printf("EVP_get_digestbyname failed\n");

        printf("Over to EVP calls \n");
        if(!(pkey = generate_key(EVP_PKEY_CMAC))) printf("Error 5 \n");
        int rc ;
        rc = EVP_DigestSignInit(ctx, NULL, md, NULL, pkey);
        if(rc != 1) {
            printf("EVP_DigestSignInit failed\n");
            ERR_print_errors_fp(stdout);
            break;
        }

        rc = EVP_DigestSignUpdate(ctx, msg, mlen);
        if(rc != 1) {
            printf("EVP_DigestSignUpdate failed\n");
            ERR_print_errors_fp(stdout);
            break;
        }

        size_t req = 0;
        rc = EVP_DigestSignFinal(ctx, NULL, &req);

        if(rc != 1) {
            printf("EVP_DigestSignFinal failed\n");
            ERR_print_errors_fp(stdout);
            break;
        }

        if(!(req > 0)) {
            printf("EVP_DigestSignFinal failed (2)\n");
            break;
        }

        *sig = OPENSSL_malloc(req);
        if(*sig == NULL) {
            printf("OPENSSL_malloc failed, error \n");
            break;
        }

        *slen = req;
        rc = EVP_DigestSignFinal(ctx, *sig, slen);
        if(rc != 1) {
            printf("EVP_DigestSignFinal failed (3)\n");
            ERR_print_errors_fp(stdout);
            break;
        }

    } while(0);

    if(ctx) {
        EVP_MD_CTX_destroy(ctx);
        ctx = NULL;
    }
}

int main(int argc, char *argv[])
{
    // https://tools.ietf.org/html/rfc4493

    // K, M and T from
    // http://csrc.nist.gov/publications/nistpubs/800-38B/Updated_CMAC_Examples.pdf
    // D.1 AES-128

    // K: 2b7e1516 28aed2a6 abf71588 09cf4f3c

    unsigned char key[] = {0x60, 0x3d, 0xeb, 0x10, 0x15, 0xca, 0x71, 0xbe, 0x2b, 0x73, 0xae, 0xf0, 0x85, 0x7d, 0x77, 0x81, 0x1f, 0x35, 0x2c, 0x07, 0x3b, 0x61, 0x08, 0xd7, 0x2d, 0x98, 0x10, 0xa3, 0x09, 0x14, 0xdf, 0xf4};

    // M: 6bc1bee2 2e409f96 e93d7e11 7393172a Mlen: 128
    unsigned char message[] = { 0x6b,0xc1,0xbe,0xe2,
            0x2e,0x40,0x9f,0x96,
            0xe9,0x3d,0x7e,0x11,
            0x73,0x93,0x17,0x2a };

    unsigned char mact[16] = {0};
    size_t mactlen;

    CMAC_CTX *ctx = CMAC_CTX_new();
    CMAC_Init(ctx, key, 32, EVP_aes_256_cbc(), NULL);
    printf("message length = %lu bytes (%lu bits)\n",sizeof(message), sizeof(message)*8);

    CMAC_Update(ctx, message, sizeof(message));
    CMAC_Final(ctx, mact, &mactlen);

    printBytes(mact, mactlen);
    //expected result T = 070a16b4 6b4d4144 f79bdd9d d04a287c

    CMAC_CTX_free(ctx);


    uint8_t key_len = sizeof(key);
    uint8_t mlen = sizeof(message);
    uint8_t *dgst = NULL;
    size_t dlen;
    trial( message, mlen, key, key_len, &dgst, &dlen);
    printf("length of sig = %d\n", dlen);
    printf("CMAC returned from trial is: ");
    int i;
    for(i = 0; i < dlen; i++)
        printf("%02x", dgst[i]);
    printf("\n");

    return 0;
}