xref: /dragonfly/share/man/man9/crypto.9 (revision 8a0bcd56)
1.\"	$OpenBSD: crypto.9,v 1.19 2002/07/16 06:31:57 angelos Exp $
2.\"
3.\" The author of this manual 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.\" $FreeBSD: src/share/man/man9/crypto.9,v 1.14 2007/09/19 16:28:46 brueffer Exp $
19.\"
20.Dd August 21, 2010
21.Dt CRYPTO 9
22.Os
23.Sh NAME
24.Nm crypto
25.Nd API for cryptographic services in the kernel
26.Sh SYNOPSIS
27.In opencrypto/cryptodev.h
28.Ft int32_t
29.Fn crypto_get_driverid "device_t dev" "int flags"
30.Ft int
31.Fn crypto_find_driver "const char *match"
32.Ft device_t
33.Fn crypto_find_device_byhid "int hid"
34.Ft int
35.Fn crypto_getcaps "int hid"
36.Ft int
37.Fn crypto_register "u_int32_t driverid" "int alg" "u_int16_t maxoplen" "u_int32_t flags"
38.Ft int
39.Fn crypto_kregister "u_int32_t driverid" "int kalg" "u_int32_t flags"
40.Ft int
41.Fn crypto_unregister "u_int32_t driverid" "int alg"
42.Ft int
43.Fn crypto_unregister_all "u_int32_t driverid"
44.Ft void
45.Fn crypto_done "struct cryptop *crp"
46.Ft void
47.Fn crypto_kdone "struct cryptkop *krp"
48.Ft int
49.Fn crypto_newsession "u_int64_t *sid" "struct cryptoini *cri" "int crid"
50.Ft int
51.Fn crypto_freesession "u_int64_t sid"
52.Ft int
53.Fn crypto_dispatch "struct cryptop *crp"
54.Ft int
55.Fn crypto_kdispatch "struct cryptkop *krp"
56.Ft int
57.Fn crypto_unblock "u_int32_t driverid" "int what"
58.Ft "struct cryptop *"
59.Fn crypto_getreq "int num"
60.Ft void
61.Fn crypto_freereq "struct cryptop *crp"
62.Bd -literal
63#define	CRYPTO_SYMQ	0x1
64#define	CRYPTO_ASYMQ	0x2
65
66#define EALG_MAX_BLOCK_LEN      16
67
68struct cryptoini {
69	int                cri_alg;
70	int                cri_klen;
71	int                cri_mlen;
72	caddr_t            cri_key;
73	u_int8_t           cri_iv[EALG_MAX_BLOCK_LEN];
74	struct cryptoini  *cri_next;
75};
76
77struct cryptodesc {
78	int                crd_skip;
79	int                crd_len;
80	int                crd_inject;
81	int                crd_flags;
82	struct cryptoini   CRD_INI;
83#define crd_iv          CRD_INI.cri_iv
84#define crd_key         CRD_INI.cri_key
85#define crd_alg         CRD_INI.cri_alg
86#define crd_klen        CRD_INI.cri_klen
87	struct cryptodesc *crd_next;
88};
89
90struct cryptop {
91	TAILQ_ENTRY(cryptop) crp_next;
92	u_int64_t          crp_sid;
93	int                crp_ilen;
94	int                crp_olen;
95	int                crp_etype;
96	int                crp_flags;
97	caddr_t            crp_buf;
98	caddr_t            crp_opaque;
99	struct cryptodesc *crp_desc;
100	int              (*crp_callback) (struct cryptop *);
101	caddr_t            crp_mac;
102};
103
104struct crparam {
105        caddr_t         crp_p;
106        u_int           crp_nbits;
107};
108
109#define CRK_MAXPARAM    8
110
111struct cryptkop {
112        TAILQ_ENTRY(cryptkop) krp_next;
113        u_int              krp_op;         /* ie. CRK_MOD_EXP or other */
114        u_int              krp_status;     /* return status */
115        u_short            krp_iparams;    /* # of input parameters */
116        u_short            krp_oparams;    /* # of output parameters */
117	u_int32_t	   krp_hid;
118        struct crparam     krp_param[CRK_MAXPARAM];
119        int               (*krp_callback)(struct cryptkop *);
120};
121.Ed
122.Sh DESCRIPTION
123.Nm
124is a framework for drivers of cryptographic hardware to register with
125the kernel so
126.Dq consumers
127(other kernel subsystems, and
128users through the
129.Pa /dev/crypto
130device) are able to make use of it.
131Drivers register with the framework the algorithms they support,
132and provide entry points (functions) the framework may call to
133establish, use, and tear down sessions.
134Sessions are used to cache cryptographic information in a particular driver
135(or associated hardware), so initialization is not needed with every request.
136Consumers of cryptographic services pass a set of
137descriptors that instruct the framework (and the drivers registered
138with it) of the operations that should be applied on the data (more
139than one cryptographic operation can be requested).
140.Pp
141Keying operations are supported as well.
142Unlike the symmetric operators described above,
143these sessionless commands perform mathematical operations using
144input and output parameters.
145.Pp
146Since the consumers may not be associated with a process, drivers may
147not
148.Xr sleep 9 .
149The same holds for the framework.
150Thus, a callback mechanism is used
151to notify a consumer that a request has been completed (the
152callback is specified by the consumer on an per-request basis).
153The callback is invoked by the framework whether the request was
154successfully completed or not.
155An error indication is provided in the latter case.
156A specific error code,
157.Er EAGAIN ,
158is used to indicate that a session number has changed and that the
159request may be re-submitted immediately with the new session number.
160Errors are only returned to the invoking function if not
161enough information to call the callback is available (meaning, there
162was a fatal error in verifying the arguments).
163For session initialization and teardown there is no callback mechanism used.
164.Pp
165The
166.Fn crypto_newsession
167routine is called by consumers of cryptographic services (such as the
168.Xr ipsec 4
169stack) that wish to establish a new session with the framework.
170On success, the first argument will contain the Session Identifier (SID).
171The second argument contains all the necessary information for
172the driver to establish the session.
173The third argument indicates whether a
174hardware driver (1) should be used or not (0).
175The various fields in the
176.Vt cryptoini
177structure are:
178.Bl -tag -width ".Va cri_next"
179.It Va cri_alg
180Contains an algorithm identifier.
181Currently supported algorithms are:
182.Pp
183.Bl -tag -width ".Dv CRYPTO_RIPEMD160_HMAC" -compact
184.It Dv CRYPTO_AES_CBC
185.It Dv CRYPTO_AES_XTS
186.It Dv CRYPTO_AES_CTR
187.It Dv CRYPTO_ARC4
188.It Dv CRYPTO_BLF_CBC
189.It Dv CRYPTO_CAMELLIA_CBC
190.It Dv CRYPTO_CAST_CBC
191.It Dv CRYPTO_DES_CBC
192.It Dv CRYPTO_3DES_CBC
193.It Dv CRYPTO_SKIPJACK_CBC
194.It Dv CRYPTO_MD5
195.It Dv CRYPTO_MD5_HMAC
196.It Dv CRYPTO_MD5_KPDK
197.It Dv CRYPTO_RIPEMD160_HMAC
198.It Dv CRYPTO_SHA1
199.It Dv CRYPTO_SHA1_HMAC
200.It Dv CRYPTO_SHA1_KPDK
201.It Dv CRYPTO_SHA2_256_HMAC
202.It Dv CRYPTO_SHA2_384_HMAC
203.It Dv CRYPTO_SHA2_512_HMAC
204.It Dv CRYPTO_NULL_HMAC
205.It Dv CRYPTO_NULL_CBC
206.El
207.It Va cri_klen
208Specifies the length of the key in bits, for variable-size key
209algorithms.
210.It Va cri_mlen
211Specifies how many bytes from the calculated hash should be copied back.
2120 means entire hash.
213.It Va cri_key
214Contains the key to be used with the algorithm.
215.It Va cri_iv
216Contains an explicit initialization vector (IV), if it does not prefix
217the data.
218This field is ignored during initialization.
219If no IV is explicitly passed (see below on details), a random IV is used
220by the device driver processing the request.
221.It Va cri_next
222Contains a pointer to another
223.Vt cryptoini
224structure.
225Multiple such structures may be linked to establish multi-algorithm sessions
226.Xr ( ipsec 4
227is an example consumer of such a feature).
228.El
229.Pp
230The
231.Vt cryptoini
232structure and its contents will not be modified by the framework (or
233the drivers used).
234Subsequent requests for processing that use the
235SID returned will avoid the cost of re-initializing the hardware (in
236essence, SID acts as an index in the session cache of the driver).
237.Pp
238.Fn crypto_freesession
239is called with the SID returned by
240.Fn crypto_newsession
241to disestablish the session.
242.Pp
243.Fn crypto_dispatch
244is called to process a request.
245The various fields in the
246.Vt cryptop
247structure are:
248.Bl -tag -width ".Va crp_callback"
249.It Va crp_sid
250Contains the SID.
251.It Va crp_ilen
252Indicates the total length in bytes of the buffer to be processed.
253.It Va crp_olen
254On return, contains the total length of the result.
255For symmetric crypto operations, this will be the same as the input length.
256This will be used if the framework needs to allocate a new
257buffer for the result (or for re-formatting the input).
258.It Va crp_callback
259This routine is invoked upon completion of the request, whether
260successful or not.
261It is invoked through the
262.Fn crypto_done
263routine.
264If the request was not successful, an error code is set in the
265.Va crp_etype
266field.
267It is the responsibility of the callback routine to enter a critical
268section.
269.It Va crp_etype
270Contains the error type, if any errors were encountered, or zero if
271the request was successfully processed.
272If the
273.Er EAGAIN
274error code is returned, the SID has changed (and has been recorded in the
275.Va crp_sid
276field).
277The consumer should record the new SID and use it in all subsequent requests.
278In this case, the request may be re-submitted immediately.
279This mechanism is used by the framework to perform
280session migration (move a session from one driver to another, because
281of availability, performance, or other considerations).
282.Pp
283Note that this field only makes sense when examined by
284the callback routine specified in
285.Va crp_callback .
286Errors are returned to the invoker of
287.Fn crypto_process
288only when enough information is not present to call the callback
289routine (i.e., if the pointer passed is
290.Dv NULL
291or if no callback routine was specified).
292.It Va crp_flags
293Is a bitmask of flags associated with this request.
294Currently defined flags are:
295.Bl -tag -width ".Dv CRYPTO_F_CBIFSYNC"
296.It Dv CRYPTO_F_IMBUF
297The buffer pointed to by
298.Va crp_buf
299is an mbuf chain.
300.It Dv CRYPTO_F_IOV
301The buffer pointed to by
302.Va crp_buf
303is an
304.Vt uio
305structure.
306.It Dv CRYPTO_F_REL
307Must return data in the same place.
308.It Dv CRYPTO_F_BATCH
309Batch operation if possible.
310.It Dv CRYPTO_F_CBIMM
311Do callback immediately instead of doing it from a dedicated kernel thread.
312.It Dv CRYPTO_F_DONE
313Operation completed.
314.It Dv CRYPTO_F_CBIFSYNC
315Do callback immediately if operation is synchronous.
316.El
317.It Va crp_buf
318Points to the input buffer.
319On return (when the callback is invoked),
320it contains the result of the request.
321The input buffer may be an mbuf
322chain or a contiguous buffer,
323depending on
324.Va crp_flags .
325.It Va crp_opaque
326This is passed through the crypto framework untouched and is
327intended for the invoking application's use.
328.It Va crp_desc
329This is a linked list of descriptors.
330Each descriptor provides
331information about what type of cryptographic operation should be done
332on the input buffer.
333The various fields are:
334.Bl -tag -width ".Va crd_inject"
335.It Va crd_iv
336The field where IV should be provided when the
337.Dv CRD_F_IV_EXPLICIT
338flag is given.
339.It Va crd_key
340When the
341.Dv CRD_F_KEY_EXPLICIT
342flag is given, the
343.Va crd_key
344points to a buffer with encryption or authentication key.
345.It Va crd_alg
346An algorithm to use.
347Must be the same as the one given at newsession time.
348.It Va crd_klen
349The
350.Va crd_key
351key length.
352.It Va crd_skip
353The offset in the input buffer where processing should start.
354.It Va crd_len
355How many bytes, after
356.Va crd_skip ,
357should be processed.
358.It Va crd_inject
359Offset from the beginning of the buffer to insert any results.
360For encryption algorithms, this is where the initialization vector
361(IV) will be inserted when encrypting or where it can be found when
362decrypting (subject to
363.Va crd_flags ) .
364For MAC algorithms, this is where the result of the keyed hash will be
365inserted.
366.It Va crd_flags
367The following flags are defined:
368.Bl -tag -width 3n
369.It Dv CRD_F_ENCRYPT
370For encryption algorithms, this bit is set when encryption is required
371(when not set, decryption is performed).
372.It Dv CRD_F_IV_PRESENT
373For encryption algorithms, this bit is set when the IV already
374precedes the data, so the
375.Va crd_inject
376value will be ignored and no IV will be written in the buffer.
377Otherwise, the IV used to encrypt the packet will be written
378at the location pointed to by
379.Va crd_inject .
380The IV length is assumed to be equal to the blocksize of the
381encryption algorithm.
382Some applications that do special
383.Dq "IV cooking" ,
384such as the half-IV mode in
385.Xr ipsec 4 ,
386can use this flag to indicate that the IV should not be written on the packet.
387This flag is typically used in conjunction with the
388.Dv CRD_F_IV_EXPLICIT
389flag.
390.It Dv CRD_F_IV_EXPLICIT
391For encryption algorithms, this bit is set when the IV is explicitly
392provided by the consumer in the
393.Va crd_iv
394field.
395Otherwise, for encryption operations the IV is provided for by
396the driver used to perform the operation, whereas for decryption
397operations it is pointed to by the
398.Va crd_inject
399field.
400This flag is typically used when the IV is calculated
401.Dq "on the fly"
402by the consumer, and does not precede the data (some
403.Xr ipsec 4
404configurations, and the encrypted swap are two such examples).
405.It Dv CRD_F_KEY_EXPLICIT
406For encryption and authentication (MAC) algorithms, this bit is set when the key
407is explicitly provided by the consumer in the
408.Va crd_key
409field for the given operation.
410Otherwise, the key is taken at newsession time from the
411.Va cri_key
412field.
413.It Dv CRD_F_COMP
414For compression algorithms, this bit is set when compression is required (when
415not set, decompression is performed).
416.El
417.It Va CRD_INI
418This
419.Vt cryptoini
420structure will not be modified by the framework or the device drivers.
421Since this information accompanies every cryptographic
422operation request, drivers may re-initialize state on-demand
423(typically an expensive operation).
424Furthermore, the cryptographic
425framework may re-route requests as a result of full queues or hardware
426failure, as described above.
427.It Va crd_next
428Point to the next descriptor.
429Linked operations are useful in protocols such as
430.Xr ipsec 4 ,
431where multiple cryptographic transforms may be applied on the same
432block of data.
433.El
434.El
435.Pp
436.Fn crypto_getreq
437allocates a
438.Vt cryptop
439structure with a linked list of as many
440.Vt cryptodesc
441structures as were specified in the argument passed to it.
442.Pp
443.Fn crypto_freereq
444deallocates a structure
445.Vt cryptop
446and any
447.Vt cryptodesc
448structures linked to it.
449Note that it is the responsibility of the
450callback routine to do the necessary cleanups associated with the
451opaque field in the
452.Vt cryptop
453structure.
454.Pp
455.Fn crypto_kdispatch
456is called to perform a keying operation.
457The various fields in the
458.Vt cryptkop
459structure are:
460.Bl -tag -width ".Va krp_callback"
461.It Va krp_op
462Operation code, such as
463.Dv CRK_MOD_EXP .
464.It Va krp_status
465Return code.
466This
467.Va errno Ns -style
468variable indicates whether lower level reasons
469for operation failure.
470.It Va krp_iparams
471Number if input parameters to the specified operation.
472Note that each operation has a (typically hardwired) number of such parameters.
473.It Va krp_oparams
474Number if output parameters from the specified operation.
475Note that each operation has a (typically hardwired) number of such parameters.
476.It Va krp_kvp
477An array of kernel memory blocks containing the parameters.
478.It Va krp_hid
479Identifier specifying which low-level driver is being used.
480.It Va krp_callback
481Callback called on completion of a keying operation.
482.El
483.Sh DRIVER-SIDE API
484The
485.Fn crypto_get_driverid ,
486.Fn crypto_register ,
487.Fn crypto_kregister ,
488.Fn crypto_unregister ,
489.Fn crypto_unblock ,
490and
491.Fn crypto_done
492routines are used by drivers that provide support for cryptographic
493primitives to register and unregister with the kernel crypto services
494framework.
495Drivers must first use the
496.Fn crypto_get_driverid
497function to acquire a driver identifier, specifying the
498.Fa cc_flags
499as an argument (normally 0, but software-only drivers should specify
500.Dv CRYPTOCAP_F_SOFTWARE ) .
501For each algorithm the driver supports, it must then call
502.Fn crypto_register .
503The first two arguments are the driver and algorithm identifiers.
504The next two arguments specify the largest possible operator length (in bits,
505important for public key operations) and flags for this algorithm.
506The last four arguments must be provided in the first call to
507.Fn crypto_register
508and are ignored in all subsequent calls.
509They are pointers to three
510driver-provided functions that the framework may call to establish new
511cryptographic context with the driver, free already established
512context, and ask for a request to be processed (encrypt, decrypt,
513etc.); and an opaque parameter to pass when calling each of these routines.
514.Fn crypto_unregister
515is called by drivers that wish to withdraw support for an algorithm.
516The two arguments are the driver and algorithm identifiers, respectively.
517Typically, drivers for
518PCMCIA
519crypto cards that are being ejected will invoke this routine for all
520algorithms supported by the card.
521.Fn crypto_unregister_all
522will unregister all algorithms registered by a driver
523and the driver will be disabled (no new sessions will be allocated on
524that driver, and any existing sessions will be migrated to other
525drivers).
526The same will be done if all algorithms associated with a driver are
527unregistered one by one.
528.Pp
529The calling convention for the three driver-supplied routines is:
530.Pp
531.Bl -item -compact
532.It
533.Ft int
534.Fn \*[lp]*newsession\*[rp] "void *" "u_int32_t *" "struct cryptoini *" ;
535.It
536.Ft int
537.Fn \*[lp]*freesession\*[rp] "void *" "u_int64_t" ;
538.It
539.Ft int
540.Fn \*[lp]*process\*[rp] "void *" "struct cryptop *" ;
541.It
542.Ft int
543.Fn \*[lp]*kprocess\*[rp] "void *" "struct cryptkop *" ;
544.El
545.Pp
546On invocation, the first argument to
547all routines is an opaque data value supplied when the algorithm
548is registered with
549.Fn crypto_register .
550The second argument to
551.Fn newsession
552contains the driver identifier obtained via
553.Fn crypto_get_driverid .
554On successful return, it should contain a driver-specific session
555identifier.
556The third argument is identical to that of
557.Fn crypto_newsession .
558.Pp
559The
560.Fn freesession
561routine takes as arguments the opaque data value and the SID
562(which is the concatenation of the
563driver identifier and the driver-specific session identifier).
564It should clear any context associated with the session (clear hardware
565registers, memory, etc.).
566.Pp
567The
568.Fn process
569routine is invoked with a request to perform crypto processing.
570This routine must not block, but should queue the request and return
571immediately.
572Upon processing the request, the callback routine should be invoked.
573In case of an unrecoverable error, the error indication must be placed in the
574.Va crp_etype
575field of the
576.Vt cryptop
577structure.
578When the request is completed, or an error is detected, the
579.Fn process
580routine should invoke
581.Fn crypto_done .
582Session migration may be performed, as mentioned previously.
583.Pp
584In case of a temporary resource exhaustion, the
585.Fn process
586routine may return
587.Er ERESTART
588in which case the crypto services will requeue the request, mark the driver
589as
590.Dq blocked ,
591and stop submitting requests for processing.
592The driver is then responsible for notifying the crypto services
593when it is again able to process requests through the
594.Fn crypto_unblock
595routine.
596This simple flow control mechanism should only be used for short-lived
597resource exhaustion as it causes operations to be queued in the crypto
598layer.
599Doing so is preferable to returning an error in such cases as
600it can cause network protocols to degrade performance by treating the
601failure much like a lost packet.
602.Pp
603The
604.Fn kprocess
605routine is invoked with a request to perform crypto key processing.
606This routine must not block, but should queue the request and return
607immediately.
608Upon processing the request, the callback routine should be invoked.
609In case of an unrecoverable error, the error indication must be placed in the
610.Va krp_status
611field of the
612.Vt cryptkop
613structure.
614When the request is completed, or an error is detected, the
615.Fn kprocess
616routine should invoked
617.Fn crypto_kdone .
618.Sh RETURN VALUES
619.Fn crypto_register ,
620.Fn crypto_kregister ,
621.Fn crypto_unregister ,
622.Fn crypto_newsession ,
623.Fn crypto_freesession ,
624and
625.Fn crypto_unblock
626return 0 on success, or an error code on failure.
627.Fn crypto_get_driverid
628returns a non-negative value on error, and \-1 on failure.
629.Fn crypto_getreq
630returns a pointer to a
631.Vt cryptop
632structure and
633.Dv NULL
634on failure.
635.Fn crypto_dispatch
636returns
637.Er EINVAL
638if its argument or the callback function was
639.Dv NULL ,
640and 0 otherwise.
641The callback is provided with an error code in case of failure, in the
642.Va crp_etype
643field.
644.Sh FILES
645.Bl -tag -width ".Pa sys/opencrypto/crypto.c"
646.It Pa sys/opencrypto/crypto.c
647most of the framework code
648.El
649.Sh SEE ALSO
650.Xr ipsec 4 ,
651.Xr kmalloc 9 ,
652.Xr sleep 9
653.Sh HISTORY
654The cryptographic framework first appeared in
655.Ox 2.7
656and was written by
657.An "Angelos D. Keromytis" Aq angelos@openbsd.org .
658.Sh BUGS
659The framework currently assumes that all the algorithms in a
660.Fn crypto_newsession
661operation must be available by the same driver.
662If that is not the case, session initialization will fail.
663.Pp
664The framework also needs a mechanism for determining which driver is
665best for a specific set of algorithms associated with a session.
666Some type of benchmarking is in order here.
667.Pp
668Multiple instances of the same algorithm in the same session are not
669supported.
670Note that 3DES is considered one algorithm (and not three
671instances of DES).
672Thus, 3DES and DES could be mixed in the same request.
673