[tahoe-dev] Running Tahoe on ARM plugs
Greg Troxel
gdt at ir.bbn.com
Sun Feb 20 04:44:20 PST 2011
> but unfortunately it
> appears like the crypto processor is only accessible from kernel mode
...
> so the distro, or the user, would
> have to patch them in - and the number of people who are going to roll
> their own custom patched kernel is pretty small compared to the number
> of people who might theoretically want to run Tahoe on a plug).
Well, if people aren't willing and able to do that, then they could
run Davies-Meyer-AES-128 in software. I wonder how efficient that
would be.
I don't quite follow how a device only being accessible from the kernel
(which is true for substantially all devices except perhaps via libusb)
leads to custom kernels, but presumably mainstream Linux maintenance is
unwilling to add support for these coprocessors.
This problem has been pretty much solved in *BSD, via the opencrypto
framework. Each accelerator has a driver, there's a kernel-mode API,
user-space access to the operations, and integration with OpenSSL.
Support for AES and SHA-1 is usually better than for RSA within this
framework.
I just bought a Soekris net5501, and also the hifn-based crypto
coprocessor, so I'll see how well this works in practice.
http://portal.acm.org/citation.cfm?id=1250980
http://2009.asiabsdcon.org/papers/abc2009-P1B-paper.pdf
CRYPTO(4) NetBSD Kernel Interfaces Manual CRYPTO(4)
NAME
crypto, swcrypto -- user-mode access to hardware-accelerated cryptography
SYNOPSIS
hifn* at pci? dev ? function ?
ubsec* at pci? dev ? function ?
pseudo-device crypto
pseudo-device swcrypto
#include <sys/ioctl.h>
#include <sys/time.h>
#include <crypto/cryptodev.h>
DESCRIPTION
The crypto driver gives user-mode applications access to hardware-accel-
erated cryptographic transforms, as implemented by the opencrypto(9) in-
kernel interface. The swcrypto driver is a software-only implementation
of the opencrypto(9) interface, and must be included to use the interface
without hardware acceleration. The /dev/crypto special device provides
an ioctl(2) based interface. User-mode applications should open the spe-
cial device, then issue ioctl(2) calls on the descriptor. The crypto
device provides two distinct modes of operation: one mode for symmetric-
keyed cryptographic requests, and a second mode for both asymmetric-key
(public-key/private-key) requests, and for modular arithmetic (for
Diffie-Hellman key exchange and other cryptographic protocols). The two
modes are described separately below.
THEORY OF OPERATION
Regardless of whether symmetric-key or asymmetric-key operations are to
be performed, use of the device requires a basic series of steps:
1. Open a file descriptor for the device. See open(2).
2. If any symmetric operation will be performed, create one session,
with CIOCGSESSION, or multiple sessions, with CIOCNGSESSION. Most
applications will require at least one symmetric session. Since
cipher and MAC keys are tied to sessions, many applications will
require more. Asymmetric operations do not use sessions.
3. Submit requests, synchronously with CIOCCRYPT (symmetric) or
CIOCFKEY (asymmetric) or asynchronously with CIOCNCRYPTM (symmetric)
or CIOCNFKEYM (asymmetric). The asynchronous interface allows mul-
tiple requests to be submitted in one call if the user so desires.
4. If the asynchronous interface is used, wait for results with
select(2) or poll(2), then collect them with CIOCNCRYPTRET (a par-
ticular request) or CIOCNCRYPTRETM (multiple requests).
5. Destroy one session with CIOCFSESSION or many at once with
CIOCNFSESSION.
6. Close the device with close(2).
SYMMETRIC-KEY OPERATION
The symmetric-key operation mode provides a context-based API to tradi-
tional symmetric-key encryption (or privacy) algorithms, or to keyed and
unkeyed one-way hash (HMAC and MAC) algorithms. The symmetric-key mode
also permits fused operation, where the hardware performs both a privacy
algorithm and an integrity-check algorithm in a single pass over the
data: either a fused encrypt/HMAC-generate operation, or a fused HMAC-
verify/decrypt operation.
To use symmetric mode, you must first create a session specifying the
algorithm(s) and key(s) to use; then issue encrypt or decrypt requests
against the session.
Symmetric-key privacy algorithms
Contingent upon device drivers for installed cryptographic hardware reg-
istering with opencrypto(9), as providers of a given algorithm, some or
all of the following symmetric-key privacy algorithms may be available:
CRYPTO_DES_CBC
CRYPTO_3DES_CBC
CRYPTO_BLF_CBC
CRYPTO_CAST_CBC
CRYPTO_SKIPJACK_CBC
CRYPTO_AES_CBC
CRYPTO_ARC4
Integrity-check operations
Contingent upon hardware support, some or all of the following keyed one-
way hash algorithms may be available:
CRYPTO_RIPEMD160_HMAC
CRYPTO_MD5_KPDK
CRYPTO_SHA1_KPDK
CRYPTO_MD5_HMAC
CRYPTO_SHA1_HMAC
CRYPTO_SHA2_HMAC
CRYPTO_MD5
CRYPTO_SHA1
The CRYPTO_MD5 and CRYPTO_SHA1 algorithms are actually unkeyed, but
should be requested as symmetric-key hash algorithms with a zero-length
key.
IOCTL Request Descriptions
CRIOGET int *fd
This operation is deprecated and will be removed after
NetBSD 5.0. It clones the fd argument to ioctl(4), yielding a
new file descriptor for the creation of sessions. Because the
device now clones on open, this operation is unnecessary.
CIOCGSESSION struct session_op *sessp
struct session_op {
u_int32_t cipher; /* e.g. CRYPTO_DES_CBC */
u_int32_t mac; /* e.g. CRYPTO_MD5_HMAC */
u_int32_t keylen; /* cipher key */
void * key;
int mackeylen; /* mac key */
void * mackey;
u_int32_t ses; /* returns: ses # */
};
Create a new cryptographic session on a file descriptor for the
device; that is, a persistent object specific to the chosen
privacy algorithm, integrity algorithm, and keys specified in
sessp. The special value 0 for either privacy or integrity is
reserved to indicate that the indicated operation (privacy or
integrity) is not desired for this session.
Multiple sessions may be bound to a single file descriptor.
The session ID returned in sessp->ses is supplied as a required
field in the symmetric-operation structure crypt_op for future
encryption or hashing requests.
This implementation will never return a session ID of 0 for a
successful creation of a session, which is a NetBSD extension.
For non-zero symmetric-key privacy algorithms, the privacy
algorithm must be specified in sessp->cipher, the key length in
sessp->keylen, and the key value in the octets addressed by
sessp->key.
For keyed one-way hash algorithms, the one-way hash must be
specified in sessp->mac, the key length in sessp->mackey, and
the key value in the octets addressed by sessp->mackeylen.
Support for a specific combination of fused privacy and
integrity-check algorithms depends on whether the underlying
hardware supports that combination. Not all combinations are
supported by all hardware, even if the hardware supports each
operation as a stand-alone non-fused operation.
CIOCNGSESSION struct crypt_sgop *sgop
struct crypt_sgop {
size_t count; /* how many */
struct session_n_op * sessions; /* where to get them */
};
struct session_n_op {
u_int32_t cipher; /* e.g. CRYPTO_DES_CBC */
u_int32_t mac; /* e.g. CRYPTO_MD5_HMAC */
u_int32_t keylen; /* cipher key */
void * key;
u_int32_t mackeylen; /* mac key */
void * mackey;
u_int32_t ses; /* returns: session # */
int status;
};
Create one or more sessions. Takes a counted array of
session_n_op structures in sgop. For each requested session
(array element n), the session number is returned in
sgop->sessions[n].ses and the status for that session creation
in sgop->sessions[n].status.
CIOCCRYPT struct crypt_op *cr_op
struct crypt_op {
u_int32_t ses;
u_int16_t op; /* e.g. COP_ENCRYPT */
u_int16_t flags;
u_int len;
void * src, *dst;
void * mac; /* must be large enough for result */
void * iv;
};
Request a symmetric-key (or hash) operation. The file descrip-
tor argument to ioctl(4) must have been bound to a valid ses-
sion. To encrypt, set cr_op->op to COP_ENCRYPT. To decrypt,
set cr_op->op to COP_DECRYPT. The field cr_op->len supplies
the length of the input buffer; the fields cr_op->src,
cr_op->dst, cr_op->mac, cr_op->iv supply the addresses of the
input buffer, output buffer, one-way hash, and initialization
vector, respectively.
CIOCNCRYPTM struct crypt_mop *cr_mop
struct crypt_mop {
size_t count; /* how many */
struct crypt_n_op * reqs; /* where to get them */
};
struct crypt_n_op {
u_int32_t ses;
u_int16_t op; /* e.g. COP_ENCRYPT */
u_int16_t flags;
u_int len;
u_int32_t reqid; /* request id */
int status; /* accepted or not */
void *opaque; /* opaque pointer ret to user */
u_int32_t keylen; /* cipher key - optional */
void * key;
u_int32_t mackeylen; /* mac key - optional */
void * mackey;
void * src, * dst;
void * mac;
void * iv;
};
This is the asynchronous version of CIOCCRYPT, which allows
multiple symmetric-key (or hash) operations to be started (see
CIOCRYPT above for the details for each operation).
The cr_mop->count field specifies the number of operations pro-
vided in the cr_mop->reqs array.
Each operation is assigned a unique request id returned in the
cr_mop->reqs[n].reqid field.
Each operation can accept an opaque value from the user to be
passed back to the user when the operation completes ((e.g. to
track context for the request). The opaque field is
cr_mop->reqs[n].opaque.
If a problem occurs with starting any of the operations then
that operation's cr_mop->reqs[n].status field is filled with
the error code. The failure of an operation does not prevent
the other operations from being started.
The select(2) or poll(2) functions must be used on the device
file descriptor to detect that some operation has completed;
results are then retrieved with CIOCNCRYPTRETM.
The key and mackey fields of the operation structure are cur-
rently unused. They are intended for use to immediately rekey
an existing session before processing a new request.
CIOCFSESSION void
Destroys the /dev/crypto session associated with the file-
descriptor argument.
CIOCNFSESSION struct crypt_sfop *sfop;
struct crypt_sfop {
size_t count;
u_int32_t *sesid;
};
Destroys the sfop->count sessions specified by the sfop array
of session identifiers.
ASYMMETRIC-KEY OPERATION
Asymmetric-key algorithms
Contingent upon hardware support, the following asymmetric (public-
key/private-key; or key-exchange subroutine) operations may also be
available:
Algorithm Input parameter Output parameter
Count Count
CRK_MOD_EXP 3 1
CRK_MOD_EXP_CRT 6 1
CRK_MOD_ADD 3 1
CRK_MOD_ADDINV 2 1
CRK_MOD_SUB 3 1
CRK_MOD_MULT 3 1
CRK_MOD_MULTINV 2 1
CRK_MOD 2 1
CRK_DSA_SIGN 5 2
CRK_DSA_VERIFY 7 0
CRK_DH_COMPUTE_KEY 3 1
See below for discussion of the input and output parameter counts.
Asymmetric-key commands
CIOCASYMFEAT int *feature_mask
Returns a bitmask of supported asymmetric-key operations. Each
of the above-listed asymmetric operations is present if and
only if the bit position numbered by the code for that opera-
tion is set. For example, CRK_MOD_EXP is available if and only
if the bit (1 << CRK_MOD_EX) is set.
CIOCFKEY struct crypt_kop *kop
struct crypt_kop {
u_int crk_op; /* e.g. CRK_MOD_EXP */
u_int crk_status; /* return status */
u_short crk_iparams; /* # of input params */
u_short crk_oparams; /* # of output params */
u_int crk_pad1;
struct crparam crk_param[CRK_MAXPARAM];
};
/* Bignum parameter, in packed bytes. */
struct crparam {
void * crp_p;
u_int crp_nbits;
};
Performs an asymmetric-key operation from the list above. The
specific operation is supplied in kop->crk_op; final status for
the operation is returned in kop->crk_status. The number of
input arguments and the number of output arguments is specified
in kop->crk_iparams and kop->crk_iparams, respectively. The
field crk_param[] must be filled in with exactly
kop->crk_iparams + kop->crk_oparams arguments, each encoded as
a struct crparam (address, bitlength) pair.
The semantics of these arguments are currently undocumented.
CIOCNFKEYM struct crypt_mkop *mkop
struct crypt_mkop {
size_t count; /* how many */
struct crypt_n_op * reqs; /* where to get them */
};
struct crypt_n_kop {
u_int crk_op; /* e.g. CRK_MOD_EXP */
u_int crk_status; /* accepted or not */
u_short crk_iparams; /* # of input params */
u_short crk_oparams; /* # of output params */
u_int32_t crk_reqid; /* request id */
struct crparam crk_param[CRK_MAXPARAM];
void *crk_opaque; /* opaque pointer ret to user */
};
This is the asynchronous version of CIOCFKEY, which starts one
or more key operations. See CIOCNCRYPTM above and
CIOCNCRYPTRETM below for descriptions of the mkop>count,
mkop>reqs, mkop>reqs[n].crk_reqid, mkop>reqs[n].crk_status, and
mkop>reqs[n].crk_opaque fields of the argument structure, and
result retrieval.
Asynchronous status commands
When requests are submitted with the CIOCNCRYPTM or CIOCNFKEYM commands,
result retrieval is asynchronous (the submit ioctls return immediately).
Use the select(2) or poll(2) functions to determine when the file
descriptor has completed operations ready to be retrieved.
CIOCNCRYPTRET struct crypt_result *cres
struct crypt_result {
u_int32_t reqid; /* request ID */
u_int32_t status; /* 0 if successful */
void * opaque; /* pointer from user */
};
Check for the status of the request specified by cres->reqid.
This requires a linear search through all completed requests
and should be used with extreme care if the number of requests
pending on this file descriptor may be large.
The cres->status field is set as follows:
0 The request has completed, and its results have
been copied out to the original crypt_n_op or
crypt_n_kop structure used to start the request.
The copyout occurs during this ioctl, so the call-
ing process must be the process that started the
request.
EINPROGRESS The request has not yet completed.
EINVAL The request was not found.
Other values indicate a problem during the processing of the
request.
CIOCNCRYPTRETM struct cryptret_t *cret
struct cryptret {
size_t count; /* space for how many */
struct crypt_result * results; /* where to put them */
};
Retrieve a number of completed requests. This ioctl accepts a
count and an array (each array element is a crypt_result_t
structure as used by CIOCNCRYPTRET above) and fills the array
with up to cret->count results of completed requests.
This ioctl fills in the cret->results[n].reqid field, so that
the request which has completed may be identified by the appli-
cation. Note that the results may include requests submitted
both as symmetric and asymmetric operations.
SEE ALSO
hifn(4), ubsec(4), opencrypto(9)
HISTORY
The crypto driver is derived from a version which appeared in
FreeBSD 4.8, which in turn is based on code which appeared in
OpenBSD 3.2.
The "new API" for asynchronous operation with multiple basic operations
per system call (the "N" ioctl variants) was contributed by Coyote Point
Systems, Inc. and first appeared in NetBSD 5.0.
BUGS
Error checking and reporting is weak.
The values specified for symmetric-key key sizes to CIOCGSESSION must
exactly match the values expected by opencrypto(9). The output buffer
and MAC buffers supplied to CIOCCRYPT must follow whether privacy or
integrity algorithms were specified for session: if you request a
non-NULL algorithm, you must supply a suitably-sized buffer.
The scheme for passing arguments for asymmetric requests is Baroque.
The naming inconsistency between CRIOGET and the various CIOC* names is
an unfortunate historical artifact.
NetBSD 5.1 March 29, 2008 NetBSD 5.1
OPENCRYPTO(9) NetBSD Kernel Developer's Manual OPENCRYPTO(9)
NAME
opencrypto, crypto_get_driverid, crypto_register, crypto_kregister,
crypto_unregister, crypto_done, crypto_kdone, crypto_newsession,
crypto_freesession, crypto_dispatch, crypto_kdispatch, crypto_getreq,
crypto_freereq -- API for cryptographic services in the kernel
SYNOPSIS
#include <opencrypto/cryptodev.h>
int32_t
crypto_get_driverid(u_int32_t);
int
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 *);
int
crypto_kregister(u_int32_t, int, u_int32_t,
int (*)(void *, struct cryptkop *, int), void *);
int
crypto_unregister(u_int32_t, int);
void
crypto_done(struct cryptop *);
void
crypto_kdone(struct cryptkop *);
int
crypto_newsession(u_int64_t *, struct cryptoini *, int);
int
crypto_freesession(u_int64_t);
int
crypto_dispatch(struct cryptop *);
int
crypto_kdispatch(struct cryptkop *);
struct cryptop *
crypto_getreq(int);
void
crypto_freereq(struct cryptop *);
#define EALG_MAX_BLOCK_LEN 16
struct cryptoini {
int cri_alg;
int cri_klen;
int cri_rnd;
void *cri_key;
u_int8_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;
struct cryptodesc *crd_next;
};
struct cryptop {
TAILQ_ENTRY(cryptop) crp_next;
u_int64_t crp_sid;
int crp_ilen;
int crp_olen;
int crp_etype;
int crp_flags;
void *crp_buf;
void *crp_opaque;
struct cryptodesc *crp_desc;
int (*crp_callback)(struct cryptop *);
void *crp_mac;
};
struct crparam {
void *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 */
u_int32_t krp_hid;
struct crparam krp_param[CRK_MAXPARAM]; /* kvm */
int (*krp_callback)(struct cryptkop *);
};
DESCRIPTION
opencrypto is a framework for drivers of cryptographic hardware to regis-
ter with the kernel so ``consumers'' (other kernel subsystems, and even-
tually users through an appropriate device) are able to make use of it.
Drivers register with the framework the algorithms they support, and pro-
vide entry points (functions) the framework may call to establish, use,
and tear down sessions. Sessions are used to cache cryptographic infor-
mation 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 opera-
tions using input and output parameters.
Since the consumers may not be associated with a process, drivers may not
use condition variables: condvar(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 an per-
request basis). The callback is invoked by the framework whether the
request was successfully completed or not. An error indication is pro-
vided in the latter case. A specific error code, EAGAIN, is used to
indicate that a session number has changed and that the request may be
re-submitted immediately with the new session number. 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). No callback mechanism is used for session initialization and
teardown.
The crypto_newsession() routine is called by consumers of cryptographic
services (such as the ipsec(4) stack) that wish to establish a new ses-
sion with the framework. On success, the first argument will contain the
Session Identifier (SID). The second argument contains all the necessary
information for the driver to establish the session. The third argument
indicates whether a hardware driver should be used (1) or not (0). The
various fields in the cryptoini structure are:
cri_alg Contains an algorithm identifier. Currently supported
algorithms are:
CRYPTO_DES_CBC
CRYPTO_3DES_CBC
CRYPTO_BLF_CBC
CRYPTO_CAST_CBC
CRYPTO_SKIPJACK_CBC
CRYPTO_MD5_HMAC
CRYPTO_SHA1_HMAC
CRYPTO_RIPEMD160_HMAC
CRYPTO_MD5_KPDK
CRYPTO_SHA1_KPDK
CRYPTO_AES_CBC
CRYPTO_ARC4
CRYPTO_MD5
CRYPTO_SHA1
cri_klen Specifies the length of the key in bits, for variable-size
key algorithms.
cri_rnd Specifies the number of rounds to be used with the algo-
rithm, for variable-round algorithms.
cri_key Contains the key to be used with the algorithm.
cri_iv Contains an explicit initialization vector (IV), if it does
not prefix the data. This field is ignored during initial-
ization. If no IV is explicitly passed (see below on
details), a random IV is used by the device driver process-
ing the request.
cri_next Contains a pointer to another cryptoini structure. Multi-
ple such structures may be linked to establish multi-algo-
rithm sessions (ipsec(4) is an example consumer of such a
feature).
The cryptoini structure and its contents will not be modified by the
framework (or the drivers used). Subsequent requests for processing that
use the SID returned will avoid the cost of re-initializing the hardware
(in essence, SID acts as an index in the session cache of the driver).
crypto_freesession() is called with the SID returned by
crypto_newsession() to disestablish the session.
crypto_dispatch() is called to process a request. The various fields in
the cryptop structure are:
crp_sid Contains the SID.
crp_ilen Indicates the total length in bytes of the buffer to be
processed.
crp_olen On return, contains the length of the result, not including
crd_skip. For symmetric crypto operations, this will be
the same as the input length.
crp_alloctype
Indicates the type of buffer, as used in the kernel
malloc(9) routine. This will be used if the framework
needs to allocate a new buffer for the result (or for re-
formatting the input).
crp_callback This routine is invoked upon completion of the request,
whether successful or not. It is invoked through the
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 appropri-
ate spl(9) level.
crp_etype Contains the error type, if any errors were encountered, or
zero if the request was successfully processed. If the
EAGAIN error code is returned, the SID has changed (and has
been recorded in the crp_sid field). The consumer should
record the new SID 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 considera-
tions).
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 rou-
tine (i.e., if the pointer passed is NULL or if no callback
routine was specified).
crp_flags Is a bitmask of flags associated with this request. Cur-
rently defined flags are:
CRYPTO_F_IMBUF The buffer pointed to by crp_buf is an mbuf
chain.
crp_buf Points to the input buffer. On return (when the callback
is invoked), it contains the result of the request. The
input buffer may be an mbuf chain or a contiguous buffer
(of a type identified by crp_alloctype), depending on
crp_flags.
crp_opaque This is passed through the crypto framework untouched and
is intended for the invoking application's use.
crp_desc This is a linked list of descriptors. Each descriptor pro-
vides information about what type of cryptographic opera-
tion should be done on the input buffer. The various
fields are:
crd_skip The offset in the input buffer where process-
ing should start.
crd_len How many bytes, after crd_skip, should be
processed.
crd_inject Offset from the beginning of the buffer to
insert any results. For encryption algo-
rithms, this is where the initialization vec-
tor (IV) will be inserted when encrypting or
where it can be found when decrypting (sub-
ject to crd_flags). For MAC algorithms, this
is where the result of the keyed hash will be
inserted.
crd_flags For adjusting general operation from user-
land, the following flags are defined:
CRD_F_ENCRYPT For encryption algorithms,
this bit is set when
encryption is required
(when not set, decryption
is performed).
CRD_F_IV_PRESENT For encryption algorithms,
this bit is set when the
IV already precedes the
data, so the crd_inject
value will be ignored and
no IV will be written in
the buffer. Otherwise,
the IV used to encrypt the
packet will be written at
the location pointed to by
crd_inject. The IV length
is assumed to be equal to
the blocksize of the
encryption algorithm.
Some applications that do
special ``IV cooking'',
such as the half-IV mode
in ipsec(4), can use this
flag to indicate that the
IV should not be written
on the packet. This flag
is typically used in con-
junction with the
CRD_F_IV_EXPLICIT flag.
CRD_F_IV_EXPLICIT For encryption algorithms,
this bit is set when the
IV is explicitly provided
by the consumer in the
crd_iv fields. Otherwise,
for encryption operations
the IV is provided for by
the driver used to perform
the operation, whereas for
decryption operations it
is pointed to by the
crd_inject field. This
flag is typically used
when the IV is calculated
``on the fly'' by the con-
sumer, and does not pre-
cede the data (some
ipsec(4) configurations,
and the encrypted swap are
two such examples).
CRD_F_COMP For compression algo-
rithms, this bit is set
when compression is
required (when not set,
decompression is per-
formed).
CRD_INI This cryptoini structure will not be modified
by the framework or the device drivers.
Since this information accompanies every
cryptographic operation request, drivers may
re-initialize state on-demand (typically an
expensive operation). Furthermore, the cryp-
tographic framework may re-route requests as
a result of full queues or hardware failure,
as described above.
crd_next Point to the next descriptor. Linked opera-
tions are useful in protocols such as
ipsec(4), where multiple cryptographic trans-
forms may be applied on the same block of
data.
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 call-
back 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 crytokop structure are:
krp_op Operation code, such as CRK_MOD_EXP.
krp_status Return code. This errno-style variable indicates whether
there were lower level reasons for operation failure.
krp_iparams Number of input parameters to the specified operation.
Note that each operation has a (typically hardwired) num-
ber of such parameters.
krp_oparams Number of output parameters from the specified operation.
Note that each operation has a (typically hardwired) num-
ber of such parameters.
krp_kvp An array of kernel memory blocks containing the parame-
ters.
krp_hid Identifier specifying which low-level driver is being
used.
krp_callback Callback called on completion of a keying operation.
The following sysctl entries exist to adjust the behaviour of the system
from userland:
kern.usercrypto Allow (1) or forbid (0) userland acces to
/dev/crypto.
kern.userasymcrypto Allow (1) or forbid (0) userland acces to do
asymmetric crypto requests.
kern.cryptodevallowsoft Enable/disable access to hardware versus soft-
ware operations:
< 0 Force userlevel requests to use software
operations, always.
= 0 Use hardware if present, grant userlevel
requests for non-accelerated operations
(handling the latter in software).
> 0 Allow user requests only for operations
which are hardware-accelerated.
DRIVER-SIDE API
The crypto_get_driverid(), crypto_register(), crypto_kregister(),
crypto_unregister(), 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 (normally 0, but software-only drivers should
specify CRYPTOCAP_F_SOFTWARE). For each algorithm the driver supports,
it must then call crypto_register(). The first argument is the driver
identifier. The second argument is an array of CRYPTO_ALGORITHM_MAX + 1
elements, indicating which algorithms are supported. The last three
arguments are pointers to three driver-provided functions that the frame-
work 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.) 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(4) crypto cards that are being ejected will invoke this routine
for all algorithms supported by the card. If called with
CRYPTO_ALGORITHM_ALL, all algorithms registered for a driver will be
unregistered in one go 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.
The calling convention for the three driver-supplied routines is:
int (*newsession) (void *, u_int32_t *, struct cryptoini *);
int (*freesession) (void *, u_int64_t);
int (*process) (void *, struct cryptop *, int);
On invocation, the first argument to newsession() contains the driver
identifier obtained via crypto_get_driverid(). On successfully return-
ing, it should contain a driver-specific session identifier. The second
argument is identical to that of crypto_newsession().
The freesession() routine takes as argument the SID (which is the con-
catenation of the driver identifier and the driver-specific session iden-
tifier). It should clear any context associated with the session (clear
hardware registers, memory, etc.).
The process() routine is invoked with a request to perform crypto pro-
cessing. 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 error, the error indication must be placed
in the crp_etype field of the cryptop structure. The hint argument can
be set to CRYPTO_HINT_MORE the there will be more request right after
this request. When the request is completed, or an error is detected,
the process() routine should invoke crypto_done(). Session migration may
be performed, as mentioned previously.
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 rou-
tine should be invoked. In case of 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 invoke crypto_kdone().
RETURN VALUES
crypto_register(), crypto_kregister(), crypto_unregister(),
crypto_newsession(), and crypto_freesession() 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 other-
wise. The callback is provided with an error code in case of failure, in
the crp_etype field.
FILES
sys/opencrypto/crypto.c most of the framework code
sys/crypto crypto algorithm implementations
SEE ALSO
condvar(9), ipsec(4), pcmcia(4), malloc(9)
Angelos D. Keromytis, Jason L. Wright, and Theo de Raadt, The Design of
the OpenBSD Cryptographic Framework, Usenix, 2003, June 2003.
HISTORY
The cryptographic framework first appeared in OpenBSD 2.7 and was written
by Angelos D. Keromytis <angelos at openbsd.org>.
Sam Leffler ported the crypto framework to FreeBSD and made performance
improvements.
Jonathan Stone <jonathan at NetBSD.org> ported the cryptoframe from FreeBSD
to NetBSD. opencrypto first appeared in NetBSD 2.0.
BUGS
The framework currently assumes that all the algorithms in a
crypto_newsession() operation must be available by the same driver. If
that's 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 sup-
ported. Note that 3DES is considered one algorithm (and not three
instances of DES). Thus, 3DES and DES could be mixed in the same
request.
A queue for completed operations should be implemented and processed at
some software spl(9) level, to avoid overall system latency issues, and
potential kernel stack exhaustion while processing a callback.
When SMP time comes, we will support use of a second processor (or more)
as a crypto device (this is actually AMP, but we need the same basic sup-
port).
NetBSD 5.1 January 1, 2010 NetBSD 5.1
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 194 bytes
Desc: not available
URL: <http://tahoe-lafs.org/pipermail/tahoe-dev/attachments/20110220/14864456/attachment-0001.pgp>
More information about the tahoe-dev
mailing list