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