blob: f71dfafab80a7e7cc17fa0e5cac0cce64abfb08e [file] [log] [blame]
Alexandru Gagniuce89660f2021-07-14 17:05:37 -05001// 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[] = {
Bo Lv8d6888f2023-07-21 14:12:57 +080051#ifndef CONFIG_AMLOGIC_MODIFY
Alexandru Gagniuce89660f2021-07-14 17:05:37 -050052 {
53 .name = "rsa2048",
54 .key_len = RSA2048_BYTES,
55 .sign = rsa_sign,
56 .add_verify_data = rsa_add_verify_data,
57 .verify = rsa_verify,
58 },
59 {
Jamin Lin2a4b0d52022-01-19 16:23:21 +080060 .name = "rsa3072",
61 .key_len = RSA3072_BYTES,
62 .sign = rsa_sign,
63 .add_verify_data = rsa_add_verify_data,
64 .verify = rsa_verify,
65 },
66 {
Alexandru Gagniuce89660f2021-07-14 17:05:37 -050067 .name = "rsa4096",
68 .key_len = RSA4096_BYTES,
69 .sign = rsa_sign,
70 .add_verify_data = rsa_add_verify_data,
71 .verify = rsa_verify,
72 },
73 {
74 .name = "ecdsa256",
75 .key_len = ECDSA256_BYTES,
76 .sign = ecdsa_sign,
77 .add_verify_data = ecdsa_add_verify_data,
78 .verify = ecdsa_verify,
79 },
Bo Lv8d6888f2023-07-21 14:12:57 +080080#endif
Alexandru Gagniuce89660f2021-07-14 17:05:37 -050081};
82
83struct padding_algo padding_algos[] = {
84 {
85 .name = "pkcs-1.5",
86 .verify = padding_pkcs_15_verify,
87 },
88 {
89 .name = "pss",
90 .verify = padding_pss_verify,
91 }
92};
93
94struct checksum_algo *image_get_checksum_algo(const char *full_name)
95{
96 int i;
97 const char *name;
98
99 for (i = 0; i < ARRAY_SIZE(checksum_algos); i++) {
100 name = checksum_algos[i].name;
101 /* Make sure names match and next char is a comma */
102 if (!strncmp(name, full_name, strlen(name)) &&
103 full_name[strlen(name)] == ',')
104 return &checksum_algos[i];
105 }
106
107 return NULL;
108}
109
110struct crypto_algo *image_get_crypto_algo(const char *full_name)
111{
112 int i;
113 const char *name;
114
115 /* Move name to after the comma */
116 name = strchr(full_name, ',');
117 if (!name)
118 return NULL;
119 name += 1;
120
121 for (i = 0; i < ARRAY_SIZE(crypto_algos); i++) {
122 if (!strcmp(crypto_algos[i].name, name))
123 return &crypto_algos[i];
124 }
125
126 return NULL;
127}
128
129struct padding_algo *image_get_padding_algo(const char *name)
130{
131 int i;
132
133 if (!name)
134 return NULL;
135
136 for (i = 0; i < ARRAY_SIZE(padding_algos); i++) {
137 if (!strcmp(padding_algos[i].name, name))
138 return &padding_algos[i];
139 }
140
141 return NULL;
142}