All of lore.kernel.org
 help / color / mirror / Atom feed
From: Jan Kiszka <jan.kiszka@siemens.com>
To: U-Boot Mailing List <u-boot@lists.denx.de>
Subject: [PATCH V2 03/13] tools: Add script for converting public key into device tree include
Date: Wed,  5 Oct 2022 10:33:43 +0200	[thread overview]
Message-ID: <35cfc657eb8cd864246227f4572fbb0166e994da.1664958832.git.jan.kiszka@siemens.com> (raw)
In-Reply-To: <cover.1664958832.git.jan.kiszka@siemens.com>

From: Jan Kiszka <jan.kiszka@siemens.com>

Allows to create a public key device tree dtsi for inclusion into U-Boot
SPL and proper during first build already. This can be achieved via
CONFIG_DEVICE_TREE_INCLUDES.

Signed-off-by: Jan Kiszka <jan.kiszka@siemens.com>
---
 tools/key2dtsi.py | 64 +++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 64 insertions(+)
 create mode 100755 tools/key2dtsi.py

diff --git a/tools/key2dtsi.py b/tools/key2dtsi.py
new file mode 100755
index 00000000000..1dbb2cc94bf
--- /dev/null
+++ b/tools/key2dtsi.py
@@ -0,0 +1,64 @@
+#!/usr/bin/env python3
+# SPDX-License-Identifier: GPL-2.0-only
+#
+# Public key to dtsi converter.
+#
+# Copyright (c) Siemens AG, 2022
+#
+
+from argparse import ArgumentParser, FileType
+from os.path import basename, splitext
+from Cryptodome.PublicKey import RSA
+from Cryptodome.Util.number import inverse
+
+def int_to_bytestr(n, length=None):
+    if not length:
+        length = (n.bit_length() + 7) // 8
+    byte_array = n.to_bytes(length, 'big')
+    return ' '.join(['{:02x}'.format(byte) for byte in byte_array])
+
+ap = ArgumentParser(description='Public key to dtsi converter')
+
+ap.add_argument('--hash', '-H', default='sha256',
+                help='hash to be used with key (default: sha256)')
+ap.add_argument('--required-conf', '-c', action='store_true',
+                help='mark key required for configuration')
+ap.add_argument('--required-image', '-i', action='store_true',
+                help='mark key required for image')
+ap.add_argument('--spl', '-s', action='store_true',
+                help='mark key for usage in SPL')
+ap.add_argument('key_file', metavar='KEY_FILE', type=FileType('r'),
+                help='key file (formats: X.509, PKCS#1, OpenSSH)')
+ap.add_argument('dtsi_file', metavar='DTSI_FILE', type=FileType('w'),
+                help='dtsi output file')
+
+args = ap.parse_args()
+
+key_name, _ = splitext(basename(args.key_file.name))
+
+key_data = args.key_file.read()
+key = RSA.importKey(key_data)
+
+r_squared = (2**key.size_in_bits())**2 % key.n
+n0_inverse = 2**32 - inverse(key.n, 2**32)
+
+out = args.dtsi_file
+out.write('/ {\n')
+out.write('\tsignature {\n')
+out.write('\t\tkey-{} {{\n'.format(key_name))
+out.write('\t\t\tkey-name-hint = "{}";\n'.format(key_name))
+out.write('\t\t\talgo = "{},rsa{}";\n'.format(args.hash, key.size_in_bits()))
+out.write('\t\t\trsa,num-bits = <{}>;\n'.format(key.size_in_bits()))
+out.write('\t\t\trsa,modulus = [{}];\n'.format(int_to_bytestr(key.n)))
+out.write('\t\t\trsa,exponent = [{}];\n'.format(int_to_bytestr(key.e, 8)))
+out.write('\t\t\trsa,r-squared = [{}];\n'.format(int_to_bytestr(r_squared)))
+out.write('\t\t\trsa,n0-inverse = <0x{:x}>;\n'.format(n0_inverse))
+if args.required_conf:
+    out.write('\t\t\trequired = "conf";\n')
+elif args.required_image:
+    out.write('\t\t\trequired = "image";\n')
+if args.spl:
+    out.write('\t\t\tu-boot,dm-spl;\n')
+out.write('\t\t};\n')
+out.write('\t};\n')
+out.write('};\n')
-- 
2.35.3


  parent reply	other threads:[~2022-10-05  8:34 UTC|newest]

Thread overview: 26+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-10-05  8:33 [PATCH V2 00/13] IOT2050-related enhancements Jan Kiszka
2022-10-05  8:33 ` [PATCH V2 01/13] env: Complete generic support for writable list Jan Kiszka
2022-10-27  7:49   ` Stefan Herbrechtsmeier
2022-10-27 12:38     ` Jan Kiszka
2022-10-28  8:34       ` Stefan Herbrechtsmeier
2022-10-05  8:33 ` [PATCH V2 02/13] env: Couple networking-related variable flags to CONFIG_NET Jan Kiszka
2022-10-05  8:33 ` Jan Kiszka [this message]
2022-10-05  8:33 ` [PATCH V2 04/13] board: siemens: iot2050: Split the build for PG1 and PG2 Jan Kiszka
2022-10-05  8:33 ` [PATCH V2 05/13] arm: dts: iot2050: Use the auto generator nodes for fdt Jan Kiszka
2022-10-05  8:33 ` [PATCH V2 06/13] iot2050: Update firmware layout Jan Kiszka
2022-10-05  8:33 ` [PATCH V2 07/13] iot2050: Add watchdog start to bootcmd Jan Kiszka
2022-10-05  8:33 ` [PATCH V2 08/13] iot2050: Add CONFIG_ENV_FLAGS_LIST_STATIC Jan Kiszka
2022-10-05  8:33 ` [PATCH V2 09/13] arm: dts: iot2050: Allow verifying U-Boot proper by SPL Jan Kiszka
2022-10-05  8:33 ` [PATCH V2 10/13] iot2050: Add script for signing artifacts Jan Kiszka
2022-10-05 15:58   ` Simon Glass
2022-10-05 16:04     ` Jan Kiszka
2022-10-05 16:16       ` Jan Kiszka
2022-10-05  8:33 ` [PATCH V2 11/13] arm: dts: iot2050: Optionally embed OTP programming data into image Jan Kiszka
2022-10-05  8:33 ` [PATCH V2 12/13] doc: iot2050: Add a note about the watchdog firmware Jan Kiszka
2022-10-05  8:33 ` [PATCH V2 13/13] board: siemens: iot2050: use the named gpio to control the user-button Jan Kiszka
2022-10-26  7:28 ` [PATCH V2 00/13] IOT2050-related enhancements Jan Kiszka
2022-10-26 13:54   ` Tom Rini
2022-10-26 14:03     ` Marek Vasut
2022-10-26 15:55       ` Jan Kiszka
2022-10-26 21:02         ` Marek Vasut
2022-10-27  4:32           ` Jan Kiszka

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=35cfc657eb8cd864246227f4572fbb0166e994da.1664958832.git.jan.kiszka@siemens.com \
    --to=jan.kiszka@siemens.com \
    --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.