1

I have a task to get modulus and exponent of a given key and insert this as a #define in my STM32 firmware.

I've done this previously using a DER key and this forum helped me a lot. (Embedded a .der file into C code using makefile)

What I need is actually to create something like:

#define MODULUS { 0x00, 0xff ..... }
#define EXPONENT { 0x........}

I need to use modulus and exponent because that is what X-CUBE-CryptoLib for STM32 requires, instead of a DER KEY.

I'm trying to figure it out using this command:

$(shell openssl rsa  -inform DER -text -noout < privatekey.der |  awk 'BEGIN { printf "#ifndef _DER_H_\n#define _DER_H_\n/* Auto generated file. Do not modify! */\n#define DER_KEY  {" } { for(i=1; i<=NF; ++i) printf " 0x" $$i ","; print "\\" } END { print "};\n#endif" }' > Core/Inc/der.h)

But the command above prints a bunch of info that I won't use, such as public exponent and some prime exponents. Also the whole field identification ("modulus:..." "private exponent:..." makes it complicated to handle this with the single awk loop I have.

Could anyone give me a hint on how I could get this task done?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • 3
    You're starting from the wrong end. Start with the `openssl` command, verify that it produces the numbers you want (among other things). Then pipe the output through something like `sed`, to remove the unwanted fields. Then have Make call that command, and store the result in a variable or two. *Then* try to have Make embed those values in something else. – Beta Jul 12 '21 at 15:40

0 Answers0