summaryrefslogtreecommitdiff
path: root/drivers/crypto/aes/aes-uclass.c
blob: 5bdd3d736c497cd92bbdb33de8dd4e9267d1264b (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
// SPDX-License-Identifier: GPL-2.0+

#define LOG_CATEGORY UCLASS_AES

#include <dm.h>
#include <malloc.h>
#include <log.h>
#include <uboot_aes.h>
#include <linux/string.h>

int dm_aes_get_available_key_slots(struct udevice *dev)
{
	const struct aes_ops *ops;

	if (!dev)
		return -ENODEV;

	ops = aes_get_ops(dev);

	if (!ops->available_key_slots)
		return -ENOSYS;

	return ops->available_key_slots(dev);
}

int dm_aes_select_key_slot(struct udevice *dev, u32 key_size, u8 slot)
{
	const struct aes_ops *ops;

	if (!dev)
		return -ENODEV;

	ops = aes_get_ops(dev);

	if (!ops->select_key_slot)
		return -ENOSYS;

	return ops->select_key_slot(dev, key_size, slot);
}

int dm_aes_set_key_for_key_slot(struct udevice *dev, u32 key_size, u8 *key, u8 slot)
{
	const struct aes_ops *ops;

	if (!dev)
		return -ENODEV;

	ops = aes_get_ops(dev);

	if (!ops->set_key_for_key_slot)
		return -ENOSYS;

	return ops->set_key_for_key_slot(dev, key_size, key, slot);
}

int dm_aes_ecb_encrypt(struct udevice *dev, u8 *src, u8 *dst, u32 num_aes_blocks)
{
	const struct aes_ops *ops;

	if (!dev)
		return -ENODEV;

	ops = aes_get_ops(dev);

	if (!ops->aes_ecb_encrypt)
		return -ENOSYS;

	return ops->aes_ecb_encrypt(dev, src, dst, num_aes_blocks);
}

int dm_aes_ecb_decrypt(struct udevice *dev, u8 *src, u8 *dst, u32 num_aes_blocks)
{
	const struct aes_ops *ops;

	if (!dev)
		return -ENODEV;

	ops = aes_get_ops(dev);

	if (!ops->aes_ecb_decrypt)
		return -ENOSYS;

	return ops->aes_ecb_decrypt(dev, src, dst, num_aes_blocks);
}

int dm_aes_cbc_encrypt(struct udevice *dev, u8 *iv, u8 *src, u8 *dst, u32 num_aes_blocks)
{
	const struct aes_ops *ops;

	if (!dev)
		return -ENODEV;

	ops = aes_get_ops(dev);

	if (!ops->aes_cbc_encrypt)
		return -ENOSYS;

	return ops->aes_cbc_encrypt(dev, iv, src, dst, num_aes_blocks);
}

int dm_aes_cbc_decrypt(struct udevice *dev, u8 *iv, u8 *src, u8 *dst, u32 num_aes_blocks)
{
	const struct aes_ops *ops;

	if (!dev)
		return -ENODEV;

	ops = aes_get_ops(dev);

	if (!ops->aes_cbc_decrypt)
		return -ENOSYS;

	return ops->aes_cbc_decrypt(dev, iv, src, dst, num_aes_blocks);
}

static void left_shift_vector(u8 *in, u8 *out, int size)
{
	int carry = 0;
	int i;

	for (i = size - 1; i >= 0; i--) {
		out[i] = (in[i] << 1) | carry;
		carry = in[i] >> 7;	/* get most significant bit */
	}
}

int dm_aes_cmac(struct udevice *dev, u8 *src, u8 *dst, u32 num_aes_blocks)
{
	const u8 AES_CMAC_CONST_RB = 0x87; /* from RFC 4493, Figure 2.2 */
	const u32 TMP_BUFFER_LEN = 128;
	u8 tmp_block[AES128_KEY_LENGTH] = { };
	u8 k1[AES128_KEY_LENGTH];
	u8 *tmp_buffer;
	int ret;

	log_debug("%s: 0x%p -> %p blocks %d\n", __func__, src, dst, num_aes_blocks);

	if (!num_aes_blocks) {
		log_debug("%s: called with 0 blocks!\n", __func__);
		return -1;
	}

	/* Compute K1 constant needed by AES-CMAC calculation */
	ret = dm_aes_cbc_encrypt(dev, (u8 *)AES_ZERO_BLOCK, (u8 *)AES_ZERO_BLOCK, tmp_block, 1);
	if (ret)
		return -1;

	left_shift_vector(tmp_block, k1, AES_BLOCK_LENGTH);

	if ((tmp_block[0] >> 7) != 0) /* get MSB of L */
		k1[AES128_KEY_LENGTH - 1] ^= AES_CMAC_CONST_RB;

	/* Set what will be the initial IV as zero */
	memset(tmp_block, 0, AES_BLOCK_LENGTH);

	/* Process all blocks except last by calling engine several times per dma buffer size */
	if (num_aes_blocks > 1) {
		tmp_buffer = malloc(AES_BLOCK_LENGTH * min(num_aes_blocks - 1, TMP_BUFFER_LEN));
		if (!tmp_buffer)
			return -1;
		while (num_aes_blocks > 1) {
			u32 blocks = min(num_aes_blocks - 1, TMP_BUFFER_LEN);

			/* Encrypt the current remaining set of blocks that fits in tmp buffer */
			ret = dm_aes_cbc_encrypt(dev, tmp_block, src, tmp_buffer, blocks);
			if (ret) {
				free(tmp_buffer);
				return -1;
			}

			num_aes_blocks -= blocks;
			src += blocks * AES_BLOCK_LENGTH;

			/* Copy the last encrypted block to tmp_block as IV */
			memcpy(tmp_block, tmp_buffer + ((blocks - 1) * AES_BLOCK_LENGTH),
			       AES_BLOCK_LENGTH);
		}
		free(tmp_buffer);
	}

	if (num_aes_blocks != 1) {
		log_debug("%s: left with %d blocks! must be 1\n", __func__, num_aes_blocks);
		return -1;
	}

	/* XOR last IV with K1 */
	aes_apply_cbc_chain_data(tmp_block, k1, tmp_block);

	/* Encrypt the last src block already with tmp_block as IV and output to dst */
	return dm_aes_cbc_encrypt(dev, tmp_block, src, dst, 1);
}

UCLASS_DRIVER(aes) = {
	.id	= UCLASS_AES,
	.name	= "aes",
};