1 .\" $OpenBSD: crypto.9,v 1.19 2002/07/16 06:31:57 angelos Exp $
3 .\" The author of this manual page is Angelos D. Keromytis (angelos@cis.upenn.edu)
5 .\" Copyright (c) 2000, 2001 Angelos D. Keromytis
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.
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
18 .\" $FreeBSD: src/share/man/man9/crypto.9,v 1.14 2007/09/19 16:28:46 brueffer Exp $
25 .Nd API for cryptographic services in the kernel
29 .In opencrypto/cryptodev.h
31 .Fn crypto_get_driverid "device_t dev" "int flags"
33 .Fn crypto_find_driver "const char *match"
35 .Fn crypto_find_device_byhid "int hid"
37 .Fn crypto_getcaps "int hid"
39 .Fn crypto_register "u_int32_t driverid" "int alg" "u_int16_t maxoplen" "u_int32_t flags"
41 .Fn crypto_kregister "u_int32_t driverid" "int kalg" "u_int32_t flags"
43 .Fn crypto_unregister "u_int32_t driverid" "int alg"
45 .Fn crypto_unregister_all "u_int32_t driverid"
47 .Fn crypto_done "struct cryptop *crp"
49 .Fn crypto_kdone "struct cryptkop *krp"
51 .Fn crypto_newsession "u_int64_t *sid" "struct cryptoini *cri" "int crid"
53 .Fn crypto_freesession "u_int64_t sid"
55 .Fn crypto_dispatch "struct cryptop *crp"
57 .Fn crypto_kdispatch "struct cryptkop *krp"
59 .Fn crypto_unblock "u_int32_t driverid" "int what"
60 .Ft "struct cryptop *"
61 .Fn crypto_getreq "int num"
63 .Fn crypto_freereq "struct cryptop *crp"
65 #define CRYPTO_SYMQ 0x1
66 #define CRYPTO_ASYMQ 0x2
68 #define EALG_MAX_BLOCK_LEN 16
75 u_int8_t cri_iv[EALG_MAX_BLOCK_LEN];
76 struct cryptoini *cri_next;
84 struct cryptoini CRD_INI;
85 #define crd_iv CRD_INI.cri_iv
86 #define crd_key CRD_INI.cri_key
87 #define crd_alg CRD_INI.cri_alg
88 #define crd_klen CRD_INI.cri_klen
89 struct cryptodesc *crd_next;
93 TAILQ_ENTRY(cryptop) crp_next;
101 struct cryptodesc *crp_desc;
102 int (*crp_callback) (struct cryptop *);
111 #define CRK_MAXPARAM 8
114 TAILQ_ENTRY(cryptkop) krp_next;
115 u_int krp_op; /* ie. CRK_MOD_EXP or other */
116 u_int krp_status; /* return status */
117 u_short krp_iparams; /* # of input parameters */
118 u_short krp_oparams; /* # of output parameters */
120 struct crparam krp_param[CRK_MAXPARAM];
121 int (*krp_callback)(struct cryptkop *);
126 is a framework for drivers of cryptographic hardware to register with
129 (other kernel subsystems, and
132 device) are able to make use of it.
133 Drivers register with the framework the algorithms they support,
134 and provide entry points (functions) the framework may call to
135 establish, use, and tear down sessions.
136 Sessions are used to cache cryptographic information in a particular driver
137 (or associated hardware), so initialization is not needed with every request.
138 Consumers of cryptographic services pass a set of
139 descriptors that instruct the framework (and the drivers registered
140 with it) of the operations that should be applied on the data (more
141 than one cryptographic operation can be requested).
143 Keying operations are supported as well.
144 Unlike the symmetric operators described above,
145 these sessionless commands perform mathematical operations using
146 input and output parameters.
148 Since the consumers may not be associated with a process, drivers may
151 The same holds for the framework.
152 Thus, a callback mechanism is used
153 to notify a consumer that a request has been completed (the
154 callback is specified by the consumer on an per-request basis).
155 The callback is invoked by the framework whether the request was
156 successfully completed or not.
157 An error indication is provided in the latter case.
158 A specific error code,
160 is used to indicate that a session number has changed and that the
161 request may be re-submitted immediately with the new session number.
162 Errors are only returned to the invoking function if not
163 enough information to call the callback is available (meaning, there
164 was a fatal error in verifying the arguments).
165 For session initialization and teardown there is no callback mechanism used.
168 .Fn crypto_newsession
169 routine is called by consumers of cryptographic services (such as the
171 stack) that wish to establish a new session with the framework.
172 On success, the first argument will contain the Session Identifier (SID).
173 The second argument contains all the necessary information for
174 the driver to establish the session.
175 The third argument indicates whether a
176 hardware driver (1) should be used or not (0).
177 The various fields in the
180 .Bl -tag -width ".Va cri_next"
182 Contains an algorithm identifier.
183 Currently supported algorithms are:
185 .Bl -tag -width ".Dv CRYPTO_RIPEMD160_HMAC" -compact
186 .It Dv CRYPTO_AES_CBC
187 .It Dv CRYPTO_AES_XTS
188 .It Dv CRYPTO_AES_CTR
189 .It Dv CRYPTO_AES_GCM
190 .It Dv CRYPTO_AES_GMAC
191 .It Dv CRYPTO_AES_128_GMAC
192 .It Dv CRYPTO_AES_192_GMAC
193 .It Dv CRYPTO_AES_256_GMAC
194 .It Dv CRYPTO_TWOFISH_CBC
195 .It Dv CRYPTO_TWOFISH_XTS
196 .It Dv CRYPTO_SERPENT_CBC
197 .It Dv CRYPTO_SERPENT_XTS
199 .It Dv CRYPTO_BLF_CBC
200 .It Dv CRYPTO_CAMELLIA_CBC
201 .It Dv CRYPTO_CAST_CBC
202 .It Dv CRYPTO_DES_CBC
203 .It Dv CRYPTO_3DES_CBC
204 .It Dv CRYPTO_SKIPJACK_CBC
206 .It Dv CRYPTO_MD5_HMAC
207 .It Dv CRYPTO_MD5_KPDK
208 .It Dv CRYPTO_RIPEMD160_HMAC
210 .It Dv CRYPTO_SHA1_HMAC
211 .It Dv CRYPTO_SHA1_KPDK
212 .It Dv CRYPTO_SHA2_256_HMAC
213 .It Dv CRYPTO_SHA2_384_HMAC
214 .It Dv CRYPTO_SHA2_512_HMAC
215 .It Dv CRYPTO_NULL_HMAC
216 .It Dv CRYPTO_NULL_CBC
219 Specifies the length of the key in bits, for variable-size key
222 Specifies how many bytes from the calculated hash should be copied back.
225 Contains the key to be used with the algorithm.
227 Contains an explicit initialization vector (IV), if it does not prefix
229 This field is ignored during initialization.
230 If no IV is explicitly passed (see below on details), a random IV is used
231 by the device driver processing the request.
233 Contains a pointer to another
236 Multiple such structures may be linked to establish multi-algorithm sessions
238 is an example consumer of such a feature).
243 structure and its contents will not be modified by the framework (or
245 Subsequent requests for processing that use the
246 SID returned will avoid the cost of re-initializing the hardware (in
247 essence, SID acts as an index in the session cache of the driver).
249 .Fn crypto_freesession
250 is called with the SID returned by
251 .Fn crypto_newsession
252 to disestablish the session.
255 is called to process a request.
256 The various fields in the
259 .Bl -tag -width ".Va crp_callback"
263 Indicates the total length in bytes of the buffer to be processed.
265 On return, contains the total length of the result.
266 For symmetric crypto operations, this will be the same as the input length.
267 This will be used if the framework needs to allocate a new
268 buffer for the result (or for re-formatting the input).
270 This routine is invoked upon completion of the request, whether
272 It is invoked through the
275 If the request was not successful, an error code is set in the
278 It is the responsibility of the callback routine to enter a critical
281 Contains the error type, if any errors were encountered, or zero if
282 the request was successfully processed.
285 error code is returned, the SID has changed (and has been recorded in the
288 The consumer should record the new SID and use it in all subsequent requests.
289 In this case, the request may be re-submitted immediately.
290 This mechanism is used by the framework to perform
291 session migration (move a session from one driver to another, because
292 of availability, performance, or other considerations).
294 Note that this field only makes sense when examined by
295 the callback routine specified in
297 Errors are returned to the invoker of
299 only when enough information is not present to call the callback
300 routine (i.e., if the pointer passed is
302 or if no callback routine was specified).
304 Is a bitmask of flags associated with this request.
305 Currently defined flags are:
306 .Bl -tag -width ".Dv CRYPTO_F_CBIFSYNC"
307 .It Dv CRYPTO_F_IMBUF
308 The buffer pointed to by
312 The buffer pointed to by
318 Must return data in the same place.
319 .It Dv CRYPTO_F_BATCH
320 Batch operation if possible.
321 .It Dv CRYPTO_F_CBIMM
322 Do callback immediately instead of doing it from a dedicated kernel thread.
325 .It Dv CRYPTO_F_CBIFSYNC
326 Do callback immediately if operation is synchronous.
329 Points to the input buffer.
330 On return (when the callback is invoked),
331 it contains the result of the request.
332 The input buffer may be an mbuf
333 chain or a contiguous buffer,
337 This is passed through the crypto framework untouched and is
338 intended for the invoking application's use.
340 This is a linked list of descriptors.
341 Each descriptor provides
342 information about what type of cryptographic operation should be done
344 The various fields are:
345 .Bl -tag -width ".Va crd_inject"
347 The field where IV should be provided when the
348 .Dv CRD_F_IV_EXPLICIT
352 .Dv CRD_F_KEY_EXPLICIT
355 points to a buffer with encryption or authentication key.
358 Must be the same as the one given at newsession time.
364 The offset in the input buffer where processing should start.
366 How many bytes, after
370 Offset from the beginning of the buffer to insert any results.
371 For encryption algorithms, this is where the initialization vector
372 (IV) will be inserted when encrypting or where it can be found when
373 decrypting (subject to
375 For MAC algorithms, this is where the result of the keyed hash will be
378 The following flags are defined:
381 For encryption algorithms, this bit is set when encryption is required
382 (when not set, decryption is performed).
383 .It Dv CRD_F_IV_PRESENT
384 For encryption algorithms, this bit is set when the IV already
385 precedes the data, so the
387 value will be ignored and no IV will be written in the buffer.
388 Otherwise, the IV used to encrypt the packet will be written
389 at the location pointed to by
391 The IV length is assumed to be equal to the blocksize of the
392 encryption algorithm.
393 Some applications that do special
395 such as the half-IV mode in
397 can use this flag to indicate that the IV should not be written on the packet.
398 This flag is typically used in conjunction with the
399 .Dv CRD_F_IV_EXPLICIT
401 .It Dv CRD_F_IV_EXPLICIT
402 For encryption algorithms, this bit is set when the IV is explicitly
403 provided by the consumer in the
406 Otherwise, for encryption operations the IV is provided for by
407 the driver used to perform the operation, whereas for decryption
408 operations it is pointed to by the
411 This flag is typically used when the IV is calculated
413 by the consumer, and does not precede the data (some
415 configurations, and the encrypted swap are two such examples).
416 .It Dv CRD_F_KEY_EXPLICIT
417 For encryption and authentication (MAC) algorithms, this bit is set when the key
418 is explicitly provided by the consumer in the
420 field for the given operation.
421 Otherwise, the key is taken at newsession time from the
425 For compression algorithms, this bit is set when compression is required (when
426 not set, decompression is performed).
431 structure will not be modified by the framework or the device drivers.
432 Since this information accompanies every cryptographic
433 operation request, drivers may re-initialize state on-demand
434 (typically an expensive operation).
435 Furthermore, the cryptographic
436 framework may re-route requests as a result of full queues or hardware
437 failure, as described above.
439 Point to the next descriptor.
440 Linked operations are useful in protocols such as
442 where multiple cryptographic transforms may be applied on the same
450 structure with a linked list of as many
452 structures as were specified in the argument passed to it.
455 deallocates a structure
459 structures linked to it.
460 Note that it is the responsibility of the
461 callback routine to do the necessary cleanups associated with the
467 is called to perform a keying operation.
468 The various fields in the
471 .Bl -tag -width ".Va krp_callback"
473 Operation code, such as
479 variable indicates whether lower level reasons
480 for operation failure.
482 Number if input parameters to the specified operation.
483 Note that each operation has a (typically hardwired) number of such parameters.
485 Number if output parameters from the specified operation.
486 Note that each operation has a (typically hardwired) number of such parameters.
488 An array of kernel memory blocks containing the parameters.
490 Identifier specifying which low-level driver is being used.
492 Callback called on completion of a keying operation.
496 .Fn crypto_get_driverid ,
497 .Fn crypto_register ,
498 .Fn crypto_kregister ,
499 .Fn crypto_unregister ,
503 routines are used by drivers that provide support for cryptographic
504 primitives to register and unregister with the kernel crypto services
506 Drivers must first use the
507 .Fn crypto_get_driverid
508 function to acquire a driver identifier, specifying the
510 as an argument (normally 0, but software-only drivers should specify
511 .Dv CRYPTOCAP_F_SOFTWARE ) .
512 For each algorithm the driver supports, it must then call
513 .Fn crypto_register .
514 The first two arguments are the driver and algorithm identifiers.
515 The next two arguments specify the largest possible operator length (in bits,
516 important for public key operations) and flags for this algorithm.
517 The last four arguments must be provided in the first call to
519 and are ignored in all subsequent calls.
520 They are pointers to three
521 driver-provided functions that the framework may call to establish new
522 cryptographic context with the driver, free already established
523 context, and ask for a request to be processed (encrypt, decrypt,
524 etc.); and an opaque parameter to pass when calling each of these routines.
525 .Fn crypto_unregister
526 is called by drivers that wish to withdraw support for an algorithm.
527 The two arguments are the driver and algorithm identifiers, respectively.
528 Typically, drivers for
530 crypto cards that are being ejected will invoke this routine for all
531 algorithms supported by the card.
532 .Fn crypto_unregister_all
533 will unregister all algorithms registered by a driver
534 and the driver will be disabled (no new sessions will be allocated on
535 that driver, and any existing sessions will be migrated to other
537 The same will be done if all algorithms associated with a driver are
538 unregistered one by one.
540 The calling convention for the three driver-supplied routines is:
545 .Fn \*[lp]*newsession\*[rp] "void *" "u_int32_t *" "struct cryptoini *" ;
548 .Fn \*[lp]*freesession\*[rp] "void *" "u_int64_t" ;
551 .Fn \*[lp]*process\*[rp] "void *" "struct cryptop *" ;
554 .Fn \*[lp]*kprocess\*[rp] "void *" "struct cryptkop *" ;
557 On invocation, the first argument to
558 all routines is an opaque data value supplied when the algorithm
560 .Fn crypto_register .
561 The second argument to
563 contains the driver identifier obtained via
564 .Fn crypto_get_driverid .
565 On successful return, it should contain a driver-specific session
567 The third argument is identical to that of
568 .Fn crypto_newsession .
572 routine takes as arguments the opaque data value and the SID
573 (which is the concatenation of the
574 driver identifier and the driver-specific session identifier).
575 It should clear any context associated with the session (clear hardware
576 registers, memory, etc.).
580 routine is invoked with a request to perform crypto processing.
581 This routine must not block, but should queue the request and return
583 Upon processing the request, the callback routine should be invoked.
584 In case of an unrecoverable error, the error indication must be placed in the
589 When the request is completed, or an error is detected, the
591 routine should invoke
593 Session migration may be performed, as mentioned previously.
595 In case of a temporary resource exhaustion, the
599 in which case the crypto services will requeue the request, mark the driver
602 and stop submitting requests for processing.
603 The driver is then responsible for notifying the crypto services
604 when it is again able to process requests through the
607 This simple flow control mechanism should only be used for short-lived
608 resource exhaustion as it causes operations to be queued in the crypto
610 Doing so is preferable to returning an error in such cases as
611 it can cause network protocols to degrade performance by treating the
612 failure much like a lost packet.
616 routine is invoked with a request to perform crypto key processing.
617 This routine must not block, but should queue the request and return
619 Upon processing the request, the callback routine should be invoked.
620 In case of an unrecoverable error, the error indication must be placed in the
625 When the request is completed, or an error is detected, the
627 routine should invoked
630 .Fn crypto_register ,
631 .Fn crypto_kregister ,
632 .Fn crypto_unregister ,
633 .Fn crypto_newsession ,
634 .Fn crypto_freesession ,
637 return 0 on success, or an error code on failure.
638 .Fn crypto_get_driverid
639 returns a non-negative value on error, and \-1 on failure.
641 returns a pointer to a
649 if its argument or the callback function was
652 The callback is provided with an error code in case of failure, in the
656 .Bl -tag -width ".Pa sys/opencrypto/crypto.c"
657 .It Pa sys/opencrypto/crypto.c
658 most of the framework code
665 The cryptographic framework first appeared in
668 .An "Angelos D. Keromytis" Aq angelos@openbsd.org .
670 The framework currently assumes that all the algorithms in a
671 .Fn crypto_newsession
672 operation must be available by the same driver.
673 If that is not the case, session initialization will fail.
675 The framework also needs a mechanism for determining which driver is
676 best for a specific set of algorithms associated with a session.
677 Some type of benchmarking is in order here.
679 Multiple instances of the same algorithm in the same session are not
681 Note that 3DES is considered one algorithm (and not three
683 Thus, 3DES and DES could be mixed in the same request.