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 #include <sys/param.h>
29 #ifdef _KERNEL
30 #include <sys/systm.h>
31 #include <sys/kernel.h>
32 #include <sys/malloc.h>
33 #elif defined(_STANDALONE)
34 #include "stand.h"
35 #else
36 #include <stdint.h>
37 #include <string.h>
38 #include <strings.h>
39 #include <errno.h>
40 #include <assert.h>
41 #include <openssl/evp.h>
42 #define _OpenSSL_
43 #endif
44 #include <geom/eli/g_eli.h>
45
46 void
g_eli_crypto_hmac_init(struct hmac_ctx * ctx,const char * hkey,size_t hkeylen)47 g_eli_crypto_hmac_init(struct hmac_ctx *ctx, const char *hkey,
48 size_t hkeylen)
49 {
50 u_char k_ipad[128], k_opad[128], key[128];
51 SHA512_CTX lctx;
52 u_int i;
53
54 bzero(key, sizeof(key));
55 if (hkeylen == 0)
56 ; /* do nothing */
57 else if (hkeylen <= 128)
58 bcopy(hkey, key, hkeylen);
59 else {
60 /* If key is longer than 128 bytes reset it to key = SHA512(key). */
61 SHA512_Init(&lctx);
62 SHA512_Update(&lctx, hkey, hkeylen);
63 SHA512_Final(key, &lctx);
64 }
65
66 /* XOR key with ipad and opad values. */
67 for (i = 0; i < sizeof(key); i++) {
68 k_ipad[i] = key[i] ^ 0x36;
69 k_opad[i] = key[i] ^ 0x5c;
70 }
71 explicit_bzero(key, sizeof(key));
72 /* Start inner SHA512. */
73 SHA512_Init(&ctx->innerctx);
74 SHA512_Update(&ctx->innerctx, k_ipad, sizeof(k_ipad));
75 explicit_bzero(k_ipad, sizeof(k_ipad));
76 /* Start outer SHA512. */
77 SHA512_Init(&ctx->outerctx);
78 SHA512_Update(&ctx->outerctx, k_opad, sizeof(k_opad));
79 explicit_bzero(k_opad, sizeof(k_opad));
80 }
81
82 void
g_eli_crypto_hmac_update(struct hmac_ctx * ctx,const uint8_t * data,size_t datasize)83 g_eli_crypto_hmac_update(struct hmac_ctx *ctx, const uint8_t *data,
84 size_t datasize)
85 {
86
87 SHA512_Update(&ctx->innerctx, data, datasize);
88 }
89
90 void
g_eli_crypto_hmac_final(struct hmac_ctx * ctx,uint8_t * md,size_t mdsize)91 g_eli_crypto_hmac_final(struct hmac_ctx *ctx, uint8_t *md, size_t mdsize)
92 {
93 u_char digest[SHA512_MDLEN];
94
95 /* Complete inner hash */
96 SHA512_Final(digest, &ctx->innerctx);
97
98 /* Complete outer hash */
99 SHA512_Update(&ctx->outerctx, digest, sizeof(digest));
100 SHA512_Final(digest, &ctx->outerctx);
101
102 explicit_bzero(ctx, sizeof(*ctx));
103 /* mdsize == 0 means "Give me the whole hash!" */
104 if (mdsize == 0)
105 mdsize = SHA512_MDLEN;
106 bcopy(digest, md, mdsize);
107 explicit_bzero(digest, sizeof(digest));
108 }
109
110 void
g_eli_crypto_hmac(const char * hkey,size_t hkeysize,const uint8_t * data,size_t datasize,uint8_t * md,size_t mdsize)111 g_eli_crypto_hmac(const char *hkey, size_t hkeysize, const uint8_t *data,
112 size_t datasize, uint8_t *md, size_t mdsize)
113 {
114 struct hmac_ctx ctx;
115
116 g_eli_crypto_hmac_init(&ctx, hkey, hkeysize);
117 g_eli_crypto_hmac_update(&ctx, data, datasize);
118 g_eli_crypto_hmac_final(&ctx, md, mdsize);
119 }
120
121 /*
122 * Here we generate IV. It is unique for every sector.
123 */
124 void
g_eli_crypto_ivgen(struct g_eli_softc * sc,off_t offset,u_char * iv,size_t size)125 g_eli_crypto_ivgen(struct g_eli_softc *sc, off_t offset, u_char *iv,
126 size_t size)
127 {
128 uint8_t off[8];
129
130 if ((sc->sc_flags & G_ELI_FLAG_NATIVE_BYTE_ORDER) != 0)
131 bcopy(&offset, off, sizeof(off));
132 else
133 le64enc(off, (uint64_t)offset);
134
135 switch (sc->sc_ealgo) {
136 case CRYPTO_AES_XTS:
137 bcopy(off, iv, sizeof(off));
138 bzero(iv + sizeof(off), size - sizeof(off));
139 break;
140 default:
141 {
142 u_char hash[SHA256_DIGEST_LENGTH];
143 SHA256_CTX ctx;
144
145 /* Copy precalculated SHA256 context for IV-Key. */
146 bcopy(&sc->sc_ivctx, &ctx, sizeof(ctx));
147 SHA256_Update(&ctx, off, sizeof(off));
148 SHA256_Final(hash, &ctx);
149 bcopy(hash, iv, MIN(sizeof(hash), size));
150 break;
151 }
152 }
153 }
154