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 
15 struct 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 
50 struct 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 = "rsa4096",
60 		.key_len = RSA4096_BYTES,
61 		.sign = rsa_sign,
62 		.add_verify_data = rsa_add_verify_data,
63 		.verify = rsa_verify,
64 	},
65 	{
66 		.name = "ecdsa256",
67 		.key_len = ECDSA256_BYTES,
68 		.sign = ecdsa_sign,
69 		.add_verify_data = ecdsa_add_verify_data,
70 		.verify = ecdsa_verify,
71 	},
72 };
73 
74 struct padding_algo padding_algos[] = {
75 	{
76 		.name = "pkcs-1.5",
77 		.verify = padding_pkcs_15_verify,
78 	},
79 	{
80 		.name = "pss",
81 		.verify = padding_pss_verify,
82 	}
83 };
84 
image_get_checksum_algo(const char * full_name)85 struct checksum_algo *image_get_checksum_algo(const char *full_name)
86 {
87 	int i;
88 	const char *name;
89 
90 	for (i = 0; i < ARRAY_SIZE(checksum_algos); i++) {
91 		name = checksum_algos[i].name;
92 		/* Make sure names match and next char is a comma */
93 		if (!strncmp(name, full_name, strlen(name)) &&
94 		    full_name[strlen(name)] == ',')
95 			return &checksum_algos[i];
96 	}
97 
98 	return NULL;
99 }
100 
image_get_crypto_algo(const char * full_name)101 struct crypto_algo *image_get_crypto_algo(const char *full_name)
102 {
103 	int i;
104 	const char *name;
105 
106 	/* Move name to after the comma */
107 	name = strchr(full_name, ',');
108 	if (!name)
109 		return NULL;
110 	name += 1;
111 
112 	for (i = 0; i < ARRAY_SIZE(crypto_algos); i++) {
113 		if (!strcmp(crypto_algos[i].name, name))
114 			return &crypto_algos[i];
115 	}
116 
117 	return NULL;
118 }
119 
image_get_padding_algo(const char * name)120 struct padding_algo *image_get_padding_algo(const char *name)
121 {
122 	int i;
123 
124 	if (!name)
125 		return NULL;
126 
127 	for (i = 0; i < ARRAY_SIZE(padding_algos); i++) {
128 		if (!strcmp(padding_algos[i].name, name))
129 			return &padding_algos[i];
130 	}
131 
132 	return NULL;
133 }
134