g_eli_crypto.c (173746) | g_eli_crypto.c (213070) |
---|---|
1/*- 2 * Copyright (c) 2005 Pawel Jakub Dawidek <pjd@FreeBSD.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright --- 11 unchanged lines hidden (view full) --- 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 */ 26 27#include <sys/cdefs.h> | 1/*- 2 * Copyright (c) 2005 Pawel Jakub Dawidek <pjd@FreeBSD.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright --- 11 unchanged lines hidden (view full) --- 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 */ 26 27#include <sys/cdefs.h> |
28__FBSDID("$FreeBSD: head/sys/geom/eli/g_eli_crypto.c 173746 2007-11-19 08:59:32Z jb $"); | 28__FBSDID("$FreeBSD: head/sys/geom/eli/g_eli_crypto.c 213070 2010-09-23 11:58:36Z pjd $"); |
29 30#include <sys/param.h> 31#ifdef _KERNEL 32#include <sys/systm.h> 33#include <sys/kernel.h> 34#include <sys/malloc.h> 35#include <sys/uio.h> 36#else --- 27 unchanged lines hidden (view full) --- 64 struct cryptop *crp; 65 struct cryptodesc *crd; 66 struct uio *uio; 67 struct iovec *iov; 68 uint64_t sid; 69 u_char *p; 70 int error; 71 | 29 30#include <sys/param.h> 31#ifdef _KERNEL 32#include <sys/systm.h> 33#include <sys/kernel.h> 34#include <sys/malloc.h> 35#include <sys/uio.h> 36#else --- 27 unchanged lines hidden (view full) --- 64 struct cryptop *crp; 65 struct cryptodesc *crd; 66 struct uio *uio; 67 struct iovec *iov; 68 uint64_t sid; 69 u_char *p; 70 int error; 71 |
72 KASSERT(algo != CRYPTO_AES_XTS, 73 ("%s: CRYPTO_AES_XTS unexpected here", __func__)); 74 |
|
72 bzero(&cri, sizeof(cri)); 73 cri.cri_alg = algo; 74 cri.cri_key = __DECONST(void *, key); 75 cri.cri_klen = keysize; 76 error = crypto_newsession(&sid, &cri, CRYPTOCAP_F_SOFTWARE); 77 if (error != 0) 78 return (error); 79 p = malloc(sizeof(*crp) + sizeof(*crd) + sizeof(*uio) + sizeof(*iov), --- 51 unchanged lines hidden (view full) --- 131g_eli_crypto_cipher(u_int algo, int enc, u_char *data, size_t datasize, 132 const u_char *key, size_t keysize) 133{ 134 EVP_CIPHER_CTX ctx; 135 const EVP_CIPHER *type; 136 u_char iv[keysize]; 137 int outsize; 138 | 75 bzero(&cri, sizeof(cri)); 76 cri.cri_alg = algo; 77 cri.cri_key = __DECONST(void *, key); 78 cri.cri_klen = keysize; 79 error = crypto_newsession(&sid, &cri, CRYPTOCAP_F_SOFTWARE); 80 if (error != 0) 81 return (error); 82 p = malloc(sizeof(*crp) + sizeof(*crd) + sizeof(*uio) + sizeof(*iov), --- 51 unchanged lines hidden (view full) --- 134g_eli_crypto_cipher(u_int algo, int enc, u_char *data, size_t datasize, 135 const u_char *key, size_t keysize) 136{ 137 EVP_CIPHER_CTX ctx; 138 const EVP_CIPHER *type; 139 u_char iv[keysize]; 140 int outsize; 141 |
142 assert(algo != CRYPTO_AES_XTS); 143 |
|
139 switch (algo) { 140 case CRYPTO_NULL_CBC: 141 type = EVP_enc_null(); 142 break; 143 case CRYPTO_AES_CBC: 144 switch (keysize) { 145 case 128: 146 type = EVP_aes_128_cbc(); --- 60 unchanged lines hidden (view full) --- 207} 208#endif /* !_KERNEL */ 209 210int 211g_eli_crypto_encrypt(u_int algo, u_char *data, size_t datasize, 212 const u_char *key, size_t keysize) 213{ 214 | 144 switch (algo) { 145 case CRYPTO_NULL_CBC: 146 type = EVP_enc_null(); 147 break; 148 case CRYPTO_AES_CBC: 149 switch (keysize) { 150 case 128: 151 type = EVP_aes_128_cbc(); --- 60 unchanged lines hidden (view full) --- 212} 213#endif /* !_KERNEL */ 214 215int 216g_eli_crypto_encrypt(u_int algo, u_char *data, size_t datasize, 217 const u_char *key, size_t keysize) 218{ 219 |
220 /* We prefer AES-CBC for metadata protection. */ 221 if (algo == CRYPTO_AES_XTS) 222 algo = CRYPTO_AES_CBC; 223 |
|
215 return (g_eli_crypto_cipher(algo, 1, data, datasize, key, keysize)); 216} 217 218int 219g_eli_crypto_decrypt(u_int algo, u_char *data, size_t datasize, 220 const u_char *key, size_t keysize) 221{ 222 | 224 return (g_eli_crypto_cipher(algo, 1, data, datasize, key, keysize)); 225} 226 227int 228g_eli_crypto_decrypt(u_int algo, u_char *data, size_t datasize, 229 const u_char *key, size_t keysize) 230{ 231 |
232 /* We prefer AES-CBC for metadata protection. */ 233 if (algo == CRYPTO_AES_XTS) 234 algo = CRYPTO_AES_CBC; 235 |
|
223 return (g_eli_crypto_cipher(algo, 0, data, datasize, key, keysize)); 224} 225 226void 227g_eli_crypto_hmac_init(struct hmac_ctx *ctx, const uint8_t *hkey, 228 size_t hkeylen) 229{ 230 u_char k_ipad[128], key[128]; --- 63 unchanged lines hidden --- | 236 return (g_eli_crypto_cipher(algo, 0, data, datasize, key, keysize)); 237} 238 239void 240g_eli_crypto_hmac_init(struct hmac_ctx *ctx, const uint8_t *hkey, 241 size_t hkeylen) 242{ 243 u_char k_ipad[128], key[128]; --- 63 unchanged lines hidden --- |