All of lore.kernel.org
 help / color / mirror / Atom feed
From: Philippe Reynes <philippe.reynes@softathome.com>
To: sjg@chromium.org, mr.nuke.me@gmail.com, joel.peshkin@broadcom.com
Cc: u-boot@lists.denx.de, Philippe Reynes <philippe.reynes@softathome.com>
Subject: [RFC PATCH v3 8/8] tools: gen_pre_load_header.sh: initial import
Date: Wed, 17 Nov 2021 18:52:15 +0100	[thread overview]
Message-ID: <20211117175215.24262-9-philippe.reynes@softathome.com> (raw)
In-Reply-To: <20211117175215.24262-1-philippe.reynes@softathome.com>

This commit adds a script gen_pre_load_header.sh
that generate the header used by the image pre-load
stage.

Signed-off-by: Philippe Reynes <philippe.reynes@softathome.com>
---
 tools/gen_pre_load_header.sh | 174 +++++++++++++++++++++++++++++++++++
 1 file changed, 174 insertions(+)
 create mode 100755 tools/gen_pre_load_header.sh

diff --git a/tools/gen_pre_load_header.sh b/tools/gen_pre_load_header.sh
new file mode 100755
index 0000000000..8256fa80ee
--- /dev/null
+++ b/tools/gen_pre_load_header.sh
@@ -0,0 +1,174 @@
+#!/bin/bash
+# SPDX-License-Identifier: GPL-2.0+
+
+#
+# default value
+#
+size='4096'
+algo='sha256,rsa2048'
+padding='pkcs-1.5'
+key=''
+verbose='false'
+input=''
+output=''
+
+usage() {
+	printf "Usage: $0 -a <algo> -k <key> [-p <padding>] [-s <size>] [-v] -i <input> -o <output>\n"
+}
+
+#
+# parse arguments
+#
+while getopts 'a:hi:k:o:p:s:v' flag; do
+	case "${flag}" in
+		a) algo="${OPTARG}" ;;
+		h) usage
+		   exit 0 ;;
+		i) input="${OPTARG}" ;;
+		k) key="${OPTARG}" ;;
+		o) output="${OPTARG}" ;;
+		p) padding="${OPTARG}" ;;
+		s) size="${OPTARG}" ;;
+		v) verbose='true' ;;
+		*) usage
+		   exit 1 ;;
+	esac
+done
+
+#
+# check that mandatory arguments are provided
+#
+if [ -z "$key" -o -z "$input" -o -z "$output" ]
+then
+	usage
+	exit 0
+fi
+
+hash=$(echo $algo | cut -d',' -f1)
+sign=$(echo $algo | cut -d',' -f2)
+
+echo "status:"
+echo "size    = $size"
+echo "algo    = $algo"
+echo "hash    = $hash"
+echo "sign    = $sign"
+echo "padding = $padding"
+echo "key     = $key"
+echo "verbose = $verbose"
+
+#
+# check if input file exist
+#
+if [ ! -f "$input" ]
+then
+	echo "Error: file '$input' doesn't exist"
+	exit 1
+fi
+
+#
+# check if output is not empty
+#
+if [ -z "$output" ]
+then
+	echo "Error: output is empty"
+	exit 1
+fi
+
+#
+# check that size is bigger than 0
+#
+if [ $size -le 0 ]
+then
+	echo "Error: $size lower than 0"
+	exit 1
+fi
+
+#
+# check if the key file exist
+#
+if [ ! -f "$key" ]
+then
+	echo "Error: file $key doesn't exist\n"
+	exit 1
+fi
+
+#
+# check if the hash is valid and supported
+#
+print_supported_hash() {
+	echo "Supported hash:"
+	echo "- sha1"
+	echo "- sha256"
+	echo "- sha384"
+	echo "- sha512"
+}
+
+case "$hash" in
+	"sha1") hashOption="-sha1" ;;
+	"sha256") hashOption="-sha256" ;;
+	"sha384") hashOption="-sha384" ;;
+	"sha512") hashOption="-sha512" ;;
+	*) echo "Error: $hash is an invalid hash"
+	   print_supported_hash
+	   exit 1;;
+esac
+
+#
+# check if the sign is valid and supported
+#
+print_supported_sign() {
+	echo "Supported sign:"
+	echo "- rsa1024"
+	echo "- rsa2048"
+	echo "- rsa4096"
+}
+
+case "$sign" in
+	"rsa1024") ;;
+	"rsa2048") ;;
+	"rsa4096") ;;
+	*) echo "Error: $sign is an invalid signature type"
+	   print_supported_sign
+	   exit 1;;
+esac
+
+#
+# check if the padding is valid and supported
+#
+print_supported_padding() {
+	echo "Supported padding:"
+	echo "- pkcs-1.5"
+	echo "- pss"
+}
+
+case "$padding" in
+	"pkcs-1.5") optionPadding='' ;;
+	"pss") optionPadding='-sigopt rsa_padding_mode:pss -sigopt rsa_pss_saltlen:-2' ;;
+	*) echo "Error: $padding is an invalid padding"
+	   print_supported_padding
+	   exit 1;;
+esac
+
+
+#
+# generate the sigature
+#
+sig=$(openssl dgst $optionHash -sign $key $optionPadding $input | xxd -p)
+
+#
+# generate the header
+#
+# 0 = magic
+# 4 = image size
+# 8 = signature
+#
+h=$(printf "%08x" 0x55425348)
+i=$(stat --printf="%s" $input)
+i=$(printf "%08x" $i)
+
+echo "$h$i$sig" | xxd -r -p > $output
+
+#
+# fill the header with '\0'  to reach the expected size
+#
+truncate -s $size $output
-- 
2.17.1


  parent reply	other threads:[~2021-11-17 17:54 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-11-17 17:52 [RFC PATCH v3 0/8] image: add a stage pre-load Philippe Reynes
2021-11-17 17:52 ` [RFC PATCH v3 1/8] lib: allow to build asn1 decoder and oid registry in SPL Philippe Reynes
2021-11-25  0:12   ` Simon Glass
2021-11-17 17:52 ` [RFC PATCH v3 2/8] lib: crypto: allow to build crypyo " Philippe Reynes
2021-11-25  0:12   ` Simon Glass
2021-11-17 17:52 ` [RFC PATCH v3 3/8] lib: rsa: allow rsa verify with pkey " Philippe Reynes
2021-11-25  0:12   ` Simon Glass
2021-11-17 17:52 ` [RFC PATCH v3 4/8] boot: image: add a stage pre-load Philippe Reynes
2021-11-25  0:12   ` Simon Glass
2021-11-17 17:52 ` [RFC PATCH v3 5/8] cmd: bootm: " Philippe Reynes
2021-11-25  0:12   ` Simon Glass
2021-11-17 17:52 ` [RFC PATCH v3 6/8] common: spl: fit_ram: allow to use image pre load Philippe Reynes
2021-11-25  0:12   ` Simon Glass
2021-11-17 17:52 ` [RFC PATCH v3 7/8] mkimage: add public key for image pre-load stage Philippe Reynes
2021-11-25  0:13   ` Simon Glass
2021-12-03 10:29     ` Philippe REYNES
2021-11-17 17:52 ` Philippe Reynes [this message]
2021-11-25  0:13   ` [RFC PATCH v3 8/8] tools: gen_pre_load_header.sh: initial import Simon Glass
2021-12-06  8:23   ` Rasmus Villemoes
2021-12-08 18:10     ` Philippe REYNES
2021-12-09  1:04       ` Rasmus Villemoes
2021-12-10  0:14       ` Simon Glass
2021-12-10  7:41         ` Rasmus Villemoes
2021-12-11 11:37           ` Simon Glass
2021-11-25  0:13 ` [RFC PATCH v3 0/8] image: add a stage pre-load Simon Glass

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20211117175215.24262-9-philippe.reynes@softathome.com \
    --to=philippe.reynes@softathome.com \
    --cc=joel.peshkin@broadcom.com \
    --cc=mr.nuke.me@gmail.com \
    --cc=sjg@chromium.org \
    --cc=u-boot@lists.denx.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.