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