Blame view
crypto/rsa_helper.c
4.25 KB
cfc2bb32b crypto: rsa - add... |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
/* * RSA key extract helper * * Copyright (c) 2015, Intel Corporation * Authors: Tadeusz Struk <tadeusz.struk@intel.com> * * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the Free * Software Foundation; either version 2 of the License, or (at your option) * any later version. * */ #include <linux/kernel.h> #include <linux/export.h> #include <linux/err.h> #include <linux/fips.h> #include <crypto/internal/rsa.h> |
22287b0b5 crypto: akcipher ... |
18 19 |
#include "rsapubkey-asn1.h" #include "rsaprivkey-asn1.h" |
cfc2bb32b crypto: rsa - add... |
20 21 22 23 24 |
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... |
25 26 |
const u8 *ptr = value; size_t n_sz = vlen; |
cfc2bb32b crypto: rsa - add... |
27 |
|
5a7de9730 crypto: rsa - ret... |
28 29 |
/* invalid key provided */ if (!value || !vlen) |
cfc2bb32b crypto: rsa - add... |
30 |
return -EINVAL; |
5a7de9730 crypto: rsa - ret... |
31 32 33 34 35 36 37 38 39 40 41 42 43 |
if (fips_enabled) { while (!*ptr && n_sz) { ptr++; n_sz--; } /* In FIPS mode only allow key size 2K & 3K */ if (n_sz != 256 && n_sz != 384) { pr_err("RSA: key size not allowed in FIPS mode "); return -EINVAL; } |
cfc2bb32b crypto: rsa - add... |
44 |
} |
5a7de9730 crypto: rsa - ret... |
45 46 47 |
key->n = value; key->n_sz = vlen; |
cfc2bb32b crypto: rsa - add... |
48 49 50 51 52 53 54 |
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... |
55 56 57 |
/* invalid key provided */ if (!value || !key->n_sz || !vlen || vlen > key->n_sz) return -EINVAL; |
cfc2bb32b crypto: rsa - add... |
58 |
|
5a7de9730 crypto: rsa - ret... |
59 60 |
key->e = value; key->e_sz = vlen; |
cfc2bb32b crypto: rsa - add... |
61 62 63 64 65 66 67 68 |
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... |
69 70 |
/* invalid key provided */ if (!value || !key->n_sz || !vlen || vlen > key->n_sz) |
cfc2bb32b crypto: rsa - add... |
71 |
return -EINVAL; |
cfc2bb32b crypto: rsa - add... |
72 |
|
5a7de9730 crypto: rsa - ret... |
73 74 |
key->d = value; key->d_sz = vlen; |
cfc2bb32b crypto: rsa - add... |
75 |
|
5a7de9730 crypto: rsa - ret... |
76 |
return 0; |
cfc2bb32b crypto: rsa - add... |
77 |
} |
cfc2bb32b crypto: rsa - add... |
78 |
|
8be0b84e5 crypto: rsa - Sto... |
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 147 148 149 150 151 152 |
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... |
153 |
/** |
5a7de9730 crypto: rsa - ret... |
154 155 156 |
* 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... |
157 158 159 160 161 162 163 |
* * @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 ... |
164 165 |
int rsa_parse_pub_key(struct rsa_key *rsa_key, const void *key, unsigned int key_len) |
cfc2bb32b crypto: rsa - add... |
166 |
{ |
5a7de9730 crypto: rsa - ret... |
167 |
return asn1_ber_decoder(&rsapubkey_decoder, rsa_key, key, key_len); |
cfc2bb32b crypto: rsa - add... |
168 |
} |
22287b0b5 crypto: akcipher ... |
169 170 171 |
EXPORT_SYMBOL_GPL(rsa_parse_pub_key); /** |
5a7de9730 crypto: rsa - ret... |
172 173 174 175 |
* 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 ... |
176 177 178 179 180 181 182 183 184 185 |
* * @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... |
186 |
return asn1_ber_decoder(&rsaprivkey_decoder, rsa_key, key, key_len); |
22287b0b5 crypto: akcipher ... |
187 188 |
} EXPORT_SYMBOL_GPL(rsa_parse_priv_key); |