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