1.\" $OpenBSD: crypto.9,v 1.13 2001/08/03 15:21:17 mpech 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 "void" 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{ 50 int cri_alg; 51 int cri_klen; 52 int cri_rnd; 53 caddr_t cri_key; 54 u_int8_t cri_iv[EALG_MAX_BLOCK_LEN]; 55 struct cryptoini *cri_next; 56}; 57 58struct cryptodesc 59{ 60 int crd_skip; 61 int crd_len; 62 int crd_inject; 63 int crd_flags; 64 struct cryptoini CRD_INI; 65 struct cryptodesc *crd_next; 66}; 67 68struct cryptop 69{ 70 u_int64_t crp_sid; 71 int crp_ilen; 72 int crp_olen; 73 int crp_alloctype; 74 int crp_etype; 75 int crp_flags; 76 caddr_t crp_buf; 77 caddr_t crp_opaque; 78 struct cryptodesc *crp_desc; 79 int (*crp_callback) (struct cryptop *); 80}; 81.Ed 82.br 83.Sh DESCRIPTION 84.Nm 85is a framework for drivers of cryptographic hardware to register with 86the kernel so 87.Dq consumers 88(other kernel subsystems, and eventually 89users through an appropriate device) are able to make use of it. 90Drivers register with the framework the algorithms they support, 91and provide entry points (functions) the framework may call to 92establish, use, and tear down sessions. 93Sessions are used to cache cryptographic information in a particular driver 94(or associated hardware), so initialization is not needed with every request. 95Consumers of cryptographic services pass a set of 96descriptors that instruct the framework (and the drivers registered 97with it) of the operations that should be applied on the data (more 98than one cryptographic operation can be requested). 99.Pp 100Since the consumers may not be associated with a process, drivers may 101not use 102.Xr tsleep 9 . 103The same holds for the framework. 104Thus, a callback mechanism is used 105to notify a consumer that a request has been completed (the 106callback is specified by the consumer on an per-request basis). 107The callback is invoked by the framework whether the request was 108successfully completed or not. 109An error indication is provided in the latter case. 110A specific error code, 111.Er EAGAIN , 112is used to indicate that a session number has changed and that the 113request may be re-submitted immediately with the new session number. 114Errors are only returned to the invoking function if not 115enough information to call the callback is available (meaning, there 116was a fatal error in verifying the arguments). 117For session initialization and teardown there is no callback mechanism used. 118.Pp 119The 120.Fn crypto_newsession 121routine is called by consumers of cryptographic services (such as the 122.Xr ipsec 4 123stack) that wish to establish a new session with the framework. 124On success, the first argument will contain the Session Identifier (SID). 125The second argument contains all the necessary information for 126the driver to establish the session. 127The various fields in the 128.Fa cryptoini 129structure are: 130.Bl -tag -width foobarmoocow 131.It Fa cri_alg 132Contains an algorithm identifier. 133Currently supported algorithms are: 134.Bd -literal 135CRYPTO_DES_CBC 136CRYPTO_3DES_CBC 137CRYPTO_BLF_CBC 138CRYPTO_CAST_CBC 139CRYPTO_SKIPJACK_CBC 140CRYPTO_MD5_HMAC 141CRYPTO_SHA1_HMAC 142CRYPTO_RIPEMD160_HMAC 143CRYPTO_MD5_KPDK 144CRYPTO_SHA1_KPDK 145.Ed 146.Pp 147.It Fa cri_klen 148Specifies the length of the key in bits, for variable-size key 149algorithms. 150.It Fa cri_rnd 151Specifies the number of rounds to be used with the algorithm, for 152variable-round algorithms. 153.It Fa cri_key 154Contains the key to be used with the algorithm. 155.It Fa cri_iv 156Contains an explicit initialization vector (IV), if it does not prefix 157the data. 158This field is ignored during initialization. 159If no IV is explicitly passed (see below on details), a random IV is used 160by the device driver processing the request. 161.It Fa cri_next 162Contains a pointer to another 163.Fa cryptoini 164structure. 165Multiple such structures may be linked to establish multi-algorithm sessions 166.Pf ( Xr ipsec 4 167is an example consumer of such a feature). 168.El 169.Pp 170The 171.Fa cryptoini 172structure and its contents will not be modified by the framework (or 173the drivers used). 174Subsequent requests for processing that use the 175SID returned will avoid the cost of re-initializing the hardware (in 176essence, SID acts as an index in the session cache of the driver). 177.Pp 178.Fn crypto_freesession 179is called with the SID returned by 180.Fn crypto_newsession 181to disestablish the session. 182.Pp 183.Fn crypto_dispatch 184is called to process a request. 185The various fields in the 186.Fa cryptop 187structure are: 188.Bl -tag -width crp_alloctype 189.It Fa crp_sid 190Contains the SID. 191.It Fa crp_ilen 192Indicates the total length in bytes of the buffer to be processed. 193.It Fa crp_olen 194On return, contains the total length of the result. 195For symmetric crypto operations, this will be the same as the input length. 196.It Fa crp_alloctype 197Indicates the type of buffer, as used in the kernel 198.Xr malloc 9 199routine. 200This will be used if the framework needs to allocate a new 201buffer for the result (or for re-formatting the input). 202.It Fa crp_callback 203This routine is invoked upon completion of the request, whether 204successful or not. 205It is invoked through the 206.Fn crypto_done 207routine. 208If the request was not successful, an error code is set in the 209.Fa crp_etype 210field. 211It is the responsibility of the callback routine to set the appropriate 212.Xr spl 9 213level. 214.It Fa crp_etype 215Contains the error type, if any errors were encountered, or zero if 216the request was successfully processed. 217If the 218.Er EAGAIN 219error code is returned, the SID has changed (and has been recorded in the 220.Fa crp_sid 221field). 222The consumer should record the new SID and use it in all subsequent requests. 223In this case, the request may be re-submitted immediately. 224This mechanism is used by the framework to perform 225session migration (move a session from one driver to another, because 226of availability, performance, or other considerations). 227.Pp 228Note that this field only makes sense when examined by 229the callback routine specified in 230.Fa crp_callback . 231Errors are returned to the invoker of 232.Fn crypto_process 233only when enough information is not present to call the callback 234routine (i.e., if the pointer passed is 235.Dv NULL 236or if no callback routine was specified). 237.It Fa crp_flags 238Is a bitmask of flags associated with this request. 239Currently defined flags are: 240.Bl -tag -width CRYPTO_F_IMBUF 241.It Dv CRYPTO_F_IMBUF 242The buffer pointed to by 243.Fa crp_buf 244is an mbuf chain. 245.El 246.Pp 247.It Fa crp_buf 248Points to the input buffer. 249On return (when the callback is invoked), 250it contains the result of the request. 251The input buffer may be an mbuf 252chain or a contiguous buffer (of a type identified by 253.Fa crp_alloctype ) , 254depending on 255.Fa crp_flags . 256.It Fa crp_opaque 257This is passed through the crypto framework untouched and is 258intended for the invoking application's use. 259.It Fa crp_desc 260This is a linked list of descriptors. 261Each descriptor provides 262information about what type of cryptographic operation should be done 263on the input buffer. 264The various fields are: 265.Bl -tag -width=crd_inject 266.It Fa crd_skip 267The offset in the input buffer where processing should start. 268.It Fa crd_len 269How many bytes, after 270.Fa Fa crd_skip , 271should be processed. 272.It Fa crd_inject 273Offset from the beginning of the buffer to insert any results. 274For encryption algorithms, this is where the initialization vector 275(IV) will be inserted when encrypting or where it can be found when 276decrypting (subject to 277.Fa Fa crd_flags ) . 278For MAC algorithms, this is where the result of the keyed hash will be 279inserted. 280.It Fa crd_flags 281The following flags are defined: 282.Bl -tag -width CRD_F_IV_EXPLICIT 283.It Dv CRD_F_ENCRYPT 284For encryption algorithms, this bit is set when encryption is required 285(when not set, decryption is performed). 286.It Dv CRD_F_IV_PRESENT 287For encryption algorithms, this bit is set when the IV already 288precedes the data, so the 289.Fa crd_inject 290value will be ignored and no IV will be written in the buffer. 291Otherwise, the IV used to encrypt the packet will be written 292at the location pointed to by 293.Fa crd_inject . 294The IV length is assumed to be equal to the blocksize of the 295encryption algorithm. 296Some applications that do special 297.Dq IV cooking , 298such as the half-IV mode in 299.Xr ipsec 4 , 300can use this flag to indicate the the IV should not be written on the packet. 301This flag is typically used in conjunction with the 302.Dv CRD_F_IV_EXPLICIT 303flag. 304.It Dv CRD_F_IV_EXPLICIT 305For encryption algorithms, this bit is set when the IV is explicitly 306provided by the consumer in the 307.Fa crd_iv 308fields. 309Otherwise, for encryption operations the IV is provided for by 310the driver used to perform the operation, whereas for decryption 311operations it is pointed to by the 312.Fa crd_inject 313field. 314This flag is typically used when the IV is calculated 315.Dq on the fly 316by the consumer, and does not precede the data (some 317.Xr ipsec 4 318configurations, and the encrypted swap are two such examples). 319.El 320.It Xo Fa crd_alg , crd_klen , crd_rnd , 321.Fa crd_key , crd_iv 322.Xc 323These have the exact same meaning as the corresponding fields in the 324.Fa cryptoini 325structure. 326These fields will not be modified by the framework or the device drivers. 327Since this information accompanies every cryptographic 328operation request, drivers may re-initialize state on-demand 329(typically an expensive operation). 330Furthermore, the cryptographic 331framework may re-route requests as a result of full queues or hardware 332failure, as described above. 333.It Fa crd_next 334Point to the next descriptor. 335Linked operations are useful in protocols such as 336.Xr ipsec 4 , 337where multiple cryptographic transforms may be applied on the same 338block of data. 339.El 340.El 341.Pp 342.Fn crypto_getreq 343allocates a 344.Fa cryptop 345structure with a linked list of as many 346.Fa cryptodesc 347structures as were specified in the argument passed to it. 348.Pp 349.Fn crypto_freereq 350deallocates a structure 351.Fa cryptop 352and any 353.Fa cryptodesc 354structures linked to it. 355Note that it is the responsibility of the 356callback routine to do the necessary cleanups associated with the 357opaque field in the 358.Fa cryptop 359structure. 360.Sh DRIVER-SIDE API 361The 362.Fn crypto_get_driverid , 363.Fn crypto_register , 364.Fn crypto_unregister , 365and 366.Fn crypto_done 367routines are used by drivers that provide support for cryptographic 368primitives to register and unregister with the kernel crypto services 369framework. 370Drivers must first use the 371.Fn crypto_get_driverid 372function to acquire a driver identifier. 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