blob: 583656af4fe288cfe886b9040814c84271dee8c2 [file] [log] [blame]
Tadeusz Strukcfc2bb32015-06-16 10:31:01 -07001/*
2 * RSA key extract helper
3 *
4 * Copyright (c) 2015, Intel Corporation
5 * Authors: Tadeusz Struk <tadeusz.struk@intel.com>
6 *
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)
10 * any later version.
11 *
12 */
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>
Tadeusz Struk22287b02015-10-08 09:26:55 -070018#include "rsapubkey-asn1.h"
19#include "rsaprivkey-asn1.h"
Tadeusz Strukcfc2bb32015-06-16 10:31:01 -070020
21int rsa_get_n(void *context, size_t hdrlen, unsigned char tag,
22 const void *value, size_t vlen)
23{
24 struct rsa_key *key = context;
Tudor Ambarus5a7de972016-06-14 16:14:58 +030025 const u8 *ptr = value;
26 size_t n_sz = vlen;
Tadeusz Strukcfc2bb32015-06-16 10:31:01 -070027
Tudor Ambarus5a7de972016-06-14 16:14:58 +030028 /* invalid key provided */
29 if (!value || !vlen)
Tadeusz Strukcfc2bb32015-06-16 10:31:01 -070030 return -EINVAL;
Tudor Ambarus5a7de972016-06-14 16:14:58 +030031
32 if (fips_enabled) {
33 while (!*ptr && n_sz) {
34 ptr++;
35 n_sz--;
36 }
37
38 /* In FIPS mode only allow key size 2K & 3K */
39 if (n_sz != 256 && n_sz != 384) {
40 pr_err("RSA: key size not allowed in FIPS mode\n");
41 return -EINVAL;
42 }
Tadeusz Strukcfc2bb32015-06-16 10:31:01 -070043 }
Tudor Ambarus5a7de972016-06-14 16:14:58 +030044
45 key->n = value;
46 key->n_sz = vlen;
47
Tadeusz Strukcfc2bb32015-06-16 10:31:01 -070048 return 0;
49}
50
51int rsa_get_e(void *context, size_t hdrlen, unsigned char tag,
52 const void *value, size_t vlen)
53{
54 struct rsa_key *key = context;
55
Tudor Ambarus5a7de972016-06-14 16:14:58 +030056 /* invalid key provided */
57 if (!value || !key->n_sz || !vlen || vlen > key->n_sz)
58 return -EINVAL;
Tadeusz Strukcfc2bb32015-06-16 10:31:01 -070059
Tudor Ambarus5a7de972016-06-14 16:14:58 +030060 key->e = value;
61 key->e_sz = vlen;
Tadeusz Strukcfc2bb32015-06-16 10:31:01 -070062
63 return 0;
64}
65
66int rsa_get_d(void *context, size_t hdrlen, unsigned char tag,
67 const void *value, size_t vlen)
68{
69 struct rsa_key *key = context;
70
Tudor Ambarus5a7de972016-06-14 16:14:58 +030071 /* invalid key provided */
72 if (!value || !key->n_sz || !vlen || vlen > key->n_sz)
Tadeusz Strukcfc2bb32015-06-16 10:31:01 -070073 return -EINVAL;
Tudor Ambarus5a7de972016-06-14 16:14:58 +030074
75 key->d = value;
76 key->d_sz = vlen;
77
Tadeusz Strukcfc2bb32015-06-16 10:31:01 -070078 return 0;
79}
80
Tadeusz Strukcfc2bb32015-06-16 10:31:01 -070081/**
Tudor Ambarus5a7de972016-06-14 16:14:58 +030082 * rsa_parse_pub_key() - decodes the BER encoded buffer and stores in the
83 * provided struct rsa_key, pointers to the raw key as is,
84 * so that the caller can copy it or MPI parse it, etc.
Tadeusz Strukcfc2bb32015-06-16 10:31:01 -070085 *
86 * @rsa_key: struct rsa_key key representation
87 * @key: key in BER format
88 * @key_len: length of key
89 *
90 * Return: 0 on success or error code in case of error
91 */
Tadeusz Struk22287b02015-10-08 09:26:55 -070092int rsa_parse_pub_key(struct rsa_key *rsa_key, const void *key,
93 unsigned int key_len)
Tadeusz Strukcfc2bb32015-06-16 10:31:01 -070094{
Tudor Ambarus5a7de972016-06-14 16:14:58 +030095 return asn1_ber_decoder(&rsapubkey_decoder, rsa_key, key, key_len);
Tadeusz Strukcfc2bb32015-06-16 10:31:01 -070096}
Tadeusz Struk22287b02015-10-08 09:26:55 -070097EXPORT_SYMBOL_GPL(rsa_parse_pub_key);
98
99/**
Tudor Ambarus5a7de972016-06-14 16:14:58 +0300100 * rsa_parse_priv_key() - decodes the BER encoded buffer and stores in the
101 * provided struct rsa_key, pointers to the raw key
102 * as is, so that the caller can copy it or MPI parse it,
103 * etc.
Tadeusz Struk22287b02015-10-08 09:26:55 -0700104 *
105 * @rsa_key: struct rsa_key key representation
106 * @key: key in BER format
107 * @key_len: length of key
108 *
109 * Return: 0 on success or error code in case of error
110 */
111int rsa_parse_priv_key(struct rsa_key *rsa_key, const void *key,
112 unsigned int key_len)
113{
Tudor Ambarus5a7de972016-06-14 16:14:58 +0300114 return asn1_ber_decoder(&rsaprivkey_decoder, rsa_key, key, key_len);
Tadeusz Struk22287b02015-10-08 09:26:55 -0700115}
116EXPORT_SYMBOL_GPL(rsa_parse_priv_key);