1// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright (c) 2013, Google Inc.
4 */
5
6#include <common.h>
7#include <log.h>
8#include <malloc.h>
9#include <asm/global_data.h>
10DECLARE_GLOBAL_DATA_PTR;
11#include <image.h>
12#include <relocate.h>
13#include <u-boot/ecdsa.h>
14#include <u-boot/rsa.h>
15#include <u-boot/hash-checksum.h>
16
17#define IMAGE_MAX_HASHED_NODES		100
18
19struct checksum_algo checksum_algos[] = {
20#if CONFIG_IS_ENABLED(SHA1)
21	{
22		.name = "sha1",
23		.checksum_len = SHA1_SUM_LEN,
24		.der_len = SHA1_DER_LEN,
25		.der_prefix = sha1_der_prefix,
26		.calculate = hash_calculate,
27	},
28#endif
29#if CONFIG_IS_ENABLED(SHA256)
30	{
31		.name = "sha256",
32		.checksum_len = SHA256_SUM_LEN,
33		.der_len = SHA256_DER_LEN,
34		.der_prefix = sha256_der_prefix,
35		.calculate = hash_calculate,
36	},
37#endif
38#if CONFIG_IS_ENABLED(SHA384)
39	{
40		.name = "sha384",
41		.checksum_len = SHA384_SUM_LEN,
42		.der_len = SHA384_DER_LEN,
43		.der_prefix = sha384_der_prefix,
44		.calculate = hash_calculate,
45	},
46#endif
47#if CONFIG_IS_ENABLED(SHA512)
48	{
49		.name = "sha512",
50		.checksum_len = SHA512_SUM_LEN,
51		.der_len = SHA512_DER_LEN,
52		.der_prefix = sha512_der_prefix,
53		.calculate = hash_calculate,
54	},
55#endif
56
57};
58
59struct checksum_algo *image_get_checksum_algo(const char *full_name)
60{
61	int i;
62	const char *name;
63
64	for (i = 0; i < ARRAY_SIZE(checksum_algos); i++) {
65		name = checksum_algos[i].name;
66		/* Make sure names match and next char is a comma */
67		if (!strncmp(name, full_name, strlen(name)) &&
68		    full_name[strlen(name)] == ',')
69			return &checksum_algos[i];
70	}
71
72	return NULL;
73}
74
75struct crypto_algo *image_get_crypto_algo(const char *full_name)
76{
77	struct crypto_algo *crypto, *end;
78	const char *name;
79
80	/* Move name to after the comma */
81	name = strchr(full_name, ',');
82	if (!name)
83		return NULL;
84	name += 1;
85
86	crypto = ll_entry_start(struct crypto_algo, cryptos);
87	end = ll_entry_end(struct crypto_algo, cryptos);
88	for (; crypto < end; crypto++) {
89		if (!strcmp(crypto->name, name))
90			return crypto;
91	}
92
93	/* Not found */
94	return NULL;
95}
96
97struct padding_algo *image_get_padding_algo(const char *name)
98{
99	struct padding_algo *padding, *end;
100
101	if (!name)
102		return NULL;
103
104	padding = ll_entry_start(struct padding_algo, paddings);
105	end = ll_entry_end(struct padding_algo, paddings);
106	for (; padding < end; padding++) {
107		if (!strcmp(padding->name, name))
108			return padding;
109	}
110
111	return NULL;
112}
113