cryptodev.h 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729
  1. /* $FreeBSD$ */
  2. /* $OpenBSD: cryptodev.h,v 1.31 2002/06/11 11:14:29 beck Exp $ */
  3. /*-
  4. * The author of this code is Angelos D. Keromytis (angelos@cis.upenn.edu)
  5. * Copyright (c) 2002-2006 Sam Leffler, Errno Consulting
  6. *
  7. * This code was written by Angelos D. Keromytis in Athens, Greece, in
  8. * February 2000. Network Security Technologies Inc. (NSTI) kindly
  9. * supported the development of this code.
  10. *
  11. * Copyright (c) 2000 Angelos D. Keromytis
  12. *
  13. * Permission to use, copy, and modify this software with or without fee
  14. * is hereby granted, provided that this entire notice is included in
  15. * all source code copies of any software which is or includes a copy or
  16. * modification of this software.
  17. *
  18. * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR
  19. * IMPLIED WARRANTY. IN PARTICULAR, NONE OF THE AUTHORS MAKES ANY
  20. * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE
  21. * MERCHANTABILITY OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR
  22. * PURPOSE.
  23. *
  24. * Copyright (c) 2001 Theo de Raadt
  25. * Copyright (c) 2014 The FreeBSD Foundation
  26. * All rights reserved.
  27. *
  28. * Portions of this software were developed by John-Mark Gurney
  29. * under sponsorship of the FreeBSD Foundation and
  30. * Rubicon Communications, LLC (Netgate).
  31. *
  32. * Redistribution and use in source and binary forms, with or without
  33. * modification, are permitted provided that the following conditions
  34. * are met:
  35. *
  36. * 1. Redistributions of source code must retain the above copyright
  37. * notice, this list of conditions and the following disclaimer.
  38. * 2. Redistributions in binary form must reproduce the above copyright
  39. * notice, this list of conditions and the following disclaimer in the
  40. * documentation and/or other materials provided with the distribution.
  41. * 3. The name of the author may not be used to endorse or promote products
  42. * derived from this software without specific prior written permission.
  43. *
  44. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  45. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  46. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  47. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  48. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  49. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  50. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  51. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  52. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  53. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  54. *
  55. * Effort sponsored in part by the Defense Advanced Research Projects
  56. * Agency (DARPA) and Air Force Research Laboratory, Air Force
  57. * Materiel Command, USAF, under agreement number F30602-01-2-0537.
  58. *
  59. */
  60. #ifndef _CRYPTO_CRYPTO_H_
  61. #define _CRYPTO_CRYPTO_H_
  62. #include <sys/ioccom.h>
  63. #ifdef _KERNEL
  64. #include <opencrypto/_cryptodev.h>
  65. #include <sys/_task.h>
  66. #endif
  67. /* Some initial values */
  68. #define CRYPTO_DRIVERS_INITIAL 4
  69. /* Hash values */
  70. #define NULL_HASH_LEN 16
  71. #define SHA1_HASH_LEN 20
  72. #define RIPEMD160_HASH_LEN 20
  73. #define SHA2_224_HASH_LEN 28
  74. #define SHA2_256_HASH_LEN 32
  75. #define SHA2_384_HASH_LEN 48
  76. #define SHA2_512_HASH_LEN 64
  77. #define AES_GMAC_HASH_LEN 16
  78. #define POLY1305_HASH_LEN 16
  79. #define AES_CBC_MAC_HASH_LEN 16
  80. /* Maximum hash algorithm result length */
  81. #define HASH_MAX_LEN SHA2_512_HASH_LEN /* Keep this updated */
  82. #define SHA1_BLOCK_LEN 64
  83. #define RIPEMD160_BLOCK_LEN 64
  84. #define SHA2_224_BLOCK_LEN 64
  85. #define SHA2_256_BLOCK_LEN 64
  86. #define SHA2_384_BLOCK_LEN 128
  87. #define SHA2_512_BLOCK_LEN 128
  88. /* HMAC values */
  89. #define NULL_HMAC_BLOCK_LEN 64
  90. /* Maximum HMAC block length */
  91. #define HMAC_MAX_BLOCK_LEN SHA2_512_BLOCK_LEN /* Keep this updated */
  92. #define HMAC_IPAD_VAL 0x36
  93. #define HMAC_OPAD_VAL 0x5C
  94. /* HMAC Key Length */
  95. #define AES_128_GMAC_KEY_LEN 16
  96. #define AES_192_GMAC_KEY_LEN 24
  97. #define AES_256_GMAC_KEY_LEN 32
  98. #define AES_128_CBC_MAC_KEY_LEN 16
  99. #define AES_192_CBC_MAC_KEY_LEN 24
  100. #define AES_256_CBC_MAC_KEY_LEN 32
  101. #define POLY1305_KEY_LEN 32
  102. /* Encryption algorithm block sizes */
  103. #define NULL_BLOCK_LEN 4 /* IPsec to maintain alignment */
  104. #define RIJNDAEL128_BLOCK_LEN 16
  105. #define AES_BLOCK_LEN 16
  106. #define AES_ICM_BLOCK_LEN 1
  107. #define CAMELLIA_BLOCK_LEN 16
  108. #define CHACHA20_NATIVE_BLOCK_LEN 64
  109. #define EALG_MAX_BLOCK_LEN CHACHA20_NATIVE_BLOCK_LEN /* Keep this updated */
  110. /* IV Lengths */
  111. #define AES_GCM_IV_LEN 12
  112. #define AES_CCM_IV_LEN 12
  113. #define AES_XTS_IV_LEN 8
  114. #define AES_XTS_ALPHA 0x87 /* GF(2^128) generator polynomial */
  115. /* Min and Max Encryption Key Sizes */
  116. #define NULL_MIN_KEY 0
  117. #define NULL_MAX_KEY 256 /* 2048 bits, max key */
  118. #define RIJNDAEL_MIN_KEY 16
  119. #define RIJNDAEL_MAX_KEY 32
  120. #define AES_MIN_KEY RIJNDAEL_MIN_KEY
  121. #define AES_MAX_KEY RIJNDAEL_MAX_KEY
  122. #define AES_XTS_MIN_KEY (2 * AES_MIN_KEY)
  123. #define AES_XTS_MAX_KEY (2 * AES_MAX_KEY)
  124. #define CAMELLIA_MIN_KEY 16
  125. #define CAMELLIA_MAX_KEY 32
  126. /* Maximum hash algorithm result length */
  127. #define AALG_MAX_RESULT_LEN 64 /* Keep this updated */
  128. #define CRYPTO_ALGORITHM_MIN 1
  129. #define CRYPTO_DES_CBC 1
  130. #define CRYPTO_3DES_CBC 2
  131. #define CRYPTO_BLF_CBC 3
  132. #define CRYPTO_CAST_CBC 4
  133. #define CRYPTO_SKIPJACK_CBC 5
  134. #define CRYPTO_MD5_HMAC 6
  135. #define CRYPTO_SHA1_HMAC 7
  136. #define CRYPTO_RIPEMD160_HMAC 8
  137. #define CRYPTO_MD5_KPDK 9
  138. #define CRYPTO_SHA1_KPDK 10
  139. #define CRYPTO_RIJNDAEL128_CBC 11 /* 128 bit blocksize */
  140. #define CRYPTO_AES_CBC 11 /* 128 bit blocksize -- the same as above */
  141. #define CRYPTO_ARC4 12
  142. #define CRYPTO_MD5 13
  143. #define CRYPTO_SHA1 14
  144. #define CRYPTO_NULL_HMAC 15
  145. #define CRYPTO_NULL_CBC 16
  146. #define CRYPTO_DEFLATE_COMP 17 /* Deflate compression algorithm */
  147. #define CRYPTO_SHA2_256_HMAC 18
  148. #define CRYPTO_SHA2_384_HMAC 19
  149. #define CRYPTO_SHA2_512_HMAC 20
  150. #define CRYPTO_CAMELLIA_CBC 21
  151. #define CRYPTO_AES_XTS 22
  152. #define CRYPTO_AES_ICM 23 /* commonly known as CTR mode */
  153. #define CRYPTO_AES_NIST_GMAC 24 /* GMAC only */
  154. #define CRYPTO_AES_NIST_GCM_16 25 /* 16 byte ICV */
  155. #ifdef _KERNEL
  156. #define CRYPTO_AES_128_NIST_GMAC 26 /* auth side */
  157. #define CRYPTO_AES_192_NIST_GMAC 27 /* auth side */
  158. #define CRYPTO_AES_256_NIST_GMAC 28 /* auth side */
  159. #endif
  160. #define CRYPTO_BLAKE2B 29 /* Blake2b hash */
  161. #define CRYPTO_BLAKE2S 30 /* Blake2s hash */
  162. #define CRYPTO_CHACHA20 31 /* Chacha20 stream cipher */
  163. #define CRYPTO_SHA2_224_HMAC 32
  164. #define CRYPTO_RIPEMD160 33
  165. #define CRYPTO_SHA2_224 34
  166. #define CRYPTO_SHA2_256 35
  167. #define CRYPTO_SHA2_384 36
  168. #define CRYPTO_SHA2_512 37
  169. #define CRYPTO_POLY1305 38
  170. #define CRYPTO_AES_CCM_CBC_MAC 39 /* auth side */
  171. #define CRYPTO_AES_CCM_16 40 /* cipher side */
  172. #define CRYPTO_ALGORITHM_MAX 40 /* Keep updated - see below */
  173. #define CRYPTO_ALGO_VALID(x) ((x) >= CRYPTO_ALGORITHM_MIN && \
  174. (x) <= CRYPTO_ALGORITHM_MAX)
  175. /* Algorithm flags */
  176. #define CRYPTO_ALG_FLAG_SUPPORTED 0x01 /* Algorithm is supported */
  177. #define CRYPTO_ALG_FLAG_RNG_ENABLE 0x02 /* Has HW RNG for DH/DSA */
  178. #define CRYPTO_ALG_FLAG_DSA_SHA 0x04 /* Can do SHA on msg */
  179. /*
  180. * Crypto driver/device flags. They can set in the crid
  181. * parameter when creating a session or submitting a key
  182. * op to affect the device/driver assigned. If neither
  183. * of these are specified then the crid is assumed to hold
  184. * the driver id of an existing (and suitable) device that
  185. * must be used to satisfy the request.
  186. */
  187. #define CRYPTO_FLAG_HARDWARE 0x01000000 /* hardware accelerated */
  188. #define CRYPTO_FLAG_SOFTWARE 0x02000000 /* software implementation */
  189. /* Does the kernel support vmpage buffers on this platform? */
  190. #ifdef __powerpc__
  191. #define CRYPTO_MAY_HAVE_VMPAGE 1
  192. #else
  193. #define CRYPTO_MAY_HAVE_VMPAGE ( PMAP_HAS_DMAP )
  194. #endif
  195. /* Does the currently running system support vmpage buffers on this platform? */
  196. #define CRYPTO_HAS_VMPAGE ( PMAP_HAS_DMAP )
  197. /* NB: deprecated */
  198. struct session_op {
  199. uint32_t cipher; /* ie. CRYPTO_AES_CBC */
  200. uint32_t mac; /* ie. CRYPTO_SHA2_256_HMAC */
  201. uint32_t keylen; /* cipher key */
  202. const void *key;
  203. int mackeylen; /* mac key */
  204. const void *mackey;
  205. uint32_t ses; /* returns: session # */
  206. };
  207. /*
  208. * session and crypt _op structs are used by userspace programs to interact
  209. * with /dev/crypto. Confusingly, the internal kernel interface is named
  210. * "cryptop" (no underscore).
  211. */
  212. struct session2_op {
  213. uint32_t cipher; /* ie. CRYPTO_AES_CBC */
  214. uint32_t mac; /* ie. CRYPTO_SHA2_256_HMAC */
  215. uint32_t keylen; /* cipher key */
  216. const void *key;
  217. int mackeylen; /* mac key */
  218. const void *mackey;
  219. uint32_t ses; /* returns: session # */
  220. int crid; /* driver id + flags (rw) */
  221. int pad[4]; /* for future expansion */
  222. };
  223. struct crypt_op {
  224. uint32_t ses;
  225. uint16_t op; /* i.e. COP_ENCRYPT */
  226. #define COP_ENCRYPT 1
  227. #define COP_DECRYPT 2
  228. uint16_t flags;
  229. #define COP_F_CIPHER_FIRST 0x0001 /* Cipher before MAC. */
  230. #define COP_F_BATCH 0x0008 /* Batch op if possible */
  231. u_int len;
  232. const void *src; /* become iov[] inside kernel */
  233. void *dst;
  234. void *mac; /* must be big enough for chosen MAC */
  235. const void *iv;
  236. };
  237. /* op and flags the same as crypt_op */
  238. struct crypt_aead {
  239. uint32_t ses;
  240. uint16_t op; /* i.e. COP_ENCRYPT */
  241. uint16_t flags;
  242. u_int len;
  243. u_int aadlen;
  244. u_int ivlen;
  245. const void *src; /* become iov[] inside kernel */
  246. void *dst;
  247. const void *aad; /* additional authenticated data */
  248. void *tag; /* must fit for chosen TAG length */
  249. const void *iv;
  250. };
  251. /*
  252. * Parameters for looking up a crypto driver/device by
  253. * device name or by id. The latter are returned for
  254. * created sessions (crid) and completed key operations.
  255. */
  256. struct crypt_find_op {
  257. int crid; /* driver id + flags */
  258. char name[32]; /* device/driver name */
  259. };
  260. /* bignum parameter, in packed bytes, ... */
  261. struct crparam {
  262. void *crp_p;
  263. u_int crp_nbits;
  264. };
  265. #define CRK_MAXPARAM 8
  266. struct crypt_kop {
  267. u_int crk_op; /* ie. CRK_MOD_EXP or other */
  268. u_int crk_status; /* return status */
  269. u_short crk_iparams; /* # of input parameters */
  270. u_short crk_oparams; /* # of output parameters */
  271. u_int crk_crid; /* NB: only used by CIOCKEY2 (rw) */
  272. struct crparam crk_param[CRK_MAXPARAM];
  273. };
  274. #define CRK_ALGORITM_MIN 0
  275. #define CRK_MOD_EXP 0
  276. #define CRK_MOD_EXP_CRT 1
  277. #define CRK_DSA_SIGN 2
  278. #define CRK_DSA_VERIFY 3
  279. #define CRK_DH_COMPUTE_KEY 4
  280. #define CRK_ALGORITHM_MAX 4 /* Keep updated - see below */
  281. #define CRF_MOD_EXP (1 << CRK_MOD_EXP)
  282. #define CRF_MOD_EXP_CRT (1 << CRK_MOD_EXP_CRT)
  283. #define CRF_DSA_SIGN (1 << CRK_DSA_SIGN)
  284. #define CRF_DSA_VERIFY (1 << CRK_DSA_VERIFY)
  285. #define CRF_DH_COMPUTE_KEY (1 << CRK_DH_COMPUTE_KEY)
  286. #define CIOCGSESSION _IOWR('c', 101, struct session_op)
  287. #define CIOCFSESSION _IOW('c', 102, uint32_t)
  288. #define CIOCCRYPT _IOWR('c', 103, struct crypt_op)
  289. #define CIOCKEY _IOWR('c', 104, struct crypt_kop)
  290. #define CIOCASYMFEAT _IOR('c', 105, uint32_t)
  291. #define CIOCGSESSION2 _IOWR('c', 106, struct session2_op)
  292. #define CIOCKEY2 _IOWR('c', 107, struct crypt_kop)
  293. #define CIOCFINDDEV _IOWR('c', 108, struct crypt_find_op)
  294. #define CIOCCRYPTAEAD _IOWR('c', 109, struct crypt_aead)
  295. struct cryptostats {
  296. uint64_t cs_ops; /* symmetric crypto ops submitted */
  297. uint64_t cs_errs; /* symmetric crypto ops that failed */
  298. uint64_t cs_kops; /* asymetric/key ops submitted */
  299. uint64_t cs_kerrs; /* asymetric/key ops that failed */
  300. uint64_t cs_intrs; /* crypto swi thread activations */
  301. uint64_t cs_rets; /* crypto return thread activations */
  302. uint64_t cs_blocks; /* symmetric op driver block */
  303. uint64_t cs_kblocks; /* symmetric op driver block */
  304. };
  305. #ifdef _KERNEL
  306. /*
  307. * Return values for cryptodev_probesession methods.
  308. */
  309. #define CRYPTODEV_PROBE_HARDWARE (-100)
  310. #define CRYPTODEV_PROBE_ACCEL_SOFTWARE (-200)
  311. #define CRYPTODEV_PROBE_SOFTWARE (-500)
  312. #if 0
  313. #define CRYPTDEB(s, ...) do { \
  314. printf("%s:%d: " s "\n", __FILE__, __LINE__, ## __VA_ARGS__); \
  315. } while (0)
  316. #else
  317. #define CRYPTDEB(...) do { } while (0)
  318. #endif
  319. struct crypto_session_params {
  320. int csp_mode; /* Type of operations to perform. */
  321. #define CSP_MODE_NONE 0
  322. #define CSP_MODE_COMPRESS 1 /* Compression/decompression. */
  323. #define CSP_MODE_CIPHER 2 /* Encrypt/decrypt. */
  324. #define CSP_MODE_DIGEST 3 /* Compute/verify digest. */
  325. #define CSP_MODE_AEAD 4 /* Combined auth/encryption. */
  326. #define CSP_MODE_ETA 5 /* IPsec style encrypt-then-auth */
  327. int csp_flags;
  328. #define CSP_F_SEPARATE_OUTPUT 0x0001 /* Requests can use separate output */
  329. #define CSP_F_SEPARATE_AAD 0x0002 /* Requests can use separate AAD */
  330. #define CSP_F_ESN 0x0004 /* Requests can use seperate ESN field */
  331. int csp_ivlen; /* IV length in bytes. */
  332. int csp_cipher_alg;
  333. int csp_cipher_klen; /* Key length in bytes. */
  334. const void *csp_cipher_key;
  335. int csp_auth_alg;
  336. int csp_auth_klen; /* Key length in bytes. */
  337. const void *csp_auth_key;
  338. int csp_auth_mlen; /* Number of digest bytes to use.
  339. 0 means all. */
  340. };
  341. enum crypto_buffer_type {
  342. CRYPTO_BUF_NONE = 0,
  343. CRYPTO_BUF_CONTIG,
  344. CRYPTO_BUF_UIO,
  345. CRYPTO_BUF_MBUF,
  346. CRYPTO_BUF_VMPAGE,
  347. CRYPTO_BUF_LAST = CRYPTO_BUF_VMPAGE
  348. };
  349. /*
  350. * Description of a data buffer for a request. Requests can either
  351. * have a single buffer that is modified in place or separate input
  352. * and output buffers.
  353. */
  354. struct crypto_buffer {
  355. union {
  356. struct {
  357. char *cb_buf;
  358. int cb_buf_len;
  359. };
  360. struct mbuf *cb_mbuf;
  361. struct {
  362. vm_page_t *cb_vm_page;
  363. int cb_vm_page_len;
  364. int cb_vm_page_offset;
  365. };
  366. struct uio *cb_uio;
  367. };
  368. enum crypto_buffer_type cb_type;
  369. };
  370. /*
  371. * A cursor is used to iterate through a crypto request data buffer.
  372. */
  373. struct crypto_buffer_cursor {
  374. union {
  375. char *cc_buf;
  376. struct mbuf *cc_mbuf;
  377. struct iovec *cc_iov;
  378. vm_page_t *cc_vmpage;
  379. };
  380. /* Optional bytes of valid data remaining */
  381. int cc_buf_len;
  382. /*
  383. * Optional offset within the current buffer segment where
  384. * valid data begins
  385. */
  386. size_t cc_offset;
  387. enum crypto_buffer_type cc_type;
  388. };
  389. /* Structure describing complete operation */
  390. struct cryptop {
  391. TAILQ_ENTRY(cryptop) crp_next;
  392. struct task crp_task;
  393. crypto_session_t crp_session; /* Session */
  394. int crp_olen; /* Result total length */
  395. int crp_etype; /*
  396. * Error type (zero means no error).
  397. * All error codes except EAGAIN
  398. * indicate possible data corruption (as in,
  399. * the data have been touched). On all
  400. * errors, the crp_session may have changed
  401. * (reset to a new one), so the caller
  402. * should always check and use the new
  403. * value on future requests.
  404. */
  405. int crp_flags;
  406. #define CRYPTO_F_BATCH 0x0008 /* Batch op if possible */
  407. #define CRYPTO_F_CBIMM 0x0010 /* Do callback immediately */
  408. #define CRYPTO_F_DONE 0x0020 /* Operation completed */
  409. #define CRYPTO_F_CBIFSYNC 0x0040 /* Do CBIMM if op is synchronous */
  410. #define CRYPTO_F_ASYNC 0x0080 /* Dispatch crypto jobs on several threads
  411. * if op is synchronous
  412. */
  413. #define CRYPTO_F_ASYNC_KEEPORDER 0x0100 /*
  414. * Dispatch the crypto jobs in the same
  415. * order there are submitted. Applied only
  416. * if CRYPTO_F_ASYNC flags is set
  417. */
  418. #define CRYPTO_F_IV_SEPARATE 0x0200 /* Use crp_iv[] as IV. */
  419. int crp_op;
  420. struct crypto_buffer crp_buf;
  421. struct crypto_buffer crp_obuf;
  422. void *crp_aad; /* AAD buffer. */
  423. int crp_aad_start; /* Location of AAD. */
  424. int crp_aad_length; /* 0 => no AAD. */
  425. uint8_t crp_esn[4]; /* high-order ESN */
  426. int crp_iv_start; /* Location of IV. IV length is from
  427. * the session.
  428. */
  429. int crp_payload_start; /* Location of ciphertext. */
  430. int crp_payload_output_start;
  431. int crp_payload_length;
  432. int crp_digest_start; /* Location of MAC/tag. Length is
  433. * from the session.
  434. */
  435. uint8_t crp_iv[EALG_MAX_BLOCK_LEN]; /* IV if IV_SEPARATE. */
  436. const void *crp_cipher_key; /* New cipher key if non-NULL. */
  437. const void *crp_auth_key; /* New auth key if non-NULL. */
  438. void *crp_opaque; /* Opaque pointer, passed along */
  439. int (*crp_callback)(struct cryptop *); /* Callback function */
  440. struct bintime crp_tstamp; /* performance time stamp */
  441. uint32_t crp_seq; /* used for ordered dispatch */
  442. uint32_t crp_retw_id; /*
  443. * the return worker to be used,
  444. * used for ordered dispatch
  445. */
  446. };
  447. static __inline void
  448. _crypto_use_buf(struct crypto_buffer *cb, void *buf, int len)
  449. {
  450. cb->cb_buf = buf;
  451. cb->cb_buf_len = len;
  452. cb->cb_type = CRYPTO_BUF_CONTIG;
  453. }
  454. static __inline void
  455. _crypto_use_mbuf(struct crypto_buffer *cb, struct mbuf *m)
  456. {
  457. cb->cb_mbuf = m;
  458. cb->cb_type = CRYPTO_BUF_MBUF;
  459. }
  460. static __inline void
  461. _crypto_use_vmpage(struct crypto_buffer *cb, vm_page_t *pages, int len,
  462. int offset)
  463. {
  464. cb->cb_vm_page = pages;
  465. cb->cb_vm_page_len = len;
  466. cb->cb_vm_page_offset = offset;
  467. cb->cb_type = CRYPTO_BUF_VMPAGE;
  468. }
  469. static __inline void
  470. _crypto_use_uio(struct crypto_buffer *cb, struct uio *uio)
  471. {
  472. cb->cb_uio = uio;
  473. cb->cb_type = CRYPTO_BUF_UIO;
  474. }
  475. static __inline void
  476. crypto_use_buf(struct cryptop *crp, void *buf, int len)
  477. {
  478. _crypto_use_buf(&crp->crp_buf, buf, len);
  479. }
  480. static __inline void
  481. crypto_use_mbuf(struct cryptop *crp, struct mbuf *m)
  482. {
  483. _crypto_use_mbuf(&crp->crp_buf, m);
  484. }
  485. static __inline void
  486. crypto_use_vmpage(struct cryptop *crp, vm_page_t *pages, int len, int offset)
  487. {
  488. _crypto_use_vmpage(&crp->crp_buf, pages, len, offset);
  489. }
  490. static __inline void
  491. crypto_use_uio(struct cryptop *crp, struct uio *uio)
  492. {
  493. _crypto_use_uio(&crp->crp_buf, uio);
  494. }
  495. static __inline void
  496. crypto_use_output_buf(struct cryptop *crp, void *buf, int len)
  497. {
  498. _crypto_use_buf(&crp->crp_obuf, buf, len);
  499. }
  500. static __inline void
  501. crypto_use_output_mbuf(struct cryptop *crp, struct mbuf *m)
  502. {
  503. _crypto_use_mbuf(&crp->crp_obuf, m);
  504. }
  505. static __inline void
  506. crypto_use_output_vmpage(struct cryptop *crp, vm_page_t *pages, int len,
  507. int offset)
  508. {
  509. _crypto_use_vmpage(&crp->crp_obuf, pages, len, offset);
  510. }
  511. static __inline void
  512. crypto_use_output_uio(struct cryptop *crp, struct uio *uio)
  513. {
  514. _crypto_use_uio(&crp->crp_obuf, uio);
  515. }
  516. #define CRYPTOP_ASYNC(crp) \
  517. (((crp)->crp_flags & CRYPTO_F_ASYNC) && \
  518. crypto_ses2caps((crp)->crp_session) & CRYPTOCAP_F_SYNC)
  519. #define CRYPTOP_ASYNC_KEEPORDER(crp) \
  520. (CRYPTOP_ASYNC(crp) && \
  521. (crp)->crp_flags & CRYPTO_F_ASYNC_KEEPORDER)
  522. #define CRYPTO_HAS_OUTPUT_BUFFER(crp) \
  523. ((crp)->crp_obuf.cb_type != CRYPTO_BUF_NONE)
  524. /* Flags in crp_op. */
  525. #define CRYPTO_OP_DECRYPT 0x0
  526. #define CRYPTO_OP_ENCRYPT 0x1
  527. #define CRYPTO_OP_IS_ENCRYPT(op) ((op) & CRYPTO_OP_ENCRYPT)
  528. #define CRYPTO_OP_COMPUTE_DIGEST 0x0
  529. #define CRYPTO_OP_VERIFY_DIGEST 0x2
  530. #define CRYPTO_OP_DECOMPRESS CRYPTO_OP_DECRYPT
  531. #define CRYPTO_OP_COMPRESS CRYPTO_OP_ENCRYPT
  532. #define CRYPTO_OP_IS_COMPRESS(op) ((op) & CRYPTO_OP_COMPRESS)
  533. /*
  534. * Hints passed to process methods.
  535. */
  536. #define CRYPTO_HINT_MORE 0x1 /* more ops coming shortly */
  537. struct cryptkop {
  538. TAILQ_ENTRY(cryptkop) krp_next;
  539. u_int krp_op; /* ie. CRK_MOD_EXP or other */
  540. u_int krp_status; /* return status */
  541. u_short krp_iparams; /* # of input parameters */
  542. u_short krp_oparams; /* # of output parameters */
  543. u_int krp_crid; /* desired device, etc. */
  544. uint32_t krp_hid; /* device used */
  545. struct crparam krp_param[CRK_MAXPARAM]; /* kvm */
  546. void (*krp_callback)(struct cryptkop *);
  547. struct cryptocap *krp_cap;
  548. };
  549. uint32_t crypto_ses2hid(crypto_session_t crypto_session);
  550. uint32_t crypto_ses2caps(crypto_session_t crypto_session);
  551. void *crypto_get_driver_session(crypto_session_t crypto_session);
  552. const struct crypto_session_params *crypto_get_params(
  553. crypto_session_t crypto_session);
  554. struct auth_hash *crypto_auth_hash(const struct crypto_session_params *csp);
  555. struct enc_xform *crypto_cipher(const struct crypto_session_params *csp);
  556. MALLOC_DECLARE(M_CRYPTO_DATA);
  557. extern int crypto_newsession(crypto_session_t *cses,
  558. const struct crypto_session_params *params, int hard);
  559. extern void crypto_freesession(crypto_session_t cses);
  560. #define CRYPTOCAP_F_HARDWARE CRYPTO_FLAG_HARDWARE
  561. #define CRYPTOCAP_F_SOFTWARE CRYPTO_FLAG_SOFTWARE
  562. #define CRYPTOCAP_F_SYNC 0x04000000 /* operates synchronously */
  563. #define CRYPTOCAP_F_ACCEL_SOFTWARE 0x08000000
  564. extern int32_t crypto_get_driverid(device_t dev, size_t session_size,
  565. int flags);
  566. extern int crypto_find_driver(const char *);
  567. extern device_t crypto_find_device_byhid(int hid);
  568. extern int crypto_getcaps(int hid);
  569. extern int crypto_kregister(uint32_t, int, uint32_t);
  570. extern int crypto_unregister_all(uint32_t driverid);
  571. extern int crypto_dispatch(struct cryptop *crp);
  572. extern int crypto_kdispatch(struct cryptkop *);
  573. #define CRYPTO_SYMQ 0x1
  574. #define CRYPTO_ASYMQ 0x2
  575. extern int crypto_unblock(uint32_t, int);
  576. extern void crypto_done(struct cryptop *crp);
  577. extern void crypto_kdone(struct cryptkop *);
  578. extern int crypto_getfeat(int *);
  579. extern void crypto_destroyreq(struct cryptop *crp);
  580. extern void crypto_initreq(struct cryptop *crp, crypto_session_t cses);
  581. extern void crypto_freereq(struct cryptop *crp);
  582. extern struct cryptop *crypto_getreq(crypto_session_t cses, int how);
  583. extern int crypto_usercrypto; /* userland may do crypto requests */
  584. extern int crypto_userasymcrypto; /* userland may do asym crypto reqs */
  585. extern int crypto_devallowsoft; /* only use hardware crypto */
  586. #ifdef SYSCTL_DECL
  587. SYSCTL_DECL(_kern_crypto);
  588. #endif
  589. /* Helper routines for drivers to initialize auth contexts for HMAC. */
  590. struct auth_hash;
  591. void hmac_init_ipad(const struct auth_hash *axf, const char *key, int klen,
  592. void *auth_ctx);
  593. void hmac_init_opad(const struct auth_hash *axf, const char *key, int klen,
  594. void *auth_ctx);
  595. /*
  596. * Crypto-related utility routines used mainly by drivers.
  597. *
  598. * Similar to m_copyback/data, *_copyback copy data from the 'src'
  599. * buffer into the crypto request's data buffer while *_copydata copy
  600. * data from the crypto request's data buffer into the the 'dst'
  601. * buffer.
  602. */
  603. void crypto_copyback(struct cryptop *crp, int off, int size,
  604. const void *src);
  605. void crypto_copydata(struct cryptop *crp, int off, int size, void *dst);
  606. int crypto_apply(struct cryptop *crp, int off, int len,
  607. int (*f)(void *, const void *, u_int), void *arg);
  608. void *crypto_contiguous_subsegment(struct cryptop *crp, size_t skip,
  609. size_t len);
  610. int crypto_apply_buf(struct crypto_buffer *cb, int off, int len,
  611. int (*f)(void *, const void *, u_int), void *arg);
  612. void *crypto_buffer_contiguous_subsegment(struct crypto_buffer *cb,
  613. size_t skip, size_t len);
  614. size_t crypto_buffer_len(struct crypto_buffer *cb);
  615. void crypto_cursor_init(struct crypto_buffer_cursor *cc,
  616. const struct crypto_buffer *cb);
  617. void crypto_cursor_advance(struct crypto_buffer_cursor *cc, size_t amount);
  618. void *crypto_cursor_segbase(struct crypto_buffer_cursor *cc);
  619. size_t crypto_cursor_seglen(struct crypto_buffer_cursor *cc);
  620. void crypto_cursor_copyback(struct crypto_buffer_cursor *cc, int size,
  621. const void *vsrc);
  622. void crypto_cursor_copydata(struct crypto_buffer_cursor *cc, int size,
  623. void *vdst);
  624. void crypto_cursor_copydata_noadv(struct crypto_buffer_cursor *cc, int size,
  625. void *vdst);
  626. static __inline void
  627. crypto_read_iv(struct cryptop *crp, void *iv)
  628. {
  629. const struct crypto_session_params *csp;
  630. csp = crypto_get_params(crp->crp_session);
  631. if (crp->crp_flags & CRYPTO_F_IV_SEPARATE)
  632. memcpy(iv, crp->crp_iv, csp->csp_ivlen);
  633. else
  634. crypto_copydata(crp, crp->crp_iv_start, csp->csp_ivlen, iv);
  635. }
  636. #endif /* _KERNEL */
  637. #endif /* _CRYPTO_CRYPTO_H_ */