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
25 .Nd API for cryptographic services in the kernel
27 .In opencrypto/cryptodev.h
29 .Fn crypto_get_driverid device_t size_t 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 *"
33 .Fn crypto_kregister uint32_t int uint32_t "int \*[lp]*\*[rp]\*[lp]void *, struct cryptkop *\*[rp]" "void *"
35 .Fn crypto_unregister uint32_t int
37 .Fn crypto_unregister_all uint32_t
39 .Fn crypto_done "struct cryptop *"
41 .Fn crypto_kdone "struct cryptkop *"
43 .Fn crypto_find_driver "const char *"
45 .Fn crypto_newsession "crypto_session_t *" "struct cryptoini *" int
47 .Fn crypto_freesession crypto_session_t
49 .Fn crypto_dispatch "struct cryptop *"
51 .Fn crypto_kdispatch "struct cryptkop *"
53 .Fn crypto_unblock uint32_t int
54 .Ft "struct cryptop *"
57 .Fn crypto_freereq void
59 #define CRYPTO_SYMQ 0x1
60 #define CRYPTO_ASYMQ 0x2
62 #define EALG_MAX_BLOCK_LEN 16
69 uint8_t cri_iv[EALG_MAX_BLOCK_LEN];
70 struct cryptoini *cri_next;
78 struct cryptoini CRD_INI;
79 #define crd_iv CRD_INI.cri_iv
80 #define crd_key CRD_INI.cri_key
81 #define crd_alg CRD_INI.cri_alg
82 #define crd_klen CRD_INI.cri_klen
83 struct cryptodesc *crd_next;
87 TAILQ_ENTRY(cryptop) crp_next;
88 crypto_session_t crp_session;
95 struct cryptodesc *crp_desc;
96 int (*crp_callback) (struct cryptop *);
105 #define CRK_MAXPARAM 8
108 TAILQ_ENTRY(cryptkop) krp_next;
109 u_int krp_op; /* ie. CRK_MOD_EXP or other */
110 u_int krp_status; /* return status */
111 u_short krp_iparams; /* # of input parameters */
112 u_short krp_oparams; /* # of output parameters */
114 struct crparam krp_param[CRK_MAXPARAM];
115 int (*krp_callback)(struct cryptkop *);
120 is a framework for drivers of cryptographic hardware to register with
123 (other kernel subsystems, and
126 device) are able to make use of it.
127 Drivers register with the framework the algorithms they support,
128 and provide entry points (functions) the framework may call to
129 establish, use, and tear down sessions.
130 Sessions are used to cache cryptographic information in a particular driver
131 (or associated hardware), so initialization is not needed with every request.
132 Consumers of cryptographic services pass a set of
133 descriptors that instruct the framework (and the drivers registered
134 with it) of the operations that should be applied on the data (more
135 than one cryptographic operation can be requested).
137 Keying operations are supported as well.
138 Unlike the symmetric operators described above,
139 these sessionless commands perform mathematical operations using
140 input and output parameters.
142 Since the consumers may not be associated with a process, drivers may
145 The same holds for the framework.
146 Thus, a callback mechanism is used
147 to notify a consumer that a request has been completed (the
148 callback is specified by the consumer on a per-request basis).
149 The callback is invoked by the framework whether the request was
150 successfully completed or not.
151 An error indication is provided in the latter case.
152 A specific error code,
154 is used to indicate that a session handle has changed and that the
155 request may be re-submitted immediately with the new session.
156 Errors are only returned to the invoking function if not
157 enough information to call the callback is available (meaning, there
158 was a fatal error in verifying the arguments).
159 For session initialization and teardown no callback mechanism is used.
162 .Fn crypto_find_driver
163 function may be called to return the specific id of the provided name.
164 If the specified driver could not be found, the returned id is -1.
167 .Fn crypto_newsession
168 routine is called by consumers of cryptographic services (such as the
170 stack) that wish to establish a new session with the framework.
171 The second argument contains all the necessary information for
172 the driver to establish the session.
173 The third argument is either a specific driver id, or one or both
175 .Dv CRYPTOCAP_F_HARDWARE ,
176 to select hardware devices,
178 .Dv CRYPTOCAP_F_SOFTWARE ,
179 to select software devices.
180 If both are specified, a hardware device will be returned
181 before a software device will be.
182 On success, the value pointed to by the first argument will be the opaque
184 The various fields in the
187 .Bl -tag -width ".Va cri_next"
189 Contains an algorithm identifier.
190 Currently supported algorithms are:
192 .Bl -tag -width ".Dv CRYPTO_RIPEMD160_HMAC" -compact
193 .It Dv CRYPTO_AES_128_NIST_GMAC
194 .It Dv CRYPTO_AES_192_NIST_GMAC
195 .It Dv CRYPTO_AES_256_NIST_GMAC
196 .It Dv CRYPTO_AES_CBC
197 .It Dv CRYPTO_AES_ICM
198 .It Dv CRYPTO_AES_NIST_GCM_16
199 .It Dv CRYPTO_AES_NIST_GMAC
200 .It Dv CRYPTO_AES_XTS
202 .It Dv CRYPTO_BLF_CBC
203 .It Dv CRYPTO_CAMELLIA_CBC
204 .It Dv CRYPTO_CAST_CBC
205 .It Dv CRYPTO_DEFLATE_COMP
206 .It Dv CRYPTO_DES_CBC
207 .It Dv CRYPTO_3DES_CBC
209 .It Dv CRYPTO_MD5_HMAC
210 .It Dv CRYPTO_MD5_KPDK
211 .It Dv CRYPTO_NULL_HMAC
212 .It Dv CRYPTO_NULL_CBC
213 .It Dv CRYPTO_RIPEMD160_HMAC
215 .It Dv CRYPTO_SHA1_HMAC
216 .It Dv CRYPTO_SHA1_KPDK
217 .It Dv CRYPTO_SHA2_256_HMAC
218 .It Dv CRYPTO_SHA2_384_HMAC
219 .It Dv CRYPTO_SHA2_512_HMAC
220 .It Dv CRYPTO_SKIPJACK_CBC
223 Specifies the length of the key in bits, for variable-size key
226 Specifies how many bytes from the calculated hash should be copied back.
229 Contains the key to be used with the algorithm.
231 Contains an explicit initialization vector (IV), if it does not prefix
233 This field is ignored during initialization
234 .Pq Nm crypto_newsession .
235 If no IV is explicitly passed (see below on details), a random IV is used
236 by the device driver processing the request.
238 Contains a pointer to another
241 Multiple such structures may be linked to establish multi-algorithm sessions
243 is an example consumer of such a feature).
248 structure and its contents will not be modified by the framework (or
251 .Fn crypto_freesession
252 is called with the session handle returned by
253 .Fn crypto_newsession
257 is called to process a request.
258 The various fields in the
261 .Bl -tag -width ".Va crp_callback"
263 Contains the session handle.
265 Indicates the total length in bytes of the buffer to be processed.
267 On return, contains the total length of the result.
268 For symmetric crypto operations, this will be the same as the input length.
269 This will be used if the framework needs to allocate a new
270 buffer for the result (or for re-formatting the input).
272 This routine is invoked upon completion of the request, whether
274 It is invoked through the
277 If the request was not successful, an error code is set in the
280 It is the responsibility of the callback routine to set the appropriate
284 Contains the error type, if any errors were encountered, or zero if
285 the request was successfully processed.
288 error code is returned, the session handle has changed (and has been recorded
292 The consumer should record the new session handle and use it in all subsequent
294 In this case, the request may be re-submitted immediately.
295 This mechanism is used by the framework to perform
296 session migration (move a session from one driver to another, because
297 of availability, performance, or other considerations).
299 Note that this field only makes sense when examined by
300 the callback routine specified in
302 Errors are returned to the invoker of
304 only when enough information is not present to call the callback
305 routine (i.e., if the pointer passed is
307 or if no callback routine was specified).
309 Is a bitmask of flags associated with this request.
310 Currently defined flags are:
311 .Bl -tag -width ".Dv CRYPTO_F_CBIFSYNC"
312 .It Dv CRYPTO_F_IMBUF
313 The buffer pointed to by
317 The buffer pointed to by
322 .It Dv CRYPTO_F_BATCH
323 Batch operation if possible.
324 .It Dv CRYPTO_F_CBIMM
325 Do callback immediately instead of doing it from a dedicated kernel thread.
328 .It Dv CRYPTO_F_CBIFSYNC
329 Do callback immediately if operation is synchronous (that the driver
333 .It Dv CRYPTO_F_ASYNC
334 Try to do the crypto operation in a pool of workers
335 if the operation is synchronous (that is, if the driver specified the
338 It aims to speed up processing by dispatching crypto operations
339 on different processors.
340 .It Dv CRYPTO_F_ASYNC_KEEPORDER
341 Dispatch callbacks in the same order they are posted.
344 flag is set and if the operation is synchronous.
347 Points to the input buffer.
348 On return (when the callback is invoked),
349 it contains the result of the request.
350 The input buffer may be an mbuf
351 chain or a contiguous buffer,
355 This is passed through the crypto framework untouched and is
356 intended for the invoking application's use.
358 This is a linked list of descriptors.
359 Each descriptor provides
360 information about what type of cryptographic operation should be done
362 The various fields are:
363 .Bl -tag -width ".Va crd_inject"
366 .Dv CRD_F_IV_EXPLICIT
367 is set, this field contains the IV.
370 .Dv CRD_F_KEY_EXPLICIT
373 points to a buffer with encryption or authentication key.
376 Must be the same as the one given at newsession time.
382 The offset in the input buffer where processing should start.
384 How many bytes, after
390 field specifies an offset in bytes from the beginning of the buffer.
391 For encryption algorithms, this may be where the IV will be inserted
392 when encrypting or where the IV may be found for
393 decryption (subject to
395 For MAC algorithms, this is where the result of the keyed hash will be
398 The following flags are defined:
401 For encryption algorithms, this bit is set when encryption is required
402 (when not set, decryption is performed).
403 .It Dv CRD_F_IV_PRESENT
404 .\" This flag name has nothing to do w/ it's behavior, fix the name.
405 For encryption, if this bit is not set the IV used to encrypt the packet
406 will be written at the location pointed to by
408 The IV length is assumed to be equal to the blocksize of the
409 encryption algorithm.
410 For encryption, if this bit is set, nothing is done.
411 For decryption, this flag has no meaning.
412 Applications that do special
414 such as the half-IV mode in
416 can use this flag to indicate that the IV should not be written on the packet.
417 This flag is typically used in conjunction with the
418 .Dv CRD_F_IV_EXPLICIT
420 .It Dv CRD_F_IV_EXPLICIT
421 This bit is set when the IV is explicitly
422 provided by the consumer in the
425 Otherwise, for encryption operations the IV is provided for by
426 the driver used to perform the operation, whereas for decryption
427 operations the offset of the IV is provided by the
430 This flag is typically used when the IV is calculated
432 by the consumer, and does not precede the data (some
434 configurations, and the encrypted swap are two such examples).
435 .It Dv CRD_F_KEY_EXPLICIT
436 For encryption and authentication (MAC) algorithms, this bit is set when the key
437 is explicitly provided by the consumer in the
439 field for the given operation.
440 Otherwise, the key is taken at newsession time from the
443 As calculating the key schedule may take a while, it is recommended that often
444 used keys are given their own session.
446 For compression algorithms, this bit is set when compression is required (when
447 not set, decompression is performed).
452 structure will not be modified by the framework or the device drivers.
453 Since this information accompanies every cryptographic
454 operation request, drivers may re-initialize state on-demand
455 (typically an expensive operation).
456 Furthermore, the cryptographic
457 framework may re-route requests as a result of full queues or hardware
458 failure, as described above.
460 Point to the next descriptor.
461 Linked operations are useful in protocols such as
463 where multiple cryptographic transforms may be applied on the same
471 structure with a linked list of as many
473 structures as were specified in the argument passed to it.
476 deallocates a structure
480 structures linked to it.
481 Note that it is the responsibility of the
482 callback routine to do the necessary cleanups associated with the
488 is called to perform a keying operation.
489 The various fields in the
492 .Bl -tag -width ".Va krp_callback"
494 Operation code, such as
500 variable indicates whether lower level reasons
501 for operation failure.
503 Number if input parameters to the specified operation.
504 Note that each operation has a (typically hardwired) number of such parameters.
506 Number if output parameters from the specified operation.
507 Note that each operation has a (typically hardwired) number of such parameters.
509 An array of kernel memory blocks containing the parameters.
511 Identifier specifying which low-level driver is being used.
513 Callback called on completion of a keying operation.
517 .Fn crypto_get_driverid ,
518 .Fn crypto_get_driver_session ,
519 .Fn crypto_register ,
520 .Fn crypto_kregister ,
521 .Fn crypto_unregister ,
525 routines are used by drivers that provide support for cryptographic
526 primitives to register and unregister with the kernel crypto services
529 Drivers must first use the
530 .Fn crypto_get_driverid
531 function to acquire a driver identifier, specifying the
535 .Dv CRYPTOCAP_F_SOFTWARE
537 .Dv CRYPTOCAP_F_HARDWARE
541 may also be specified, and should be specified if the driver does all of
542 it's operations synchronously.
543 Drivers must pass the size of their session struct as the second argument.
544 An appropriately sized memory will be allocated by the framework, zeroed, and
545 passed to the driver's
549 For each algorithm the driver supports, it must then call
550 .Fn crypto_register .
551 The first two arguments are the driver and algorithm identifiers.
552 The next two arguments specify the largest possible operator length (in bits,
553 important for public key operations) and flags for this algorithm.
554 The last four arguments must be provided in the first call to
556 and are ignored in all subsequent calls.
557 They are pointers to three
558 driver-provided functions that the framework may call to establish new
559 cryptographic context with the driver, free already established
560 context, and ask for a request to be processed (encrypt, decrypt,
561 etc.); and an opaque parameter to pass when calling each of these routines.
563 .Fn crypto_unregister
564 is called by drivers that wish to withdraw support for an algorithm.
565 The two arguments are the driver and algorithm identifiers, respectively.
566 Typically, drivers for
568 crypto cards that are being ejected will invoke this routine for all
569 algorithms supported by the card.
570 .Fn crypto_unregister_all
571 will unregister all algorithms registered by a driver
572 and the driver will be disabled (no new sessions will be allocated on
573 that driver, and any existing sessions will be migrated to other
575 The same will be done if all algorithms associated with a driver are
576 unregistered one by one.
578 .Fn crypto_unregister_all
579 there will be no threads in either the newsession or freesession function
582 The calling convention for the driver-supplied routines are:
587 .Fn \*[lp]*newsession\*[rp] "device_t" "crypto_session_t" "struct cryptoini *" ;
590 .Fn \*[lp]*freesession\*[rp] "device_t" "crypto_session_t" ;
593 .Fn \*[lp]*process\*[rp] "device_t" "struct cryptop *" "int" ;
596 .Fn \*[lp]*kprocess\*[rp] "device_t" "struct cryptkop *" "int" ;
599 On invocation, the first argument to
603 .Fn crypto_get_driverid .
604 The second argument to
606 is the opaque session handle for the new session.
607 The third argument is identical to that of
608 .Fn crypto_newsession .
610 Drivers obtain a pointer to their session memory by invoking
611 .Fn crypto_get_driver_session
618 routine takes as arguments the opaque data value and the session handle.
619 It should clear any context associated with the session (clear hardware
620 registers, memory, etc.).
621 If no resources need to be released other than the contents of session memory,
622 the method is optional.
625 framework will zero and release the allocated session memory (after running the
627 method, if one exists).
631 routine is invoked with a request to perform crypto processing.
632 This routine must not block or sleep, but should queue the request and return
633 immediately or process the request to completion.
634 In case of an unrecoverable error, the error indication must be placed in the
639 When the request is completed, or an error is detected, the
643 Session migration may be performed, as mentioned previously.
645 In case of a temporary resource exhaustion, the
649 in which case the crypto services will requeue the request, mark the driver
652 and stop submitting requests for processing.
653 The driver is then responsible for notifying the crypto services
654 when it is again able to process requests through the
657 This simple flow control mechanism should only be used for short-lived
658 resource exhaustion as it causes operations to be queued in the crypto
660 Doing so is preferable to returning an error in such cases as
661 it can cause network protocols to degrade performance by treating the
662 failure much like a lost packet.
666 routine is invoked with a request to perform crypto key processing.
667 This routine must not block, but should queue the request and return
669 Upon processing the request, the callback routine should be invoked.
670 In case of an unrecoverable error, the error indication must be placed in the
675 When the request is completed, or an error is detected, the
677 routine should invoked
680 .Fn crypto_register ,
681 .Fn crypto_kregister ,
682 .Fn crypto_unregister ,
683 .Fn crypto_newsession ,
684 .Fn crypto_freesession ,
687 return 0 on success, or an error code on failure.
688 .Fn crypto_get_driverid
689 returns a non-negative value on error, and \-1 on failure.
691 returns a pointer to a
699 if its argument or the callback function was
702 The callback is provided with an error code in case of failure, in the
706 .Bl -tag -width ".Pa sys/opencrypto/crypto.c"
707 .It Pa sys/opencrypto/crypto.c
708 most of the framework code
717 The cryptographic framework first appeared in
720 .An Angelos D. Keromytis Aq Mt angelos@openbsd.org .
722 The framework currently assumes that all the algorithms in a
723 .Fn crypto_newsession
724 operation must be available by the same driver.
725 If that is not the case, session initialization will fail.
727 The framework also needs a mechanism for determining which driver is
728 best for a specific set of algorithms associated with a session.
729 Some type of benchmarking is in order here.
731 Multiple instances of the same algorithm in the same session are not