blob: 21b4fa5d39df5775061b2b697dc8f0ef7d1f9824 [file] [log] [blame]
Alexandru Gagniuc298bb162021-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[] = {
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 {
Jamin Lin5975ad72022-01-19 16:23:21 +080059 .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 {
Alexandru Gagniuc298bb162021-07-14 17:05:37 -050066 .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 },
Joakim Tjernlundca9e9ec2024-09-20 18:14:35 +020079 {
80 .name = "secp521r1",
81 .key_len = ECDSA521_BYTES,
82 .sign = ecdsa_sign,
83 .add_verify_data = ecdsa_add_verify_data,
84 .verify = ecdsa_verify,
85 },
Alexandru Gagniuc298bb162021-07-14 17:05:37 -050086};
87
88struct padding_algo padding_algos[] = {
89 {
90 .name = "pkcs-1.5",
91 .verify = padding_pkcs_15_verify,
92 },
93 {
94 .name = "pss",
95 .verify = padding_pss_verify,
96 }
97};
98
99struct checksum_algo *image_get_checksum_algo(const char *full_name)
100{
101 int i;
102 const char *name;
103
104 for (i = 0; i < ARRAY_SIZE(checksum_algos); i++) {
105 name = checksum_algos[i].name;
106 /* Make sure names match and next char is a comma */
107 if (!strncmp(name, full_name, strlen(name)) &&
108 full_name[strlen(name)] == ',')
109 return &checksum_algos[i];
110 }
111
112 return NULL;
113}
114
115struct crypto_algo *image_get_crypto_algo(const char *full_name)
116{
117 int i;
118 const char *name;
119
120 /* Move name to after the comma */
121 name = strchr(full_name, ',');
122 if (!name)
123 return NULL;
124 name += 1;
125
126 for (i = 0; i < ARRAY_SIZE(crypto_algos); i++) {
127 if (!strcmp(crypto_algos[i].name, name))
128 return &crypto_algos[i];
129 }
130
131 return NULL;
132}
133
134struct padding_algo *image_get_padding_algo(const char *name)
135{
136 int i;
137
138 if (!name)
139 return NULL;
140
141 for (i = 0; i < ARRAY_SIZE(padding_algos); i++) {
142 if (!strcmp(padding_algos[i].name, name))
143 return &padding_algos[i];
144 }
145
146 return NULL;
147}