1 /*
2  * Copyright (c) 2018 Sean Eric Fagan <sef@ixsystems.com>
3  * Portions Copyright (c) 2005-2011 Pawel Jakub Dawidek <pawel@dawidek.net>
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  *
27  * Portions of this file were taken from GELI's implementation of hmac.
28  *
29  * $FreeBSD$
30  */
31 
32 #ifndef _ZFS_FREEBSD_CRYPTO_H
33 #define	_ZFS_FREEBSD_CRYPTO_H
34 
35 #include <sys/errno.h>
36 #include <sys/mutex.h>
37 #include <opencrypto/cryptodev.h>
38 #include <crypto/sha2/sha256.h>
39 #include <crypto/sha2/sha512.h>
40 
41 #define	SUN_CKM_AES_CCM	"CKM_AES_CCM"
42 #define	SUN_CKM_AES_GCM	"CKM_AES_GCM"
43 #define	SUN_CKM_SHA512_HMAC	"CKM_SHA512_HMAC"
44 
45 #define	CRYPTO_BITS2BYTES(n) ((n) == 0 ? 0 : (((n) - 1) >> 3) + 1)
46 #define	CRYPTO_BYTES2BITS(n) ((n) << 3)
47 
48 struct zio_crypt_info;
49 
50 typedef struct freebsd_crypt_session {
51 	struct mtx		fs_lock;
52 	crypto_session_t	fs_sid;
53 	boolean_t	fs_done;
54 } freebsd_crypt_session_t;
55 
56 /*
57  * Unused types to minimize code differences.
58  */
59 typedef void *crypto_mechanism_t;
60 typedef void *crypto_ctx_template_t;
61 /*
62  * Like the ICP crypto_key type, this only
63  * supports <data, length> (the equivalent of
64  * the former CRYPTO_KEY_RAW).
65  */
66 typedef struct crypto_key {
67 	void	*ck_data;
68 	size_t	ck_length;
69 } crypto_key_t;
70 
71 typedef struct hmac_ctx {
72 	SHA512_CTX	innerctx;
73 	SHA512_CTX	outerctx;
74 } *crypto_context_t;
75 
76 /*
77  * The only algorithm ZFS uses for hashing is SHA512_HMAC.
78  */
79 void crypto_mac(const crypto_key_t *key, const void *in_data,
80 	size_t in_data_size, void *out_data, size_t out_data_size);
81 void crypto_mac_init(struct hmac_ctx *ctx, const crypto_key_t *key);
82 void crypto_mac_update(struct hmac_ctx *ctx, const void *data,
83 	size_t data_size);
84 void crypto_mac_final(struct hmac_ctx *ctx, void *out_data,
85 	size_t out_data_size);
86 
87 int freebsd_crypt_newsession(freebsd_crypt_session_t *sessp,
88     const struct zio_crypt_info *, crypto_key_t *);
89 void freebsd_crypt_freesession(freebsd_crypt_session_t *sessp);
90 
91 int freebsd_crypt_uio(boolean_t, freebsd_crypt_session_t *,
92 	const struct zio_crypt_info *, zfs_uio_t *, crypto_key_t *, uint8_t *,
93 	size_t, size_t);
94 
95 #endif /* _ZFS_FREEBSD_CRYPTO_H */
96