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