2 * RSA key extract helper
4 * Copyright (c) 2015, Intel Corporation
5 * Authors: Tadeusz Struk <tadeusz.struk@intel.com>
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the Free
9 * Software Foundation; either version 2 of the License, or (at your option)
13 #include <linux/kernel.h>
14 #include <linux/export.h>
15 #include <linux/err.h>
16 #include <linux/fips.h>
17 #include <crypto/internal/rsa.h>
18 #include "rsapubkey.asn1.h"
19 #include "rsaprivkey.asn1.h"
21 int rsa_get_n(void *context, size_t hdrlen, unsigned char tag,
22 const void *value, size_t vlen)
24 struct rsa_key *key = context;
25 const u8 *ptr = value;
28 /* invalid key provided */
33 while (n_sz && !*ptr) {
38 /* In FIPS mode only allow key size 2K and higher */
40 pr_err("RSA: key size not allowed in FIPS mode\n");
51 int rsa_get_e(void *context, size_t hdrlen, unsigned char tag,
52 const void *value, size_t vlen)
54 struct rsa_key *key = context;
56 /* invalid key provided */
57 if (!value || !key->n_sz || !vlen || vlen > key->n_sz)
66 int rsa_get_d(void *context, size_t hdrlen, unsigned char tag,
67 const void *value, size_t vlen)
69 struct rsa_key *key = context;
71 /* invalid key provided */
72 if (!value || !key->n_sz || !vlen || vlen > key->n_sz)
81 int rsa_get_p(void *context, size_t hdrlen, unsigned char tag,
82 const void *value, size_t vlen)
84 struct rsa_key *key = context;
86 /* invalid key provided */
87 if (!value || !vlen || vlen > key->n_sz)
96 int rsa_get_q(void *context, size_t hdrlen, unsigned char tag,
97 const void *value, size_t vlen)
99 struct rsa_key *key = context;
101 /* invalid key provided */
102 if (!value || !vlen || vlen > key->n_sz)
111 int rsa_get_dp(void *context, size_t hdrlen, unsigned char tag,
112 const void *value, size_t vlen)
114 struct rsa_key *key = context;
116 /* invalid key provided */
117 if (!value || !vlen || vlen > key->n_sz)
126 int rsa_get_dq(void *context, size_t hdrlen, unsigned char tag,
127 const void *value, size_t vlen)
129 struct rsa_key *key = context;
131 /* invalid key provided */
132 if (!value || !vlen || vlen > key->n_sz)
141 int rsa_get_qinv(void *context, size_t hdrlen, unsigned char tag,
142 const void *value, size_t vlen)
144 struct rsa_key *key = context;
146 /* invalid key provided */
147 if (!value || !vlen || vlen > key->n_sz)
157 * rsa_parse_pub_key() - decodes the BER encoded buffer and stores in the
158 * provided struct rsa_key, pointers to the raw key as is,
159 * so that the caller can copy it or MPI parse it, etc.
161 * @rsa_key: struct rsa_key key representation
162 * @key: key in BER format
163 * @key_len: length of key
165 * Return: 0 on success or error code in case of error
167 int rsa_parse_pub_key(struct rsa_key *rsa_key, const void *key,
168 unsigned int key_len)
170 return asn1_ber_decoder(&rsapubkey_decoder, rsa_key, key, key_len);
172 EXPORT_SYMBOL_GPL(rsa_parse_pub_key);
175 * rsa_parse_priv_key() - decodes the BER encoded buffer and stores in the
176 * provided struct rsa_key, pointers to the raw key
177 * as is, so that the caller can copy it or MPI parse it,
180 * @rsa_key: struct rsa_key key representation
181 * @key: key in BER format
182 * @key_len: length of key
184 * Return: 0 on success or error code in case of error
186 int rsa_parse_priv_key(struct rsa_key *rsa_key, const void *key,
187 unsigned int key_len)
189 return asn1_ber_decoder(&rsaprivkey_decoder, rsa_key, key, key_len);
191 EXPORT_SYMBOL_GPL(rsa_parse_priv_key);