Make CMAC API similar to HMAC API. Add methods for CMAC.
This commit is contained in:
parent
8c968e0355
commit
c8ef656df2
1
CHANGES
1
CHANGES
@ -5,6 +5,7 @@
|
|||||||
Changes between 1.0.0 and 1.1.0 [xx XXX xxxx]
|
Changes between 1.0.0 and 1.1.0 [xx XXX xxxx]
|
||||||
|
|
||||||
*) Initial CMAC implementation. WARNING: EXPERIMENTAL, API MAY CHANGE.
|
*) Initial CMAC implementation. WARNING: EXPERIMENTAL, API MAY CHANGE.
|
||||||
|
Add CMAC pkey methods.
|
||||||
[Steve Henson]
|
[Steve Henson]
|
||||||
|
|
||||||
*) Experiemental regnegotiation in s_server -www mode. If the client
|
*) Experiemental regnegotiation in s_server -www mode. If the client
|
||||||
|
@ -69,6 +69,7 @@ extern const EVP_PKEY_ASN1_METHOD dsa_asn1_meths[];
|
|||||||
extern const EVP_PKEY_ASN1_METHOD dh_asn1_meth;
|
extern const EVP_PKEY_ASN1_METHOD dh_asn1_meth;
|
||||||
extern const EVP_PKEY_ASN1_METHOD eckey_asn1_meth;
|
extern const EVP_PKEY_ASN1_METHOD eckey_asn1_meth;
|
||||||
extern const EVP_PKEY_ASN1_METHOD hmac_asn1_meth;
|
extern const EVP_PKEY_ASN1_METHOD hmac_asn1_meth;
|
||||||
|
extern const EVP_PKEY_ASN1_METHOD cmac_asn1_meth;
|
||||||
|
|
||||||
/* Keep this sorted in type order !! */
|
/* Keep this sorted in type order !! */
|
||||||
static const EVP_PKEY_ASN1_METHOD *standard_methods[] =
|
static const EVP_PKEY_ASN1_METHOD *standard_methods[] =
|
||||||
@ -90,7 +91,8 @@ static const EVP_PKEY_ASN1_METHOD *standard_methods[] =
|
|||||||
#ifndef OPENSSL_NO_EC
|
#ifndef OPENSSL_NO_EC
|
||||||
&eckey_asn1_meth,
|
&eckey_asn1_meth,
|
||||||
#endif
|
#endif
|
||||||
&hmac_asn1_meth
|
&hmac_asn1_meth,
|
||||||
|
&cmac_asn1_meth
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef int sk_cmp_fn_type(const char * const *a, const char * const *b);
|
typedef int sk_cmp_fn_type(const char * const *a, const char * const *b);
|
||||||
|
@ -17,8 +17,8 @@ TEST=
|
|||||||
APPS=
|
APPS=
|
||||||
|
|
||||||
LIB=$(TOP)/libcrypto.a
|
LIB=$(TOP)/libcrypto.a
|
||||||
LIBSRC=cmac.c
|
LIBSRC=cmac.c cm_ameth.c cm_pmeth.c
|
||||||
LIBOBJ=cmac.o
|
LIBOBJ=cmac.o cm_ameth.o cm_pmeth.o
|
||||||
|
|
||||||
SRC= $(LIBSRC)
|
SRC= $(LIBSRC)
|
||||||
|
|
||||||
|
102
crypto/cmac/cm_ameth.c
Normal file
102
crypto/cmac/cm_ameth.c
Normal file
@ -0,0 +1,102 @@
|
|||||||
|
/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
|
||||||
|
* project 2010.
|
||||||
|
*/
|
||||||
|
/* ====================================================================
|
||||||
|
* Copyright (c) 2010 The OpenSSL Project. All rights reserved.
|
||||||
|
*
|
||||||
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
* modification, are permitted provided that the following conditions
|
||||||
|
* are met:
|
||||||
|
*
|
||||||
|
* 1. Redistributions of source code must retain the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer.
|
||||||
|
*
|
||||||
|
* 2. Redistributions in binary form must reproduce the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer in
|
||||||
|
* the documentation and/or other materials provided with the
|
||||||
|
* distribution.
|
||||||
|
*
|
||||||
|
* 3. All advertising materials mentioning features or use of this
|
||||||
|
* software must display the following acknowledgment:
|
||||||
|
* "This product includes software developed by the OpenSSL Project
|
||||||
|
* for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
|
||||||
|
*
|
||||||
|
* 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
|
||||||
|
* endorse or promote products derived from this software without
|
||||||
|
* prior written permission. For written permission, please contact
|
||||||
|
* licensing@OpenSSL.org.
|
||||||
|
*
|
||||||
|
* 5. Products derived from this software may not be called "OpenSSL"
|
||||||
|
* nor may "OpenSSL" appear in their names without prior written
|
||||||
|
* permission of the OpenSSL Project.
|
||||||
|
*
|
||||||
|
* 6. Redistributions of any form whatsoever must retain the following
|
||||||
|
* acknowledgment:
|
||||||
|
* "This product includes software developed by the OpenSSL Project
|
||||||
|
* for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
|
||||||
|
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||||
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||||
|
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
|
||||||
|
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||||
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||||
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||||
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||||
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
||||||
|
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||||
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
|
||||||
|
* OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
* ====================================================================
|
||||||
|
*
|
||||||
|
* This product includes cryptographic software written by Eric Young
|
||||||
|
* (eay@cryptsoft.com). This product includes software written by Tim
|
||||||
|
* Hudson (tjh@cryptsoft.com).
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include "cryptlib.h"
|
||||||
|
#include <openssl/evp.h>
|
||||||
|
#include <openssl/cmac.h>
|
||||||
|
#include "asn1_locl.h"
|
||||||
|
|
||||||
|
/* CMAC "ASN1" method. This is just here to indicate the
|
||||||
|
* maximum CMAC output length and to free up a CMAC
|
||||||
|
* key.
|
||||||
|
*/
|
||||||
|
|
||||||
|
static int cmac_size(const EVP_PKEY *pkey)
|
||||||
|
{
|
||||||
|
return EVP_MAX_BLOCK_LENGTH;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void cmac_key_free(EVP_PKEY *pkey)
|
||||||
|
{
|
||||||
|
CMAC_CTX *cmctx = (CMAC_CTX *)pkey->pkey.ptr;
|
||||||
|
if (cmctx)
|
||||||
|
CMAC_CTX_free(cmctx);
|
||||||
|
}
|
||||||
|
|
||||||
|
const EVP_PKEY_ASN1_METHOD cmac_asn1_meth =
|
||||||
|
{
|
||||||
|
EVP_PKEY_CMAC,
|
||||||
|
EVP_PKEY_CMAC,
|
||||||
|
0,
|
||||||
|
|
||||||
|
"CMAC",
|
||||||
|
"OpenSSL CMAC method",
|
||||||
|
|
||||||
|
0,0,0,0,
|
||||||
|
|
||||||
|
0,0,0,
|
||||||
|
|
||||||
|
cmac_size,
|
||||||
|
0,
|
||||||
|
0,0,0,0,0,0,
|
||||||
|
|
||||||
|
cmac_key_free,
|
||||||
|
0,
|
||||||
|
0,0
|
||||||
|
};
|
||||||
|
|
229
crypto/cmac/cm_pmeth.c
Normal file
229
crypto/cmac/cm_pmeth.c
Normal file
@ -0,0 +1,229 @@
|
|||||||
|
/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
|
||||||
|
* project 2010.
|
||||||
|
*/
|
||||||
|
/* ====================================================================
|
||||||
|
* Copyright (c) 2010 The OpenSSL Project. All rights reserved.
|
||||||
|
*
|
||||||
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
* modification, are permitted provided that the following conditions
|
||||||
|
* are met:
|
||||||
|
*
|
||||||
|
* 1. Redistributions of source code must retain the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer.
|
||||||
|
*
|
||||||
|
* 2. Redistributions in binary form must reproduce the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer in
|
||||||
|
* the documentation and/or other materials provided with the
|
||||||
|
* distribution.
|
||||||
|
*
|
||||||
|
* 3. All advertising materials mentioning features or use of this
|
||||||
|
* software must display the following acknowledgment:
|
||||||
|
* "This product includes software developed by the OpenSSL Project
|
||||||
|
* for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
|
||||||
|
*
|
||||||
|
* 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
|
||||||
|
* endorse or promote products derived from this software without
|
||||||
|
* prior written permission. For written permission, please contact
|
||||||
|
* licensing@OpenSSL.org.
|
||||||
|
*
|
||||||
|
* 5. Products derived from this software may not be called "OpenSSL"
|
||||||
|
* nor may "OpenSSL" appear in their names without prior written
|
||||||
|
* permission of the OpenSSL Project.
|
||||||
|
*
|
||||||
|
* 6. Redistributions of any form whatsoever must retain the following
|
||||||
|
* acknowledgment:
|
||||||
|
* "This product includes software developed by the OpenSSL Project
|
||||||
|
* for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
|
||||||
|
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||||
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||||
|
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
|
||||||
|
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||||
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||||
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||||
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||||
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
||||||
|
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||||
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
|
||||||
|
* OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
* ====================================================================
|
||||||
|
*
|
||||||
|
* This product includes cryptographic software written by Eric Young
|
||||||
|
* (eay@cryptsoft.com). This product includes software written by Tim
|
||||||
|
* Hudson (tjh@cryptsoft.com).
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include "cryptlib.h"
|
||||||
|
#include <openssl/x509.h>
|
||||||
|
#include <openssl/x509v3.h>
|
||||||
|
#include <openssl/evp.h>
|
||||||
|
#include <openssl/cmac.h>
|
||||||
|
#include "evp_locl.h"
|
||||||
|
|
||||||
|
/* The context structure and "key" is simply a CMAC_CTX */
|
||||||
|
|
||||||
|
static int pkey_cmac_init(EVP_PKEY_CTX *ctx)
|
||||||
|
{
|
||||||
|
ctx->data = CMAC_CTX_new();
|
||||||
|
if (!ctx->data)
|
||||||
|
return 0;
|
||||||
|
ctx->keygen_info_count = 0;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int pkey_cmac_copy(EVP_PKEY_CTX *dst, EVP_PKEY_CTX *src)
|
||||||
|
{
|
||||||
|
if (!pkey_cmac_init(dst))
|
||||||
|
return 0;
|
||||||
|
if (!CMAC_CTX_copy(dst->data, src->data))
|
||||||
|
return 0;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void pkey_cmac_cleanup(EVP_PKEY_CTX *ctx)
|
||||||
|
{
|
||||||
|
CMAC_CTX_free(ctx->data);
|
||||||
|
}
|
||||||
|
|
||||||
|
static int pkey_cmac_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
|
||||||
|
{
|
||||||
|
CMAC_CTX *cmkey = CMAC_CTX_new();
|
||||||
|
CMAC_CTX *cmctx = ctx->data;
|
||||||
|
if (!cmkey)
|
||||||
|
return 0;
|
||||||
|
if (!CMAC_CTX_copy(cmkey, cmctx))
|
||||||
|
{
|
||||||
|
CMAC_CTX_free(cmkey);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
EVP_PKEY_assign(pkey, EVP_PKEY_CMAC, cmkey);
|
||||||
|
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int int_update(EVP_MD_CTX *ctx,const void *data,size_t count)
|
||||||
|
{
|
||||||
|
if (!CMAC_Update(ctx->pctx->data, data, count))
|
||||||
|
return 0;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int cmac_signctx_init(EVP_PKEY_CTX *ctx, EVP_MD_CTX *mctx)
|
||||||
|
{
|
||||||
|
EVP_MD_CTX_set_flags(mctx, EVP_MD_CTX_FLAG_NO_INIT);
|
||||||
|
mctx->update = int_update;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int cmac_signctx(EVP_PKEY_CTX *ctx, unsigned char *sig, size_t *siglen,
|
||||||
|
EVP_MD_CTX *mctx)
|
||||||
|
{
|
||||||
|
return CMAC_Final(ctx->data, sig, siglen);
|
||||||
|
}
|
||||||
|
|
||||||
|
static int pkey_cmac_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
|
||||||
|
{
|
||||||
|
CMAC_CTX *cmctx = ctx->data;
|
||||||
|
switch (type)
|
||||||
|
{
|
||||||
|
|
||||||
|
case EVP_PKEY_CTRL_SET_MAC_KEY:
|
||||||
|
if (!p2 || p1 < 0)
|
||||||
|
return 0;
|
||||||
|
if (!CMAC_Init(cmctx, p2, p1, NULL, NULL))
|
||||||
|
return 0;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case EVP_PKEY_CTRL_CIPHER:
|
||||||
|
if (!CMAC_Init(cmctx, NULL, 0, p2, NULL))
|
||||||
|
return 0;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case EVP_PKEY_CTRL_MD:
|
||||||
|
if (ctx->pkey && !CMAC_CTX_copy(ctx->data,
|
||||||
|
(CMAC_CTX *)ctx->pkey->pkey.ptr))
|
||||||
|
return 0;
|
||||||
|
if (!CMAC_Init(cmctx, NULL, 0, NULL, NULL))
|
||||||
|
return 0;
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
return -2;
|
||||||
|
|
||||||
|
}
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int pkey_cmac_ctrl_str(EVP_PKEY_CTX *ctx,
|
||||||
|
const char *type, const char *value)
|
||||||
|
{
|
||||||
|
if (!value)
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
if (!strcmp(type, "key"))
|
||||||
|
{
|
||||||
|
void *p = (void *)value;
|
||||||
|
return pkey_cmac_ctrl(ctx, EVP_PKEY_CTRL_SET_MAC_KEY,
|
||||||
|
strlen(p), p);
|
||||||
|
}
|
||||||
|
if (!strcmp(type, "cipher"))
|
||||||
|
{
|
||||||
|
const EVP_CIPHER *c;
|
||||||
|
c = EVP_get_cipherbyname(value);
|
||||||
|
if (!c)
|
||||||
|
return 0;
|
||||||
|
return pkey_cmac_ctrl(ctx, EVP_PKEY_CTRL_CIPHER, -1, (void *)c);
|
||||||
|
}
|
||||||
|
if (!strcmp(type, "hexkey"))
|
||||||
|
{
|
||||||
|
unsigned char *key;
|
||||||
|
int r;
|
||||||
|
long keylen;
|
||||||
|
key = string_to_hex(value, &keylen);
|
||||||
|
if (!key)
|
||||||
|
return 0;
|
||||||
|
r = pkey_cmac_ctrl(ctx, EVP_PKEY_CTRL_SET_MAC_KEY, keylen, key);
|
||||||
|
OPENSSL_free(key);
|
||||||
|
return r;
|
||||||
|
}
|
||||||
|
return -2;
|
||||||
|
}
|
||||||
|
|
||||||
|
const EVP_PKEY_METHOD cmac_pkey_meth =
|
||||||
|
{
|
||||||
|
EVP_PKEY_CMAC,
|
||||||
|
EVP_PKEY_FLAG_SIGCTX_CUSTOM,
|
||||||
|
pkey_cmac_init,
|
||||||
|
pkey_cmac_copy,
|
||||||
|
pkey_cmac_cleanup,
|
||||||
|
|
||||||
|
0, 0,
|
||||||
|
|
||||||
|
0,
|
||||||
|
pkey_cmac_keygen,
|
||||||
|
|
||||||
|
0, 0,
|
||||||
|
|
||||||
|
0, 0,
|
||||||
|
|
||||||
|
0,0,
|
||||||
|
|
||||||
|
cmac_signctx_init,
|
||||||
|
cmac_signctx,
|
||||||
|
|
||||||
|
0,0,
|
||||||
|
|
||||||
|
0,0,
|
||||||
|
|
||||||
|
0,0,
|
||||||
|
|
||||||
|
0,0,
|
||||||
|
|
||||||
|
pkey_cmac_ctrl,
|
||||||
|
pkey_cmac_ctrl_str
|
||||||
|
|
||||||
|
};
|
@ -122,6 +122,22 @@ void CMAC_CTX_free(CMAC_CTX *ctx)
|
|||||||
OPENSSL_free(ctx);
|
OPENSSL_free(ctx);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int CMAC_CTX_copy(CMAC_CTX *out, const CMAC_CTX *in)
|
||||||
|
{
|
||||||
|
int bl;
|
||||||
|
if (in->nlast_block == -1)
|
||||||
|
return 0;
|
||||||
|
if (!EVP_CIPHER_CTX_copy(&out->cctx, &in->cctx))
|
||||||
|
return 0;
|
||||||
|
bl = EVP_CIPHER_CTX_block_size(&in->cctx);
|
||||||
|
memcpy(out->k1, in->k1, bl);
|
||||||
|
memcpy(out->k2, in->k2, bl);
|
||||||
|
memcpy(out->tbl, in->tbl, bl);
|
||||||
|
memcpy(out->last_block, in->last_block, bl);
|
||||||
|
out->nlast_block = in->nlast_block;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
int CMAC_Init(CMAC_CTX *ctx, const void *key, size_t keylen,
|
int CMAC_Init(CMAC_CTX *ctx, const void *key, size_t keylen,
|
||||||
const EVP_CIPHER *cipher, ENGINE *impl)
|
const EVP_CIPHER *cipher, ENGINE *impl)
|
||||||
{
|
{
|
||||||
@ -130,11 +146,11 @@ int CMAC_Init(CMAC_CTX *ctx, const void *key, size_t keylen,
|
|||||||
if (!key && !cipher && !impl && keylen == 0)
|
if (!key && !cipher && !impl && keylen == 0)
|
||||||
{
|
{
|
||||||
/* Not initialised */
|
/* Not initialised */
|
||||||
if (ctx->last_block == -1)
|
if (ctx->nlast_block == -1)
|
||||||
return 0;
|
return 0;
|
||||||
if (!EVP_EncryptInit_ex(&ctx->cctx, NULL, NULL, NULL, zero_iv))
|
if (!EVP_EncryptInit_ex(&ctx->cctx, NULL, NULL, NULL, zero_iv))
|
||||||
return 0;
|
return 0;
|
||||||
return 0;
|
return 1;
|
||||||
}
|
}
|
||||||
/* Initialiase context */
|
/* Initialiase context */
|
||||||
if (cipher && !EVP_EncryptInit_ex(&ctx->cctx, cipher, impl, NULL, NULL))
|
if (cipher && !EVP_EncryptInit_ex(&ctx->cctx, cipher, impl, NULL, NULL))
|
||||||
@ -158,6 +174,8 @@ int CMAC_Init(CMAC_CTX *ctx, const void *key, size_t keylen,
|
|||||||
/* Reset context again ready for first data block */
|
/* Reset context again ready for first data block */
|
||||||
if (!EVP_EncryptInit_ex(&ctx->cctx, NULL, NULL, NULL, zero_iv))
|
if (!EVP_EncryptInit_ex(&ctx->cctx, NULL, NULL, NULL, zero_iv))
|
||||||
return 0;
|
return 0;
|
||||||
|
/* Zero tbl so resume works */
|
||||||
|
memset(ctx->tbl, 0, bl);
|
||||||
ctx->nlast_block = 0;
|
ctx->nlast_block = 0;
|
||||||
}
|
}
|
||||||
return 1;
|
return 1;
|
||||||
@ -205,18 +223,21 @@ int CMAC_Update(CMAC_CTX *ctx, const void *in, size_t dlen)
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t CMAC_Final(CMAC_CTX *ctx, unsigned char *out)
|
int CMAC_Final(CMAC_CTX *ctx, unsigned char *out, size_t *poutlen)
|
||||||
{
|
{
|
||||||
int i, bl, lb;
|
int i, bl, lb;
|
||||||
if (ctx->nlast_block == -1)
|
if (ctx->nlast_block == -1)
|
||||||
return 0;
|
return 0;
|
||||||
bl = EVP_CIPHER_CTX_block_size(&ctx->cctx);
|
bl = EVP_CIPHER_CTX_block_size(&ctx->cctx);
|
||||||
|
*poutlen = (size_t)bl;
|
||||||
|
if (!out)
|
||||||
|
return 1;
|
||||||
lb = ctx->nlast_block;
|
lb = ctx->nlast_block;
|
||||||
/* Is last block complete? */
|
/* Is last block complete? */
|
||||||
if (lb == bl)
|
if (lb == bl)
|
||||||
{
|
{
|
||||||
for (i = 0; i < bl; i++)
|
for (i = 0; i < bl; i++)
|
||||||
ctx->last_block[i] ^= ctx->k1[i];
|
out[i] = ctx->last_block[i] ^ ctx->k1[i];
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@ -224,9 +245,25 @@ size_t CMAC_Final(CMAC_CTX *ctx, unsigned char *out)
|
|||||||
if (bl - lb > 1)
|
if (bl - lb > 1)
|
||||||
memset(ctx->last_block + lb + 1, 0, bl - lb - 1);
|
memset(ctx->last_block + lb + 1, 0, bl - lb - 1);
|
||||||
for (i = 0; i < bl; i++)
|
for (i = 0; i < bl; i++)
|
||||||
ctx->last_block[i] ^= ctx->k2[i];
|
out[i] = ctx->last_block[i] ^ ctx->k2[i];
|
||||||
}
|
}
|
||||||
if (!EVP_Cipher(&ctx->cctx, out, ctx->last_block, bl))
|
if (!EVP_Cipher(&ctx->cctx, out, out, bl))
|
||||||
|
{
|
||||||
|
OPENSSL_cleanse(out, bl);
|
||||||
return 0;
|
return 0;
|
||||||
return bl;
|
}
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int CMAC_resume(CMAC_CTX *ctx)
|
||||||
|
{
|
||||||
|
if (ctx->nlast_block == -1)
|
||||||
|
return 0;
|
||||||
|
/* The buffer "tbl" containes the last fully encrypted block
|
||||||
|
* which is the last IV (or all zeroes if no last encrypted block).
|
||||||
|
* The last block has not been modified since CMAC_final().
|
||||||
|
* So reinitliasing using the last decrypted block will allow
|
||||||
|
* CMAC to continue after calling CMAC_Final().
|
||||||
|
*/
|
||||||
|
return EVP_EncryptInit_ex(&ctx->cctx, NULL, NULL, NULL, ctx->tbl);
|
||||||
}
|
}
|
||||||
|
@ -68,11 +68,13 @@ CMAC_CTX *CMAC_CTX_new(void);
|
|||||||
void CMAC_CTX_cleanup(CMAC_CTX *ctx);
|
void CMAC_CTX_cleanup(CMAC_CTX *ctx);
|
||||||
void CMAC_CTX_free(CMAC_CTX *ctx);
|
void CMAC_CTX_free(CMAC_CTX *ctx);
|
||||||
EVP_CIPHER_CTX *CMAC_CTX_get0_cipher_ctx(CMAC_CTX *ctx);
|
EVP_CIPHER_CTX *CMAC_CTX_get0_cipher_ctx(CMAC_CTX *ctx);
|
||||||
|
int CMAC_CTX_copy(CMAC_CTX *out, const CMAC_CTX *in);
|
||||||
|
|
||||||
int CMAC_Init(CMAC_CTX *ctx, const void *key, size_t keylen,
|
int CMAC_Init(CMAC_CTX *ctx, const void *key, size_t keylen,
|
||||||
const EVP_CIPHER *cipher, ENGINE *impl);
|
const EVP_CIPHER *cipher, ENGINE *impl);
|
||||||
int CMAC_Update(CMAC_CTX *ctx, const void *data, size_t dlen);
|
int CMAC_Update(CMAC_CTX *ctx, const void *data, size_t dlen);
|
||||||
int CMAC_Final(CMAC_CTX *ctx, unsigned char *out);
|
int CMAC_Final(CMAC_CTX *ctx, unsigned char *out, size_t *poutlen);
|
||||||
|
int CMAC_resume(CMAC_CTX *ctx);
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
|
@ -116,6 +116,7 @@
|
|||||||
#define EVP_PKEY_DH NID_dhKeyAgreement
|
#define EVP_PKEY_DH NID_dhKeyAgreement
|
||||||
#define EVP_PKEY_EC NID_X9_62_id_ecPublicKey
|
#define EVP_PKEY_EC NID_X9_62_id_ecPublicKey
|
||||||
#define EVP_PKEY_HMAC NID_hmac
|
#define EVP_PKEY_HMAC NID_hmac
|
||||||
|
#define EVP_PKEY_CMAC NID_cmac
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
extern "C" {
|
extern "C" {
|
||||||
@ -1042,10 +1043,16 @@ void EVP_PKEY_asn1_set_ctrl(EVP_PKEY_ASN1_METHOD *ameth,
|
|||||||
#define EVP_PKEY_CTRL_CMS_DECRYPT 10
|
#define EVP_PKEY_CTRL_CMS_DECRYPT 10
|
||||||
#define EVP_PKEY_CTRL_CMS_SIGN 11
|
#define EVP_PKEY_CTRL_CMS_SIGN 11
|
||||||
|
|
||||||
|
#define EVP_PKEY_CTRL_CIPHER 12
|
||||||
|
|
||||||
#define EVP_PKEY_ALG_CTRL 0x1000
|
#define EVP_PKEY_ALG_CTRL 0x1000
|
||||||
|
|
||||||
|
|
||||||
#define EVP_PKEY_FLAG_AUTOARGLEN 2
|
#define EVP_PKEY_FLAG_AUTOARGLEN 2
|
||||||
|
/* Method handles all operations: don't assume any digest related
|
||||||
|
* defaults.
|
||||||
|
*/
|
||||||
|
#define EVP_PKEY_FLAG_SIGCTX_CUSTOM 4
|
||||||
|
|
||||||
const EVP_PKEY_METHOD *EVP_PKEY_meth_find(int type);
|
const EVP_PKEY_METHOD *EVP_PKEY_meth_find(int type);
|
||||||
EVP_PKEY_METHOD* EVP_PKEY_meth_new(int id, int flags);
|
EVP_PKEY_METHOD* EVP_PKEY_meth_new(int id, int flags);
|
||||||
|
@ -72,17 +72,21 @@ static int do_sigver_init(EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx,
|
|||||||
if (ctx->pctx == NULL)
|
if (ctx->pctx == NULL)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
if (type == NULL)
|
if (!(ctx->pctx->pmeth->flags & EVP_PKEY_FLAG_SIGCTX_CUSTOM))
|
||||||
{
|
{
|
||||||
int def_nid;
|
|
||||||
if (EVP_PKEY_get_default_digest_nid(pkey, &def_nid) > 0)
|
|
||||||
type = EVP_get_digestbynid(def_nid);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (type == NULL)
|
if (type == NULL)
|
||||||
{
|
{
|
||||||
EVPerr(EVP_F_DO_SIGVER_INIT, EVP_R_NO_DEFAULT_DIGEST);
|
int def_nid;
|
||||||
return 0;
|
if (EVP_PKEY_get_default_digest_nid(pkey, &def_nid) > 0)
|
||||||
|
type = EVP_get_digestbynid(def_nid);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (type == NULL)
|
||||||
|
{
|
||||||
|
EVPerr(EVP_F_DO_SIGVER_INIT, EVP_R_NO_DEFAULT_DIGEST);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ver)
|
if (ver)
|
||||||
@ -109,8 +113,8 @@ static int do_sigver_init(EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx,
|
|||||||
}
|
}
|
||||||
if (EVP_PKEY_CTX_set_signature_md(ctx->pctx, type) <= 0)
|
if (EVP_PKEY_CTX_set_signature_md(ctx->pctx, type) <= 0)
|
||||||
return 0;
|
return 0;
|
||||||
if (pctx)
|
if (ctx->pctx->pmeth->flags & EVP_PKEY_FLAG_SIGCTX_CUSTOM)
|
||||||
*pctx = ctx->pctx;
|
return 1;
|
||||||
if (!EVP_DigestInit_ex(ctx, type, e))
|
if (!EVP_DigestInit_ex(ctx, type, e))
|
||||||
return 0;
|
return 0;
|
||||||
return 1;
|
return 1;
|
||||||
@ -131,7 +135,20 @@ int EVP_DigestVerifyInit(EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx,
|
|||||||
int EVP_DigestSignFinal(EVP_MD_CTX *ctx, unsigned char *sigret, size_t *siglen)
|
int EVP_DigestSignFinal(EVP_MD_CTX *ctx, unsigned char *sigret, size_t *siglen)
|
||||||
{
|
{
|
||||||
int sctx, r = 0;
|
int sctx, r = 0;
|
||||||
if (ctx->pctx->pmeth->signctx)
|
EVP_PKEY_CTX *pctx = ctx->pctx;
|
||||||
|
if (pctx->pmeth->flags & EVP_PKEY_FLAG_SIGCTX_CUSTOM)
|
||||||
|
{
|
||||||
|
EVP_PKEY_CTX *dctx;
|
||||||
|
if (!sigret)
|
||||||
|
return pctx->pmeth->signctx(pctx, sigret, siglen, ctx);
|
||||||
|
dctx = EVP_PKEY_CTX_dup(ctx->pctx);
|
||||||
|
if (!dctx)
|
||||||
|
return 0;
|
||||||
|
r = dctx->pmeth->signctx(dctx, sigret, siglen, ctx);
|
||||||
|
EVP_PKEY_CTX_free(dctx);
|
||||||
|
return r;
|
||||||
|
}
|
||||||
|
if (pctx->pmeth->signctx)
|
||||||
sctx = 1;
|
sctx = 1;
|
||||||
else
|
else
|
||||||
sctx = 0;
|
sctx = 0;
|
||||||
@ -158,13 +175,13 @@ int EVP_DigestSignFinal(EVP_MD_CTX *ctx, unsigned char *sigret, size_t *siglen)
|
|||||||
{
|
{
|
||||||
if (sctx)
|
if (sctx)
|
||||||
{
|
{
|
||||||
if (ctx->pctx->pmeth->signctx(ctx->pctx, sigret, siglen, ctx) <= 0)
|
if (pctx->pmeth->signctx(pctx, sigret, siglen, ctx) <= 0)
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
int s = EVP_MD_size(ctx->digest);
|
int s = EVP_MD_size(ctx->digest);
|
||||||
if (s < 0 || EVP_PKEY_sign(ctx->pctx, sigret, siglen, NULL, s) <= 0)
|
if (s < 0 || EVP_PKEY_sign(pctx, sigret, siglen, NULL, s) <= 0)
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -73,7 +73,7 @@ DECLARE_STACK_OF(EVP_PKEY_METHOD)
|
|||||||
STACK_OF(EVP_PKEY_METHOD) *app_pkey_methods = NULL;
|
STACK_OF(EVP_PKEY_METHOD) *app_pkey_methods = NULL;
|
||||||
|
|
||||||
extern const EVP_PKEY_METHOD rsa_pkey_meth, dh_pkey_meth, dsa_pkey_meth;
|
extern const EVP_PKEY_METHOD rsa_pkey_meth, dh_pkey_meth, dsa_pkey_meth;
|
||||||
extern const EVP_PKEY_METHOD ec_pkey_meth, hmac_pkey_meth;
|
extern const EVP_PKEY_METHOD ec_pkey_meth, hmac_pkey_meth, cmac_pkey_meth;
|
||||||
|
|
||||||
static const EVP_PKEY_METHOD *standard_methods[] =
|
static const EVP_PKEY_METHOD *standard_methods[] =
|
||||||
{
|
{
|
||||||
@ -90,6 +90,7 @@ static const EVP_PKEY_METHOD *standard_methods[] =
|
|||||||
&ec_pkey_meth,
|
&ec_pkey_meth,
|
||||||
#endif
|
#endif
|
||||||
&hmac_pkey_meth,
|
&hmac_pkey_meth,
|
||||||
|
&cmac_pkey_meth
|
||||||
};
|
};
|
||||||
|
|
||||||
DECLARE_OBJ_BSEARCH_CMP_FN(const EVP_PKEY_METHOD *, const EVP_PKEY_METHOD *,
|
DECLARE_OBJ_BSEARCH_CMP_FN(const EVP_PKEY_METHOD *, const EVP_PKEY_METHOD *,
|
||||||
|
@ -62,9 +62,9 @@
|
|||||||
* [including the GNU Public Licence.]
|
* [including the GNU Public Licence.]
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#define NUM_NID 894
|
#define NUM_NID 895
|
||||||
#define NUM_SN 887
|
#define NUM_SN 888
|
||||||
#define NUM_LN 887
|
#define NUM_LN 888
|
||||||
#define NUM_OBJ 841
|
#define NUM_OBJ 841
|
||||||
|
|
||||||
static const unsigned char lvalues[5835]={
|
static const unsigned char lvalues[5835]={
|
||||||
@ -2354,6 +2354,7 @@ static const ASN1_OBJECT nid_objs[NUM_NID]={
|
|||||||
{"dmdName","dmdName",NID_dmdName,3,&(lvalues[5820]),0},
|
{"dmdName","dmdName",NID_dmdName,3,&(lvalues[5820]),0},
|
||||||
{"id-alg-PWRI-KEK","id-alg-PWRI-KEK",NID_id_alg_PWRI_KEK,11,
|
{"id-alg-PWRI-KEK","id-alg-PWRI-KEK",NID_id_alg_PWRI_KEK,11,
|
||||||
&(lvalues[5823]),0},
|
&(lvalues[5823]),0},
|
||||||
|
{"CMAC","cmac",NID_cmac,0,NULL,0},
|
||||||
};
|
};
|
||||||
|
|
||||||
static const unsigned int sn_objs[NUM_SN]={
|
static const unsigned int sn_objs[NUM_SN]={
|
||||||
@ -2403,6 +2404,7 @@ static const unsigned int sn_objs[NUM_SN]={
|
|||||||
110, /* "CAST5-CFB" */
|
110, /* "CAST5-CFB" */
|
||||||
109, /* "CAST5-ECB" */
|
109, /* "CAST5-ECB" */
|
||||||
111, /* "CAST5-OFB" */
|
111, /* "CAST5-OFB" */
|
||||||
|
894, /* "CMAC" */
|
||||||
13, /* "CN" */
|
13, /* "CN" */
|
||||||
141, /* "CRLReason" */
|
141, /* "CRLReason" */
|
||||||
417, /* "CSPName" */
|
417, /* "CSPName" */
|
||||||
@ -3471,6 +3473,7 @@ static const unsigned int ln_objs[NUM_LN]={
|
|||||||
407, /* "characteristic-two-field" */
|
407, /* "characteristic-two-field" */
|
||||||
395, /* "clearance" */
|
395, /* "clearance" */
|
||||||
633, /* "cleartext track 2" */
|
633, /* "cleartext track 2" */
|
||||||
|
894, /* "cmac" */
|
||||||
13, /* "commonName" */
|
13, /* "commonName" */
|
||||||
513, /* "content types" */
|
513, /* "content types" */
|
||||||
50, /* "contentType" */
|
50, /* "contentType" */
|
||||||
|
@ -3916,3 +3916,7 @@
|
|||||||
#define LN_hmac "hmac"
|
#define LN_hmac "hmac"
|
||||||
#define NID_hmac 855
|
#define NID_hmac 855
|
||||||
|
|
||||||
|
#define SN_cmac "CMAC"
|
||||||
|
#define LN_cmac "cmac"
|
||||||
|
#define NID_cmac 894
|
||||||
|
|
||||||
|
@ -891,3 +891,4 @@ supportedAlgorithms 890
|
|||||||
deltaRevocationList 891
|
deltaRevocationList 891
|
||||||
dmdName 892
|
dmdName 892
|
||||||
id_alg_PWRI_KEK 893
|
id_alg_PWRI_KEK 893
|
||||||
|
cmac 894
|
||||||
|
@ -1258,3 +1258,5 @@ kisa 1 6 : SEED-OFB : seed-ofb
|
|||||||
# There is no OID that just denotes "HMAC" oddly enough...
|
# There is no OID that just denotes "HMAC" oddly enough...
|
||||||
|
|
||||||
: HMAC : hmac
|
: HMAC : hmac
|
||||||
|
# Nor CMAC either
|
||||||
|
: CMAC : cmac
|
||||||
|
Loading…
x
Reference in New Issue
Block a user