1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * RSA key extract helper
5 * Copyright (c) 2015, Intel Corporation
6 * Authors: Tadeusz Struk <tadeusz.struk@intel.com>
9 #include <linux/kernel.h>
10 #include <linux/export.h>
12 #include <linux/err.h>
14 #include <linux/fips.h>
16 #include <crypto/internal/rsa.h>
17 #include "rsapubkey.asn1.h"
19 #include "rsaprivkey.asn1.h"
22 int rsa_get_n(void *context, size_t hdrlen, unsigned char tag,
23 const void *value, size_t vlen)
25 struct rsa_key *key = context;
27 const u8 *ptr = value;
31 /* invalid key provided */
37 while (n_sz && !*ptr) {
42 /* In FIPS mode only allow key size 2K and higher */
44 pr_err("RSA: key size not allowed in FIPS mode\n");
56 int rsa_get_e(void *context, size_t hdrlen, unsigned char tag,
57 const void *value, size_t vlen)
59 struct rsa_key *key = context;
61 /* invalid key provided */
62 if (!value || !key->n_sz || !vlen || vlen > key->n_sz)
71 int rsa_get_d(void *context, size_t hdrlen, unsigned char tag,
72 const void *value, size_t vlen)
74 struct rsa_key *key = context;
76 /* invalid key provided */
77 if (!value || !key->n_sz || !vlen || vlen > key->n_sz)
86 int rsa_get_p(void *context, size_t hdrlen, unsigned char tag,
87 const void *value, size_t vlen)
89 struct rsa_key *key = context;
91 /* invalid key provided */
92 if (!value || !vlen || vlen > key->n_sz)
101 int rsa_get_q(void *context, size_t hdrlen, unsigned char tag,
102 const void *value, size_t vlen)
104 struct rsa_key *key = context;
106 /* invalid key provided */
107 if (!value || !vlen || vlen > key->n_sz)
116 int rsa_get_dp(void *context, size_t hdrlen, unsigned char tag,
117 const void *value, size_t vlen)
119 struct rsa_key *key = context;
121 /* invalid key provided */
122 if (!value || !vlen || vlen > key->n_sz)
131 int rsa_get_dq(void *context, size_t hdrlen, unsigned char tag,
132 const void *value, size_t vlen)
134 struct rsa_key *key = context;
136 /* invalid key provided */
137 if (!value || !vlen || vlen > key->n_sz)
146 int rsa_get_qinv(void *context, size_t hdrlen, unsigned char tag,
147 const void *value, size_t vlen)
149 struct rsa_key *key = context;
151 /* invalid key provided */
152 if (!value || !vlen || vlen > key->n_sz)
162 * rsa_parse_pub_key() - decodes the BER encoded buffer and stores in the
163 * provided struct rsa_key, pointers to the raw key as is,
164 * so that the caller can copy it or MPI parse it, etc.
166 * @rsa_key: struct rsa_key key representation
167 * @key: key in BER format
168 * @key_len: length of key
170 * Return: 0 on success or error code in case of error
172 int rsa_parse_pub_key(struct rsa_key *rsa_key, const void *key,
173 unsigned int key_len)
175 return asn1_ber_decoder(&rsapubkey_decoder, rsa_key, key, key_len);
177 EXPORT_SYMBOL_GPL(rsa_parse_pub_key);
181 * rsa_parse_priv_key() - decodes the BER encoded buffer and stores in the
182 * provided struct rsa_key, pointers to the raw key
183 * as is, so that the caller can copy it or MPI parse it,
186 * @rsa_key: struct rsa_key key representation
187 * @key: key in BER format
188 * @key_len: length of key
190 * Return: 0 on success or error code in case of error
192 int rsa_parse_priv_key(struct rsa_key *rsa_key, const void *key,
193 unsigned int key_len)
195 return asn1_ber_decoder(&rsaprivkey_decoder, rsa_key, key, key_len);
197 EXPORT_SYMBOL_GPL(rsa_parse_priv_key);