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;
26 key->n = mpi_read_raw_data(value, vlen);
31 /* In FIPS mode only allow key size 2K & 3K */
32 if (fips_enabled && (mpi_get_size(key->n) != 256 &&
33 mpi_get_size(key->n) != 384)) {
34 pr_err("RSA: key size not allowed in FIPS mode\n");
42 int rsa_get_e(void *context, size_t hdrlen, unsigned char tag,
43 const void *value, size_t vlen)
45 struct rsa_key *key = context;
47 key->e = mpi_read_raw_data(value, vlen);
55 int rsa_get_d(void *context, size_t hdrlen, unsigned char tag,
56 const void *value, size_t vlen)
58 struct rsa_key *key = context;
60 key->d = mpi_read_raw_data(value, vlen);
65 /* In FIPS mode only allow key size 2K & 3K */
66 if (fips_enabled && (mpi_get_size(key->d) != 256 &&
67 mpi_get_size(key->d) != 384)) {
68 pr_err("RSA: key size not allowed in FIPS mode\n");
76 static void free_mpis(struct rsa_key *key)
87 * rsa_free_key() - frees rsa key allocated by rsa_parse_key()
89 * @rsa_key: struct rsa_key key representation
91 void rsa_free_key(struct rsa_key *key)
95 EXPORT_SYMBOL_GPL(rsa_free_key);
98 * rsa_parse_pub_key() - extracts an rsa public key from BER encoded buffer
99 * and stores it in the provided struct rsa_key
101 * @rsa_key: struct rsa_key key representation
102 * @key: key in BER format
103 * @key_len: length of key
105 * Return: 0 on success or error code in case of error
107 int rsa_parse_pub_key(struct rsa_key *rsa_key, const void *key,
108 unsigned int key_len)
113 ret = asn1_ber_decoder(&rsapubkey_decoder, rsa_key, key, key_len);
122 EXPORT_SYMBOL_GPL(rsa_parse_pub_key);
125 * rsa_parse_pub_key() - extracts an rsa private key from BER encoded buffer
126 * and stores it in the provided struct rsa_key
128 * @rsa_key: struct rsa_key key representation
129 * @key: key in BER format
130 * @key_len: length of key
132 * Return: 0 on success or error code in case of error
134 int rsa_parse_priv_key(struct rsa_key *rsa_key, const void *key,
135 unsigned int key_len)
140 ret = asn1_ber_decoder(&rsaprivkey_decoder, rsa_key, key, key_len);
149 EXPORT_SYMBOL_GPL(rsa_parse_priv_key);