CRYPTO(4)
β¨ CRYPTO(4) β¨
okay bestie, this one's from the Kernel Interfaces Manual and it's lowkey iconic, so let's get into it π
π NAME π
crypto, cryptodev β user-mode access to hardware-accelerated cryptography π¦
πΈ SYNOPSIS πΈ
device crypto
device cryptodev
#include <sys/ioctl.h>
#include <sys/time.h>
#include <crypto/cryptodev.h>
π DESCRIPTION π
So the crypto driver is literally the plug that lets user-mode applications get at hardware-accelerated cryptographic transforms, exactly as they're implemented by the crypto(9) in-kernel interface. slay.
The /dev/crypto special device is the one serving up an ioctl(2) based interface. Your user-mode app opens the special device, and then just... issues ioctl(2) calls on the descriptor. that's the vibe. User-mode access to /dev/crypto is controlled by two sysctl(8) variables: kern.userasymcrypto and kern.cryptodevallowsoft. π
Not me having to tell you the crypto device has two distinct modes of operation:
- one mode for symmetric-keyed cryptographic requests and digests π§
- a second mode for both asymmetric-key (public-key/private-key) requests and modular arithmetic (that's for Diffie-Hellman key exchange and other cryptographic protocols) π
Both modes get described separately below, don't worry.
π THEORY OF OPERATION π
Regardless of whether you're doing symmetric-key or asymmetric-key operations, using the device requires a basic series of steps. periodt:
- Open the
/dev/cryptodevice. - Create a new cryptography file descriptor via
CRIOGETto use for all subsequentioctl(2)commands. - Close the
/dev/cryptodevice. - If any symmetric-keyed cryptographic or digest operations will be performed, create a session with
CIOCGSESSION. 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. - Submit requests, synchronously with
CIOCCRYPT(symmetric),CIOCCRYPTAEAD(symmetric), orCIOCKEY(asymmetric). - Optionally destroy a session with
CIOCFSESSION. - Close the cryptography file descriptor with
close(2). This will automatically close any remaining sessions associated with the file descriptor. π«Ά
π₯ SYMMETRIC-KEY OPERATION π₯
The symmetric-key operation mode gives you a context-based API to traditional 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. it's giving efficiency π
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.
π§ Algorithms π§
For a list of supported algorithms, see crypto(7) and crypto(9). β¨
π IOCTL Request Descriptions π
CRIOGETint *fdβ Clone the fd argument toioctl(2), yielding a new file descriptor for the creation of sessions.CIOCFINDDEVstruct crypt_find_op *fopβ here's the struct, bestie:
struct crypt_find_op {
int crid; /* driver id + flags */
char name[32]; /* device/driver name */
};
If crid is -1, then find the driver named name and return the id in crid. If crid is not -1, return the name of the driver with crid in name. In either case, if the driver is not found, ENOENT is returned.
CIOCGSESSIONstruct session_op *sesspβ the moment you've all been waiting for:
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 */
const void *key;
int mackeylen; /* mac key */
const 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.
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. π«
CIOCGSESSION2struct session2_op *sesspβ round two, babe:
struct session2_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 */
const void *key;
int mackeylen; /* mac key */
const void *mackey;
u_int32_t ses; /* returns: ses # */
int crid; /* driver id + flags (rw) */
int pad[4]; /* for future expansion */
};
This request is similar to CIOGSESSION except that sessp->crid requests either a specific crypto device or a class of devices (software vs hardware). The sessp->pad field must be initialized to zero. no cap.
CIOCCRYPTstruct crypt_op *cr_opβ the main character:
struct crypt_op {
u_int32_t ses;
u_int16_t op; /* e.g. COP_ENCRYPT */
u_int16_t flags;
u_int len;
caddr_t src, dst;
caddr_t mac; /* must be large enough for result */
caddr_t iv;
};
Request a symmetric-key (or hash) operation. 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. If a session is using both a privacy algorithm and a hash algorithm, the request will generate a hash of the input buffer before generating the output buffer by default. If the COP_F_CIPHER_FIRST flag is included in the cr_op->flags field, then the request will generate a hash of the output buffer after executing the privacy algorithm. π₯
CIOCCRYPTAEADstruct crypt_aead *cr_aeadβ the aead edition:
struct crypt_aead {
u_int32_t ses;
u_int16_t op; /* e.g. COP_ENCRYPT */
u_int16_t flags;
u_int len;
u_int aadlen;
u_int ivlen;
caddr_t src, dst;
caddr_t aad;
caddr_t tag; /* must be large enough for result */
caddr_t iv;
};
The CIOCCRYPTAEAD is similar to the CIOCCRYPT but provides additional data in cr_aead->aad to include in the authentication mode. πΈ
CIOCFSESSIONu_int32_t ses_idβ Destroys the session identified by ses_id. bye bestie π
π 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_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 π
CIOCASYMFEATint *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 operation is set. For example,CRK_MOD_EXPis available if and only if the bit(1 << CRK_MOD_EXP)is set. iykyk πCIOCKEYstruct crypt_kop *kopβ the big one:
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. π mystery behaviour fr fr, good luck bestie β¨
β¨ SEE ALSO β¨
ok so if you wanna go down the rabbit hole (and bestie, you know you do ππ), here's the whole squad:
aesni(4)β the AES-NI glow-uphifn(4)β Hifn crypto acceleratoripsec(4)β IPsec, the classicpadlock(4)β VIA PadLock, we love hersafe(4)β SafeNet crypto acceleratorubsec(4)β Broadcom uBsec crypto acceleratorcrypto(7)β the crypto subsystem overview, different vibe, same family π«Άgeli(8)β disk encryption, main character energy frcrypto(9)β the kernel-side API, this is where the real lore lives
π HISTORY π
so the crypto driver first appeared in OpenBSD 3.0. iconic. π
then it got imported to FreeBSD 5.0. it's giving cross-platform royalty, no cap π
π BUGS π
bestie. sit down. we need to talk. π
- Error checking and reporting is weak. that's it, that's the bug. it's giving "we'll handle it later" and later never came π
- The values you specify for symmetric-key key sizes to
CIOCGSESSIONmust exactly match the values expected byopencrypto(9). not "close enough", not "vibes-based", exactly. if it doesn't match, it's not gonna slay, it's gonna flop π - The output buffer and MAC buffers supplied to
CIOCCRYPTmust follow whether privacy or integrity algorithms were specified for the session: if you request a non-NULLalgorithm, you must supply a suitably-sized buffer. tl;dr β you asked for it, you gotta bring the space for it πβ¨ - The scheme for passing arguments for asymmetric requests is baroque. yes, baroque. like, ornate. like, unnecessarily decorated. like someone was feeling artsy that day and we all pay for it now π»π
CRIOGETshould not exist. it should be possible to use theCIOC*commands directly on a/dev/cryptofile descriptor. periodt. the extra step is giving "extra credit assignment nobody asked for" π
fr fr, if you're out here debugging crypto sessions and nothing makes sense, it's probably one of these. it's not you, bestie. it's the API π
and that's the whole page, babe π this one's from the Debian folks and it's dated December 17, 2019 β so yeah, it's been sitting in the group chat for a minute, but the tea is still hot ββ¨ now go forth and encrypt responsibly, you gorgeous nerd π¦π
π¦ where this page comes from
- debian bookworm: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- debian trixie: freebsd-manpages 12.2-2 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu jammy: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu noble: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu questing: freebsd-manpages 12.2-2
- ubuntu resolute: freebsd-manpages 12.2-2build1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu stonking: freebsd-manpages 12.2-2build1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- Copyright (c) 2008 The NetBSD Foundation, Inc
- Copyright (c) 2014 The FreeBSD Foundation
- Copyright (c) 2004 Jonathan Stone <jonathan@dsg.stanford.edu>. All rights reserved
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms