blob: 2f7070cb622b60858f08c003a5714ae1384184a5 [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 [ $# -ne 2 ]
30then
31 echo "Usage: $0 private-key.pem filename"
32 exit -1
33fi
34
35TMPFILE1=$(mktemp)
36TMPFILE2=$(mktemp)
37
38python -c "
39import hashlib
40import struct
41import os
42
43file_len = os.path.getsize('$2')
44b = struct.pack('16I', \
45 0x53535353, 0x54535543, 0x00000000, 0x00000000, \
46 0x00000000, 0x00000000, 0x00000000, 0x00000000, \
47 0x00000000, 0x00000001, file_len, 0x00000040, \
48 0x00000000, file_len, file_len+0x40, 0x00000094, )
49
50f = open('$2', 'rb')
51b += f.read()
52f.close()
53
54d = hashlib.sha1(b).digest()
55
56f = open('${TMPFILE1}', 'wb')
57f.write(d)
58f.close()
59"
60
61openssl rsautl -sign -inkey $1 -in ${TMPFILE1} -out ${TMPFILE2}
62RET=$?
63
64python -c "
65import struct
66import os
67
68file_len = os.path.getsize('$2')
69b = struct.pack('16I', \
70 0x53535353, 0x54535543, 0x00000000, 0x00000000, \
71 0x00000000, 0x00000000, 0x00000000, 0x00000000, \
72 0x00000000, 0x00000001, file_len, 0x00000040, \
73 0x00000000, file_len, file_len+0x40, 0x00000094, )
74
75f = open('${TMPFILE2}', 'rb')
76b += f.read()
77f.close()
78
79f = open('${TMPFILE1}', 'rb')
80b += f.read(20)
81f.close()
82
83b += '\0' * 44
84
85f = open('$2.sign', 'wb')
86f.write(b)
87f.close()
88"
89
90rm -f ${TMPFILE1} ${TMPFILE2}
91exit ${RET}