#ifndef _CRYPTO_CRYPTO_H_
#define _CRYPTO_CRYPTO_H_
#include <sys/ioccom.h>
#ifdef _KERNEL
#include <opencrypto/_cryptodev.h>
#include <sys/_task.h>
#include <sys/libkern.h>
#include <sys/time.h>
#endif
#define CRYPTO_DRIVERS_INITIAL 4
#define NULL_HASH_LEN 16
#define SHA1_HASH_LEN 20
#define RIPEMD160_HASH_LEN 20
#define SHA2_224_HASH_LEN 28
#define SHA2_256_HASH_LEN 32
#define SHA2_384_HASH_LEN 48
#define SHA2_512_HASH_LEN 64
#define AES_GMAC_HASH_LEN 16
#define POLY1305_HASH_LEN 16
#define AES_CBC_MAC_HASH_LEN 16
#define HASH_MAX_LEN SHA2_512_HASH_LEN
#define SHA1_BLOCK_LEN 64
#define RIPEMD160_BLOCK_LEN 64
#define SHA2_224_BLOCK_LEN 64
#define SHA2_256_BLOCK_LEN 64
#define SHA2_384_BLOCK_LEN 128
#define SHA2_512_BLOCK_LEN 128
#define POLY1305_BLOCK_LEN 16
#define NULL_HMAC_BLOCK_LEN 64
#define HMAC_MAX_BLOCK_LEN SHA2_512_BLOCK_LEN
#define HMAC_IPAD_VAL 0x36
#define HMAC_OPAD_VAL 0x5C
#define AES_128_GMAC_KEY_LEN 16
#define AES_192_GMAC_KEY_LEN 24
#define AES_256_GMAC_KEY_LEN 32
#define AES_128_CBC_MAC_KEY_LEN 16
#define AES_192_CBC_MAC_KEY_LEN 24
#define AES_256_CBC_MAC_KEY_LEN 32
#define POLY1305_KEY_LEN 32
#define NULL_BLOCK_LEN 4
#define RIJNDAEL128_BLOCK_LEN 16
#define AES_BLOCK_LEN 16
#define AES_ICM_BLOCK_LEN 1
#define CAMELLIA_BLOCK_LEN 16
#define CHACHA20_NATIVE_BLOCK_LEN 64
#define EALG_MAX_BLOCK_LEN CHACHA20_NATIVE_BLOCK_LEN
#define AES_GCM_IV_LEN 12
#define AES_CCM_IV_LEN 12
#define AES_XTS_IV_LEN 8
#define AES_XTS_ALPHA 0x87
#define CHACHA20_POLY1305_IV_LEN 12
#define XCHACHA20_POLY1305_IV_LEN 24
#define NULL_MIN_KEY 0
#define NULL_MAX_KEY 256
#define RIJNDAEL_MIN_KEY 16
#define RIJNDAEL_MAX_KEY 32
#define AES_MIN_KEY RIJNDAEL_MIN_KEY
#define AES_MAX_KEY RIJNDAEL_MAX_KEY
#define AES_XTS_MIN_KEY (2 * AES_MIN_KEY)
#define AES_XTS_MAX_KEY (2 * AES_MAX_KEY)
#define CAMELLIA_MIN_KEY 16
#define CAMELLIA_MAX_KEY 32
#define CHACHA20_POLY1305_KEY 32
#define XCHACHA20_POLY1305_KEY 32
#define AALG_MAX_RESULT_LEN 64
#define CRYPTO_ALGORITHM_MIN 1
#define CRYPTO_DES_CBC 1
#define CRYPTO_3DES_CBC 2
#define CRYPTO_BLF_CBC 3
#define CRYPTO_CAST_CBC 4
#define CRYPTO_SKIPJACK_CBC 5
#define CRYPTO_MD5_HMAC 6
#define CRYPTO_SHA1_HMAC 7
#define CRYPTO_RIPEMD160_HMAC 8
#define CRYPTO_MD5_KPDK 9
#define CRYPTO_SHA1_KPDK 10
#define CRYPTO_RIJNDAEL128_CBC 11
#define CRYPTO_AES_CBC 11
#define CRYPTO_ARC4 12
#define CRYPTO_MD5 13
#define CRYPTO_SHA1 14
#define CRYPTO_NULL_HMAC 15
#define CRYPTO_NULL_CBC 16
#define CRYPTO_DEFLATE_COMP 17
#define CRYPTO_SHA2_256_HMAC 18
#define CRYPTO_SHA2_384_HMAC 19
#define CRYPTO_SHA2_512_HMAC 20
#define CRYPTO_CAMELLIA_CBC 21
#define CRYPTO_AES_XTS 22
#define CRYPTO_AES_ICM 23
#define CRYPTO_AES_NIST_GMAC 24
#define CRYPTO_AES_NIST_GCM_16 25
#ifdef _KERNEL
#define CRYPTO_AES_128_NIST_GMAC 26
#define CRYPTO_AES_192_NIST_GMAC 27
#define CRYPTO_AES_256_NIST_GMAC 28
#endif
#define CRYPTO_BLAKE2B 29
#define CRYPTO_BLAKE2S 30
#define CRYPTO_CHACHA20 31
#define CRYPTO_SHA2_224_HMAC 32
#define CRYPTO_RIPEMD160 33
#define CRYPTO_SHA2_224 34
#define CRYPTO_SHA2_256 35
#define CRYPTO_SHA2_384 36
#define CRYPTO_SHA2_512 37
#define CRYPTO_POLY1305 38
#define CRYPTO_AES_CCM_CBC_MAC 39
#define CRYPTO_AES_CCM_16 40
#define CRYPTO_CHACHA20_POLY1305 41
#define CRYPTO_XCHACHA20_POLY1305 42
#define CRYPTO_ALGORITHM_MAX 42
#define CRYPTO_ALGO_VALID(x) ((x) >= CRYPTO_ALGORITHM_MIN && \
(x) <= CRYPTO_ALGORITHM_MAX)
#define CRYPTO_FLAG_HARDWARE 0x01000000
#define CRYPTO_FLAG_SOFTWARE 0x02000000
#ifdef __powerpc__
#define CRYPTO_MAY_HAVE_VMPAGE 1
#else
#define CRYPTO_MAY_HAVE_VMPAGE ( PMAP_HAS_DMAP )
#endif
#define CRYPTO_HAS_VMPAGE ( PMAP_HAS_DMAP )
struct session_op {
uint32_t cipher;
uint32_t mac;
uint32_t keylen;
const void *key;
int mackeylen;
const void *mackey;
uint32_t ses;
};
struct session2_op {
uint32_t cipher;
uint32_t mac;
uint32_t keylen;
const void *key;
int mackeylen;
const void *mackey;
uint32_t ses;
int crid;
int ivlen;
int maclen;
int pad[2];
};
struct crypt_op {
uint32_t ses;
uint16_t op;
#define COP_ENCRYPT 1
#define COP_DECRYPT 2
uint16_t flags;
#define COP_F_CIPHER_FIRST 0x0001
#define COP_F_BATCH 0x0008
u_int len;
const void *src;
void *dst;
void *mac;
const void *iv;
};
struct crypt_aead {
uint32_t ses;
uint16_t op;
uint16_t flags;
u_int len;
u_int aadlen;
u_int ivlen;
const void *src;
void *dst;
const void *aad;
void *tag;
const void *iv;
};
struct crypt_find_op {
int crid;
char name[32];
};
#define CIOCGSESSION _IOWR('c', 101, struct session_op)
#define CIOCFSESSION _IOW('c', 102, uint32_t)
#define CIOCCRYPT _IOWR('c', 103, struct crypt_op)
#define CIOCGSESSION2 _IOWR('c', 106, struct session2_op)
#define CIOCFINDDEV _IOWR('c', 108, struct crypt_find_op)
#define CIOCCRYPTAEAD _IOWR('c', 109, struct crypt_aead)
struct cryptostats {
uint64_t cs_ops;
uint64_t cs_errs;
uint64_t cs_kops;
uint64_t cs_kerrs;
uint64_t cs_intrs;
uint64_t cs_rets;
uint64_t cs_blocks;
uint64_t cs_kblocks;
};
#ifdef _KERNEL
#define CRYPTODEV_PROBE_HARDWARE (-100)
#define CRYPTODEV_PROBE_ACCEL_SOFTWARE (-200)
#define CRYPTODEV_PROBE_SOFTWARE (-500)
#if 0
#define CRYPTDEB(s, ...) do { \
printf("%s:%d: " s "\n", __FILE__, __LINE__, ## __VA_ARGS__); \
} while (0)
#else
#define CRYPTDEB(...) do { } while (0)
#endif
struct crypto_session_params {
int csp_mode;
#define CSP_MODE_NONE 0
#define CSP_MODE_COMPRESS 1
#define CSP_MODE_CIPHER 2
#define CSP_MODE_DIGEST 3
#define CSP_MODE_AEAD 4
#define CSP_MODE_ETA 5
int csp_flags;
#define CSP_F_SEPARATE_OUTPUT 0x0001
#define CSP_F_SEPARATE_AAD 0x0002
#define CSP_F_ESN 0x0004
int csp_ivlen;
int csp_cipher_alg;
int csp_cipher_klen;
const void *csp_cipher_key;
int csp_auth_alg;
int csp_auth_klen;
const void *csp_auth_key;
int csp_auth_mlen;
};
enum crypto_buffer_type {
CRYPTO_BUF_NONE = 0,
CRYPTO_BUF_CONTIG,
CRYPTO_BUF_UIO,
CRYPTO_BUF_MBUF,
CRYPTO_BUF_VMPAGE,
CRYPTO_BUF_SINGLE_MBUF,
CRYPTO_BUF_LAST = CRYPTO_BUF_SINGLE_MBUF
};
struct crypto_buffer {
union {
struct {
char *cb_buf;
int cb_buf_len;
};
struct mbuf *cb_mbuf;
struct {
vm_page_t *cb_vm_page;
int cb_vm_page_len;
int cb_vm_page_offset;
};
struct uio *cb_uio;
};
enum crypto_buffer_type cb_type;
};
struct crypto_buffer_cursor {
union {
char *cc_buf;
struct mbuf *cc_mbuf;
struct iovec *cc_iov;
vm_page_t *cc_vmpage;
};
int cc_buf_len;
size_t cc_offset;
enum crypto_buffer_type cc_type;
};
struct cryptop {
TAILQ_ENTRY(cryptop) crp_next;
struct task crp_task;
crypto_session_t crp_session;
int crp_olen;
int crp_etype;
#define crp_startcopy crp_flags
int crp_flags;
#define CRYPTO_F_CBIMM 0x0010
#define CRYPTO_F_DONE 0x0020
#define CRYPTO_F_CBIFSYNC 0x0040
#define CRYPTO_F_ASYNC_ORDERED 0x0100
#define CRYPTO_F_IV_SEPARATE 0x0200
int crp_op;
struct crypto_buffer crp_buf;
struct crypto_buffer crp_obuf;
void *crp_aad;
int crp_aad_start;
int crp_aad_length;
uint8_t crp_esn[4];
int crp_iv_start;
int crp_payload_start;
int crp_payload_output_start;
int crp_payload_length;
int crp_digest_start;
uint8_t crp_iv[EALG_MAX_BLOCK_LEN];
const void *crp_cipher_key;
const void *crp_auth_key;
#define crp_endcopy crp_opaque
void *crp_opaque;
int (*crp_callback)(struct cryptop *);
struct bintime crp_tstamp;
uint32_t crp_seq;
uint32_t crp_retw_id;
};
TAILQ_HEAD(cryptopq, cryptop);
static __inline void
_crypto_use_buf(struct crypto_buffer *cb, void *buf, int len)
{
cb->cb_buf = buf;
cb->cb_buf_len = len;
cb->cb_type = CRYPTO_BUF_CONTIG;
}
static __inline void
_crypto_use_mbuf(struct crypto_buffer *cb, struct mbuf *m)
{
cb->cb_mbuf = m;
cb->cb_type = CRYPTO_BUF_MBUF;
}
static __inline void
_crypto_use_single_mbuf(struct crypto_buffer *cb, struct mbuf *m)
{
cb->cb_mbuf = m;
cb->cb_type = CRYPTO_BUF_SINGLE_MBUF;
}
static __inline void
_crypto_use_vmpage(struct crypto_buffer *cb, vm_page_t *pages, int len,
int offset)
{
cb->cb_vm_page = pages;
cb->cb_vm_page_len = len;
cb->cb_vm_page_offset = offset;
cb->cb_type = CRYPTO_BUF_VMPAGE;
}
static __inline void
_crypto_use_uio(struct crypto_buffer *cb, struct uio *uio)
{
cb->cb_uio = uio;
cb->cb_type = CRYPTO_BUF_UIO;
}
static __inline void
crypto_use_buf(struct cryptop *crp, void *buf, int len)
{
_crypto_use_buf(&crp->crp_buf, buf, len);
}
static __inline void
crypto_use_mbuf(struct cryptop *crp, struct mbuf *m)
{
_crypto_use_mbuf(&crp->crp_buf, m);
}
static __inline void
crypto_use_single_mbuf(struct cryptop *crp, struct mbuf *m)
{
_crypto_use_single_mbuf(&crp->crp_buf, m);
}
static __inline void
crypto_use_vmpage(struct cryptop *crp, vm_page_t *pages, int len, int offset)
{
_crypto_use_vmpage(&crp->crp_buf, pages, len, offset);
}
static __inline void
crypto_use_uio(struct cryptop *crp, struct uio *uio)
{
_crypto_use_uio(&crp->crp_buf, uio);
}
static __inline void
crypto_use_output_buf(struct cryptop *crp, void *buf, int len)
{
_crypto_use_buf(&crp->crp_obuf, buf, len);
}
static __inline void
crypto_use_output_mbuf(struct cryptop *crp, struct mbuf *m)
{
_crypto_use_mbuf(&crp->crp_obuf, m);
}
static __inline void
crypto_use_output_single_mbuf(struct cryptop *crp, struct mbuf *m)
{
_crypto_use_single_mbuf(&crp->crp_obuf, m);
}
static __inline void
crypto_use_output_vmpage(struct cryptop *crp, vm_page_t *pages, int len,
int offset)
{
_crypto_use_vmpage(&crp->crp_obuf, pages, len, offset);
}
static __inline void
crypto_use_output_uio(struct cryptop *crp, struct uio *uio)
{
_crypto_use_uio(&crp->crp_obuf, uio);
}
#define CRYPTO_HAS_OUTPUT_BUFFER(crp) \
((crp)->crp_obuf.cb_type != CRYPTO_BUF_NONE)
#define CRYPTO_OP_DECRYPT 0x0
#define CRYPTO_OP_ENCRYPT 0x1
#define CRYPTO_OP_IS_ENCRYPT(op) ((op) & CRYPTO_OP_ENCRYPT)
#define CRYPTO_OP_COMPUTE_DIGEST 0x0
#define CRYPTO_OP_VERIFY_DIGEST 0x2
#define CRYPTO_OP_DECOMPRESS CRYPTO_OP_DECRYPT
#define CRYPTO_OP_COMPRESS CRYPTO_OP_ENCRYPT
#define CRYPTO_OP_IS_COMPRESS(op) ((op) & CRYPTO_OP_COMPRESS)
#define CRYPTO_HINT_MORE 0x1
uint32_t crypto_ses2hid(crypto_session_t crypto_session);
uint32_t crypto_ses2caps(crypto_session_t crypto_session);
void *crypto_get_driver_session(crypto_session_t crypto_session);
const struct crypto_session_params *crypto_get_params(
crypto_session_t crypto_session);
const struct auth_hash *crypto_auth_hash(const struct crypto_session_params *csp);
const struct enc_xform *crypto_cipher(const struct crypto_session_params *csp);
#ifdef MALLOC_DECLARE
MALLOC_DECLARE(M_CRYPTO_DATA);
#endif
int crypto_newsession(crypto_session_t *cses,
const struct crypto_session_params *params, int crid);
void crypto_freesession(crypto_session_t cses);
#define CRYPTOCAP_F_HARDWARE CRYPTO_FLAG_HARDWARE
#define CRYPTOCAP_F_SOFTWARE CRYPTO_FLAG_SOFTWARE
#define CRYPTOCAP_F_SYNC 0x04000000
#define CRYPTOCAP_F_ACCEL_SOFTWARE 0x08000000
#define CRYPTO_SESS_SYNC(sess) \
((crypto_ses2caps(sess) & CRYPTOCAP_F_SYNC) != 0)
int32_t crypto_get_driverid(device_t dev, size_t session_size, int flags);
int crypto_find_driver(const char *);
device_t crypto_find_device_byhid(int hid);
int crypto_getcaps(int hid);
int crypto_unregister_all(uint32_t driverid);
int crypto_dispatch(struct cryptop *crp);
#define CRYPTO_ASYNC_ORDERED 0x1
int crypto_dispatch_async(struct cryptop *crp, int flags);
void crypto_dispatch_batch(struct cryptopq *crpq, int flags);
#define CRYPTO_SYMQ 0x1
int crypto_unblock(uint32_t, int);
void crypto_done(struct cryptop *crp);
struct cryptop *crypto_clonereq(struct cryptop *crp, crypto_session_t cses,
int how);
void crypto_destroyreq(struct cryptop *crp);
void crypto_initreq(struct cryptop *crp, crypto_session_t cses);
void crypto_freereq(struct cryptop *crp);
struct cryptop *crypto_getreq(crypto_session_t cses, int how);
extern int crypto_usercrypto;
extern int crypto_devallowsoft;
#ifdef SYSCTL_DECL
SYSCTL_DECL(_kern_crypto);
#endif
struct auth_hash;
void hmac_init_ipad(const struct auth_hash *axf, const char *key, int klen,
void *auth_ctx);
void hmac_init_opad(const struct auth_hash *axf, const char *key, int klen,
void *auth_ctx);
void crypto_copyback(struct cryptop *crp, int off, int size,
const void *src);
void crypto_copydata(struct cryptop *crp, int off, int size, void *dst);
int crypto_apply(struct cryptop *crp, int off, int len,
int (*f)(void *, const void *, u_int), void *arg);
void *crypto_contiguous_subsegment(struct cryptop *crp, size_t skip,
size_t len);
int crypto_apply_buf(struct crypto_buffer *cb, int off, int len,
int (*f)(void *, const void *, u_int), void *arg);
void *crypto_buffer_contiguous_subsegment(struct crypto_buffer *cb,
size_t skip, size_t len);
size_t crypto_buffer_len(struct crypto_buffer *cb);
void crypto_cursor_init(struct crypto_buffer_cursor *cc,
const struct crypto_buffer *cb);
void crypto_cursor_advance(struct crypto_buffer_cursor *cc, size_t amount);
void *crypto_cursor_segment(struct crypto_buffer_cursor *cc, size_t *len);
void crypto_cursor_copyback(struct crypto_buffer_cursor *cc, int size,
const void *vsrc);
void crypto_cursor_copydata(struct crypto_buffer_cursor *cc, int size,
void *vdst);
void crypto_cursor_copydata_noadv(struct crypto_buffer_cursor *cc, int size,
void *vdst);
static __inline void
crypto_cursor_copy(const struct crypto_buffer_cursor *fromc,
struct crypto_buffer_cursor *toc)
{
memcpy(toc, fromc, sizeof(*toc));
}
static __inline void
crypto_read_iv(struct cryptop *crp, void *iv)
{
const struct crypto_session_params *csp;
csp = crypto_get_params(crp->crp_session);
if (crp->crp_flags & CRYPTO_F_IV_SEPARATE)
memcpy(iv, crp->crp_iv, csp->csp_ivlen);
else
crypto_copydata(crp, crp->crp_iv_start, csp->csp_ivlen, iv);
}
static __inline size_t
ccm_max_payload_length(const struct crypto_session_params *csp)
{
const u_int L = 15 - csp->csp_ivlen;
switch (L) {
case 2:
return (0xffff);
case 3:
return (0xffffff);
#ifdef __LP64__
case 4:
return (0xffffffff);
case 5:
return (0xffffffffff);
case 6:
return (0xffffffffffff);
case 7:
return (0xffffffffffffff);
default:
return (0xffffffffffffffff);
#else
default:
return (0xffffffff);
#endif
}
}
#endif
#endif