blob: 335f291ebce584219ee43dc9d1545c6e75babb59 [file] [log] [blame]
rjw1f884582022-01-06 17:20:42 +08001#!/bin/bash
2
3#
4# Copyright (C) 2015 MediaTek Inc. All rights reserved.
5# Tristan Shieh <tristan.shieh@mediatek.com>
6#
7# Redistribution and use in source and binary forms, with or without
8# modification, are permitted provided that the following conditions
9# are met:
10# 1. Redistributions of source code must retain the above copyright
11# notice, this list of conditions and the following disclaimer.
12# 2. Redistributions in binary form must reproduce the above copyright
13# notice, this list of conditions and the following disclaimer in the
14# documentation and/or other materials provided with the distribution.
15#
16# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26# SUCH DAMAGE.
27#
28
29if [ $# -lt 2 -o $# -gt 3 ]
30then
31 echo "Usage: $0 private-key.pem filename [pss]"
32 exit -1
33fi
34
35TMPFILE1=$(mktemp)
36TMPFILE2=$(mktemp)
37
38python -c "
39import hashlib
40
41f = open('$2', 'rb')
42b = f.read()
43f.close()
44
45d = hashlib.sha256(b).digest()
46if '$3' != 'pss':
47 b = '\0\0'
48 for i in range(0, len(d), 2):
49 b += d[i + 1] + d[i]
50 b += '\0' * 222
51else:
52 b = d
53
54f = open('${TMPFILE1}', 'wb')
55f.write(b)
56f.close()
57"
58
59if [ -z "$3" -o "$3" != "pss" ]; then
60openssl rsautl -sign -inkey $1 -raw -in ${TMPFILE1} -out ${TMPFILE2}
61else
62openssl pkeyutl -sign -inkey $1 -in ${TMPFILE1} -out ${TMPFILE2} -pkeyopt digest:sha256 -pkeyopt rsa_padding_mode:pss -pkeyopt rsa_pss_saltlen:32
63fi
64RET=$?
65
66python -c "
67f = open('${TMPFILE2}', 'rb')
68d = f.read()
69f.close()
70b = ''
71if '$3' != 'pss':
72 for i in range(0, len(d), 2):
73 b += d[i + 1] + d[i]
74else:
75 b = d;
76f = open('$2.sign', 'wb')
77f.write(b)
78f.close()
79"
80
81if [ "$3" = "pss" ]; then
82 echo "Signature file $2.sign with pss padding is generated"
83else
84 echo "Signature file $2.sign with legacy padding is generated"
85fi
86
87rm -f ${TMPFILE1} ${TMPFILE2}
88exit ${RET}