summaryrefslogtreecommitdiffstats
path: root/lib/blobgen.c
blob: 5a556a68ce1ff8bdebb840c9f224ec739b38199c (plain)
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/*
 * Copyright (C) 2016 Pengutronix, Steffen Trumtrar <kernel@pengutronix.de>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * version 2, as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 */
#include <blobgen.h>
#include <base64.h>
#include <malloc.h>
#include <crypto.h>
#include <dma.h>
#include <environment.h>

static LIST_HEAD(blobs);
static struct blobgen *bg_default;

/**
 * blob_gen_register - register a blob device
 * @dev: The parent device
 * @bg: The blobgen device
 *
 * This registers a blob device. Returns 0 for success or a negative error
 * code otherwise.
 */
int blob_gen_register(struct device_d *dev, struct blobgen *bg)
{
	int ret;

	dev_set_name(&bg->dev, "blob");
	bg->dev.parent = dev;

	ret = register_device(&bg->dev);
	if (ret)
		return ret;

	list_add_tail(&bg->list, &blobs);

	if (!bg_default)
		bg_default = bg;

	return 0;
}

/**
 * blobgen_get - get a blob generator of given name
 * @name: The name of the blob generator to look for
 *
 * Finds a blob generator by name and returns it. Returns NULL if none is found.
 */
struct blobgen *blobgen_get(const char *name)
{
	struct device_d *dev;
	struct blobgen *bg;

	if (!name)
		return bg_default;

	dev = get_device_by_name(name);
	if (!dev)
		return NULL;

	list_for_each_entry(bg, &blobs, list) {
		if (dev == &bg->dev)
			return bg;
	}

	return NULL;
}

/**
 * blob_encrypt - encrypt a data blob
 * @bg: The blob generator to use
 * @modifier: Modifier string
 * @plain: The plaintext input
 * @plainsize: Length of the plain data in bytes
 * @retblob: The encrypted blob is returned here
 * @blobsize: The returned length of the encrypted blob
 *
 * This encrypts a blob passed in @plain to an allocated buffer returned in
 * @retblob. Returns 0 for success or a negative error code otherwise. @retblob
 * is valid when the function returns successfully. The caller must free the
 * buffer after use.
 */
int blob_encrypt(struct blobgen *bg, const char *modifier, const void *plain,
		 int plainsize, void **retblob, int *blobsize)
{
	void *blob;
	int ret;

	if (plainsize > bg->max_payload_size)
		return -EINVAL;

	pr_debug("%s plain:\n", __func__);
	pr_memory_display(MSG_DEBUG, plain, 0, plainsize, 1, 0);

	blob = dma_alloc(MAX_BLOB_LEN);
	if (!blob)
		return -ENOMEM;

	ret = bg->encrypt(bg, modifier, plain, plainsize, blob, blobsize);

	if (ret) {
		free(blob);
	} else {
		pr_debug("%s encrypted:\n", __func__);
		pr_memory_display(MSG_DEBUG, blob, 0, *blobsize, 1, 0);
		*retblob = blob;
	}

	return ret;
}

/**
 * blob_encrypt_to_env -  encrypt blob to environment variable
 * @bg: The blob generator to use
 * @modifier: Modifier string
 * @plain: The plaintext input
 * @plainsize: Length of the plain data in bytes
 * @varname: Name of the variable to set with the output blob
 *
 * This uses blob_encrypt to encrypt a blob. The result is base64 encoded and
 * written to the environment variable @varname. Returns 0 for success or a
 * negative error code otherwise.
 */
int blob_encrypt_to_env(struct blobgen *bg, const char *modifier,
			const void *plain, int plainsize, const char *varname)
{
	int ret;
	int blobsize;
	void *blob;
	char *value;

	ret = blob_encrypt(bg, modifier, plain, plainsize, &blob, &blobsize);
	if (ret)
		return ret;

	value = malloc(BASE64_LENGTH(blobsize) + 1);
	if (!value)
		return -ENOMEM;

	uuencode(value, blob, blobsize);

	pr_debug("%s encrypted base64: \"%s\"\n", __func__, value);

	ret = setenv(varname, value);

	free(value);
	free(blob);

	return ret;
}

/**
 * blob_decrypt - decrypt a blob
 * @bg: The blob generator to use
 * @modifier: Modifier string
 * @blob: The encrypted blob
 * @blobsize: Size of the encrypted blob
 * @plain: Plaintext is returned here
 * @plainsize: Size of the data returned in bytes
 *
 * This function decrypts a blob generated with blob_encrypt. @modifier must match
 * the modifier used to encrypt the data. Returns 0 when the data could be
 * decrypted successfully or a negative error code otherwise.
 */
int blob_decrypt(struct blobgen *bg, const char *modifier, const void *blob,
		 int blobsize, void **plain, int *plainsize)
{
	int ret;

	pr_debug("%s encrypted:\n", __func__);
	pr_memory_display(MSG_DEBUG, blob, 0, blobsize, 1, 0);

	ret = bg->decrypt(bg, modifier, blob, blobsize, plain, plainsize);

	if (!ret) {
		pr_debug("%s decrypted:\n", __func__);
		pr_memory_display(MSG_DEBUG, *plain, 0, *plainsize, 1, 0);
	}

	return ret;
}

/**
 * blob_decrypt_from_base64 - decrypt a base64 encoded blob
 * @bg: The blob generator to use
 * @modifier: Modifier string
 * @encrypted: base64 encoded encrypted data
 * @plain: Plaintext is returned here
 * @plainsize: Size of the data returned in bytes
 *
 * like blob_decrypt, but takes the encrypted data as a base64 encoded string.
 * Returns 0 when the data could be decrypted successfully or a negative error
 * code otherwise.
 */
int blob_decrypt_from_base64(struct blobgen *bg, const char *modifier,
			     const char *encrypted, void **plain,
			     int *plainsize)
{
	char *data;
	int ret, len;

	data = dma_alloc(MAX_BLOB_LEN);
	if (!data)
		return -ENOMEM;

	pr_debug("encrypted base64: \"%s\"\n", encrypted);

	len = decode_base64(data, MAX_BLOB_LEN, encrypted);

	ret = blob_decrypt(bg, modifier, data, len, plain, plainsize);

	free(data);

	return ret;
}