18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-or-later */ 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * Public Key Encryption 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * Copyright (c) 2015, Intel Corporation 68c2ecf20Sopenharmony_ci * Authors: Tadeusz Struk <tadeusz.struk@intel.com> 78c2ecf20Sopenharmony_ci */ 88c2ecf20Sopenharmony_ci#ifndef _CRYPTO_AKCIPHER_H 98c2ecf20Sopenharmony_ci#define _CRYPTO_AKCIPHER_H 108c2ecf20Sopenharmony_ci#include <linux/crypto.h> 118c2ecf20Sopenharmony_ci 128c2ecf20Sopenharmony_ci/** 138c2ecf20Sopenharmony_ci * struct akcipher_request - public key request 148c2ecf20Sopenharmony_ci * 158c2ecf20Sopenharmony_ci * @base: Common attributes for async crypto requests 168c2ecf20Sopenharmony_ci * @src: Source data 178c2ecf20Sopenharmony_ci * For verify op this is signature + digest, in that case 188c2ecf20Sopenharmony_ci * total size of @src is @src_len + @dst_len. 198c2ecf20Sopenharmony_ci * @dst: Destination data (Should be NULL for verify op) 208c2ecf20Sopenharmony_ci * @src_len: Size of the input buffer 218c2ecf20Sopenharmony_ci * For verify op it's size of signature part of @src, this part 228c2ecf20Sopenharmony_ci * is supposed to be operated by cipher. 238c2ecf20Sopenharmony_ci * @dst_len: Size of @dst buffer (for all ops except verify). 248c2ecf20Sopenharmony_ci * It needs to be at least as big as the expected result 258c2ecf20Sopenharmony_ci * depending on the operation. 268c2ecf20Sopenharmony_ci * After operation it will be updated with the actual size of the 278c2ecf20Sopenharmony_ci * result. 288c2ecf20Sopenharmony_ci * In case of error where the dst sgl size was insufficient, 298c2ecf20Sopenharmony_ci * it will be updated to the size required for the operation. 308c2ecf20Sopenharmony_ci * For verify op this is size of digest part in @src. 318c2ecf20Sopenharmony_ci * @__ctx: Start of private context data 328c2ecf20Sopenharmony_ci */ 338c2ecf20Sopenharmony_cistruct akcipher_request { 348c2ecf20Sopenharmony_ci struct crypto_async_request base; 358c2ecf20Sopenharmony_ci struct scatterlist *src; 368c2ecf20Sopenharmony_ci struct scatterlist *dst; 378c2ecf20Sopenharmony_ci unsigned int src_len; 388c2ecf20Sopenharmony_ci unsigned int dst_len; 398c2ecf20Sopenharmony_ci void *__ctx[] CRYPTO_MINALIGN_ATTR; 408c2ecf20Sopenharmony_ci}; 418c2ecf20Sopenharmony_ci 428c2ecf20Sopenharmony_ci/** 438c2ecf20Sopenharmony_ci * struct crypto_akcipher - user-instantiated objects which encapsulate 448c2ecf20Sopenharmony_ci * algorithms and core processing logic 458c2ecf20Sopenharmony_ci * 468c2ecf20Sopenharmony_ci * @base: Common crypto API algorithm data structure 478c2ecf20Sopenharmony_ci */ 488c2ecf20Sopenharmony_cistruct crypto_akcipher { 498c2ecf20Sopenharmony_ci struct crypto_tfm base; 508c2ecf20Sopenharmony_ci}; 518c2ecf20Sopenharmony_ci 528c2ecf20Sopenharmony_ci/** 538c2ecf20Sopenharmony_ci * struct akcipher_alg - generic public key algorithm 548c2ecf20Sopenharmony_ci * 558c2ecf20Sopenharmony_ci * @sign: Function performs a sign operation as defined by public key 568c2ecf20Sopenharmony_ci * algorithm. In case of error, where the dst_len was insufficient, 578c2ecf20Sopenharmony_ci * the req->dst_len will be updated to the size required for the 588c2ecf20Sopenharmony_ci * operation 598c2ecf20Sopenharmony_ci * @verify: Function performs a complete verify operation as defined by 608c2ecf20Sopenharmony_ci * public key algorithm, returning verification status. Requires 618c2ecf20Sopenharmony_ci * digest value as input parameter. 628c2ecf20Sopenharmony_ci * @encrypt: Function performs an encrypt operation as defined by public key 638c2ecf20Sopenharmony_ci * algorithm. In case of error, where the dst_len was insufficient, 648c2ecf20Sopenharmony_ci * the req->dst_len will be updated to the size required for the 658c2ecf20Sopenharmony_ci * operation 668c2ecf20Sopenharmony_ci * @decrypt: Function performs a decrypt operation as defined by public key 678c2ecf20Sopenharmony_ci * algorithm. In case of error, where the dst_len was insufficient, 688c2ecf20Sopenharmony_ci * the req->dst_len will be updated to the size required for the 698c2ecf20Sopenharmony_ci * operation 708c2ecf20Sopenharmony_ci * @set_pub_key: Function invokes the algorithm specific set public key 718c2ecf20Sopenharmony_ci * function, which knows how to decode and interpret 728c2ecf20Sopenharmony_ci * the BER encoded public key and parameters 738c2ecf20Sopenharmony_ci * @set_priv_key: Function invokes the algorithm specific set private key 748c2ecf20Sopenharmony_ci * function, which knows how to decode and interpret 758c2ecf20Sopenharmony_ci * the BER encoded private key and parameters 768c2ecf20Sopenharmony_ci * @max_size: Function returns dest buffer size required for a given key. 778c2ecf20Sopenharmony_ci * @init: Initialize the cryptographic transformation object. 788c2ecf20Sopenharmony_ci * This function is used to initialize the cryptographic 798c2ecf20Sopenharmony_ci * transformation object. This function is called only once at 808c2ecf20Sopenharmony_ci * the instantiation time, right after the transformation context 818c2ecf20Sopenharmony_ci * was allocated. In case the cryptographic hardware has some 828c2ecf20Sopenharmony_ci * special requirements which need to be handled by software, this 838c2ecf20Sopenharmony_ci * function shall check for the precise requirement of the 848c2ecf20Sopenharmony_ci * transformation and put any software fallbacks in place. 858c2ecf20Sopenharmony_ci * @exit: Deinitialize the cryptographic transformation object. This is a 868c2ecf20Sopenharmony_ci * counterpart to @init, used to remove various changes set in 878c2ecf20Sopenharmony_ci * @init. 888c2ecf20Sopenharmony_ci * 898c2ecf20Sopenharmony_ci * @reqsize: Request context size required by algorithm implementation 908c2ecf20Sopenharmony_ci * @base: Common crypto API algorithm data structure 918c2ecf20Sopenharmony_ci */ 928c2ecf20Sopenharmony_cistruct akcipher_alg { 938c2ecf20Sopenharmony_ci int (*sign)(struct akcipher_request *req); 948c2ecf20Sopenharmony_ci int (*verify)(struct akcipher_request *req); 958c2ecf20Sopenharmony_ci int (*encrypt)(struct akcipher_request *req); 968c2ecf20Sopenharmony_ci int (*decrypt)(struct akcipher_request *req); 978c2ecf20Sopenharmony_ci int (*set_pub_key)(struct crypto_akcipher *tfm, const void *key, 988c2ecf20Sopenharmony_ci unsigned int keylen); 998c2ecf20Sopenharmony_ci int (*set_priv_key)(struct crypto_akcipher *tfm, const void *key, 1008c2ecf20Sopenharmony_ci unsigned int keylen); 1018c2ecf20Sopenharmony_ci unsigned int (*max_size)(struct crypto_akcipher *tfm); 1028c2ecf20Sopenharmony_ci int (*init)(struct crypto_akcipher *tfm); 1038c2ecf20Sopenharmony_ci void (*exit)(struct crypto_akcipher *tfm); 1048c2ecf20Sopenharmony_ci 1058c2ecf20Sopenharmony_ci unsigned int reqsize; 1068c2ecf20Sopenharmony_ci struct crypto_alg base; 1078c2ecf20Sopenharmony_ci}; 1088c2ecf20Sopenharmony_ci 1098c2ecf20Sopenharmony_ci/** 1108c2ecf20Sopenharmony_ci * DOC: Generic Public Key API 1118c2ecf20Sopenharmony_ci * 1128c2ecf20Sopenharmony_ci * The Public Key API is used with the algorithms of type 1138c2ecf20Sopenharmony_ci * CRYPTO_ALG_TYPE_AKCIPHER (listed as type "akcipher" in /proc/crypto) 1148c2ecf20Sopenharmony_ci */ 1158c2ecf20Sopenharmony_ci 1168c2ecf20Sopenharmony_ci/** 1178c2ecf20Sopenharmony_ci * crypto_alloc_akcipher() - allocate AKCIPHER tfm handle 1188c2ecf20Sopenharmony_ci * @alg_name: is the cra_name / name or cra_driver_name / driver name of the 1198c2ecf20Sopenharmony_ci * public key algorithm e.g. "rsa" 1208c2ecf20Sopenharmony_ci * @type: specifies the type of the algorithm 1218c2ecf20Sopenharmony_ci * @mask: specifies the mask for the algorithm 1228c2ecf20Sopenharmony_ci * 1238c2ecf20Sopenharmony_ci * Allocate a handle for public key algorithm. The returned struct 1248c2ecf20Sopenharmony_ci * crypto_akcipher is the handle that is required for any subsequent 1258c2ecf20Sopenharmony_ci * API invocation for the public key operations. 1268c2ecf20Sopenharmony_ci * 1278c2ecf20Sopenharmony_ci * Return: allocated handle in case of success; IS_ERR() is true in case 1288c2ecf20Sopenharmony_ci * of an error, PTR_ERR() returns the error code. 1298c2ecf20Sopenharmony_ci */ 1308c2ecf20Sopenharmony_cistruct crypto_akcipher *crypto_alloc_akcipher(const char *alg_name, u32 type, 1318c2ecf20Sopenharmony_ci u32 mask); 1328c2ecf20Sopenharmony_ci 1338c2ecf20Sopenharmony_cistatic inline struct crypto_tfm *crypto_akcipher_tfm( 1348c2ecf20Sopenharmony_ci struct crypto_akcipher *tfm) 1358c2ecf20Sopenharmony_ci{ 1368c2ecf20Sopenharmony_ci return &tfm->base; 1378c2ecf20Sopenharmony_ci} 1388c2ecf20Sopenharmony_ci 1398c2ecf20Sopenharmony_cistatic inline struct akcipher_alg *__crypto_akcipher_alg(struct crypto_alg *alg) 1408c2ecf20Sopenharmony_ci{ 1418c2ecf20Sopenharmony_ci return container_of(alg, struct akcipher_alg, base); 1428c2ecf20Sopenharmony_ci} 1438c2ecf20Sopenharmony_ci 1448c2ecf20Sopenharmony_cistatic inline struct crypto_akcipher *__crypto_akcipher_tfm( 1458c2ecf20Sopenharmony_ci struct crypto_tfm *tfm) 1468c2ecf20Sopenharmony_ci{ 1478c2ecf20Sopenharmony_ci return container_of(tfm, struct crypto_akcipher, base); 1488c2ecf20Sopenharmony_ci} 1498c2ecf20Sopenharmony_ci 1508c2ecf20Sopenharmony_cistatic inline struct akcipher_alg *crypto_akcipher_alg( 1518c2ecf20Sopenharmony_ci struct crypto_akcipher *tfm) 1528c2ecf20Sopenharmony_ci{ 1538c2ecf20Sopenharmony_ci return __crypto_akcipher_alg(crypto_akcipher_tfm(tfm)->__crt_alg); 1548c2ecf20Sopenharmony_ci} 1558c2ecf20Sopenharmony_ci 1568c2ecf20Sopenharmony_cistatic inline unsigned int crypto_akcipher_reqsize(struct crypto_akcipher *tfm) 1578c2ecf20Sopenharmony_ci{ 1588c2ecf20Sopenharmony_ci return crypto_akcipher_alg(tfm)->reqsize; 1598c2ecf20Sopenharmony_ci} 1608c2ecf20Sopenharmony_ci 1618c2ecf20Sopenharmony_cistatic inline void akcipher_request_set_tfm(struct akcipher_request *req, 1628c2ecf20Sopenharmony_ci struct crypto_akcipher *tfm) 1638c2ecf20Sopenharmony_ci{ 1648c2ecf20Sopenharmony_ci req->base.tfm = crypto_akcipher_tfm(tfm); 1658c2ecf20Sopenharmony_ci} 1668c2ecf20Sopenharmony_ci 1678c2ecf20Sopenharmony_cistatic inline struct crypto_akcipher *crypto_akcipher_reqtfm( 1688c2ecf20Sopenharmony_ci struct akcipher_request *req) 1698c2ecf20Sopenharmony_ci{ 1708c2ecf20Sopenharmony_ci return __crypto_akcipher_tfm(req->base.tfm); 1718c2ecf20Sopenharmony_ci} 1728c2ecf20Sopenharmony_ci 1738c2ecf20Sopenharmony_ci/** 1748c2ecf20Sopenharmony_ci * crypto_free_akcipher() - free AKCIPHER tfm handle 1758c2ecf20Sopenharmony_ci * 1768c2ecf20Sopenharmony_ci * @tfm: AKCIPHER tfm handle allocated with crypto_alloc_akcipher() 1778c2ecf20Sopenharmony_ci * 1788c2ecf20Sopenharmony_ci * If @tfm is a NULL or error pointer, this function does nothing. 1798c2ecf20Sopenharmony_ci */ 1808c2ecf20Sopenharmony_cistatic inline void crypto_free_akcipher(struct crypto_akcipher *tfm) 1818c2ecf20Sopenharmony_ci{ 1828c2ecf20Sopenharmony_ci crypto_destroy_tfm(tfm, crypto_akcipher_tfm(tfm)); 1838c2ecf20Sopenharmony_ci} 1848c2ecf20Sopenharmony_ci 1858c2ecf20Sopenharmony_ci/** 1868c2ecf20Sopenharmony_ci * akcipher_request_alloc() - allocates public key request 1878c2ecf20Sopenharmony_ci * 1888c2ecf20Sopenharmony_ci * @tfm: AKCIPHER tfm handle allocated with crypto_alloc_akcipher() 1898c2ecf20Sopenharmony_ci * @gfp: allocation flags 1908c2ecf20Sopenharmony_ci * 1918c2ecf20Sopenharmony_ci * Return: allocated handle in case of success or NULL in case of an error. 1928c2ecf20Sopenharmony_ci */ 1938c2ecf20Sopenharmony_cistatic inline struct akcipher_request *akcipher_request_alloc( 1948c2ecf20Sopenharmony_ci struct crypto_akcipher *tfm, gfp_t gfp) 1958c2ecf20Sopenharmony_ci{ 1968c2ecf20Sopenharmony_ci struct akcipher_request *req; 1978c2ecf20Sopenharmony_ci 1988c2ecf20Sopenharmony_ci req = kmalloc(sizeof(*req) + crypto_akcipher_reqsize(tfm), gfp); 1998c2ecf20Sopenharmony_ci if (likely(req)) 2008c2ecf20Sopenharmony_ci akcipher_request_set_tfm(req, tfm); 2018c2ecf20Sopenharmony_ci 2028c2ecf20Sopenharmony_ci return req; 2038c2ecf20Sopenharmony_ci} 2048c2ecf20Sopenharmony_ci 2058c2ecf20Sopenharmony_ci/** 2068c2ecf20Sopenharmony_ci * akcipher_request_free() - zeroize and free public key request 2078c2ecf20Sopenharmony_ci * 2088c2ecf20Sopenharmony_ci * @req: request to free 2098c2ecf20Sopenharmony_ci */ 2108c2ecf20Sopenharmony_cistatic inline void akcipher_request_free(struct akcipher_request *req) 2118c2ecf20Sopenharmony_ci{ 2128c2ecf20Sopenharmony_ci kfree_sensitive(req); 2138c2ecf20Sopenharmony_ci} 2148c2ecf20Sopenharmony_ci 2158c2ecf20Sopenharmony_ci/** 2168c2ecf20Sopenharmony_ci * akcipher_request_set_callback() - Sets an asynchronous callback. 2178c2ecf20Sopenharmony_ci * 2188c2ecf20Sopenharmony_ci * Callback will be called when an asynchronous operation on a given 2198c2ecf20Sopenharmony_ci * request is finished. 2208c2ecf20Sopenharmony_ci * 2218c2ecf20Sopenharmony_ci * @req: request that the callback will be set for 2228c2ecf20Sopenharmony_ci * @flgs: specify for instance if the operation may backlog 2238c2ecf20Sopenharmony_ci * @cmpl: callback which will be called 2248c2ecf20Sopenharmony_ci * @data: private data used by the caller 2258c2ecf20Sopenharmony_ci */ 2268c2ecf20Sopenharmony_cistatic inline void akcipher_request_set_callback(struct akcipher_request *req, 2278c2ecf20Sopenharmony_ci u32 flgs, 2288c2ecf20Sopenharmony_ci crypto_completion_t cmpl, 2298c2ecf20Sopenharmony_ci void *data) 2308c2ecf20Sopenharmony_ci{ 2318c2ecf20Sopenharmony_ci req->base.complete = cmpl; 2328c2ecf20Sopenharmony_ci req->base.data = data; 2338c2ecf20Sopenharmony_ci req->base.flags = flgs; 2348c2ecf20Sopenharmony_ci} 2358c2ecf20Sopenharmony_ci 2368c2ecf20Sopenharmony_ci/** 2378c2ecf20Sopenharmony_ci * akcipher_request_set_crypt() - Sets request parameters 2388c2ecf20Sopenharmony_ci * 2398c2ecf20Sopenharmony_ci * Sets parameters required by crypto operation 2408c2ecf20Sopenharmony_ci * 2418c2ecf20Sopenharmony_ci * @req: public key request 2428c2ecf20Sopenharmony_ci * @src: ptr to input scatter list 2438c2ecf20Sopenharmony_ci * @dst: ptr to output scatter list or NULL for verify op 2448c2ecf20Sopenharmony_ci * @src_len: size of the src input scatter list to be processed 2458c2ecf20Sopenharmony_ci * @dst_len: size of the dst output scatter list or size of signature 2468c2ecf20Sopenharmony_ci * portion in @src for verify op 2478c2ecf20Sopenharmony_ci */ 2488c2ecf20Sopenharmony_cistatic inline void akcipher_request_set_crypt(struct akcipher_request *req, 2498c2ecf20Sopenharmony_ci struct scatterlist *src, 2508c2ecf20Sopenharmony_ci struct scatterlist *dst, 2518c2ecf20Sopenharmony_ci unsigned int src_len, 2528c2ecf20Sopenharmony_ci unsigned int dst_len) 2538c2ecf20Sopenharmony_ci{ 2548c2ecf20Sopenharmony_ci req->src = src; 2558c2ecf20Sopenharmony_ci req->dst = dst; 2568c2ecf20Sopenharmony_ci req->src_len = src_len; 2578c2ecf20Sopenharmony_ci req->dst_len = dst_len; 2588c2ecf20Sopenharmony_ci} 2598c2ecf20Sopenharmony_ci 2608c2ecf20Sopenharmony_ci/** 2618c2ecf20Sopenharmony_ci * crypto_akcipher_maxsize() - Get len for output buffer 2628c2ecf20Sopenharmony_ci * 2638c2ecf20Sopenharmony_ci * Function returns the dest buffer size required for a given key. 2648c2ecf20Sopenharmony_ci * Function assumes that the key is already set in the transformation. If this 2658c2ecf20Sopenharmony_ci * function is called without a setkey or with a failed setkey, you will end up 2668c2ecf20Sopenharmony_ci * in a NULL dereference. 2678c2ecf20Sopenharmony_ci * 2688c2ecf20Sopenharmony_ci * @tfm: AKCIPHER tfm handle allocated with crypto_alloc_akcipher() 2698c2ecf20Sopenharmony_ci */ 2708c2ecf20Sopenharmony_cistatic inline unsigned int crypto_akcipher_maxsize(struct crypto_akcipher *tfm) 2718c2ecf20Sopenharmony_ci{ 2728c2ecf20Sopenharmony_ci struct akcipher_alg *alg = crypto_akcipher_alg(tfm); 2738c2ecf20Sopenharmony_ci 2748c2ecf20Sopenharmony_ci return alg->max_size(tfm); 2758c2ecf20Sopenharmony_ci} 2768c2ecf20Sopenharmony_ci 2778c2ecf20Sopenharmony_ci/** 2788c2ecf20Sopenharmony_ci * crypto_akcipher_encrypt() - Invoke public key encrypt operation 2798c2ecf20Sopenharmony_ci * 2808c2ecf20Sopenharmony_ci * Function invokes the specific public key encrypt operation for a given 2818c2ecf20Sopenharmony_ci * public key algorithm 2828c2ecf20Sopenharmony_ci * 2838c2ecf20Sopenharmony_ci * @req: asymmetric key request 2848c2ecf20Sopenharmony_ci * 2858c2ecf20Sopenharmony_ci * Return: zero on success; error code in case of error 2868c2ecf20Sopenharmony_ci */ 2878c2ecf20Sopenharmony_cistatic inline int crypto_akcipher_encrypt(struct akcipher_request *req) 2888c2ecf20Sopenharmony_ci{ 2898c2ecf20Sopenharmony_ci struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req); 2908c2ecf20Sopenharmony_ci struct akcipher_alg *alg = crypto_akcipher_alg(tfm); 2918c2ecf20Sopenharmony_ci struct crypto_alg *calg = tfm->base.__crt_alg; 2928c2ecf20Sopenharmony_ci unsigned int src_len = req->src_len; 2938c2ecf20Sopenharmony_ci int ret; 2948c2ecf20Sopenharmony_ci 2958c2ecf20Sopenharmony_ci crypto_stats_get(calg); 2968c2ecf20Sopenharmony_ci ret = alg->encrypt(req); 2978c2ecf20Sopenharmony_ci crypto_stats_akcipher_encrypt(src_len, ret, calg); 2988c2ecf20Sopenharmony_ci return ret; 2998c2ecf20Sopenharmony_ci} 3008c2ecf20Sopenharmony_ci 3018c2ecf20Sopenharmony_ci/** 3028c2ecf20Sopenharmony_ci * crypto_akcipher_decrypt() - Invoke public key decrypt operation 3038c2ecf20Sopenharmony_ci * 3048c2ecf20Sopenharmony_ci * Function invokes the specific public key decrypt operation for a given 3058c2ecf20Sopenharmony_ci * public key algorithm 3068c2ecf20Sopenharmony_ci * 3078c2ecf20Sopenharmony_ci * @req: asymmetric key request 3088c2ecf20Sopenharmony_ci * 3098c2ecf20Sopenharmony_ci * Return: zero on success; error code in case of error 3108c2ecf20Sopenharmony_ci */ 3118c2ecf20Sopenharmony_cistatic inline int crypto_akcipher_decrypt(struct akcipher_request *req) 3128c2ecf20Sopenharmony_ci{ 3138c2ecf20Sopenharmony_ci struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req); 3148c2ecf20Sopenharmony_ci struct akcipher_alg *alg = crypto_akcipher_alg(tfm); 3158c2ecf20Sopenharmony_ci struct crypto_alg *calg = tfm->base.__crt_alg; 3168c2ecf20Sopenharmony_ci unsigned int src_len = req->src_len; 3178c2ecf20Sopenharmony_ci int ret; 3188c2ecf20Sopenharmony_ci 3198c2ecf20Sopenharmony_ci crypto_stats_get(calg); 3208c2ecf20Sopenharmony_ci ret = alg->decrypt(req); 3218c2ecf20Sopenharmony_ci crypto_stats_akcipher_decrypt(src_len, ret, calg); 3228c2ecf20Sopenharmony_ci return ret; 3238c2ecf20Sopenharmony_ci} 3248c2ecf20Sopenharmony_ci 3258c2ecf20Sopenharmony_ci/** 3268c2ecf20Sopenharmony_ci * crypto_akcipher_sign() - Invoke public key sign operation 3278c2ecf20Sopenharmony_ci * 3288c2ecf20Sopenharmony_ci * Function invokes the specific public key sign operation for a given 3298c2ecf20Sopenharmony_ci * public key algorithm 3308c2ecf20Sopenharmony_ci * 3318c2ecf20Sopenharmony_ci * @req: asymmetric key request 3328c2ecf20Sopenharmony_ci * 3338c2ecf20Sopenharmony_ci * Return: zero on success; error code in case of error 3348c2ecf20Sopenharmony_ci */ 3358c2ecf20Sopenharmony_cistatic inline int crypto_akcipher_sign(struct akcipher_request *req) 3368c2ecf20Sopenharmony_ci{ 3378c2ecf20Sopenharmony_ci struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req); 3388c2ecf20Sopenharmony_ci struct akcipher_alg *alg = crypto_akcipher_alg(tfm); 3398c2ecf20Sopenharmony_ci struct crypto_alg *calg = tfm->base.__crt_alg; 3408c2ecf20Sopenharmony_ci int ret; 3418c2ecf20Sopenharmony_ci 3428c2ecf20Sopenharmony_ci crypto_stats_get(calg); 3438c2ecf20Sopenharmony_ci ret = alg->sign(req); 3448c2ecf20Sopenharmony_ci crypto_stats_akcipher_sign(ret, calg); 3458c2ecf20Sopenharmony_ci return ret; 3468c2ecf20Sopenharmony_ci} 3478c2ecf20Sopenharmony_ci 3488c2ecf20Sopenharmony_ci/** 3498c2ecf20Sopenharmony_ci * crypto_akcipher_verify() - Invoke public key signature verification 3508c2ecf20Sopenharmony_ci * 3518c2ecf20Sopenharmony_ci * Function invokes the specific public key signature verification operation 3528c2ecf20Sopenharmony_ci * for a given public key algorithm. 3538c2ecf20Sopenharmony_ci * 3548c2ecf20Sopenharmony_ci * @req: asymmetric key request 3558c2ecf20Sopenharmony_ci * 3568c2ecf20Sopenharmony_ci * Note: req->dst should be NULL, req->src should point to SG of size 3578c2ecf20Sopenharmony_ci * (req->src_size + req->dst_size), containing signature (of req->src_size 3588c2ecf20Sopenharmony_ci * length) with appended digest (of req->dst_size length). 3598c2ecf20Sopenharmony_ci * 3608c2ecf20Sopenharmony_ci * Return: zero on verification success; error code in case of error. 3618c2ecf20Sopenharmony_ci */ 3628c2ecf20Sopenharmony_cistatic inline int crypto_akcipher_verify(struct akcipher_request *req) 3638c2ecf20Sopenharmony_ci{ 3648c2ecf20Sopenharmony_ci struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req); 3658c2ecf20Sopenharmony_ci struct akcipher_alg *alg = crypto_akcipher_alg(tfm); 3668c2ecf20Sopenharmony_ci struct crypto_alg *calg = tfm->base.__crt_alg; 3678c2ecf20Sopenharmony_ci int ret; 3688c2ecf20Sopenharmony_ci 3698c2ecf20Sopenharmony_ci crypto_stats_get(calg); 3708c2ecf20Sopenharmony_ci ret = alg->verify(req); 3718c2ecf20Sopenharmony_ci crypto_stats_akcipher_verify(ret, calg); 3728c2ecf20Sopenharmony_ci return ret; 3738c2ecf20Sopenharmony_ci} 3748c2ecf20Sopenharmony_ci 3758c2ecf20Sopenharmony_ci/** 3768c2ecf20Sopenharmony_ci * crypto_akcipher_set_pub_key() - Invoke set public key operation 3778c2ecf20Sopenharmony_ci * 3788c2ecf20Sopenharmony_ci * Function invokes the algorithm specific set key function, which knows 3798c2ecf20Sopenharmony_ci * how to decode and interpret the encoded key and parameters 3808c2ecf20Sopenharmony_ci * 3818c2ecf20Sopenharmony_ci * @tfm: tfm handle 3828c2ecf20Sopenharmony_ci * @key: BER encoded public key, algo OID, paramlen, BER encoded 3838c2ecf20Sopenharmony_ci * parameters 3848c2ecf20Sopenharmony_ci * @keylen: length of the key (not including other data) 3858c2ecf20Sopenharmony_ci * 3868c2ecf20Sopenharmony_ci * Return: zero on success; error code in case of error 3878c2ecf20Sopenharmony_ci */ 3888c2ecf20Sopenharmony_cistatic inline int crypto_akcipher_set_pub_key(struct crypto_akcipher *tfm, 3898c2ecf20Sopenharmony_ci const void *key, 3908c2ecf20Sopenharmony_ci unsigned int keylen) 3918c2ecf20Sopenharmony_ci{ 3928c2ecf20Sopenharmony_ci struct akcipher_alg *alg = crypto_akcipher_alg(tfm); 3938c2ecf20Sopenharmony_ci 3948c2ecf20Sopenharmony_ci return alg->set_pub_key(tfm, key, keylen); 3958c2ecf20Sopenharmony_ci} 3968c2ecf20Sopenharmony_ci 3978c2ecf20Sopenharmony_ci/** 3988c2ecf20Sopenharmony_ci * crypto_akcipher_set_priv_key() - Invoke set private key operation 3998c2ecf20Sopenharmony_ci * 4008c2ecf20Sopenharmony_ci * Function invokes the algorithm specific set key function, which knows 4018c2ecf20Sopenharmony_ci * how to decode and interpret the encoded key and parameters 4028c2ecf20Sopenharmony_ci * 4038c2ecf20Sopenharmony_ci * @tfm: tfm handle 4048c2ecf20Sopenharmony_ci * @key: BER encoded private key, algo OID, paramlen, BER encoded 4058c2ecf20Sopenharmony_ci * parameters 4068c2ecf20Sopenharmony_ci * @keylen: length of the key (not including other data) 4078c2ecf20Sopenharmony_ci * 4088c2ecf20Sopenharmony_ci * Return: zero on success; error code in case of error 4098c2ecf20Sopenharmony_ci */ 4108c2ecf20Sopenharmony_cistatic inline int crypto_akcipher_set_priv_key(struct crypto_akcipher *tfm, 4118c2ecf20Sopenharmony_ci const void *key, 4128c2ecf20Sopenharmony_ci unsigned int keylen) 4138c2ecf20Sopenharmony_ci{ 4148c2ecf20Sopenharmony_ci struct akcipher_alg *alg = crypto_akcipher_alg(tfm); 4158c2ecf20Sopenharmony_ci 4168c2ecf20Sopenharmony_ci return alg->set_priv_key(tfm, key, keylen); 4178c2ecf20Sopenharmony_ci} 4188c2ecf20Sopenharmony_ci#endif 419