uvm_swap_encrypt.c revision 1.18
1/* $OpenBSD: uvm_swap_encrypt.c,v 1.18 2014/09/14 14:17:27 jsg 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 <dev/rndvar.h> 41#include <crypto/rijndael.h> 42 43#include <uvm/uvm.h> 44 45struct swap_key *kcur = NULL; 46rijndael_ctx swap_ctxt; 47 48int uvm_doswapencrypt = 1; 49u_int uvm_swpkeyscreated = 0; 50u_int uvm_swpkeysdeleted = 0; 51 52int swap_encrypt_initialized = 0; 53 54int 55swap_encrypt_ctl(int *name, u_int namelen, void *oldp, size_t *oldlenp, 56 void *newp, size_t newlen, struct proc *p) 57{ 58 /* all sysctl names at this level are terminal */ 59 if (namelen != 1) 60 return (ENOTDIR); /* overloaded */ 61 62 switch (name[0]) { 63 case SWPENC_ENABLE: { 64 int doencrypt = uvm_doswapencrypt; 65 int result; 66 67 result = sysctl_int(oldp, oldlenp, newp, newlen, &doencrypt); 68 if (result) 69 return result; 70 71 /* 72 * Swap Encryption has been turned on, we need to 73 * initialize state for swap devices that have been 74 * added. 75 */ 76 if (doencrypt) 77 uvm_swap_initcrypt_all(); 78 uvm_doswapencrypt = doencrypt; 79 return (0); 80 } 81 case SWPENC_CREATED: 82 return (sysctl_rdint(oldp, oldlenp, newp, uvm_swpkeyscreated)); 83 case SWPENC_DELETED: 84 return (sysctl_rdint(oldp, oldlenp, newp, uvm_swpkeysdeleted)); 85 default: 86 return (EOPNOTSUPP); 87 } 88 /* NOTREACHED */ 89} 90 91void 92swap_key_delete(struct swap_key *key) 93{ 94 /* Make sure that this key gets removed if we just used it */ 95 swap_key_cleanup(key); 96 97 explicit_bzero(key, sizeof(*key)); 98 uvm_swpkeysdeleted++; 99} 100 101/* 102 * Encrypt the data before it goes to swap, the size should be 64-bit 103 * aligned. 104 */ 105 106void 107swap_encrypt(struct swap_key *key, caddr_t src, caddr_t dst, u_int64_t block, 108 size_t count) 109{ 110 u_int32_t *dsrc = (u_int32_t *)src; 111 u_int32_t *ddst = (u_int32_t *)dst; 112 u_int32_t iv[4]; 113 u_int32_t iv1, iv2, iv3, iv4; 114 115 if (!swap_encrypt_initialized) 116 swap_encrypt_initialized = 1; 117 118 swap_key_prepare(key, 1); 119 120 count /= sizeof(u_int32_t); 121 122 iv[0] = block >> 32; iv[1] = block; iv[2] = ~iv[0]; iv[3] = ~iv[1]; 123 rijndael_encrypt(&swap_ctxt, (u_char *)iv, (u_char *)iv); 124 iv1 = iv[0]; iv2 = iv[1]; iv3 = iv[2]; iv4 = iv[3]; 125 126 for (; count > 0; count -= 4) { 127 ddst[0] = dsrc[0] ^ iv1; 128 ddst[1] = dsrc[1] ^ iv2; 129 ddst[2] = dsrc[2] ^ iv3; 130 ddst[3] = dsrc[3] ^ iv4; 131 /* 132 * Do not worry about endianess, it only needs to decrypt 133 * on this machine. 134 */ 135 rijndael_encrypt(&swap_ctxt, (u_char *)ddst, (u_char *)ddst); 136 iv1 = ddst[0]; 137 iv2 = ddst[1]; 138 iv3 = ddst[2]; 139 iv4 = ddst[3]; 140 141 dsrc += 4; 142 ddst += 4; 143 } 144} 145 146/* 147 * Decrypt the data after we retrieved it from swap, the size should be 64-bit 148 * aligned. 149 */ 150 151void 152swap_decrypt(struct swap_key *key, caddr_t src, caddr_t dst, u_int64_t block, 153 size_t count) 154{ 155 u_int32_t *dsrc = (u_int32_t *)src; 156 u_int32_t *ddst = (u_int32_t *)dst; 157 u_int32_t iv[4]; 158 u_int32_t iv1, iv2, iv3, iv4, niv1, niv2, niv3, niv4; 159 160 if (!swap_encrypt_initialized) 161 panic("swap_decrypt: key not initialized"); 162 163 swap_key_prepare(key, 0); 164 165 count /= sizeof(u_int32_t); 166 167 iv[0] = block >> 32; iv[1] = block; iv[2] = ~iv[0]; iv[3] = ~iv[1]; 168 rijndael_encrypt(&swap_ctxt, (u_char *)iv, (u_char *)iv); 169 iv1 = iv[0]; iv2 = iv[1]; iv3 = iv[2]; iv4 = iv[3]; 170 171 for (; count > 0; count -= 4) { 172 ddst[0] = niv1 = dsrc[0]; 173 ddst[1] = niv2 = dsrc[1]; 174 ddst[2] = niv3 = dsrc[2]; 175 ddst[3] = niv4 = dsrc[3]; 176 rijndael_decrypt(&swap_ctxt, (u_char *)ddst, (u_char *)ddst); 177 ddst[0] ^= iv1; 178 ddst[1] ^= iv2; 179 ddst[2] ^= iv3; 180 ddst[3] ^= iv4; 181 182 iv1 = niv1; 183 iv2 = niv2; 184 iv3 = niv3; 185 iv4 = niv4; 186 187 dsrc += 4; 188 ddst += 4; 189 } 190} 191 192void 193swap_key_prepare(struct swap_key *key, int encrypt) 194{ 195 /* 196 * Check if we have prepared for this key already, 197 * if we only have the encryption schedule, we have 198 * to recompute and get the decryption schedule also. 199 */ 200 if (kcur == key && (encrypt || !swap_ctxt.enc_only)) 201 return; 202 203 if (encrypt) 204 rijndael_set_key_enc_only(&swap_ctxt, (u_char *)key->key, 205 sizeof(key->key) * 8); 206 else 207 rijndael_set_key(&swap_ctxt, (u_char *)key->key, 208 sizeof(key->key) * 8); 209 210 kcur = key; 211} 212 213/* 214 * Make sure that a specific key is no longer available. 215 */ 216 217void 218swap_key_cleanup(struct swap_key *key) 219{ 220 /* Check if we have a key */ 221 if (kcur == NULL || kcur != key) 222 return; 223 224 /* Zero out the subkeys */ 225 explicit_bzero(&swap_ctxt, sizeof(swap_ctxt)); 226 227 kcur = NULL; 228} 229