Home | History | Annotate | Line # | Download | only in opencrypto
      1  1.51  riastrad /*	$NetBSD: cryptodev.h,v 1.51 2023/07/11 10:42:16 riastradh Exp $ */
      2   1.5  jonathan /*	$FreeBSD: src/sys/opencrypto/cryptodev.h,v 1.2.2.6 2003/07/02 17:04:50 sam Exp $	*/
      3   1.1  jonathan /*	$OpenBSD: cryptodev.h,v 1.33 2002/07/17 23:52:39 art Exp $	*/
      4   1.1  jonathan 
      5  1.13       tls /*-
      6  1.13       tls  * Copyright (c) 2008 The NetBSD Foundation, Inc.
      7  1.13       tls  * All rights reserved.
      8  1.13       tls  *
      9  1.13       tls  * This code is derived from software contributed to The NetBSD Foundation
     10  1.13       tls  * by Coyote Point Systems, Inc.
     11  1.13       tls  *
     12  1.13       tls  * Redistribution and use in source and binary forms, with or without
     13  1.13       tls  * modification, are permitted provided that the following conditions
     14  1.13       tls  * are met:
     15  1.13       tls  * 1. Redistributions of source code must retain the above copyright
     16  1.13       tls  *    notice, this list of conditions and the following disclaimer.
     17  1.13       tls  * 2. Redistributions in binary form must reproduce the above copyright
     18  1.13       tls  *    notice, this list of conditions and the following disclaimer in the
     19  1.13       tls  *    documentation and/or other materials provided with the distribution.
     20  1.13       tls  *
     21  1.13       tls  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     22  1.13       tls  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     23  1.13       tls  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     24  1.13       tls  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     25  1.13       tls  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     26  1.13       tls  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     27  1.13       tls  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     28  1.13       tls  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     29  1.13       tls  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     30  1.13       tls  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     31  1.13       tls  * POSSIBILITY OF SUCH DAMAGE.
     32  1.13       tls  */
     33  1.13       tls 
     34   1.1  jonathan /*
     35   1.1  jonathan  * The author of this code is Angelos D. Keromytis (angelos (at) cis.upenn.edu)
     36   1.1  jonathan  *
     37   1.1  jonathan  * This code was written by Angelos D. Keromytis in Athens, Greece, in
     38   1.1  jonathan  * February 2000. Network Security Technologies Inc. (NSTI) kindly
     39   1.1  jonathan  * supported the development of this code.
     40   1.1  jonathan  *
     41   1.1  jonathan  * Copyright (c) 2000 Angelos D. Keromytis
     42   1.1  jonathan  *
     43   1.1  jonathan  * Permission to use, copy, and modify this software with or without fee
     44   1.1  jonathan  * is hereby granted, provided that this entire notice is included in
     45   1.1  jonathan  * all source code copies of any software which is or includes a copy or
     46   1.1  jonathan  * modification of this software.
     47   1.1  jonathan  *
     48   1.1  jonathan  * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR
     49   1.1  jonathan  * IMPLIED WARRANTY. IN PARTICULAR, NONE OF THE AUTHORS MAKES ANY
     50   1.1  jonathan  * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE
     51   1.1  jonathan  * MERCHANTABILITY OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR
     52   1.1  jonathan  * PURPOSE.
     53   1.1  jonathan  *
     54   1.1  jonathan  * Copyright (c) 2001 Theo de Raadt
     55   1.1  jonathan  *
     56   1.1  jonathan  * Redistribution and use in source and binary forms, with or without
     57   1.1  jonathan  * modification, are permitted provided that the following conditions
     58   1.1  jonathan  * are met:
     59   1.1  jonathan  *
     60   1.1  jonathan  * 1. Redistributions of source code must retain the above copyright
     61   1.1  jonathan  *   notice, this list of conditions and the following disclaimer.
     62   1.1  jonathan  * 2. Redistributions in binary form must reproduce the above copyright
     63   1.1  jonathan  *   notice, this list of conditions and the following disclaimer in the
     64   1.1  jonathan  *   documentation and/or other materials provided with the distribution.
     65   1.1  jonathan  * 3. The name of the author may not be used to endorse or promote products
     66   1.1  jonathan  *   derived from this software without specific prior written permission.
     67   1.1  jonathan  *
     68   1.1  jonathan  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     69   1.1  jonathan  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     70   1.1  jonathan  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     71   1.1  jonathan  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     72   1.1  jonathan  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     73   1.1  jonathan  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     74   1.1  jonathan  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     75   1.1  jonathan  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     76   1.1  jonathan  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     77   1.1  jonathan  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     78   1.1  jonathan  *
     79   1.1  jonathan  * Effort sponsored in part by the Defense Advanced Research Projects
     80   1.1  jonathan  * Agency (DARPA) and Air Force Research Laboratory, Air Force
     81   1.1  jonathan  * Materiel Command, USAF, under agreement number F30602-01-2-0537.
     82   1.1  jonathan  *
     83   1.1  jonathan  */
     84   1.1  jonathan 
     85   1.1  jonathan #ifndef _CRYPTO_CRYPTO_H_
     86   1.1  jonathan #define _CRYPTO_CRYPTO_H_
     87   1.1  jonathan 
     88  1.51  riastrad #include <sys/types.h>
     89  1.51  riastrad 
     90   1.1  jonathan #include <sys/ioccom.h>
     91  1.27  dholland #include <sys/time.h>
     92   1.1  jonathan 
     93  1.29  knakahar #if defined(_KERNEL_OPT)
     94  1.29  knakahar #include "opt_ocf.h"
     95  1.29  knakahar #endif
     96  1.29  knakahar 
     97   1.1  jonathan /* Some initial values */
     98   1.1  jonathan #define CRYPTO_DRIVERS_INITIAL	4
     99   1.1  jonathan #define CRYPTO_SW_SESSIONS	32
    100   1.1  jonathan 
    101   1.1  jonathan /* HMAC values */
    102  1.19  drochner #define HMAC_BLOCK_LEN		64 /* for compatibility */
    103   1.1  jonathan #define HMAC_IPAD_VAL		0x36
    104   1.1  jonathan #define HMAC_OPAD_VAL		0x5C
    105   1.1  jonathan 
    106   1.1  jonathan /* Encryption algorithm block sizes */
    107   1.1  jonathan #define DES_BLOCK_LEN		8
    108   1.1  jonathan #define DES3_BLOCK_LEN		8
    109   1.1  jonathan #define BLOWFISH_BLOCK_LEN	8
    110   1.1  jonathan #define SKIPJACK_BLOCK_LEN	8
    111   1.1  jonathan #define CAST128_BLOCK_LEN	8
    112   1.1  jonathan #define RIJNDAEL128_BLOCK_LEN	16
    113   1.1  jonathan #define EALG_MAX_BLOCK_LEN	16 /* Keep this updated */
    114   1.1  jonathan 
    115   1.1  jonathan /* Maximum hash algorithm result length */
    116   1.1  jonathan #define AALG_MAX_RESULT_LEN	64 /* Keep this updated */
    117   1.1  jonathan 
    118   1.1  jonathan #define	CRYPTO_ALGORITHM_MIN	1
    119   1.1  jonathan #define CRYPTO_DES_CBC		1
    120   1.1  jonathan #define CRYPTO_3DES_CBC		2
    121   1.1  jonathan #define CRYPTO_BLF_CBC		3
    122   1.1  jonathan #define CRYPTO_CAST_CBC		4
    123   1.1  jonathan #define CRYPTO_SKIPJACK_CBC	5
    124   1.1  jonathan #define CRYPTO_MD5_HMAC		6
    125   1.1  jonathan #define CRYPTO_SHA1_HMAC	7
    126   1.1  jonathan #define CRYPTO_RIPEMD160_HMAC	8
    127   1.1  jonathan #define CRYPTO_MD5_KPDK		9
    128   1.1  jonathan #define CRYPTO_SHA1_KPDK	10
    129   1.1  jonathan #define CRYPTO_RIJNDAEL128_CBC	11 /* 128 bit blocksize */
    130   1.1  jonathan #define CRYPTO_AES_CBC		11 /* 128 bit blocksize -- the same as above */
    131   1.1  jonathan #define CRYPTO_ARC4		12
    132   1.1  jonathan #define CRYPTO_MD5		13
    133   1.1  jonathan #define CRYPTO_SHA1		14
    134  1.19  drochner #define CRYPTO_SHA2_256_HMAC	15
    135  1.19  drochner #define CRYPTO_SHA2_HMAC	CRYPTO_SHA2_256_HMAC /* for compatibility */
    136   1.1  jonathan #define CRYPTO_NULL_HMAC	16
    137   1.1  jonathan #define CRYPTO_NULL_CBC		17
    138   1.1  jonathan #define CRYPTO_DEFLATE_COMP	18 /* Deflate compression algorithm */
    139  1.11       tls #define CRYPTO_MD5_HMAC_96	19
    140  1.11       tls #define CRYPTO_SHA1_HMAC_96	20
    141  1.11       tls #define CRYPTO_RIPEMD160_HMAC_96	21
    142  1.18  drochner #define CRYPTO_GZIP_COMP	22 /* gzip compression algorithm */
    143  1.18  drochner #define CRYPTO_DEFLATE_COMP_NOGROW 23 /* Deflate, fail if not compressible */
    144  1.19  drochner #define CRYPTO_SHA2_384_HMAC	24
    145  1.19  drochner #define CRYPTO_SHA2_512_HMAC	25
    146  1.20  drochner #define CRYPTO_CAMELLIA_CBC	26
    147  1.22  drochner #define CRYPTO_AES_CTR		27
    148  1.23  drochner #define CRYPTO_AES_XCBC_MAC_96	28
    149  1.24  drochner #define CRYPTO_AES_GCM_16	29
    150  1.24  drochner #define CRYPTO_AES_128_GMAC	30
    151  1.24  drochner #define CRYPTO_AES_192_GMAC	31
    152  1.24  drochner #define CRYPTO_AES_256_GMAC	32
    153  1.24  drochner #define CRYPTO_AES_GMAC		33
    154  1.24  drochner #define CRYPTO_ALGORITHM_MAX	33 /* Keep updated - see below */
    155   1.1  jonathan 
    156   1.1  jonathan /* Algorithm flags */
    157   1.1  jonathan #define	CRYPTO_ALG_FLAG_SUPPORTED	0x01 /* Algorithm is supported */
    158   1.1  jonathan #define	CRYPTO_ALG_FLAG_RNG_ENABLE	0x02 /* Has HW RNG for DH/DSA */
    159   1.1  jonathan #define	CRYPTO_ALG_FLAG_DSA_SHA		0x04 /* Can do SHA on msg */
    160   1.1  jonathan 
    161   1.1  jonathan struct session_op {
    162   1.1  jonathan 	u_int32_t	cipher;		/* ie. CRYPTO_DES_CBC */
    163   1.1  jonathan 	u_int32_t	mac;		/* ie. CRYPTO_MD5_HMAC */
    164  1.16    darran 	u_int32_t	comp_alg;	/* ie. CRYPTO_GZIP_COMP */
    165   1.1  jonathan 
    166   1.1  jonathan 	u_int32_t	keylen;		/* cipher key */
    167   1.8  christos 	void *		key;
    168   1.1  jonathan 	int		mackeylen;	/* mac key */
    169   1.8  christos 	void *		mackey;
    170   1.1  jonathan 
    171   1.6     perry   	u_int32_t	ses;		/* returns: session # */
    172   1.1  jonathan };
    173   1.1  jonathan 
    174  1.13       tls /* to support multiple session creation */
    175  1.13       tls 
    176  1.13       tls struct session_n_op {
    177  1.13       tls 	u_int32_t	cipher;		/* ie. CRYPTO_DES_CBC */
    178  1.13       tls 	u_int32_t	mac;		/* ie. CRYPTO_MD5_HMAC */
    179  1.16    darran 	u_int32_t	comp_alg;	/* ie. CRYPTO_GZIP_COMP */
    180  1.13       tls 
    181  1.13       tls 	u_int32_t	keylen;		/* cipher key */
    182  1.13       tls 	void *		key;
    183  1.13       tls 	int		mackeylen;	/* mac key */
    184  1.13       tls 	void *		mackey;
    185  1.13       tls 
    186  1.13       tls 	u_int32_t	ses;		/* returns: session # */
    187  1.13       tls 	int		status;
    188  1.13       tls };
    189  1.13       tls 
    190   1.1  jonathan struct crypt_op {
    191   1.1  jonathan 	u_int32_t	ses;
    192   1.1  jonathan 	u_int16_t	op;		/* i.e. COP_ENCRYPT */
    193   1.1  jonathan #define COP_ENCRYPT	1
    194   1.1  jonathan #define COP_DECRYPT	2
    195  1.16    darran #define COP_COMP	3
    196  1.16    darran #define COP_DECOMP	4
    197   1.1  jonathan 	u_int16_t	flags;
    198   1.1  jonathan #define	COP_F_BATCH 	0x0008		/* Dispatch as quickly as possible */
    199  1.16    darran 	u_int		len;		/* src len */
    200   1.8  christos 	void *		src, *dst;	/* become iov[] inside kernel */
    201   1.8  christos 	void *		mac;		/* must be big enough for chosen MAC */
    202   1.8  christos 	void *		iv;
    203  1.16    darran 	u_int		dst_len;	/* dst len if not 0 */
    204   1.1  jonathan };
    205   1.1  jonathan 
    206  1.13       tls /* to support multiple session creation */
    207  1.13       tls /*
    208  1.13       tls  *
    209  1.13       tls  * The reqid field is filled when the operation has
    210  1.13       tls  * been accepted and started, and can be used to later retrieve
    211  1.13       tls  * the operation results via CIOCNCRYPTRET or identify the
    212  1.13       tls  * request in the completion list returned by CIOCNCRYPTRETM.
    213  1.13       tls  *
    214  1.13       tls  * The opaque pointer can be set arbitrarily by the user
    215  1.13       tls  * and it is passed back in the crypt_result structure
    216  1.13       tls  * when the request completes.  This field can be used for example
    217  1.13       tls  * to track context for the request and avoid lookups in the
    218  1.13       tls  * user application.
    219  1.13       tls  */
    220  1.13       tls 
    221  1.13       tls struct crypt_n_op {
    222  1.13       tls 	u_int32_t	ses;
    223  1.13       tls 	u_int16_t	op;		/* i.e. COP_ENCRYPT */
    224  1.13       tls #define COP_ENCRYPT	1
    225  1.13       tls #define COP_DECRYPT	2
    226  1.13       tls 	u_int16_t	flags;
    227  1.13       tls #define COP_F_BATCH	0x0008		/* Dispatch as quickly as possible */
    228  1.16    darran #define COP_F_MORE	0x0010		/* more data to follow */
    229  1.16    darran 	u_int		len;		/* src len */
    230  1.13       tls 
    231  1.13       tls 	u_int32_t	reqid;		/* request id */
    232  1.13       tls 	int		status;		/* status of request -accepted or not */
    233  1.13       tls 	void		*opaque;	/* opaque pointer returned to user */
    234  1.13       tls 	u_int32_t	keylen;		/* cipher key - optional */
    235  1.13       tls 	void *		key;
    236  1.13       tls 	u_int32_t	mackeylen;	/* also optional */
    237  1.13       tls 	void *		mackey;
    238  1.13       tls 
    239  1.13       tls 	void *		src, *dst;	/* become iov[] inside kernel */
    240  1.13       tls 	void *		mac;		/* must be big enough for chosen MAC */
    241  1.13       tls 	void *		iv;
    242  1.16    darran 	u_int		dst_len;	/* dst len if not 0 */
    243  1.13       tls };
    244  1.13       tls 
    245  1.13       tls /* CIOCNCRYPTM ioctl argument, supporting one or more asynchronous
    246  1.13       tls  * crypt_n_op operations.
    247  1.13       tls  * Each crypt_n_op will receive a request id which can be used to check its
    248  1.13       tls  * status via CIOCNCRYPTRET, or to watch for its completion in the list
    249  1.13       tls  * obtained via CIOCNCRYPTRETM.
    250  1.13       tls  */
    251  1.13       tls struct crypt_mop {
    252  1.13       tls 	size_t 		count;		/* how many */
    253  1.13       tls 	struct crypt_n_op *	reqs;	/* where to get them */
    254  1.13       tls };
    255  1.13       tls 
    256  1.13       tls struct crypt_sfop {
    257  1.13       tls 	size_t		count;
    258  1.13       tls 	u_int32_t	*sesid;
    259  1.28   msaitoh };
    260  1.13       tls 
    261  1.13       tls struct crypt_sgop {
    262  1.13       tls 	size_t		count;
    263  1.13       tls 	struct session_n_op * sessions;
    264  1.13       tls };
    265  1.13       tls 
    266  1.40    hikaru #define CRYPTO_MAX_MAC_LEN	32 /* Keep this updated */
    267   1.1  jonathan 
    268   1.1  jonathan /* bignum parameter, in packed bytes, ... */
    269   1.1  jonathan struct crparam {
    270   1.8  christos 	void *		crp_p;
    271   1.1  jonathan 	u_int		crp_nbits;
    272   1.1  jonathan };
    273   1.1  jonathan 
    274   1.1  jonathan #define CRK_MAXPARAM	8
    275   1.1  jonathan 
    276   1.1  jonathan struct crypt_kop {
    277   1.1  jonathan 	u_int		crk_op;		/* ie. CRK_MOD_EXP or other */
    278   1.1  jonathan 	u_int		crk_status;	/* return status */
    279   1.1  jonathan 	u_short		crk_iparams;	/* # of input parameters */
    280   1.1  jonathan 	u_short		crk_oparams;	/* # of output parameters */
    281   1.1  jonathan 	u_int		crk_pad1;
    282   1.1  jonathan 	struct crparam	crk_param[CRK_MAXPARAM];
    283   1.1  jonathan };
    284  1.13       tls 
    285  1.13       tls /*
    286  1.13       tls  * Used with the CIOCNFKEYM ioctl.
    287  1.13       tls  *
    288  1.13       tls  * This structure allows the OCF to return a request id
    289  1.13       tls  * for each of the kop operations specified in the CIOCNFKEYM call.
    290  1.13       tls  *
    291  1.13       tls  * The crk_opaque pointer can be arbitrarily set by the user
    292  1.13       tls  * and it is passed back in the crypt_result structure
    293  1.13       tls  * when the request completes.  This field can be used for example
    294  1.13       tls  * to track context for the request and avoid lookups in the
    295  1.13       tls  * user application.
    296  1.13       tls  */
    297  1.13       tls struct crypt_n_kop {
    298  1.13       tls 	u_int		crk_op;		/* ie. CRK_MOD_EXP or other */
    299  1.13       tls 	u_int		crk_status;	/* return status */
    300  1.13       tls 	u_short		crk_iparams;	/* # of input parameters */
    301  1.13       tls 	u_short		crk_oparams;	/* # of output parameters */
    302  1.13       tls         u_int32_t	crk_reqid;	/* request id */
    303  1.13       tls 	struct crparam	crk_param[CRK_MAXPARAM];
    304  1.13       tls 	void		*crk_opaque;	/* opaque pointer returned to user */
    305  1.13       tls };
    306  1.13       tls 
    307  1.13       tls struct crypt_mkop {
    308  1.13       tls 	size_t	count;			/* how many */
    309  1.13       tls 	struct crypt_n_kop *	reqs;	/* where to get them */
    310  1.13       tls };
    311  1.13       tls 
    312  1.13       tls /* Asynchronous key or crypto result.
    313  1.13       tls  * Note that the status will be set in the crypt_result structure,
    314  1.13       tls  * not in the original crypt_kop structure (crk_status).
    315  1.13       tls  */
    316  1.13       tls struct crypt_result {
    317  1.13       tls 	u_int32_t	reqid;		/* request id */
    318  1.13       tls 	u_int32_t	status;		/* status of request: 0 if successful */
    319  1.13       tls 	void *		opaque;		/* Opaque pointer from the user, passed along */
    320  1.13       tls };
    321  1.13       tls 
    322  1.13       tls struct cryptret {
    323  1.13       tls 	size_t		count;		/* space for how many */
    324  1.13       tls 	struct crypt_result *	results;	/* where to put them */
    325  1.28   msaitoh };
    326  1.13       tls 
    327  1.13       tls 
    328  1.41    andvar /* Asymmetric key operations */
    329  1.42    andvar #define	CRK_ALGORITHM_MIN	0
    330   1.1  jonathan #define CRK_MOD_EXP		0
    331   1.1  jonathan #define CRK_MOD_EXP_CRT		1
    332   1.1  jonathan #define CRK_DSA_SIGN		2
    333   1.1  jonathan #define CRK_DSA_VERIFY		3
    334   1.1  jonathan #define CRK_DH_COMPUTE_KEY	4
    335   1.9       tls #define CRK_MOD_ADD		5
    336   1.9       tls #define CRK_MOD_ADDINV		6
    337   1.9       tls #define CRK_MOD_SUB		7
    338   1.9       tls #define CRK_MOD_MULT		8
    339   1.9       tls #define CRK_MOD_MULTINV		9
    340   1.9       tls #define CRK_MOD			10
    341   1.9       tls #define CRK_ALGORITHM_MAX	10 /* Keep updated - see below */
    342   1.1  jonathan 
    343   1.1  jonathan #define CRF_MOD_EXP		(1 << CRK_MOD_EXP)
    344   1.1  jonathan #define CRF_MOD_EXP_CRT		(1 << CRK_MOD_EXP_CRT)
    345   1.1  jonathan #define CRF_DSA_SIGN		(1 << CRK_DSA_SIGN)
    346   1.1  jonathan #define CRF_DSA_VERIFY		(1 << CRK_DSA_VERIFY)
    347   1.1  jonathan #define CRF_DH_COMPUTE_KEY	(1 << CRK_DH_COMPUTE_KEY)
    348   1.9       tls #define CRF_MOD_ADD		(1 << CRK_MOD_ADD)
    349   1.9       tls #define CRF_MOD_ADDINV		(1 << CRK_MOD_ADDINV)
    350   1.9       tls #define CRF_MOD_SUB		(1 << CRK_MOD_SUB)
    351   1.9       tls #define CRF_MOD_MULT		(1 << CRK_MOD_MULT)
    352   1.9       tls #define CRF_MOD_MULTINV		(1 << CRK_MOD_MULTINV)
    353   1.9       tls #define CRF_MOD			(1 << CRK_MOD)
    354   1.1  jonathan 
    355   1.1  jonathan /*
    356  1.13       tls  * A large comment here once held descriptions of the ioctl
    357  1.13       tls  * requests implemented by the device.  This text has been moved
    358  1.13       tls  * to the crypto(4) manual page and, later, removed from this file
    359  1.13       tls  * as it was always a step behind the times.
    360  1.13       tls  */
    361  1.13       tls 
    362  1.13       tls /*
    363   1.1  jonathan  * done against open of /dev/crypto, to get a cloned descriptor.
    364  1.13       tls  * Please use F_SETFD against the cloned descriptor.  But this ioctl
    365  1.13       tls  * is obsolete (the device now clones): please, just don't use it.
    366   1.1  jonathan  */
    367   1.1  jonathan #define	CRIOGET		_IOWR('c', 100, u_int32_t)
    368   1.1  jonathan 
    369   1.1  jonathan /* the following are done against the cloned descriptor */
    370   1.1  jonathan #define	CIOCFSESSION	_IOW('c', 102, u_int32_t)
    371   1.1  jonathan #define CIOCKEY		_IOWR('c', 104, struct crypt_kop)
    372  1.13       tls #define CIOCNFKEYM	_IOWR('c', 108, struct crypt_mkop)
    373  1.13       tls #define CIOCNFSESSION	_IOW('c', 109, struct crypt_sfop)
    374  1.13       tls #define CIOCNCRYPTRETM	_IOWR('c', 110, struct cryptret)
    375  1.13       tls #define CIOCNCRYPTRET	_IOWR('c', 111, struct crypt_result)
    376   1.1  jonathan 
    377  1.16    darran #define	CIOCGSESSION	_IOWR('c', 112, struct session_op)
    378  1.16    darran #define	CIOCNGSESSION	_IOWR('c', 113, struct crypt_sgop)
    379  1.16    darran #define CIOCCRYPT	_IOWR('c', 114, struct crypt_op)
    380  1.16    darran #define CIOCNCRYPTM	_IOWR('c', 115, struct crypt_mop)
    381  1.16    darran 
    382   1.1  jonathan #define CIOCASYMFEAT	_IOR('c', 105, u_int32_t)
    383   1.1  jonathan 
    384   1.1  jonathan struct cryptotstat {
    385   1.1  jonathan 	struct timespec	acc;		/* total accumulated time */
    386   1.1  jonathan 	struct timespec	min;		/* max time */
    387   1.1  jonathan 	struct timespec	max;		/* max time */
    388   1.1  jonathan 	u_int32_t	count;		/* number of observations */
    389   1.1  jonathan };
    390   1.1  jonathan 
    391   1.1  jonathan struct cryptostats {
    392   1.1  jonathan 	u_int32_t	cs_ops;		/* symmetric crypto ops submitted */
    393   1.1  jonathan 	u_int32_t	cs_errs;	/* symmetric crypto ops that failed */
    394  1.41    andvar 	u_int32_t	cs_kops;	/* asymmetric/key ops submitted */
    395  1.41    andvar 	u_int32_t	cs_kerrs;	/* asymmetric/key ops that failed */
    396   1.1  jonathan 	u_int32_t	cs_intrs;	/* crypto swi thread activations */
    397   1.1  jonathan 	u_int32_t	cs_rets;	/* crypto return thread activations */
    398   1.1  jonathan 	u_int32_t	cs_blocks;	/* symmetric op driver block */
    399   1.1  jonathan 	u_int32_t	cs_kblocks;	/* symmetric op driver block */
    400   1.1  jonathan 	/*
    401   1.1  jonathan 	 * When CRYPTO_TIMING is defined at compile time and the
    402   1.1  jonathan 	 * sysctl debug.crypto is set to 1, the crypto system will
    403   1.1  jonathan 	 * accumulate statistics about how long it takes to process
    404   1.1  jonathan 	 * crypto requests at various points during processing.
    405   1.1  jonathan 	 */
    406  1.42    andvar 	struct cryptotstat cs_invoke;	/* crypto_dispatch -> crypto_invoke */
    407   1.1  jonathan 	struct cryptotstat cs_done;	/* crypto_invoke -> crypto_done */
    408   1.1  jonathan 	struct cryptotstat cs_cb;	/* crypto_done -> callback */
    409   1.1  jonathan 	struct cryptotstat cs_finis;	/* callback -> callback return */
    410   1.1  jonathan };
    411   1.1  jonathan 
    412   1.1  jonathan #ifdef _KERNEL
    413  1.51  riastrad 
    414  1.51  riastrad #include <sys/condvar.h>
    415  1.51  riastrad #include <sys/malloc.h>
    416  1.51  riastrad #include <sys/mutex.h>
    417  1.51  riastrad #include <sys/queue.h>
    418  1.51  riastrad #include <sys/systm.h>
    419  1.51  riastrad 
    420  1.51  riastrad struct cpu_info;
    421  1.51  riastrad struct uio;
    422  1.51  riastrad 
    423   1.1  jonathan /* Standard initialization structure beginning */
    424   1.1  jonathan struct cryptoini {
    425   1.1  jonathan 	int		cri_alg;	/* Algorithm to use */
    426   1.1  jonathan 	int		cri_klen;	/* Key length, in bits */
    427   1.1  jonathan 	int		cri_rnd;	/* Algorithm rounds, where relevant */
    428   1.8  christos 	char	       *cri_key;	/* key to use */
    429   1.1  jonathan 	u_int8_t	cri_iv[EALG_MAX_BLOCK_LEN];	/* IV to use */
    430   1.1  jonathan 	struct cryptoini *cri_next;
    431   1.1  jonathan };
    432   1.1  jonathan 
    433   1.1  jonathan /* Describe boundaries of a single crypto operation */
    434   1.1  jonathan struct cryptodesc {
    435   1.1  jonathan 	int		crd_skip;	/* How many bytes to ignore from start */
    436   1.1  jonathan 	int		crd_len;	/* How many bytes to process */
    437   1.1  jonathan 	int		crd_inject;	/* Where to inject results, if applicable */
    438   1.1  jonathan 	int		crd_flags;
    439   1.1  jonathan 
    440   1.1  jonathan #define	CRD_F_ENCRYPT		0x01	/* Set when doing encryption */
    441   1.1  jonathan #define	CRD_F_IV_PRESENT	0x02	/* When encrypting, IV is already in
    442   1.1  jonathan 					   place, so don't copy. */
    443   1.1  jonathan #define	CRD_F_IV_EXPLICIT	0x04	/* IV explicitly provided */
    444   1.1  jonathan #define	CRD_F_DSA_SHA_NEEDED	0x08	/* Compute SHA-1 of buffer for DSA */
    445  1.16    darran #define CRD_F_COMP		0x10    /* Set when doing compression */
    446   1.1  jonathan 
    447   1.1  jonathan 	struct cryptoini	CRD_INI; /* Initialization/context data */
    448   1.1  jonathan #define crd_iv		CRD_INI.cri_iv
    449   1.1  jonathan #define crd_key		CRD_INI.cri_key
    450   1.1  jonathan #define crd_rnd		CRD_INI.cri_rnd
    451   1.1  jonathan #define crd_alg		CRD_INI.cri_alg
    452   1.1  jonathan #define crd_klen	CRD_INI.cri_klen
    453   1.1  jonathan 
    454   1.1  jonathan 	struct cryptodesc *crd_next;
    455   1.1  jonathan };
    456   1.1  jonathan 
    457   1.1  jonathan /* Structure describing complete operation */
    458   1.1  jonathan struct cryptop {
    459  1.16    darran 	TAILQ_ENTRY(cryptop) crp_next;
    460  1.13       tls 	u_int64_t	crp_sid;	/* Session ID */
    461   1.1  jonathan 
    462   1.1  jonathan 	int		crp_ilen;	/* Input data total length */
    463   1.1  jonathan 	int		crp_olen;	/* Result total length */
    464   1.1  jonathan 
    465   1.1  jonathan 	int		crp_etype;	/*
    466   1.1  jonathan 					 * Error type (zero means no error).
    467  1.49  riastrad 					 * All error codes
    468   1.1  jonathan 					 * indicate possible data corruption (as in,
    469   1.1  jonathan 					 * the data have been touched). On all
    470   1.1  jonathan 					 * errors, the crp_sid may have changed
    471   1.1  jonathan 					 * (reset to a new one), so the caller
    472   1.1  jonathan 					 * should always check and use the new
    473   1.1  jonathan 					 * value on future requests.
    474   1.1  jonathan 					 */
    475  1.37  knakahar 	int		crp_flags;	/*
    476  1.37  knakahar 					 * other than crypto.c must not write
    477  1.37  knakahar 					 * after crypto_dispatch().
    478  1.37  knakahar 					 */
    479   1.5  jonathan #define CRYPTO_F_IMBUF		0x0001	/* Input/output are mbuf chains */
    480   1.5  jonathan #define CRYPTO_F_IOV		0x0002	/* Input/output are uio */
    481   1.5  jonathan #define CRYPTO_F_REL		0x0004	/* Must return data in same place */
    482   1.5  jonathan #define	CRYPTO_F_BATCH		0x0008	/* Batch op if possible possible */
    483  1.43  riastrad #define	CRYPTO_F_UNUSED0	0x0010	/* was CRYPTO_F_CBIMM */
    484  1.45  riastrad #define	CRYPTO_F_UNUSED1	0x0020	/* was CRYPTO_F_DONE */
    485  1.45  riastrad #define	CRYPTO_F_UNUSED2	0x0040	/* was CRYPTO_F_CBIFSYNC */
    486  1.12       tls #define	CRYPTO_F_ONRETQ		0x0080	/* Request is on return queue */
    487  1.45  riastrad #define	CRYPTO_F_UNUSED3	0x0100	/* was CRYPTO_F_USER */
    488  1.16    darran #define	CRYPTO_F_MORE		0x0200	/* more data to follow */
    489  1.37  knakahar 
    490  1.37  knakahar 	int		crp_devflags;	/* other than cryptodev.c must not use. */
    491  1.37  knakahar #define	CRYPTODEV_F_RET		0x0001	/* return from crypto.c to cryptodev.c */
    492   1.1  jonathan 
    493   1.8  christos 	void *		crp_buf;	/* Data to be processed */
    494   1.8  christos 	void *		crp_opaque;	/* Opaque pointer, passed along */
    495   1.1  jonathan 	struct cryptodesc *crp_desc;	/* Linked list of processing descriptors */
    496   1.1  jonathan 
    497  1.46  riastrad 	void (*crp_callback)(struct cryptop *); /*
    498  1.38  knakahar 						* Callback function.
    499  1.38  knakahar 						* That must not sleep as it is
    500  1.38  knakahar 						* called in softint context.
    501  1.38  knakahar 						*/
    502   1.1  jonathan 
    503   1.8  christos 	void *		crp_mac;
    504  1.25  drochner 
    505  1.25  drochner 	/*
    506  1.25  drochner 	 * everything below is private to crypto(4)
    507  1.25  drochner 	 */
    508  1.25  drochner 	u_int32_t	crp_reqid;	/* request id */
    509  1.25  drochner 	void *		crp_usropaque;	/* Opaque pointer from user, passed along */
    510   1.1  jonathan 	struct timespec	crp_tstamp;	/* performance time stamp */
    511  1.12       tls 	kcondvar_t	crp_cv;
    512  1.13       tls 	struct fcrypt 	*fcrp;
    513  1.13       tls 	void * 		dst;
    514  1.13       tls 	void *		mac;
    515  1.13       tls 	u_int		len;
    516  1.13       tls 	u_char		tmp_iv[EALG_MAX_BLOCK_LEN];
    517  1.13       tls 	u_char		tmp_mac[CRYPTO_MAX_MAC_LEN];
    518  1.13       tls 
    519  1.13       tls 	struct iovec	iovec[1];
    520  1.13       tls 	struct uio	uio;
    521  1.16    darran 	uint32_t	magic;
    522  1.38  knakahar 	struct cpu_info	*reqcpu;	/*
    523  1.38  knakahar 					 * save requested CPU to do cryptoret
    524  1.38  knakahar 					 * softint in the same CPU.
    525  1.38  knakahar 					 */
    526   1.1  jonathan };
    527   1.1  jonathan 
    528   1.1  jonathan #define CRYPTO_BUF_CONTIG	0x0
    529   1.1  jonathan #define CRYPTO_BUF_IOV		0x1
    530   1.1  jonathan #define CRYPTO_BUF_MBUF		0x2
    531   1.1  jonathan 
    532   1.1  jonathan #define CRYPTO_OP_DECRYPT	0x0
    533   1.1  jonathan #define CRYPTO_OP_ENCRYPT	0x1
    534   1.1  jonathan 
    535   1.1  jonathan /*
    536   1.1  jonathan  * Hints passed to process methods.
    537   1.1  jonathan  */
    538   1.1  jonathan #define	CRYPTO_HINT_MORE	0x1	/* more ops coming shortly */
    539   1.1  jonathan 
    540   1.1  jonathan struct cryptkop {
    541  1.16    darran 	TAILQ_ENTRY(cryptkop) krp_next;
    542  1.13       tls 
    543  1.13       tls 	u_int32_t	krp_reqid;	/* request id */
    544  1.13       tls 	void *		krp_usropaque;	/* Opaque pointer from user, passed along */
    545   1.1  jonathan 
    546   1.1  jonathan 	u_int		krp_op;		/* ie. CRK_MOD_EXP or other */
    547   1.1  jonathan 	u_int		krp_status;	/* return status */
    548   1.1  jonathan 	u_short		krp_iparams;	/* # of input parameters */
    549   1.1  jonathan 	u_short		krp_oparams;	/* # of output parameters */
    550   1.1  jonathan 	u_int32_t	krp_hid;
    551   1.1  jonathan 	struct crparam	krp_param[CRK_MAXPARAM];	/* kvm */
    552  1.46  riastrad 	void		(*krp_callback)(struct cryptkop *);  /*
    553  1.38  knakahar 							      * Callback function.
    554  1.38  knakahar 							      * That must not sleep as it is
    555  1.38  knakahar 							      * called in softint context.
    556  1.38  knakahar 							      */
    557  1.12       tls 	int		krp_flags;	/* same values as crp_flags */
    558  1.37  knakahar 	int		krp_devflags;	/* same values as crp_devflags */
    559  1.12       tls 	kcondvar_t	krp_cv;
    560  1.13       tls 	struct fcrypt 	*fcrp;
    561  1.13       tls 	struct crparam	crk_param[CRK_MAXPARAM];
    562  1.38  knakahar 	struct cpu_info	*reqcpu;
    563   1.1  jonathan };
    564   1.1  jonathan 
    565   1.1  jonathan /* Crypto capabilities structure */
    566   1.1  jonathan struct cryptocap {
    567   1.1  jonathan 	u_int32_t	cc_sessions;
    568   1.1  jonathan 
    569   1.1  jonathan 	/*
    570   1.1  jonathan 	 * Largest possible operator length (in bits) for each type of
    571   1.1  jonathan 	 * encryption algorithm.
    572   1.1  jonathan 	 */
    573   1.1  jonathan 	u_int16_t	cc_max_op_len[CRYPTO_ALGORITHM_MAX + 1];
    574   1.1  jonathan 
    575   1.1  jonathan 	u_int8_t	cc_alg[CRYPTO_ALGORITHM_MAX + 1];
    576   1.1  jonathan 
    577   1.1  jonathan 	u_int8_t	cc_kalg[CRK_ALGORITHM_MAX + 1];
    578   1.1  jonathan 
    579   1.1  jonathan 	u_int8_t	cc_flags;
    580   1.1  jonathan 	u_int8_t	cc_qblocked;		/* symmetric q blocked */
    581   1.1  jonathan 	u_int8_t	cc_kqblocked;		/* asymmetric q blocked */
    582   1.5  jonathan #define CRYPTOCAP_F_CLEANUP	0x01		/* needs resource cleanup */
    583   1.5  jonathan #define CRYPTOCAP_F_SOFTWARE	0x02		/* software implementation */
    584   1.5  jonathan #define CRYPTOCAP_F_SYNC	0x04		/* operates synchronously */
    585   1.1  jonathan 
    586   1.1  jonathan 	void		*cc_arg;		/* callback argument */
    587   1.1  jonathan 	int		(*cc_newsession)(void*, u_int32_t*, struct cryptoini*);
    588   1.1  jonathan 	int		(*cc_process) (void*, struct cryptop *, int);
    589  1.47  riastrad 	void		(*cc_freesession) (void *, u_int64_t);
    590   1.1  jonathan 	void		*cc_karg;		/* callback argument */
    591   1.1  jonathan 	int		(*cc_kprocess) (void*, struct cryptkop *, int);
    592  1.35  knakahar 
    593  1.35  knakahar 	kmutex_t	cc_lock;
    594   1.1  jonathan };
    595   1.5  jonathan 
    596   1.5  jonathan /*
    597   1.5  jonathan  * Session ids are 64 bits.  The lower 32 bits contain a "local id" which
    598   1.5  jonathan  * is a driver-private session identifier.  The upper 32 bits contain a
    599   1.5  jonathan  * "hardware id" used by the core crypto code to identify the driver and
    600   1.5  jonathan  * a copy of the driver's capabilities that can be used by client code to
    601   1.5  jonathan  * optimize operation.
    602   1.5  jonathan  */
    603  1.44  riastrad #define	CRYPTO_SESID2HID(_sid)	((((_sid) >> 32) & 0xffffff) - 1)
    604   1.5  jonathan #define	CRYPTO_SESID2CAPS(_sid)	(((_sid) >> 56) & 0xff)
    605   1.5  jonathan #define	CRYPTO_SESID2LID(_sid)	(((u_int32_t) (_sid)) & 0xffffffff)
    606   1.1  jonathan 
    607   1.1  jonathan MALLOC_DECLARE(M_CRYPTO_DATA);
    608   1.1  jonathan 
    609   1.1  jonathan extern	int crypto_newsession(u_int64_t *sid, struct cryptoini *cri, int hard);
    610  1.48  riastrad extern	void crypto_freesession(u_int64_t sid);
    611   1.1  jonathan extern	int32_t crypto_get_driverid(u_int32_t flags);
    612   1.1  jonathan extern	int crypto_register(u_int32_t driverid, int alg, u_int16_t maxoplen,
    613   1.1  jonathan 	    u_int32_t flags,
    614   1.1  jonathan 	    int (*newses)(void*, u_int32_t*, struct cryptoini*),
    615  1.47  riastrad 	    void (*freeses)(void *, u_int64_t),
    616   1.1  jonathan 	    int (*process)(void*, struct cryptop *, int),
    617   1.1  jonathan 	    void *arg);
    618   1.1  jonathan extern	int crypto_kregister(u_int32_t, int, u_int32_t,
    619   1.1  jonathan 	    int (*)(void*, struct cryptkop *, int),
    620   1.1  jonathan 	    void *arg);
    621   1.1  jonathan extern	int crypto_unregister(u_int32_t driverid, int alg);
    622   1.1  jonathan extern	int crypto_unregister_all(u_int32_t driverid);
    623  1.50  riastrad extern	void crypto_dispatch(struct cryptop *crp);
    624  1.50  riastrad extern	void crypto_kdispatch(struct cryptkop *);
    625   1.1  jonathan #define	CRYPTO_SYMQ	0x1
    626   1.1  jonathan #define	CRYPTO_ASYMQ	0x2
    627   1.1  jonathan extern	int crypto_unblock(u_int32_t, int);
    628   1.1  jonathan extern	void crypto_done(struct cryptop *crp);
    629   1.1  jonathan extern	void crypto_kdone(struct cryptkop *);
    630   1.1  jonathan extern	int crypto_getfeat(int *);
    631   1.1  jonathan 
    632   1.8  christos void	cuio_copydata(struct uio *, int, int, void *);
    633   1.8  christos void	cuio_copyback(struct uio *, int, int, void *);
    634   1.1  jonathan int	cuio_apply(struct uio *, int, int,
    635   1.8  christos 	    int (*f)(void *, void *, unsigned int), void *);
    636   1.1  jonathan 
    637   1.1  jonathan extern	void crypto_freereq(struct cryptop *crp);
    638   1.1  jonathan extern	struct cryptop *crypto_getreq(int num);
    639   1.1  jonathan 
    640  1.34  knakahar extern	void crypto_kfreereq(struct cryptkop *);
    641  1.34  knakahar extern	struct cryptkop *crypto_kgetreq(int, int);
    642  1.34  knakahar 
    643   1.1  jonathan extern	int crypto_usercrypto;		/* userland may do crypto requests */
    644   1.1  jonathan extern	int crypto_userasymcrypto;	/* userland may do asym crypto reqs */
    645   1.1  jonathan extern	int crypto_devallowsoft;	/* only use hardware crypto */
    646   1.3  jonathan 
    647  1.12       tls /*
    648   1.3  jonathan  * initialize the crypto framework subsystem (not the pseudo-device).
    649   1.3  jonathan  * This must be called very early in boot, so the framework is ready
    650  1.42    andvar  * to handle registration requests when crypto hardware is autoconfigured.
    651  1.42    andvar  * (This declaration doesn't really belong here but there's no header
    652   1.3  jonathan  * for the raw framework.)
    653   1.3  jonathan  */
    654  1.26  pgoyette int	crypto_init(void);
    655   1.1  jonathan 
    656   1.1  jonathan /*
    657   1.1  jonathan  * Crypto-related utility routines used mainly by drivers.
    658   1.1  jonathan  *
    659   1.1  jonathan  * XXX these don't really belong here; but for now they're
    660   1.1  jonathan  *     kept apart from the rest of the system.
    661   1.1  jonathan  */
    662   1.1  jonathan struct uio;
    663   1.8  christos extern	void cuio_copydata(struct uio* uio, int off, int len, void *cp);
    664   1.8  christos extern	void cuio_copyback(struct uio* uio, int off, int len, void *cp);
    665   1.1  jonathan extern int	cuio_getptr(struct uio *, int loc, int *off);
    666   1.1  jonathan 
    667  1.12       tls #ifdef CRYPTO_DEBUG	/* yuck, netipsec defines these differently */
    668  1.12       tls #ifndef DPRINTF
    669  1.32  knakahar #define DPRINTF(a, ...)	printf("%s: " a, __func__, ##__VA_ARGS__)
    670  1.12       tls #endif
    671  1.12       tls #else
    672  1.12       tls #ifndef DPRINTF
    673  1.32  knakahar #define DPRINTF(a, ...)
    674  1.12       tls #endif
    675  1.12       tls #endif
    676   1.1  jonathan 
    677   1.1  jonathan #endif /* _KERNEL */
    678  1.36  knakahar /*
    679  1.36  knakahar  * Locking notes:
    680  1.36  knakahar  * + crypto_drivers itself is protected by crypto_drv_mtx (an adaptive lock)
    681  1.36  knakahar  * + crypto_drivers[i] and its all members are protected by
    682  1.36  knakahar  *   crypto_drivers[i].cc_lock (a spin lock)
    683  1.36  knakahar  *       spin lock as crypto_unblock() can be called in interrupt context
    684  1.39  knakahar  * + percpu'ed crp_q and crp_kq are procted by splsoftnet.
    685  1.39  knakahar  * + crp_ret_q, crp_ret_kq and crypto_exit_flag that are members of
    686  1.39  knakahar  *   struct crypto_crp_ret_qs are protected by crypto_crp_ret_qs.crp_ret_q_mtx
    687  1.39  knakahar  *   (a spin lock)
    688  1.36  knakahar  *       spin lock as crypto_done() can be called in interrupt context
    689  1.39  knakahar  *       NOTE:
    690  1.39  knakahar  *       It is not known whether crypto_done()(in interrupt context) is called
    691  1.39  knakahar  *       in the same CPU as crypto_dispatch() is called.
    692  1.39  knakahar  *       So, struct crypto_crp_ret_qs cannot be percpu(9).
    693  1.36  knakahar  *
    694  1.36  knakahar  * Locking order:
    695  1.39  knakahar  *     - crypto_drv_mtx => crypto_drivers[i].cc_lock
    696  1.36  knakahar  */
    697   1.1  jonathan #endif /* _CRYPTO_CRYPTO_H_ */
    698