CRYPTO(9) | Kernel Developer's Manual | CRYPTO(9) |
crypto
— API for
cryptographic services in the kernel
#include
<opencrypto/cryptodev.h>
int32_t
crypto_get_driverid
(device_t,
size_t,
int);
int
crypto_register
(uint32_t,
int,
uint16_t,
uint32_t,
int (*)(void *, uint32_t *,
struct cryptoini *), int
(*)(void *, uint64_t),
int (*)(void *, struct cryptop
*), void *);
int
crypto_kregister
(uint32_t,
int,
uint32_t,
int (*)(void *, struct cryptkop
*), void *);
int
crypto_unregister
(uint32_t,
int);
int
crypto_unregister_all
(uint32_t);
void
crypto_done
(struct
cryptop *);
void
crypto_kdone
(struct
cryptkop *);
int
crypto_find_driver
(const
char *);
int
crypto_newsession
(crypto_session_t
*, struct cryptoini
*, int);
int
crypto_freesession
(crypto_session_t);
int
crypto_dispatch
(struct
cryptop *);
int
crypto_kdispatch
(struct
cryptkop *);
int
crypto_unblock
(uint32_t,
int);
struct cryptop *
crypto_getreq
(int);
void
crypto_freereq
(void);
#define CRYPTO_SYMQ 0x1 #define CRYPTO_ASYMQ 0x2 #define EALG_MAX_BLOCK_LEN 16 struct cryptoini { int cri_alg; int cri_klen; int cri_mlen; caddr_t cri_key; uint8_t cri_iv[EALG_MAX_BLOCK_LEN]; struct cryptoini *cri_next; }; struct cryptodesc { int crd_skip; int crd_len; int crd_inject; int crd_flags; struct cryptoini CRD_INI; #define crd_iv CRD_INI.cri_iv #define crd_key CRD_INI.cri_key #define crd_alg CRD_INI.cri_alg #define crd_klen CRD_INI.cri_klen struct cryptodesc *crd_next; }; struct cryptop { TAILQ_ENTRY(cryptop) crp_next; crypto_session_t crp_session; int crp_ilen; int crp_olen; int crp_etype; int crp_flags; caddr_t crp_buf; caddr_t crp_opaque; struct cryptodesc *crp_desc; int (*crp_callback) (struct cryptop *); caddr_t crp_mac; }; struct crparam { caddr_t crp_p; u_int crp_nbits; }; #define CRK_MAXPARAM 8 struct cryptkop { TAILQ_ENTRY(cryptkop) krp_next; u_int krp_op; /* ie. CRK_MOD_EXP or other */ u_int krp_status; /* return status */ u_short krp_iparams; /* # of input parameters */ u_short krp_oparams; /* # of output parameters */ uint32_t krp_hid; struct crparam krp_param[CRK_MAXPARAM]; int (*krp_callback)(struct cryptkop *); };
crypto
is a framework for drivers of
cryptographic hardware to register with the kernel so
“consumers” (other kernel subsystems, and users through the
/dev/crypto device) are able to make use of it.
Drivers register with the framework the algorithms they support, and provide
entry points (functions) the framework may call to establish, use, and tear
down sessions. Sessions are used to cache cryptographic information in a
particular driver (or associated hardware), so initialization is not needed
with every request. Consumers of cryptographic services pass a set of
descriptors that instruct the framework (and the drivers registered with it)
of the operations that should be applied on the data (more than one
cryptographic operation can be requested).
Keying operations are supported as well. Unlike the symmetric operators described above, these sessionless commands perform mathematical operations using input and output parameters.
Since the consumers may not be associated with a process, drivers
may not sleep(9). The same holds for the framework. Thus,
a callback mechanism is used to notify a consumer that a request has been
completed (the callback is specified by the consumer on a per-request
basis). The callback is invoked by the framework whether the request was
successfully completed or not. An error indication is provided in the latter
case. A specific error code, EAGAIN
, is used to
indicate that a session handle has changed and that the request may be
re-submitted immediately with the new session. Errors are only returned to
the invoking function if not enough information to call the callback is
available (meaning, there was a fatal error in verifying the arguments). For
session initialization and teardown no callback mechanism is used.
The
crypto_find_driver
()
function may be called to return the specific id of the provided name. If
the specified driver could not be found, the returned id is -1.
The
crypto_newsession
()
routine is called by consumers of cryptographic services (such as the
ipsec(4) stack) that wish to establish a new session with
the framework. The second argument contains all the necessary information
for the driver to establish the session. The third argument is either a
specific driver id, or one or both of
CRYPTOCAP_F_HARDWARE
, to select hardware devices, or
CRYPTOCAP_F_SOFTWARE
, to select software devices. If
both are specified, a hardware device will be returned before a software
device will be. On success, the value pointed to by the first argument will
be the opaque session handle. The various fields in the
cryptoini structure are:
CRYPTO_AES_128_NIST_GMAC
CRYPTO_AES_192_NIST_GMAC
CRYPTO_AES_256_NIST_GMAC
CRYPTO_AES_CBC
CRYPTO_AES_ICM
CRYPTO_AES_NIST_GCM_16
CRYPTO_AES_NIST_GMAC
CRYPTO_AES_XTS
CRYPTO_ARC4
CRYPTO_BLF_CBC
CRYPTO_CAMELLIA_CBC
CRYPTO_CAST_CBC
CRYPTO_DEFLATE_COMP
CRYPTO_DES_CBC
CRYPTO_3DES_CBC
CRYPTO_MD5
CRYPTO_MD5_HMAC
CRYPTO_MD5_KPDK
CRYPTO_NULL_HMAC
CRYPTO_NULL_CBC
CRYPTO_RIPEMD160_HMAC
CRYPTO_SHA1
CRYPTO_SHA1_HMAC
CRYPTO_SHA1_KPDK
CRYPTO_SHA2_256_HMAC
CRYPTO_SHA2_384_HMAC
CRYPTO_SHA2_512_HMAC
CRYPTO_SKIPJACK_CBC
crypto_newsession
). If no IV is explicitly passed
(see below on details), a random IV is used by the device driver
processing the request.The cryptoini structure and its contents will not be modified by the framework (or the drivers used).
crypto_freesession
()
is called with the session handle returned by
crypto_newsession
() to free the session.
crypto_dispatch
()
is called to process a request. The various fields in the
cryptop structure are:
crypto_done
()
routine. If the request was not successful, an error code is set in the
crp_etype field. It is the responsibility of the
callback routine to set the appropriate spl(9)
level.EAGAIN
error code is returned, the session handle has changed (and has been
recorded in the crp_session field). The consumer
should record the new session handle and use it in all subsequent
requests. In this case, the request may be re-submitted immediately. This
mechanism is used by the framework to perform session migration (move a
session from one driver to another, because of availability, performance,
or other considerations).
Note that this field only makes sense when
examined by the callback routine specified in
crp_callback. Errors are returned to the invoker
of
crypto_process
()
only when enough information is not present to call the callback routine
(i.e., if the pointer passed is NULL
or if no
callback routine was specified).
CRYPTO_F_IMBUF
CRYPTO_F_IOV
CRYPTO_F_BATCH
CRYPTO_F_CBIMM
CRYPTO_F_DONE
CRYPTO_F_CBIFSYNC
CRYPTOCAP_F_SYNC
flag).CRYPTO_F_ASYNC
CRYPTOCAP_F_SYNC
flag). It aims to speed up
processing by dispatching crypto operations on different
processors.CRYPTO_F_ASYNC_KEEPORDER
CRYPTO_F_ASYNC
flag is set and if the
operation is synchronous.CRD_F_IV_EXPLICIT
is set, this
field contains the IV.CRD_F_KEY_EXPLICIT
flag is set, the
crd_key points to a buffer with encryption or
authentication key.CRD_F_ENCRYPT
CRD_F_IV_PRESENT
CRD_F_IV_EXPLICIT
flag.CRD_F_IV_EXPLICIT
CRD_F_KEY_EXPLICIT
CRD_F_COMP
crypto_getreq
()
allocates a cryptop structure with a linked list of as
many cryptodesc structures as were specified in the
argument passed to it.
crypto_freereq
()
deallocates a structure cryptop and any
cryptodesc structures linked to it. Note that it is
the responsibility of the callback routine to do the necessary cleanups
associated with the opaque field in the cryptop
structure.
crypto_kdispatch
()
is called to perform a keying operation. The various fields in the
cryptkop structure are:
CRK_MOD_EXP
.The crypto_get_driverid
(),
crypto_get_driver_session
(),
crypto_register
(),
crypto_kregister
(),
crypto_unregister
(),
crypto_unblock
(), and
crypto_done
() routines are used by drivers that
provide support for cryptographic primitives to register and unregister with
the kernel crypto services framework.
Drivers must first use the
crypto_get_driverid
()
function to acquire a driver identifier, specifying the
flags as an argument. One of
CRYPTOCAP_F_SOFTWARE
or
CRYPTOCAP_F_HARDWARE
must be specified. The
CRYPTOCAP_F_SYNC
may also be specified, and should
be specified if the driver does all of it's operations synchronously.
Drivers must pass the size of their session struct as the second argument.
An appropriately sized memory will be allocated by the framework, zeroed,
and passed to the driver's
newsession
()
method.
For each algorithm the driver supports, it
must then call
crypto_register
().
The first two arguments are the driver and algorithm identifiers. The next
two arguments specify the largest possible operator length (in bits,
important for public key operations) and flags for this algorithm. The last
four arguments must be provided in the first call to
crypto_register
() and are ignored in all subsequent
calls. They are pointers to three driver-provided functions that the
framework may call to establish new cryptographic context with the driver,
free already established context, and ask for a request to be processed
(encrypt, decrypt, etc.); and an opaque parameter to pass when calling each
of these routines.
crypto_unregister
()
is called by drivers that wish to withdraw support for an algorithm. The two
arguments are the driver and algorithm identifiers, respectively. Typically,
drivers for PCMCIA crypto cards that are being ejected will invoke this
routine for all algorithms supported by the card.
crypto_unregister_all
()
will unregister all algorithms registered by a driver and the driver will be
disabled (no new sessions will be allocated on that driver, and any existing
sessions will be migrated to other drivers). The same will be done if all
algorithms associated with a driver are unregistered one by one. After a
call to crypto_unregister_all
() there will be no
threads in either the newsession or freesession function of the driver.
The calling convention for the driver-supplied routines are:
(*newsession)
(device_t,
crypto_session_t, struct cryptoini
*);(*freesession)
(device_t,
crypto_session_t);(*process)
(device_t,
struct cryptop *, int);(*kprocess)
(device_t,
struct cryptkop *, int);On invocation, the first argument to
all routines is the device_t that was provided to
crypto_get_driverid
().
The second argument to
newsession
()
is the opaque session handle for the new session. The third argument is
identical to that of crypto_newsession
().
Drivers obtain a pointer to their
session memory by invoking
crypto_get_driver_session
()
on the opaque crypto_session_t handle.
The
freesession
()
routine takes as arguments the opaque data value and the session handle. It
should clear any context associated with the session (clear hardware
registers, memory, etc.). If no resources need to be released other than the
contents of session memory, the method is optional. The
crypto
framework will zero and release the allocated
session memory (after running the freesession
()
method, if one exists).
The
process
()
routine is invoked with a request to perform crypto processing. This routine
must not block or sleep, but should queue the request and return immediately
or process the request to completion. In case of an unrecoverable error, the
error indication must be placed in the crp_etype field
of the cryptop structure. When the request is
completed, or an error is detected, the process
()
routine must invoke crypto_done
(). Session migration
may be performed, as mentioned previously.
In case of a temporary resource exhaustion, the
process
()
routine may return ERESTART
in which case the crypto
services will requeue the request, mark the driver as
“blocked”, and stop submitting requests for processing. The
driver is then responsible for notifying the crypto services when it is
again able to process requests through the
crypto_unblock
()
routine. This simple flow control mechanism should only be used for
short-lived resource exhaustion as it causes operations to be queued in the
crypto layer. Doing so is preferable to returning an error in such cases as
it can cause network protocols to degrade performance by treating the
failure much like a lost packet.
The
kprocess
()
routine is invoked with a request to perform crypto key processing. This
routine must not block, but should queue the request and return immediately.
Upon processing the request, the callback routine should be invoked. In case
of an unrecoverable error, the error indication must be placed in the
krp_status field of the cryptkop
structure. When the request is completed, or an error is detected, the
kprocess
() routine should invoked
crypto_kdone
().
crypto_register
(),
crypto_kregister
(),
crypto_unregister
(),
crypto_newsession
(),
crypto_freesession
(), and
crypto_unblock
() return 0 on success, or an error
code on failure. crypto_get_driverid
() returns a
non-negative value on error, and -1 on failure.
crypto_getreq
() returns a pointer to a
cryptop structure and NULL
on
failure. crypto_dispatch
() returns
EINVAL
if its argument or the callback function was
NULL
, and 0 otherwise. The callback is provided with
an error code in case of failure, in the crp_etype
field.
The cryptographic framework first appeared in OpenBSD 2.7 and was written by Angelos D. Keromytis <angelos@openbsd.org>.
The framework currently assumes that all the algorithms in a
crypto_newsession
() operation must be available by
the same driver. If that is not the case, session initialization will
fail.
The framework also needs a mechanism for determining which driver is best for a specific set of algorithms associated with a session. Some type of benchmarking is in order here.
Multiple instances of the same algorithm in the same session are not supported.
July 17, 2018 | Debian |