1.\" $OpenBSD: crypto.9,v 1.43 2020/03/28 13:16:09 krw Exp $ 2.\" 3.\" The author of this man page is Angelos D. Keromytis (angelos@cis.upenn.edu) 4.\" 5.\" Copyright (c) 2000, 2001 Angelos D. Keromytis 6.\" 7.\" Permission to use, copy, and modify this software with or without fee 8.\" is hereby granted, provided that this entire notice is included in 9.\" all source code copies of any software which is or includes a copy or 10.\" modification of this software. 11.\" 12.\" THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR 13.\" IMPLIED WARRANTY. IN PARTICULAR, NONE OF THE AUTHORS MAKES ANY 14.\" REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE 15.\" MERCHANTABILITY OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR 16.\" PURPOSE. 17.\" 18.Dd $Mdocdate: March 28 2020 $ 19.Dt CRYPTO_GET_DRIVERID 9 20.Os 21.Sh NAME 22.Nm crypto_get_driverid , 23.Nm crypto_register , 24.Nm crypto_unregister , 25.Nm crypto_done , 26.Nm crypto_newsession , 27.Nm crypto_freesession , 28.Nm crypto_dispatch , 29.Nm crypto_getreq , 30.Nm crypto_freereq 31.Nd API for cryptographic services in the kernel 32.Sh SYNOPSIS 33.In crypto/cryptodev.h 34.Ft int32_t 35.Fn crypto_get_driverid "u_int8_t" 36.Ft int 37.Fn crypto_register "u_int32_t" "int *" "int (*)(u_int32_t *, struct cryptoini *)" "int (*)(u_int64_t)" "int (*)(struct cryptop *)" 38.Ft int 39.Fn crypto_unregister "u_int32_t" "int" 40.Ft void 41.Fn crypto_done "struct cryptop *" 42.Ft int 43.Fn crypto_newsession "u_int64_t *" "struct cryptoini *" "int" 44.Ft int 45.Fn crypto_freesession "u_int64_t" 46.Ft int 47.Fn crypto_dispatch "struct cryptop *" 48.Ft struct cryptop * 49.Fn crypto_getreq "int" 50.Ft void 51.Fn crypto_freereq "struct cryptop *" 52.Bd -literal 53 54#define EALG_MAX_BLOCK_LEN 16 55 56struct cryptoini { 57 int cri_alg; 58 int cri_klen; 59 int cri_rnd; 60 caddr_t cri_key; 61 u_int8_t cri_iv[EALG_MAX_BLOCK_LEN]; 62 struct cryptoini *cri_next; 63}; 64 65struct cryptodesc { 66 int crd_skip; 67 int crd_len; 68 int crd_inject; 69 int crd_flags; 70 struct cryptoini CRD_INI; 71 struct cryptodesc *crd_next; 72}; 73 74struct cryptop { 75 u_int64_t crp_sid; 76 int crp_ilen; 77 int crp_olen; 78 int crp_alloctype; 79 int crp_etype; 80 int crp_flags; 81 void *crp_buf; 82 void *crp_opaque; 83 struct cryptodesc *crp_desc; 84 int (*crp_callback)(struct cryptop *); 85 struct cryptop *crp_next; 86 caddr_t crp_mac; 87}; 88.Ed 89.Sh DESCRIPTION 90.Nm 91is a framework for drivers of cryptographic hardware to register with 92the kernel so 93.Dq consumers 94(other kernel subsystems, and eventually 95users through an appropriate device) are able to make use of it. 96Drivers register with the framework the algorithms they support, 97and provide entry points (functions) the framework may call to 98establish, use, and tear down sessions. 99Sessions are used to cache cryptographic information in a particular driver 100(or associated hardware), so initialization is not needed with every request. 101Consumers of cryptographic services pass a set of 102descriptors that instruct the framework (and the drivers registered 103with it) of the operations that should be applied on the data (more 104than one cryptographic operation can be requested). 105.Pp 106Since the consumers may not be associated with a process, drivers may 107not use 108.Xr tsleep 9 . 109The same holds for the framework. 110Thus, a callback mechanism is used 111to notify a consumer that a request has been completed (the 112callback is specified by the consumer on a per-request basis). 113The callback is invoked by the framework whether the request was 114successfully completed or not. 115An error indication is provided in the latter case. 116A specific error code, 117.Er EAGAIN , 118is used to indicate that a session number has changed and that the 119request may be re-submitted immediately with the new session number. 120Errors are only returned to the invoking function if not 121enough information to call the callback is available (meaning, there 122was a fatal error in verifying the arguments). 123For session initialization and teardown there is no callback mechanism used. 124.Pp 125The 126.Fn crypto_newsession 127routine is called by consumers of cryptographic services (such as the 128.Xr ipsec 4 129stack) that wish to establish a new session with the framework. 130On success, the first argument will contain the Session Identifier (SID). 131The second argument contains all the necessary information for 132the driver to establish the session. 133The third argument indicates whether a 134hardware driver should be used (1) or not (0). 135The various fields in the 136.Fa cryptoini 137structure are: 138.Bl -tag -width foobarmoocow 139.It Fa cri_alg 140Contains an algorithm identifier. 141Currently supported encryption algorithms are: 142.Bd -literal 143CRYPTO_3DES_CBC 144CRYPTO_BLF_CBC 145CRYPTO_CAST_CBC 146CRYPTO_AES_CBC 147CRYPTO_AES_CTR 148CRYPTO_AES_XTS 149.Ed 150.Pp 151Authentication algorithms are: 152.Bd -literal 153CRYPTO_MD5_HMAC 154CRYPTO_SHA1_HMAC 155CRYPTO_RIPEMD160_HMAC 156CRYPTO_SHA2_256_HMAC 157CRYPTO_SHA2_384_HMAC 158CRYPTO_SHA2_512_HMAC 159.Ed 160.Pp 161Algorithms performing authenticated encryption are: 162.Bd -literal 163CRYPTO_AES_GCM_16 164CRYPTO_AES_GMAC 165CRYPTO_CHACHA20_POLY1305 166.Ed 167.It Fa cri_klen 168Specifies the length of the key in bits, for variable-size key 169algorithms. 170.It Fa cri_rnd 171Specifies the number of rounds to be used with the algorithm, for 172variable-round algorithms. 173.It Fa cri_key 174Contains the key to be used with the algorithm. 175.It Fa cri_iv 176Contains an explicit initialization vector (IV), if it does not prefix 177the data. 178This field is ignored during initialization. 179If no IV is explicitly passed (see below on details), a random IV is used 180by the device driver processing the request. 181.Pp 182In the case of the CRYPTO_AES_XTS transform, the IV should be provided 183as a 64-bit block number in host byte order. 184.It Fa cri_next 185Contains a pointer to another 186.Fa cryptoini 187structure. 188Multiple such structures may be linked to establish multi-algorithm sessions 189.Pf ( Xr ipsec 4 190is an example consumer of such a feature). 191.El 192.Pp 193The 194.Fa cryptoini 195structure and its contents will not be modified by the framework (or 196the drivers used). 197Subsequent requests for processing that use the 198SID returned will avoid the cost of re-initializing the hardware (in 199essence, SID acts as an index in the session cache of the driver). 200.Pp 201.Fn crypto_freesession 202is called with the SID returned by 203.Fn crypto_newsession 204to disestablish the session. 205.Pp 206.Fn crypto_dispatch 207is called to process a request. 208The various fields in the 209.Fa cryptop 210structure are: 211.Bl -tag -width crp_alloctype 212.It Fa crp_sid 213Contains the SID. 214.It Fa crp_ilen 215Indicates the total length in bytes of the buffer to be processed. 216.It Fa crp_olen 217On return, contains the length of the result, not including 218.Fa crd_skip . 219For symmetric crypto operations, this will be the same as the input length. 220.It Fa crp_alloctype 221Indicates the type of buffer, as used in the kernel 222.Xr malloc 9 223routine. 224This will be used if the framework needs to allocate a new 225buffer for the result (or for re-formatting the input). 226.It Fa crp_callback 227This routine is invoked upon completion of the request, whether 228successful or not. 229It is invoked through the 230.Fn crypto_done 231routine. 232If the request was not successful, an error code is set in the 233.Fa crp_etype 234field. 235It is the responsibility of the callback routine to set the appropriate 236.Xr spl 9 237level. 238.It Fa crp_etype 239Contains the error type, if any errors were encountered, or zero if 240the request was successfully processed. 241If the 242.Er EAGAIN 243error code is returned, the SID has changed (and has been recorded in the 244.Fa crp_sid 245field). 246The consumer should record the new SID and use it in all subsequent requests. 247In this case, the request may be re-submitted immediately. 248This mechanism is used by the framework to perform 249session migration (move a session from one driver to another, because 250of availability, performance, or other considerations). 251.Pp 252Note that this field only makes sense when examined by 253the callback routine specified in 254.Fa crp_callback . 255Errors are returned to the invoker of 256.Fn crypto_process 257only when enough information is not present to call the callback 258routine (i.e., if the pointer passed is 259.Dv NULL 260or if no callback routine was specified). 261.It Fa crp_flags 262Is a bitmask of flags associated with this request. 263Currently defined flags are: 264.Bl -tag -width CRYPTO_F_IMBUF 265.It Dv CRYPTO_F_IMBUF 266The buffer pointed to by 267.Fa crp_buf 268is an mbuf chain. 269.El 270.It Fa crp_buf 271Points to the input buffer. 272On return (when the callback is invoked), 273it contains the result of the request. 274The input buffer may be an mbuf 275chain or a struct uio depending on 276.Fa crp_flags . 277.It Fa crp_opaque 278This is passed through the crypto framework untouched and is 279intended for the invoking application's use. 280.It Fa crp_desc 281This is a linked list of descriptors. 282Each descriptor provides 283information about what type of cryptographic operation should be done 284on the input buffer. 285The various fields are: 286.Bl -tag -width "crd_inject" 287.It Fa crd_skip 288The offset in the input buffer where processing should start. 289.It Fa crd_len 290How many bytes, after 291.Fa crd_skip , 292should be processed. 293.It Fa crd_inject 294Offset from the beginning of the buffer to insert any results. 295For encryption algorithms, this is where the initialization vector 296(IV) will be inserted when encrypting or where it can be found when 297decrypting (subject to 298.Fa crd_flags ) . 299For MAC algorithms, this is where the result of the keyed hash will be 300inserted. 301.It Fa crd_flags 302The following flags are defined: 303.Bl -tag -width CRD_F_IV_EXPLICIT 304.It Dv CRD_F_ENCRYPT 305For encryption algorithms, this bit is set when encryption is required 306(when not set, decryption is performed). 307.It Dv CRD_F_IV_PRESENT 308For encryption algorithms, this bit is set when the IV already 309precedes the data, so the 310.Fa crd_inject 311value will be ignored and no IV will be written in the buffer. 312Otherwise, the IV used to encrypt the packet will be written 313at the location pointed to by 314.Fa crd_inject . 315The IV length is assumed to be equal to the blocksize of the 316encryption algorithm. 317Some applications that do special 318.Dq IV cooking , 319such as the half-IV mode in 320.Xr ipsec 4 , 321can use this flag to indicate that the IV should not be written on the packet. 322This flag is typically used in conjunction with the 323.Dv CRD_F_IV_EXPLICIT 324flag. 325.It Dv CRD_F_IV_EXPLICIT 326For encryption algorithms, this bit is set when the IV is explicitly 327provided by the consumer in the 328.Fa crd_iv 329fields. 330Otherwise, for encryption operations the IV is provided for by 331the driver used to perform the operation, whereas for decryption 332operations it is pointed to by the 333.Fa crd_inject 334field. 335This flag is typically used when the IV is calculated 336.Dq on the fly 337by the consumer, and does not precede the data (some 338.Xr ipsec 4 339configurations, and the encrypted swap are two such examples). 340.It Dv CRD_F_COMP 341For compression algorithms, this bit is set when compression is required (when 342not set, decompression is performed). 343.El 344.It Fa CRD_INI 345This 346.Fa cryptoini 347structure will not be modified by the framework or the device drivers. 348Since this information accompanies every cryptographic 349operation request, drivers may re-initialize state on-demand 350(typically an expensive operation). 351Furthermore, the cryptographic 352framework may re-route requests as a result of full queues or hardware 353failure, as described above. 354.It Fa crd_next 355Point to the next descriptor. 356Linked operations are useful in protocols such as 357.Xr ipsec 4 , 358where multiple cryptographic transforms may be applied on the same 359block of data. 360.El 361.El 362.Pp 363.Fn crypto_getreq 364allocates a 365.Fa cryptop 366structure with a linked list of as many 367.Fa cryptodesc 368structures as were specified in the argument passed to it. 369.Pp 370.Fn crypto_freereq 371deallocates a structure 372.Fa cryptop 373and any 374.Fa cryptodesc 375structures linked to it. 376Note that it is the responsibility of the 377callback routine to do the necessary cleanups associated with the 378opaque field in the 379.Fa cryptop 380structure. 381.Sh DRIVER-SIDE API 382The 383.Fn crypto_get_driverid , 384.Fn crypto_register , 385.Fn crypto_unregister , 386and 387.Fn crypto_done 388routines are used by drivers that provide support for cryptographic 389primitives to register and unregister with the kernel crypto services 390framework. 391Drivers must first use the 392.Fn crypto_get_driverid 393function to acquire a driver identifier, specifying the 394.Fa cc_flags 395as an argument (normally 0, but software-only drivers should specify 396.Dv CRYPTOCAP_F_SOFTWARE ) . 397For each algorithm the driver supports, it must then call 398.Fn crypto_register . 399The first argument is the driver identifier. 400The second argument is an array of 401.Dv CRYPTO_ALGORITHM_MAX + 1 402elements, indicating which algorithms are supported. 403The last three arguments are pointers to three 404driver-provided functions that the framework may call to establish new 405cryptographic context with the driver, free already established 406context, and ask for a request to be processed (encrypt, decrypt, etc.\&) 407.Fn crypto_unregister 408is called by drivers that wish to withdraw support for an algorithm. 409The two arguments are the driver and algorithm identifiers, respectively. 410Typically, drivers for 411.Xr pcmcia 4 412crypto cards that are being ejected will invoke this routine for all 413algorithms supported by the card. 414If called with 415.Dv CRYPTO_ALGORITHM_MAX + 1 , 416all algorithms registered for a driver will be unregistered in one go 417and the driver will be disabled (no new sessions will be allocated on 418that driver, and any existing sessions will be migrated to other 419drivers). 420The same will be done if all algorithms associated with a driver are 421unregistered one by one. 422.Pp 423The calling convention for the three driver-supplied routines is: 424.Bd -literal 425int (*newsession) (u_int32_t *, struct cryptoini *); 426int (*freesession) (u_int64_t); 427int (*process) (struct cryptop *); 428.Ed 429.Pp 430On invocation, the first argument to 431.Fn newsession 432contains the driver identifier obtained via 433.Fn crypto_get_driverid . 434On successfully returning, it should contain a driver-specific session 435identifier. 436The second argument is identical to that of 437.Fn crypto_newsession . 438.Pp 439The 440.Fn freesession 441routine takes as argument the SID (which is the concatenation of the 442driver identifier and the driver-specific session identifier). 443It should clear any context associated with the session (clear hardware 444registers, memory, etc.). 445.Pp 446The 447.Fn process 448routine is invoked with a request to perform crypto processing. 449This routine must not block, but should queue the request and return 450immediately. 451Upon processing the request, the callback routine should be invoked. 452In case of error, the error indication must be placed in the 453.Fa crp_etype 454field of the 455.Fa cryptop 456structure. 457When the request is completed, or an error is detected, the 458.Fn process 459routine should invoke 460.Fn crypto_done . 461Session migration may be performed, as mentioned previously. 462.Sh RETURN VALUES 463.Fn crypto_register , 464.Fn crypto_unregister , 465.Fn crypto_newsession , 466and 467.Fn crypto_freesession 468return 0 on success, or an error code on failure. 469.Fn crypto_get_driverid 470returns a non-negative value on error, and \-1 on failure. 471.Fn crypto_getreq 472returns a pointer to a 473.Fa cryptop 474structure and 475.Dv NULL 476on failure. 477.Fn crypto_dispatch 478returns 479.Er EINVAL 480if its argument or the callback function was 481.Dv NULL , 482and 0 otherwise. 483The callback is provided with an error code in case of failure, in the 484.Fa crp_etype 485field. 486.Sh FILES 487.Bl -tag -width sys/crypto/crypto.c 488.It Pa sys/crypto/crypto.c 489most of the framework code 490.El 491.Sh SEE ALSO 492.Xr ipsec 4 , 493.Xr pcmcia 4 , 494.Xr malloc 9 , 495.Xr tsleep 9 496.Sh HISTORY 497The cryptographic framework first appeared in 498.Ox 2.7 499and was written by 500.An Angelos D. Keromytis Aq Mt angelos@openbsd.org . 501.Sh BUGS 502The framework currently assumes that all the algorithms in a 503.Fn crypto_newsession 504operation must be available by the same driver. 505If that's not the case, session initialization will fail. 506.Pp 507The framework also needs a mechanism for determining which driver is 508best for a specific set of algorithms associated with a session. 509Some type of benchmarking is in order here. 510.Pp 511Multiple instances of the same algorithm in the same session are not 512supported. 513.Pp 514A queue for completed operations should be implemented and processed 515at some software 516.Xr spl 9 517level, to avoid overall system latency issues, and potential kernel 518stack exhaustion while processing a callback. 519