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*f405d8ebSJohn-Mark Gurney.Dd July 10, 2015 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 2994b59118SJohn-Mark Gurney.Fn crypto_get_driverid device_t int 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 43*f405d8ebSJohn-Mark Gurney.Fn crypto_find_driver "const char *" 44*f405d8ebSJohn-Mark Gurney.Ft int 456b99842aSEd Schouten.Fn crypto_newsession "uint64_t *" "struct cryptoini *" int 46f4bf4335SSam Leffler.Ft int 476b99842aSEd Schouten.Fn crypto_freesession uint64_t 48f4bf4335SSam Leffler.Ft int 49f4bf4335SSam Leffler.Fn crypto_dispatch "struct cryptop *" 50f4bf4335SSam Leffler.Ft int 51f4bf4335SSam Leffler.Fn crypto_kdispatch "struct cryptkop *" 521403a8c7SSam Leffler.Ft int 536b99842aSEd Schouten.Fn crypto_unblock uint32_t int 547621fdabSRuslan Ermilov.Ft "struct cryptop *" 557621fdabSRuslan Ermilov.Fn crypto_getreq int 56f4bf4335SSam Leffler.Ft void 577621fdabSRuslan Ermilov.Fn crypto_freereq void 58f4bf4335SSam Leffler.Bd -literal 591403a8c7SSam Leffler#define CRYPTO_SYMQ 0x1 601403a8c7SSam Leffler#define CRYPTO_ASYMQ 0x2 611403a8c7SSam Leffler 62f4bf4335SSam Leffler#define EALG_MAX_BLOCK_LEN 16 63f4bf4335SSam Leffler 64f4bf4335SSam Lefflerstruct cryptoini { 65f4bf4335SSam Leffler int cri_alg; 66f4bf4335SSam Leffler int cri_klen; 67f3326d72SPawel Jakub Dawidek int cri_mlen; 68f4bf4335SSam Leffler caddr_t cri_key; 696b99842aSEd Schouten uint8_t cri_iv[EALG_MAX_BLOCK_LEN]; 70f4bf4335SSam Leffler struct cryptoini *cri_next; 71f4bf4335SSam Leffler}; 72f4bf4335SSam Leffler 73f4bf4335SSam Lefflerstruct cryptodesc { 74f4bf4335SSam Leffler int crd_skip; 75f4bf4335SSam Leffler int crd_len; 76f4bf4335SSam Leffler int crd_inject; 77f4bf4335SSam Leffler int crd_flags; 78f4bf4335SSam Leffler struct cryptoini CRD_INI; 793b72821fSPawel Jakub Dawidek#define crd_iv CRD_INI.cri_iv 803b72821fSPawel Jakub Dawidek#define crd_key CRD_INI.cri_key 813b72821fSPawel Jakub Dawidek#define crd_alg CRD_INI.cri_alg 823b72821fSPawel Jakub Dawidek#define crd_klen CRD_INI.cri_klen 83f4bf4335SSam Leffler struct cryptodesc *crd_next; 84f4bf4335SSam Leffler}; 85f4bf4335SSam Leffler 86f4bf4335SSam Lefflerstruct cryptop { 871403a8c7SSam Leffler TAILQ_ENTRY(cryptop) crp_next; 886b99842aSEd Schouten uint64_t crp_sid; 89f4bf4335SSam Leffler int crp_ilen; 90f4bf4335SSam Leffler int crp_olen; 91f4bf4335SSam Leffler int crp_etype; 92f4bf4335SSam Leffler int crp_flags; 93f4bf4335SSam Leffler caddr_t crp_buf; 94f4bf4335SSam Leffler caddr_t crp_opaque; 95f4bf4335SSam Leffler struct cryptodesc *crp_desc; 96f4bf4335SSam Leffler int (*crp_callback) (struct cryptop *); 97f4bf4335SSam Leffler caddr_t crp_mac; 98f4bf4335SSam Leffler}; 99f4bf4335SSam Leffler 100f4bf4335SSam Lefflerstruct crparam { 101f4bf4335SSam Leffler caddr_t crp_p; 102f4bf4335SSam Leffler u_int crp_nbits; 103f4bf4335SSam Leffler}; 104f4bf4335SSam Leffler 105f4bf4335SSam Leffler#define CRK_MAXPARAM 8 106f4bf4335SSam Leffler 107f4bf4335SSam Lefflerstruct cryptkop { 1081403a8c7SSam Leffler TAILQ_ENTRY(cryptkop) krp_next; 109f4bf4335SSam Leffler u_int krp_op; /* ie. CRK_MOD_EXP or other */ 110f4bf4335SSam Leffler u_int krp_status; /* return status */ 111f4bf4335SSam Leffler u_short krp_iparams; /* # of input parameters */ 112f4bf4335SSam Leffler u_short krp_oparams; /* # of output parameters */ 1136b99842aSEd Schouten uint32_t krp_hid; 1141403a8c7SSam Leffler struct crparam krp_param[CRK_MAXPARAM]; 115f4bf4335SSam Leffler int (*krp_callback)(struct cryptkop *); 116f4bf4335SSam Leffler}; 117f4bf4335SSam Leffler.Ed 118f4bf4335SSam Leffler.Sh DESCRIPTION 119f4bf4335SSam Leffler.Nm 120f4bf4335SSam Leffleris a framework for drivers of cryptographic hardware to register with 121f4bf4335SSam Lefflerthe kernel so 122f4bf4335SSam Leffler.Dq consumers 1231403a8c7SSam Leffler(other kernel subsystems, and 1241403a8c7SSam Lefflerusers through the 1251403a8c7SSam Leffler.Pa /dev/crypto 1261403a8c7SSam Lefflerdevice) are able to make use of it. 127f4bf4335SSam LefflerDrivers register with the framework the algorithms they support, 128f4bf4335SSam Lefflerand provide entry points (functions) the framework may call to 129f4bf4335SSam Lefflerestablish, use, and tear down sessions. 130f4bf4335SSam LefflerSessions are used to cache cryptographic information in a particular driver 131f4bf4335SSam Leffler(or associated hardware), so initialization is not needed with every request. 132f4bf4335SSam LefflerConsumers of cryptographic services pass a set of 133f4bf4335SSam Lefflerdescriptors that instruct the framework (and the drivers registered 134f4bf4335SSam Lefflerwith it) of the operations that should be applied on the data (more 135f4bf4335SSam Lefflerthan one cryptographic operation can be requested). 136f4bf4335SSam Leffler.Pp 137f4bf4335SSam LefflerKeying operations are supported as well. 138f4bf4335SSam LefflerUnlike the symmetric operators described above, 139f4bf4335SSam Lefflerthese sessionless commands perform mathematical operations using 140f4bf4335SSam Lefflerinput and output parameters. 141f4bf4335SSam Leffler.Pp 142f4bf4335SSam LefflerSince the consumers may not be associated with a process, drivers may 1431403a8c7SSam Lefflernot 1441403a8c7SSam Leffler.Xr sleep 9 . 145f4bf4335SSam LefflerThe same holds for the framework. 146f4bf4335SSam LefflerThus, a callback mechanism is used 147f4bf4335SSam Lefflerto notify a consumer that a request has been completed (the 1480f7e2491SChristian Brueffercallback is specified by the consumer on a per-request basis). 149f4bf4335SSam LefflerThe callback is invoked by the framework whether the request was 150f4bf4335SSam Lefflersuccessfully completed or not. 151f4bf4335SSam LefflerAn error indication is provided in the latter case. 152f4bf4335SSam LefflerA specific error code, 153f4bf4335SSam Leffler.Er EAGAIN , 154f4bf4335SSam Leffleris used to indicate that a session number has changed and that the 155f4bf4335SSam Lefflerrequest may be re-submitted immediately with the new session number. 156f4bf4335SSam LefflerErrors are only returned to the invoking function if not 157f4bf4335SSam Lefflerenough information to call the callback is available (meaning, there 158f4bf4335SSam Lefflerwas a fatal error in verifying the arguments). 159f4bf4335SSam LefflerFor session initialization and teardown there is no callback mechanism used. 160f4bf4335SSam Leffler.Pp 161f4bf4335SSam LefflerThe 162*f405d8ebSJohn-Mark Gurney.Fn crypto_find_driver 163*f405d8ebSJohn-Mark Gurneyfunction may be called to return the specific id of the provided name. 164*f405d8ebSJohn-Mark GurneyIf the specified driver could not be found, the returned id is -1. 165*f405d8ebSJohn-Mark Gurney.Pp 166*f405d8ebSJohn-Mark GurneyThe 167f4bf4335SSam Leffler.Fn crypto_newsession 168f4bf4335SSam Lefflerroutine is called by consumers of cryptographic services (such as the 169f4bf4335SSam Leffler.Xr ipsec 4 170f4bf4335SSam Lefflerstack) that wish to establish a new session with the framework. 171f4bf4335SSam LefflerThe second argument contains all the necessary information for 172f4bf4335SSam Lefflerthe driver to establish the session. 173*f405d8ebSJohn-Mark GurneyThe third argument is either a specific driver id, or one or both 174*f405d8ebSJohn-Mark Gurneyof 175*f405d8ebSJohn-Mark Gurney.Dv CRYPTOCAP_F_HARDWARE , 176*f405d8ebSJohn-Mark Gurneyto select hardware devices, 177*f405d8ebSJohn-Mark Gurneyor 178*f405d8ebSJohn-Mark Gurney.Dv CRYPTOCAP_F_SOFTWARE , 179*f405d8ebSJohn-Mark Gurneyto select software devices. 180*f405d8ebSJohn-Mark GurneyIf both are specified, a hardware device will be returned 181*f405d8ebSJohn-Mark Gurneybefore a software device will be. 182*f405d8ebSJohn-Mark GurneyOn success, the value pointed to by the first argument will be the 183*f405d8ebSJohn-Mark GurneySession IDentifier (SID). 184f4bf4335SSam LefflerThe various fields in the 1857621fdabSRuslan Ermilov.Vt cryptoini 186f4bf4335SSam Lefflerstructure are: 1877621fdabSRuslan Ermilov.Bl -tag -width ".Va cri_next" 1887621fdabSRuslan Ermilov.It Va cri_alg 189f4bf4335SSam LefflerContains an algorithm identifier. 190f4bf4335SSam LefflerCurrently supported algorithms are: 191f4bf4335SSam Leffler.Pp 1927621fdabSRuslan Ermilov.Bl -tag -width ".Dv CRYPTO_RIPEMD160_HMAC" -compact 19308fca7a5SJohn-Mark Gurney.It Dv CRYPTO_AES_128_NIST_GMAC 19408fca7a5SJohn-Mark Gurney.It Dv CRYPTO_AES_192_NIST_GMAC 19508fca7a5SJohn-Mark Gurney.It Dv CRYPTO_AES_256_NIST_GMAC 1967621fdabSRuslan Ermilov.It Dv CRYPTO_AES_CBC 19708fca7a5SJohn-Mark Gurney.It Dv CRYPTO_AES_ICM 19808fca7a5SJohn-Mark Gurney.It Dv CRYPTO_AES_NIST_GCM_16 19908fca7a5SJohn-Mark Gurney.It Dv CRYPTO_AES_NIST_GMAC 20008fca7a5SJohn-Mark Gurney.It Dv CRYPTO_AES_XTS 2017621fdabSRuslan Ermilov.It Dv CRYPTO_ARC4 202b61e8b3eSChristian Brueffer.It Dv CRYPTO_BLF_CBC 203b61e8b3eSChristian Brueffer.It Dv CRYPTO_CAMELLIA_CBC 204b61e8b3eSChristian Brueffer.It Dv CRYPTO_CAST_CBC 20508fca7a5SJohn-Mark Gurney.It Dv CRYPTO_DEFLATE_COMP 206b61e8b3eSChristian Brueffer.It Dv CRYPTO_DES_CBC 207b61e8b3eSChristian Brueffer.It Dv CRYPTO_3DES_CBC 2087621fdabSRuslan Ermilov.It Dv CRYPTO_MD5 209b61e8b3eSChristian Brueffer.It Dv CRYPTO_MD5_HMAC 210b61e8b3eSChristian Brueffer.It Dv CRYPTO_MD5_KPDK 21108fca7a5SJohn-Mark Gurney.It Dv CRYPTO_NULL_HMAC 21208fca7a5SJohn-Mark Gurney.It Dv CRYPTO_NULL_CBC 213b61e8b3eSChristian Brueffer.It Dv CRYPTO_RIPEMD160_HMAC 2147621fdabSRuslan Ermilov.It Dv CRYPTO_SHA1 215b61e8b3eSChristian Brueffer.It Dv CRYPTO_SHA1_HMAC 216b61e8b3eSChristian Brueffer.It Dv CRYPTO_SHA1_KPDK 21771ee05c8SPawel Jakub Dawidek.It Dv CRYPTO_SHA2_256_HMAC 21871ee05c8SPawel Jakub Dawidek.It Dv CRYPTO_SHA2_384_HMAC 21971ee05c8SPawel Jakub Dawidek.It Dv CRYPTO_SHA2_512_HMAC 22008fca7a5SJohn-Mark Gurney.It Dv CRYPTO_SKIPJACK_CBC 2217621fdabSRuslan Ermilov.El 2227621fdabSRuslan Ermilov.It Va cri_klen 223f4bf4335SSam LefflerSpecifies the length of the key in bits, for variable-size key 224f4bf4335SSam Leffleralgorithms. 225f3326d72SPawel Jakub Dawidek.It Va cri_mlen 226f3326d72SPawel Jakub DawidekSpecifies how many bytes from the calculated hash should be copied back. 227f3326d72SPawel Jakub Dawidek0 means entire hash. 2287621fdabSRuslan Ermilov.It Va cri_key 229f4bf4335SSam LefflerContains the key to be used with the algorithm. 2307621fdabSRuslan Ermilov.It Va cri_iv 231f4bf4335SSam LefflerContains an explicit initialization vector (IV), if it does not prefix 232f4bf4335SSam Lefflerthe data. 23308fca7a5SJohn-Mark GurneyThis field is ignored during initialization 23408fca7a5SJohn-Mark Gurney.Pq Nm crypto_newsession . 235f4bf4335SSam LefflerIf no IV is explicitly passed (see below on details), a random IV is used 236f4bf4335SSam Lefflerby the device driver processing the request. 2377621fdabSRuslan Ermilov.It Va cri_next 238f4bf4335SSam LefflerContains a pointer to another 2397621fdabSRuslan Ermilov.Vt cryptoini 240f4bf4335SSam Lefflerstructure. 241f4bf4335SSam LefflerMultiple such structures may be linked to establish multi-algorithm sessions 2427621fdabSRuslan Ermilov.Xr ( ipsec 4 243f4bf4335SSam Leffleris an example consumer of such a feature). 244f4bf4335SSam Leffler.El 245f4bf4335SSam Leffler.Pp 246f4bf4335SSam LefflerThe 2477621fdabSRuslan Ermilov.Vt cryptoini 248f4bf4335SSam Lefflerstructure and its contents will not be modified by the framework (or 249f4bf4335SSam Lefflerthe drivers used). 250f4bf4335SSam LefflerSubsequent requests for processing that use the 251f4bf4335SSam LefflerSID returned will avoid the cost of re-initializing the hardware (in 252f4bf4335SSam Leffleressence, SID acts as an index in the session cache of the driver). 253f4bf4335SSam Leffler.Pp 254f4bf4335SSam Leffler.Fn crypto_freesession 255f4bf4335SSam Leffleris called with the SID returned by 256f4bf4335SSam Leffler.Fn crypto_newsession 257f4bf4335SSam Lefflerto disestablish the session. 258f4bf4335SSam Leffler.Pp 259f4bf4335SSam Leffler.Fn crypto_dispatch 260f4bf4335SSam Leffleris called to process a request. 261f4bf4335SSam LefflerThe various fields in the 2627621fdabSRuslan Ermilov.Vt cryptop 263f4bf4335SSam Lefflerstructure are: 2647621fdabSRuslan Ermilov.Bl -tag -width ".Va crp_callback" 2657621fdabSRuslan Ermilov.It Va crp_sid 266f4bf4335SSam LefflerContains the SID. 2677621fdabSRuslan Ermilov.It Va crp_ilen 268f4bf4335SSam LefflerIndicates the total length in bytes of the buffer to be processed. 2697621fdabSRuslan Ermilov.It Va crp_olen 270f4bf4335SSam LefflerOn return, contains the total length of the result. 271f4bf4335SSam LefflerFor symmetric crypto operations, this will be the same as the input length. 272f4bf4335SSam LefflerThis will be used if the framework needs to allocate a new 273f4bf4335SSam Lefflerbuffer for the result (or for re-formatting the input). 2747621fdabSRuslan Ermilov.It Va crp_callback 275f4bf4335SSam LefflerThis routine is invoked upon completion of the request, whether 276f4bf4335SSam Lefflersuccessful or not. 277f4bf4335SSam LefflerIt is invoked through the 278f4bf4335SSam Leffler.Fn crypto_done 279f4bf4335SSam Lefflerroutine. 280f4bf4335SSam LefflerIf the request was not successful, an error code is set in the 2817621fdabSRuslan Ermilov.Va crp_etype 282f4bf4335SSam Lefflerfield. 283f4bf4335SSam LefflerIt is the responsibility of the callback routine to set the appropriate 284f4bf4335SSam Leffler.Xr spl 9 285f4bf4335SSam Lefflerlevel. 2867621fdabSRuslan Ermilov.It Va crp_etype 287f4bf4335SSam LefflerContains the error type, if any errors were encountered, or zero if 288f4bf4335SSam Lefflerthe request was successfully processed. 289f4bf4335SSam LefflerIf the 290f4bf4335SSam Leffler.Er EAGAIN 291f4bf4335SSam Lefflererror code is returned, the SID has changed (and has been recorded in the 2927621fdabSRuslan Ermilov.Va crp_sid 293f4bf4335SSam Lefflerfield). 294f4bf4335SSam LefflerThe consumer should record the new SID and use it in all subsequent requests. 295f4bf4335SSam LefflerIn this case, the request may be re-submitted immediately. 296f4bf4335SSam LefflerThis mechanism is used by the framework to perform 297f4bf4335SSam Lefflersession migration (move a session from one driver to another, because 298f4bf4335SSam Lefflerof availability, performance, or other considerations). 299f4bf4335SSam Leffler.Pp 300f4bf4335SSam LefflerNote that this field only makes sense when examined by 301f4bf4335SSam Lefflerthe callback routine specified in 3027621fdabSRuslan Ermilov.Va crp_callback . 303f4bf4335SSam LefflerErrors are returned to the invoker of 304f4bf4335SSam Leffler.Fn crypto_process 305f4bf4335SSam Leffleronly when enough information is not present to call the callback 306f4bf4335SSam Lefflerroutine (i.e., if the pointer passed is 307f4bf4335SSam Leffler.Dv NULL 308f4bf4335SSam Leffleror if no callback routine was specified). 3097621fdabSRuslan Ermilov.It Va crp_flags 310f4bf4335SSam LefflerIs a bitmask of flags associated with this request. 311f4bf4335SSam LefflerCurrently defined flags are: 312265803d0SPawel Jakub Dawidek.Bl -tag -width ".Dv CRYPTO_F_CBIFSYNC" 313f4bf4335SSam Leffler.It Dv CRYPTO_F_IMBUF 314f4bf4335SSam LefflerThe buffer pointed to by 3157621fdabSRuslan Ermilov.Va crp_buf 316f4bf4335SSam Leffleris an mbuf chain. 317265803d0SPawel Jakub Dawidek.It Dv CRYPTO_F_IOV 318265803d0SPawel Jakub DawidekThe buffer pointed to by 319265803d0SPawel Jakub Dawidek.Va crp_buf 32081ae4b8dSRuslan Ermilovis an 32181ae4b8dSRuslan Ermilov.Vt uio 32281ae4b8dSRuslan Ermilovstructure. 323265803d0SPawel Jakub Dawidek.It Dv CRYPTO_F_BATCH 324265803d0SPawel Jakub DawidekBatch operation if possible. 325265803d0SPawel Jakub Dawidek.It Dv CRYPTO_F_CBIMM 3269280e5faSMike PritchardDo callback immediately instead of doing it from a dedicated kernel thread. 327265803d0SPawel Jakub Dawidek.It Dv CRYPTO_F_DONE 328265803d0SPawel Jakub DawidekOperation completed. 329265803d0SPawel Jakub Dawidek.It Dv CRYPTO_F_CBIFSYNC 33094b59118SJohn-Mark GurneyDo callback immediately if operation is synchronous (that the driver 33194b59118SJohn-Mark Gurneyspecified the 33294b59118SJohn-Mark Gurney.Dv CRYPTOCAP_F_SYNC 33394b59118SJohn-Mark Gurneyflag). 334f4bf4335SSam Leffler.El 3357621fdabSRuslan Ermilov.It Va crp_buf 336f4bf4335SSam LefflerPoints to the input buffer. 337f4bf4335SSam LefflerOn return (when the callback is invoked), 338f4bf4335SSam Lefflerit contains the result of the request. 339f4bf4335SSam LefflerThe input buffer may be an mbuf 3401403a8c7SSam Lefflerchain or a contiguous buffer, 341f4bf4335SSam Lefflerdepending on 3427621fdabSRuslan Ermilov.Va crp_flags . 3437621fdabSRuslan Ermilov.It Va crp_opaque 344f4bf4335SSam LefflerThis is passed through the crypto framework untouched and is 345f4bf4335SSam Lefflerintended for the invoking application's use. 3467621fdabSRuslan Ermilov.It Va crp_desc 347f4bf4335SSam LefflerThis is a linked list of descriptors. 348f4bf4335SSam LefflerEach descriptor provides 349f4bf4335SSam Lefflerinformation about what type of cryptographic operation should be done 350f4bf4335SSam Leffleron the input buffer. 351f4bf4335SSam LefflerThe various fields are: 3527621fdabSRuslan Ermilov.Bl -tag -width ".Va crd_inject" 3533b72821fSPawel Jakub Dawidek.It Va crd_iv 3542ca5eb5dSJohn-Mark GurneyWhen the flag 3553b72821fSPawel Jakub Dawidek.Dv CRD_F_IV_EXPLICIT 3562ca5eb5dSJohn-Mark Gurneyis set, this field contains the IV. 3573b72821fSPawel Jakub Dawidek.It Va crd_key 3583b72821fSPawel Jakub DawidekWhen the 3593b72821fSPawel Jakub Dawidek.Dv CRD_F_KEY_EXPLICIT 3602ca5eb5dSJohn-Mark Gurneyflag is set, the 3613b72821fSPawel Jakub Dawidek.Va crd_key 3623b72821fSPawel Jakub Dawidekpoints to a buffer with encryption or authentication key. 3633b72821fSPawel Jakub Dawidek.It Va crd_alg 3643b72821fSPawel Jakub DawidekAn algorithm to use. 3653b72821fSPawel Jakub DawidekMust be the same as the one given at newsession time. 3663b72821fSPawel Jakub Dawidek.It Va crd_klen 3673b72821fSPawel Jakub DawidekThe 3683b72821fSPawel Jakub Dawidek.Va crd_key 3693b72821fSPawel Jakub Dawidekkey length. 3707621fdabSRuslan Ermilov.It Va crd_skip 371f4bf4335SSam LefflerThe offset in the input buffer where processing should start. 3727621fdabSRuslan Ermilov.It Va crd_len 373f4bf4335SSam LefflerHow many bytes, after 3747621fdabSRuslan Ermilov.Va crd_skip , 375f4bf4335SSam Lefflershould be processed. 3767621fdabSRuslan Ermilov.It Va crd_inject 377e08d13cfSJohn-Mark GurneyThe 378e08d13cfSJohn-Mark Gurney.Va crd_inject 379e08d13cfSJohn-Mark Gurneyfield specifies an offset in bytes from the beginning of the buffer. 380e08d13cfSJohn-Mark GurneyFor encryption algorithms, this may be where the IV will be inserted 381e08d13cfSJohn-Mark Gurneywhen encrypting or where the IV may be found for 382e08d13cfSJohn-Mark Gurneydecryption (subject to 3837621fdabSRuslan Ermilov.Va crd_flags ) . 384f4bf4335SSam LefflerFor MAC algorithms, this is where the result of the keyed hash will be 385f4bf4335SSam Lefflerinserted. 3867621fdabSRuslan Ermilov.It Va crd_flags 387f4bf4335SSam LefflerThe following flags are defined: 38881ae4b8dSRuslan Ermilov.Bl -tag -width 3n 389f4bf4335SSam Leffler.It Dv CRD_F_ENCRYPT 390f4bf4335SSam LefflerFor encryption algorithms, this bit is set when encryption is required 391f4bf4335SSam Leffler(when not set, decryption is performed). 392f4bf4335SSam Leffler.It Dv CRD_F_IV_PRESENT 3932ca5eb5dSJohn-Mark Gurney.\" This flag name has nothing to do w/ it's behavior, fix the name. 3942ca5eb5dSJohn-Mark GurneyFor encryption, if this bit is not set the IV used to encrypt the packet 3952ca5eb5dSJohn-Mark Gurneywill be written at the location pointed to by 3967621fdabSRuslan Ermilov.Va crd_inject . 397f4bf4335SSam LefflerThe IV length is assumed to be equal to the blocksize of the 398f4bf4335SSam Lefflerencryption algorithm. 3992ca5eb5dSJohn-Mark GurneyFor encryption, if this bit is set, nothing is done. 4002ca5eb5dSJohn-Mark GurneyFor decryption, this flag has no meaning. 40108fca7a5SJohn-Mark GurneyApplications that do special 4027621fdabSRuslan Ermilov.Dq "IV cooking" , 403f4bf4335SSam Lefflersuch as the half-IV mode in 404f4bf4335SSam Leffler.Xr ipsec 4 , 405f4bf4335SSam Lefflercan use this flag to indicate that the IV should not be written on the packet. 406f4bf4335SSam LefflerThis flag is typically used in conjunction with the 407f4bf4335SSam Leffler.Dv CRD_F_IV_EXPLICIT 408f4bf4335SSam Lefflerflag. 409f4bf4335SSam Leffler.It Dv CRD_F_IV_EXPLICIT 4102ca5eb5dSJohn-Mark GurneyThis bit is set when the IV is explicitly 411f4bf4335SSam Lefflerprovided by the consumer in the 4123b72821fSPawel Jakub Dawidek.Va crd_iv 4131deaec43SPawel Jakub Dawidekfield. 414f4bf4335SSam LefflerOtherwise, for encryption operations the IV is provided for by 415f4bf4335SSam Lefflerthe driver used to perform the operation, whereas for decryption 416e08d13cfSJohn-Mark Gurneyoperations the offset of the IV is provided by the 4177621fdabSRuslan Ermilov.Va crd_inject 418f4bf4335SSam Lefflerfield. 419f4bf4335SSam LefflerThis flag is typically used when the IV is calculated 4207621fdabSRuslan Ermilov.Dq "on the fly" 421f4bf4335SSam Lefflerby the consumer, and does not precede the data (some 422f4bf4335SSam Leffler.Xr ipsec 4 423f4bf4335SSam Lefflerconfigurations, and the encrypted swap are two such examples). 4241deaec43SPawel Jakub Dawidek.It Dv CRD_F_KEY_EXPLICIT 4253b72821fSPawel Jakub DawidekFor encryption and authentication (MAC) algorithms, this bit is set when the key 4263b72821fSPawel Jakub Dawidekis explicitly provided by the consumer in the 4273b72821fSPawel Jakub Dawidek.Va crd_key 4281deaec43SPawel Jakub Dawidekfield for the given operation. 4293b72821fSPawel Jakub DawidekOtherwise, the key is taken at newsession time from the 4303b72821fSPawel Jakub Dawidek.Va cri_key 4313b72821fSPawel Jakub Dawidekfield. 43208fca7a5SJohn-Mark GurneyAs calculating the key schedule may take a while, it is recommended that often 43308fca7a5SJohn-Mark Gurneyused keys are given their own session. 434f4bf4335SSam Leffler.It Dv CRD_F_COMP 435f4bf4335SSam LefflerFor compression algorithms, this bit is set when compression is required (when 436f4bf4335SSam Lefflernot set, decompression is performed). 437f4bf4335SSam Leffler.El 4387621fdabSRuslan Ermilov.It Va CRD_INI 439f4bf4335SSam LefflerThis 4407621fdabSRuslan Ermilov.Vt cryptoini 441f4bf4335SSam Lefflerstructure will not be modified by the framework or the device drivers. 442f4bf4335SSam LefflerSince this information accompanies every cryptographic 443f4bf4335SSam Leffleroperation request, drivers may re-initialize state on-demand 444f4bf4335SSam Leffler(typically an expensive operation). 445f4bf4335SSam LefflerFurthermore, the cryptographic 446f4bf4335SSam Lefflerframework may re-route requests as a result of full queues or hardware 447f4bf4335SSam Lefflerfailure, as described above. 4487621fdabSRuslan Ermilov.It Va crd_next 449f4bf4335SSam LefflerPoint to the next descriptor. 450f4bf4335SSam LefflerLinked operations are useful in protocols such as 451f4bf4335SSam Leffler.Xr ipsec 4 , 452f4bf4335SSam Lefflerwhere multiple cryptographic transforms may be applied on the same 453f4bf4335SSam Lefflerblock of data. 454f4bf4335SSam Leffler.El 455f4bf4335SSam Leffler.El 456f4bf4335SSam Leffler.Pp 457f4bf4335SSam Leffler.Fn crypto_getreq 458f4bf4335SSam Lefflerallocates a 4597621fdabSRuslan Ermilov.Vt cryptop 460f4bf4335SSam Lefflerstructure with a linked list of as many 4617621fdabSRuslan Ermilov.Vt cryptodesc 462f4bf4335SSam Lefflerstructures as were specified in the argument passed to it. 463f4bf4335SSam Leffler.Pp 464f4bf4335SSam Leffler.Fn crypto_freereq 465f4bf4335SSam Lefflerdeallocates a structure 4667621fdabSRuslan Ermilov.Vt cryptop 467f4bf4335SSam Lefflerand any 4687621fdabSRuslan Ermilov.Vt cryptodesc 469f4bf4335SSam Lefflerstructures linked to it. 470f4bf4335SSam LefflerNote that it is the responsibility of the 471f4bf4335SSam Lefflercallback routine to do the necessary cleanups associated with the 472f4bf4335SSam Leffleropaque field in the 4737621fdabSRuslan Ermilov.Vt cryptop 474f4bf4335SSam Lefflerstructure. 475f4bf4335SSam Leffler.Pp 476f4bf4335SSam Leffler.Fn crypto_kdispatch 477f4bf4335SSam Leffleris called to perform a keying operation. 478f4bf4335SSam LefflerThe various fields in the 4797621fdabSRuslan Ermilov.Vt cryptkop 480f4bf4335SSam Lefflerstructure are: 481c2965cd1SJoel Dahl.Bl -tag -width ".Va krp_callback" 4827621fdabSRuslan Ermilov.It Va krp_op 4837621fdabSRuslan ErmilovOperation code, such as 4847621fdabSRuslan Ermilov.Dv CRK_MOD_EXP . 4857621fdabSRuslan Ermilov.It Va krp_status 486f4bf4335SSam LefflerReturn code. 4877621fdabSRuslan ErmilovThis 4887621fdabSRuslan Ermilov.Va errno Ns -style 4897621fdabSRuslan Ermilovvariable indicates whether lower level reasons 490f4bf4335SSam Lefflerfor operation failure. 4917621fdabSRuslan Ermilov.It Va krp_iparams 492f4bf4335SSam LefflerNumber if input parameters to the specified operation. 493f4bf4335SSam LefflerNote that each operation has a (typically hardwired) number of such parameters. 4947621fdabSRuslan Ermilov.It Va krp_oparams 495f4bf4335SSam LefflerNumber if output parameters from the specified operation. 496f4bf4335SSam LefflerNote that each operation has a (typically hardwired) number of such parameters. 4977621fdabSRuslan Ermilov.It Va krp_kvp 498f4bf4335SSam LefflerAn array of kernel memory blocks containing the parameters. 4997621fdabSRuslan Ermilov.It Va krp_hid 500f4bf4335SSam LefflerIdentifier specifying which low-level driver is being used. 5017621fdabSRuslan Ermilov.It Va krp_callback 502f4bf4335SSam LefflerCallback called on completion of a keying operation. 503f4bf4335SSam Leffler.El 504f4bf4335SSam Leffler.Sh DRIVER-SIDE API 505f4bf4335SSam LefflerThe 506f4bf4335SSam Leffler.Fn crypto_get_driverid , 507f4bf4335SSam Leffler.Fn crypto_register , 508f4bf4335SSam Leffler.Fn crypto_kregister , 509f4bf4335SSam Leffler.Fn crypto_unregister , 5101403a8c7SSam Leffler.Fn crypto_unblock , 511f4bf4335SSam Lefflerand 512f4bf4335SSam Leffler.Fn crypto_done 513f4bf4335SSam Lefflerroutines are used by drivers that provide support for cryptographic 514f4bf4335SSam Lefflerprimitives to register and unregister with the kernel crypto services 515f4bf4335SSam Lefflerframework. 51694b59118SJohn-Mark Gurney.Pp 517f4bf4335SSam LefflerDrivers must first use the 518f4bf4335SSam Leffler.Fn crypto_get_driverid 519f4bf4335SSam Lefflerfunction to acquire a driver identifier, specifying the 52094b59118SJohn-Mark Gurney.Fa flags 52194b59118SJohn-Mark Gurneyas an argument. 52294b59118SJohn-Mark GurneyOne of 52394b59118SJohn-Mark Gurney.Dv CRYPTOCAP_F_SOFTWARE 52494b59118SJohn-Mark Gurneyor 52594b59118SJohn-Mark Gurney.Dv CRYPTOCAP_F_HARDWARE 52694b59118SJohn-Mark Gurneymust be specified. 52794b59118SJohn-Mark GurneyThe 52894b59118SJohn-Mark Gurney.Dv CRYPTOCAP_F_SYNC 52994b59118SJohn-Mark Gurneymay also be specified, and should be specified if the driver does all of 53094b59118SJohn-Mark Gurneyit's operations synchronously. 53194b59118SJohn-Mark Gurney.Pp 532f4bf4335SSam LefflerFor each algorithm the driver supports, it must then call 533f4bf4335SSam Leffler.Fn crypto_register . 534f4bf4335SSam LefflerThe first two arguments are the driver and algorithm identifiers. 535f4bf4335SSam LefflerThe next two arguments specify the largest possible operator length (in bits, 5361403a8c7SSam Lefflerimportant for public key operations) and flags for this algorithm. 5371403a8c7SSam LefflerThe last four arguments must be provided in the first call to 538f4bf4335SSam Leffler.Fn crypto_register 539f4bf4335SSam Lefflerand are ignored in all subsequent calls. 540f4bf4335SSam LefflerThey are pointers to three 541f4bf4335SSam Lefflerdriver-provided functions that the framework may call to establish new 542f4bf4335SSam Lefflercryptographic context with the driver, free already established 543f4bf4335SSam Lefflercontext, and ask for a request to be processed (encrypt, decrypt, 5441403a8c7SSam Leffleretc.); and an opaque parameter to pass when calling each of these routines. 54594b59118SJohn-Mark Gurney.Pp 546f4bf4335SSam Leffler.Fn crypto_unregister 547f4bf4335SSam Leffleris called by drivers that wish to withdraw support for an algorithm. 548f4bf4335SSam LefflerThe two arguments are the driver and algorithm identifiers, respectively. 549f4bf4335SSam LefflerTypically, drivers for 5501403a8c7SSam LefflerPCMCIA 551f4bf4335SSam Lefflercrypto cards that are being ejected will invoke this routine for all 552f4bf4335SSam Leffleralgorithms supported by the card. 5531403a8c7SSam Leffler.Fn crypto_unregister_all 5541403a8c7SSam Lefflerwill unregister all algorithms registered by a driver 555f4bf4335SSam Lefflerand the driver will be disabled (no new sessions will be allocated on 556f4bf4335SSam Lefflerthat driver, and any existing sessions will be migrated to other 557f4bf4335SSam Lefflerdrivers). 558f4bf4335SSam LefflerThe same will be done if all algorithms associated with a driver are 559f4bf4335SSam Lefflerunregistered one by one. 56094b59118SJohn-Mark GurneyAfter a call to 56194b59118SJohn-Mark Gurney.Fn crypto_unregister_all 56294b59118SJohn-Mark Gurneythere will be no threads in either the newsession or freesession function 56394b59118SJohn-Mark Gurneyof the driver. 564f4bf4335SSam Leffler.Pp 56594b59118SJohn-Mark GurneyThe calling convention for the three driver-supplied routines are: 5667621fdabSRuslan Ermilov.Pp 5677621fdabSRuslan Ermilov.Bl -item -compact 5687621fdabSRuslan Ermilov.It 5697621fdabSRuslan Ermilov.Ft int 57094b59118SJohn-Mark Gurney.Fn \*[lp]*newsession\*[rp] "device_t" "uint32_t *" "struct cryptoini *" ; 5717621fdabSRuslan Ermilov.It 5727621fdabSRuslan Ermilov.Ft int 57394b59118SJohn-Mark Gurney.Fn \*[lp]*freesession\*[rp] "device_t" "uint64_t" ; 5747621fdabSRuslan Ermilov.It 5757621fdabSRuslan Ermilov.Ft int 57694b59118SJohn-Mark Gurney.Fn \*[lp]*process\*[rp] "device_t" "struct cryptop *" "int" ; 5777621fdabSRuslan Ermilov.It 5787621fdabSRuslan Ermilov.Ft int 57994b59118SJohn-Mark Gurney.Fn \*[lp]*kprocess\*[rp] "device_t" "struct cryptkop *" "int" ; 5807621fdabSRuslan Ermilov.El 581f4bf4335SSam Leffler.Pp 582f4bf4335SSam LefflerOn invocation, the first argument to 58394b59118SJohn-Mark Gurneyall routines is the 58494b59118SJohn-Mark Gurney.Fa device_t 58594b59118SJohn-Mark Gurneythat was provided to 58694b59118SJohn-Mark Gurney.Fn crypto_get_driverid . 5871403a8c7SSam LefflerThe second argument to 588f4bf4335SSam Leffler.Fn newsession 589f4bf4335SSam Lefflercontains the driver identifier obtained via 590f4bf4335SSam Leffler.Fn crypto_get_driverid . 5917621fdabSRuslan ErmilovOn successful return, it should contain a driver-specific session 592f4bf4335SSam Leffleridentifier. 5931403a8c7SSam LefflerThe third argument is identical to that of 594f4bf4335SSam Leffler.Fn crypto_newsession . 595f4bf4335SSam Leffler.Pp 596f4bf4335SSam LefflerThe 597f4bf4335SSam Leffler.Fn freesession 5981403a8c7SSam Lefflerroutine takes as arguments the opaque data value and the SID 5991403a8c7SSam Leffler(which is the concatenation of the 600f4bf4335SSam Lefflerdriver identifier and the driver-specific session identifier). 601f4bf4335SSam LefflerIt should clear any context associated with the session (clear hardware 602f4bf4335SSam Lefflerregisters, memory, etc.). 603f4bf4335SSam Leffler.Pp 604f4bf4335SSam LefflerThe 605f4bf4335SSam Leffler.Fn process 606f4bf4335SSam Lefflerroutine is invoked with a request to perform crypto processing. 60794b59118SJohn-Mark GurneyThis routine must not block or sleep, but should queue the request and return 60894b59118SJohn-Mark Gurneyimmediately or process the request to completion. 6091403a8c7SSam LefflerIn case of an unrecoverable error, the error indication must be placed in the 6107621fdabSRuslan Ermilov.Va crp_etype 611f4bf4335SSam Lefflerfield of the 6127621fdabSRuslan Ermilov.Vt cryptop 613f4bf4335SSam Lefflerstructure. 614f4bf4335SSam LefflerWhen the request is completed, or an error is detected, the 615f4bf4335SSam Leffler.Fn process 61694b59118SJohn-Mark Gurneyroutine must invoke 617f4bf4335SSam Leffler.Fn crypto_done . 618f4bf4335SSam LefflerSession migration may be performed, as mentioned previously. 619f4bf4335SSam Leffler.Pp 6201403a8c7SSam LefflerIn case of a temporary resource exhaustion, the 6217621fdabSRuslan Ermilov.Fn process 6221403a8c7SSam Lefflerroutine may return 6231403a8c7SSam Leffler.Er ERESTART 6241403a8c7SSam Lefflerin which case the crypto services will requeue the request, mark the driver 6257621fdabSRuslan Ermilovas 6267621fdabSRuslan Ermilov.Dq blocked , 6277621fdabSRuslan Ermilovand stop submitting requests for processing. 6281403a8c7SSam LefflerThe driver is then responsible for notifying the crypto services 6291403a8c7SSam Lefflerwhen it is again able to process requests through the 6301403a8c7SSam Leffler.Fn crypto_unblock 6311403a8c7SSam Lefflerroutine. 6321403a8c7SSam LefflerThis simple flow control mechanism should only be used for short-lived 6331403a8c7SSam Lefflerresource exhaustion as it causes operations to be queued in the crypto 6341403a8c7SSam Lefflerlayer. 6357621fdabSRuslan ErmilovDoing so is preferable to returning an error in such cases as 6367621fdabSRuslan Ermilovit can cause network protocols to degrade performance by treating the 6371403a8c7SSam Lefflerfailure much like a lost packet. 6381403a8c7SSam Leffler.Pp 639f4bf4335SSam LefflerThe 640f4bf4335SSam Leffler.Fn kprocess 641f4bf4335SSam Lefflerroutine is invoked with a request to perform crypto key processing. 642f4bf4335SSam LefflerThis routine must not block, but should queue the request and return 643f4bf4335SSam Lefflerimmediately. 644f4bf4335SSam LefflerUpon processing the request, the callback routine should be invoked. 6451403a8c7SSam LefflerIn case of an unrecoverable error, the error indication must be placed in the 6467621fdabSRuslan Ermilov.Va krp_status 647f4bf4335SSam Lefflerfield of the 6487621fdabSRuslan Ermilov.Vt cryptkop 649f4bf4335SSam Lefflerstructure. 650f4bf4335SSam LefflerWhen the request is completed, or an error is detected, the 651f4bf4335SSam Leffler.Fn kprocess 652f4bf4335SSam Lefflerroutine should invoked 653f4bf4335SSam Leffler.Fn crypto_kdone . 654f4bf4335SSam Leffler.Sh RETURN VALUES 655f4bf4335SSam Leffler.Fn crypto_register , 656f4bf4335SSam Leffler.Fn crypto_kregister , 657f4bf4335SSam Leffler.Fn crypto_unregister , 658f4bf4335SSam Leffler.Fn crypto_newsession , 6591403a8c7SSam Leffler.Fn crypto_freesession , 660f4bf4335SSam Lefflerand 6617621fdabSRuslan Ermilov.Fn crypto_unblock 662f4bf4335SSam Lefflerreturn 0 on success, or an error code on failure. 663f4bf4335SSam Leffler.Fn crypto_get_driverid 664f4bf4335SSam Lefflerreturns a non-negative value on error, and \-1 on failure. 665f4bf4335SSam Leffler.Fn crypto_getreq 666f4bf4335SSam Lefflerreturns a pointer to a 6677621fdabSRuslan Ermilov.Vt cryptop 668f4bf4335SSam Lefflerstructure and 669f4bf4335SSam Leffler.Dv NULL 670f4bf4335SSam Leffleron failure. 671f4bf4335SSam Leffler.Fn crypto_dispatch 672f4bf4335SSam Lefflerreturns 673f4bf4335SSam Leffler.Er EINVAL 6747621fdabSRuslan Ermilovif its argument or the callback function was 675f4bf4335SSam Leffler.Dv NULL , 676f4bf4335SSam Lefflerand 0 otherwise. 677f4bf4335SSam LefflerThe callback is provided with an error code in case of failure, in the 6787621fdabSRuslan Ermilov.Va crp_etype 679f4bf4335SSam Lefflerfield. 680f4bf4335SSam Leffler.Sh FILES 681627e7962SSam Leffler.Bl -tag -width ".Pa sys/opencrypto/crypto.c" 682627e7962SSam Leffler.It Pa sys/opencrypto/crypto.c 683f4bf4335SSam Lefflermost of the framework code 684f4bf4335SSam Leffler.El 685f4bf4335SSam Leffler.Sh SEE ALSO 686c7c8edc3SJohn-Mark Gurney.Xr crypto 4 , 687f4bf4335SSam Leffler.Xr ipsec 4 , 68808fca7a5SJohn-Mark Gurney.Xr crypto 7 , 689f4bf4335SSam Leffler.Xr malloc 9 , 6901403a8c7SSam Leffler.Xr sleep 9 691f4bf4335SSam Leffler.Sh HISTORY 692f4bf4335SSam LefflerThe cryptographic framework first appeared in 6937621fdabSRuslan Ermilov.Ox 2.7 6947621fdabSRuslan Ermilovand was written by 6958a7314fcSBaptiste Daroussin.An Angelos D. Keromytis Aq Mt angelos@openbsd.org . 696f4bf4335SSam Leffler.Sh BUGS 697f4bf4335SSam LefflerThe framework currently assumes that all the algorithms in a 698f4bf4335SSam Leffler.Fn crypto_newsession 699f4bf4335SSam Leffleroperation must be available by the same driver. 7007621fdabSRuslan ErmilovIf that is not the case, session initialization will fail. 701f4bf4335SSam Leffler.Pp 702f4bf4335SSam LefflerThe framework also needs a mechanism for determining which driver is 703f4bf4335SSam Lefflerbest for a specific set of algorithms associated with a session. 704f4bf4335SSam LefflerSome type of benchmarking is in order here. 705f4bf4335SSam Leffler.Pp 706f4bf4335SSam LefflerMultiple instances of the same algorithm in the same session are not 707f4bf4335SSam Lefflersupported. 708f4bf4335SSam LefflerNote that 3DES is considered one algorithm (and not three 709f4bf4335SSam Lefflerinstances of DES). 710f4bf4335SSam LefflerThus, 3DES and DES could be mixed in the same request. 711