xref: /openbsd-src/share/man/man9/crypto.9 (revision 9d8a60ff1dae84d5c890bdc5040be018a3bdd3dc)
1*9d8a60ffSkrw.\"	$OpenBSD: crypto.9,v 1.43 2020/03/28 13:16:09 krw Exp $
293f4d5abSangelos.\"
393f4d5abSangelos.\" The author of this man page is Angelos D. Keromytis (angelos@cis.upenn.edu)
493f4d5abSangelos.\"
519b5c32fSangelos.\" Copyright (c) 2000, 2001 Angelos D. Keromytis
693f4d5abSangelos.\"
7f3c2e7bbSangelos.\" Permission to use, copy, and modify this software with or without fee
893f4d5abSangelos.\" is hereby granted, provided that this entire notice is included in
993f4d5abSangelos.\" all source code copies of any software which is or includes a copy or
1093f4d5abSangelos.\" modification of this software.
1193f4d5abSangelos.\"
1293f4d5abSangelos.\" THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR
1393f4d5abSangelos.\" IMPLIED WARRANTY. IN PARTICULAR, NONE OF THE AUTHORS MAKES ANY
1493f4d5abSangelos.\" REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE
1593f4d5abSangelos.\" MERCHANTABILITY OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR
1693f4d5abSangelos.\" PURPOSE.
1793f4d5abSangelos.\"
18*9d8a60ffSkrw.Dd $Mdocdate: March 28 2020 $
1933378d91Sjmc.Dt CRYPTO_GET_DRIVERID 9
2093f4d5abSangelos.Os
2193f4d5abSangelos.Sh NAME
2233378d91Sjmc.Nm crypto_get_driverid ,
2333378d91Sjmc.Nm crypto_register ,
2433378d91Sjmc.Nm crypto_unregister ,
2533378d91Sjmc.Nm crypto_done ,
2633378d91Sjmc.Nm crypto_newsession ,
2733378d91Sjmc.Nm crypto_freesession ,
2833378d91Sjmc.Nm crypto_dispatch ,
2933378d91Sjmc.Nm crypto_getreq ,
3033378d91Sjmc.Nm crypto_freereq
3193f4d5abSangelos.Nd API for cryptographic services in the kernel
3293f4d5abSangelos.Sh SYNOPSIS
33dddd2645Sschwarze.In crypto/cryptodev.h
3493f4d5abSangelos.Ft int32_t
35460f87bbSderaadt.Fn crypto_get_driverid "u_int8_t"
3693f4d5abSangelos.Ft int
37997a580eSjason.Fn crypto_register "u_int32_t" "int *" "int (*)(u_int32_t *, struct cryptoini *)" "int (*)(u_int64_t)" "int (*)(struct cryptop *)"
3893f4d5abSangelos.Ft int
3993f4d5abSangelos.Fn crypto_unregister "u_int32_t" "int"
40f31333f1Sangelos.Ft void
41f31333f1Sangelos.Fn crypto_done "struct cryptop *"
4293f4d5abSangelos.Ft int
4369bcbef5Sangelos.Fn crypto_newsession "u_int64_t *" "struct cryptoini *" "int"
4493f4d5abSangelos.Ft int
4593f4d5abSangelos.Fn crypto_freesession "u_int64_t"
4693f4d5abSangelos.Ft int
4793f4d5abSangelos.Fn crypto_dispatch "struct cryptop *"
4893f4d5abSangelos.Ft struct cryptop *
4993f4d5abSangelos.Fn crypto_getreq "int"
5093f4d5abSangelos.Ft void
5147b681c1Sjfb.Fn crypto_freereq "struct cryptop *"
5293f4d5abSangelos.Bd -literal
5393f4d5abSangelos
5469bcbef5Sangelos#define EALG_MAX_BLOCK_LEN      16
5593f4d5abSangelos
56460f87bbSderaadtstruct cryptoini {
5793f4d5abSangelos	int                cri_alg;
5893f4d5abSangelos	int                cri_klen;
5993f4d5abSangelos	int                cri_rnd;
6093f4d5abSangelos	caddr_t            cri_key;
6193f4d5abSangelos	u_int8_t           cri_iv[EALG_MAX_BLOCK_LEN];
6293f4d5abSangelos	struct cryptoini  *cri_next;
6393f4d5abSangelos};
6493f4d5abSangelos
65460f87bbSderaadtstruct cryptodesc {
6693f4d5abSangelos	int                crd_skip;
6793f4d5abSangelos	int                crd_len;
6893f4d5abSangelos	int                crd_inject;
6993f4d5abSangelos	int                crd_flags;
7093f4d5abSangelos	struct cryptoini   CRD_INI;
7193f4d5abSangelos	struct cryptodesc *crd_next;
7293f4d5abSangelos};
7393f4d5abSangelos
74460f87bbSderaadtstruct cryptop {
7593f4d5abSangelos	u_int64_t          crp_sid;
7693f4d5abSangelos	int                crp_ilen;
7793f4d5abSangelos	int                crp_olen;
7893f4d5abSangelos	int                crp_alloctype;
7993f4d5abSangelos	int                crp_etype;
8093f4d5abSangelos	int                crp_flags;
81a658e761Stedu	void              *crp_buf;
82a658e761Stedu	void              *crp_opaque;
8393f4d5abSangelos	struct cryptodesc *crp_desc;
8493f4d5abSangelos	int              (*crp_callback)(struct cryptop *);
8569bcbef5Sangelos	struct cryptop    *crp_next;
8669bcbef5Sangelos	caddr_t            crp_mac;
8793f4d5abSangelos};
8893f4d5abSangelos.Ed
8993f4d5abSangelos.Sh DESCRIPTION
9093f4d5abSangelos.Nm
9193f4d5abSangelosis a framework for drivers of cryptographic hardware to register with
9243763bcfSaaronthe kernel so
9343763bcfSaaron.Dq consumers
9443763bcfSaaron(other kernel subsystems, and eventually
9543763bcfSaaronusers through an appropriate device) are able to make use of it.
9643763bcfSaaronDrivers register with the framework the algorithms they support,
9793f4d5abSangelosand provide entry points (functions) the framework may call to
9843763bcfSaaronestablish, use, and tear down sessions.
9943763bcfSaaronSessions are used to cache cryptographic information in a particular driver
10043763bcfSaaron(or associated hardware), so initialization is not needed with every request.
10143763bcfSaaronConsumers of cryptographic services pass a set of
10293f4d5abSangelosdescriptors that instruct the framework (and the drivers registered
10393f4d5abSangeloswith it) of the operations that should be applied on the data (more
10493f4d5abSangelosthan one cryptographic operation can be requested).
10593f4d5abSangelos.Pp
10693f4d5abSangelosSince the consumers may not be associated with a process, drivers may
10793f4d5abSangelosnot use
10893f4d5abSangelos.Xr tsleep 9 .
10943763bcfSaaronThe same holds for the framework.
11043763bcfSaaronThus, a callback mechanism is used
11193f4d5abSangelosto notify a consumer that a request has been completed (the
112282d2818Sjmccallback is specified by the consumer on a per-request basis).
11343763bcfSaaronThe callback is invoked by the framework whether the request was
11443763bcfSaaronsuccessfully completed or not.
11543763bcfSaaronAn error indication is provided in the latter case.
11643763bcfSaaronA specific error code,
11743763bcfSaaron.Er EAGAIN ,
11893f4d5abSangelosis used to indicate that a session number has changed and that the
11943763bcfSaaronrequest may be re-submitted immediately with the new session number.
12043763bcfSaaronErrors are only returned to the invoking function if not
12193f4d5abSangelosenough information to call the callback is available (meaning, there
12243763bcfSaaronwas a fatal error in verifying the arguments).
12343763bcfSaaronFor session initialization and teardown there is no callback mechanism used.
12493f4d5abSangelos.Pp
12593f4d5abSangelosThe
12693f4d5abSangelos.Fn crypto_newsession
12793f4d5abSangelosroutine is called by consumers of cryptographic services (such as the
12893f4d5abSangelos.Xr ipsec 4
12943763bcfSaaronstack) that wish to establish a new session with the framework.
13043763bcfSaaronOn success, the first argument will contain the Session Identifier (SID).
13143763bcfSaaronThe second argument contains all the necessary information for
1324c8a7c32Smpechthe driver to establish the session.
1334c8a7c32SmpechThe third argument indicates whether a
13418280b74Sjmchardware driver should be used (1) or not (0).
1354c8a7c32SmpechThe various fields in the
13643763bcfSaaron.Fa cryptoini
13793f4d5abSangelosstructure are:
13893f4d5abSangelos.Bl -tag -width foobarmoocow
13943763bcfSaaron.It Fa cri_alg
14043763bcfSaaronContains an algorithm identifier.
141717eaa37SmikebCurrently supported encryption algorithms are:
14293f4d5abSangelos.Bd -literal
14393f4d5abSangelosCRYPTO_3DES_CBC
14493f4d5abSangelosCRYPTO_BLF_CBC
14593f4d5abSangelosCRYPTO_CAST_CBC
14669bcbef5SangelosCRYPTO_AES_CBC
14719ae986eSdjmCRYPTO_AES_CTR
14819ae986eSdjmCRYPTO_AES_XTS
149717eaa37Smikeb.Ed
150717eaa37Smikeb.Pp
151717eaa37SmikebAuthentication algorithms are:
152717eaa37Smikeb.Bd -literal
153717eaa37SmikebCRYPTO_MD5_HMAC
154717eaa37SmikebCRYPTO_SHA1_HMAC
155717eaa37SmikebCRYPTO_RIPEMD160_HMAC
156717eaa37SmikebCRYPTO_SHA2_256_HMAC
157717eaa37SmikebCRYPTO_SHA2_384_HMAC
158717eaa37SmikebCRYPTO_SHA2_512_HMAC
159717eaa37Smikeb.Ed
160717eaa37Smikeb.Pp
161717eaa37SmikebAlgorithms performing authenticated encryption are:
162717eaa37Smikeb.Bd -literal
163717eaa37SmikebCRYPTO_AES_GCM_16
164717eaa37SmikebCRYPTO_AES_GMAC
165717eaa37SmikebCRYPTO_CHACHA20_POLY1305
16693f4d5abSangelos.Ed
16743763bcfSaaron.It Fa cri_klen
16893f4d5abSangelosSpecifies the length of the key in bits, for variable-size key
16993f4d5abSangelosalgorithms.
17043763bcfSaaron.It Fa cri_rnd
17193f4d5abSangelosSpecifies the number of rounds to be used with the algorithm, for
17293f4d5abSangelosvariable-round algorithms.
17343763bcfSaaron.It Fa cri_key
17493f4d5abSangelosContains the key to be used with the algorithm.
17543763bcfSaaron.It Fa cri_iv
17693f4d5abSangelosContains an explicit initialization vector (IV), if it does not prefix
17743763bcfSaaronthe data.
17843763bcfSaaronThis field is ignored during initialization.
17943763bcfSaaronIf no IV is explicitly passed (see below on details), a random IV is used
18043763bcfSaaronby the device driver processing the request.
18119ae986eSdjm.Pp
18219ae986eSdjmIn the case of the CRYPTO_AES_XTS transform, the IV should be provided
1832cfb0259Sjmcas a 64-bit block number in host byte order.
18443763bcfSaaron.It Fa cri_next
18593f4d5abSangelosContains a pointer to another
18643763bcfSaaron.Fa cryptoini
18743763bcfSaaronstructure.
18843763bcfSaaronMultiple such structures may be linked to establish multi-algorithm sessions
18943763bcfSaaron.Pf ( Xr ipsec 4
19093f4d5abSangelosis an example consumer of such a feature).
19193f4d5abSangelos.El
19293f4d5abSangelos.Pp
19393f4d5abSangelosThe
19443763bcfSaaron.Fa cryptoini
19593f4d5abSangelosstructure and its contents will not be modified by the framework (or
19643763bcfSaaronthe drivers used).
19743763bcfSaaronSubsequent requests for processing that use the
19893f4d5abSangelosSID returned will avoid the cost of re-initializing the hardware (in
19993f4d5abSangelosessence, SID acts as an index in the session cache of the driver).
20093f4d5abSangelos.Pp
20193f4d5abSangelos.Fn crypto_freesession
20293f4d5abSangelosis called with the SID returned by
20393f4d5abSangelos.Fn crypto_newsession
20493f4d5abSangelosto disestablish the session.
20593f4d5abSangelos.Pp
20693f4d5abSangelos.Fn crypto_dispatch
20743763bcfSaaronis called to process a request.
20843763bcfSaaronThe various fields in the
20943763bcfSaaron.Fa cryptop
21093f4d5abSangelosstructure are:
21193f4d5abSangelos.Bl -tag -width crp_alloctype
21243763bcfSaaron.It Fa crp_sid
21393f4d5abSangelosContains the SID.
21443763bcfSaaron.It Fa crp_ilen
21593f4d5abSangelosIndicates the total length in bytes of the buffer to be processed.
21643763bcfSaaron.It Fa crp_olen
2171932d5a4SjasonOn return, contains the length of the result, not including
2181932d5a4Sjason.Fa crd_skip .
21943763bcfSaaronFor symmetric crypto operations, this will be the same as the input length.
22043763bcfSaaron.It Fa crp_alloctype
22193f4d5abSangelosIndicates the type of buffer, as used in the kernel
22293f4d5abSangelos.Xr malloc 9
22343763bcfSaaronroutine.
22443763bcfSaaronThis will be used if the framework needs to allocate a new
22593f4d5abSangelosbuffer for the result (or for re-formatting the input).
22643763bcfSaaron.It Fa crp_callback
22793f4d5abSangelosThis routine is invoked upon completion of the request, whether
22843763bcfSaaronsuccessful or not.
229f31333f1SangelosIt is invoked through the
230f31333f1Sangelos.Fn crypto_done
231f31333f1Sangelosroutine.
23243763bcfSaaronIf the request was not successful, an error code is set in the
23343763bcfSaaron.Fa crp_etype
23493f4d5abSangelosfield.
235bba14cd0SangelosIt is the responsibility of the callback routine to set the appropriate
236bba14cd0Sangelos.Xr spl 9
237bba14cd0Sangeloslevel.
23843763bcfSaaron.It Fa crp_etype
23993f4d5abSangelosContains the error type, if any errors were encountered, or zero if
24043763bcfSaaronthe request was successfully processed.
24143763bcfSaaronIf the
24243763bcfSaaron.Er EAGAIN
24343763bcfSaaronerror code is returned, the SID has changed (and has been recorded in the
24443763bcfSaaron.Fa crp_sid
24543763bcfSaaronfield).
24643763bcfSaaronThe consumer should record the new SID and use it in all subsequent requests.
24743763bcfSaaronIn this case, the request may be re-submitted immediately.
24843763bcfSaaronThis mechanism is used by the framework to perform
24993f4d5abSangelossession migration (move a session from one driver to another, because
25093f4d5abSangelosof availability, performance, or other considerations).
25193f4d5abSangelos.Pp
25293f4d5abSangelosNote that this field only makes sense when examined by
25393f4d5abSangelosthe callback routine specified in
25443763bcfSaaron.Fa crp_callback .
25593f4d5abSangelosErrors are returned to the invoker of
25693f4d5abSangelos.Fn crypto_process
25793f4d5abSangelosonly when enough information is not present to call the callback
25893f4d5abSangelosroutine (i.e., if the pointer passed is
25993f4d5abSangelos.Dv NULL
26093f4d5abSangelosor if no callback routine was specified).
261136a7998Saaron.It Fa crp_flags
26243763bcfSaaronIs a bitmask of flags associated with this request.
26343763bcfSaaronCurrently defined flags are:
26493f4d5abSangelos.Bl -tag -width CRYPTO_F_IMBUF
26543763bcfSaaron.It Dv CRYPTO_F_IMBUF
26693f4d5abSangelosThe buffer pointed to by
26743763bcfSaaron.Fa crp_buf
26893f4d5abSangelosis an mbuf chain.
26993f4d5abSangelos.El
27043763bcfSaaron.It Fa crp_buf
27143763bcfSaaronPoints to the input buffer.
27243763bcfSaaronOn return (when the callback is invoked),
27343763bcfSaaronit contains the result of the request.
27443763bcfSaaronThe input buffer may be an mbuf
275a658e761Steduchain or a struct uio depending on
27643763bcfSaaron.Fa crp_flags .
277baca17a6Sangelos.It Fa crp_opaque
278baca17a6SangelosThis is passed through the crypto framework untouched and is
27993f4d5abSangelosintended for the invoking application's use.
28043763bcfSaaron.It Fa crp_desc
28143763bcfSaaronThis is a linked list of descriptors.
28243763bcfSaaronEach descriptor provides
28393f4d5abSangelosinformation about what type of cryptographic operation should be done
28443763bcfSaaronon the input buffer.
28543763bcfSaaronThe various fields are:
286282d2818Sjmc.Bl -tag -width "crd_inject"
28743763bcfSaaron.It Fa crd_skip
28893f4d5abSangelosThe offset in the input buffer where processing should start.
28943763bcfSaaron.It Fa crd_len
29093f4d5abSangelosHow many bytes, after
291282d2818Sjmc.Fa crd_skip ,
29293f4d5abSangelosshould be processed.
29343763bcfSaaron.It Fa crd_inject
29443763bcfSaaronOffset from the beginning of the buffer to insert any results.
29543763bcfSaaronFor encryption algorithms, this is where the initialization vector
29693f4d5abSangelos(IV) will be inserted when encrypting or where it can be found when
29793f4d5abSangelosdecrypting (subject to
298282d2818Sjmc.Fa crd_flags ) .
29993f4d5abSangelosFor MAC algorithms, this is where the result of the keyed hash will be
30093f4d5abSangelosinserted.
30143763bcfSaaron.It Fa crd_flags
30293f4d5abSangelosThe following flags are defined:
30343763bcfSaaron.Bl -tag -width CRD_F_IV_EXPLICIT
30443763bcfSaaron.It Dv CRD_F_ENCRYPT
30593f4d5abSangelosFor encryption algorithms, this bit is set when encryption is required
30693f4d5abSangelos(when not set, decryption is performed).
30743763bcfSaaron.It Dv CRD_F_IV_PRESENT
30893f4d5abSangelosFor encryption algorithms, this bit is set when the IV already
30993f4d5abSangelosprecedes the data, so the
31043763bcfSaaron.Fa crd_inject
31143763bcfSaaronvalue will be ignored and no IV will be written in the buffer.
31243763bcfSaaronOtherwise, the IV used to encrypt the packet will be written
31393f4d5abSangelosat the location pointed to by
31443763bcfSaaron.Fa crd_inject .
31593f4d5abSangelosThe IV length is assumed to be equal to the blocksize of the
31643763bcfSaaronencryption algorithm.
31743763bcfSaaronSome applications that do special
31843763bcfSaaron.Dq IV cooking ,
31943763bcfSaaronsuch as the half-IV mode in
32093f4d5abSangelos.Xr ipsec 4 ,
3210626ab20Snaddycan use this flag to indicate that the IV should not be written on the packet.
32243763bcfSaaronThis flag is typically used in conjunction with the
32343763bcfSaaron.Dv CRD_F_IV_EXPLICIT
32493f4d5abSangelosflag.
32543763bcfSaaron.It Dv CRD_F_IV_EXPLICIT
32693f4d5abSangelosFor encryption algorithms, this bit is set when the IV is explicitly
32793f4d5abSangelosprovided by the consumer in the
32843763bcfSaaron.Fa crd_iv
32943763bcfSaaronfields.
33043763bcfSaaronOtherwise, for encryption operations the IV is provided for by
33193f4d5abSangelosthe driver used to perform the operation, whereas for decryption
33293f4d5abSangelosoperations it is pointed to by the
33343763bcfSaaron.Fa crd_inject
33443763bcfSaaronfield.
33543763bcfSaaronThis flag is typically used when the IV is calculated
33643763bcfSaaron.Dq on the fly
33743763bcfSaaronby the consumer, and does not precede the data (some
33893f4d5abSangelos.Xr ipsec 4
33993f4d5abSangelosconfigurations, and the encrypted swap are two such examples).
34069bcbef5Sangelos.It Dv CRD_F_COMP
34169bcbef5SangelosFor compression algorithms, this bit is set when compression is required (when
34269bcbef5Sangelosnot set, decompression is performed).
34393f4d5abSangelos.El
34469bcbef5Sangelos.It Fa CRD_INI
34569bcbef5SangelosThis
34643763bcfSaaron.Fa cryptoini
34769bcbef5Sangelosstructure will not be modified by the framework or the device drivers.
34843763bcfSaaronSince this information accompanies every cryptographic
34993f4d5abSangelosoperation request, drivers may re-initialize state on-demand
35043763bcfSaaron(typically an expensive operation).
35143763bcfSaaronFurthermore, the cryptographic
35293f4d5abSangelosframework may re-route requests as a result of full queues or hardware
35393f4d5abSangelosfailure, as described above.
35443763bcfSaaron.It Fa crd_next
35543763bcfSaaronPoint to the next descriptor.
35643763bcfSaaronLinked operations are useful in protocols such as
35793f4d5abSangelos.Xr ipsec 4 ,
35893f4d5abSangeloswhere multiple cryptographic transforms may be applied on the same
35993f4d5abSangelosblock of data.
36093f4d5abSangelos.El
36193f4d5abSangelos.El
36293f4d5abSangelos.Pp
36393f4d5abSangelos.Fn crypto_getreq
36493f4d5abSangelosallocates a
36543763bcfSaaron.Fa cryptop
36693f4d5abSangelosstructure with a linked list of as many
36743763bcfSaaron.Fa cryptodesc
36893f4d5abSangelosstructures as were specified in the argument passed to it.
36993f4d5abSangelos.Pp
37093f4d5abSangelos.Fn crypto_freereq
37193f4d5abSangelosdeallocates a structure
37243763bcfSaaron.Fa cryptop
37393f4d5abSangelosand any
37443763bcfSaaron.Fa cryptodesc
37543763bcfSaaronstructures linked to it.
37643763bcfSaaronNote that it is the responsibility of the
37793f4d5abSangeloscallback routine to do the necessary cleanups associated with the
378baca17a6Sangelosopaque field in the
37943763bcfSaaron.Fa cryptop
38093f4d5abSangelosstructure.
38193f4d5abSangelos.Sh DRIVER-SIDE API
38293f4d5abSangelosThe
38393f4d5abSangelos.Fn crypto_get_driverid ,
38493f4d5abSangelos.Fn crypto_register ,
385f31333f1Sangelos.Fn crypto_unregister ,
38693f4d5abSangelosand
387f31333f1Sangelos.Fn crypto_done
38893f4d5abSangelosroutines are used by drivers that provide support for cryptographic
38993f4d5abSangelosprimitives to register and unregister with the kernel crypto services
39043763bcfSaaronframework.
39143763bcfSaaronDrivers must first use the
39293f4d5abSangelos.Fn crypto_get_driverid
393460f87bbSderaadtfunction to acquire a driver identifier, specifying the
394460f87bbSderaadt.Fa cc_flags
395460f87bbSderaadtas an argument (normally 0, but software-only drivers should specify
396601d79cdSjmc.Dv CRYPTOCAP_F_SOFTWARE ) .
39743763bcfSaaronFor each algorithm the driver supports, it must then call
39893f4d5abSangelos.Fn crypto_register .
399997a580eSjasonThe first argument is the driver identifier.
400997a580eSjasonThe second argument is an array of
401997a580eSjason.Dv CRYPTO_ALGORITHM_MAX + 1
402997a580eSjasonelements, indicating which algorithms are supported.
403997a580eSjasonThe last three arguments are pointers to three
40493f4d5abSangelosdriver-provided functions that the framework may call to establish new
40593f4d5abSangeloscryptographic context with the driver, free already established
4069c8e991aSjmccontext, and ask for a request to be processed (encrypt, decrypt, etc.\&)
40793f4d5abSangelos.Fn crypto_unregister
40843763bcfSaaronis called by drivers that wish to withdraw support for an algorithm.
40943763bcfSaaronThe two arguments are the driver and algorithm identifiers, respectively.
41043763bcfSaaronTypically, drivers for
41193f4d5abSangelos.Xr pcmcia 4
41293f4d5abSangeloscrypto cards that are being ejected will invoke this routine for all
41393f4d5abSangelosalgorithms supported by the card.
4149939a4a5SangelosIf called with
415*9d8a60ffSkrw.Dv CRYPTO_ALGORITHM_MAX + 1 ,
4169939a4a5Sangelosall algorithms registered for a driver will be unregistered in one go
4179939a4a5Sangelosand the driver will be disabled (no new sessions will be allocated on
4189939a4a5Sangelosthat driver, and any existing sessions will be migrated to other
4199939a4a5Sangelosdrivers).
4209939a4a5SangelosThe same will be done if all algorithms associated with a driver are
4219939a4a5Sangelosunregistered one by one.
42293f4d5abSangelos.Pp
42393f4d5abSangelosThe calling convention for the three driver-supplied routines is:
42493f4d5abSangelos.Bd -literal
42593f4d5abSangelosint (*newsession) (u_int32_t *, struct cryptoini *);
42693f4d5abSangelosint (*freesession) (u_int64_t);
42793f4d5abSangelosint (*process) (struct cryptop *);
42893f4d5abSangelos.Ed
42993f4d5abSangelos.Pp
43093f4d5abSangelosOn invocation, the first argument to
43193f4d5abSangelos.Fn newsession
43293f4d5abSangeloscontains the driver identifier obtained via
43393f4d5abSangelos.Fn crypto_get_driverid .
43493f4d5abSangelosOn successfully returning, it should contain a driver-specific session
43543763bcfSaaronidentifier.
43643763bcfSaaronThe second argument is identical to that of
43793f4d5abSangelos.Fn crypto_newsession .
43893f4d5abSangelos.Pp
43993f4d5abSangelosThe
44093f4d5abSangelos.Fn freesession
44193f4d5abSangelosroutine takes as argument the SID (which is the concatenation of the
44243763bcfSaarondriver identifier and the driver-specific session identifier).
44343763bcfSaaronIt should clear any context associated with the session (clear hardware
44443763bcfSaaronregisters, memory, etc.).
44593f4d5abSangelos.Pp
44693f4d5abSangelosThe
44793f4d5abSangelos.Fn process
44843763bcfSaaronroutine is invoked with a request to perform crypto processing.
44943763bcfSaaronThis routine must not block, but should queue the request and return
45043763bcfSaaronimmediately.
45143763bcfSaaronUpon processing the request, the callback routine should be invoked.
45243763bcfSaaronIn case of error, the error indication must be placed in the
45343763bcfSaaron.Fa crp_etype
45493f4d5abSangelosfield of the
45543763bcfSaaron.Fa cryptop
456f31333f1Sangelosstructure.
457f31333f1SangelosWhen the request is completed, or an error is detected, the
458f31333f1Sangelos.Fn process
45918280b74Sjmcroutine should invoke
460f31333f1Sangelos.Fn crypto_done .
46143763bcfSaaronSession migration may be performed, as mentioned previously.
46293f4d5abSangelos.Sh RETURN VALUES
46393f4d5abSangelos.Fn crypto_register ,
46493f4d5abSangelos.Fn crypto_unregister ,
46593f4d5abSangelos.Fn crypto_newsession ,
46693f4d5abSangelosand
46793f4d5abSangelos.Fn crypto_freesession
46893f4d5abSangelosreturn 0 on success, or an error code on failure.
46993f4d5abSangelos.Fn crypto_get_driverid
47093f4d5abSangelosreturns a non-negative value on error, and \-1 on failure.
47193f4d5abSangelos.Fn crypto_getreq
47293f4d5abSangelosreturns a pointer to a
47343763bcfSaaron.Fa cryptop
47493f4d5abSangelosstructure and
47593f4d5abSangelos.Dv NULL
47693f4d5abSangeloson failure.
47793f4d5abSangelos.Fn crypto_dispatch
47893f4d5abSangelosreturns
47943763bcfSaaron.Er EINVAL
48018280b74Sjmcif its argument or the callback function was
48193f4d5abSangelos.Dv NULL ,
48243763bcfSaaronand 0 otherwise.
48343763bcfSaaronThe callback is provided with an error code in case of failure, in the
48443763bcfSaaron.Fa crp_etype
48593f4d5abSangelosfield.
48643763bcfSaaron.Sh FILES
48743763bcfSaaron.Bl -tag -width sys/crypto/crypto.c
48843763bcfSaaron.It Pa sys/crypto/crypto.c
48943763bcfSaaronmost of the framework code
49043763bcfSaaron.El
49193f4d5abSangelos.Sh SEE ALSO
49293f4d5abSangelos.Xr ipsec 4 ,
49319b5c32fSangelos.Xr pcmcia 4 ,
4947051a84fSaaron.Xr malloc 9 ,
49593f4d5abSangelos.Xr tsleep 9
4967051a84fSaaron.Sh HISTORY
4977051a84fSaaronThe cryptographic framework first appeared in
4987051a84fSaaron.Ox 2.7
499601d79cdSjmcand was written by
500f0641c22Sschwarze.An Angelos D. Keromytis Aq Mt angelos@openbsd.org .
50193f4d5abSangelos.Sh BUGS
50293f4d5abSangelosThe framework currently assumes that all the algorithms in a
50393f4d5abSangelos.Fn crypto_newsession
50443763bcfSaaronoperation must be available by the same driver.
50543763bcfSaaronIf that's not the case, session initialization will fail.
50693f4d5abSangelos.Pp
50793f4d5abSangelosThe framework also needs a mechanism for determining which driver is
50843763bcfSaaronbest for a specific set of algorithms associated with a session.
50943763bcfSaaronSome type of benchmarking is in order here.
51093f4d5abSangelos.Pp
51193f4d5abSangelosMultiple instances of the same algorithm in the same session are not
51243763bcfSaaronsupported.
51393f4d5abSangelos.Pp
51493f4d5abSangelosA queue for completed operations should be implemented and processed
51593f4d5abSangelosat some software
51693f4d5abSangelos.Xr spl 9
51793f4d5abSangeloslevel, to avoid overall system latency issues, and potential kernel
51893f4d5abSangelosstack exhaustion while processing a callback.
519