xref: /netbsd-src/share/man/man9/opencrypto.9 (revision 1b9578b8c2c1f848eeb16dabbfd7d1f0d9fdefbd)
1.\"	$OpenBSD: crypto.9,v 1.25 2003/07/11 13:47:41 jmc Exp $
2.\"	$NetBSD: opencrypto.9,v 1.11 2011/05/23 13:46:54 drochner Exp $
3.\"
4.\" The author of this man page is Angelos D. Keromytis (angelos@cis.upenn.edu)
5.\"
6.\" Copyright (c) 2000, 2001 Angelos D. Keromytis
7.\"
8.\" Permission to use, copy, and modify this software with or without fee
9.\" is hereby granted, provided that this entire notice is included in
10.\" all source code copies of any software which is or includes a copy or
11.\" modification of this software.
12.\"
13.\" THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR
14.\" IMPLIED WARRANTY. IN PARTICULAR, NONE OF THE AUTHORS MAKES ANY
15.\" REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE
16.\" MERCHANTABILITY OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR
17.\" PURPOSE.
18.\"
19.Dd January 1, 2010
20.Dt OPENCRYPTO 9
21.Os
22.Sh NAME
23.Nm opencrypto ,
24.Nm crypto_get_driverid ,
25.Nm crypto_register ,
26.Nm crypto_kregister ,
27.Nm crypto_unregister ,
28.Nm crypto_done ,
29.Nm crypto_kdone ,
30.Nm crypto_newsession ,
31.Nm crypto_freesession ,
32.Nm crypto_dispatch ,
33.Nm crypto_kdispatch ,
34.Nm crypto_getreq ,
35.Nm crypto_freereq
36.Nd API for cryptographic services in the kernel
37.Sh SYNOPSIS
38.In opencrypto/cryptodev.h
39.Ft int32_t
40.Fn crypto_get_driverid "u_int32_t"
41.Ft int
42.Fn crypto_register "u_int32_t" "int" "u_int16_t" "u_int32_t" "int (*)(void *, u_int32_t *, struct cryptoini *)" "int (*)(void *, u_int32_t *)" "int (*)(u_int64_t)" "int (*)(struct cryptop *)" "void *"
43.Ft int
44.Fn crypto_kregister "u_int32_t" "int" "u_int32_t" "int (*)(void *, struct cryptkop *, int)" "void *"
45.Ft int
46.Fn crypto_unregister "u_int32_t" "int"
47.Ft void
48.Fn crypto_done "struct cryptop *"
49.Ft void
50.Fn crypto_kdone "struct cryptkop *"
51.Ft int
52.Fn crypto_newsession "u_int64_t *" "struct cryptoini *" "int"
53.Ft int
54.Fn crypto_freesession "u_int64_t"
55.Ft int
56.Fn crypto_dispatch "struct cryptop *"
57.Ft int
58.Fn crypto_kdispatch "struct cryptkop *"
59.Ft struct cryptop *
60.Fn crypto_getreq "int"
61.Ft void
62.Fn crypto_freereq "struct cryptop *"
63.Bd -literal
64
65#define EALG_MAX_BLOCK_LEN      16
66
67struct cryptoini {
68	int                cri_alg;
69	int                cri_klen;
70	int                cri_rnd;
71	void            *cri_key;
72	u_int8_t           cri_iv[EALG_MAX_BLOCK_LEN];
73	struct cryptoini  *cri_next;
74};
75
76struct cryptodesc {
77	int                crd_skip;
78	int                crd_len;
79	int                crd_inject;
80	int                crd_flags;
81	struct cryptoini   CRD_INI;
82	struct cryptodesc *crd_next;
83};
84
85struct cryptop {
86	TAILQ_ENTRY(cryptop) crp_next;
87	u_int64_t          crp_sid;
88	int                crp_ilen;
89	int                crp_olen;
90	int                crp_etype;
91	int                crp_flags;
92	void            *crp_buf;
93	void            *crp_opaque;
94	struct cryptodesc *crp_desc;
95	int              (*crp_callback)(struct cryptop *);
96	void            *crp_mac;
97};
98
99struct crparam {
100        void         *crp_p;
101        u_int           crp_nbits;
102};
103
104#define CRK_MAXPARAM    8
105
106struct cryptkop {
107	TAILQ_ENTRY(cryptkop) krp_next;
108        u_int              krp_op;         /* i.e. CRK_MOD_EXP or other */
109        u_int              krp_status;     /* return status */
110        u_short            krp_iparams;    /* # of input parameters */
111        u_short            krp_oparams;    /* # of output parameters */
112	u_int32_t	   krp_hid;
113        struct crparam     krp_param[CRK_MAXPARAM];	  /* kvm */
114        int               (*krp_callback)(struct cryptkop *);
115};
116.Ed
117.Sh DESCRIPTION
118.Nm
119is a framework for drivers of cryptographic hardware to register with
120the kernel so
121.Dq consumers
122(other kernel subsystems, and eventually
123users through an appropriate device) are able to make use of it.
124Drivers register with the framework the algorithms they support,
125and provide entry points (functions) the framework may call to
126establish, use, and tear down sessions.
127Sessions are used to cache cryptographic information in a particular driver
128(or associated hardware), so initialization is not needed with every request.
129Consumers of cryptographic services pass a set of
130descriptors that instruct the framework (and the drivers registered
131with it) of the operations that should be applied on the data (more
132than one cryptographic operation can be requested).
133.Pp
134Keying operations are supported as well.
135Unlike the symmetric operators described above,
136these sessionless commands perform mathematical operations using
137input and output parameters.
138.Pp
139Since the consumers may not be associated with a process, drivers may
140not use condition variables:
141.Xr condvar 9 .
142The same holds for the framework.
143Thus, a callback mechanism is used
144to notify a consumer that a request has been completed (the
145callback is specified by the consumer on an per-request basis).
146The callback is invoked by the framework whether the request was
147successfully completed or not.
148An error indication is provided in the latter case.
149A specific error code,
150.Er EAGAIN ,
151is used to indicate that a session number has changed and that the
152request may be re-submitted immediately with the new session number.
153Errors are only returned to the invoking function if not
154enough information to call the callback is available (meaning, there
155was a fatal error in verifying the arguments).
156No callback mechanism is used for session initialization and teardown.
157.Pp
158The
159.Fn crypto_newsession
160routine is called by consumers of cryptographic services (such as the
161.Xr ipsec 4
162stack) that wish to establish a new session with the framework.
163On success, the first argument will contain the Session Identifier (SID).
164The second argument contains all the necessary information for
165the driver to establish the session.
166The third argument indicates whether a
167hardware driver should be used (1) or not (0).
168The various fields in the
169.Fa cryptoini
170structure are:
171.Bl -tag -width foobarmoocow
172.It Fa cri_alg
173Contains an algorithm identifier.
174Currently supported algorithms are:
175.Bd -literal
176CRYPTO_DES_CBC
177CRYPTO_3DES_CBC
178CRYPTO_BLF_CBC
179CRYPTO_CAST_CBC
180CRYPTO_SKIPJACK_CBC
181CRYPTO_MD5_HMAC
182CRYPTO_SHA1_HMAC
183CRYPTO_RIPEMD160_HMAC
184CRYPTO_MD5_KPDK
185CRYPTO_SHA1_KPDK
186CRYPTO_AES_CBC
187CRYPTO_ARC4
188CRYPTO_MD5
189CRYPTO_SHA1
190.Ed
191.Pp
192.It Fa cri_klen
193Specifies the length of the key in bits, for variable-size key
194algorithms.
195.It Fa cri_rnd
196Specifies the number of rounds to be used with the algorithm, for
197variable-round algorithms.
198.It Fa cri_key
199Contains the key to be used with the algorithm.
200.It Fa cri_iv
201Contains an explicit initialization vector (IV), if it does not prefix
202the data.
203This field is ignored during initialization.
204If no IV is explicitly passed (see below on details), a random IV is used
205by the device driver processing the request.
206.It Fa cri_next
207Contains a pointer to another
208.Fa cryptoini
209structure.
210Multiple such structures may be linked to establish multi-algorithm sessions
211.Pf ( Xr ipsec 4
212is an example consumer of such a feature).
213.El
214.Pp
215The
216.Fa cryptoini
217structure and its contents will not be modified by the framework (or
218the drivers used).
219Subsequent requests for processing that use the
220SID returned will avoid the cost of re-initializing the hardware (in
221essence, SID acts as an index in the session cache of the driver).
222.Pp
223.Fn crypto_freesession
224is called with the SID returned by
225.Fn crypto_newsession
226to disestablish the session.
227.Pp
228.Fn crypto_dispatch
229is called to process a request.
230The various fields in the
231.Fa cryptop
232structure are:
233.Bl -tag -width crp_callback
234.It Fa crp_sid
235Contains the SID.
236.It Fa crp_ilen
237Indicates the total length in bytes of the buffer to be processed.
238.It Fa crp_olen
239On return, contains the length of the result, not including
240.Fa crd_skip .
241For symmetric crypto operations, this will be the same as the input length.
242.It Fa crp_alloctype
243Indicates the type of buffer, as used in the kernel
244.Xr malloc 9
245routine.
246This will be used if the framework needs to allocate a new
247buffer for the result (or for re-formatting the input).
248.It Fa crp_callback
249This routine is invoked upon completion of the request, whether
250successful or not.
251It is invoked through the
252.Fn crypto_done
253routine.
254If the request was not successful, an error code is set in the
255.Fa crp_etype
256field.
257It is the responsibility of the callback routine to set the appropriate
258.Xr spl 9
259level.
260.It Fa crp_etype
261Contains the error type, if any errors were encountered, or zero if
262the request was successfully processed.
263If the
264.Er EAGAIN
265error code is returned, the SID has changed (and has been recorded in the
266.Fa crp_sid
267field).
268The consumer should record the new SID and use it in all subsequent requests.
269In this case, the request may be re-submitted immediately.
270This mechanism is used by the framework to perform
271session migration (move a session from one driver to another, because
272of availability, performance, or other considerations).
273.Pp
274Note that this field only makes sense when examined by
275the callback routine specified in
276.Fa crp_callback .
277Errors are returned to the invoker of
278.Fn crypto_process
279only when enough information is not present to call the callback
280routine (i.e., if the pointer passed is
281.Dv NULL
282or if no callback routine was specified).
283.It Fa crp_flags
284Is a bitmask of flags associated with this request.
285Currently defined flags are:
286.Bl -tag -width CRYPTO_F_IMBUF
287.It Dv CRYPTO_F_IMBUF
288The buffer pointed to by
289.Fa crp_buf
290is an mbuf chain.
291.El
292.Pp
293.It Fa crp_buf
294Points to the input buffer.
295On return (when the callback is invoked),
296it contains the result of the request.
297The input buffer may be an mbuf
298chain or a contiguous buffer (of a type identified by
299.Fa crp_alloctype ) ,
300depending on
301.Fa crp_flags .
302.It Fa crp_opaque
303This is passed through the crypto framework untouched and is
304intended for the invoking application's use.
305.It Fa crp_desc
306This is a linked list of descriptors.
307Each descriptor provides
308information about what type of cryptographic operation should be done
309on the input buffer.
310The various fields are:
311.Bl -tag -width ".Fa crd_inject"
312.It Fa crd_skip
313The offset in the input buffer where processing should start.
314.It Fa crd_len
315How many bytes, after
316.Fa crd_skip ,
317should be processed.
318.It Fa crd_inject
319Offset from the beginning of the buffer to insert any results.
320For encryption algorithms, this is where the initialization vector
321(IV) will be inserted when encrypting or where it can be found when
322decrypting (subject to
323.Fa crd_flags ) .
324For MAC algorithms, this is where the result of the keyed hash will be
325inserted.
326.It Fa crd_flags
327For adjusting general operation from userland,
328the following flags are defined:
329.Bl -tag -width CRD_F_IV_EXPLICIT
330.It Dv CRD_F_ENCRYPT
331For encryption algorithms, this bit is set when encryption is required
332(when not set, decryption is performed).
333.It Dv CRD_F_IV_PRESENT
334For encryption algorithms, this bit is set when the IV already
335precedes the data, so the
336.Fa crd_inject
337value will be ignored and no IV will be written in the buffer.
338Otherwise, the IV used to encrypt the packet will be written
339at the location pointed to by
340.Fa crd_inject .
341Some applications that do special
342.Dq IV cooking ,
343such as the half-IV mode in
344.Xr ipsec 4 ,
345can use this flag to indicate that the IV should not be written on the packet.
346This flag is typically used in conjunction with the
347.Dv CRD_F_IV_EXPLICIT
348flag.
349.It Dv CRD_F_IV_EXPLICIT
350For encryption algorithms, this bit is set when the IV is explicitly
351provided by the consumer in the
352.Fa crd_iv
353fields.
354Otherwise, for encryption operations the IV is provided for by
355the driver used to perform the operation, whereas for decryption
356operations it is pointed to by the
357.Fa crd_inject
358field.
359This flag is typically used when the IV is calculated
360.Dq on the fly
361by the consumer, and does not precede the data (some
362.Xr ipsec 4
363configurations, and the encrypted swap are two such examples).
364.It Dv CRD_F_COMP
365For compression algorithms, this bit is set when compression is required (when
366not set, decompression is performed).
367.El
368.It Fa CRD_INI
369This
370.Fa cryptoini
371structure will not be modified by the framework or the device drivers.
372Since this information accompanies every cryptographic
373operation request, drivers may re-initialize state on-demand
374(typically an expensive operation).
375Furthermore, the cryptographic
376framework may re-route requests as a result of full queues or hardware
377failure, as described above.
378.It Fa crd_next
379Point to the next descriptor.
380Linked operations are useful in protocols such as
381.Xr ipsec 4 ,
382where multiple cryptographic transforms may be applied on the same
383block of data.
384.El
385.El
386.Pp
387.Fn crypto_getreq
388allocates a
389.Fa cryptop
390structure with a linked list of as many
391.Fa cryptodesc
392structures as were specified in the argument passed to it.
393.Pp
394.Fn crypto_freereq
395deallocates a structure
396.Fa cryptop
397and any
398.Fa cryptodesc
399structures linked to it.
400Note that it is the responsibility of the
401callback routine to do the necessary cleanups associated with the
402opaque field in the
403.Fa cryptop
404structure.
405.Pp
406.Fn crypto_kdispatch
407is called to perform a keying operation.
408The various fields in the
409.Fa crytokop
410structure are:
411.Bl -tag -width crp_alloctype
412.It Fa krp_op
413Operation code, such as CRK_MOD_EXP.
414.It Fa krp_status
415Return code.
416This errno-style variable indicates whether there were lower level reasons
417for operation failure.
418.It Fa krp_iparams
419Number of input parameters to the specified operation.
420Note that each operation has a (typically hardwired) number of such parameters.
421.It Fa krp_oparams
422Number of output parameters from the specified operation.
423Note that each operation has a (typically hardwired) number of such parameters.
424.It Fa krp_kvp
425An array of kernel memory blocks containing the parameters.
426.It Fa krp_hid
427Identifier specifying which low-level driver is being used.
428.It Fa krp_callback
429Callback called on completion of a keying operation.
430.El
431.Pp
432The following sysctl entries exist to adjust
433the behaviour of the system from userland:
434.Bl -tag -width kern.cryptodevallowsoft
435.It kern.usercrypto
436Allow (1) or forbid (0) userland access to
437.Pa /dev/crypto .
438.It kern.userasymcrypto
439Allow (1) or forbid (0) userland access to
440do asymmetric crypto requests.
441.It kern.cryptodevallowsoft
442Enable/disable access to hardware versus software operations:
443.Bl -tag -width xxx
444.It \*[Lt] 0
445Force userlevel requests to use software operations, always.
446.It = 0
447Use hardware if present, grant userlevel requests for non-accelerated
448operations (handling the latter in software).
449.It \*[Gt] 0
450Allow user requests only for operations which are hardware-accelerated.
451.El
452.El
453.Sh DRIVER-SIDE API
454The
455.Fn crypto_get_driverid ,
456.Fn crypto_register ,
457.Fn crypto_kregister ,
458.Fn crypto_unregister ,
459and
460.Fn crypto_done
461routines are used by drivers that provide support for cryptographic
462primitives to register and unregister with the kernel crypto services
463framework.
464Drivers must first use the
465.Fn crypto_get_driverid
466function to acquire a driver identifier, specifying the
467.Fa flags
468as an argument (normally 0, but software-only drivers should specify
469.Dv CRYPTOCAP_F_SOFTWARE ) .
470For each algorithm the driver supports, it must then call
471.Fn crypto_register .
472The first argument is the driver identifier.
473The second argument is an array of
474.Dv CRYPTO_ALGORITHM_MAX + 1
475elements, indicating which algorithms are supported.
476The last three arguments are pointers to three
477driver-provided functions that the framework may call to establish new
478cryptographic context with the driver, free already established
479context, and ask for a request to be processed (encrypt, decrypt,
480etc.)
481.Fn crypto_unregister
482is called by drivers that wish to withdraw support for an algorithm.
483The two arguments are the driver and algorithm identifiers, respectively.
484Typically, drivers for
485.Xr pcmcia 4
486crypto cards that are being ejected will invoke this routine for all
487algorithms supported by the card.
488If called with
489.Dv CRYPTO_ALGORITHM_ALL ,
490all algorithms registered for a driver will be unregistered in one go
491and the driver will be disabled (no new sessions will be allocated on
492that driver, and any existing sessions will be migrated to other
493drivers).
494The same will be done if all algorithms associated with a driver are
495unregistered one by one.
496.Pp
497The calling convention for the three driver-supplied routines is:
498.Bd -literal
499int (*newsession) (void *, u_int32_t *, struct cryptoini *);
500int (*freesession) (void *, u_int64_t);
501int (*process) (void *, struct cryptop *, int);
502.Ed
503.Pp
504On invocation, the first argument to
505.Fn newsession
506contains the driver identifier obtained via
507.Fn crypto_get_driverid .
508On successfully returning, it should contain a driver-specific session
509identifier.
510The second argument is identical to that of
511.Fn crypto_newsession .
512.Pp
513The
514.Fn freesession
515routine takes as argument the SID (which is the concatenation of the
516driver identifier and the driver-specific session identifier).
517It should clear any context associated with the session (clear hardware
518registers, memory, etc.).
519.Pp
520The
521.Fn process
522routine is invoked with a request to perform crypto processing.
523This routine must not block, but should queue the request and return
524immediately.
525Upon processing the request, the callback routine should be invoked.
526In case of error, the error indication must be placed in the
527.Fa crp_etype
528field of the
529.Fa cryptop
530structure.
531The
532.Fa hint
533argument can be set to
534.Dv CRYPTO_HINT_MORE
535when there will be more request right after this request.
536When the request is completed, or an error is detected, the
537.Fn process
538routine should invoke
539.Fn crypto_done .
540Session migration may be performed, as mentioned previously.
541.Pp
542The
543.Fn kprocess
544routine is invoked with a request to perform crypto key processing.
545This routine must not block, but should queue the request and return
546immediately.
547Upon processing the request, the callback routine should be invoked.
548In case of error, the error indication must be placed in the
549.Fa krp_status
550field of the
551.Fa cryptkop
552structure.
553When the request is completed, or an error is detected, the
554.Fn kprocess
555routine should invoke
556.Fn crypto_kdone .
557.Sh RETURN VALUES
558.Fn crypto_register ,
559.Fn crypto_kregister ,
560.Fn crypto_unregister ,
561.Fn crypto_newsession ,
562and
563.Fn crypto_freesession
564return 0 on success, or an error code on failure.
565.Fn crypto_get_driverid
566returns a non-negative value on error, and \-1 on failure.
567.Fn crypto_getreq
568returns a pointer to a
569.Fa cryptop
570structure and
571.Dv NULL
572on failure.
573.Fn crypto_dispatch
574returns
575.Er EINVAL
576if its argument or the callback function was
577.Dv NULL ,
578and 0 otherwise.
579The callback is provided with an error code in case of failure, in the
580.Fa crp_etype
581field.
582.Sh FILES
583.Bl -tag -width sys/opencrypto/crypto.c
584.It Pa sys/opencrypto/crypto.c
585most of the framework code
586.It Pa sys/crypto
587crypto algorithm implementations
588.El
589.Sh SEE ALSO
590.Xr ipsec 4 ,
591.Xr pcmcia 4 ,
592.Xr condvar 9 ,
593.Xr malloc 9
594.Rs
595.%A "Angelos D. Keromytis"
596.%A "Jason L. Wright"
597.%A "Theo de Raadt"
598.%T "The Design of the OpenBSD Cryptographic Framework"
599.%I "Usenix"
600.%N "2003"
601.%D "June 2003"
602.Re
603.Sh HISTORY
604The cryptographic framework first appeared in
605.Ox 2.7
606and was written by
607.An Angelos D. Keromytis Aq angelos@openbsd.org .
608.Pp
609.An Sam Leffler
610ported the crypto framework to
611.Fx
612and made performance improvements.
613.Pp
614.An Jonathan Stone Aq jonathan@NetBSD.org
615ported the cryptoframe from
616.Fx
617to
618.Nx .
619.Nm opencrypto
620first appeared in
621.Nx 2.0 .
622.Sh BUGS
623The framework currently assumes that all the algorithms in a
624.Fn crypto_newsession
625operation must be available by the same driver.
626If that's not the case, session initialization will fail.
627.Pp
628The framework also needs a mechanism for determining which driver is
629best for a specific set of algorithms associated with a session.
630Some type of benchmarking is in order here.
631.Pp
632Multiple instances of the same algorithm in the same session are not
633supported.
634Note that 3DES is considered one algorithm (and not three
635instances of DES).
636Thus, 3DES and DES could be mixed in the same request.
637.Pp
638A queue for completed operations should be implemented and processed
639at some software
640.Xr spl 9
641level, to avoid overall system latency issues, and potential kernel
642stack exhaustion while processing a callback.
643.Pp
644When SMP time comes, we will support use of a second processor (or
645more) as a crypto device (this is actually AMP, but we need the same
646basic support).
647