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