blob: e51e457c230700533fdf0571ff909c8d88a901e4 [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
12SHA1_Suffix="0x30,0x21,0x30,0x09,0x06,0x05,0x2b,0x0e,0x03,0x02,0x1a,0x05"\
13",0x00,0x04,0x14"
14SHA256_Suffix="0x30,0x31,0x30,0x0d,0x06,0x09,0x60,0x86,0x48,0x01,0x65,0x03"\
15",0x04,0x02,0x01,0x05,0x00,0x04,0x20"
16SHA512_Suffix="0x30,0x51,0x30,0x0d,0x06,0x09,0x60,0x86,0x48,0x01,0x65,0x03"\
17",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,"
98 eval suffix=\$${hashalgo}_Suffix
99 echo $suffix
100 echo "};"
101 echo
102 done
103done
104
105echo "const int kNumAlgorithms = $algorithmcounter;";
106echo "#define NUMALGORITHMS $algorithmcounter"
107echo
108
109# Generate algorithm signature length map
110echo "const int siglen_map[NUMALGORITHMS] = {"
111for rsaalgo in ${RSAAlgos[@]}
112do
113 for hashalgo in ${HashAlgos[@]}
114 do
115 echo ${rsaalgo}NUMWORDS,
116 done
117done
118echo "};"
119echo
120
121# Generate algorithm padding array map
122echo "const uint8_t* padding_map[NUMALGORITHMS] = {"
123for rsaalgo in ${RSAAlgos[@]}
124do
125 for hashalgo in ${HashAlgos[@]}
126 do
127 echo padding${rsaalgo}_${hashalgo},
128 done
129done
130echo "};"
131echo
132
133# Generate algorithm padding size map
134echo "const int padding_size_map[NUMALGORITHMS] = {"
135for rsaalgo in ${RSAAlgos[@]}
136do
137 for hashalgo in ${HashAlgos[@]}
138 do
139 echo ${rsaalgo}NUMBYTES - ${hashalgo}_DIGEST_SIZE,
140 done
141done
142echo "};"
143echo
144
145# Generate algorithm message digest's input block size.
146echo "const int hash_blocksize_map[NUMALGORITHMS] = {"
147for rsaalgo in ${RSAAlgos[@]}
148do
149 for hashalgo in ${HashAlgos[@]}
150 do
151 echo ${hashalgo}_BLOCK_SIZE,
152 done
153done
154echo "};"
155echo
156
157# Generate algorithm description strings.
158echo "const char* algo_strings[NUMALGORITHMS] = {"
159for rsaalgo in ${RSAAlgos[@]}
160do
161 for hashalgo in ${HashAlgos[@]}
162 do
163 echo \"${rsaalgo} ${hashalgo}\",
164 done
165done
166echo "};"
167echo
168
169#echo "#endif /* VBOOT_REFERENCE_PADDING_H_ */"