xref: /openbsd/sys/uvm/uvm_swap_encrypt.c (revision a6445c1d)
1 /*	$OpenBSD: uvm_swap_encrypt.c,v 1.19 2014/11/18 02:37:31 tedu Exp $	*/
2 
3 /*
4  * Copyright 1999 Niels Provos <provos@citi.umich.edu>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *      This product includes software developed by Niels Provos.
18  * 4. The name of the author may not be used to endorse or promote products
19  *    derived from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/kernel.h>
36 #include <sys/malloc.h>
37 #include <sys/sysctl.h>
38 #include <sys/time.h>
39 #include <sys/conf.h>
40 #include <crypto/rijndael.h>
41 
42 #include <uvm/uvm.h>
43 
44 struct swap_key *kcur = NULL;
45 rijndael_ctx swap_ctxt;
46 
47 int uvm_doswapencrypt = 1;
48 u_int uvm_swpkeyscreated = 0;
49 u_int uvm_swpkeysdeleted = 0;
50 
51 int swap_encrypt_initialized = 0;
52 
53 int
54 swap_encrypt_ctl(int *name, u_int namelen, void *oldp, size_t *oldlenp,
55     void *newp, size_t newlen, struct proc *p)
56 {
57 	/* all sysctl names at this level are terminal */
58 	if (namelen != 1)
59 		return (ENOTDIR);		/* overloaded */
60 
61 	switch (name[0]) {
62 	case SWPENC_ENABLE: {
63 		int doencrypt = uvm_doswapencrypt;
64 		int result;
65 
66 		result = sysctl_int(oldp, oldlenp, newp, newlen, &doencrypt);
67 		if (result)
68 			return result;
69 
70 		/*
71 		 * Swap Encryption has been turned on, we need to
72 		 * initialize state for swap devices that have been
73 		 * added.
74 		 */
75 		if (doencrypt)
76 			uvm_swap_initcrypt_all();
77 		uvm_doswapencrypt = doencrypt;
78 		return (0);
79 	}
80 	case SWPENC_CREATED:
81 		return (sysctl_rdint(oldp, oldlenp, newp, uvm_swpkeyscreated));
82 	case SWPENC_DELETED:
83 		return (sysctl_rdint(oldp, oldlenp, newp, uvm_swpkeysdeleted));
84 	default:
85 		return (EOPNOTSUPP);
86 	}
87 	/* NOTREACHED */
88 }
89 
90 void
91 swap_key_delete(struct swap_key *key)
92 {
93 	/* Make sure that this key gets removed if we just used it */
94 	swap_key_cleanup(key);
95 
96 	explicit_bzero(key, sizeof(*key));
97 	uvm_swpkeysdeleted++;
98 }
99 
100 /*
101  * Encrypt the data before it goes to swap, the size should be 64-bit
102  * aligned.
103  */
104 
105 void
106 swap_encrypt(struct swap_key *key, caddr_t src, caddr_t dst, u_int64_t block,
107     size_t count)
108 {
109 	u_int32_t *dsrc = (u_int32_t *)src;
110 	u_int32_t *ddst = (u_int32_t *)dst;
111 	u_int32_t iv[4];
112 	u_int32_t iv1, iv2, iv3, iv4;
113 
114 	if (!swap_encrypt_initialized)
115 		swap_encrypt_initialized = 1;
116 
117 	swap_key_prepare(key, 1);
118 
119 	count /= sizeof(u_int32_t);
120 
121 	iv[0] = block >> 32; iv[1] = block; iv[2] = ~iv[0]; iv[3] = ~iv[1];
122 	rijndael_encrypt(&swap_ctxt, (u_char *)iv, (u_char *)iv);
123 	iv1 = iv[0]; iv2 = iv[1]; iv3 = iv[2]; iv4 = iv[3];
124 
125 	for (; count > 0; count -= 4) {
126 		ddst[0] = dsrc[0] ^ iv1;
127 		ddst[1] = dsrc[1] ^ iv2;
128 		ddst[2] = dsrc[2] ^ iv3;
129 		ddst[3] = dsrc[3] ^ iv4;
130 		/*
131 		 * Do not worry about endianess, it only needs to decrypt
132 		 * on this machine.
133 		 */
134 		rijndael_encrypt(&swap_ctxt, (u_char *)ddst, (u_char *)ddst);
135 		iv1 = ddst[0];
136 		iv2 = ddst[1];
137 		iv3 = ddst[2];
138 		iv4 = ddst[3];
139 
140 		dsrc += 4;
141 		ddst += 4;
142 	}
143 }
144 
145 /*
146  * Decrypt the data after we retrieved it from swap, the size should be 64-bit
147  * aligned.
148  */
149 
150 void
151 swap_decrypt(struct swap_key *key, caddr_t src, caddr_t dst, u_int64_t block,
152     size_t count)
153 {
154 	u_int32_t *dsrc = (u_int32_t *)src;
155 	u_int32_t *ddst = (u_int32_t *)dst;
156 	u_int32_t iv[4];
157 	u_int32_t iv1, iv2, iv3, iv4, niv1, niv2, niv3, niv4;
158 
159 	if (!swap_encrypt_initialized)
160 		panic("swap_decrypt: key not initialized");
161 
162 	swap_key_prepare(key, 0);
163 
164 	count /= sizeof(u_int32_t);
165 
166 	iv[0] = block >> 32; iv[1] = block; iv[2] = ~iv[0]; iv[3] = ~iv[1];
167 	rijndael_encrypt(&swap_ctxt, (u_char *)iv, (u_char *)iv);
168 	iv1 = iv[0]; iv2 = iv[1]; iv3 = iv[2]; iv4 = iv[3];
169 
170 	for (; count > 0; count -= 4) {
171 		ddst[0] = niv1 = dsrc[0];
172 		ddst[1] = niv2 = dsrc[1];
173 		ddst[2] = niv3 = dsrc[2];
174 		ddst[3] = niv4 = dsrc[3];
175 		rijndael_decrypt(&swap_ctxt, (u_char *)ddst, (u_char *)ddst);
176 		ddst[0] ^= iv1;
177 		ddst[1] ^= iv2;
178 		ddst[2] ^= iv3;
179 		ddst[3] ^= iv4;
180 
181 		iv1 = niv1;
182 		iv2 = niv2;
183 		iv3 = niv3;
184 		iv4 = niv4;
185 
186 		dsrc += 4;
187 		ddst += 4;
188 	}
189 }
190 
191 void
192 swap_key_prepare(struct swap_key *key, int encrypt)
193 {
194 	/*
195 	 * Check if we have prepared for this key already,
196 	 * if we only have the encryption schedule, we have
197 	 * to recompute and get the decryption schedule also.
198 	 */
199 	if (kcur == key && (encrypt || !swap_ctxt.enc_only))
200 		return;
201 
202 	if (encrypt)
203 		rijndael_set_key_enc_only(&swap_ctxt, (u_char *)key->key,
204 		    sizeof(key->key) * 8);
205 	else
206 		rijndael_set_key(&swap_ctxt, (u_char *)key->key,
207 		    sizeof(key->key) * 8);
208 
209 	kcur = key;
210 }
211 
212 /*
213  * Make sure that a specific key is no longer available.
214  */
215 
216 void
217 swap_key_cleanup(struct swap_key *key)
218 {
219 	/* Check if we have a key */
220 	if (kcur == NULL || kcur != key)
221 		return;
222 
223 	/* Zero out the subkeys */
224 	explicit_bzero(&swap_ctxt, sizeof(swap_ctxt));
225 
226 	kcur = NULL;
227 }
228