scripts: imgtool: Add command to dump private keys

This applies a few improvements to a commit previously included in
PR #596:

* Move functions to dump a private key to the private key classes
* Remove language option; always dumps in C format
* Add option to generate a minimal dump. This will remove extra
  parameters that are present in keys generated with the `keygen`
  command.
  For P256 this will remove the public point, which is already
  ignored by the parsing function. The resulting key dump shrinks
  from 138 to 70 bytes.
  For RSA it will remove the DP/DQ/QP parameters which are only
  used with CRT enabled, and if not available, can be calculated at
  runtime. This reduces the size of a key dump from around 1190
  bytes to somewhere close to 800 bytes. A patch to the RSA parsing
  routine will be added in another commit.

Signed-off-by: Fabio Utzig <utzig@apache.org>
Signed-off-by: Ioannis Konstantelias <ikonstadel@gmail.com>
diff --git a/scripts/imgtool/keys/general.py b/scripts/imgtool/keys/general.py
index 3ba34cb..f6b8a09 100644
--- a/scripts/imgtool/keys/general.py
+++ b/scripts/imgtool/keys/general.py
@@ -5,11 +5,10 @@
 AUTOGEN_MESSAGE = "/* Autogenerated by imgtool.py, do not edit. */"
 
 class KeyClass(object):
-    def _public_emit(self, header, trailer, indent, file=sys.stdout, len_format=None):
+    def _emit(self, header, trailer, encoded_bytes, indent, file=sys.stdout, len_format=None):
         print(AUTOGEN_MESSAGE, file=file)
         print(header, end='', file=file)
-        encoded = self.get_public_bytes()
-        for count, b in enumerate(encoded):
+        for count, b in enumerate(encoded_bytes):
             if count % 8 == 0:
                 print("\n" + indent, end='', file=file)
             else:
@@ -17,19 +16,30 @@
             print("0x{:02x},".format(b), end='', file=file)
         print("\n" + trailer, file=file)
         if len_format is not None:
-            print(len_format.format(len(encoded)), file=file)
+            print(len_format.format(len(encoded_bytes)), file=file)
 
-    def emit_c(self, file=sys.stdout):
-        self._public_emit(
+    def emit_c_public(self, file=sys.stdout):
+        self._emit(
                 header="const unsigned char {}_pub_key[] = {{".format(self.shortname()),
                 trailer="};",
+                encoded_bytes=self.get_public_bytes(),
                 indent="    ",
                 len_format="const unsigned int {}_pub_key_len = {{}};".format(self.shortname()),
                 file=file)
 
-    def emit_rust(self, file=sys.stdout):
-        self._public_emit(
+    def emit_rust_public(self, file=sys.stdout):
+        self._emit(
                 header="static {}_PUB_KEY: &'static [u8] = &[".format(self.shortname().upper()),
                 trailer="];",
+                encoded_bytes=self.get_public_bytes(),
                 indent="    ",
                 file=file)
+
+    def emit_private(self, minimal, file=sys.stdout):
+        self._emit(
+                header="const unsigned char enc_priv_key[] = {",
+                trailer="};",
+                encoded_bytes=self.get_private_bytes(minimal),
+                indent="    ",
+                len_format="const unsigned int enc_priv_key_len = {};",
+                file=file)