^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) * Public Key Encryption
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (c) 2015, Intel Corporation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Authors: Tadeusz Struk <tadeusz.struk@intel.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #ifndef _CRYPTO_AKCIPHER_H
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #define _CRYPTO_AKCIPHER_H
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/crypto.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) * struct akcipher_request - public key request
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) * @base: Common attributes for async crypto requests
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) * @src: Source data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) * For verify op this is signature + digest, in that case
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) * total size of @src is @src_len + @dst_len.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) * @dst: Destination data (Should be NULL for verify op)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) * @src_len: Size of the input buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) * For verify op it's size of signature part of @src, this part
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) * is supposed to be operated by cipher.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) * @dst_len: Size of @dst buffer (for all ops except verify).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) * It needs to be at least as big as the expected result
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) * depending on the operation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) * After operation it will be updated with the actual size of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) * result.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) * In case of error where the dst sgl size was insufficient,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) * it will be updated to the size required for the operation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) * For verify op this is size of digest part in @src.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) * @__ctx: Start of private context data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) struct akcipher_request {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) struct crypto_async_request base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) struct scatterlist *src;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) struct scatterlist *dst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) unsigned int src_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) unsigned int dst_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) void *__ctx[] CRYPTO_MINALIGN_ATTR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) * struct crypto_akcipher - user-instantiated objects which encapsulate
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) * algorithms and core processing logic
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) * @base: Common crypto API algorithm data structure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) struct crypto_akcipher {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) struct crypto_tfm base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) * struct akcipher_alg - generic public key algorithm
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) * @sign: Function performs a sign operation as defined by public key
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) * algorithm. In case of error, where the dst_len was insufficient,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) * the req->dst_len will be updated to the size required for the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) * operation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) * @verify: Function performs a complete verify operation as defined by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) * public key algorithm, returning verification status. Requires
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) * digest value as input parameter.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) * @encrypt: Function performs an encrypt operation as defined by public key
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) * algorithm. In case of error, where the dst_len was insufficient,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) * the req->dst_len will be updated to the size required for the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) * operation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) * @decrypt: Function performs a decrypt operation as defined by public key
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) * algorithm. In case of error, where the dst_len was insufficient,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) * the req->dst_len will be updated to the size required for the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) * operation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) * @set_pub_key: Function invokes the algorithm specific set public key
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) * function, which knows how to decode and interpret
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) * the BER encoded public key and parameters
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) * @set_priv_key: Function invokes the algorithm specific set private key
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) * function, which knows how to decode and interpret
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) * the BER encoded private key and parameters
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) * @max_size: Function returns dest buffer size required for a given key.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) * @init: Initialize the cryptographic transformation object.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) * This function is used to initialize the cryptographic
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) * transformation object. This function is called only once at
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) * the instantiation time, right after the transformation context
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) * was allocated. In case the cryptographic hardware has some
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) * special requirements which need to be handled by software, this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) * function shall check for the precise requirement of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) * transformation and put any software fallbacks in place.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) * @exit: Deinitialize the cryptographic transformation object. This is a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) * counterpart to @init, used to remove various changes set in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) * @init.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) * @reqsize: Request context size required by algorithm implementation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) * @base: Common crypto API algorithm data structure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) struct akcipher_alg {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) int (*sign)(struct akcipher_request *req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) int (*verify)(struct akcipher_request *req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) int (*encrypt)(struct akcipher_request *req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) int (*decrypt)(struct akcipher_request *req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) int (*set_pub_key)(struct crypto_akcipher *tfm, const void *key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) unsigned int keylen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) int (*set_priv_key)(struct crypto_akcipher *tfm, const void *key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) unsigned int keylen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) unsigned int (*max_size)(struct crypto_akcipher *tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) int (*init)(struct crypto_akcipher *tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) void (*exit)(struct crypto_akcipher *tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) unsigned int reqsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) struct crypto_alg base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) * DOC: Generic Public Key API
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) * The Public Key API is used with the algorithms of type
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) * CRYPTO_ALG_TYPE_AKCIPHER (listed as type "akcipher" in /proc/crypto)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) */
^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) * crypto_alloc_akcipher() - allocate AKCIPHER tfm handle
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) * public key algorithm e.g. "rsa"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) * @type: specifies the type of the algorithm
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) * @mask: specifies the mask for the algorithm
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) * Allocate a handle for public key algorithm. The returned struct
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) * crypto_akcipher is the handle that is required for any subsequent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) * API invocation for the public key operations.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) * Return: allocated handle in case of success; IS_ERR() is true in case
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) * of an error, PTR_ERR() returns the error code.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) struct crypto_akcipher *crypto_alloc_akcipher(const char *alg_name, u32 type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) u32 mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) static inline struct crypto_tfm *crypto_akcipher_tfm(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) struct crypto_akcipher *tfm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) return &tfm->base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) static inline struct akcipher_alg *__crypto_akcipher_alg(struct crypto_alg *alg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) return container_of(alg, struct akcipher_alg, base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) static inline struct crypto_akcipher *__crypto_akcipher_tfm(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) struct crypto_tfm *tfm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) return container_of(tfm, struct crypto_akcipher, base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) static inline struct akcipher_alg *crypto_akcipher_alg(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) struct crypto_akcipher *tfm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) return __crypto_akcipher_alg(crypto_akcipher_tfm(tfm)->__crt_alg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) static inline unsigned int crypto_akcipher_reqsize(struct crypto_akcipher *tfm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) return crypto_akcipher_alg(tfm)->reqsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) static inline void akcipher_request_set_tfm(struct akcipher_request *req,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) struct crypto_akcipher *tfm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) req->base.tfm = crypto_akcipher_tfm(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) static inline struct crypto_akcipher *crypto_akcipher_reqtfm(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) struct akcipher_request *req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) return __crypto_akcipher_tfm(req->base.tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) * crypto_free_akcipher() - free AKCIPHER tfm handle
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) * @tfm: AKCIPHER tfm handle allocated with crypto_alloc_akcipher()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) * If @tfm is a NULL or error pointer, this function does nothing.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) static inline void crypto_free_akcipher(struct crypto_akcipher *tfm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) crypto_destroy_tfm(tfm, crypto_akcipher_tfm(tfm));
^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) * akcipher_request_alloc() - allocates public key request
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) * @tfm: AKCIPHER tfm handle allocated with crypto_alloc_akcipher()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) * @gfp: allocation flags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) * Return: allocated handle in case of success or NULL in case of an error.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) static inline struct akcipher_request *akcipher_request_alloc(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) struct crypto_akcipher *tfm, gfp_t gfp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) struct akcipher_request *req;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) req = kmalloc(sizeof(*req) + crypto_akcipher_reqsize(tfm), gfp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) if (likely(req))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) akcipher_request_set_tfm(req, tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) return req;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) * akcipher_request_free() - zeroize and free public key request
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) * @req: request to free
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) static inline void akcipher_request_free(struct akcipher_request *req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) kfree_sensitive(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) }
^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) * akcipher_request_set_callback() - Sets an asynchronous callback.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) * Callback will be called when an asynchronous operation on a given
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) * request is finished.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) * @req: request that the callback will be set for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) * @flgs: specify for instance if the operation may backlog
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) * @cmpl: callback which will be called
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) * @data: private data used by the caller
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) static inline void akcipher_request_set_callback(struct akcipher_request *req,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) u32 flgs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) crypto_completion_t cmpl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) req->base.complete = cmpl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) req->base.data = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) req->base.flags = flgs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) * akcipher_request_set_crypt() - Sets request parameters
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) * Sets parameters required by crypto operation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) * @req: public key request
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) * @src: ptr to input scatter list
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) * @dst: ptr to output scatter list or NULL for verify op
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) * @src_len: size of the src input scatter list to be processed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) * @dst_len: size of the dst output scatter list or size of signature
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) * portion in @src for verify op
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) static inline void akcipher_request_set_crypt(struct akcipher_request *req,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) struct scatterlist *src,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) struct scatterlist *dst,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) unsigned int src_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) unsigned int dst_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) req->src = src;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) req->dst = dst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) req->src_len = src_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) req->dst_len = dst_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) * crypto_akcipher_maxsize() - Get len for output buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) * Function returns the dest buffer size required for a given key.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) * Function assumes that the key is already set in the transformation. If this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) * function is called without a setkey or with a failed setkey, you will end up
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) * in a NULL dereference.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) * @tfm: AKCIPHER tfm handle allocated with crypto_alloc_akcipher()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) static inline unsigned int crypto_akcipher_maxsize(struct crypto_akcipher *tfm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) struct akcipher_alg *alg = crypto_akcipher_alg(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) return alg->max_size(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) * crypto_akcipher_encrypt() - Invoke public key encrypt operation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) * Function invokes the specific public key encrypt operation for a given
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) * public key algorithm
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) * @req: asymmetric key request
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) * Return: zero on success; error code in case of error
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) static inline int crypto_akcipher_encrypt(struct akcipher_request *req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) struct akcipher_alg *alg = crypto_akcipher_alg(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) struct crypto_alg *calg = tfm->base.__crt_alg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) unsigned int src_len = req->src_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) crypto_stats_get(calg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) ret = alg->encrypt(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) crypto_stats_akcipher_encrypt(src_len, ret, calg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) * crypto_akcipher_decrypt() - Invoke public key decrypt operation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) * Function invokes the specific public key decrypt operation for a given
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) * public key algorithm
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) * @req: asymmetric key request
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) * Return: zero on success; error code in case of error
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) static inline int crypto_akcipher_decrypt(struct akcipher_request *req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) struct akcipher_alg *alg = crypto_akcipher_alg(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) struct crypto_alg *calg = tfm->base.__crt_alg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) unsigned int src_len = req->src_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) crypto_stats_get(calg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) ret = alg->decrypt(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) crypto_stats_akcipher_decrypt(src_len, ret, calg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) * crypto_akcipher_sign() - Invoke public key sign operation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) * Function invokes the specific public key sign operation for a given
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) * public key algorithm
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) * @req: asymmetric key request
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) * Return: zero on success; error code in case of error
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) static inline int crypto_akcipher_sign(struct akcipher_request *req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) struct akcipher_alg *alg = crypto_akcipher_alg(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) struct crypto_alg *calg = tfm->base.__crt_alg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) crypto_stats_get(calg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) ret = alg->sign(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) crypto_stats_akcipher_sign(ret, calg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) * crypto_akcipher_verify() - Invoke public key signature verification
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) * Function invokes the specific public key signature verification operation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) * for a given public key algorithm.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) * @req: asymmetric key request
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) * Note: req->dst should be NULL, req->src should point to SG of size
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) * (req->src_size + req->dst_size), containing signature (of req->src_size
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) * length) with appended digest (of req->dst_size length).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) * Return: zero on verification success; error code in case of error.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) static inline int crypto_akcipher_verify(struct akcipher_request *req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) struct akcipher_alg *alg = crypto_akcipher_alg(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) struct crypto_alg *calg = tfm->base.__crt_alg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) crypto_stats_get(calg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) ret = alg->verify(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) crypto_stats_akcipher_verify(ret, calg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) * crypto_akcipher_set_pub_key() - Invoke set public key operation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) * Function invokes the algorithm specific set key function, which knows
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) * how to decode and interpret the encoded key and parameters
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) * @tfm: tfm handle
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) * @key: BER encoded public key, algo OID, paramlen, BER encoded
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) * parameters
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) * @keylen: length of the key (not including other data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) * Return: zero on success; error code in case of error
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) static inline int crypto_akcipher_set_pub_key(struct crypto_akcipher *tfm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) const void *key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) unsigned int keylen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) struct akcipher_alg *alg = crypto_akcipher_alg(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) return alg->set_pub_key(tfm, key, keylen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) * crypto_akcipher_set_priv_key() - Invoke set private key operation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) * Function invokes the algorithm specific set key function, which knows
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) * how to decode and interpret the encoded key and parameters
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) * @tfm: tfm handle
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) * @key: BER encoded private key, algo OID, paramlen, BER encoded
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) * parameters
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) * @keylen: length of the key (not including other data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) * Return: zero on success; error code in case of error
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) static inline int crypto_akcipher_set_priv_key(struct crypto_akcipher *tfm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) const void *key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) unsigned int keylen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) struct akcipher_alg *alg = crypto_akcipher_alg(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) return alg->set_priv_key(tfm, key, keylen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) #endif