2 * Copyright (c) 2005-2010 Pawel Jakub Dawidek <pjd@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/kernel.h>
34 #include <sys/malloc.h>
42 #include <openssl/evp.h>
45 #include <geom/eli/g_eli.h>
48 MALLOC_DECLARE(M_ELI);
51 g_eli_crypto_done(struct cryptop *crp)
54 crp->crp_opaque = (void *)crp;
60 g_eli_crypto_cipher(u_int algo, int enc, u_char *data, size_t datasize,
61 const u_char *key, size_t keysize)
65 struct cryptodesc *crd;
72 KASSERT(algo != CRYPTO_AES_XTS,
73 ("%s: CRYPTO_AES_XTS unexpected here", __func__));
75 bzero(&cri, sizeof(cri));
77 cri.cri_key = __DECONST(void *, key);
78 cri.cri_klen = keysize;
79 error = crypto_newsession(&sid, &cri, CRYPTOCAP_F_SOFTWARE);
82 p = malloc(sizeof(*crp) + sizeof(*crd) + sizeof(*uio) + sizeof(*iov),
83 M_ELI, M_NOWAIT | M_ZERO);
85 crypto_freesession(sid);
88 crp = (struct cryptop *)p; p += sizeof(*crp);
89 crd = (struct cryptodesc *)p; p += sizeof(*crd);
90 uio = (struct uio *)p; p += sizeof(*uio);
91 iov = (struct iovec *)p; p += sizeof(*iov);
93 iov->iov_len = datasize;
98 uio->uio_segflg = UIO_SYSSPACE;
99 uio->uio_resid = datasize;
102 crd->crd_len = datasize;
103 crd->crd_flags = CRD_F_IV_EXPLICIT | CRD_F_IV_PRESENT;
105 crd->crd_flags |= CRD_F_ENCRYPT;
107 crd->crd_key = __DECONST(void *, key);
108 crd->crd_klen = keysize;
109 bzero(crd->crd_iv, sizeof(crd->crd_iv));
110 crd->crd_next = NULL;
113 crp->crp_ilen = datasize;
114 crp->crp_olen = datasize;
115 crp->crp_opaque = NULL;
116 crp->crp_callback = g_eli_crypto_done;
117 crp->crp_buf = (void *)uio;
118 crp->crp_flags = CRYPTO_F_IOV | CRYPTO_F_CBIFSYNC | CRYPTO_F_REL;
121 error = crypto_dispatch(crp);
123 while (crp->crp_opaque == NULL)
124 tsleep(crp, PRIBIO, "geli", hz / 5);
125 error = crp->crp_etype;
129 crypto_freesession(sid);
134 g_eli_crypto_cipher(u_int algo, int enc, u_char *data, size_t datasize,
135 const u_char *key, size_t keysize)
138 const EVP_CIPHER *type;
142 assert(algo != CRYPTO_AES_XTS);
145 case CRYPTO_NULL_CBC:
146 type = EVP_enc_null();
151 type = EVP_aes_128_cbc();
154 type = EVP_aes_192_cbc();
157 type = EVP_aes_256_cbc();
166 #ifndef OPENSSL_NO_CAMELLIA
167 case CRYPTO_CAMELLIA_CBC:
170 type = EVP_camellia_128_cbc();
173 type = EVP_camellia_192_cbc();
176 type = EVP_camellia_256_cbc();
183 case CRYPTO_3DES_CBC:
184 type = EVP_des_ede3_cbc();
190 EVP_CIPHER_CTX_init(&ctx);
192 EVP_CipherInit_ex(&ctx, type, NULL, NULL, NULL, enc);
193 EVP_CIPHER_CTX_set_key_length(&ctx, keysize / 8);
194 EVP_CIPHER_CTX_set_padding(&ctx, 0);
195 bzero(iv, sizeof(iv));
196 EVP_CipherInit_ex(&ctx, NULL, NULL, key, iv, enc);
198 if (EVP_CipherUpdate(&ctx, data, &outsize, data, datasize) == 0) {
199 EVP_CIPHER_CTX_cleanup(&ctx);
202 assert(outsize == (int)datasize);
204 if (EVP_CipherFinal_ex(&ctx, data + outsize, &outsize) == 0) {
205 EVP_CIPHER_CTX_cleanup(&ctx);
208 assert(outsize == 0);
210 EVP_CIPHER_CTX_cleanup(&ctx);
213 #endif /* !_KERNEL */
216 g_eli_crypto_encrypt(u_int algo, u_char *data, size_t datasize,
217 const u_char *key, size_t keysize)
220 /* We prefer AES-CBC for metadata protection. */
221 if (algo == CRYPTO_AES_XTS)
222 algo = CRYPTO_AES_CBC;
224 return (g_eli_crypto_cipher(algo, 1, data, datasize, key, keysize));
228 g_eli_crypto_decrypt(u_int algo, u_char *data, size_t datasize,
229 const u_char *key, size_t keysize)
232 /* We prefer AES-CBC for metadata protection. */
233 if (algo == CRYPTO_AES_XTS)
234 algo = CRYPTO_AES_CBC;
236 return (g_eli_crypto_cipher(algo, 0, data, datasize, key, keysize));
240 g_eli_crypto_hmac_init(struct hmac_ctx *ctx, const uint8_t *hkey,
243 u_char k_ipad[128], key[128];
247 bzero(key, sizeof(key));
250 else if (hkeylen <= 128)
251 bcopy(hkey, key, hkeylen);
253 /* If key is longer than 128 bytes reset it to key = SHA512(key). */
255 SHA512_Update(&lctx, hkey, hkeylen);
256 SHA512_Final(key, &lctx);
259 /* XOR key with ipad and opad values. */
260 for (i = 0; i < sizeof(key); i++) {
261 k_ipad[i] = key[i] ^ 0x36;
262 ctx->k_opad[i] = key[i] ^ 0x5c;
264 bzero(key, sizeof(key));
265 /* Perform inner SHA512. */
266 SHA512_Init(&ctx->shactx);
267 SHA512_Update(&ctx->shactx, k_ipad, sizeof(k_ipad));
271 g_eli_crypto_hmac_update(struct hmac_ctx *ctx, const uint8_t *data,
275 SHA512_Update(&ctx->shactx, data, datasize);
279 g_eli_crypto_hmac_final(struct hmac_ctx *ctx, uint8_t *md, size_t mdsize)
281 u_char digest[SHA512_MDLEN];
284 SHA512_Final(digest, &ctx->shactx);
285 /* Perform outer SHA512. */
287 SHA512_Update(&lctx, ctx->k_opad, sizeof(ctx->k_opad));
288 bzero(ctx, sizeof(*ctx));
289 SHA512_Update(&lctx, digest, sizeof(digest));
290 SHA512_Final(digest, &lctx);
291 /* mdsize == 0 means "Give me the whole hash!" */
293 mdsize = SHA512_MDLEN;
294 bcopy(digest, md, mdsize);
298 g_eli_crypto_hmac(const uint8_t *hkey, size_t hkeysize, const uint8_t *data,
299 size_t datasize, uint8_t *md, size_t mdsize)
303 g_eli_crypto_hmac_init(&ctx, hkey, hkeysize);
304 g_eli_crypto_hmac_update(&ctx, data, datasize);
305 g_eli_crypto_hmac_final(&ctx, md, mdsize);