1f4bf4335SSam Leffler.\" $OpenBSD: crypto.9,v 1.19 2002/07/16 06:31:57 angelos Exp $ 2f4bf4335SSam Leffler.\" 3571dba6eSHiten Pandya.\" The author of this manual page is Angelos D. Keromytis (angelos@cis.upenn.edu) 4f4bf4335SSam Leffler.\" 5f4bf4335SSam Leffler.\" Copyright (c) 2000, 2001 Angelos D. Keromytis 6f4bf4335SSam Leffler.\" 7f4bf4335SSam Leffler.\" Permission to use, copy, and modify this software with or without fee 8f4bf4335SSam Leffler.\" is hereby granted, provided that this entire notice is included in 9f4bf4335SSam Leffler.\" all source code copies of any software which is or includes a copy or 10f4bf4335SSam Leffler.\" modification of this software. 11f4bf4335SSam Leffler.\" 12f4bf4335SSam Leffler.\" THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR 13f4bf4335SSam Leffler.\" IMPLIED WARRANTY. IN PARTICULAR, NONE OF THE AUTHORS MAKES ANY 14f4bf4335SSam Leffler.\" REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE 15f4bf4335SSam Leffler.\" MERCHANTABILITY OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR 16f4bf4335SSam Leffler.\" PURPOSE. 17f4bf4335SSam Leffler.\" 187621fdabSRuslan Ermilov.\" $FreeBSD$ 197621fdabSRuslan Ermilov.\" 20*08fca7a5SJohn-Mark Gurney.Dd December 12, 2014 21f4bf4335SSam Leffler.Dt CRYPTO 9 22f4bf4335SSam Leffler.Os 23f4bf4335SSam Leffler.Sh NAME 24f4bf4335SSam Leffler.Nm crypto 25f4bf4335SSam Leffler.Nd API for cryptographic services in the kernel 26f4bf4335SSam Leffler.Sh SYNOPSIS 277621fdabSRuslan Ermilov.In opencrypto/cryptodev.h 28f4bf4335SSam Leffler.Ft int32_t 296b99842aSEd Schouten.Fn crypto_get_driverid uint8_t 30f4bf4335SSam Leffler.Ft int 316b99842aSEd Schouten.Fn crypto_register uint32_t int uint16_t uint32_t "int \*[lp]*\*[rp]\*[lp]void *, uint32_t *, struct cryptoini *\*[rp]" "int \*[lp]*\*[rp]\*[lp]void *, uint64_t\*[rp]" "int \*[lp]*\*[rp]\*[lp]void *, struct cryptop *\*[rp]" "void *" 32f4bf4335SSam Leffler.Ft int 336b99842aSEd Schouten.Fn crypto_kregister uint32_t int uint32_t "int \*[lp]*\*[rp]\*[lp]void *, struct cryptkop *\*[rp]" "void *" 34f4bf4335SSam Leffler.Ft int 356b99842aSEd Schouten.Fn crypto_unregister uint32_t int 361403a8c7SSam Leffler.Ft int 376b99842aSEd Schouten.Fn crypto_unregister_all uint32_t 38f4bf4335SSam Leffler.Ft void 39f4bf4335SSam Leffler.Fn crypto_done "struct cryptop *" 40f4bf4335SSam Leffler.Ft void 41f4bf4335SSam Leffler.Fn crypto_kdone "struct cryptkop *" 42f4bf4335SSam Leffler.Ft int 436b99842aSEd Schouten.Fn crypto_newsession "uint64_t *" "struct cryptoini *" int 44f4bf4335SSam Leffler.Ft int 456b99842aSEd Schouten.Fn crypto_freesession uint64_t 46f4bf4335SSam Leffler.Ft int 47f4bf4335SSam Leffler.Fn crypto_dispatch "struct cryptop *" 48f4bf4335SSam Leffler.Ft int 49f4bf4335SSam Leffler.Fn crypto_kdispatch "struct cryptkop *" 501403a8c7SSam Leffler.Ft int 516b99842aSEd Schouten.Fn crypto_unblock uint32_t int 527621fdabSRuslan Ermilov.Ft "struct cryptop *" 537621fdabSRuslan Ermilov.Fn crypto_getreq int 54f4bf4335SSam Leffler.Ft void 557621fdabSRuslan Ermilov.Fn crypto_freereq void 56f4bf4335SSam Leffler.Bd -literal 571403a8c7SSam Leffler#define CRYPTO_SYMQ 0x1 581403a8c7SSam Leffler#define CRYPTO_ASYMQ 0x2 591403a8c7SSam Leffler 60f4bf4335SSam Leffler#define EALG_MAX_BLOCK_LEN 16 61f4bf4335SSam Leffler 62f4bf4335SSam Lefflerstruct cryptoini { 63f4bf4335SSam Leffler int cri_alg; 64f4bf4335SSam Leffler int cri_klen; 65f3326d72SPawel Jakub Dawidek int cri_mlen; 66f4bf4335SSam Leffler caddr_t cri_key; 676b99842aSEd Schouten uint8_t cri_iv[EALG_MAX_BLOCK_LEN]; 68f4bf4335SSam Leffler struct cryptoini *cri_next; 69f4bf4335SSam Leffler}; 70f4bf4335SSam Leffler 71f4bf4335SSam Lefflerstruct cryptodesc { 72f4bf4335SSam Leffler int crd_skip; 73f4bf4335SSam Leffler int crd_len; 74f4bf4335SSam Leffler int crd_inject; 75f4bf4335SSam Leffler int crd_flags; 76f4bf4335SSam Leffler struct cryptoini CRD_INI; 773b72821fSPawel Jakub Dawidek#define crd_iv CRD_INI.cri_iv 783b72821fSPawel Jakub Dawidek#define crd_key CRD_INI.cri_key 793b72821fSPawel Jakub Dawidek#define crd_alg CRD_INI.cri_alg 803b72821fSPawel Jakub Dawidek#define crd_klen CRD_INI.cri_klen 81f4bf4335SSam Leffler struct cryptodesc *crd_next; 82f4bf4335SSam Leffler}; 83f4bf4335SSam Leffler 84f4bf4335SSam Lefflerstruct cryptop { 851403a8c7SSam Leffler TAILQ_ENTRY(cryptop) crp_next; 866b99842aSEd Schouten uint64_t crp_sid; 87f4bf4335SSam Leffler int crp_ilen; 88f4bf4335SSam Leffler int crp_olen; 89f4bf4335SSam Leffler int crp_etype; 90f4bf4335SSam Leffler int crp_flags; 91f4bf4335SSam Leffler caddr_t crp_buf; 92f4bf4335SSam Leffler caddr_t crp_opaque; 93f4bf4335SSam Leffler struct cryptodesc *crp_desc; 94f4bf4335SSam Leffler int (*crp_callback) (struct cryptop *); 95f4bf4335SSam Leffler caddr_t crp_mac; 96f4bf4335SSam Leffler}; 97f4bf4335SSam Leffler 98f4bf4335SSam Lefflerstruct crparam { 99f4bf4335SSam Leffler caddr_t crp_p; 100f4bf4335SSam Leffler u_int crp_nbits; 101f4bf4335SSam Leffler}; 102f4bf4335SSam Leffler 103f4bf4335SSam Leffler#define CRK_MAXPARAM 8 104f4bf4335SSam Leffler 105f4bf4335SSam Lefflerstruct cryptkop { 1061403a8c7SSam Leffler TAILQ_ENTRY(cryptkop) krp_next; 107f4bf4335SSam Leffler u_int krp_op; /* ie. CRK_MOD_EXP or other */ 108f4bf4335SSam Leffler u_int krp_status; /* return status */ 109f4bf4335SSam Leffler u_short krp_iparams; /* # of input parameters */ 110f4bf4335SSam Leffler u_short krp_oparams; /* # of output parameters */ 1116b99842aSEd Schouten uint32_t krp_hid; 1121403a8c7SSam Leffler struct crparam krp_param[CRK_MAXPARAM]; 113f4bf4335SSam Leffler int (*krp_callback)(struct cryptkop *); 114f4bf4335SSam Leffler}; 115f4bf4335SSam Leffler.Ed 116f4bf4335SSam Leffler.Sh DESCRIPTION 117f4bf4335SSam Leffler.Nm 118f4bf4335SSam Leffleris a framework for drivers of cryptographic hardware to register with 119f4bf4335SSam Lefflerthe kernel so 120f4bf4335SSam Leffler.Dq consumers 1211403a8c7SSam Leffler(other kernel subsystems, and 1221403a8c7SSam Lefflerusers through the 1231403a8c7SSam Leffler.Pa /dev/crypto 1241403a8c7SSam Lefflerdevice) are able to make use of it. 125f4bf4335SSam LefflerDrivers register with the framework the algorithms they support, 126f4bf4335SSam Lefflerand provide entry points (functions) the framework may call to 127f4bf4335SSam Lefflerestablish, use, and tear down sessions. 128f4bf4335SSam LefflerSessions are used to cache cryptographic information in a particular driver 129f4bf4335SSam Leffler(or associated hardware), so initialization is not needed with every request. 130f4bf4335SSam LefflerConsumers of cryptographic services pass a set of 131f4bf4335SSam Lefflerdescriptors that instruct the framework (and the drivers registered 132f4bf4335SSam Lefflerwith it) of the operations that should be applied on the data (more 133f4bf4335SSam Lefflerthan one cryptographic operation can be requested). 134f4bf4335SSam Leffler.Pp 135f4bf4335SSam LefflerKeying operations are supported as well. 136f4bf4335SSam LefflerUnlike the symmetric operators described above, 137f4bf4335SSam Lefflerthese sessionless commands perform mathematical operations using 138f4bf4335SSam Lefflerinput and output parameters. 139f4bf4335SSam Leffler.Pp 140f4bf4335SSam LefflerSince the consumers may not be associated with a process, drivers may 1411403a8c7SSam Lefflernot 1421403a8c7SSam Leffler.Xr sleep 9 . 143f4bf4335SSam LefflerThe same holds for the framework. 144f4bf4335SSam LefflerThus, a callback mechanism is used 145f4bf4335SSam Lefflerto notify a consumer that a request has been completed (the 1460f7e2491SChristian Brueffercallback is specified by the consumer on a per-request basis). 147f4bf4335SSam LefflerThe callback is invoked by the framework whether the request was 148f4bf4335SSam Lefflersuccessfully completed or not. 149f4bf4335SSam LefflerAn error indication is provided in the latter case. 150f4bf4335SSam LefflerA specific error code, 151f4bf4335SSam Leffler.Er EAGAIN , 152f4bf4335SSam Leffleris used to indicate that a session number has changed and that the 153f4bf4335SSam Lefflerrequest may be re-submitted immediately with the new session number. 154f4bf4335SSam LefflerErrors are only returned to the invoking function if not 155f4bf4335SSam Lefflerenough information to call the callback is available (meaning, there 156f4bf4335SSam Lefflerwas a fatal error in verifying the arguments). 157f4bf4335SSam LefflerFor session initialization and teardown there is no callback mechanism used. 158f4bf4335SSam Leffler.Pp 159f4bf4335SSam LefflerThe 160f4bf4335SSam Leffler.Fn crypto_newsession 161f4bf4335SSam Lefflerroutine is called by consumers of cryptographic services (such as the 162f4bf4335SSam Leffler.Xr ipsec 4 163f4bf4335SSam Lefflerstack) that wish to establish a new session with the framework. 164f4bf4335SSam LefflerOn success, the first argument will contain the Session Identifier (SID). 165f4bf4335SSam LefflerThe second argument contains all the necessary information for 166f4bf4335SSam Lefflerthe driver to establish the session. 167f4bf4335SSam LefflerThe third argument indicates whether a 168f4bf4335SSam Lefflerhardware driver (1) should be used or not (0). 169f4bf4335SSam LefflerThe various fields in the 1707621fdabSRuslan Ermilov.Vt cryptoini 171f4bf4335SSam Lefflerstructure are: 1727621fdabSRuslan Ermilov.Bl -tag -width ".Va cri_next" 1737621fdabSRuslan Ermilov.It Va cri_alg 174f4bf4335SSam LefflerContains an algorithm identifier. 175f4bf4335SSam LefflerCurrently supported algorithms are: 176f4bf4335SSam Leffler.Pp 1777621fdabSRuslan Ermilov.Bl -tag -width ".Dv CRYPTO_RIPEMD160_HMAC" -compact 178*08fca7a5SJohn-Mark Gurney.It Dv CRYPTO_AES_128_NIST_GMAC 179*08fca7a5SJohn-Mark Gurney.It Dv CRYPTO_AES_192_NIST_GMAC 180*08fca7a5SJohn-Mark Gurney.It Dv CRYPTO_AES_256_NIST_GMAC 1817621fdabSRuslan Ermilov.It Dv CRYPTO_AES_CBC 182*08fca7a5SJohn-Mark Gurney.It Dv CRYPTO_AES_ICM 183*08fca7a5SJohn-Mark Gurney.It Dv CRYPTO_AES_NIST_GCM_16 184*08fca7a5SJohn-Mark Gurney.It Dv CRYPTO_AES_NIST_GMAC 185*08fca7a5SJohn-Mark Gurney.It Dv CRYPTO_AES_XTS 1867621fdabSRuslan Ermilov.It Dv CRYPTO_ARC4 187b61e8b3eSChristian Brueffer.It Dv CRYPTO_BLF_CBC 188b61e8b3eSChristian Brueffer.It Dv CRYPTO_CAMELLIA_CBC 189b61e8b3eSChristian Brueffer.It Dv CRYPTO_CAST_CBC 190*08fca7a5SJohn-Mark Gurney.It Dv CRYPTO_DEFLATE_COMP 191b61e8b3eSChristian Brueffer.It Dv CRYPTO_DES_CBC 192b61e8b3eSChristian Brueffer.It Dv CRYPTO_3DES_CBC 1937621fdabSRuslan Ermilov.It Dv CRYPTO_MD5 194b61e8b3eSChristian Brueffer.It Dv CRYPTO_MD5_HMAC 195b61e8b3eSChristian Brueffer.It Dv CRYPTO_MD5_KPDK 196*08fca7a5SJohn-Mark Gurney.It Dv CRYPTO_NULL_HMAC 197*08fca7a5SJohn-Mark Gurney.It Dv CRYPTO_NULL_CBC 198b61e8b3eSChristian Brueffer.It Dv CRYPTO_RIPEMD160_HMAC 1997621fdabSRuslan Ermilov.It Dv CRYPTO_SHA1 200b61e8b3eSChristian Brueffer.It Dv CRYPTO_SHA1_HMAC 201b61e8b3eSChristian Brueffer.It Dv CRYPTO_SHA1_KPDK 20271ee05c8SPawel Jakub Dawidek.It Dv CRYPTO_SHA2_256_HMAC 20371ee05c8SPawel Jakub Dawidek.It Dv CRYPTO_SHA2_384_HMAC 20471ee05c8SPawel Jakub Dawidek.It Dv CRYPTO_SHA2_512_HMAC 205*08fca7a5SJohn-Mark Gurney.It Dv CRYPTO_SKIPJACK_CBC 2067621fdabSRuslan Ermilov.El 2077621fdabSRuslan Ermilov.It Va cri_klen 208f4bf4335SSam LefflerSpecifies the length of the key in bits, for variable-size key 209f4bf4335SSam Leffleralgorithms. 210f3326d72SPawel Jakub Dawidek.It Va cri_mlen 211f3326d72SPawel Jakub DawidekSpecifies how many bytes from the calculated hash should be copied back. 212f3326d72SPawel Jakub Dawidek0 means entire hash. 2137621fdabSRuslan Ermilov.It Va cri_key 214f4bf4335SSam LefflerContains the key to be used with the algorithm. 2157621fdabSRuslan Ermilov.It Va cri_iv 216f4bf4335SSam LefflerContains an explicit initialization vector (IV), if it does not prefix 217f4bf4335SSam Lefflerthe data. 218*08fca7a5SJohn-Mark GurneyThis field is ignored during initialization 219*08fca7a5SJohn-Mark Gurney.Pq Nm crypto_newsession . 220f4bf4335SSam LefflerIf no IV is explicitly passed (see below on details), a random IV is used 221f4bf4335SSam Lefflerby the device driver processing the request. 2227621fdabSRuslan Ermilov.It Va cri_next 223f4bf4335SSam LefflerContains a pointer to another 2247621fdabSRuslan Ermilov.Vt cryptoini 225f4bf4335SSam Lefflerstructure. 226f4bf4335SSam LefflerMultiple such structures may be linked to establish multi-algorithm sessions 2277621fdabSRuslan Ermilov.Xr ( ipsec 4 228f4bf4335SSam Leffleris an example consumer of such a feature). 229f4bf4335SSam Leffler.El 230f4bf4335SSam Leffler.Pp 231f4bf4335SSam LefflerThe 2327621fdabSRuslan Ermilov.Vt cryptoini 233f4bf4335SSam Lefflerstructure and its contents will not be modified by the framework (or 234f4bf4335SSam Lefflerthe drivers used). 235f4bf4335SSam LefflerSubsequent requests for processing that use the 236f4bf4335SSam LefflerSID returned will avoid the cost of re-initializing the hardware (in 237f4bf4335SSam Leffleressence, SID acts as an index in the session cache of the driver). 238f4bf4335SSam Leffler.Pp 239f4bf4335SSam Leffler.Fn crypto_freesession 240f4bf4335SSam Leffleris called with the SID returned by 241f4bf4335SSam Leffler.Fn crypto_newsession 242f4bf4335SSam Lefflerto disestablish the session. 243f4bf4335SSam Leffler.Pp 244f4bf4335SSam Leffler.Fn crypto_dispatch 245f4bf4335SSam Leffleris called to process a request. 246f4bf4335SSam LefflerThe various fields in the 2477621fdabSRuslan Ermilov.Vt cryptop 248f4bf4335SSam Lefflerstructure are: 2497621fdabSRuslan Ermilov.Bl -tag -width ".Va crp_callback" 2507621fdabSRuslan Ermilov.It Va crp_sid 251f4bf4335SSam LefflerContains the SID. 2527621fdabSRuslan Ermilov.It Va crp_ilen 253f4bf4335SSam LefflerIndicates the total length in bytes of the buffer to be processed. 2547621fdabSRuslan Ermilov.It Va crp_olen 255f4bf4335SSam LefflerOn return, contains the total length of the result. 256f4bf4335SSam LefflerFor symmetric crypto operations, this will be the same as the input length. 257f4bf4335SSam LefflerThis will be used if the framework needs to allocate a new 258f4bf4335SSam Lefflerbuffer for the result (or for re-formatting the input). 2597621fdabSRuslan Ermilov.It Va crp_callback 260f4bf4335SSam LefflerThis routine is invoked upon completion of the request, whether 261f4bf4335SSam Lefflersuccessful or not. 262f4bf4335SSam LefflerIt is invoked through the 263f4bf4335SSam Leffler.Fn crypto_done 264f4bf4335SSam Lefflerroutine. 265f4bf4335SSam LefflerIf the request was not successful, an error code is set in the 2667621fdabSRuslan Ermilov.Va crp_etype 267f4bf4335SSam Lefflerfield. 268f4bf4335SSam LefflerIt is the responsibility of the callback routine to set the appropriate 269f4bf4335SSam Leffler.Xr spl 9 270f4bf4335SSam Lefflerlevel. 2717621fdabSRuslan Ermilov.It Va crp_etype 272f4bf4335SSam LefflerContains the error type, if any errors were encountered, or zero if 273f4bf4335SSam Lefflerthe request was successfully processed. 274f4bf4335SSam LefflerIf the 275f4bf4335SSam Leffler.Er EAGAIN 276f4bf4335SSam Lefflererror code is returned, the SID has changed (and has been recorded in the 2777621fdabSRuslan Ermilov.Va crp_sid 278f4bf4335SSam Lefflerfield). 279f4bf4335SSam LefflerThe consumer should record the new SID and use it in all subsequent requests. 280f4bf4335SSam LefflerIn this case, the request may be re-submitted immediately. 281f4bf4335SSam LefflerThis mechanism is used by the framework to perform 282f4bf4335SSam Lefflersession migration (move a session from one driver to another, because 283f4bf4335SSam Lefflerof availability, performance, or other considerations). 284f4bf4335SSam Leffler.Pp 285f4bf4335SSam LefflerNote that this field only makes sense when examined by 286f4bf4335SSam Lefflerthe callback routine specified in 2877621fdabSRuslan Ermilov.Va crp_callback . 288f4bf4335SSam LefflerErrors are returned to the invoker of 289f4bf4335SSam Leffler.Fn crypto_process 290f4bf4335SSam Leffleronly when enough information is not present to call the callback 291f4bf4335SSam Lefflerroutine (i.e., if the pointer passed is 292f4bf4335SSam Leffler.Dv NULL 293f4bf4335SSam Leffleror if no callback routine was specified). 2947621fdabSRuslan Ermilov.It Va crp_flags 295f4bf4335SSam LefflerIs a bitmask of flags associated with this request. 296f4bf4335SSam LefflerCurrently defined flags are: 297265803d0SPawel Jakub Dawidek.Bl -tag -width ".Dv CRYPTO_F_CBIFSYNC" 298f4bf4335SSam Leffler.It Dv CRYPTO_F_IMBUF 299f4bf4335SSam LefflerThe buffer pointed to by 3007621fdabSRuslan Ermilov.Va crp_buf 301f4bf4335SSam Leffleris an mbuf chain. 302265803d0SPawel Jakub Dawidek.It Dv CRYPTO_F_IOV 303265803d0SPawel Jakub DawidekThe buffer pointed to by 304265803d0SPawel Jakub Dawidek.Va crp_buf 30581ae4b8dSRuslan Ermilovis an 30681ae4b8dSRuslan Ermilov.Vt uio 30781ae4b8dSRuslan Ermilovstructure. 308265803d0SPawel Jakub Dawidek.It Dv CRYPTO_F_BATCH 309265803d0SPawel Jakub DawidekBatch operation if possible. 310265803d0SPawel Jakub Dawidek.It Dv CRYPTO_F_CBIMM 3119280e5faSMike PritchardDo callback immediately instead of doing it from a dedicated kernel thread. 312265803d0SPawel Jakub Dawidek.It Dv CRYPTO_F_DONE 313265803d0SPawel Jakub DawidekOperation completed. 314265803d0SPawel Jakub Dawidek.It Dv CRYPTO_F_CBIFSYNC 3159280e5faSMike PritchardDo callback immediately if operation is synchronous. 316f4bf4335SSam Leffler.El 3177621fdabSRuslan Ermilov.It Va crp_buf 318f4bf4335SSam LefflerPoints to the input buffer. 319f4bf4335SSam LefflerOn return (when the callback is invoked), 320f4bf4335SSam Lefflerit contains the result of the request. 321f4bf4335SSam LefflerThe input buffer may be an mbuf 3221403a8c7SSam Lefflerchain or a contiguous buffer, 323f4bf4335SSam Lefflerdepending on 3247621fdabSRuslan Ermilov.Va crp_flags . 3257621fdabSRuslan Ermilov.It Va crp_opaque 326f4bf4335SSam LefflerThis is passed through the crypto framework untouched and is 327f4bf4335SSam Lefflerintended for the invoking application's use. 3287621fdabSRuslan Ermilov.It Va crp_desc 329f4bf4335SSam LefflerThis is a linked list of descriptors. 330f4bf4335SSam LefflerEach descriptor provides 331f4bf4335SSam Lefflerinformation about what type of cryptographic operation should be done 332f4bf4335SSam Leffleron the input buffer. 333f4bf4335SSam LefflerThe various fields are: 3347621fdabSRuslan Ermilov.Bl -tag -width ".Va crd_inject" 3353b72821fSPawel Jakub Dawidek.It Va crd_iv 3363b72821fSPawel Jakub DawidekThe field where IV should be provided when the 3373b72821fSPawel Jakub Dawidek.Dv CRD_F_IV_EXPLICIT 3383b72821fSPawel Jakub Dawidekflag is given. 3393b72821fSPawel Jakub Dawidek.It Va crd_key 3403b72821fSPawel Jakub DawidekWhen the 3413b72821fSPawel Jakub Dawidek.Dv CRD_F_KEY_EXPLICIT 3423b72821fSPawel Jakub Dawidekflag is given, the 3433b72821fSPawel Jakub Dawidek.Va crd_key 3443b72821fSPawel Jakub Dawidekpoints to a buffer with encryption or authentication key. 3453b72821fSPawel Jakub Dawidek.It Va crd_alg 3463b72821fSPawel Jakub DawidekAn algorithm to use. 3473b72821fSPawel Jakub DawidekMust be the same as the one given at newsession time. 3483b72821fSPawel Jakub Dawidek.It Va crd_klen 3493b72821fSPawel Jakub DawidekThe 3503b72821fSPawel Jakub Dawidek.Va crd_key 3513b72821fSPawel Jakub Dawidekkey length. 3527621fdabSRuslan Ermilov.It Va crd_skip 353f4bf4335SSam LefflerThe offset in the input buffer where processing should start. 3547621fdabSRuslan Ermilov.It Va crd_len 355f4bf4335SSam LefflerHow many bytes, after 3567621fdabSRuslan Ermilov.Va crd_skip , 357f4bf4335SSam Lefflershould be processed. 3587621fdabSRuslan Ermilov.It Va crd_inject 359f4bf4335SSam LefflerOffset from the beginning of the buffer to insert any results. 360f4bf4335SSam LefflerFor encryption algorithms, this is where the initialization vector 361f4bf4335SSam Leffler(IV) will be inserted when encrypting or where it can be found when 362f4bf4335SSam Lefflerdecrypting (subject to 3637621fdabSRuslan Ermilov.Va crd_flags ) . 364f4bf4335SSam LefflerFor MAC algorithms, this is where the result of the keyed hash will be 365f4bf4335SSam Lefflerinserted. 3667621fdabSRuslan Ermilov.It Va crd_flags 367f4bf4335SSam LefflerThe following flags are defined: 36881ae4b8dSRuslan Ermilov.Bl -tag -width 3n 369f4bf4335SSam Leffler.It Dv CRD_F_ENCRYPT 370f4bf4335SSam LefflerFor encryption algorithms, this bit is set when encryption is required 371f4bf4335SSam Leffler(when not set, decryption is performed). 372f4bf4335SSam Leffler.It Dv CRD_F_IV_PRESENT 373*08fca7a5SJohn-Mark GurneyFor encryption, this bit is set when the IV already 374f4bf4335SSam Lefflerprecedes the data, so the 3757621fdabSRuslan Ermilov.Va crd_inject 376f4bf4335SSam Lefflervalue will be ignored and no IV will be written in the buffer. 377f4bf4335SSam LefflerOtherwise, the IV used to encrypt the packet will be written 378f4bf4335SSam Lefflerat the location pointed to by 3797621fdabSRuslan Ermilov.Va crd_inject . 380f4bf4335SSam LefflerThe IV length is assumed to be equal to the blocksize of the 381f4bf4335SSam Lefflerencryption algorithm. 382*08fca7a5SJohn-Mark GurneyApplications that do special 3837621fdabSRuslan Ermilov.Dq "IV cooking" , 384f4bf4335SSam Lefflersuch as the half-IV mode in 385f4bf4335SSam Leffler.Xr ipsec 4 , 386f4bf4335SSam Lefflercan use this flag to indicate that the IV should not be written on the packet. 387f4bf4335SSam LefflerThis flag is typically used in conjunction with the 388f4bf4335SSam Leffler.Dv CRD_F_IV_EXPLICIT 389f4bf4335SSam Lefflerflag. 390f4bf4335SSam Leffler.It Dv CRD_F_IV_EXPLICIT 391f4bf4335SSam LefflerFor encryption algorithms, this bit is set when the IV is explicitly 392f4bf4335SSam Lefflerprovided by the consumer in the 3933b72821fSPawel Jakub Dawidek.Va crd_iv 3941deaec43SPawel Jakub Dawidekfield. 395f4bf4335SSam LefflerOtherwise, for encryption operations the IV is provided for by 396f4bf4335SSam Lefflerthe driver used to perform the operation, whereas for decryption 397f4bf4335SSam Leffleroperations it is pointed to by the 3987621fdabSRuslan Ermilov.Va crd_inject 399f4bf4335SSam Lefflerfield. 400f4bf4335SSam LefflerThis flag is typically used when the IV is calculated 4017621fdabSRuslan Ermilov.Dq "on the fly" 402f4bf4335SSam Lefflerby the consumer, and does not precede the data (some 403f4bf4335SSam Leffler.Xr ipsec 4 404f4bf4335SSam Lefflerconfigurations, and the encrypted swap are two such examples). 4051deaec43SPawel Jakub Dawidek.It Dv CRD_F_KEY_EXPLICIT 4063b72821fSPawel Jakub DawidekFor encryption and authentication (MAC) algorithms, this bit is set when the key 4073b72821fSPawel Jakub Dawidekis explicitly provided by the consumer in the 4083b72821fSPawel Jakub Dawidek.Va crd_key 4091deaec43SPawel Jakub Dawidekfield for the given operation. 4103b72821fSPawel Jakub DawidekOtherwise, the key is taken at newsession time from the 4113b72821fSPawel Jakub Dawidek.Va cri_key 4123b72821fSPawel Jakub Dawidekfield. 413*08fca7a5SJohn-Mark GurneyAs calculating the key schedule may take a while, it is recommended that often 414*08fca7a5SJohn-Mark Gurneyused keys are given their own session. 415f4bf4335SSam Leffler.It Dv CRD_F_COMP 416f4bf4335SSam LefflerFor compression algorithms, this bit is set when compression is required (when 417f4bf4335SSam Lefflernot set, decompression is performed). 418f4bf4335SSam Leffler.El 4197621fdabSRuslan Ermilov.It Va CRD_INI 420f4bf4335SSam LefflerThis 4217621fdabSRuslan Ermilov.Vt cryptoini 422f4bf4335SSam Lefflerstructure will not be modified by the framework or the device drivers. 423f4bf4335SSam LefflerSince this information accompanies every cryptographic 424f4bf4335SSam Leffleroperation request, drivers may re-initialize state on-demand 425f4bf4335SSam Leffler(typically an expensive operation). 426f4bf4335SSam LefflerFurthermore, the cryptographic 427f4bf4335SSam Lefflerframework may re-route requests as a result of full queues or hardware 428f4bf4335SSam Lefflerfailure, as described above. 4297621fdabSRuslan Ermilov.It Va crd_next 430f4bf4335SSam LefflerPoint to the next descriptor. 431f4bf4335SSam LefflerLinked operations are useful in protocols such as 432f4bf4335SSam Leffler.Xr ipsec 4 , 433f4bf4335SSam Lefflerwhere multiple cryptographic transforms may be applied on the same 434f4bf4335SSam Lefflerblock of data. 435f4bf4335SSam Leffler.El 436f4bf4335SSam Leffler.El 437f4bf4335SSam Leffler.Pp 438f4bf4335SSam Leffler.Fn crypto_getreq 439f4bf4335SSam Lefflerallocates a 4407621fdabSRuslan Ermilov.Vt cryptop 441f4bf4335SSam Lefflerstructure with a linked list of as many 4427621fdabSRuslan Ermilov.Vt cryptodesc 443f4bf4335SSam Lefflerstructures as were specified in the argument passed to it. 444f4bf4335SSam Leffler.Pp 445f4bf4335SSam Leffler.Fn crypto_freereq 446f4bf4335SSam Lefflerdeallocates a structure 4477621fdabSRuslan Ermilov.Vt cryptop 448f4bf4335SSam Lefflerand any 4497621fdabSRuslan Ermilov.Vt cryptodesc 450f4bf4335SSam Lefflerstructures linked to it. 451f4bf4335SSam LefflerNote that it is the responsibility of the 452f4bf4335SSam Lefflercallback routine to do the necessary cleanups associated with the 453f4bf4335SSam Leffleropaque field in the 4547621fdabSRuslan Ermilov.Vt cryptop 455f4bf4335SSam Lefflerstructure. 456f4bf4335SSam Leffler.Pp 457f4bf4335SSam Leffler.Fn crypto_kdispatch 458f4bf4335SSam Leffleris called to perform a keying operation. 459f4bf4335SSam LefflerThe various fields in the 4607621fdabSRuslan Ermilov.Vt cryptkop 461f4bf4335SSam Lefflerstructure are: 462c2965cd1SJoel Dahl.Bl -tag -width ".Va krp_callback" 4637621fdabSRuslan Ermilov.It Va krp_op 4647621fdabSRuslan ErmilovOperation code, such as 4657621fdabSRuslan Ermilov.Dv CRK_MOD_EXP . 4667621fdabSRuslan Ermilov.It Va krp_status 467f4bf4335SSam LefflerReturn code. 4687621fdabSRuslan ErmilovThis 4697621fdabSRuslan Ermilov.Va errno Ns -style 4707621fdabSRuslan Ermilovvariable indicates whether lower level reasons 471f4bf4335SSam Lefflerfor operation failure. 4727621fdabSRuslan Ermilov.It Va krp_iparams 473f4bf4335SSam LefflerNumber if input parameters to the specified operation. 474f4bf4335SSam LefflerNote that each operation has a (typically hardwired) number of such parameters. 4757621fdabSRuslan Ermilov.It Va krp_oparams 476f4bf4335SSam LefflerNumber if output parameters from the specified operation. 477f4bf4335SSam LefflerNote that each operation has a (typically hardwired) number of such parameters. 4787621fdabSRuslan Ermilov.It Va krp_kvp 479f4bf4335SSam LefflerAn array of kernel memory blocks containing the parameters. 4807621fdabSRuslan Ermilov.It Va krp_hid 481f4bf4335SSam LefflerIdentifier specifying which low-level driver is being used. 4827621fdabSRuslan Ermilov.It Va krp_callback 483f4bf4335SSam LefflerCallback called on completion of a keying operation. 484f4bf4335SSam Leffler.El 485f4bf4335SSam Leffler.Sh DRIVER-SIDE API 486f4bf4335SSam LefflerThe 487f4bf4335SSam Leffler.Fn crypto_get_driverid , 488f4bf4335SSam Leffler.Fn crypto_register , 489f4bf4335SSam Leffler.Fn crypto_kregister , 490f4bf4335SSam Leffler.Fn crypto_unregister , 4911403a8c7SSam Leffler.Fn crypto_unblock , 492f4bf4335SSam Lefflerand 493f4bf4335SSam Leffler.Fn crypto_done 494f4bf4335SSam Lefflerroutines are used by drivers that provide support for cryptographic 495f4bf4335SSam Lefflerprimitives to register and unregister with the kernel crypto services 496f4bf4335SSam Lefflerframework. 497f4bf4335SSam LefflerDrivers must first use the 498f4bf4335SSam Leffler.Fn crypto_get_driverid 499f4bf4335SSam Lefflerfunction to acquire a driver identifier, specifying the 500f4bf4335SSam Leffler.Fa cc_flags 501f4bf4335SSam Leffleras an argument (normally 0, but software-only drivers should specify 5027621fdabSRuslan Ermilov.Dv CRYPTOCAP_F_SOFTWARE ) . 503f4bf4335SSam LefflerFor each algorithm the driver supports, it must then call 504f4bf4335SSam Leffler.Fn crypto_register . 505f4bf4335SSam LefflerThe first two arguments are the driver and algorithm identifiers. 506f4bf4335SSam LefflerThe next two arguments specify the largest possible operator length (in bits, 5071403a8c7SSam Lefflerimportant for public key operations) and flags for this algorithm. 5081403a8c7SSam LefflerThe last four arguments must be provided in the first call to 509f4bf4335SSam Leffler.Fn crypto_register 510f4bf4335SSam Lefflerand are ignored in all subsequent calls. 511f4bf4335SSam LefflerThey are pointers to three 512f4bf4335SSam Lefflerdriver-provided functions that the framework may call to establish new 513f4bf4335SSam Lefflercryptographic context with the driver, free already established 514f4bf4335SSam Lefflercontext, and ask for a request to be processed (encrypt, decrypt, 5151403a8c7SSam Leffleretc.); and an opaque parameter to pass when calling each of these routines. 516f4bf4335SSam Leffler.Fn crypto_unregister 517f4bf4335SSam Leffleris called by drivers that wish to withdraw support for an algorithm. 518f4bf4335SSam LefflerThe two arguments are the driver and algorithm identifiers, respectively. 519f4bf4335SSam LefflerTypically, drivers for 5201403a8c7SSam LefflerPCMCIA 521f4bf4335SSam Lefflercrypto cards that are being ejected will invoke this routine for all 522f4bf4335SSam Leffleralgorithms supported by the card. 5231403a8c7SSam Leffler.Fn crypto_unregister_all 5241403a8c7SSam Lefflerwill unregister all algorithms registered by a driver 525f4bf4335SSam Lefflerand the driver will be disabled (no new sessions will be allocated on 526f4bf4335SSam Lefflerthat driver, and any existing sessions will be migrated to other 527f4bf4335SSam Lefflerdrivers). 528f4bf4335SSam LefflerThe same will be done if all algorithms associated with a driver are 529f4bf4335SSam Lefflerunregistered one by one. 530f4bf4335SSam Leffler.Pp 531f4bf4335SSam LefflerThe calling convention for the three driver-supplied routines is: 5327621fdabSRuslan Ermilov.Pp 5337621fdabSRuslan Ermilov.Bl -item -compact 5347621fdabSRuslan Ermilov.It 5357621fdabSRuslan Ermilov.Ft int 5366b99842aSEd Schouten.Fn \*[lp]*newsession\*[rp] "void *" "uint32_t *" "struct cryptoini *" ; 5377621fdabSRuslan Ermilov.It 5387621fdabSRuslan Ermilov.Ft int 5396b99842aSEd Schouten.Fn \*[lp]*freesession\*[rp] "void *" "uint64_t" ; 5407621fdabSRuslan Ermilov.It 5417621fdabSRuslan Ermilov.Ft int 5427621fdabSRuslan Ermilov.Fn \*[lp]*process\*[rp] "void *" "struct cryptop *" ; 5437621fdabSRuslan Ermilov.It 5447621fdabSRuslan Ermilov.Ft int 5457621fdabSRuslan Ermilov.Fn \*[lp]*kprocess\*[rp] "void *" "struct cryptkop *" ; 5467621fdabSRuslan Ermilov.El 547f4bf4335SSam Leffler.Pp 548f4bf4335SSam LefflerOn invocation, the first argument to 5491403a8c7SSam Lefflerall routines is an opaque data value supplied when the algorithm 5501403a8c7SSam Leffleris registered with 5511403a8c7SSam Leffler.Fn crypto_register . 5521403a8c7SSam LefflerThe second argument to 553f4bf4335SSam Leffler.Fn newsession 554f4bf4335SSam Lefflercontains the driver identifier obtained via 555f4bf4335SSam Leffler.Fn crypto_get_driverid . 5567621fdabSRuslan ErmilovOn successful return, it should contain a driver-specific session 557f4bf4335SSam Leffleridentifier. 5581403a8c7SSam LefflerThe third argument is identical to that of 559f4bf4335SSam Leffler.Fn crypto_newsession . 560f4bf4335SSam Leffler.Pp 561f4bf4335SSam LefflerThe 562f4bf4335SSam Leffler.Fn freesession 5631403a8c7SSam Lefflerroutine takes as arguments the opaque data value and the SID 5641403a8c7SSam Leffler(which is the concatenation of the 565f4bf4335SSam Lefflerdriver identifier and the driver-specific session identifier). 566f4bf4335SSam LefflerIt should clear any context associated with the session (clear hardware 567f4bf4335SSam Lefflerregisters, memory, etc.). 568f4bf4335SSam Leffler.Pp 569f4bf4335SSam LefflerThe 570f4bf4335SSam Leffler.Fn process 571f4bf4335SSam Lefflerroutine is invoked with a request to perform crypto processing. 572f4bf4335SSam LefflerThis routine must not block, but should queue the request and return 573f4bf4335SSam Lefflerimmediately. 574f4bf4335SSam LefflerUpon processing the request, the callback routine should be invoked. 5751403a8c7SSam LefflerIn case of an unrecoverable error, the error indication must be placed in the 5767621fdabSRuslan Ermilov.Va crp_etype 577f4bf4335SSam Lefflerfield of the 5787621fdabSRuslan Ermilov.Vt cryptop 579f4bf4335SSam Lefflerstructure. 580f4bf4335SSam LefflerWhen the request is completed, or an error is detected, the 581f4bf4335SSam Leffler.Fn process 5821403a8c7SSam Lefflerroutine should invoke 583f4bf4335SSam Leffler.Fn crypto_done . 584f4bf4335SSam LefflerSession migration may be performed, as mentioned previously. 585f4bf4335SSam Leffler.Pp 5861403a8c7SSam LefflerIn case of a temporary resource exhaustion, the 5877621fdabSRuslan Ermilov.Fn process 5881403a8c7SSam Lefflerroutine may return 5891403a8c7SSam Leffler.Er ERESTART 5901403a8c7SSam Lefflerin which case the crypto services will requeue the request, mark the driver 5917621fdabSRuslan Ermilovas 5927621fdabSRuslan Ermilov.Dq blocked , 5937621fdabSRuslan Ermilovand stop submitting requests for processing. 5941403a8c7SSam LefflerThe driver is then responsible for notifying the crypto services 5951403a8c7SSam Lefflerwhen it is again able to process requests through the 5961403a8c7SSam Leffler.Fn crypto_unblock 5971403a8c7SSam Lefflerroutine. 5981403a8c7SSam LefflerThis simple flow control mechanism should only be used for short-lived 5991403a8c7SSam Lefflerresource exhaustion as it causes operations to be queued in the crypto 6001403a8c7SSam Lefflerlayer. 6017621fdabSRuslan ErmilovDoing so is preferable to returning an error in such cases as 6027621fdabSRuslan Ermilovit can cause network protocols to degrade performance by treating the 6031403a8c7SSam Lefflerfailure much like a lost packet. 6041403a8c7SSam Leffler.Pp 605f4bf4335SSam LefflerThe 606f4bf4335SSam Leffler.Fn kprocess 607f4bf4335SSam Lefflerroutine is invoked with a request to perform crypto key processing. 608f4bf4335SSam LefflerThis routine must not block, but should queue the request and return 609f4bf4335SSam Lefflerimmediately. 610f4bf4335SSam LefflerUpon processing the request, the callback routine should be invoked. 6111403a8c7SSam LefflerIn case of an unrecoverable error, the error indication must be placed in the 6127621fdabSRuslan Ermilov.Va krp_status 613f4bf4335SSam Lefflerfield of the 6147621fdabSRuslan Ermilov.Vt cryptkop 615f4bf4335SSam Lefflerstructure. 616f4bf4335SSam LefflerWhen the request is completed, or an error is detected, the 617f4bf4335SSam Leffler.Fn kprocess 618f4bf4335SSam Lefflerroutine should invoked 619f4bf4335SSam Leffler.Fn crypto_kdone . 620f4bf4335SSam Leffler.Sh RETURN VALUES 621f4bf4335SSam Leffler.Fn crypto_register , 622f4bf4335SSam Leffler.Fn crypto_kregister , 623f4bf4335SSam Leffler.Fn crypto_unregister , 624f4bf4335SSam Leffler.Fn crypto_newsession , 6251403a8c7SSam Leffler.Fn crypto_freesession , 626f4bf4335SSam Lefflerand 6277621fdabSRuslan Ermilov.Fn crypto_unblock 628f4bf4335SSam Lefflerreturn 0 on success, or an error code on failure. 629f4bf4335SSam Leffler.Fn crypto_get_driverid 630f4bf4335SSam Lefflerreturns a non-negative value on error, and \-1 on failure. 631f4bf4335SSam Leffler.Fn crypto_getreq 632f4bf4335SSam Lefflerreturns a pointer to a 6337621fdabSRuslan Ermilov.Vt cryptop 634f4bf4335SSam Lefflerstructure and 635f4bf4335SSam Leffler.Dv NULL 636f4bf4335SSam Leffleron failure. 637f4bf4335SSam Leffler.Fn crypto_dispatch 638f4bf4335SSam Lefflerreturns 639f4bf4335SSam Leffler.Er EINVAL 6407621fdabSRuslan Ermilovif its argument or the callback function was 641f4bf4335SSam Leffler.Dv NULL , 642f4bf4335SSam Lefflerand 0 otherwise. 643f4bf4335SSam LefflerThe callback is provided with an error code in case of failure, in the 6447621fdabSRuslan Ermilov.Va crp_etype 645f4bf4335SSam Lefflerfield. 646f4bf4335SSam Leffler.Sh FILES 647627e7962SSam Leffler.Bl -tag -width ".Pa sys/opencrypto/crypto.c" 648627e7962SSam Leffler.It Pa sys/opencrypto/crypto.c 649f4bf4335SSam Lefflermost of the framework code 650f4bf4335SSam Leffler.El 651f4bf4335SSam Leffler.Sh SEE ALSO 652c7c8edc3SJohn-Mark Gurney.Xr crypto 4 , 653f4bf4335SSam Leffler.Xr ipsec 4 , 654*08fca7a5SJohn-Mark Gurney.Xr crypto 7 , 655f4bf4335SSam Leffler.Xr malloc 9 , 6561403a8c7SSam Leffler.Xr sleep 9 657f4bf4335SSam Leffler.Sh HISTORY 658f4bf4335SSam LefflerThe cryptographic framework first appeared in 6597621fdabSRuslan Ermilov.Ox 2.7 6607621fdabSRuslan Ermilovand was written by 6618a7314fcSBaptiste Daroussin.An Angelos D. Keromytis Aq Mt angelos@openbsd.org . 662f4bf4335SSam Leffler.Sh BUGS 663f4bf4335SSam LefflerThe framework currently assumes that all the algorithms in a 664f4bf4335SSam Leffler.Fn crypto_newsession 665f4bf4335SSam Leffleroperation must be available by the same driver. 6667621fdabSRuslan ErmilovIf that is not the case, session initialization will fail. 667f4bf4335SSam Leffler.Pp 668f4bf4335SSam LefflerThe framework also needs a mechanism for determining which driver is 669f4bf4335SSam Lefflerbest for a specific set of algorithms associated with a session. 670f4bf4335SSam LefflerSome type of benchmarking is in order here. 671f4bf4335SSam Leffler.Pp 672f4bf4335SSam LefflerMultiple instances of the same algorithm in the same session are not 673f4bf4335SSam Lefflersupported. 674f4bf4335SSam LefflerNote that 3DES is considered one algorithm (and not three 675f4bf4335SSam Lefflerinstances of DES). 676f4bf4335SSam LefflerThus, 3DES and DES could be mixed in the same request. 677