]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - stand/libsa/geli/geliboot_crypto.c
loader: implement GELI writes
[FreeBSD/FreeBSD.git] / stand / libsa / geli / geliboot_crypto.c
1 /*-
2  * Copyright (c) 2005-2010 Pawel Jakub Dawidek <pjd@FreeBSD.org>
3  * Copyright (c) 2015 Allan Jude <allanjude@FreeBSD.org>
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
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.
14  *
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
25  * SUCH DAMAGE.
26  *
27  * $FreeBSD$
28  */
29
30 #include <stdio.h>
31 #include <string.h>
32 #include <strings.h>
33
34 #include "geliboot_internal.h"
35 #include "geliboot.h"
36
37 int
38 geliboot_crypt(u_int algo, geli_op_t enc, u_char *data, size_t datasize,
39     const u_char *key, size_t keysize, u_char *iv)
40 {
41         keyInstance aeskey;
42         cipherInstance cipher;
43         struct aes_xts_ctx xtsctx, *ctxp;
44         size_t xts_len;
45         int err, blks, i;
46
47         switch (algo) {
48         case CRYPTO_AES_CBC:
49                 err = rijndael_makeKey(&aeskey, !enc, keysize, 
50                     (const char *)key);
51                 if (err < 0) {
52                         printf("Failed to setup crypo keys: %d\n", err);
53                         return (err);
54                 }
55
56                 err = rijndael_cipherInit(&cipher, MODE_CBC, iv);
57                 if (err < 0) {
58                         printf("Failed to setup IV: %d\n", err);
59                         return (err);
60                 }
61
62                 switch (enc) {
63                 case GELI_DECRYPT:
64                         blks = rijndael_blockDecrypt(&cipher, &aeskey, data, 
65                             datasize * 8, data);
66                         break;
67                 case GELI_ENCRYPT:
68                         blks = rijndael_blockEncrypt(&cipher, &aeskey, data, 
69                             datasize * 8, data);
70                         break;
71                 }
72                 if (datasize != (blks / 8)) {
73                         printf("Failed to %s the entire input: %u != %zu\n",
74                             enc ? "decrypt" : "encrypt",
75                             blks, datasize);
76                         return (1);
77                 }
78                 break;
79         case CRYPTO_AES_XTS:
80                 xts_len = keysize << 1;
81                 ctxp = &xtsctx;
82
83                 rijndael_set_key(&ctxp->key1, key, xts_len / 2);
84                 rijndael_set_key(&ctxp->key2, key + (xts_len / 16), xts_len / 2);
85
86                 enc_xform_aes_xts.reinit((caddr_t)ctxp, iv);
87
88                 switch (enc) {
89                 case GELI_DECRYPT:
90                         for (i = 0; i < datasize; i += AES_XTS_BLOCKSIZE) {
91                                 enc_xform_aes_xts.decrypt((caddr_t)ctxp, data + i);
92                         }
93                         break;
94                 case GELI_ENCRYPT:
95                         for (i = 0; i < datasize; i += AES_XTS_BLOCKSIZE) {
96                                 enc_xform_aes_xts.encrypt((caddr_t)ctxp,
97                                     data + i);
98                         }
99                         break;
100                 }
101                 break;
102         default:
103                 printf("Unsupported crypto algorithm #%d\n", algo);
104                 return (1);
105         }
106
107         return (0);
108 }
109
110 static int
111 g_eli_crypto_cipher(u_int algo, geli_op_t enc, u_char *data, size_t datasize,
112     const u_char *key, size_t keysize)
113 {
114         u_char iv[keysize];
115
116         explicit_bzero(iv, sizeof(iv));
117         return (geliboot_crypt(algo, enc, data, datasize, key, keysize, iv));
118 }
119
120 int
121 g_eli_crypto_encrypt(u_int algo, u_char *data, size_t datasize,
122     const u_char *key, size_t keysize)
123 {
124
125         /* We prefer AES-CBC for metadata protection. */
126         if (algo == CRYPTO_AES_XTS)
127                 algo = CRYPTO_AES_CBC;
128
129         return (g_eli_crypto_cipher(algo, GELI_ENCRYPT, data, datasize, key,
130             keysize));
131 }
132
133 int
134 g_eli_crypto_decrypt(u_int algo, u_char *data, size_t datasize,
135     const u_char *key, size_t keysize)
136 {
137
138         /* We prefer AES-CBC for metadata protection. */
139         if (algo == CRYPTO_AES_XTS)
140                 algo = CRYPTO_AES_CBC;
141
142         return (g_eli_crypto_cipher(algo, GELI_DECRYPT, data, datasize, key,
143             keysize));
144 }