/* SPDX-License-Identifier: GPL-2.0-or-later */1/*2* Public Key Encryption3*4* Copyright (c) 2015, Intel Corporation5* Authors: Tadeusz Struk <[email protected]>6*/7#ifndef _CRYPTO_AKCIPHER_H8#define _CRYPTO_AKCIPHER_H910#include <linux/atomic.h>11#include <linux/crypto.h>1213/**14* struct akcipher_request - public key cipher request15*16* @base: Common attributes for async crypto requests17* @src: Source data18* @dst: Destination data19* @src_len: Size of the input buffer20* @dst_len: Size of @dst buffer21* It needs to be at least as big as the expected result22* depending on the operation.23* After operation it will be updated with the actual size of the24* result.25* In case of error where the dst sgl size was insufficient,26* it will be updated to the size required for the operation.27* @__ctx: Start of private context data28*/29struct akcipher_request {30struct crypto_async_request base;31struct scatterlist *src;32struct scatterlist *dst;33unsigned int src_len;34unsigned int dst_len;35void *__ctx[] CRYPTO_MINALIGN_ATTR;36};3738/**39* struct crypto_akcipher - user-instantiated objects which encapsulate40* algorithms and core processing logic41*42* @reqsize: Request context size required by algorithm implementation43* @base: Common crypto API algorithm data structure44*/45struct crypto_akcipher {46unsigned int reqsize;4748struct crypto_tfm base;49};5051/**52* struct akcipher_alg - generic public key cipher algorithm53*54* @encrypt: Function performs an encrypt operation as defined by public key55* algorithm. In case of error, where the dst_len was insufficient,56* the req->dst_len will be updated to the size required for the57* operation58* @decrypt: Function performs a decrypt operation as defined by public key59* algorithm. In case of error, where the dst_len was insufficient,60* the req->dst_len will be updated to the size required for the61* operation62* @set_pub_key: Function invokes the algorithm specific set public key63* function, which knows how to decode and interpret64* the BER encoded public key and parameters65* @set_priv_key: Function invokes the algorithm specific set private key66* function, which knows how to decode and interpret67* the BER encoded private key and parameters68* @max_size: Function returns dest buffer size required for a given key.69* @init: Initialize the cryptographic transformation object.70* This function is used to initialize the cryptographic71* transformation object. This function is called only once at72* the instantiation time, right after the transformation context73* was allocated. In case the cryptographic hardware has some74* special requirements which need to be handled by software, this75* function shall check for the precise requirement of the76* transformation and put any software fallbacks in place.77* @exit: Deinitialize the cryptographic transformation object. This is a78* counterpart to @init, used to remove various changes set in79* @init.80*81* @base: Common crypto API algorithm data structure82*/83struct akcipher_alg {84int (*encrypt)(struct akcipher_request *req);85int (*decrypt)(struct akcipher_request *req);86int (*set_pub_key)(struct crypto_akcipher *tfm, const void *key,87unsigned int keylen);88int (*set_priv_key)(struct crypto_akcipher *tfm, const void *key,89unsigned int keylen);90unsigned int (*max_size)(struct crypto_akcipher *tfm);91int (*init)(struct crypto_akcipher *tfm);92void (*exit)(struct crypto_akcipher *tfm);9394struct crypto_alg base;95};9697/**98* DOC: Generic Public Key Cipher API99*100* The Public Key Cipher API is used with the algorithms of type101* CRYPTO_ALG_TYPE_AKCIPHER (listed as type "akcipher" in /proc/crypto)102*/103104/**105* crypto_alloc_akcipher() - allocate AKCIPHER tfm handle106* @alg_name: is the cra_name / name or cra_driver_name / driver name of the107* public key algorithm e.g. "rsa"108* @type: specifies the type of the algorithm109* @mask: specifies the mask for the algorithm110*111* Allocate a handle for public key algorithm. The returned struct112* crypto_akcipher is the handle that is required for any subsequent113* API invocation for the public key operations.114*115* Return: allocated handle in case of success; IS_ERR() is true in case116* of an error, PTR_ERR() returns the error code.117*/118struct crypto_akcipher *crypto_alloc_akcipher(const char *alg_name, u32 type,119u32 mask);120121static inline struct crypto_tfm *crypto_akcipher_tfm(122struct crypto_akcipher *tfm)123{124return &tfm->base;125}126127static inline struct akcipher_alg *__crypto_akcipher_alg(struct crypto_alg *alg)128{129return container_of(alg, struct akcipher_alg, base);130}131132static inline struct crypto_akcipher *__crypto_akcipher_tfm(133struct crypto_tfm *tfm)134{135return container_of(tfm, struct crypto_akcipher, base);136}137138static inline struct akcipher_alg *crypto_akcipher_alg(139struct crypto_akcipher *tfm)140{141return __crypto_akcipher_alg(crypto_akcipher_tfm(tfm)->__crt_alg);142}143144static inline unsigned int crypto_akcipher_reqsize(struct crypto_akcipher *tfm)145{146return tfm->reqsize;147}148149static inline void akcipher_request_set_tfm(struct akcipher_request *req,150struct crypto_akcipher *tfm)151{152req->base.tfm = crypto_akcipher_tfm(tfm);153}154155static inline struct crypto_akcipher *crypto_akcipher_reqtfm(156struct akcipher_request *req)157{158return __crypto_akcipher_tfm(req->base.tfm);159}160161/**162* crypto_free_akcipher() - free AKCIPHER tfm handle163*164* @tfm: AKCIPHER tfm handle allocated with crypto_alloc_akcipher()165*166* If @tfm is a NULL or error pointer, this function does nothing.167*/168static inline void crypto_free_akcipher(struct crypto_akcipher *tfm)169{170crypto_destroy_tfm(tfm, crypto_akcipher_tfm(tfm));171}172173/**174* akcipher_request_alloc() - allocates public key request175*176* @tfm: AKCIPHER tfm handle allocated with crypto_alloc_akcipher()177* @gfp: allocation flags178*179* Return: allocated handle in case of success or NULL in case of an error.180*/181static inline struct akcipher_request *akcipher_request_alloc(182struct crypto_akcipher *tfm, gfp_t gfp)183{184struct akcipher_request *req;185186req = kmalloc(sizeof(*req) + crypto_akcipher_reqsize(tfm), gfp);187if (likely(req))188akcipher_request_set_tfm(req, tfm);189190return req;191}192193/**194* akcipher_request_free() - zeroize and free public key request195*196* @req: request to free197*/198static inline void akcipher_request_free(struct akcipher_request *req)199{200kfree_sensitive(req);201}202203/**204* akcipher_request_set_callback() - Sets an asynchronous callback.205*206* Callback will be called when an asynchronous operation on a given207* request is finished.208*209* @req: request that the callback will be set for210* @flgs: specify for instance if the operation may backlog211* @cmpl: callback which will be called212* @data: private data used by the caller213*/214static inline void akcipher_request_set_callback(struct akcipher_request *req,215u32 flgs,216crypto_completion_t cmpl,217void *data)218{219req->base.complete = cmpl;220req->base.data = data;221req->base.flags = flgs;222}223224/**225* akcipher_request_set_crypt() - Sets request parameters226*227* Sets parameters required by crypto operation228*229* @req: public key request230* @src: ptr to input scatter list231* @dst: ptr to output scatter list232* @src_len: size of the src input scatter list to be processed233* @dst_len: size of the dst output scatter list234*/235static inline void akcipher_request_set_crypt(struct akcipher_request *req,236struct scatterlist *src,237struct scatterlist *dst,238unsigned int src_len,239unsigned int dst_len)240{241req->src = src;242req->dst = dst;243req->src_len = src_len;244req->dst_len = dst_len;245}246247/**248* crypto_akcipher_maxsize() - Get len for output buffer249*250* Function returns the dest buffer size required for a given key.251* Function assumes that the key is already set in the transformation. If this252* function is called without a setkey or with a failed setkey, you will end up253* in a NULL dereference.254*255* @tfm: AKCIPHER tfm handle allocated with crypto_alloc_akcipher()256*/257static inline unsigned int crypto_akcipher_maxsize(struct crypto_akcipher *tfm)258{259struct akcipher_alg *alg = crypto_akcipher_alg(tfm);260261return alg->max_size(tfm);262}263264/**265* crypto_akcipher_encrypt() - Invoke public key encrypt operation266*267* Function invokes the specific public key encrypt operation for a given268* public key algorithm269*270* @req: asymmetric key request271*272* Return: zero on success; error code in case of error273*/274static inline int crypto_akcipher_encrypt(struct akcipher_request *req)275{276struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);277278return crypto_akcipher_alg(tfm)->encrypt(req);279}280281/**282* crypto_akcipher_decrypt() - Invoke public key decrypt operation283*284* Function invokes the specific public key decrypt operation for a given285* public key algorithm286*287* @req: asymmetric key request288*289* Return: zero on success; error code in case of error290*/291static inline int crypto_akcipher_decrypt(struct akcipher_request *req)292{293struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);294295return crypto_akcipher_alg(tfm)->decrypt(req);296}297298/**299* crypto_akcipher_sync_encrypt() - Invoke public key encrypt operation300*301* Function invokes the specific public key encrypt operation for a given302* public key algorithm303*304* @tfm: AKCIPHER tfm handle allocated with crypto_alloc_akcipher()305* @src: source buffer306* @slen: source length307* @dst: destination obuffer308* @dlen: destination length309*310* Return: zero on success; error code in case of error311*/312int crypto_akcipher_sync_encrypt(struct crypto_akcipher *tfm,313const void *src, unsigned int slen,314void *dst, unsigned int dlen);315316/**317* crypto_akcipher_sync_decrypt() - Invoke public key decrypt operation318*319* Function invokes the specific public key decrypt operation for a given320* public key algorithm321*322* @tfm: AKCIPHER tfm handle allocated with crypto_alloc_akcipher()323* @src: source buffer324* @slen: source length325* @dst: destination obuffer326* @dlen: destination length327*328* Return: Output length on success; error code in case of error329*/330int crypto_akcipher_sync_decrypt(struct crypto_akcipher *tfm,331const void *src, unsigned int slen,332void *dst, unsigned int dlen);333334/**335* crypto_akcipher_set_pub_key() - Invoke set public key operation336*337* Function invokes the algorithm specific set key function, which knows338* how to decode and interpret the encoded key and parameters339*340* @tfm: tfm handle341* @key: BER encoded public key, algo OID, paramlen, BER encoded342* parameters343* @keylen: length of the key (not including other data)344*345* Return: zero on success; error code in case of error346*/347static inline int crypto_akcipher_set_pub_key(struct crypto_akcipher *tfm,348const void *key,349unsigned int keylen)350{351struct akcipher_alg *alg = crypto_akcipher_alg(tfm);352353return alg->set_pub_key(tfm, key, keylen);354}355356/**357* crypto_akcipher_set_priv_key() - Invoke set private key operation358*359* Function invokes the algorithm specific set key function, which knows360* how to decode and interpret the encoded key and parameters361*362* @tfm: tfm handle363* @key: BER encoded private key, algo OID, paramlen, BER encoded364* parameters365* @keylen: length of the key (not including other data)366*367* Return: zero on success; error code in case of error368*/369static inline int crypto_akcipher_set_priv_key(struct crypto_akcipher *tfm,370const void *key,371unsigned int keylen)372{373struct akcipher_alg *alg = crypto_akcipher_alg(tfm);374375return alg->set_priv_key(tfm, key, keylen);376}377#endif378379380