2 * Copyright (c) 2005-2010 Pawel Jakub Dawidek <pjd@FreeBSD.org>
3 * Copyright (c) 2015 Allan Jude <allanjude@FreeBSD.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 geliboot_crypt(u_int algo, int enc, u_char *data, size_t datasize,
38 const u_char *key, size_t keysize, u_char *iv)
41 cipherInstance cipher;
42 struct aes_xts_ctx xtsctx, *ctxp;
48 err = rijndael_makeKey(&aeskey, !enc, keysize,
51 printf("Failed to setup decryption keys: %d\n", err);
55 err = rijndael_cipherInit(&cipher, MODE_CBC, iv);
57 printf("Failed to setup IV: %d\n", err);
63 blks = rijndael_blockDecrypt(&cipher, &aeskey, data,
67 blks = rijndael_blockEncrypt(&cipher, &aeskey, data,
71 if (datasize != (blks / 8)) {
72 printf("Failed to decrypt the entire input: "
73 "%u != %u\n", blks, datasize);
78 xts_len = keysize << 1;
81 rijndael_set_key(&ctxp->key1, key, xts_len / 2);
82 rijndael_set_key(&ctxp->key2, key + (xts_len / 16), xts_len / 2);
84 enc_xform_aes_xts.reinit(ctxp, iv);
88 for (i = 0; i < datasize; i += AES_XTS_BLOCKSIZE) {
89 enc_xform_aes_xts.decrypt(ctxp, data + i);
93 for (i = 0; i < datasize; i += AES_XTS_BLOCKSIZE) {
94 enc_xform_aes_xts.encrypt(ctxp, data + i);
100 printf("Unsupported crypto algorithm #%d\n", algo);
108 g_eli_crypto_cipher(u_int algo, int enc, u_char *data, size_t datasize,
109 const u_char *key, size_t keysize)
113 bzero(iv, sizeof(iv));
114 return (geliboot_crypt(algo, enc, data, datasize, key, keysize, iv));
118 g_eli_crypto_encrypt(u_int algo, u_char *data, size_t datasize,
119 const u_char *key, size_t keysize)
122 /* We prefer AES-CBC for metadata protection. */
123 if (algo == CRYPTO_AES_XTS)
124 algo = CRYPTO_AES_CBC;
126 return (g_eli_crypto_cipher(algo, 1, data, datasize, key, keysize));
130 g_eli_crypto_decrypt(u_int algo, u_char *data, size_t datasize,
131 const u_char *key, size_t keysize)
134 /* We prefer AES-CBC for metadata protection. */
135 if (algo == CRYPTO_AES_XTS)
136 algo = CRYPTO_AES_CBC;
138 return (g_eli_crypto_cipher(algo, 0, data, datasize, key, keysize));