rjw | 1f88458 | 2022-01-06 17:20:42 +0800 | [diff] [blame^] | 1 | #!/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 | |
| 29 | if [ $# -ne 2 ] |
| 30 | then |
| 31 | echo "Usage: $0 private-key.pem filename" |
| 32 | exit -1 |
| 33 | fi |
| 34 | |
| 35 | TMPFILE1=$(mktemp) |
| 36 | TMPFILE2=$(mktemp) |
| 37 | |
| 38 | python -c " |
| 39 | import hashlib |
| 40 | import struct |
| 41 | import os |
| 42 | |
| 43 | file_len = os.path.getsize('$2') |
| 44 | b = 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 | |
| 50 | f = open('$2', 'rb') |
| 51 | b += f.read() |
| 52 | f.close() |
| 53 | |
| 54 | d = hashlib.sha1(b).digest() |
| 55 | |
| 56 | f = open('${TMPFILE1}', 'wb') |
| 57 | f.write(d) |
| 58 | f.close() |
| 59 | " |
| 60 | |
| 61 | openssl rsautl -sign -inkey $1 -in ${TMPFILE1} -out ${TMPFILE2} |
| 62 | RET=$? |
| 63 | |
| 64 | python -c " |
| 65 | import struct |
| 66 | import os |
| 67 | |
| 68 | file_len = os.path.getsize('$2') |
| 69 | b = 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 | |
| 75 | f = open('${TMPFILE2}', 'rb') |
| 76 | b += f.read() |
| 77 | f.close() |
| 78 | |
| 79 | f = open('${TMPFILE1}', 'rb') |
| 80 | b += f.read(20) |
| 81 | f.close() |
| 82 | |
| 83 | b += '\0' * 44 |
| 84 | |
| 85 | f = open('$2.sign', 'wb') |
| 86 | f.write(b) |
| 87 | f.close() |
| 88 | " |
| 89 | |
| 90 | rm -f ${TMPFILE1} ${TMPFILE2} |
| 91 | exit ${RET} |