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