summaryrefslogtreecommitdiffstats
path: root/drivers/crypto/imx-scc/scc-blobgen.c
blob: e1a1372420018f3d32c4d18fa5956512c2d37b9f (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
/*
 * 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.
 */
#include <common.h>
#include <dma.h>
#include <digest.h>
#include <driver.h>
#include <init.h>
#include <blobgen.h>
#include <stdlib.h>
#include <crypto.h>
#include <crypto/sha.h>

#include "scc.h"

#define MAX_IVLEN		BLOCKSIZE_BYTES

static struct digest *sha256;

static int sha256sum(uint8_t *src, uint8_t *dst, unsigned int size)
{
	if (!sha256)
		sha256 = digest_alloc("sha256");

	if (!sha256) {
		pr_err("Unable to allocate sha256 digest\n");
		return -EINVAL;
	}

	return digest_digest(sha256, src, size, dst);
}

static int imx_scc_blob_encrypt(struct blobgen *bg, const char *modifier,
				const void *plain, int plainsize, void *blob,
				int *blobsize)
{
	char *s;
	int bufsiz;
	struct ablkcipher_request req = {};
	uint8_t iv[MAX_IVLEN];
	uint8_t hash[SHA256_DIGEST_SIZE];
	int ret;

	bufsiz = ALIGN(plainsize + KEYMOD_LENGTH, 8);

	s = malloc(bufsiz + SHA256_DIGEST_SIZE);
	if (!s)
		return -ENOMEM;

	memset(s, 0, bufsiz);

	strncpy(s, modifier, KEYMOD_LENGTH);
	memcpy(s + KEYMOD_LENGTH, plain, plainsize);

	ret = sha256sum(s, hash, bufsiz);
	if (ret)
		goto out;

	memcpy(s + bufsiz, hash, SHA256_DIGEST_SIZE);

	bufsiz += SHA256_DIGEST_SIZE;

	req.info = iv;
	req.src = s;
	req.dst = blob;
	req.nbytes = bufsiz;

	get_random_bytes(req.info, MAX_IVLEN);

	ret = imx_scc_cbc_des_encrypt(&req);
	if (ret)
		goto out;

	memcpy(blob + bufsiz, req.info, MAX_IVLEN);
	*blobsize = bufsiz + MAX_IVLEN;

out:
	free(s);

	return ret;
}

static int imx_scc_blob_decrypt(struct blobgen *bg, const char *modifier,
				const void *blob, int blobsize, void **plain,
				int *plainsize)
{
	struct ablkcipher_request req = {};
	uint8_t iv[MAX_IVLEN];
	uint8_t hash[SHA256_DIGEST_SIZE];
	int ret;
	uint8_t *data;
	int ciphersize = blobsize - MAX_IVLEN;

	if (blobsize <= MAX_IVLEN + SHA256_DIGEST_SIZE + KEYMOD_LENGTH)
		return -EINVAL;

	data = malloc(ciphersize);
	if (!data)
		return -ENOMEM;

	req.info = iv;
	req.nbytes = ciphersize;
	req.src = (void *)blob;
	req.dst = data;

	memcpy(req.info, blob + req.nbytes, MAX_IVLEN);

	ret = imx_scc_cbc_des_decrypt(&req);
	if (ret)
		goto out;

	ret = sha256sum(data, hash, ciphersize - SHA256_DIGEST_SIZE);
	if (ret)
		goto out;

	if (memcmp(data + ciphersize - SHA256_DIGEST_SIZE, hash,
	    SHA256_DIGEST_SIZE)) {
		pr_err("%s: Corrupted SHA256 digest. Can't continue.\n",
		       bg->dev.name);
		pr_err("%s: Calculated hash:\n", bg->dev.name);
		memory_display(hash, 0, SHA256_DIGEST_SIZE, 1, 0);
		pr_err("%s: Received hash:\n", bg->dev.name);
		memory_display(data + ciphersize - SHA256_DIGEST_SIZE,
			       0, SHA256_DIGEST_SIZE, 1, 0);

		ret = -EILSEQ;
		goto out;
	}

	*plainsize = ciphersize - SHA256_DIGEST_SIZE - KEYMOD_LENGTH;
	*plain = xmemdup(data + KEYMOD_LENGTH, *plainsize);
out:
	free(data);

	return ret;
}

int imx_scc_blob_gen_probe(struct device_d *dev)
{
	struct blobgen *bg;
	int ret;

	bg = xzalloc(sizeof(*bg));

	bg->max_payload_size = MAX_BLOB_LEN - MAX_IVLEN -
				SHA256_DIGEST_SIZE - KEYMOD_LENGTH;
	bg->encrypt = imx_scc_blob_encrypt;
	bg->decrypt = imx_scc_blob_decrypt;

	ret = blob_gen_register(dev, bg);
	if (ret)
		free(bg);

	return ret;
}