13e1c6f35SVadim Fedorenko // SPDX-License-Identifier: GPL-2.0-only
23e1c6f35SVadim Fedorenko /* Copyright (c) 2024 Meta, Inc */
33e1c6f35SVadim Fedorenko #include <linux/bpf.h>
43e1c6f35SVadim Fedorenko #include <linux/bpf_crypto.h>
53e1c6f35SVadim Fedorenko #include <linux/bpf_mem_alloc.h>
63e1c6f35SVadim Fedorenko #include <linux/btf.h>
73e1c6f35SVadim Fedorenko #include <linux/btf_ids.h>
83e1c6f35SVadim Fedorenko #include <linux/filter.h>
93e1c6f35SVadim Fedorenko #include <linux/scatterlist.h>
103e1c6f35SVadim Fedorenko #include <linux/skbuff.h>
113e1c6f35SVadim Fedorenko #include <crypto/skcipher.h>
123e1c6f35SVadim Fedorenko
133e1c6f35SVadim Fedorenko struct bpf_crypto_type_list {
143e1c6f35SVadim Fedorenko const struct bpf_crypto_type *type;
153e1c6f35SVadim Fedorenko struct list_head list;
163e1c6f35SVadim Fedorenko };
173e1c6f35SVadim Fedorenko
183e1c6f35SVadim Fedorenko /* BPF crypto initialization parameters struct */
193e1c6f35SVadim Fedorenko /**
203e1c6f35SVadim Fedorenko * struct bpf_crypto_params - BPF crypto initialization parameters structure
213e1c6f35SVadim Fedorenko * @type: The string of crypto operation type.
223e1c6f35SVadim Fedorenko * @reserved: Reserved member, will be reused for more options in future
233e1c6f35SVadim Fedorenko * Values:
243e1c6f35SVadim Fedorenko * 0
253e1c6f35SVadim Fedorenko * @algo: The string of algorithm to initialize.
263e1c6f35SVadim Fedorenko * @key: The cipher key used to init crypto algorithm.
273e1c6f35SVadim Fedorenko * @key_len: The length of cipher key.
283e1c6f35SVadim Fedorenko * @authsize: The length of authentication tag used by algorithm.
293e1c6f35SVadim Fedorenko */
303e1c6f35SVadim Fedorenko struct bpf_crypto_params {
313e1c6f35SVadim Fedorenko char type[14];
323e1c6f35SVadim Fedorenko u8 reserved[2];
333e1c6f35SVadim Fedorenko char algo[128];
343e1c6f35SVadim Fedorenko u8 key[256];
353e1c6f35SVadim Fedorenko u32 key_len;
363e1c6f35SVadim Fedorenko u32 authsize;
373e1c6f35SVadim Fedorenko };
383e1c6f35SVadim Fedorenko
393e1c6f35SVadim Fedorenko static LIST_HEAD(bpf_crypto_types);
403e1c6f35SVadim Fedorenko static DECLARE_RWSEM(bpf_crypto_types_sem);
413e1c6f35SVadim Fedorenko
423e1c6f35SVadim Fedorenko /**
433e1c6f35SVadim Fedorenko * struct bpf_crypto_ctx - refcounted BPF crypto context structure
443e1c6f35SVadim Fedorenko * @type: The pointer to bpf crypto type
453e1c6f35SVadim Fedorenko * @tfm: The pointer to instance of crypto API struct.
463e1c6f35SVadim Fedorenko * @siv_len: Size of IV and state storage for cipher
473e1c6f35SVadim Fedorenko * @rcu: The RCU head used to free the crypto context with RCU safety.
483e1c6f35SVadim Fedorenko * @usage: Object reference counter. When the refcount goes to 0, the
493e1c6f35SVadim Fedorenko * memory is released back to the BPF allocator, which provides
503e1c6f35SVadim Fedorenko * RCU safety.
513e1c6f35SVadim Fedorenko */
523e1c6f35SVadim Fedorenko struct bpf_crypto_ctx {
533e1c6f35SVadim Fedorenko const struct bpf_crypto_type *type;
543e1c6f35SVadim Fedorenko void *tfm;
553e1c6f35SVadim Fedorenko u32 siv_len;
563e1c6f35SVadim Fedorenko struct rcu_head rcu;
573e1c6f35SVadim Fedorenko refcount_t usage;
583e1c6f35SVadim Fedorenko };
593e1c6f35SVadim Fedorenko
bpf_crypto_register_type(const struct bpf_crypto_type * type)603e1c6f35SVadim Fedorenko int bpf_crypto_register_type(const struct bpf_crypto_type *type)
613e1c6f35SVadim Fedorenko {
623e1c6f35SVadim Fedorenko struct bpf_crypto_type_list *node;
633e1c6f35SVadim Fedorenko int err = -EEXIST;
643e1c6f35SVadim Fedorenko
653e1c6f35SVadim Fedorenko down_write(&bpf_crypto_types_sem);
663e1c6f35SVadim Fedorenko list_for_each_entry(node, &bpf_crypto_types, list) {
673e1c6f35SVadim Fedorenko if (!strcmp(node->type->name, type->name))
683e1c6f35SVadim Fedorenko goto unlock;
693e1c6f35SVadim Fedorenko }
703e1c6f35SVadim Fedorenko
713e1c6f35SVadim Fedorenko node = kmalloc(sizeof(*node), GFP_KERNEL);
723e1c6f35SVadim Fedorenko err = -ENOMEM;
733e1c6f35SVadim Fedorenko if (!node)
743e1c6f35SVadim Fedorenko goto unlock;
753e1c6f35SVadim Fedorenko
763e1c6f35SVadim Fedorenko node->type = type;
773e1c6f35SVadim Fedorenko list_add(&node->list, &bpf_crypto_types);
783e1c6f35SVadim Fedorenko err = 0;
793e1c6f35SVadim Fedorenko
803e1c6f35SVadim Fedorenko unlock:
813e1c6f35SVadim Fedorenko up_write(&bpf_crypto_types_sem);
823e1c6f35SVadim Fedorenko
833e1c6f35SVadim Fedorenko return err;
843e1c6f35SVadim Fedorenko }
853e1c6f35SVadim Fedorenko EXPORT_SYMBOL_GPL(bpf_crypto_register_type);
863e1c6f35SVadim Fedorenko
bpf_crypto_unregister_type(const struct bpf_crypto_type * type)873e1c6f35SVadim Fedorenko int bpf_crypto_unregister_type(const struct bpf_crypto_type *type)
883e1c6f35SVadim Fedorenko {
893e1c6f35SVadim Fedorenko struct bpf_crypto_type_list *node;
903e1c6f35SVadim Fedorenko int err = -ENOENT;
913e1c6f35SVadim Fedorenko
923e1c6f35SVadim Fedorenko down_write(&bpf_crypto_types_sem);
933e1c6f35SVadim Fedorenko list_for_each_entry(node, &bpf_crypto_types, list) {
943e1c6f35SVadim Fedorenko if (strcmp(node->type->name, type->name))
953e1c6f35SVadim Fedorenko continue;
963e1c6f35SVadim Fedorenko
973e1c6f35SVadim Fedorenko list_del(&node->list);
983e1c6f35SVadim Fedorenko kfree(node);
993e1c6f35SVadim Fedorenko err = 0;
1003e1c6f35SVadim Fedorenko break;
1013e1c6f35SVadim Fedorenko }
1023e1c6f35SVadim Fedorenko up_write(&bpf_crypto_types_sem);
1033e1c6f35SVadim Fedorenko
1043e1c6f35SVadim Fedorenko return err;
1053e1c6f35SVadim Fedorenko }
1063e1c6f35SVadim Fedorenko EXPORT_SYMBOL_GPL(bpf_crypto_unregister_type);
1073e1c6f35SVadim Fedorenko
bpf_crypto_get_type(const char * name)1083e1c6f35SVadim Fedorenko static const struct bpf_crypto_type *bpf_crypto_get_type(const char *name)
1093e1c6f35SVadim Fedorenko {
1103e1c6f35SVadim Fedorenko const struct bpf_crypto_type *type = ERR_PTR(-ENOENT);
1113e1c6f35SVadim Fedorenko struct bpf_crypto_type_list *node;
1123e1c6f35SVadim Fedorenko
1133e1c6f35SVadim Fedorenko down_read(&bpf_crypto_types_sem);
1143e1c6f35SVadim Fedorenko list_for_each_entry(node, &bpf_crypto_types, list) {
1153e1c6f35SVadim Fedorenko if (strcmp(node->type->name, name))
1163e1c6f35SVadim Fedorenko continue;
1173e1c6f35SVadim Fedorenko
1183e1c6f35SVadim Fedorenko if (try_module_get(node->type->owner))
1193e1c6f35SVadim Fedorenko type = node->type;
1203e1c6f35SVadim Fedorenko break;
1213e1c6f35SVadim Fedorenko }
1223e1c6f35SVadim Fedorenko up_read(&bpf_crypto_types_sem);
1233e1c6f35SVadim Fedorenko
1243e1c6f35SVadim Fedorenko return type;
1253e1c6f35SVadim Fedorenko }
1263e1c6f35SVadim Fedorenko
1273e1c6f35SVadim Fedorenko __bpf_kfunc_start_defs();
1283e1c6f35SVadim Fedorenko
1293e1c6f35SVadim Fedorenko /**
1303e1c6f35SVadim Fedorenko * bpf_crypto_ctx_create() - Create a mutable BPF crypto context.
1313e1c6f35SVadim Fedorenko *
1323e1c6f35SVadim Fedorenko * Allocates a crypto context that can be used, acquired, and released by
1333e1c6f35SVadim Fedorenko * a BPF program. The crypto context returned by this function must either
1343e1c6f35SVadim Fedorenko * be embedded in a map as a kptr, or freed with bpf_crypto_ctx_release().
1353e1c6f35SVadim Fedorenko * As crypto API functions use GFP_KERNEL allocations, this function can
1363e1c6f35SVadim Fedorenko * only be used in sleepable BPF programs.
1373e1c6f35SVadim Fedorenko *
1383e1c6f35SVadim Fedorenko * bpf_crypto_ctx_create() allocates memory for crypto context.
1393e1c6f35SVadim Fedorenko * It may return NULL if no memory is available.
1403e1c6f35SVadim Fedorenko * @params: pointer to struct bpf_crypto_params which contains all the
1413e1c6f35SVadim Fedorenko * details needed to initialise crypto context.
1423e1c6f35SVadim Fedorenko * @params__sz: size of steuct bpf_crypto_params usef by bpf program
1433e1c6f35SVadim Fedorenko * @err: integer to store error code when NULL is returned.
1443e1c6f35SVadim Fedorenko */
1453e1c6f35SVadim Fedorenko __bpf_kfunc struct bpf_crypto_ctx *
bpf_crypto_ctx_create(const struct bpf_crypto_params * params,u32 params__sz,int * err)1463e1c6f35SVadim Fedorenko bpf_crypto_ctx_create(const struct bpf_crypto_params *params, u32 params__sz,
1473e1c6f35SVadim Fedorenko int *err)
1483e1c6f35SVadim Fedorenko {
1493e1c6f35SVadim Fedorenko const struct bpf_crypto_type *type;
1503e1c6f35SVadim Fedorenko struct bpf_crypto_ctx *ctx;
1513e1c6f35SVadim Fedorenko
1523e1c6f35SVadim Fedorenko if (!params || params->reserved[0] || params->reserved[1] ||
1533e1c6f35SVadim Fedorenko params__sz != sizeof(struct bpf_crypto_params)) {
1543e1c6f35SVadim Fedorenko *err = -EINVAL;
1553e1c6f35SVadim Fedorenko return NULL;
1563e1c6f35SVadim Fedorenko }
1573e1c6f35SVadim Fedorenko
1583e1c6f35SVadim Fedorenko type = bpf_crypto_get_type(params->type);
1593e1c6f35SVadim Fedorenko if (IS_ERR(type)) {
1603e1c6f35SVadim Fedorenko *err = PTR_ERR(type);
1613e1c6f35SVadim Fedorenko return NULL;
1623e1c6f35SVadim Fedorenko }
1633e1c6f35SVadim Fedorenko
1643e1c6f35SVadim Fedorenko if (!type->has_algo(params->algo)) {
1653e1c6f35SVadim Fedorenko *err = -EOPNOTSUPP;
1663e1c6f35SVadim Fedorenko goto err_module_put;
1673e1c6f35SVadim Fedorenko }
1683e1c6f35SVadim Fedorenko
1693e1c6f35SVadim Fedorenko if (!!params->authsize ^ !!type->setauthsize) {
1703e1c6f35SVadim Fedorenko *err = -EOPNOTSUPP;
1713e1c6f35SVadim Fedorenko goto err_module_put;
1723e1c6f35SVadim Fedorenko }
1733e1c6f35SVadim Fedorenko
1743e1c6f35SVadim Fedorenko if (!params->key_len || params->key_len > sizeof(params->key)) {
1753e1c6f35SVadim Fedorenko *err = -EINVAL;
1763e1c6f35SVadim Fedorenko goto err_module_put;
1773e1c6f35SVadim Fedorenko }
1783e1c6f35SVadim Fedorenko
1793e1c6f35SVadim Fedorenko ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
1803e1c6f35SVadim Fedorenko if (!ctx) {
1813e1c6f35SVadim Fedorenko *err = -ENOMEM;
1823e1c6f35SVadim Fedorenko goto err_module_put;
1833e1c6f35SVadim Fedorenko }
1843e1c6f35SVadim Fedorenko
1853e1c6f35SVadim Fedorenko ctx->type = type;
1863e1c6f35SVadim Fedorenko ctx->tfm = type->alloc_tfm(params->algo);
1873e1c6f35SVadim Fedorenko if (IS_ERR(ctx->tfm)) {
1883e1c6f35SVadim Fedorenko *err = PTR_ERR(ctx->tfm);
1893e1c6f35SVadim Fedorenko goto err_free_ctx;
1903e1c6f35SVadim Fedorenko }
1913e1c6f35SVadim Fedorenko
1923e1c6f35SVadim Fedorenko if (params->authsize) {
1933e1c6f35SVadim Fedorenko *err = type->setauthsize(ctx->tfm, params->authsize);
1943e1c6f35SVadim Fedorenko if (*err)
1953e1c6f35SVadim Fedorenko goto err_free_tfm;
1963e1c6f35SVadim Fedorenko }
1973e1c6f35SVadim Fedorenko
1983e1c6f35SVadim Fedorenko *err = type->setkey(ctx->tfm, params->key, params->key_len);
1993e1c6f35SVadim Fedorenko if (*err)
2003e1c6f35SVadim Fedorenko goto err_free_tfm;
2013e1c6f35SVadim Fedorenko
2023e1c6f35SVadim Fedorenko if (type->get_flags(ctx->tfm) & CRYPTO_TFM_NEED_KEY) {
2033e1c6f35SVadim Fedorenko *err = -EINVAL;
2043e1c6f35SVadim Fedorenko goto err_free_tfm;
2053e1c6f35SVadim Fedorenko }
2063e1c6f35SVadim Fedorenko
2073e1c6f35SVadim Fedorenko ctx->siv_len = type->ivsize(ctx->tfm) + type->statesize(ctx->tfm);
2083e1c6f35SVadim Fedorenko
2093e1c6f35SVadim Fedorenko refcount_set(&ctx->usage, 1);
2103e1c6f35SVadim Fedorenko
2113e1c6f35SVadim Fedorenko return ctx;
2123e1c6f35SVadim Fedorenko
2133e1c6f35SVadim Fedorenko err_free_tfm:
2143e1c6f35SVadim Fedorenko type->free_tfm(ctx->tfm);
2153e1c6f35SVadim Fedorenko err_free_ctx:
2163e1c6f35SVadim Fedorenko kfree(ctx);
2173e1c6f35SVadim Fedorenko err_module_put:
2183e1c6f35SVadim Fedorenko module_put(type->owner);
2193e1c6f35SVadim Fedorenko
2203e1c6f35SVadim Fedorenko return NULL;
2213e1c6f35SVadim Fedorenko }
2223e1c6f35SVadim Fedorenko
crypto_free_cb(struct rcu_head * head)2233e1c6f35SVadim Fedorenko static void crypto_free_cb(struct rcu_head *head)
2243e1c6f35SVadim Fedorenko {
2253e1c6f35SVadim Fedorenko struct bpf_crypto_ctx *ctx;
2263e1c6f35SVadim Fedorenko
2273e1c6f35SVadim Fedorenko ctx = container_of(head, struct bpf_crypto_ctx, rcu);
2283e1c6f35SVadim Fedorenko ctx->type->free_tfm(ctx->tfm);
2293e1c6f35SVadim Fedorenko module_put(ctx->type->owner);
2303e1c6f35SVadim Fedorenko kfree(ctx);
2313e1c6f35SVadim Fedorenko }
2323e1c6f35SVadim Fedorenko
2333e1c6f35SVadim Fedorenko /**
2343e1c6f35SVadim Fedorenko * bpf_crypto_ctx_acquire() - Acquire a reference to a BPF crypto context.
2353e1c6f35SVadim Fedorenko * @ctx: The BPF crypto context being acquired. The ctx must be a trusted
2363e1c6f35SVadim Fedorenko * pointer.
2373e1c6f35SVadim Fedorenko *
2383e1c6f35SVadim Fedorenko * Acquires a reference to a BPF crypto context. The context returned by this function
2393e1c6f35SVadim Fedorenko * must either be embedded in a map as a kptr, or freed with
2403e1c6f35SVadim Fedorenko * bpf_crypto_ctx_release().
2413e1c6f35SVadim Fedorenko */
2423e1c6f35SVadim Fedorenko __bpf_kfunc struct bpf_crypto_ctx *
bpf_crypto_ctx_acquire(struct bpf_crypto_ctx * ctx)2433e1c6f35SVadim Fedorenko bpf_crypto_ctx_acquire(struct bpf_crypto_ctx *ctx)
2443e1c6f35SVadim Fedorenko {
2453e1c6f35SVadim Fedorenko if (!refcount_inc_not_zero(&ctx->usage))
2463e1c6f35SVadim Fedorenko return NULL;
2473e1c6f35SVadim Fedorenko return ctx;
2483e1c6f35SVadim Fedorenko }
2493e1c6f35SVadim Fedorenko
2503e1c6f35SVadim Fedorenko /**
2513e1c6f35SVadim Fedorenko * bpf_crypto_ctx_release() - Release a previously acquired BPF crypto context.
2523e1c6f35SVadim Fedorenko * @ctx: The crypto context being released.
2533e1c6f35SVadim Fedorenko *
2543e1c6f35SVadim Fedorenko * Releases a previously acquired reference to a BPF crypto context. When the final
2553e1c6f35SVadim Fedorenko * reference of the BPF crypto context has been released, its memory
2563e1c6f35SVadim Fedorenko * will be released.
2573e1c6f35SVadim Fedorenko */
bpf_crypto_ctx_release(struct bpf_crypto_ctx * ctx)2583e1c6f35SVadim Fedorenko __bpf_kfunc void bpf_crypto_ctx_release(struct bpf_crypto_ctx *ctx)
2593e1c6f35SVadim Fedorenko {
2603e1c6f35SVadim Fedorenko if (refcount_dec_and_test(&ctx->usage))
2613e1c6f35SVadim Fedorenko call_rcu(&ctx->rcu, crypto_free_cb);
2623e1c6f35SVadim Fedorenko }
2633e1c6f35SVadim Fedorenko
bpf_crypto_crypt(const struct bpf_crypto_ctx * ctx,const struct bpf_dynptr_kern * src,const struct bpf_dynptr_kern * dst,const struct bpf_dynptr_kern * siv,bool decrypt)2643e1c6f35SVadim Fedorenko static int bpf_crypto_crypt(const struct bpf_crypto_ctx *ctx,
2653e1c6f35SVadim Fedorenko const struct bpf_dynptr_kern *src,
2663e1c6f35SVadim Fedorenko const struct bpf_dynptr_kern *dst,
2673e1c6f35SVadim Fedorenko const struct bpf_dynptr_kern *siv,
2683e1c6f35SVadim Fedorenko bool decrypt)
2693e1c6f35SVadim Fedorenko {
2703e1c6f35SVadim Fedorenko u32 src_len, dst_len, siv_len;
2713e1c6f35SVadim Fedorenko const u8 *psrc;
2723e1c6f35SVadim Fedorenko u8 *pdst, *piv;
2733e1c6f35SVadim Fedorenko int err;
2743e1c6f35SVadim Fedorenko
2753e1c6f35SVadim Fedorenko if (__bpf_dynptr_is_rdonly(dst))
2763e1c6f35SVadim Fedorenko return -EINVAL;
2773e1c6f35SVadim Fedorenko
278*65d6d61dSVadim Fedorenko siv_len = siv ? __bpf_dynptr_size(siv) : 0;
2793e1c6f35SVadim Fedorenko src_len = __bpf_dynptr_size(src);
2803e1c6f35SVadim Fedorenko dst_len = __bpf_dynptr_size(dst);
2813e1c6f35SVadim Fedorenko if (!src_len || !dst_len)
2823e1c6f35SVadim Fedorenko return -EINVAL;
2833e1c6f35SVadim Fedorenko
2843e1c6f35SVadim Fedorenko if (siv_len != ctx->siv_len)
2853e1c6f35SVadim Fedorenko return -EINVAL;
2863e1c6f35SVadim Fedorenko
2873e1c6f35SVadim Fedorenko psrc = __bpf_dynptr_data(src, src_len);
2883e1c6f35SVadim Fedorenko if (!psrc)
2893e1c6f35SVadim Fedorenko return -EINVAL;
2903e1c6f35SVadim Fedorenko pdst = __bpf_dynptr_data_rw(dst, dst_len);
2913e1c6f35SVadim Fedorenko if (!pdst)
2923e1c6f35SVadim Fedorenko return -EINVAL;
2933e1c6f35SVadim Fedorenko
2943e1c6f35SVadim Fedorenko piv = siv_len ? __bpf_dynptr_data_rw(siv, siv_len) : NULL;
2953e1c6f35SVadim Fedorenko if (siv_len && !piv)
2963e1c6f35SVadim Fedorenko return -EINVAL;
2973e1c6f35SVadim Fedorenko
2983e1c6f35SVadim Fedorenko err = decrypt ? ctx->type->decrypt(ctx->tfm, psrc, pdst, src_len, piv)
2993e1c6f35SVadim Fedorenko : ctx->type->encrypt(ctx->tfm, psrc, pdst, src_len, piv);
3003e1c6f35SVadim Fedorenko
3013e1c6f35SVadim Fedorenko return err;
3023e1c6f35SVadim Fedorenko }
3033e1c6f35SVadim Fedorenko
3043e1c6f35SVadim Fedorenko /**
3053e1c6f35SVadim Fedorenko * bpf_crypto_decrypt() - Decrypt buffer using configured context and IV provided.
3063e1c6f35SVadim Fedorenko * @ctx: The crypto context being used. The ctx must be a trusted pointer.
3073e1c6f35SVadim Fedorenko * @src: bpf_dynptr to the encrypted data. Must be a trusted pointer.
3083e1c6f35SVadim Fedorenko * @dst: bpf_dynptr to the buffer where to store the result. Must be a trusted pointer.
309*65d6d61dSVadim Fedorenko * @siv__nullable: bpf_dynptr to IV data and state data to be used by decryptor. May be NULL.
3103e1c6f35SVadim Fedorenko *
3113e1c6f35SVadim Fedorenko * Decrypts provided buffer using IV data and the crypto context. Crypto context must be configured.
3123e1c6f35SVadim Fedorenko */
bpf_crypto_decrypt(struct bpf_crypto_ctx * ctx,const struct bpf_dynptr * src,const struct bpf_dynptr * dst,const struct bpf_dynptr * siv__nullable)3133e1c6f35SVadim Fedorenko __bpf_kfunc int bpf_crypto_decrypt(struct bpf_crypto_ctx *ctx,
314cce4c40bSDaniel Xu const struct bpf_dynptr *src,
315cce4c40bSDaniel Xu const struct bpf_dynptr *dst,
316*65d6d61dSVadim Fedorenko const struct bpf_dynptr *siv__nullable)
3173e1c6f35SVadim Fedorenko {
318cce4c40bSDaniel Xu const struct bpf_dynptr_kern *src_kern = (struct bpf_dynptr_kern *)src;
319cce4c40bSDaniel Xu const struct bpf_dynptr_kern *dst_kern = (struct bpf_dynptr_kern *)dst;
320*65d6d61dSVadim Fedorenko const struct bpf_dynptr_kern *siv_kern = (struct bpf_dynptr_kern *)siv__nullable;
321cce4c40bSDaniel Xu
322cce4c40bSDaniel Xu return bpf_crypto_crypt(ctx, src_kern, dst_kern, siv_kern, true);
3233e1c6f35SVadim Fedorenko }
3243e1c6f35SVadim Fedorenko
3253e1c6f35SVadim Fedorenko /**
3263e1c6f35SVadim Fedorenko * bpf_crypto_encrypt() - Encrypt buffer using configured context and IV provided.
3273e1c6f35SVadim Fedorenko * @ctx: The crypto context being used. The ctx must be a trusted pointer.
3283e1c6f35SVadim Fedorenko * @src: bpf_dynptr to the plain data. Must be a trusted pointer.
329*65d6d61dSVadim Fedorenko * @dst: bpf_dynptr to the buffer where to store the result. Must be a trusted pointer.
330*65d6d61dSVadim Fedorenko * @siv__nullable: bpf_dynptr to IV data and state data to be used by decryptor. May be NULL.
3313e1c6f35SVadim Fedorenko *
3323e1c6f35SVadim Fedorenko * Encrypts provided buffer using IV data and the crypto context. Crypto context must be configured.
3333e1c6f35SVadim Fedorenko */
bpf_crypto_encrypt(struct bpf_crypto_ctx * ctx,const struct bpf_dynptr * src,const struct bpf_dynptr * dst,const struct bpf_dynptr * siv__nullable)3343e1c6f35SVadim Fedorenko __bpf_kfunc int bpf_crypto_encrypt(struct bpf_crypto_ctx *ctx,
335cce4c40bSDaniel Xu const struct bpf_dynptr *src,
336cce4c40bSDaniel Xu const struct bpf_dynptr *dst,
337*65d6d61dSVadim Fedorenko const struct bpf_dynptr *siv__nullable)
3383e1c6f35SVadim Fedorenko {
339cce4c40bSDaniel Xu const struct bpf_dynptr_kern *src_kern = (struct bpf_dynptr_kern *)src;
340cce4c40bSDaniel Xu const struct bpf_dynptr_kern *dst_kern = (struct bpf_dynptr_kern *)dst;
341*65d6d61dSVadim Fedorenko const struct bpf_dynptr_kern *siv_kern = (struct bpf_dynptr_kern *)siv__nullable;
342cce4c40bSDaniel Xu
343cce4c40bSDaniel Xu return bpf_crypto_crypt(ctx, src_kern, dst_kern, siv_kern, false);
3443e1c6f35SVadim Fedorenko }
3453e1c6f35SVadim Fedorenko
3463e1c6f35SVadim Fedorenko __bpf_kfunc_end_defs();
3473e1c6f35SVadim Fedorenko
3483e1c6f35SVadim Fedorenko BTF_KFUNCS_START(crypt_init_kfunc_btf_ids)
3493e1c6f35SVadim Fedorenko BTF_ID_FLAGS(func, bpf_crypto_ctx_create, KF_ACQUIRE | KF_RET_NULL | KF_SLEEPABLE)
3503e1c6f35SVadim Fedorenko BTF_ID_FLAGS(func, bpf_crypto_ctx_release, KF_RELEASE)
3513e1c6f35SVadim Fedorenko BTF_ID_FLAGS(func, bpf_crypto_ctx_acquire, KF_ACQUIRE | KF_RCU | KF_RET_NULL)
3523e1c6f35SVadim Fedorenko BTF_KFUNCS_END(crypt_init_kfunc_btf_ids)
3533e1c6f35SVadim Fedorenko
3543e1c6f35SVadim Fedorenko static const struct btf_kfunc_id_set crypt_init_kfunc_set = {
3553e1c6f35SVadim Fedorenko .owner = THIS_MODULE,
3563e1c6f35SVadim Fedorenko .set = &crypt_init_kfunc_btf_ids,
3573e1c6f35SVadim Fedorenko };
3583e1c6f35SVadim Fedorenko
3593e1c6f35SVadim Fedorenko BTF_KFUNCS_START(crypt_kfunc_btf_ids)
3603e1c6f35SVadim Fedorenko BTF_ID_FLAGS(func, bpf_crypto_decrypt, KF_RCU)
3613e1c6f35SVadim Fedorenko BTF_ID_FLAGS(func, bpf_crypto_encrypt, KF_RCU)
3623e1c6f35SVadim Fedorenko BTF_KFUNCS_END(crypt_kfunc_btf_ids)
3633e1c6f35SVadim Fedorenko
3643e1c6f35SVadim Fedorenko static const struct btf_kfunc_id_set crypt_kfunc_set = {
3653e1c6f35SVadim Fedorenko .owner = THIS_MODULE,
3663e1c6f35SVadim Fedorenko .set = &crypt_kfunc_btf_ids,
3673e1c6f35SVadim Fedorenko };
3683e1c6f35SVadim Fedorenko
3693e1c6f35SVadim Fedorenko BTF_ID_LIST(bpf_crypto_dtor_ids)
BTF_ID(struct,bpf_crypto_ctx)3703e1c6f35SVadim Fedorenko BTF_ID(struct, bpf_crypto_ctx)
3713e1c6f35SVadim Fedorenko BTF_ID(func, bpf_crypto_ctx_release)
3723e1c6f35SVadim Fedorenko
3733e1c6f35SVadim Fedorenko static int __init crypto_kfunc_init(void)
3743e1c6f35SVadim Fedorenko {
3753e1c6f35SVadim Fedorenko int ret;
3763e1c6f35SVadim Fedorenko const struct btf_id_dtor_kfunc bpf_crypto_dtors[] = {
3773e1c6f35SVadim Fedorenko {
3783e1c6f35SVadim Fedorenko .btf_id = bpf_crypto_dtor_ids[0],
3793e1c6f35SVadim Fedorenko .kfunc_btf_id = bpf_crypto_dtor_ids[1]
3803e1c6f35SVadim Fedorenko },
3813e1c6f35SVadim Fedorenko };
3823e1c6f35SVadim Fedorenko
3833e1c6f35SVadim Fedorenko ret = register_btf_kfunc_id_set(BPF_PROG_TYPE_SCHED_CLS, &crypt_kfunc_set);
3843e1c6f35SVadim Fedorenko ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_SCHED_ACT, &crypt_kfunc_set);
3853e1c6f35SVadim Fedorenko ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_XDP, &crypt_kfunc_set);
3863e1c6f35SVadim Fedorenko ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_SYSCALL,
3873e1c6f35SVadim Fedorenko &crypt_init_kfunc_set);
3883e1c6f35SVadim Fedorenko return ret ?: register_btf_id_dtor_kfuncs(bpf_crypto_dtors,
3893e1c6f35SVadim Fedorenko ARRAY_SIZE(bpf_crypto_dtors),
3903e1c6f35SVadim Fedorenko THIS_MODULE);
3913e1c6f35SVadim Fedorenko }
3923e1c6f35SVadim Fedorenko
3933e1c6f35SVadim Fedorenko late_initcall(crypto_kfunc_init);
394