OPENCRYPTO(9)
๐ OPENCRYPTO(9) ๐ โ aka the kernel crypto API, bestie
Kernel Developer's Manual ยท Kernel Developer's Manual โ yes it's a section 9 page, yes it's a whole vibe, no cap.
โจ NAME โจ
opencrypto, crypto_get_driverid, crypto_register, crypto_kregister, crypto_unregister, crypto_unregister_all, crypto_done, crypto_kdone, crypto_newsession, crypto_freesession, crypto_dispatch, crypto_kdispatch, crypto_getreq, crypto_freereq crypto_kgetreq, crypto_kfreereq โ API for cryptographic services in the kernel ๐
๐ฆ SYNOPSIS ๐ฆ
Okay so first you gotta bring in the header, obviously:
#include <opencrypto/cryptodev.h>
Then it's justโฆ a lot of function signatures. Here they all are, verbatim, because I would never do you like that and paraphrase a prototype:
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);
int
crypto_unregister_all(u_int32_t);
void
crypto_done(struct cryptop *);
void
crypto_kdone(struct cryptkop *);
int
crypto_newsession(u_int64_t *, struct cryptoini *, int);
void
crypto_freesession(u_int64_t);
void
crypto_dispatch(struct cryptop *);
void
crypto_kdispatch(struct cryptkop *);
struct cryptop *
crypto_getreq(int);
void
crypto_freereq(struct cryptop *);
struct cryptop *
crypto_kgetreq(int, int);
void
crypto_kfreereq(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; /* i.e. 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 ๐ธ
So opencrypto is this framework for drivers of cryptographic hardware to register themselves with the kernel, so that "consumers" (other kernel subsystems, and eventually users through an appropriate device) can actually make use of them. ๐
Drivers register with the framework the algorithms they support, and they hand over entry points (functions) that the framework may call to establish, use, and tear down sessions. And why sessions? Because sessions cache cryptographic information in a particular driver (or its associated hardware), so you're not re-initializing the whole thing with every single request. Main character energy: set it up once, slay forever. ๐
Consumers of cryptographic services pass a set of descriptors that tell the framework (and the drivers registered with it) which operations should be applied on the data โ and yes, more than one cryptographic operation can be requested at a time. Overachievers, we love to see it.
Keying operations are supported too, btw. Unlike the symmetric operators described above, these are sessionless commands that perform mathematical operations using input and output parameters. No session needed, they just show up and do math. ๐งฎ
โ ๏ธ Important vibe check: since the consumers may not be associated with a process, drivers may not use condition variables: condvar(9). The same holds for the framework. Not a suggestion, fr fr.
So instead, a callback mechanism is used to notify a consumer that a request has been completed โ and the callback is specified by the consumer on a per-request basis. The framework invokes that callback whether the request was successfully completed or not, and gives you an error indication in the not-successful case. One specific error code, EAGAIN, is used to say "hey, a session number has changed and you may re-submit the request immediately with the new session number." ๐ซถ
Errors are only returned to the invoking function if there isn't enough information to call the callback (meaning: there was a fatal error in verifying the arguments). And no callback mechanism is used for session initialization and teardown โ those two are on their own, no notes.
๐ Sessions: crypto_newsession() and friends
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.
- 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_CBCCRYPTO_3DES_CBCCRYPTO_BLF_CBCCRYPTO_CAST_CBCCRYPTO_CAMELLIA_CBCCRYPTO_SKIPJACK_CBCCRYPTO_ARC4CRYPTO_AES_CBCCRYPTO_AES_CTRCRYPTO_AES_GCM_16CRYPTO_AES_GMACCRYPTO_AES_128_GMACCRYPTO_AES_192_GMACCRYPTO_AES_256_GMACCRYPTO_AES_XCBC_MAC_96CRYPTO_MD5CRYPTO_MD5_HMACCRYPTO_MD5_HMAC_96CRYPTO_MD5_KPDKCRYPTO_NULL_CBCCRYPTO_NULL_HMACCRYPTO_SHA1CRYPTO_SHA1_HMACCRYPTO_SHA1_HMAC_96CRYPTO_SHA1_KPDKCRYPTO_SHA2_256_HMACCRYPTO_SHA2_384_HMACCRYPTO_SHA2_512_HMACCRYPTO_RIPEMD160_HMACCRYPTO_RIPEMD160_HMAC_96CRYPTO_DEFLATE_COMPCRYPTO_DEFLATE_COMP_NOGROWCRYPTO_GZIP_COMP
cri_klenโ Specifies the length of the key in bits, for variable-size key algorithms. (Bits, bestie. Bits. ๐ญ)cri_rndโ Specifies the number of rounds to be used with the algorithm, 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 initialization. If no IV is explicitly passed (see below on details), a random IV is used by the device driver processing the request. โจcri_nextโ Contains a pointer to anothercryptoinistructure. You can link multiple such structures to establish multi-algorithm sessions (ipsec(4)is an example consumer of such a feature). Chain them like bracelets. ๐ซถ
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, the SID acts as an index in the session cache of the driver. Iconic little index. ๐
crypto_freesession() is called with the SID returned by crypto_newsession() to disestablish the session. Breakup szn. ๐
๐ crypto_dispatch() and the cryptop struct
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 includingcrd_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 kernelmalloc(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 by the driver through thecrypto_done()routine. If the request was not successful, an error code is set in thecrp_etypefield.crp_etypeโ Contains the error type, if any errors were encountered, or zero if the request was successfully processed.
โ ๏ธ Note that this field only makes sense when examined by the callback routine specified incrp_callback. Errors are returned to the invoker ofcrypto_process()only when enough information is not present to call the callback routine (i.e., if the pointer passed isNULLor if no callback routine was specified). Don't come forcrp_etypein the wrong context, it'll ghost you. ๐
crp_flagsโ Is a bitmask of flags associated with this request. Currently defined flags are:CRYPTO_F_IMBUFโ The buffer pointed to bycrp_bufis 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 bycrp_alloctype), depending oncrp_flags.crp_opaqueโ This is passed through the crypto framework untouched and is intended for the invoking application's use. It's giving "not my business, that's for you." ๐crp_descโ This is a linked list of descriptors. Each descriptor provides information about what type of cryptographic operation should be done on the input buffer. The various fields are:crd_skipโ The offset in the input buffer where processing should start.crd_lenโ How many bytes, aftercrd_skip, should be processed.
crd_injectโ offset from the very start of the buffer where the results get dropped in ๐ง. For encryption algorithms this is literally the spot where the initialization vector (IV) goes when you're encrypting, or where you can find it when you're decrypting (all subject tocrd_flags, bestie โ read the fine print). For MAC algorithms, this is where the result of the keyed hash gets inserted.crd_flagsโ for adjusting general operation from userland, these flags are defined โจ:CRD_F_ENCRYPTโ for encryption algorithms, this bit is set when encryption is what you want (and when it's not set, decryption happens instead). ๐CRD_F_IV_PRESENTโ for encryption algorithms, set this bit when the IV already precedes the data, so thecrd_injectvalue gets ignored and no IV is written into the buffer. Otherwise, the IV used to encrypt the packet gets written at the location pointed to bycrd_inject. Some apps do special โIV cookingโ โ like the half-IV mode inipsec(4)โ and they can use this flag to say โhey, don't write the IV on the packet.โ This flag is typically used in conjunction with theCRD_F_IV_EXPLICITflag. ๐ณCRD_F_IV_EXPLICITโ for encryption algorithms, this bit is set when the IV is explicitly provided by the consumer in thecrd_ivfields. Otherwise, for encryption operations the IV is provided for by the driver used to perform the operation, whereas for decryption operations it's pointed to by thecrd_injectfield. This flag is typically used when the IV is calculated โon the flyโ by the consumer and does not precede the data โ someipsec(4)configurations and the encrypted swap are two such examples. ๐งCRD_F_COMPโ for compression algorithms, this bit is set when compression is required (and when it's not set, decompression is what you get). ๐๏ธ
CRD_INIโ thiscryptoinistructure will not be modified by the framework or the device drivers, no cap. Since this info accompanies every single cryptographic operation request, drivers may re-initialize state on-demand (which is typically an expensive operation, ngl ๐ญ). Furthermore, the cryptographic framework may re-route requests as a result of full queues or hardware failure, as described above.crd_nextโ points to the next descriptor. Linked operations are super useful in protocols such asipsec(4), where multiple cryptographic transforms 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, which must be at least 1. ๐
crypto_freereq() deallocates a structure cryptop and any cryptodesc structures linked to it. Heads up though: 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 crytokop structure are:
krp_opโ operation code, such asCRK_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) number of such parameters.krp_oparamsโ number of output parameters from the specified operation. Note that each operation has a (typically hardwired) number of such parameters.krp_kvpโ an array of kernel memory blocks containing the parameters.krp_hidโ identifier specifying which low-level driver is being used.krp_callbackโ callback called on completion of a keying operation. ๐
crypto_kgetreq() allocates a cryptkop structure. The first argument means the same as crypto_getreq(), except it is currently limited to be exactly 1. The second argument means flags passed to pool_get(). ๐ง
crypto_kfreereq() deallocates a structure cryptkop structure. ๐ซถ
The following sysctl entries exist to adjust the behaviour of the system from userland:
kern.usercryptoโ allow (1) or forbid (0) userland access to/dev/crypto. ๐ธkern.userasymcryptoโ allow (1) or forbid (0) userland access to do asymmetric crypto requests. ๐ฆkern.cryptodevallowsoftโ enable/disable access to hardware versus software 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. ๐ฅ
opencrypto.crypto_ret_q.maxlenโ limit the length ofqueue(crypto_ret_q)which mediates between crypto driver's completion and callingcryptopcallback. When the queue exceeds this limit,crypto_getreq()fails.<= 0means unlimited. โพ๏ธ
opencrypto.crypto_ret_kq.maxlenโ limit the length ofqueue(crypto_ret_kq)which mediates between crypto driver's completion and callingcryptkopcallback. When the queue exceeds this limit,crypto_kgetreq()fails.<= 0means unlimited. โพ๏ธ
The following sysctl entries exist to get statistics. ๐
opencrypto.crypto_ret_q.lenโ currentcrypto_ret_qlength.opencrypto.crypto_ret_q.dropsโ the count ofcrypto_getreq()failed as overflowopencrypto.crypto_ret_q.maxlen.opencrypto.crypto_ret_kq.lenโ currentcrypto_ret_kqlength.opencrypto.crypto_ret_kq.dropsโ the count ofcrypto_kgetreq()failed as overflowopencrypto.crypto_ret_kq.maxlen. ๐
๐โจ DRIVER-SIDE API โจ๐
okay so driver-side besties, this part is literally your whole group chat ๐ The crypto_get_driverid(), crypto_register(), crypto_kregister(), crypto_unregister(), crypto_unregister_all(), and crypto_done() routines are used by drivers that provide support for cryptographic primitives to register and unregister with the kernel crypto services framework. No cap.
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 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.). Obsessed with this API tbh ๐
crypto_unregister() is called by drivers that wish to withdraw support for an algorithm. The two arguments are the driver and algorithm identifiers, respectively. Then the page literally drops algorithms supported by the card. โ yes, that exact phrase, preserved, no cap. ๐ If all algorithms associated with a driver are unregistered, the driver will be disabled (no new sessions will be allocated on that driver, and any existing sessions will be migrated to other drivers). crypto_unregister_all() will unregister all registered algorithms, disable the driver, and migrate existing sessions to other drivers.
The calling convention for the three driver-supplied routines is:
int (*newsession) (void *, u_int32_t *, struct cryptoini *);
void (*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 returning, 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 concatenation of the driver identifier and the driver-specific session identifier returned by newsession().) 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 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 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 when 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 routine 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(), andcrypto_newsession()return0on success, or an error code on failure.crypto_get_driverid()returns a non-negative value on error, and-1on failure. bestie I am not fixing that sentence, I am just delivering it ๐crypto_getreq()returns a pointer to a cryptop structure andNULLon failure.crypto_kgetreq()returns a pointer to a cryptkop structure andNULLon failure.crypto_dispatch()arranges to invoke the callback with an error code in thecrp_etypefield, or zero on success.
๐ FILES ๐
sys/opencrypto/crypto.cโ most of the framework codesys/cryptoโ crypto algorithm implementations
๐ฆ SEE ALSO ๐ฆ
ipsec(4),pcmcia(4),condvar(9),malloc(9),pool(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@openbsd.org>.
Sam Leffler ported the crypto framework to FreeBSD and made performance improvements.
Jonathan Stone <jonathan@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 supported. Note that 3DES is considered one algorithm (and not three instances of DES). Thus, 3DES and DES could be mixed in the same request.
and that's the whole page, bestie ๐ซถ This edition is from Debian, dated May 26, 2017, and the manual page is OPENCRYPTO(9). periodt ๐โจ
๐ฆ where this page comes from
- nixpkgs unstable: netbsd-source 11.0
- Copyright (c) 2000, 2001 Angelos D. Keromytis
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms