blob: 6086c8de17b63b0cd7cc02684e3c9748fa7704ff [file] [log] [blame]
Gaurav Shah322536d2010-01-28 15:01:23 -08001#!/bin/bash
2
3# Copyright (c) 2010 The Chromium OS Authors. All rights reserved.
4# Use of this source code is governed by a BSD-style license that can be
5# found in the LICENSE file.
6
7# Script to generate padding.c containing PKCS 1.5 padding byte arrays for
8# various combinations of RSA key lengths and message digest algorithms.
9
10Pad_Preamble="0x00,0x01"
11
Gaurav Shah8bf29d82010-01-28 19:43:24 -080012SHA1_digestinfo="0x30,0x21,0x30,0x09,0x06,0x05,0x2b,0x0e,0x03,0x02,0x1a,0x05"\
Gaurav Shah322536d2010-01-28 15:01:23 -080013",0x00,0x04,0x14"
Gaurav Shah8bf29d82010-01-28 19:43:24 -080014SHA256_digestinfo="0x30,0x31,0x30,0x0d,0x06,0x09,0x60,0x86,0x48,0x01,0x65,0x03"\
Gaurav Shah322536d2010-01-28 15:01:23 -080015",0x04,0x02,0x01,0x05,0x00,0x04,0x20"
Gaurav Shah8bf29d82010-01-28 19:43:24 -080016SHA512_digestinfo="0x30,0x51,0x30,0x0d,0x06,0x09,0x60,0x86,0x48,0x01,0x65,0x03"\
Gaurav Shah322536d2010-01-28 15:01:23 -080017",0x04,0x02,0x03,0x05,0x00,0x04,0x40"
18
19RSA1024_Len=128
20RSA2048_Len=256
21RSA4096_Len=512
22RSA8192_Len=1024
23
24SHA1_T_Len=35
25SHA256_T_Len=51
26SHA512_T_Len=83
27
28HashAlgos=( SHA1 SHA256 SHA512 )
29RSAAlgos=( RSA1024 RSA2048 RSA4096 RSA8192 )
30
31function genFFOctets {
32 count=$1
33 while [ $count -gt 0 ]; do
34 echo -n "0xff,"
35 let count=count-1
36 done
37}
38
39
40cat <<EOF
41/*
42 * DO NOT MODIFY THIS FILE DIRECTLY.
43 *
44 * This file is automatically generated by genpadding.sh and contains padding
45 * arrays corresponding to various combinations of algorithms for RSA signatures.
46 */
47
48EOF
49
50
51echo '#include "rsa.h"'
52echo '#include "sha.h"'
53echo
54echo
55cat <<EOF
56/*
57 * PKCS 1.5 padding (from the RSA PKCS#1 v2.1 standard)
58 *
59 * Depending on the RSA key size and hash function, the padding is calculated
60 * as follows:
61 *
62 * 0x00 || 0x01 || PS || 0x00 || T
63 *
64 * T: DER Encoded DigestInfo value which depends on the hash function used.
65 *
66 * SHA-1: (0x)30 21 30 09 06 05 2b 0e 03 02 1a 05 00 04 14 || H.
67 * SHA-256: (0x)30 31 30 0d 06 09 60 86 48 01 65 03 04 02 01 05 00 04 20 || H.
68 * SHA-512: (0x)30 51 30 0d 06 09 60 86 48 01 65 03 04 02 03 05 00 04 40 || H.
69 *
70 * Length(T) = 35 octets for SHA-1
71 * Length(T) = 51 octets for SHA-256
72 * Length(T) = 83 octets for SHA-512
73 *
74 * PS: octet string consisting of {Length(RSA Key) - Length(T) - 3} 0xFF
75 *
76 */
77EOF
78echo
79echo
80
81
82# Generate padding arrays.
83algorithmcounter=0
84
85for rsaalgo in ${RSAAlgos[@]}
86do
87 for hashalgo in ${HashAlgos[@]}
88 do
89 echo "/* Algorithm Type $algorithmcounter */"
90 let algorithmcounter=algorithmcounter+1
91 eval rsalen=${rsaalgo}_Len
92 eval hashlen=${hashalgo}_T_Len
93 let nums=rsalen-hashlen-3
94 echo "const uint8_t padding${rsaalgo}_${hashalgo}[${rsaalgo}NUMBYTES - ${hashalgo}_DIGEST_SIZE] = {"
95 echo -n $Pad_Preamble,
96 genFFOctets $nums
97 echo -n "0x00,"
Gaurav Shah8bf29d82010-01-28 19:43:24 -080098 eval digestinfo=\$${hashalgo}_digestinfo
99 echo $digestinfo
Gaurav Shah322536d2010-01-28 15:01:23 -0800100 echo "};"
101 echo
102 done
103done
104
105echo "const int kNumAlgorithms = $algorithmcounter;";
106echo "#define NUMALGORITHMS $algorithmcounter"
107echo
108
Gaurav Shah8bf29d82010-01-28 19:43:24 -0800109# Output DigestInfo field lengths.
110cat <<EOF
111#define SHA1_DIGESTINFO_LEN 15
112#define SHA256_DIGESTINFO_LEN 19
113#define SHA512_DIGESTINFO_LEN 19
114EOF
115
116
117# Generate DigestInfo arrays.
118for hashalgo in ${HashAlgos[@]}
119do
120 echo "const uint8_t ${hashalgo}_digestinfo[] = {"
121 eval digestinfo=\$${hashalgo}_digestinfo
122 echo $digestinfo
123 echo "};"
124 echo
125done
126
127# Generate DigestInfo to size map.
128echo "const int digestinfo_size_map[] = {"
129for rsaalgo in ${RSAAlgos[@]}
130do
131 for hashalgo in ${HashAlgos[@]}
132 do
133 echo ${hashalgo}_DIGESTINFO_LEN,
134 done
135done
136echo "};"
137echo
138
139# Generate algorithm signature length map.
Gaurav Shah322536d2010-01-28 15:01:23 -0800140echo "const int siglen_map[NUMALGORITHMS] = {"
141for rsaalgo in ${RSAAlgos[@]}
142do
143 for hashalgo in ${HashAlgos[@]}
144 do
Gaurav Shahcae5fa62010-02-28 20:02:29 -0800145 echo ${rsaalgo}NUMBYTES,
Gaurav Shah322536d2010-01-28 15:01:23 -0800146 done
147done
148echo "};"
149echo
150
Gaurav Shah8bf29d82010-01-28 19:43:24 -0800151# Generate algorithm padding array map.
Gaurav Shah322536d2010-01-28 15:01:23 -0800152echo "const uint8_t* padding_map[NUMALGORITHMS] = {"
153for rsaalgo in ${RSAAlgos[@]}
154do
155 for hashalgo in ${HashAlgos[@]}
156 do
157 echo padding${rsaalgo}_${hashalgo},
158 done
159done
160echo "};"
161echo
162
Gaurav Shah8bf29d82010-01-28 19:43:24 -0800163# Generate algorithm padding size map.
Gaurav Shah322536d2010-01-28 15:01:23 -0800164echo "const int padding_size_map[NUMALGORITHMS] = {"
165for rsaalgo in ${RSAAlgos[@]}
166do
167 for hashalgo in ${HashAlgos[@]}
168 do
169 echo ${rsaalgo}NUMBYTES - ${hashalgo}_DIGEST_SIZE,
170 done
171done
172echo "};"
173echo
174
Gaurav Shah8bf29d82010-01-28 19:43:24 -0800175# Generate algorithm to message digest's output size map.
176echo "const int hash_size_map[NUMALGORITHMS] = {"
177for rsaalgo in ${RSAAlgos[@]}
178do
179 for hashalgo in ${HashAlgos[@]}
180 do
181 echo ${hashalgo}_DIGEST_SIZE,
182 done
183done
184echo "};"
185echo
186
187# Generate algorithm to message digest's input block size map.
Gaurav Shah322536d2010-01-28 15:01:23 -0800188echo "const int hash_blocksize_map[NUMALGORITHMS] = {"
189for rsaalgo in ${RSAAlgos[@]}
190do
191 for hashalgo in ${HashAlgos[@]}
192 do
193 echo ${hashalgo}_BLOCK_SIZE,
194 done
195done
196echo "};"
197echo
198
Gaurav Shah8bf29d82010-01-28 19:43:24 -0800199# Generate algorithm to message's digest ASN.1 DigestInfo map.
200echo "const uint8_t* hash_digestinfo_map[NUMALGORITHMS] = {"
201for rsaalgo in ${RSAAlgos[@]}
202do
203 for hashalgo in ${HashAlgos[@]}
204 do
205 echo ${hashalgo}_digestinfo,
206 done
207done
208echo "};"
209echo
210
211
Gaurav Shah322536d2010-01-28 15:01:23 -0800212# Generate algorithm description strings.
213echo "const char* algo_strings[NUMALGORITHMS] = {"
214for rsaalgo in ${RSAAlgos[@]}
215do
216 for hashalgo in ${HashAlgos[@]}
217 do
218 echo \"${rsaalgo} ${hashalgo}\",
219 done
220done
221echo "};"
222echo
223
224#echo "#endif /* VBOOT_REFERENCE_PADDING_H_ */"