1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * RSA key extract helper
5 * Copyright (c) 2015, Intel Corporation
9 #include <linux/compat.h>
10 #include <linux/kernel.h>
11 #include <linux/export.h>
13 #include <linux/err.h>
15 #include <linux/fips.h>
17 #include <crypto/internal/rsa.h>
18 #include "rsapubkey.asn1.h"
20 #include "rsaprivkey.asn1.h"
23 int rsa_get_n(void *context, size_t hdrlen, unsigned char tag,
24 const void *value, size_t vlen)
26 struct rsa_key *key = context;
28 const u8 *ptr = value;
32 /* invalid key provided */
38 while (n_sz && !*ptr) {
43 /* In FIPS mode only allow key size 2K and higher */
45 pr_err("RSA: key size not allowed in FIPS mode\n");
57 int rsa_get_e(void *context, size_t hdrlen, unsigned char tag,
58 const void *value, size_t vlen)
60 struct rsa_key *key = context;
62 /* invalid key provided */
63 if (!value || !key->n_sz || !vlen || vlen > key->n_sz)
72 int rsa_get_d(void *context, size_t hdrlen, unsigned char tag,
73 const void *value, size_t vlen)
75 struct rsa_key *key = context;
77 /* invalid key provided */
78 if (!value || !key->n_sz || !vlen || vlen > key->n_sz)
87 int rsa_get_p(void *context, size_t hdrlen, unsigned char tag,
88 const void *value, size_t vlen)
90 struct rsa_key *key = context;
92 /* invalid key provided */
93 if (!value || !vlen || vlen > key->n_sz)
102 int rsa_get_q(void *context, size_t hdrlen, unsigned char tag,
103 const void *value, size_t vlen)
105 struct rsa_key *key = context;
107 /* invalid key provided */
108 if (!value || !vlen || vlen > key->n_sz)
117 int rsa_get_dp(void *context, size_t hdrlen, unsigned char tag,
118 const void *value, size_t vlen)
120 struct rsa_key *key = context;
122 /* invalid key provided */
123 if (!value || !vlen || vlen > key->n_sz)
132 int rsa_get_dq(void *context, size_t hdrlen, unsigned char tag,
133 const void *value, size_t vlen)
135 struct rsa_key *key = context;
137 /* invalid key provided */
138 if (!value || !vlen || vlen > key->n_sz)
147 int rsa_get_qinv(void *context, size_t hdrlen, unsigned char tag,
148 const void *value, size_t vlen)
150 struct rsa_key *key = context;
152 /* invalid key provided */
153 if (!value || !vlen || vlen > key->n_sz)
163 * rsa_parse_pub_key() - decodes the BER encoded buffer and stores in the
164 * provided struct rsa_key, pointers to the raw key as is,
165 * so that the caller can copy it or MPI parse it, etc.
167 * @rsa_key: struct rsa_key key representation
168 * @key: key in BER format
169 * @key_len: length of key
171 * Return: 0 on success or error code in case of error
173 int rsa_parse_pub_key(struct rsa_key *rsa_key, const void *key,
174 unsigned int key_len)
176 return asn1_ber_decoder(&rsapubkey_decoder, rsa_key, key, key_len);
178 EXPORT_SYMBOL_GPL(rsa_parse_pub_key);
182 * rsa_parse_priv_key() - decodes the BER encoded buffer and stores in the
183 * provided struct rsa_key, pointers to the raw key
184 * as is, so that the caller can copy it or MPI parse it,
187 * @rsa_key: struct rsa_key key representation
188 * @key: key in BER format
189 * @key_len: length of key
191 * Return: 0 on success or error code in case of error
193 int rsa_parse_priv_key(struct rsa_key *rsa_key, const void *key,
194 unsigned int key_len)
196 return asn1_ber_decoder(&rsaprivkey_decoder, rsa_key, key, key_len);
198 EXPORT_SYMBOL_GPL(rsa_parse_priv_key);