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.\" 20b61e8b3eSChristian Brueffer.Dd September 19, 2007 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 297621fdabSRuslan Ermilov.Fn crypto_get_driverid u_int8_t 30f4bf4335SSam Leffler.Ft int 317621fdabSRuslan Ermilov.Fn crypto_register u_int32_t int u_int16_t u_int32_t "int \*[lp]*\*[rp]\*[lp]void *, u_int32_t *, struct cryptoini *\*[rp]" "int \*[lp]*\*[rp]\*[lp]void *, u_int64_t\*[rp]" "int \*[lp]*\*[rp]\*[lp]void *, struct cryptop *\*[rp]" "void *" 32f4bf4335SSam Leffler.Ft int 337621fdabSRuslan Ermilov.Fn crypto_kregister u_int32_t int u_int32_t "int \*[lp]*\*[rp]\*[lp]void *, struct cryptkop *\*[rp]" "void *" 34f4bf4335SSam Leffler.Ft int 357621fdabSRuslan Ermilov.Fn crypto_unregister u_int32_t int 361403a8c7SSam Leffler.Ft int 377621fdabSRuslan Ermilov.Fn crypto_unregister_all u_int32_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 437621fdabSRuslan Ermilov.Fn crypto_newsession "u_int64_t *" "struct cryptoini *" int 44f4bf4335SSam Leffler.Ft int 457621fdabSRuslan Ermilov.Fn crypto_freesession u_int64_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 517621fdabSRuslan Ermilov.Fn crypto_unblock u_int32_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; 67f4bf4335SSam Leffler u_int8_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; 86f4bf4335SSam Leffler u_int64_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 */ 111f4bf4335SSam Leffler u_int32_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 146f4bf4335SSam Lefflercallback is specified by the consumer on an 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 1787621fdabSRuslan Ermilov.It Dv CRYPTO_AES_CBC 1797621fdabSRuslan Ermilov.It Dv CRYPTO_ARC4 180b61e8b3eSChristian Brueffer.It Dv CRYPTO_BLF_CBC 181b61e8b3eSChristian Brueffer.It Dv CRYPTO_CAMELLIA_CBC 182b61e8b3eSChristian Brueffer.It Dv CRYPTO_CAST_CBC 183b61e8b3eSChristian Brueffer.It Dv CRYPTO_DES_CBC 184b61e8b3eSChristian Brueffer.It Dv CRYPTO_3DES_CBC 185b61e8b3eSChristian Brueffer.It Dv CRYPTO_SKIPJACK_CBC 1867621fdabSRuslan Ermilov.It Dv CRYPTO_MD5 187b61e8b3eSChristian Brueffer.It Dv CRYPTO_MD5_HMAC 188b61e8b3eSChristian Brueffer.It Dv CRYPTO_MD5_KPDK 189b61e8b3eSChristian Brueffer.It Dv CRYPTO_RIPEMD160_HMAC 1907621fdabSRuslan Ermilov.It Dv CRYPTO_SHA1 191b61e8b3eSChristian Brueffer.It Dv CRYPTO_SHA1_HMAC 192b61e8b3eSChristian Brueffer.It Dv CRYPTO_SHA1_KPDK 19371ee05c8SPawel Jakub Dawidek.It Dv CRYPTO_SHA2_256_HMAC 19471ee05c8SPawel Jakub Dawidek.It Dv CRYPTO_SHA2_384_HMAC 19571ee05c8SPawel Jakub Dawidek.It Dv CRYPTO_SHA2_512_HMAC 1967621fdabSRuslan Ermilov.It Dv CRYPTO_NULL_HMAC 1977621fdabSRuslan Ermilov.It Dv CRYPTO_NULL_CBC 1987621fdabSRuslan Ermilov.El 1997621fdabSRuslan Ermilov.It Va cri_klen 200f4bf4335SSam LefflerSpecifies the length of the key in bits, for variable-size key 201f4bf4335SSam Leffleralgorithms. 202f3326d72SPawel Jakub Dawidek.It Va cri_mlen 203f3326d72SPawel Jakub DawidekSpecifies how many bytes from the calculated hash should be copied back. 204f3326d72SPawel Jakub Dawidek0 means entire hash. 2057621fdabSRuslan Ermilov.It Va cri_key 206f4bf4335SSam LefflerContains the key to be used with the algorithm. 2077621fdabSRuslan Ermilov.It Va cri_iv 208f4bf4335SSam LefflerContains an explicit initialization vector (IV), if it does not prefix 209f4bf4335SSam Lefflerthe data. 210f4bf4335SSam LefflerThis field is ignored during initialization. 211f4bf4335SSam LefflerIf no IV is explicitly passed (see below on details), a random IV is used 212f4bf4335SSam Lefflerby the device driver processing the request. 2137621fdabSRuslan Ermilov.It Va cri_next 214f4bf4335SSam LefflerContains a pointer to another 2157621fdabSRuslan Ermilov.Vt cryptoini 216f4bf4335SSam Lefflerstructure. 217f4bf4335SSam LefflerMultiple such structures may be linked to establish multi-algorithm sessions 2187621fdabSRuslan Ermilov.Xr ( ipsec 4 219f4bf4335SSam Leffleris an example consumer of such a feature). 220f4bf4335SSam Leffler.El 221f4bf4335SSam Leffler.Pp 222f4bf4335SSam LefflerThe 2237621fdabSRuslan Ermilov.Vt cryptoini 224f4bf4335SSam Lefflerstructure and its contents will not be modified by the framework (or 225f4bf4335SSam Lefflerthe drivers used). 226f4bf4335SSam LefflerSubsequent requests for processing that use the 227f4bf4335SSam LefflerSID returned will avoid the cost of re-initializing the hardware (in 228f4bf4335SSam Leffleressence, SID acts as an index in the session cache of the driver). 229f4bf4335SSam Leffler.Pp 230f4bf4335SSam Leffler.Fn crypto_freesession 231f4bf4335SSam Leffleris called with the SID returned by 232f4bf4335SSam Leffler.Fn crypto_newsession 233f4bf4335SSam Lefflerto disestablish the session. 234f4bf4335SSam Leffler.Pp 235f4bf4335SSam Leffler.Fn crypto_dispatch 236f4bf4335SSam Leffleris called to process a request. 237f4bf4335SSam LefflerThe various fields in the 2387621fdabSRuslan Ermilov.Vt cryptop 239f4bf4335SSam Lefflerstructure are: 2407621fdabSRuslan Ermilov.Bl -tag -width ".Va crp_callback" 2417621fdabSRuslan Ermilov.It Va crp_sid 242f4bf4335SSam LefflerContains the SID. 2437621fdabSRuslan Ermilov.It Va crp_ilen 244f4bf4335SSam LefflerIndicates the total length in bytes of the buffer to be processed. 2457621fdabSRuslan Ermilov.It Va crp_olen 246f4bf4335SSam LefflerOn return, contains the total length of the result. 247f4bf4335SSam LefflerFor symmetric crypto operations, this will be the same as the input length. 248f4bf4335SSam LefflerThis will be used if the framework needs to allocate a new 249f4bf4335SSam Lefflerbuffer for the result (or for re-formatting the input). 2507621fdabSRuslan Ermilov.It Va crp_callback 251f4bf4335SSam LefflerThis routine is invoked upon completion of the request, whether 252f4bf4335SSam Lefflersuccessful or not. 253f4bf4335SSam LefflerIt is invoked through the 254f4bf4335SSam Leffler.Fn crypto_done 255f4bf4335SSam Lefflerroutine. 256f4bf4335SSam LefflerIf the request was not successful, an error code is set in the 2577621fdabSRuslan Ermilov.Va crp_etype 258f4bf4335SSam Lefflerfield. 259f4bf4335SSam LefflerIt is the responsibility of the callback routine to set the appropriate 260f4bf4335SSam Leffler.Xr spl 9 261f4bf4335SSam Lefflerlevel. 2627621fdabSRuslan Ermilov.It Va crp_etype 263f4bf4335SSam LefflerContains the error type, if any errors were encountered, or zero if 264f4bf4335SSam Lefflerthe request was successfully processed. 265f4bf4335SSam LefflerIf the 266f4bf4335SSam Leffler.Er EAGAIN 267f4bf4335SSam Lefflererror code is returned, the SID has changed (and has been recorded in the 2687621fdabSRuslan Ermilov.Va crp_sid 269f4bf4335SSam Lefflerfield). 270f4bf4335SSam LefflerThe consumer should record the new SID and use it in all subsequent requests. 271f4bf4335SSam LefflerIn this case, the request may be re-submitted immediately. 272f4bf4335SSam LefflerThis mechanism is used by the framework to perform 273f4bf4335SSam Lefflersession migration (move a session from one driver to another, because 274f4bf4335SSam Lefflerof availability, performance, or other considerations). 275f4bf4335SSam Leffler.Pp 276f4bf4335SSam LefflerNote that this field only makes sense when examined by 277f4bf4335SSam Lefflerthe callback routine specified in 2787621fdabSRuslan Ermilov.Va crp_callback . 279f4bf4335SSam LefflerErrors are returned to the invoker of 280f4bf4335SSam Leffler.Fn crypto_process 281f4bf4335SSam Leffleronly when enough information is not present to call the callback 282f4bf4335SSam Lefflerroutine (i.e., if the pointer passed is 283f4bf4335SSam Leffler.Dv NULL 284f4bf4335SSam Leffleror if no callback routine was specified). 2857621fdabSRuslan Ermilov.It Va crp_flags 286f4bf4335SSam LefflerIs a bitmask of flags associated with this request. 287f4bf4335SSam LefflerCurrently defined flags are: 288265803d0SPawel Jakub Dawidek.Bl -tag -width ".Dv CRYPTO_F_CBIFSYNC" 289f4bf4335SSam Leffler.It Dv CRYPTO_F_IMBUF 290f4bf4335SSam LefflerThe buffer pointed to by 2917621fdabSRuslan Ermilov.Va crp_buf 292f4bf4335SSam Leffleris an mbuf chain. 293265803d0SPawel Jakub Dawidek.It Dv CRYPTO_F_IOV 294265803d0SPawel Jakub DawidekThe buffer pointed to by 295265803d0SPawel Jakub Dawidek.Va crp_buf 29681ae4b8dSRuslan Ermilovis an 29781ae4b8dSRuslan Ermilov.Vt uio 29881ae4b8dSRuslan Ermilovstructure. 299265803d0SPawel Jakub Dawidek.It Dv CRYPTO_F_REL 300265803d0SPawel Jakub DawidekMust return data in the same place. 301265803d0SPawel Jakub Dawidek.It Dv CRYPTO_F_BATCH 302265803d0SPawel Jakub DawidekBatch operation if possible. 303265803d0SPawel Jakub Dawidek.It Dv CRYPTO_F_CBIMM 3049280e5faSMike PritchardDo callback immediately instead of doing it from a dedicated kernel thread. 305265803d0SPawel Jakub Dawidek.It Dv CRYPTO_F_DONE 306265803d0SPawel Jakub DawidekOperation completed. 307265803d0SPawel Jakub Dawidek.It Dv CRYPTO_F_CBIFSYNC 3089280e5faSMike PritchardDo callback immediately if operation is synchronous. 309f4bf4335SSam Leffler.El 3107621fdabSRuslan Ermilov.It Va crp_buf 311f4bf4335SSam LefflerPoints to the input buffer. 312f4bf4335SSam LefflerOn return (when the callback is invoked), 313f4bf4335SSam Lefflerit contains the result of the request. 314f4bf4335SSam LefflerThe input buffer may be an mbuf 3151403a8c7SSam Lefflerchain or a contiguous buffer, 316f4bf4335SSam Lefflerdepending on 3177621fdabSRuslan Ermilov.Va crp_flags . 3187621fdabSRuslan Ermilov.It Va crp_opaque 319f4bf4335SSam LefflerThis is passed through the crypto framework untouched and is 320f4bf4335SSam Lefflerintended for the invoking application's use. 3217621fdabSRuslan Ermilov.It Va crp_desc 322f4bf4335SSam LefflerThis is a linked list of descriptors. 323f4bf4335SSam LefflerEach descriptor provides 324f4bf4335SSam Lefflerinformation about what type of cryptographic operation should be done 325f4bf4335SSam Leffleron the input buffer. 326f4bf4335SSam LefflerThe various fields are: 3277621fdabSRuslan Ermilov.Bl -tag -width ".Va crd_inject" 3283b72821fSPawel Jakub Dawidek.It Va crd_iv 3293b72821fSPawel Jakub DawidekThe field where IV should be provided when the 3303b72821fSPawel Jakub Dawidek.Dv CRD_F_IV_EXPLICIT 3313b72821fSPawel Jakub Dawidekflag is given. 3323b72821fSPawel Jakub Dawidek.It Va crd_key 3333b72821fSPawel Jakub DawidekWhen the 3343b72821fSPawel Jakub Dawidek.Dv CRD_F_KEY_EXPLICIT 3353b72821fSPawel Jakub Dawidekflag is given, the 3363b72821fSPawel Jakub Dawidek.Va crd_key 3373b72821fSPawel Jakub Dawidekpoints to a buffer with encryption or authentication key. 3383b72821fSPawel Jakub Dawidek.It Va crd_alg 3393b72821fSPawel Jakub DawidekAn algorithm to use. 3403b72821fSPawel Jakub DawidekMust be the same as the one given at newsession time. 3413b72821fSPawel Jakub Dawidek.It Va crd_klen 3423b72821fSPawel Jakub DawidekThe 3433b72821fSPawel Jakub Dawidek.Va crd_key 3443b72821fSPawel Jakub Dawidekkey length. 3457621fdabSRuslan Ermilov.It Va crd_skip 346f4bf4335SSam LefflerThe offset in the input buffer where processing should start. 3477621fdabSRuslan Ermilov.It Va crd_len 348f4bf4335SSam LefflerHow many bytes, after 3497621fdabSRuslan Ermilov.Va crd_skip , 350f4bf4335SSam Lefflershould be processed. 3517621fdabSRuslan Ermilov.It Va crd_inject 352f4bf4335SSam LefflerOffset from the beginning of the buffer to insert any results. 353f4bf4335SSam LefflerFor encryption algorithms, this is where the initialization vector 354f4bf4335SSam Leffler(IV) will be inserted when encrypting or where it can be found when 355f4bf4335SSam Lefflerdecrypting (subject to 3567621fdabSRuslan Ermilov.Va crd_flags ) . 357f4bf4335SSam LefflerFor MAC algorithms, this is where the result of the keyed hash will be 358f4bf4335SSam Lefflerinserted. 3597621fdabSRuslan Ermilov.It Va crd_flags 360f4bf4335SSam LefflerThe following flags are defined: 36181ae4b8dSRuslan Ermilov.Bl -tag -width 3n 362f4bf4335SSam Leffler.It Dv CRD_F_ENCRYPT 363f4bf4335SSam LefflerFor encryption algorithms, this bit is set when encryption is required 364f4bf4335SSam Leffler(when not set, decryption is performed). 365f4bf4335SSam Leffler.It Dv CRD_F_IV_PRESENT 366f4bf4335SSam LefflerFor encryption algorithms, this bit is set when the IV already 367f4bf4335SSam Lefflerprecedes the data, so the 3687621fdabSRuslan Ermilov.Va crd_inject 369f4bf4335SSam Lefflervalue will be ignored and no IV will be written in the buffer. 370f4bf4335SSam LefflerOtherwise, the IV used to encrypt the packet will be written 371f4bf4335SSam Lefflerat the location pointed to by 3727621fdabSRuslan Ermilov.Va crd_inject . 373f4bf4335SSam LefflerThe IV length is assumed to be equal to the blocksize of the 374f4bf4335SSam Lefflerencryption algorithm. 375f4bf4335SSam LefflerSome applications that do special 3767621fdabSRuslan Ermilov.Dq "IV cooking" , 377f4bf4335SSam Lefflersuch as the half-IV mode in 378f4bf4335SSam Leffler.Xr ipsec 4 , 379f4bf4335SSam Lefflercan use this flag to indicate that the IV should not be written on the packet. 380f4bf4335SSam LefflerThis flag is typically used in conjunction with the 381f4bf4335SSam Leffler.Dv CRD_F_IV_EXPLICIT 382f4bf4335SSam Lefflerflag. 383f4bf4335SSam Leffler.It Dv CRD_F_IV_EXPLICIT 384f4bf4335SSam LefflerFor encryption algorithms, this bit is set when the IV is explicitly 385f4bf4335SSam Lefflerprovided by the consumer in the 3863b72821fSPawel Jakub Dawidek.Va crd_iv 3871deaec43SPawel Jakub Dawidekfield. 388f4bf4335SSam LefflerOtherwise, for encryption operations the IV is provided for by 389f4bf4335SSam Lefflerthe driver used to perform the operation, whereas for decryption 390f4bf4335SSam Leffleroperations it is pointed to by the 3917621fdabSRuslan Ermilov.Va crd_inject 392f4bf4335SSam Lefflerfield. 393f4bf4335SSam LefflerThis flag is typically used when the IV is calculated 3947621fdabSRuslan Ermilov.Dq "on the fly" 395f4bf4335SSam Lefflerby the consumer, and does not precede the data (some 396f4bf4335SSam Leffler.Xr ipsec 4 397f4bf4335SSam Lefflerconfigurations, and the encrypted swap are two such examples). 3981deaec43SPawel Jakub Dawidek.It Dv CRD_F_KEY_EXPLICIT 3993b72821fSPawel Jakub DawidekFor encryption and authentication (MAC) algorithms, this bit is set when the key 4003b72821fSPawel Jakub Dawidekis explicitly provided by the consumer in the 4013b72821fSPawel Jakub Dawidek.Va crd_key 4021deaec43SPawel Jakub Dawidekfield for the given operation. 4033b72821fSPawel Jakub DawidekOtherwise, the key is taken at newsession time from the 4043b72821fSPawel Jakub Dawidek.Va cri_key 4053b72821fSPawel Jakub Dawidekfield. 406f4bf4335SSam Leffler.It Dv CRD_F_COMP 407f4bf4335SSam LefflerFor compression algorithms, this bit is set when compression is required (when 408f4bf4335SSam Lefflernot set, decompression is performed). 409f4bf4335SSam Leffler.El 4107621fdabSRuslan Ermilov.It Va CRD_INI 411f4bf4335SSam LefflerThis 4127621fdabSRuslan Ermilov.Vt cryptoini 413f4bf4335SSam Lefflerstructure will not be modified by the framework or the device drivers. 414f4bf4335SSam LefflerSince this information accompanies every cryptographic 415f4bf4335SSam Leffleroperation request, drivers may re-initialize state on-demand 416f4bf4335SSam Leffler(typically an expensive operation). 417f4bf4335SSam LefflerFurthermore, the cryptographic 418f4bf4335SSam Lefflerframework may re-route requests as a result of full queues or hardware 419f4bf4335SSam Lefflerfailure, as described above. 4207621fdabSRuslan Ermilov.It Va crd_next 421f4bf4335SSam LefflerPoint to the next descriptor. 422f4bf4335SSam LefflerLinked operations are useful in protocols such as 423f4bf4335SSam Leffler.Xr ipsec 4 , 424f4bf4335SSam Lefflerwhere multiple cryptographic transforms may be applied on the same 425f4bf4335SSam Lefflerblock of data. 426f4bf4335SSam Leffler.El 427f4bf4335SSam Leffler.El 428f4bf4335SSam Leffler.Pp 429f4bf4335SSam Leffler.Fn crypto_getreq 430f4bf4335SSam Lefflerallocates a 4317621fdabSRuslan Ermilov.Vt cryptop 432f4bf4335SSam Lefflerstructure with a linked list of as many 4337621fdabSRuslan Ermilov.Vt cryptodesc 434f4bf4335SSam Lefflerstructures as were specified in the argument passed to it. 435f4bf4335SSam Leffler.Pp 436f4bf4335SSam Leffler.Fn crypto_freereq 437f4bf4335SSam Lefflerdeallocates a structure 4387621fdabSRuslan Ermilov.Vt cryptop 439f4bf4335SSam Lefflerand any 4407621fdabSRuslan Ermilov.Vt cryptodesc 441f4bf4335SSam Lefflerstructures linked to it. 442f4bf4335SSam LefflerNote that it is the responsibility of the 443f4bf4335SSam Lefflercallback routine to do the necessary cleanups associated with the 444f4bf4335SSam Leffleropaque field in the 4457621fdabSRuslan Ermilov.Vt cryptop 446f4bf4335SSam Lefflerstructure. 447f4bf4335SSam Leffler.Pp 448f4bf4335SSam Leffler.Fn crypto_kdispatch 449f4bf4335SSam Leffleris called to perform a keying operation. 450f4bf4335SSam LefflerThe various fields in the 4517621fdabSRuslan Ermilov.Vt cryptkop 452f4bf4335SSam Lefflerstructure are: 4537621fdabSRuslan Ermilov.Bl -tag -width ".Va krp_callback' 4547621fdabSRuslan Ermilov.It Va krp_op 4557621fdabSRuslan ErmilovOperation code, such as 4567621fdabSRuslan Ermilov.Dv CRK_MOD_EXP . 4577621fdabSRuslan Ermilov.It Va krp_status 458f4bf4335SSam LefflerReturn code. 4597621fdabSRuslan ErmilovThis 4607621fdabSRuslan Ermilov.Va errno Ns -style 4617621fdabSRuslan Ermilovvariable indicates whether lower level reasons 462f4bf4335SSam Lefflerfor operation failure. 4637621fdabSRuslan Ermilov.It Va krp_iparams 464f4bf4335SSam LefflerNumber if input parameters to the specified operation. 465f4bf4335SSam LefflerNote that each operation has a (typically hardwired) number of such parameters. 4667621fdabSRuslan Ermilov.It Va krp_oparams 467f4bf4335SSam LefflerNumber if output parameters from the specified operation. 468f4bf4335SSam LefflerNote that each operation has a (typically hardwired) number of such parameters. 4697621fdabSRuslan Ermilov.It Va krp_kvp 470f4bf4335SSam LefflerAn array of kernel memory blocks containing the parameters. 4717621fdabSRuslan Ermilov.It Va krp_hid 472f4bf4335SSam LefflerIdentifier specifying which low-level driver is being used. 4737621fdabSRuslan Ermilov.It Va krp_callback 474f4bf4335SSam LefflerCallback called on completion of a keying operation. 475f4bf4335SSam Leffler.El 476f4bf4335SSam Leffler.Sh DRIVER-SIDE API 477f4bf4335SSam LefflerThe 478f4bf4335SSam Leffler.Fn crypto_get_driverid , 479f4bf4335SSam Leffler.Fn crypto_register , 480f4bf4335SSam Leffler.Fn crypto_kregister , 481f4bf4335SSam Leffler.Fn crypto_unregister , 4821403a8c7SSam Leffler.Fn crypto_unblock , 483f4bf4335SSam Lefflerand 484f4bf4335SSam Leffler.Fn crypto_done 485f4bf4335SSam Lefflerroutines are used by drivers that provide support for cryptographic 486f4bf4335SSam Lefflerprimitives to register and unregister with the kernel crypto services 487f4bf4335SSam Lefflerframework. 488f4bf4335SSam LefflerDrivers must first use the 489f4bf4335SSam Leffler.Fn crypto_get_driverid 490f4bf4335SSam Lefflerfunction to acquire a driver identifier, specifying the 491f4bf4335SSam Leffler.Fa cc_flags 492f4bf4335SSam Leffleras an argument (normally 0, but software-only drivers should specify 4937621fdabSRuslan Ermilov.Dv CRYPTOCAP_F_SOFTWARE ) . 494f4bf4335SSam LefflerFor each algorithm the driver supports, it must then call 495f4bf4335SSam Leffler.Fn crypto_register . 496f4bf4335SSam LefflerThe first two arguments are the driver and algorithm identifiers. 497f4bf4335SSam LefflerThe next two arguments specify the largest possible operator length (in bits, 4981403a8c7SSam Lefflerimportant for public key operations) and flags for this algorithm. 4991403a8c7SSam LefflerThe last four arguments must be provided in the first call to 500f4bf4335SSam Leffler.Fn crypto_register 501f4bf4335SSam Lefflerand are ignored in all subsequent calls. 502f4bf4335SSam LefflerThey are pointers to three 503f4bf4335SSam Lefflerdriver-provided functions that the framework may call to establish new 504f4bf4335SSam Lefflercryptographic context with the driver, free already established 505f4bf4335SSam Lefflercontext, and ask for a request to be processed (encrypt, decrypt, 5061403a8c7SSam Leffleretc.); and an opaque parameter to pass when calling each of these routines. 507f4bf4335SSam Leffler.Fn crypto_unregister 508f4bf4335SSam Leffleris called by drivers that wish to withdraw support for an algorithm. 509f4bf4335SSam LefflerThe two arguments are the driver and algorithm identifiers, respectively. 510f4bf4335SSam LefflerTypically, drivers for 5111403a8c7SSam LefflerPCMCIA 512f4bf4335SSam Lefflercrypto cards that are being ejected will invoke this routine for all 513f4bf4335SSam Leffleralgorithms supported by the card. 5141403a8c7SSam Leffler.Fn crypto_unregister_all 5151403a8c7SSam Lefflerwill unregister all algorithms registered by a driver 516f4bf4335SSam Lefflerand the driver will be disabled (no new sessions will be allocated on 517f4bf4335SSam Lefflerthat driver, and any existing sessions will be migrated to other 518f4bf4335SSam Lefflerdrivers). 519f4bf4335SSam LefflerThe same will be done if all algorithms associated with a driver are 520f4bf4335SSam Lefflerunregistered one by one. 521f4bf4335SSam Leffler.Pp 522f4bf4335SSam LefflerThe calling convention for the three driver-supplied routines is: 5237621fdabSRuslan Ermilov.Pp 5247621fdabSRuslan Ermilov.Bl -item -compact 5257621fdabSRuslan Ermilov.It 5267621fdabSRuslan Ermilov.Ft int 5277621fdabSRuslan Ermilov.Fn \*[lp]*newsession\*[rp] "void *" "u_int32_t *" "struct cryptoini *" ; 5287621fdabSRuslan Ermilov.It 5297621fdabSRuslan Ermilov.Ft int 5307621fdabSRuslan Ermilov.Fn \*[lp]*freesession\*[rp] "void *" "u_int64_t" ; 5317621fdabSRuslan Ermilov.It 5327621fdabSRuslan Ermilov.Ft int 5337621fdabSRuslan Ermilov.Fn \*[lp]*process\*[rp] "void *" "struct cryptop *" ; 5347621fdabSRuslan Ermilov.It 5357621fdabSRuslan Ermilov.Ft int 5367621fdabSRuslan Ermilov.Fn \*[lp]*kprocess\*[rp] "void *" "struct cryptkop *" ; 5377621fdabSRuslan Ermilov.El 538f4bf4335SSam Leffler.Pp 539f4bf4335SSam LefflerOn invocation, the first argument to 5401403a8c7SSam Lefflerall routines is an opaque data value supplied when the algorithm 5411403a8c7SSam Leffleris registered with 5421403a8c7SSam Leffler.Fn crypto_register . 5431403a8c7SSam LefflerThe second argument to 544f4bf4335SSam Leffler.Fn newsession 545f4bf4335SSam Lefflercontains the driver identifier obtained via 546f4bf4335SSam Leffler.Fn crypto_get_driverid . 5477621fdabSRuslan ErmilovOn successful return, it should contain a driver-specific session 548f4bf4335SSam Leffleridentifier. 5491403a8c7SSam LefflerThe third argument is identical to that of 550f4bf4335SSam Leffler.Fn crypto_newsession . 551f4bf4335SSam Leffler.Pp 552f4bf4335SSam LefflerThe 553f4bf4335SSam Leffler.Fn freesession 5541403a8c7SSam Lefflerroutine takes as arguments the opaque data value and the SID 5551403a8c7SSam Leffler(which is the concatenation of the 556f4bf4335SSam Lefflerdriver identifier and the driver-specific session identifier). 557f4bf4335SSam LefflerIt should clear any context associated with the session (clear hardware 558f4bf4335SSam Lefflerregisters, memory, etc.). 559f4bf4335SSam Leffler.Pp 560f4bf4335SSam LefflerThe 561f4bf4335SSam Leffler.Fn process 562f4bf4335SSam Lefflerroutine is invoked with a request to perform crypto processing. 563f4bf4335SSam LefflerThis routine must not block, but should queue the request and return 564f4bf4335SSam Lefflerimmediately. 565f4bf4335SSam LefflerUpon processing the request, the callback routine should be invoked. 5661403a8c7SSam LefflerIn case of an unrecoverable error, the error indication must be placed in the 5677621fdabSRuslan Ermilov.Va crp_etype 568f4bf4335SSam Lefflerfield of the 5697621fdabSRuslan Ermilov.Vt cryptop 570f4bf4335SSam Lefflerstructure. 571f4bf4335SSam LefflerWhen the request is completed, or an error is detected, the 572f4bf4335SSam Leffler.Fn process 5731403a8c7SSam Lefflerroutine should invoke 574f4bf4335SSam Leffler.Fn crypto_done . 575f4bf4335SSam LefflerSession migration may be performed, as mentioned previously. 576f4bf4335SSam Leffler.Pp 5771403a8c7SSam LefflerIn case of a temporary resource exhaustion, the 5787621fdabSRuslan Ermilov.Fn process 5791403a8c7SSam Lefflerroutine may return 5801403a8c7SSam Leffler.Er ERESTART 5811403a8c7SSam Lefflerin which case the crypto services will requeue the request, mark the driver 5827621fdabSRuslan Ermilovas 5837621fdabSRuslan Ermilov.Dq blocked , 5847621fdabSRuslan Ermilovand stop submitting requests for processing. 5851403a8c7SSam LefflerThe driver is then responsible for notifying the crypto services 5861403a8c7SSam Lefflerwhen it is again able to process requests through the 5871403a8c7SSam Leffler.Fn crypto_unblock 5881403a8c7SSam Lefflerroutine. 5891403a8c7SSam LefflerThis simple flow control mechanism should only be used for short-lived 5901403a8c7SSam Lefflerresource exhaustion as it causes operations to be queued in the crypto 5911403a8c7SSam Lefflerlayer. 5927621fdabSRuslan ErmilovDoing so is preferable to returning an error in such cases as 5937621fdabSRuslan Ermilovit can cause network protocols to degrade performance by treating the 5941403a8c7SSam Lefflerfailure much like a lost packet. 5951403a8c7SSam Leffler.Pp 596f4bf4335SSam LefflerThe 597f4bf4335SSam Leffler.Fn kprocess 598f4bf4335SSam Lefflerroutine is invoked with a request to perform crypto key processing. 599f4bf4335SSam LefflerThis routine must not block, but should queue the request and return 600f4bf4335SSam Lefflerimmediately. 601f4bf4335SSam LefflerUpon processing the request, the callback routine should be invoked. 6021403a8c7SSam LefflerIn case of an unrecoverable error, the error indication must be placed in the 6037621fdabSRuslan Ermilov.Va krp_status 604f4bf4335SSam Lefflerfield of the 6057621fdabSRuslan Ermilov.Vt cryptkop 606f4bf4335SSam Lefflerstructure. 607f4bf4335SSam LefflerWhen the request is completed, or an error is detected, the 608f4bf4335SSam Leffler.Fn kprocess 609f4bf4335SSam Lefflerroutine should invoked 610f4bf4335SSam Leffler.Fn crypto_kdone . 611f4bf4335SSam Leffler.Sh RETURN VALUES 612f4bf4335SSam Leffler.Fn crypto_register , 613f4bf4335SSam Leffler.Fn crypto_kregister , 614f4bf4335SSam Leffler.Fn crypto_unregister , 615f4bf4335SSam Leffler.Fn crypto_newsession , 6161403a8c7SSam Leffler.Fn crypto_freesession , 617f4bf4335SSam Lefflerand 6187621fdabSRuslan Ermilov.Fn crypto_unblock 619f4bf4335SSam Lefflerreturn 0 on success, or an error code on failure. 620f4bf4335SSam Leffler.Fn crypto_get_driverid 621f4bf4335SSam Lefflerreturns a non-negative value on error, and \-1 on failure. 622f4bf4335SSam Leffler.Fn crypto_getreq 623f4bf4335SSam Lefflerreturns a pointer to a 6247621fdabSRuslan Ermilov.Vt cryptop 625f4bf4335SSam Lefflerstructure and 626f4bf4335SSam Leffler.Dv NULL 627f4bf4335SSam Leffleron failure. 628f4bf4335SSam Leffler.Fn crypto_dispatch 629f4bf4335SSam Lefflerreturns 630f4bf4335SSam Leffler.Er EINVAL 6317621fdabSRuslan Ermilovif its argument or the callback function was 632f4bf4335SSam Leffler.Dv NULL , 633f4bf4335SSam Lefflerand 0 otherwise. 634f4bf4335SSam LefflerThe callback is provided with an error code in case of failure, in the 6357621fdabSRuslan Ermilov.Va crp_etype 636f4bf4335SSam Lefflerfield. 637f4bf4335SSam Leffler.Sh FILES 638627e7962SSam Leffler.Bl -tag -width ".Pa sys/opencrypto/crypto.c" 639627e7962SSam Leffler.It Pa sys/opencrypto/crypto.c 640f4bf4335SSam Lefflermost of the framework code 641f4bf4335SSam Leffler.El 642f4bf4335SSam Leffler.Sh SEE ALSO 643f4bf4335SSam Leffler.Xr ipsec 4 , 644f4bf4335SSam Leffler.Xr malloc 9 , 6451403a8c7SSam Leffler.Xr sleep 9 646f4bf4335SSam Leffler.Sh HISTORY 647f4bf4335SSam LefflerThe cryptographic framework first appeared in 6487621fdabSRuslan Ermilov.Ox 2.7 6497621fdabSRuslan Ermilovand was written by 6507621fdabSRuslan Ermilov.An "Angelos D. Keromytis" Aq angelos@openbsd.org . 651f4bf4335SSam Leffler.Sh BUGS 652f4bf4335SSam LefflerThe framework currently assumes that all the algorithms in a 653f4bf4335SSam Leffler.Fn crypto_newsession 654f4bf4335SSam Leffleroperation must be available by the same driver. 6557621fdabSRuslan ErmilovIf that is not the case, session initialization will fail. 656f4bf4335SSam Leffler.Pp 657f4bf4335SSam LefflerThe framework also needs a mechanism for determining which driver is 658f4bf4335SSam Lefflerbest for a specific set of algorithms associated with a session. 659f4bf4335SSam LefflerSome type of benchmarking is in order here. 660f4bf4335SSam Leffler.Pp 661f4bf4335SSam LefflerMultiple instances of the same algorithm in the same session are not 662f4bf4335SSam Lefflersupported. 663f4bf4335SSam LefflerNote that 3DES is considered one algorithm (and not three 664f4bf4335SSam Lefflerinstances of DES). 665f4bf4335SSam LefflerThus, 3DES and DES could be mixed in the same request. 666