^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) /* SPDX-License-Identifier: GPL-2.0-or-later */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Key-agreement Protocol Primitives (KPP)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (c) 2016, Intel Corporation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Authors: Salvatore Benedetto <salvatore.benedetto@intel.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #ifndef _CRYPTO_KPP_
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #define _CRYPTO_KPP_
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/crypto.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) * struct kpp_request
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) * @base: Common attributes for async crypto requests
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) * @src: Source data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) * @dst: Destination data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) * @src_len: Size of the input buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) * @dst_len: Size of the output buffer. It needs to be at least
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) * as big as the expected result depending on the operation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) * After operation it will be updated with the actual size of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) * result. In case of error where the dst sgl size was insufficient,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) * it will be updated to the size required for the operation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) * @__ctx: Start of private context data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) struct kpp_request {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) struct crypto_async_request base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) struct scatterlist *src;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) struct scatterlist *dst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) unsigned int src_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) unsigned int dst_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) void *__ctx[] CRYPTO_MINALIGN_ATTR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) * struct crypto_kpp - user-instantiated object which encapsulate
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) * algorithms and core processing logic
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) * @base: Common crypto API algorithm data structure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) struct crypto_kpp {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) struct crypto_tfm base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) * struct kpp_alg - generic key-agreement protocol primitives
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) * @set_secret: Function invokes the protocol specific function to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) * store the secret private key along with parameters.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) * The implementation knows how to decode the buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) * @generate_public_key: Function generate the public key to be sent to the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) * counterpart. In case of error, where output is not big
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) * enough req->dst_len will be updated to the size
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) * required
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) * @compute_shared_secret: Function compute the shared secret as defined by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) * the algorithm. The result is given back to the user.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) * In case of error, where output is not big enough,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) * req->dst_len will be updated to the size required
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) * @max_size: Function returns the size of the output buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) * @init: Initialize the object. This is called only once at
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) * instantiation time. In case the cryptographic hardware
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) * needs to be initialized. Software fallback should be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) * put in place here.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) * @exit: Undo everything @init did.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) * @reqsize: Request context size required by algorithm
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) * implementation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) * @base: Common crypto API algorithm data structure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) struct kpp_alg {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) int (*set_secret)(struct crypto_kpp *tfm, const void *buffer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) unsigned int len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) int (*generate_public_key)(struct kpp_request *req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) int (*compute_shared_secret)(struct kpp_request *req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) unsigned int (*max_size)(struct crypto_kpp *tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) int (*init)(struct crypto_kpp *tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) void (*exit)(struct crypto_kpp *tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) unsigned int reqsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) struct crypto_alg base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) * DOC: Generic Key-agreement Protocol Primitives API
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) * The KPP API is used with the algorithm type
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) * CRYPTO_ALG_TYPE_KPP (listed as type "kpp" in /proc/crypto)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) * crypto_alloc_kpp() - allocate KPP tfm handle
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) * @alg_name: is the name of the kpp algorithm (e.g. "dh", "ecdh")
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) * @type: specifies the type of the algorithm
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) * @mask: specifies the mask for the algorithm
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) * Allocate a handle for kpp algorithm. The returned struct crypto_kpp
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) * is required for any following API invocation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) * Return: allocated handle in case of success; IS_ERR() is true in case of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) * an error, PTR_ERR() returns the error code.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) struct crypto_kpp *crypto_alloc_kpp(const char *alg_name, u32 type, u32 mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) static inline struct crypto_tfm *crypto_kpp_tfm(struct crypto_kpp *tfm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) return &tfm->base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) static inline struct kpp_alg *__crypto_kpp_alg(struct crypto_alg *alg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) return container_of(alg, struct kpp_alg, base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) static inline struct crypto_kpp *__crypto_kpp_tfm(struct crypto_tfm *tfm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) return container_of(tfm, struct crypto_kpp, base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) static inline struct kpp_alg *crypto_kpp_alg(struct crypto_kpp *tfm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) return __crypto_kpp_alg(crypto_kpp_tfm(tfm)->__crt_alg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) static inline unsigned int crypto_kpp_reqsize(struct crypto_kpp *tfm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) return crypto_kpp_alg(tfm)->reqsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) static inline void kpp_request_set_tfm(struct kpp_request *req,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) struct crypto_kpp *tfm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) req->base.tfm = crypto_kpp_tfm(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) static inline struct crypto_kpp *crypto_kpp_reqtfm(struct kpp_request *req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) return __crypto_kpp_tfm(req->base.tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) static inline u32 crypto_kpp_get_flags(struct crypto_kpp *tfm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) return crypto_tfm_get_flags(crypto_kpp_tfm(tfm));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) static inline void crypto_kpp_set_flags(struct crypto_kpp *tfm, u32 flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) crypto_tfm_set_flags(crypto_kpp_tfm(tfm), flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) * crypto_free_kpp() - free KPP tfm handle
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) * @tfm: KPP tfm handle allocated with crypto_alloc_kpp()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) * If @tfm is a NULL or error pointer, this function does nothing.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) static inline void crypto_free_kpp(struct crypto_kpp *tfm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) crypto_destroy_tfm(tfm, crypto_kpp_tfm(tfm));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) * kpp_request_alloc() - allocates kpp request
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) * @tfm: KPP tfm handle allocated with crypto_alloc_kpp()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) * @gfp: allocation flags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) * Return: allocated handle in case of success or NULL in case of an error.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) static inline struct kpp_request *kpp_request_alloc(struct crypto_kpp *tfm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) gfp_t gfp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) struct kpp_request *req;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) req = kmalloc(sizeof(*req) + crypto_kpp_reqsize(tfm), gfp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) if (likely(req))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) kpp_request_set_tfm(req, tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) return req;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) * kpp_request_free() - zeroize and free kpp request
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) * @req: request to free
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) static inline void kpp_request_free(struct kpp_request *req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) kfree_sensitive(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) * kpp_request_set_callback() - Sets an asynchronous callback.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) * Callback will be called when an asynchronous operation on a given
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) * request is finished.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) * @req: request that the callback will be set for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) * @flgs: specify for instance if the operation may backlog
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) * @cmpl: callback which will be called
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) * @data: private data used by the caller
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) static inline void kpp_request_set_callback(struct kpp_request *req,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) u32 flgs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) crypto_completion_t cmpl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) req->base.complete = cmpl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) req->base.data = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) req->base.flags = flgs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) * kpp_request_set_input() - Sets input buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) * Sets parameters required by generate_public_key
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) * @req: kpp request
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) * @input: ptr to input scatter list
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) * @input_len: size of the input scatter list
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) static inline void kpp_request_set_input(struct kpp_request *req,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) struct scatterlist *input,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) unsigned int input_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) req->src = input;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) req->src_len = input_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) * kpp_request_set_output() - Sets output buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) * Sets parameters required by kpp operation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) * @req: kpp request
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) * @output: ptr to output scatter list
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) * @output_len: size of the output scatter list
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) static inline void kpp_request_set_output(struct kpp_request *req,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) struct scatterlist *output,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) unsigned int output_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) req->dst = output;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) req->dst_len = output_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) enum {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) CRYPTO_KPP_SECRET_TYPE_UNKNOWN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) CRYPTO_KPP_SECRET_TYPE_DH,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) CRYPTO_KPP_SECRET_TYPE_ECDH,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) * struct kpp_secret - small header for packing secret buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) * @type: define type of secret. Each kpp type will define its own
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) * @len: specify the len of the secret, include the header, that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) * follows the struct
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) struct kpp_secret {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) unsigned short type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) unsigned short len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) * crypto_kpp_set_secret() - Invoke kpp operation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) * Function invokes the specific kpp operation for a given alg.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) * @tfm: tfm handle
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) * @buffer: Buffer holding the packet representation of the private
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) * key. The structure of the packet key depends on the particular
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) * KPP implementation. Packing and unpacking helpers are provided
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) * for ECDH and DH (see the respective header files for those
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) * implementations).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) * @len: Length of the packet private key buffer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) * Return: zero on success; error code in case of error
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) static inline int crypto_kpp_set_secret(struct crypto_kpp *tfm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) const void *buffer, unsigned int len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) struct kpp_alg *alg = crypto_kpp_alg(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) struct crypto_alg *calg = tfm->base.__crt_alg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) crypto_stats_get(calg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) ret = alg->set_secret(tfm, buffer, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) crypto_stats_kpp_set_secret(calg, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) * crypto_kpp_generate_public_key() - Invoke kpp operation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) * Function invokes the specific kpp operation for generating the public part
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) * for a given kpp algorithm.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) * To generate a private key, the caller should use a random number generator.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) * The output of the requested length serves as the private key.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) * @req: kpp key request
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) * Return: zero on success; error code in case of error
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) static inline int crypto_kpp_generate_public_key(struct kpp_request *req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) struct crypto_kpp *tfm = crypto_kpp_reqtfm(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) struct kpp_alg *alg = crypto_kpp_alg(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) struct crypto_alg *calg = tfm->base.__crt_alg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) crypto_stats_get(calg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) ret = alg->generate_public_key(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) crypto_stats_kpp_generate_public_key(calg, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) * crypto_kpp_compute_shared_secret() - Invoke kpp operation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) * Function invokes the specific kpp operation for computing the shared secret
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) * for a given kpp algorithm.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) * @req: kpp key request
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) * Return: zero on success; error code in case of error
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) static inline int crypto_kpp_compute_shared_secret(struct kpp_request *req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) struct crypto_kpp *tfm = crypto_kpp_reqtfm(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) struct kpp_alg *alg = crypto_kpp_alg(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) struct crypto_alg *calg = tfm->base.__crt_alg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) crypto_stats_get(calg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) ret = alg->compute_shared_secret(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) crypto_stats_kpp_compute_shared_secret(calg, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) * crypto_kpp_maxsize() - Get len for output buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) * Function returns the output buffer size required for a given key.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) * Function assumes that the key is already set in the transformation. If this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) * function is called without a setkey or with a failed setkey, you will end up
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) * in a NULL dereference.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) * @tfm: KPP tfm handle allocated with crypto_alloc_kpp()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) static inline unsigned int crypto_kpp_maxsize(struct crypto_kpp *tfm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) struct kpp_alg *alg = crypto_kpp_alg(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) return alg->max_size(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) #endif