1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (c) 2005-2010 Pawel Jakub Dawidek <pjd@FreeBSD.org> 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 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 */ 28 29 #include <sys/param.h> 30 #ifdef _KERNEL 31 #include <sys/systm.h> 32 #include <sys/kernel.h> 33 #include <sys/malloc.h> 34 #else 35 #include <stdint.h> 36 #include <string.h> 37 #include <strings.h> 38 #include <errno.h> 39 #include <assert.h> 40 #include <openssl/evp.h> 41 #define _OpenSSL_ 42 #endif 43 #include <geom/eli/g_eli.h> 44 45 #ifdef _KERNEL 46 MALLOC_DECLARE(M_ELI); 47 48 static int 49 g_eli_crypto_done(struct cryptop *crp) 50 { 51 52 crp->crp_opaque = (void *)crp; 53 wakeup(crp); 54 return (0); 55 } 56 57 static int 58 g_eli_crypto_cipher(u_int algo, int enc, u_char *data, size_t datasize, 59 const u_char *key, size_t keysize) 60 { 61 struct crypto_session_params csp; 62 struct cryptop *crp; 63 crypto_session_t sid; 64 int error; 65 66 KASSERT(algo != CRYPTO_AES_XTS, 67 ("%s: CRYPTO_AES_XTS unexpected here", __func__)); 68 69 memset(&csp, 0, sizeof(csp)); 70 csp.csp_mode = CSP_MODE_CIPHER; 71 csp.csp_cipher_alg = algo; 72 csp.csp_ivlen = g_eli_ivlen(algo); 73 csp.csp_cipher_key = key; 74 csp.csp_cipher_klen = keysize / 8; 75 error = crypto_newsession(&sid, &csp, CRYPTOCAP_F_SOFTWARE); 76 if (error != 0) 77 return (error); 78 crp = crypto_getreq(sid, M_NOWAIT); 79 if (crp == NULL) { 80 crypto_freesession(sid); 81 return (ENOMEM); 82 } 83 84 crp->crp_payload_start = 0; 85 crp->crp_payload_length = datasize; 86 crp->crp_flags = CRYPTO_F_CBIFSYNC | CRYPTO_F_IV_SEPARATE; 87 crp->crp_op = enc ? CRYPTO_OP_ENCRYPT : CRYPTO_OP_DECRYPT; 88 memset(crp->crp_iv, 0, sizeof(crp->crp_iv)); 89 90 crp->crp_opaque = NULL; 91 crp->crp_callback = g_eli_crypto_done; 92 crypto_use_buf(crp, data, datasize); 93 94 error = crypto_dispatch(crp); 95 if (error == 0) { 96 while (crp->crp_opaque == NULL) 97 tsleep(crp, PRIBIO, "geli", hz / 5); 98 error = crp->crp_etype; 99 } 100 101 crypto_freereq(crp); 102 crypto_freesession(sid); 103 return (error); 104 } 105 #else /* !_KERNEL */ 106 static int 107 g_eli_crypto_cipher(u_int algo, int enc, u_char *data, size_t datasize, 108 const u_char *key, size_t keysize) 109 { 110 EVP_CIPHER_CTX *ctx; 111 const EVP_CIPHER *type; 112 u_char iv[G_ELI_IVKEYLEN]; 113 int outsize; 114 115 assert(algo != CRYPTO_AES_XTS); 116 117 switch (algo) { 118 case CRYPTO_NULL_CBC: 119 type = EVP_enc_null(); 120 break; 121 case CRYPTO_AES_CBC: 122 switch (keysize) { 123 case 128: 124 type = EVP_aes_128_cbc(); 125 break; 126 case 192: 127 type = EVP_aes_192_cbc(); 128 break; 129 case 256: 130 type = EVP_aes_256_cbc(); 131 break; 132 default: 133 return (EINVAL); 134 } 135 break; 136 #ifndef OPENSSL_NO_CAMELLIA 137 case CRYPTO_CAMELLIA_CBC: 138 switch (keysize) { 139 case 128: 140 type = EVP_camellia_128_cbc(); 141 break; 142 case 192: 143 type = EVP_camellia_192_cbc(); 144 break; 145 case 256: 146 type = EVP_camellia_256_cbc(); 147 break; 148 default: 149 return (EINVAL); 150 } 151 break; 152 #endif 153 default: 154 return (EINVAL); 155 } 156 157 ctx = EVP_CIPHER_CTX_new(); 158 if (ctx == NULL) 159 return (ENOMEM); 160 161 EVP_CipherInit_ex(ctx, type, NULL, NULL, NULL, enc); 162 EVP_CIPHER_CTX_set_key_length(ctx, keysize / 8); 163 EVP_CIPHER_CTX_set_padding(ctx, 0); 164 bzero(iv, sizeof(iv)); 165 EVP_CipherInit_ex(ctx, NULL, NULL, key, iv, enc); 166 167 if (EVP_CipherUpdate(ctx, data, &outsize, data, datasize) == 0) { 168 EVP_CIPHER_CTX_free(ctx); 169 return (EINVAL); 170 } 171 assert(outsize == (int)datasize); 172 173 if (EVP_CipherFinal_ex(ctx, data + outsize, &outsize) == 0) { 174 EVP_CIPHER_CTX_free(ctx); 175 return (EINVAL); 176 } 177 assert(outsize == 0); 178 179 EVP_CIPHER_CTX_free(ctx); 180 return (0); 181 } 182 #endif /* !_KERNEL */ 183 184 int 185 g_eli_crypto_encrypt(u_int algo, u_char *data, size_t datasize, 186 const u_char *key, size_t keysize) 187 { 188 189 /* We prefer AES-CBC for metadata protection. */ 190 if (algo == CRYPTO_AES_XTS) 191 algo = CRYPTO_AES_CBC; 192 193 return (g_eli_crypto_cipher(algo, 1, data, datasize, key, keysize)); 194 } 195 196 int 197 g_eli_crypto_decrypt(u_int algo, u_char *data, size_t datasize, 198 const u_char *key, size_t keysize) 199 { 200 201 /* We prefer AES-CBC for metadata protection. */ 202 if (algo == CRYPTO_AES_XTS) 203 algo = CRYPTO_AES_CBC; 204 205 return (g_eli_crypto_cipher(algo, 0, data, datasize, key, keysize)); 206 } 207