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