Blame view
crypto/rsa_helper.c
4.03 KB
2874c5fd2 treewide: Replace... |
1 |
// SPDX-License-Identifier: GPL-2.0-or-later |
cfc2bb32b crypto: rsa - add... |
2 3 4 5 6 |
/* * RSA key extract helper * * Copyright (c) 2015, Intel Corporation * Authors: Tadeusz Struk <tadeusz.struk@intel.com> |
cfc2bb32b crypto: rsa - add... |
7 8 9 10 11 12 |
*/ #include <linux/kernel.h> #include <linux/export.h> #include <linux/err.h> #include <linux/fips.h> #include <crypto/internal/rsa.h> |
4fa8bc949 kbuild: rename *-... |
13 14 |
#include "rsapubkey.asn1.h" #include "rsaprivkey.asn1.h" |
cfc2bb32b crypto: rsa - add... |
15 16 17 18 19 |
int rsa_get_n(void *context, size_t hdrlen, unsigned char tag, const void *value, size_t vlen) { struct rsa_key *key = context; |
5a7de9730 crypto: rsa - ret... |
20 21 |
const u8 *ptr = value; size_t n_sz = vlen; |
cfc2bb32b crypto: rsa - add... |
22 |
|
5a7de9730 crypto: rsa - ret... |
23 24 |
/* invalid key provided */ if (!value || !vlen) |
cfc2bb32b crypto: rsa - add... |
25 |
return -EINVAL; |
5a7de9730 crypto: rsa - ret... |
26 27 |
if (fips_enabled) { |
d2890c377 crypto: rsa - fix... |
28 |
while (n_sz && !*ptr) { |
5a7de9730 crypto: rsa - ret... |
29 30 31 |
ptr++; n_sz--; } |
e09287dfe crypto: rsa - all... |
32 33 |
/* In FIPS mode only allow key size 2K and higher */ if (n_sz < 256) { |
5a7de9730 crypto: rsa - ret... |
34 35 36 37 |
pr_err("RSA: key size not allowed in FIPS mode "); return -EINVAL; } |
cfc2bb32b crypto: rsa - add... |
38 |
} |
5a7de9730 crypto: rsa - ret... |
39 40 41 |
key->n = value; key->n_sz = vlen; |
cfc2bb32b crypto: rsa - add... |
42 43 44 45 46 47 48 |
return 0; } int rsa_get_e(void *context, size_t hdrlen, unsigned char tag, const void *value, size_t vlen) { struct rsa_key *key = context; |
5a7de9730 crypto: rsa - ret... |
49 50 51 |
/* invalid key provided */ if (!value || !key->n_sz || !vlen || vlen > key->n_sz) return -EINVAL; |
cfc2bb32b crypto: rsa - add... |
52 |
|
5a7de9730 crypto: rsa - ret... |
53 54 |
key->e = value; key->e_sz = vlen; |
cfc2bb32b crypto: rsa - add... |
55 56 57 58 59 60 61 62 |
return 0; } int rsa_get_d(void *context, size_t hdrlen, unsigned char tag, const void *value, size_t vlen) { struct rsa_key *key = context; |
5a7de9730 crypto: rsa - ret... |
63 64 |
/* invalid key provided */ if (!value || !key->n_sz || !vlen || vlen > key->n_sz) |
cfc2bb32b crypto: rsa - add... |
65 |
return -EINVAL; |
cfc2bb32b crypto: rsa - add... |
66 |
|
5a7de9730 crypto: rsa - ret... |
67 68 |
key->d = value; key->d_sz = vlen; |
cfc2bb32b crypto: rsa - add... |
69 |
|
5a7de9730 crypto: rsa - ret... |
70 |
return 0; |
cfc2bb32b crypto: rsa - add... |
71 |
} |
cfc2bb32b crypto: rsa - add... |
72 |
|
8be0b84e5 crypto: rsa - Sto... |
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
int rsa_get_p(void *context, size_t hdrlen, unsigned char tag, const void *value, size_t vlen) { struct rsa_key *key = context; /* invalid key provided */ if (!value || !vlen || vlen > key->n_sz) return -EINVAL; key->p = value; key->p_sz = vlen; return 0; } int rsa_get_q(void *context, size_t hdrlen, unsigned char tag, const void *value, size_t vlen) { struct rsa_key *key = context; /* invalid key provided */ if (!value || !vlen || vlen > key->n_sz) return -EINVAL; key->q = value; key->q_sz = vlen; return 0; } int rsa_get_dp(void *context, size_t hdrlen, unsigned char tag, const void *value, size_t vlen) { struct rsa_key *key = context; /* invalid key provided */ if (!value || !vlen || vlen > key->n_sz) return -EINVAL; key->dp = value; key->dp_sz = vlen; return 0; } int rsa_get_dq(void *context, size_t hdrlen, unsigned char tag, const void *value, size_t vlen) { struct rsa_key *key = context; /* invalid key provided */ if (!value || !vlen || vlen > key->n_sz) return -EINVAL; key->dq = value; key->dq_sz = vlen; return 0; } int rsa_get_qinv(void *context, size_t hdrlen, unsigned char tag, const void *value, size_t vlen) { struct rsa_key *key = context; /* invalid key provided */ if (!value || !vlen || vlen > key->n_sz) return -EINVAL; key->qinv = value; key->qinv_sz = vlen; return 0; } |
cfc2bb32b crypto: rsa - add... |
147 |
/** |
5a7de9730 crypto: rsa - ret... |
148 149 150 |
* rsa_parse_pub_key() - decodes the BER encoded buffer and stores in the * provided struct rsa_key, pointers to the raw key as is, * so that the caller can copy it or MPI parse it, etc. |
cfc2bb32b crypto: rsa - add... |
151 152 153 154 155 156 157 |
* * @rsa_key: struct rsa_key key representation * @key: key in BER format * @key_len: length of key * * Return: 0 on success or error code in case of error */ |
22287b0b5 crypto: akcipher ... |
158 159 |
int rsa_parse_pub_key(struct rsa_key *rsa_key, const void *key, unsigned int key_len) |
cfc2bb32b crypto: rsa - add... |
160 |
{ |
5a7de9730 crypto: rsa - ret... |
161 |
return asn1_ber_decoder(&rsapubkey_decoder, rsa_key, key, key_len); |
cfc2bb32b crypto: rsa - add... |
162 |
} |
22287b0b5 crypto: akcipher ... |
163 164 165 |
EXPORT_SYMBOL_GPL(rsa_parse_pub_key); /** |
5a7de9730 crypto: rsa - ret... |
166 167 168 169 |
* rsa_parse_priv_key() - decodes the BER encoded buffer and stores in the * provided struct rsa_key, pointers to the raw key * as is, so that the caller can copy it or MPI parse it, * etc. |
22287b0b5 crypto: akcipher ... |
170 171 172 173 174 175 176 177 178 179 |
* * @rsa_key: struct rsa_key key representation * @key: key in BER format * @key_len: length of key * * Return: 0 on success or error code in case of error */ int rsa_parse_priv_key(struct rsa_key *rsa_key, const void *key, unsigned int key_len) { |
5a7de9730 crypto: rsa - ret... |
180 |
return asn1_ber_decoder(&rsaprivkey_decoder, rsa_key, key, key_len); |
22287b0b5 crypto: akcipher ... |
181 182 |
} EXPORT_SYMBOL_GPL(rsa_parse_priv_key); |