The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/geom/eli/g_eli_hmac.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*-
    2  * Copyright (c) 2005-2010 Pawel Jakub Dawidek <pjd@FreeBSD.org>
    3  * All rights reserved.
    4  *
    5  * Redistribution and use in source and binary forms, with or without
    6  * modification, are permitted provided that the following conditions
    7  * are met:
    8  * 1. Redistributions of source code must retain the above copyright
    9  *    notice, this list of conditions and the following disclaimer.
   10  * 2. Redistributions in binary form must reproduce the above copyright
   11  *    notice, this list of conditions and the following disclaimer in the
   12  *    documentation and/or other materials provided with the distribution.
   13  *
   14  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
   15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
   18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   24  * SUCH DAMAGE.
   25  */
   26 
   27 #include <sys/cdefs.h>
   28 __FBSDID("$FreeBSD: releng/11.2/sys/geom/eli/g_eli_hmac.c 329175 2018-02-12 17:44:35Z kevans $");
   29 
   30 #include <sys/param.h>
   31 #ifdef _KERNEL
   32 #include <sys/systm.h>
   33 #include <sys/kernel.h>
   34 #include <sys/malloc.h>
   35 #elif defined(_STANDALONE)
   36 #include "stand.h"
   37 #else
   38 #include <stdint.h>
   39 #include <string.h>
   40 #include <strings.h>
   41 #include <errno.h>
   42 #include <assert.h>
   43 #include <openssl/evp.h>
   44 #define _OpenSSL_
   45 #endif
   46 #include <geom/eli/g_eli.h>
   47 
   48 void
   49 g_eli_crypto_hmac_init(struct hmac_ctx *ctx, const uint8_t *hkey,
   50     size_t hkeylen)
   51 {
   52         u_char k_ipad[128], k_opad[128], key[128];
   53         SHA512_CTX lctx;
   54         u_int i;
   55 
   56         bzero(key, sizeof(key));
   57         if (hkeylen == 0)
   58                 ; /* do nothing */
   59         else if (hkeylen <= 128)
   60                 bcopy(hkey, key, hkeylen);
   61         else {
   62                 /* If key is longer than 128 bytes reset it to key = SHA512(key). */
   63                 SHA512_Init(&lctx);
   64                 SHA512_Update(&lctx, hkey, hkeylen);
   65                 SHA512_Final(key, &lctx);
   66         }
   67 
   68         /* XOR key with ipad and opad values. */
   69         for (i = 0; i < sizeof(key); i++) {
   70                 k_ipad[i] = key[i] ^ 0x36;
   71                 k_opad[i] = key[i] ^ 0x5c;
   72         }
   73         explicit_bzero(key, sizeof(key));
   74         /* Start inner SHA512. */
   75         SHA512_Init(&ctx->innerctx);
   76         SHA512_Update(&ctx->innerctx, k_ipad, sizeof(k_ipad));
   77         explicit_bzero(k_ipad, sizeof(k_ipad));
   78         /* Start outer SHA512. */
   79         SHA512_Init(&ctx->outerctx);
   80         SHA512_Update(&ctx->outerctx, k_opad, sizeof(k_opad));
   81         explicit_bzero(k_opad, sizeof(k_opad));
   82 }
   83 
   84 void
   85 g_eli_crypto_hmac_update(struct hmac_ctx *ctx, const uint8_t *data,
   86     size_t datasize)
   87 {
   88 
   89         SHA512_Update(&ctx->innerctx, data, datasize);
   90 }
   91 
   92 void
   93 g_eli_crypto_hmac_final(struct hmac_ctx *ctx, uint8_t *md, size_t mdsize)
   94 {
   95         u_char digest[SHA512_MDLEN];
   96 
   97         /* Complete inner hash */
   98         SHA512_Final(digest, &ctx->innerctx);
   99         
  100         /* Complete outer hash */
  101         SHA512_Update(&ctx->outerctx, digest, sizeof(digest));
  102         SHA512_Final(digest, &ctx->outerctx);
  103         
  104         explicit_bzero(ctx, sizeof(*ctx));
  105         /* mdsize == 0 means "Give me the whole hash!" */
  106         if (mdsize == 0)
  107                 mdsize = SHA512_MDLEN;
  108         bcopy(digest, md, mdsize);
  109         explicit_bzero(digest, sizeof(digest));
  110 }
  111 
  112 void
  113 g_eli_crypto_hmac(const uint8_t *hkey, size_t hkeysize, const uint8_t *data,
  114     size_t datasize, uint8_t *md, size_t mdsize)
  115 {
  116         struct hmac_ctx ctx;
  117 
  118         g_eli_crypto_hmac_init(&ctx, hkey, hkeysize);
  119         g_eli_crypto_hmac_update(&ctx, data, datasize);
  120         g_eli_crypto_hmac_final(&ctx, md, mdsize);
  121 }
  122 
  123 /*
  124  * Here we generate IV. It is unique for every sector.
  125  */
  126 void
  127 g_eli_crypto_ivgen(struct g_eli_softc *sc, off_t offset, u_char *iv,
  128     size_t size)
  129 {
  130         uint8_t off[8];
  131 
  132         if ((sc->sc_flags & G_ELI_FLAG_NATIVE_BYTE_ORDER) != 0)
  133                 bcopy(&offset, off, sizeof(off));
  134         else
  135                 le64enc(off, (uint64_t)offset);
  136 
  137         switch (sc->sc_ealgo) {
  138         case CRYPTO_AES_XTS:
  139                 bcopy(off, iv, sizeof(off));
  140                 bzero(iv + sizeof(off), size - sizeof(off));
  141                 break;
  142         default:
  143             {
  144                 u_char hash[SHA256_DIGEST_LENGTH];
  145                 SHA256_CTX ctx;
  146 
  147                 /* Copy precalculated SHA256 context for IV-Key. */
  148                 bcopy(&sc->sc_ivctx, &ctx, sizeof(ctx));
  149                 SHA256_Update(&ctx, off, sizeof(off));
  150                 SHA256_Final(hash, &ctx);
  151                 bcopy(hash, iv, MIN(sizeof(hash), size));
  152                 break;
  153             }
  154         }
  155 }

Cache object: 29bcac9dce4f65826835fbb054df4261


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.