/* SPDX-License-Identifier: GPL-2.0-or-later */1/*2* RSA internal helpers3*4* Copyright (c) 2015, Intel Corporation5* Authors: Tadeusz Struk <[email protected]>6*/7#ifndef _RSA_HELPER_8#define _RSA_HELPER_9#include <linux/types.h>10#include <crypto/akcipher.h>1112/**13* rsa_key - RSA key structure14* @n : RSA modulus raw byte stream15* @e : RSA public exponent raw byte stream16* @d : RSA private exponent raw byte stream17* @p : RSA prime factor p of n raw byte stream18* @q : RSA prime factor q of n raw byte stream19* @dp : RSA exponent d mod (p - 1) raw byte stream20* @dq : RSA exponent d mod (q - 1) raw byte stream21* @qinv : RSA CRT coefficient q^(-1) mod p raw byte stream22* @n_sz : length in bytes of RSA modulus n23* @e_sz : length in bytes of RSA public exponent24* @d_sz : length in bytes of RSA private exponent25* @p_sz : length in bytes of p field26* @q_sz : length in bytes of q field27* @dp_sz : length in bytes of dp field28* @dq_sz : length in bytes of dq field29* @qinv_sz : length in bytes of qinv field30*/31struct rsa_key {32const u8 *n;33const u8 *e;34const u8 *d;35const u8 *p;36const u8 *q;37const u8 *dp;38const u8 *dq;39const u8 *qinv;40size_t n_sz;41size_t e_sz;42size_t d_sz;43size_t p_sz;44size_t q_sz;45size_t dp_sz;46size_t dq_sz;47size_t qinv_sz;48};4950int rsa_parse_pub_key(struct rsa_key *rsa_key, const void *key,51unsigned int key_len);5253int rsa_parse_priv_key(struct rsa_key *rsa_key, const void *key,54unsigned int key_len);5556#define RSA_PUB (true)57#define RSA_PRIV (false)5859static inline int rsa_set_key(struct crypto_akcipher *child,60unsigned int *key_size, bool is_pubkey,61const void *key, unsigned int keylen)62{63int err;6465*key_size = 0;6667if (is_pubkey)68err = crypto_akcipher_set_pub_key(child, key, keylen);69else70err = crypto_akcipher_set_priv_key(child, key, keylen);71if (err)72return err;7374/* Find out new modulus size from rsa implementation */75err = crypto_akcipher_maxsize(child);76if (err > PAGE_SIZE)77return -ENOTSUPP;7879*key_size = err;80return 0;81}8283extern struct crypto_template rsa_pkcs1pad_tmpl;84extern struct crypto_template rsassa_pkcs1_tmpl;85#endif868788