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