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>
8 #include <linux/kernel.h>
9 #include <linux/export.h>
10 #include <linux/err.h>
11 #include <linux/fips.h>
12 #include <crypto/internal/rsa.h>
13 #include "rsapubkey.asn1.h"
14 #include "rsaprivkey.asn1.h"
16 int rsa_get_n(void *context, size_t hdrlen, unsigned char tag,
17 const void *value, size_t vlen)
19 struct rsa_key *key = context;
20 const u8 *ptr = value;
23 /* invalid key provided */
28 while (n_sz && !*ptr) {
33 /* In FIPS mode only allow key size 2K and higher */
35 pr_err("RSA: key size not allowed in FIPS mode\n");
46 int rsa_get_e(void *context, size_t hdrlen, unsigned char tag,
47 const void *value, size_t vlen)
49 struct rsa_key *key = context;
51 /* invalid key provided */
52 if (!value || !key->n_sz || !vlen || vlen > key->n_sz)
61 int rsa_get_d(void *context, size_t hdrlen, unsigned char tag,
62 const void *value, size_t vlen)
64 struct rsa_key *key = context;
66 /* invalid key provided */
67 if (!value || !key->n_sz || !vlen || vlen > key->n_sz)
76 int rsa_get_p(void *context, size_t hdrlen, unsigned char tag,
77 const void *value, size_t vlen)
79 struct rsa_key *key = context;
81 /* invalid key provided */
82 if (!value || !vlen || vlen > key->n_sz)
91 int rsa_get_q(void *context, size_t hdrlen, unsigned char tag,
92 const void *value, size_t vlen)
94 struct rsa_key *key = context;
96 /* invalid key provided */
97 if (!value || !vlen || vlen > key->n_sz)
106 int rsa_get_dp(void *context, size_t hdrlen, unsigned char tag,
107 const void *value, size_t vlen)
109 struct rsa_key *key = context;
111 /* invalid key provided */
112 if (!value || !vlen || vlen > key->n_sz)
121 int rsa_get_dq(void *context, size_t hdrlen, unsigned char tag,
122 const void *value, size_t vlen)
124 struct rsa_key *key = context;
126 /* invalid key provided */
127 if (!value || !vlen || vlen > key->n_sz)
136 int rsa_get_qinv(void *context, size_t hdrlen, unsigned char tag,
137 const void *value, size_t vlen)
139 struct rsa_key *key = context;
141 /* invalid key provided */
142 if (!value || !vlen || vlen > key->n_sz)
152 * rsa_parse_pub_key() - decodes the BER encoded buffer and stores in the
153 * provided struct rsa_key, pointers to the raw key as is,
154 * so that the caller can copy it or MPI parse it, etc.
156 * @rsa_key: struct rsa_key key representation
157 * @key: key in BER format
158 * @key_len: length of key
160 * Return: 0 on success or error code in case of error
162 int rsa_parse_pub_key(struct rsa_key *rsa_key, const void *key,
163 unsigned int key_len)
165 return asn1_ber_decoder(&rsapubkey_decoder, rsa_key, key, key_len);
167 EXPORT_SYMBOL_GPL(rsa_parse_pub_key);
170 * rsa_parse_priv_key() - decodes the BER encoded buffer and stores in the
171 * provided struct rsa_key, pointers to the raw key
172 * as is, so that the caller can copy it or MPI parse it,
175 * @rsa_key: struct rsa_key key representation
176 * @key: key in BER format
177 * @key_len: length of key
179 * Return: 0 on success or error code in case of error
181 int rsa_parse_priv_key(struct rsa_key *rsa_key, const void *key,
182 unsigned int key_len)
184 return asn1_ber_decoder(&rsaprivkey_decoder, rsa_key, key, key_len);
186 EXPORT_SYMBOL_GPL(rsa_parse_priv_key);