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 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
72 KASSERT(algo != CRYPTO_AES_XTS,
73 ("%s: CRYPTO_AES_XTS unexpected here", __func__));
74
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
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
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
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 ---
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 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
72 KASSERT(algo != CRYPTO_AES_XTS,
73 ("%s: CRYPTO_AES_XTS unexpected here", __func__));
74
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
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
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
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 ---