1 /* $OpenBSD: cryptodev.h,v 1.71 2017/08/10 18:57:20 tedu Exp $ */ 2 3 /* 4 * The author of this code is Angelos D. Keromytis (angelos@cis.upenn.edu) 5 * 6 * This code was written by Angelos D. Keromytis in Athens, Greece, in 7 * February 2000. Network Security Technologies Inc. (NSTI) kindly 8 * supported the development of this code. 9 * 10 * Copyright (c) 2000 Angelos D. Keromytis 11 * 12 * Permission to use, copy, and modify this software with or without fee 13 * is hereby granted, provided that this entire notice is included in 14 * all source code copies of any software which is or includes a copy or 15 * modification of this software. 16 * 17 * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR 18 * IMPLIED WARRANTY. IN PARTICULAR, NONE OF THE AUTHORS MAKES ANY 19 * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE 20 * MERCHANTABILITY OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR 21 * PURPOSE. 22 * 23 * Copyright (c) 2001 Theo de Raadt 24 * 25 * Redistribution and use in source and binary forms, with or without 26 * modification, are permitted provided that the following conditions 27 * are met: 28 * 29 * 1. Redistributions of source code must retain the above copyright 30 * notice, this list of conditions and the following disclaimer. 31 * 2. Redistributions in binary form must reproduce the above copyright 32 * notice, this list of conditions and the following disclaimer in the 33 * documentation and/or other materials provided with the distribution. 34 * 35 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 36 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 37 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 38 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 39 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 40 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 41 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 42 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 43 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 44 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 45 * 46 * Effort sponsored in part by the Defense Advanced Research Projects 47 * Agency (DARPA) and Air Force Research Laboratory, Air Force 48 * Materiel Command, USAF, under agreement number F30602-01-2-0537. 49 * 50 */ 51 52 #ifndef _CRYPTO_CRYPTO_H_ 53 #define _CRYPTO_CRYPTO_H_ 54 55 #include <sys/task.h> 56 57 /* Some initial values */ 58 #define CRYPTO_DRIVERS_INITIAL 4 59 #define CRYPTO_DRIVERS_MAX 128 60 #define CRYPTO_SW_SESSIONS 32 61 62 /* HMAC values */ 63 #define HMAC_MD5_BLOCK_LEN 64 64 #define HMAC_SHA1_BLOCK_LEN 64 65 #define HMAC_RIPEMD160_BLOCK_LEN 64 66 #define HMAC_SHA2_256_BLOCK_LEN 64 67 #define HMAC_SHA2_384_BLOCK_LEN 128 68 #define HMAC_SHA2_512_BLOCK_LEN 128 69 #define HMAC_MAX_BLOCK_LEN HMAC_SHA2_512_BLOCK_LEN /* keep in sync */ 70 #define HMAC_IPAD_VAL 0x36 71 #define HMAC_OPAD_VAL 0x5C 72 73 /* Encryption algorithm block sizes */ 74 #define DES3_BLOCK_LEN 8 75 #define BLOWFISH_BLOCK_LEN 8 76 #define CAST128_BLOCK_LEN 8 77 #define RIJNDAEL128_BLOCK_LEN 16 78 #define CHACHA20_BLOCK_LEN 64 79 #define EALG_MAX_BLOCK_LEN 64 /* Keep this updated */ 80 81 /* Maximum hash algorithm result length */ 82 #define AALG_MAX_RESULT_LEN 64 /* Keep this updated */ 83 84 #define CRYPTO_3DES_CBC 1 85 #define CRYPTO_BLF_CBC 2 86 #define CRYPTO_CAST_CBC 3 87 #define CRYPTO_MD5_HMAC 4 88 #define CRYPTO_SHA1_HMAC 5 89 #define CRYPTO_RIPEMD160_HMAC 6 90 #define CRYPTO_RIJNDAEL128_CBC 7 /* 128 bit blocksize */ 91 #define CRYPTO_AES_CBC 7 /* 128 bit blocksize -- the same as above */ 92 #define CRYPTO_DEFLATE_COMP 8 /* Deflate compression algorithm */ 93 #define CRYPTO_NULL 9 94 #define CRYPTO_LZS_COMP 10 /* LZS compression algorithm */ 95 #define CRYPTO_SHA2_256_HMAC 11 96 #define CRYPTO_SHA2_384_HMAC 12 97 #define CRYPTO_SHA2_512_HMAC 13 98 #define CRYPTO_AES_CTR 14 99 #define CRYPTO_AES_XTS 15 100 #define CRYPTO_AES_GCM_16 16 101 #define CRYPTO_AES_128_GMAC 17 102 #define CRYPTO_AES_192_GMAC 18 103 #define CRYPTO_AES_256_GMAC 19 104 #define CRYPTO_AES_GMAC 20 105 #define CRYPTO_CHACHA20_POLY1305 21 106 #define CRYPTO_CHACHA20_POLY1305_MAC 22 107 #define CRYPTO_ESN 23 /* Support for Extended Sequence Numbers */ 108 #define CRYPTO_ALGORITHM_MAX 23 /* Keep updated */ 109 110 /* Algorithm flags */ 111 #define CRYPTO_ALG_FLAG_SUPPORTED 0x01 /* Algorithm is supported */ 112 113 /* Standard initialization structure beginning */ 114 struct cryptoini { 115 int cri_alg; /* Algorithm to use */ 116 int cri_klen; /* Key length, in bits */ 117 int cri_rnd; /* Algorithm rounds, where relevant */ 118 caddr_t cri_key; /* key to use */ 119 union { 120 u_int8_t iv[EALG_MAX_BLOCK_LEN]; /* IV to use */ 121 u_int8_t esn[4]; /* high-order ESN */ 122 } u; 123 #define cri_iv u.iv 124 #define cri_esn u.esn 125 struct cryptoini *cri_next; 126 }; 127 128 /* Describe boundaries of a single crypto operation */ 129 struct cryptodesc { 130 int crd_skip; /* How many bytes to ignore from start */ 131 int crd_len; /* How many bytes to process */ 132 int crd_inject; /* Where to inject results, if applicable */ 133 int crd_flags; 134 135 #define CRD_F_ENCRYPT 0x01 /* Set when doing encryption */ 136 #define CRD_F_IV_PRESENT 0x02 /* When encrypting, IV is already in 137 place, so don't copy. */ 138 #define CRD_F_IV_EXPLICIT 0x04 /* IV explicitly provided */ 139 #define CRD_F_COMP 0x10 /* Set when doing compression */ 140 #define CRD_F_ESN 0x20 /* Set when ESN field is provided */ 141 142 struct cryptoini CRD_INI; /* Initialization/context data */ 143 #define crd_esn CRD_INI.cri_esn 144 #define crd_iv CRD_INI.cri_iv 145 #define crd_key CRD_INI.cri_key 146 #define crd_rnd CRD_INI.cri_rnd 147 #define crd_alg CRD_INI.cri_alg 148 #define crd_klen CRD_INI.cri_klen 149 }; 150 151 /* Structure describing complete operation */ 152 struct cryptop { 153 struct task crp_task; 154 155 u_int64_t crp_sid; /* Session ID */ 156 int crp_ilen; /* Input data total length */ 157 int crp_olen; /* Result total length */ 158 int crp_alloctype; /* Type of buf to allocate if needed */ 159 160 int crp_etype; /* 161 * Error type (zero means no error). 162 * All error codes except EAGAIN 163 * indicate possible data corruption (as in, 164 * the data have been touched). On all 165 * errors, the crp_sid may have changed 166 * (reset to a new one), so the caller 167 * should always check and use the new 168 * value on future requests. 169 */ 170 int crp_flags; 171 172 #define CRYPTO_F_IMBUF 0x0001 /* Input/output are mbuf chains, otherwise contig */ 173 #define CRYPTO_F_IOV 0x0002 /* Input/output are uio */ 174 #define CRYPTO_F_NOQUEUE 0x0008 /* Don't use crypto queue/thread */ 175 #define CRYPTO_F_DONE 0x0010 /* request completed */ 176 177 void *crp_buf; /* Data to be processed */ 178 void *crp_opaque; /* Opaque pointer, passed along */ 179 180 struct cryptodesc *crp_desc; /* List of processing descriptors */ 181 struct cryptodesc crp_sdesc[2]; /* Static array for small ops */ 182 int crp_ndesc; /* Amount of descriptors to use */ 183 int crp_ndescalloc;/* Amount of descriptors allocated */ 184 185 void (*crp_callback)(struct cryptop *); /* Callback function */ 186 187 caddr_t crp_mac; 188 }; 189 190 #define CRYPTO_BUF_IOV 0x1 191 #define CRYPTO_BUF_MBUF 0x2 192 193 #define CRYPTO_OP_DECRYPT 0x0 194 #define CRYPTO_OP_ENCRYPT 0x1 195 196 /* Crypto capabilities structure */ 197 struct cryptocap { 198 u_int64_t cc_operations; /* Counter of how many ops done */ 199 u_int64_t cc_bytes; /* Counter of how many bytes done */ 200 u_int64_t cc_koperations; /* How many PK ops done */ 201 202 u_int32_t cc_sessions; /* How many sessions allocated */ 203 204 /* Symmetric/hash algorithms supported */ 205 int cc_alg[CRYPTO_ALGORITHM_MAX + 1]; 206 207 int cc_queued; /* Operations queued */ 208 209 u_int8_t cc_flags; 210 #define CRYPTOCAP_F_CLEANUP 0x01 211 #define CRYPTOCAP_F_SOFTWARE 0x02 212 #define CRYPTOCAP_F_MPSAFE 0x04 213 214 int (*cc_newsession) (u_int32_t *, struct cryptoini *); 215 int (*cc_process) (struct cryptop *); 216 int (*cc_freesession) (u_int64_t); 217 }; 218 219 220 int crypto_newsession(u_int64_t *, struct cryptoini *, int); 221 int crypto_freesession(u_int64_t); 222 int crypto_dispatch(struct cryptop *); 223 int crypto_register(u_int32_t, int *, 224 int (*)(u_int32_t *, struct cryptoini *), int (*)(u_int64_t), 225 int (*)(struct cryptop *)); 226 int crypto_unregister(u_int32_t, int); 227 int32_t crypto_get_driverid(u_int8_t); 228 int crypto_invoke(struct cryptop *); 229 void crypto_done(struct cryptop *); 230 231 void cuio_copydata(struct uio *, int, int, caddr_t); 232 void cuio_copyback(struct uio *, int, int, const void *); 233 int cuio_getptr(struct uio *, int, int *); 234 int cuio_apply(struct uio *, int, int, 235 int (*f)(caddr_t, caddr_t, unsigned int), caddr_t); 236 237 struct cryptop *crypto_getreq(int); 238 void crypto_freereq(struct cryptop *); 239 #endif /* _CRYPTO_CRYPTO_H_ */ 240