|
Packit Service |
084de1 |
/* fips/rand/fips_drbg_hmac.c */
|
|
Packit Service |
084de1 |
/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
|
|
Packit Service |
084de1 |
* project.
|
|
Packit Service |
084de1 |
*/
|
|
Packit Service |
084de1 |
/* ====================================================================
|
|
Packit Service |
084de1 |
* Copyright (c) 2011 The OpenSSL Project. All rights reserved.
|
|
Packit Service |
084de1 |
*
|
|
Packit Service |
084de1 |
* Redistribution and use in source and binary forms, with or without
|
|
Packit Service |
084de1 |
* modification, are permitted provided that the following conditions
|
|
Packit Service |
084de1 |
* are met:
|
|
Packit Service |
084de1 |
*
|
|
Packit Service |
084de1 |
* 1. Redistributions of source code must retain the above copyright
|
|
Packit Service |
084de1 |
* notice, this list of conditions and the following disclaimer.
|
|
Packit Service |
084de1 |
*
|
|
Packit Service |
084de1 |
* 2. Redistributions in binary form must reproduce the above copyright
|
|
Packit Service |
084de1 |
* notice, this list of conditions and the following disclaimer in
|
|
Packit Service |
084de1 |
* the documentation and/or other materials provided with the
|
|
Packit Service |
084de1 |
* distribution.
|
|
Packit Service |
084de1 |
*
|
|
Packit Service |
084de1 |
* 3. All advertising materials mentioning features or use of this
|
|
Packit Service |
084de1 |
* software must display the following acknowledgment:
|
|
Packit Service |
084de1 |
* "This product includes software developed by the OpenSSL Project
|
|
Packit Service |
084de1 |
* for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
|
|
Packit Service |
084de1 |
*
|
|
Packit Service |
084de1 |
* 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
|
|
Packit Service |
084de1 |
* endorse or promote products derived from this software without
|
|
Packit Service |
084de1 |
* prior written permission. For written permission, please contact
|
|
Packit Service |
084de1 |
* licensing@OpenSSL.org.
|
|
Packit Service |
084de1 |
*
|
|
Packit Service |
084de1 |
* 5. Products derived from this software may not be called "OpenSSL"
|
|
Packit Service |
084de1 |
* nor may "OpenSSL" appear in their names without prior written
|
|
Packit Service |
084de1 |
* permission of the OpenSSL Project.
|
|
Packit Service |
084de1 |
*
|
|
Packit Service |
084de1 |
* 6. Redistributions of any form whatsoever must retain the following
|
|
Packit Service |
084de1 |
* acknowledgment:
|
|
Packit Service |
084de1 |
* "This product includes software developed by the OpenSSL Project
|
|
Packit Service |
084de1 |
* for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
|
|
Packit Service |
084de1 |
*
|
|
Packit Service |
084de1 |
* THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
|
|
Packit Service |
084de1 |
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
Packit Service |
084de1 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|
Packit Service |
084de1 |
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
|
|
Packit Service |
084de1 |
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
Packit Service |
084de1 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
Packit Service |
084de1 |
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
Packit Service |
084de1 |
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
Packit Service |
084de1 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
|
Packit Service |
084de1 |
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
Packit Service |
084de1 |
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
|
|
Packit Service |
084de1 |
* OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
Packit Service |
084de1 |
* ====================================================================
|
|
Packit Service |
084de1 |
*/
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
#include <stdlib.h>
|
|
Packit Service |
084de1 |
#include <string.h>
|
|
Packit Service |
084de1 |
#include <openssl/crypto.h>
|
|
Packit Service |
084de1 |
#include <openssl/evp.h>
|
|
Packit Service |
084de1 |
#include <openssl/hmac.h>
|
|
Packit Service |
084de1 |
#include <openssl/aes.h>
|
|
Packit Service |
084de1 |
#include <openssl/fips.h>
|
|
Packit Service |
084de1 |
#include <openssl/fips_rand.h>
|
|
Packit Service |
084de1 |
#include "fips_rand_lcl.h"
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
static int drbg_hmac_update(DRBG_CTX *dctx,
|
|
Packit Service |
084de1 |
const unsigned char *in1, size_t in1len,
|
|
Packit Service |
084de1 |
const unsigned char *in2, size_t in2len,
|
|
Packit Service |
084de1 |
const unsigned char *in3, size_t in3len)
|
|
Packit Service |
084de1 |
{
|
|
Packit Service |
084de1 |
static unsigned char c0 = 0, c1 = 1;
|
|
Packit Service |
084de1 |
DRBG_HMAC_CTX *hmac = &dctx->d.hmac;
|
|
Packit Service |
084de1 |
HMAC_CTX *hctx = hmac->hctx;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
if (!HMAC_Init_ex(hctx, hmac->K, dctx->blocklength, hmac->md, NULL))
|
|
Packit Service |
084de1 |
return 0;
|
|
Packit Service |
084de1 |
if (!HMAC_Update(hctx, hmac->V, dctx->blocklength))
|
|
Packit Service |
084de1 |
return 0;
|
|
Packit Service |
084de1 |
if (!HMAC_Update(hctx, &c0, 1))
|
|
Packit Service |
084de1 |
return 0;
|
|
Packit Service |
084de1 |
if (in1len && !HMAC_Update(hctx, in1, in1len))
|
|
Packit Service |
084de1 |
return 0;
|
|
Packit Service |
084de1 |
if (in2len && !HMAC_Update(hctx, in2, in2len))
|
|
Packit Service |
084de1 |
return 0;
|
|
Packit Service |
084de1 |
if (in3len && !HMAC_Update(hctx, in3, in3len))
|
|
Packit Service |
084de1 |
return 0;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
if (!HMAC_Final(hctx, hmac->K, NULL))
|
|
Packit Service |
084de1 |
return 0;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
if (!HMAC_Init_ex(hctx, hmac->K, dctx->blocklength, hmac->md, NULL))
|
|
Packit Service |
084de1 |
return 0;
|
|
Packit Service |
084de1 |
if (!HMAC_Update(hctx, hmac->V, dctx->blocklength))
|
|
Packit Service |
084de1 |
return 0;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
if (!HMAC_Final(hctx, hmac->V, NULL))
|
|
Packit Service |
084de1 |
return 0;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
if (!in1len && !in2len && !in3len)
|
|
Packit Service |
084de1 |
return 1;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
if (!HMAC_Init_ex(hctx, hmac->K, dctx->blocklength, hmac->md, NULL))
|
|
Packit Service |
084de1 |
return 0;
|
|
Packit Service |
084de1 |
if (!HMAC_Update(hctx, hmac->V, dctx->blocklength))
|
|
Packit Service |
084de1 |
return 0;
|
|
Packit Service |
084de1 |
if (!HMAC_Update(hctx, &c1, 1))
|
|
Packit Service |
084de1 |
return 0;
|
|
Packit Service |
084de1 |
if (in1len && !HMAC_Update(hctx, in1, in1len))
|
|
Packit Service |
084de1 |
return 0;
|
|
Packit Service |
084de1 |
if (in2len && !HMAC_Update(hctx, in2, in2len))
|
|
Packit Service |
084de1 |
return 0;
|
|
Packit Service |
084de1 |
if (in3len && !HMAC_Update(hctx, in3, in3len))
|
|
Packit Service |
084de1 |
return 0;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
if (!HMAC_Final(hctx, hmac->K, NULL))
|
|
Packit Service |
084de1 |
return 0;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
if (!HMAC_Init_ex(hctx, hmac->K, dctx->blocklength, hmac->md, NULL))
|
|
Packit Service |
084de1 |
return 0;
|
|
Packit Service |
084de1 |
if (!HMAC_Update(hctx, hmac->V, dctx->blocklength))
|
|
Packit Service |
084de1 |
return 0;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
if (!HMAC_Final(hctx, hmac->V, NULL))
|
|
Packit Service |
084de1 |
return 0;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
return 1;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
static int drbg_hmac_instantiate(DRBG_CTX *dctx,
|
|
Packit Service |
084de1 |
const unsigned char *ent, size_t ent_len,
|
|
Packit Service |
084de1 |
const unsigned char *nonce, size_t nonce_len,
|
|
Packit Service |
084de1 |
const unsigned char *pstr, size_t pstr_len)
|
|
Packit Service |
084de1 |
{
|
|
Packit Service |
084de1 |
DRBG_HMAC_CTX *hmac = &dctx->d.hmac;
|
|
Packit Service |
084de1 |
memset(hmac->K, 0, dctx->blocklength);
|
|
Packit Service |
084de1 |
memset(hmac->V, 1, dctx->blocklength);
|
|
Packit Service |
084de1 |
if (!drbg_hmac_update(dctx,
|
|
Packit Service |
084de1 |
ent, ent_len, nonce, nonce_len, pstr, pstr_len))
|
|
Packit Service |
084de1 |
return 0;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
#ifdef HMAC_DRBG_TRACE
|
|
Packit Service |
084de1 |
fprintf(stderr, "K+V after instantiate:\n");
|
|
Packit Service |
084de1 |
hexprint(stderr, hmac->K, hmac->blocklength);
|
|
Packit Service |
084de1 |
hexprint(stderr, hmac->V, hmac->blocklength);
|
|
Packit Service |
084de1 |
#endif
|
|
Packit Service |
084de1 |
return 1;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
static int drbg_hmac_reseed(DRBG_CTX *dctx,
|
|
Packit Service |
084de1 |
const unsigned char *ent, size_t ent_len,
|
|
Packit Service |
084de1 |
const unsigned char *adin, size_t adin_len)
|
|
Packit Service |
084de1 |
{
|
|
Packit Service |
084de1 |
if (!drbg_hmac_update(dctx, ent, ent_len, adin, adin_len, NULL, 0))
|
|
Packit Service |
084de1 |
return 0;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
#ifdef HMAC_DRBG_TRACE
|
|
Packit Service |
084de1 |
{
|
|
Packit Service |
084de1 |
DRBG_HMAC_CTX *hmac = &dctx->d.hmac;
|
|
Packit Service |
084de1 |
fprintf(stderr, "K+V after reseed:\n");
|
|
Packit Service |
084de1 |
hexprint(stderr, hmac->K, hmac->blocklength);
|
|
Packit Service |
084de1 |
hexprint(stderr, hmac->V, hmac->blocklength);
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
#endif
|
|
Packit Service |
084de1 |
return 1;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
static int drbg_hmac_generate(DRBG_CTX *dctx,
|
|
Packit Service |
084de1 |
unsigned char *out, size_t outlen,
|
|
Packit Service |
084de1 |
const unsigned char *adin, size_t adin_len)
|
|
Packit Service |
084de1 |
{
|
|
Packit Service |
084de1 |
DRBG_HMAC_CTX *hmac = &dctx->d.hmac;
|
|
Packit Service |
084de1 |
HMAC_CTX *hctx = hmac->hctx;
|
|
Packit Service |
084de1 |
const unsigned char *Vtmp = hmac->V;
|
|
Packit Service |
084de1 |
if (adin_len && !drbg_hmac_update(dctx, adin, adin_len, NULL, 0, NULL, 0))
|
|
Packit Service |
084de1 |
return 0;
|
|
Packit Service |
084de1 |
for (;;) {
|
|
Packit Service |
084de1 |
if (!HMAC_Init_ex(hctx, hmac->K, dctx->blocklength, hmac->md, NULL))
|
|
Packit Service |
084de1 |
return 0;
|
|
Packit Service |
084de1 |
if (!HMAC_Update(hctx, Vtmp, dctx->blocklength))
|
|
Packit Service |
084de1 |
return 0;
|
|
Packit Service |
084de1 |
if (outlen > dctx->blocklength) {
|
|
Packit Service |
084de1 |
if (!HMAC_Final(hctx, out, NULL))
|
|
Packit Service |
084de1 |
return 0;
|
|
Packit Service |
084de1 |
Vtmp = out;
|
|
Packit Service |
084de1 |
} else {
|
|
Packit Service |
084de1 |
if (!HMAC_Final(hctx, hmac->V, NULL))
|
|
Packit Service |
084de1 |
return 0;
|
|
Packit Service |
084de1 |
memcpy(out, hmac->V, outlen);
|
|
Packit Service |
084de1 |
break;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
out += dctx->blocklength;
|
|
Packit Service |
084de1 |
outlen -= dctx->blocklength;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
if (!drbg_hmac_update(dctx, adin, adin_len, NULL, 0, NULL, 0))
|
|
Packit Service |
084de1 |
return 0;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
return 1;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
static int drbg_hmac_uninstantiate(DRBG_CTX *dctx)
|
|
Packit Service |
084de1 |
{
|
|
Packit Service |
084de1 |
HMAC_CTX_free(dctx->d.hmac.hctx);
|
|
Packit Service |
084de1 |
OPENSSL_cleanse(&dctx->d.hmac, sizeof(DRBG_HMAC_CTX));
|
|
Packit Service |
084de1 |
return 1;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
int fips_drbg_hmac_init(DRBG_CTX *dctx)
|
|
Packit Service |
084de1 |
{
|
|
Packit Service |
084de1 |
const EVP_MD *md = NULL;
|
|
Packit Service |
084de1 |
DRBG_HMAC_CTX *hctx = &dctx->d.hmac;
|
|
Packit Service |
084de1 |
dctx->strength = 256;
|
|
Packit Service |
084de1 |
switch (dctx->type) {
|
|
Packit Service |
084de1 |
case NID_hmacWithSHA1:
|
|
Packit Service |
084de1 |
md = EVP_sha1();
|
|
Packit Service |
084de1 |
dctx->strength = 128;
|
|
Packit Service |
084de1 |
break;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
case NID_hmacWithSHA224:
|
|
Packit Service |
084de1 |
md = EVP_sha224();
|
|
Packit Service |
084de1 |
dctx->strength = 192;
|
|
Packit Service |
084de1 |
break;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
case NID_hmacWithSHA256:
|
|
Packit Service |
084de1 |
md = EVP_sha256();
|
|
Packit Service |
084de1 |
break;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
case NID_hmacWithSHA384:
|
|
Packit Service |
084de1 |
md = EVP_sha384();
|
|
Packit Service |
084de1 |
break;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
case NID_hmacWithSHA512:
|
|
Packit Service |
084de1 |
md = EVP_sha512();
|
|
Packit Service |
084de1 |
break;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
default:
|
|
Packit Service |
084de1 |
dctx->strength = 0;
|
|
Packit Service |
084de1 |
return -2;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
dctx->instantiate = drbg_hmac_instantiate;
|
|
Packit Service |
084de1 |
dctx->reseed = drbg_hmac_reseed;
|
|
Packit Service |
084de1 |
dctx->generate = drbg_hmac_generate;
|
|
Packit Service |
084de1 |
dctx->uninstantiate = drbg_hmac_uninstantiate;
|
|
Packit Service |
084de1 |
hctx->hctx = HMAC_CTX_new();
|
|
Packit Service |
084de1 |
if (hctx->hctx == NULL)
|
|
Packit Service |
084de1 |
return -1;
|
|
Packit Service |
084de1 |
hctx->md = md;
|
|
Packit Service |
084de1 |
dctx->blocklength = M_EVP_MD_size(md);
|
|
Packit Service |
084de1 |
dctx->seedlen = M_EVP_MD_size(md);
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
dctx->min_entropy = dctx->strength / 8;
|
|
Packit Service |
084de1 |
dctx->max_entropy = DRBG_MAX_LENGTH;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
dctx->min_nonce = dctx->min_entropy / 2;
|
|
Packit Service |
084de1 |
dctx->max_nonce = DRBG_MAX_LENGTH;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
dctx->max_pers = DRBG_MAX_LENGTH;
|
|
Packit Service |
084de1 |
dctx->max_adin = DRBG_MAX_LENGTH;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
dctx->max_request = 1 << 16;
|
|
Packit Service |
084de1 |
dctx->reseed_interval = 1 << 24;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
return 1;
|
|
Packit Service |
084de1 |
}
|