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