-
Notifications
You must be signed in to change notification settings - Fork 12
/
lrng_drng_kcapi.c
208 lines (171 loc) · 5.07 KB
/
lrng_drng_kcapi.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
// SPDX-License-Identifier: GPL-2.0 OR BSD-2-Clause
/*
* Backend for the LRNG providing the cryptographic primitives using the
* kernel crypto API.
*
* Copyright (C) 2022, Stephan Mueller <smueller@chronox.de>
*/
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
#include <linux/lrng.h>
#include <crypto/hash.h>
#include <crypto/rng.h>
#include <linux/init.h>
#include <linux/module.h>
#include "lrng_drng_kcapi.h"
static char *drng_name = NULL;
module_param(drng_name, charp, 0444);
MODULE_PARM_DESC(drng_name, "Kernel crypto API name of DRNG");
static char *seed_hash = NULL;
module_param(seed_hash, charp, 0444);
MODULE_PARM_DESC(seed_hash,
"Kernel crypto API name of hash with output size equal to seedsize of DRNG to bring seed string to the size required by the DRNG");
struct lrng_drng_info {
struct crypto_rng *kcapi_rng;
struct crypto_shash *hash_tfm;
};
static int lrng_kcapi_drng_seed_helper(void *drng, const u8 *inbuf,
u32 inbuflen)
{
struct lrng_drng_info *lrng_drng_info = (struct lrng_drng_info *)drng;
struct crypto_rng *kcapi_rng = lrng_drng_info->kcapi_rng;
struct crypto_shash *hash_tfm = lrng_drng_info->hash_tfm;
SHASH_DESC_ON_STACK(shash, hash_tfm);
u32 digestsize;
u8 digest[HASH_MAX_DIGESTSIZE] __aligned(8);
int ret;
if (!hash_tfm)
return crypto_rng_reset(kcapi_rng, inbuf, inbuflen);
shash->tfm = hash_tfm;
digestsize = crypto_shash_digestsize(hash_tfm);
ret = crypto_shash_digest(shash, inbuf, inbuflen, digest);
shash_desc_zero(shash);
if (ret)
return ret;
ret = crypto_rng_reset(kcapi_rng, digest, digestsize);
if (ret)
return ret;
memzero_explicit(digest, digestsize);
return 0;
}
static int lrng_kcapi_drng_generate_helper(void *drng, u8 *outbuf,
u32 outbuflen)
{
struct lrng_drng_info *lrng_drng_info = (struct lrng_drng_info *)drng;
struct crypto_rng *kcapi_rng = lrng_drng_info->kcapi_rng;
int ret = crypto_rng_get_bytes(kcapi_rng, outbuf, outbuflen);
if (ret < 0)
return ret;
return outbuflen;
}
static void *lrng_kcapi_drng_alloc(u32 sec_strength)
{
struct lrng_drng_info *lrng_drng_info;
struct crypto_rng *kcapi_rng;
u32 time = random_get_entropy();
int seedsize, rv;
void *ret = ERR_PTR(-ENOMEM);
if (!drng_name) {
pr_err("DRNG name missing\n");
return ERR_PTR(-EINVAL);
}
if (!memcmp(drng_name, "stdrng", 6) ||
!memcmp(drng_name, "lrng", 4) ||
!memcmp(drng_name, "drbg", 4) ||
!memcmp(drng_name, "jitterentropy_rng", 17)) {
pr_err("Refusing to load the requested random number generator\n");
return ERR_PTR(-EINVAL);
}
lrng_drng_info = kzalloc(sizeof(*lrng_drng_info), GFP_KERNEL);
if (!lrng_drng_info)
return ERR_PTR(-ENOMEM);
kcapi_rng = crypto_alloc_rng(drng_name, 0, 0);
if (IS_ERR(kcapi_rng)) {
pr_err("DRNG %s cannot be allocated\n", drng_name);
ret = ERR_CAST(kcapi_rng);
goto free;
}
lrng_drng_info->kcapi_rng = kcapi_rng;
seedsize = crypto_rng_seedsize(kcapi_rng);
if (seedsize) {
struct crypto_shash *hash_tfm;
if (!seed_hash) {
switch (seedsize) {
case 32:
seed_hash = "sha256";
break;
case 48:
seed_hash = "sha384";
break;
case 64:
seed_hash = "sha512";
break;
default:
pr_err("Seed size %d cannot be processed\n",
seedsize);
goto dealloc;
}
}
hash_tfm = crypto_alloc_shash(seed_hash, 0, 0);
if (IS_ERR(hash_tfm)) {
ret = ERR_CAST(hash_tfm);
goto dealloc;
}
if (seedsize != crypto_shash_digestsize(hash_tfm)) {
pr_err("Seed hash output size not equal to DRNG seed size\n");
crypto_free_shash(hash_tfm);
ret = ERR_PTR(-EINVAL);
goto dealloc;
}
lrng_drng_info->hash_tfm = hash_tfm;
pr_info("Seed hash %s allocated\n", seed_hash);
}
rv = lrng_kcapi_drng_seed_helper(lrng_drng_info, (u8 *)(&time),
sizeof(time));
if (rv) {
ret = ERR_PTR(rv);
goto dealloc;
}
pr_info("Kernel crypto API DRNG %s allocated\n", drng_name);
return lrng_drng_info;
dealloc:
crypto_free_rng(kcapi_rng);
free:
kfree(lrng_drng_info);
return ret;
}
static void lrng_kcapi_drng_dealloc(void *drng)
{
struct lrng_drng_info *lrng_drng_info = (struct lrng_drng_info *)drng;
struct crypto_rng *kcapi_rng = lrng_drng_info->kcapi_rng;
crypto_free_rng(kcapi_rng);
if (lrng_drng_info->hash_tfm)
crypto_free_shash(lrng_drng_info->hash_tfm);
kfree(lrng_drng_info);
pr_info("DRNG %s deallocated\n", drng_name);
}
static const char *lrng_kcapi_drng_name(void)
{
return drng_name;
}
const struct lrng_drng_cb lrng_kcapi_drng_cb = {
.drng_name = lrng_kcapi_drng_name,
.drng_alloc = lrng_kcapi_drng_alloc,
.drng_dealloc = lrng_kcapi_drng_dealloc,
.drng_seed = lrng_kcapi_drng_seed_helper,
.drng_generate = lrng_kcapi_drng_generate_helper,
};
#ifndef CONFIG_LRNG_DFLT_DRNG_KCAPI
static int __init lrng_kcapi_init(void)
{
return lrng_set_drng_cb(&lrng_kcapi_drng_cb);
}
static void __exit lrng_kcapi_exit(void)
{
lrng_set_drng_cb(NULL);
}
late_initcall(lrng_kcapi_init);
module_exit(lrng_kcapi_exit);
MODULE_LICENSE("Dual BSD/GPL");
MODULE_AUTHOR("Stephan Mueller <smueller@chronox.de>");
MODULE_DESCRIPTION("Entropy Source and DRNG Manager - kernel crypto API DRNG backend");
#endif /* CONFIG_LRNG_DFLT_DRNG_KCAPI */