1/*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3 *
4 * Copyright (c) 2003-2012 Broadcom Corporation
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 *
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in
15 *    the documentation and/or other materials provided with the
16 *    distribution.
17 *
18 * THIS SOFTWARE IS PROVIDED BY BROADCOM ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL BROADCOM OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
25 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
27 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
28 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 *
30 * $FreeBSD$
31 */
32
33#ifndef _NLMSECLIB_H_
34#define	_NLMSECLIB_H_
35
36/*
37 * Cryptographic parameter definitions
38 */
39#define	XLP_SEC_DES_KEY_LENGTH		8	/* Bytes */
40#define	XLP_SEC_3DES_KEY_LENGTH		24	/* Bytes */
41#define	XLP_SEC_AES128_KEY_LENGTH	16	/* Bytes */
42#define	XLP_SEC_AES192_KEY_LENGTH	24	/* Bytes */
43#define	XLP_SEC_AES256_KEY_LENGTH	32	/* Bytes */
44#define	XLP_SEC_AES128F8_KEY_LENGTH	32	/* Bytes */
45#define	XLP_SEC_AES192F8_KEY_LENGTH	48	/* Bytes */
46#define	XLP_SEC_AES256F8_KEY_LENGTH	64	/* Bytes */
47#define	XLP_SEC_KASUMI_F8_KEY_LENGTH	16	/* Bytes */
48#define	XLP_SEC_MAX_CRYPT_KEY_LENGTH	XLP_SEC_AES256F8_KEY_LENGTH
49
50#define	XLP_SEC_DES_IV_LENGTH		8	/* Bytes */
51#define	XLP_SEC_AES_IV_LENGTH		16	/* Bytes */
52#define	XLP_SEC_ARC4_IV_LENGTH		0	/* Bytes */
53#define	XLP_SEC_KASUMI_F8_IV_LENGTH	16	/* Bytes */
54#define	XLP_SEC_MAX_IV_LENGTH		16	/* Bytes */
55#define	XLP_SEC_IV_LENGTH_BYTES		8	/* Bytes */
56
57#define	XLP_SEC_AES_BLOCK_SIZE		16	/* Bytes */
58#define	XLP_SEC_DES_BLOCK_SIZE		8	/* Bytes */
59#define	XLP_SEC_3DES_BLOCK_SIZE		8	/* Bytes */
60
61#define	XLP_SEC_MD5_BLOCK_SIZE		64	/* Bytes */
62#define	XLP_SEC_SHA1_BLOCK_SIZE		64	/* Bytes */
63#define	XLP_SEC_SHA256_BLOCK_SIZE	64	/* Bytes */
64#define	XLP_SEC_SHA384_BLOCK_SIZE	128	/* Bytes */
65#define	XLP_SEC_SHA512_BLOCK_SIZE	128	/* Bytes */
66#define	XLP_SEC_GCM_BLOCK_SIZE		16	/* XXX: Bytes */
67#define	XLP_SEC_KASUMI_F9_BLOCK_SIZE	16	/* XXX: Bytes */
68#define	XLP_SEC_MAX_BLOCK_SIZE		64	/* Max of MD5/SHA */
69#define	XLP_SEC_MD5_LENGTH		16	/* Bytes */
70#define	XLP_SEC_SHA1_LENGTH		20	/* Bytes */
71#define	XLP_SEC_SHA256_LENGTH		32	/* Bytes */
72#define	XLP_SEC_SHA384_LENGTH		64	/* Bytes */
73#define	XLP_SEC_SHA512_LENGTH		64	/* Bytes */
74#define	XLP_SEC_GCM_LENGTH		16	/* Bytes */
75#define	XLP_SEC_KASUMI_F9_LENGTH	16	/* Bytes */
76#define	XLP_SEC_KASUMI_F9_RESULT_LENGTH	4	/* Bytes */
77#define	XLP_SEC_HMAC_LENGTH		64	/* Max of MD5/SHA/SHA256 */
78#define	XLP_SEC_MAX_AUTH_KEY_LENGTH	XLP_SEC_SHA512_BLOCK_SIZE
79#define	XLP_SEC_MAX_RC4_STATE_SIZE	264	/* char s[256], int i, int j */
80
81#define	CRYPTO_ERROR(msg1)	((unsigned int)msg1)
82
83#define	NLM_CRYPTO_LEFT_REQS (CMS_DEFAULT_CREDIT/2)
84#define	NLM_CRYPTO_NUM_SEGS_REQD(__bufsize)				\
85	((__bufsize + NLM_CRYPTO_MAX_SEG_LEN - 1) / NLM_CRYPTO_MAX_SEG_LEN)
86
87#define	NLM_CRYPTO_PKT_DESC_SIZE(nsegs) (32 + (nsegs * 16))
88
89extern unsigned int creditleft;
90
91struct xlp_sec_command {
92	struct cryptop *crp;
93	struct xlp_sec_session *ses;
94	struct nlm_crypto_pkt_ctrl *ctrlp;
95	struct nlm_crypto_pkt_param *paramp;
96	void *iv;
97	uint8_t des3key[24];
98	uint8_t *hashdest;
99	uint8_t hashsrc;
100	uint8_t hmacpad;
101	uint32_t hashoff;
102	uint32_t hashlen;
103	uint32_t cipheroff;
104	uint32_t cipherlen;
105	uint32_t ivoff;
106	uint32_t ivlen;
107	uint32_t hashalg;
108	uint32_t hashmode;
109	uint32_t cipheralg;
110	uint32_t ciphermode;
111	uint32_t nsegs;
112	uint32_t hash_dst_len; /* used to store hash alg dst size */
113};
114
115struct xlp_sec_session {
116	int hs_mlen;
117};
118
119/*
120 * Holds data specific to nlm security accelerators
121 */
122struct xlp_sec_softc {
123	device_t sc_dev;	/* device backpointer */
124	uint64_t sec_base;
125	int32_t sc_cid;
126	int sc_needwakeup;
127	uint32_t sec_vc_start;
128	uint32_t sec_vc_end;
129	uint32_t sec_msgsz;
130};
131
132#ifdef NLM_SEC_DEBUG
133void	print_crypto_params(struct xlp_sec_command *cmd, struct nlm_fmn_msg m);
134void	print_cmd(struct xlp_sec_command *cmd);
135#endif
136int	nlm_crypto_form_srcdst_segs(struct xlp_sec_command *cmd,
137	    const struct crypto_session_params *csp);
138int	nlm_crypto_do_cipher(struct xlp_sec_softc *sc,
139	    struct xlp_sec_command *cmd,
140	    const struct crypto_session_params *csp);
141int	nlm_crypto_do_digest(struct xlp_sec_softc *sc,
142	    struct xlp_sec_command *cmd,
143	    const struct crypto_session_params *csp);
144int	nlm_crypto_do_cipher_digest(struct xlp_sec_softc *sc,
145	    struct xlp_sec_command *cmd,
146	    const struct crypto_session_params *csp);
147int	nlm_get_digest_param(struct xlp_sec_command *cmd,
148	    const struct crypto_session_params *csp);
149int	nlm_get_cipher_param(struct xlp_sec_command *cmd,
150	    const struct crypto_session_params *csp);
151
152#endif /* _NLMSECLIB_H_ */
153