]>
git.ipfire.org Git - thirdparty/u-boot.git/blob - lib/crypto/rsa_helper.c
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/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 <linux/printk.h>
19 #include "rsapubkey.asn1.h"
21 #include "rsaprivkey.asn1.h"
24 int rsa_get_n(void *context
, size_t hdrlen
, unsigned char tag
,
25 const void *value
, size_t vlen
)
27 struct rsa_key
*key
= context
;
29 const u8
*ptr
= value
;
33 /* invalid key provided */
39 while (n_sz
&& !*ptr
) {
44 /* In FIPS mode only allow key size 2K and higher */
46 pr_err("RSA: key size not allowed in FIPS mode\n");
58 int rsa_get_e(void *context
, size_t hdrlen
, unsigned char tag
,
59 const void *value
, size_t vlen
)
61 struct rsa_key
*key
= context
;
63 /* invalid key provided */
64 if (!value
|| !key
->n_sz
|| !vlen
|| vlen
> key
->n_sz
)
73 int rsa_get_d(void *context
, size_t hdrlen
, unsigned char tag
,
74 const void *value
, size_t vlen
)
76 struct rsa_key
*key
= context
;
78 /* invalid key provided */
79 if (!value
|| !key
->n_sz
|| !vlen
|| vlen
> key
->n_sz
)
88 int rsa_get_p(void *context
, size_t hdrlen
, unsigned char tag
,
89 const void *value
, size_t vlen
)
91 struct rsa_key
*key
= context
;
93 /* invalid key provided */
94 if (!value
|| !vlen
|| vlen
> key
->n_sz
)
103 int rsa_get_q(void *context
, size_t hdrlen
, unsigned char tag
,
104 const void *value
, size_t vlen
)
106 struct rsa_key
*key
= context
;
108 /* invalid key provided */
109 if (!value
|| !vlen
|| vlen
> key
->n_sz
)
118 int rsa_get_dp(void *context
, size_t hdrlen
, unsigned char tag
,
119 const void *value
, size_t vlen
)
121 struct rsa_key
*key
= context
;
123 /* invalid key provided */
124 if (!value
|| !vlen
|| vlen
> key
->n_sz
)
133 int rsa_get_dq(void *context
, size_t hdrlen
, unsigned char tag
,
134 const void *value
, size_t vlen
)
136 struct rsa_key
*key
= context
;
138 /* invalid key provided */
139 if (!value
|| !vlen
|| vlen
> key
->n_sz
)
148 int rsa_get_qinv(void *context
, size_t hdrlen
, unsigned char tag
,
149 const void *value
, size_t vlen
)
151 struct rsa_key
*key
= context
;
153 /* invalid key provided */
154 if (!value
|| !vlen
|| vlen
> key
->n_sz
)
164 * rsa_parse_pub_key() - decodes the BER encoded buffer and stores in the
165 * provided struct rsa_key, pointers to the raw key as is,
166 * so that the caller can copy it or MPI parse it, etc.
168 * @rsa_key: struct rsa_key key representation
169 * @key: key in BER format
170 * @key_len: length of key
172 * Return: 0 on success or error code in case of error
174 int rsa_parse_pub_key(struct rsa_key
*rsa_key
, const void *key
,
175 unsigned int key_len
)
177 return asn1_ber_decoder(&rsapubkey_decoder
, rsa_key
, key
, key_len
);
179 EXPORT_SYMBOL_GPL(rsa_parse_pub_key
);
183 * rsa_parse_priv_key() - decodes the BER encoded buffer and stores in the
184 * provided struct rsa_key, pointers to the raw key
185 * as is, so that the caller can copy it or MPI parse it,
188 * @rsa_key: struct rsa_key key representation
189 * @key: key in BER format
190 * @key_len: length of key
192 * Return: 0 on success or error code in case of error
194 int rsa_parse_priv_key(struct rsa_key
*rsa_key
, const void *key
,
195 unsigned int key_len
)
197 return asn1_ber_decoder(&rsaprivkey_decoder
, rsa_key
, key
, key_len
);
199 EXPORT_SYMBOL_GPL(rsa_parse_priv_key
);