xref: /freebsd-src/sys/contrib/openzfs/include/os/freebsd/zfs/sys/freebsd_crypto.h (revision 16d6b3b3da62aa5baaf3c66c8d4e6f8c8f70aeb7)
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_KEY_RAW	1
46 
47 #define	CRYPTO_BITS2BYTES(n) ((n) == 0 ? 0 : (((n) - 1) >> 3) + 1)
48 #define	CRYPTO_BYTES2BITS(n) ((n) << 3)
49 
50 struct zio_crypt_info;
51 
52 typedef struct freebsd_crypt_session {
53 	struct mtx		fs_lock;
54 	crypto_session_t	fs_sid;
55 	boolean_t	fs_done;
56 } freebsd_crypt_session_t;
57 
58 /*
59  * Unused types to minimize code differences.
60  */
61 typedef void *crypto_mechanism_t;
62 typedef void *crypto_ctx_template_t;
63 /*
64  * Unlike the ICP crypto_key type, this only
65  * supports <data, length> (the equivalent of
66  * CRYPTO_KEY_RAW).
67  */
68 typedef struct crypto_key {
69 	int	ck_format;	/* Unused, but minimizes code diff */
70 	void	*ck_data;
71 	size_t	ck_length;
72 } crypto_key_t;
73 
74 typedef struct hmac_ctx {
75 	SHA512_CTX	innerctx;
76 	SHA512_CTX	outerctx;
77 } *crypto_context_t;
78 
79 /*
80  * The only algorithm ZFS uses for hashing is SHA512_HMAC.
81  */
82 void crypto_mac(const crypto_key_t *key, const void *in_data,
83 	size_t in_data_size, void *out_data, size_t out_data_size);
84 void crypto_mac_init(struct hmac_ctx *ctx, const crypto_key_t *key);
85 void crypto_mac_update(struct hmac_ctx *ctx, const void *data,
86 	size_t data_size);
87 void crypto_mac_final(struct hmac_ctx *ctx, void *out_data,
88 	size_t out_data_size);
89 
90 int freebsd_crypt_newsession(freebsd_crypt_session_t *sessp,
91     struct zio_crypt_info *, crypto_key_t *);
92 void freebsd_crypt_freesession(freebsd_crypt_session_t *sessp);
93 
94 int freebsd_crypt_uio(boolean_t, freebsd_crypt_session_t *,
95 	struct zio_crypt_info *, uio_t *, crypto_key_t *, uint8_t *,
96 	size_t, size_t);
97 
98 #endif /* _ZFS_FREEBSD_CRYPTO_H */
99