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