Blame crypto/fips/fips_drbg_hmac.c

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