1// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright (c) 2013, Google Inc.
4 */
5
6#include "mkimage.h"
7#include <fdt_support.h>
8#include <time.h>
9#include <linux/libfdt.h>
10#include <image.h>
11#include <u-boot/ecdsa.h>
12#include <u-boot/rsa.h>
13#include <u-boot/hash-checksum.h>
14
15struct checksum_algo checksum_algos[] = {
16	{
17		.name = "sha1",
18		.checksum_len = SHA1_SUM_LEN,
19		.der_len = SHA1_DER_LEN,
20		.der_prefix = sha1_der_prefix,
21		.calculate_sign = EVP_sha1,
22		.calculate = hash_calculate,
23	},
24	{
25		.name = "sha256",
26		.checksum_len = SHA256_SUM_LEN,
27		.der_len = SHA256_DER_LEN,
28		.der_prefix = sha256_der_prefix,
29		.calculate_sign = EVP_sha256,
30		.calculate = hash_calculate,
31	},
32	{
33		.name = "sha384",
34		.checksum_len = SHA384_SUM_LEN,
35		.der_len = SHA384_DER_LEN,
36		.der_prefix = sha384_der_prefix,
37		.calculate_sign = EVP_sha384,
38		.calculate = hash_calculate,
39	},
40	{
41		.name = "sha512",
42		.checksum_len = SHA512_SUM_LEN,
43		.der_len = SHA512_DER_LEN,
44		.der_prefix = sha512_der_prefix,
45		.calculate_sign = EVP_sha512,
46		.calculate = hash_calculate,
47	},
48};
49
50struct crypto_algo crypto_algos[] = {
51	{
52		.name = "rsa2048",
53		.key_len = RSA2048_BYTES,
54		.sign = rsa_sign,
55		.add_verify_data = rsa_add_verify_data,
56		.verify = rsa_verify,
57	},
58	{
59		.name = "rsa3072",
60		.key_len = RSA3072_BYTES,
61		.sign = rsa_sign,
62		.add_verify_data = rsa_add_verify_data,
63		.verify = rsa_verify,
64	},
65	{
66		.name = "rsa4096",
67		.key_len = RSA4096_BYTES,
68		.sign = rsa_sign,
69		.add_verify_data = rsa_add_verify_data,
70		.verify = rsa_verify,
71	},
72	{
73		.name = "ecdsa256",
74		.key_len = ECDSA256_BYTES,
75		.sign = ecdsa_sign,
76		.add_verify_data = ecdsa_add_verify_data,
77		.verify = ecdsa_verify,
78	},
79};
80
81struct padding_algo padding_algos[] = {
82	{
83		.name = "pkcs-1.5",
84		.verify = padding_pkcs_15_verify,
85	},
86	{
87		.name = "pss",
88		.verify = padding_pss_verify,
89	}
90};
91
92struct checksum_algo *image_get_checksum_algo(const char *full_name)
93{
94	int i;
95	const char *name;
96
97	for (i = 0; i < ARRAY_SIZE(checksum_algos); i++) {
98		name = checksum_algos[i].name;
99		/* Make sure names match and next char is a comma */
100		if (!strncmp(name, full_name, strlen(name)) &&
101		    full_name[strlen(name)] == ',')
102			return &checksum_algos[i];
103	}
104
105	return NULL;
106}
107
108struct crypto_algo *image_get_crypto_algo(const char *full_name)
109{
110	int i;
111	const char *name;
112
113	/* Move name to after the comma */
114	name = strchr(full_name, ',');
115	if (!name)
116		return NULL;
117	name += 1;
118
119	for (i = 0; i < ARRAY_SIZE(crypto_algos); i++) {
120		if (!strcmp(crypto_algos[i].name, name))
121			return &crypto_algos[i];
122	}
123
124	return NULL;
125}
126
127struct padding_algo *image_get_padding_algo(const char *name)
128{
129	int i;
130
131	if (!name)
132		return NULL;
133
134	for (i = 0; i < ARRAY_SIZE(padding_algos); i++) {
135		if (!strcmp(padding_algos[i].name, name))
136			return &padding_algos[i];
137	}
138
139	return NULL;
140}
141