imgtool: fix getpriv format type for keys

A previous change was added to allow the `getpriv` command to dump ec256
keys in both openssl and pkcs8. That PR did not touch other key file
types which resulted in errors using that command with RSA, X25519, etc.

This commit generalizes the passing of the `format` parameter, so each
key type can decide which format it allows a dump to be produced in,
and what default to use.

Fixes #1529

Signed-off-by: Fabio Utzig <utzig@apache.org>
diff --git a/scripts/imgtool/keys/x25519.py b/scripts/imgtool/keys/x25519.py
index 1e0aadb..a99cf18 100644
--- a/scripts/imgtool/keys/x25519.py
+++ b/scripts/imgtool/keys/x25519.py
@@ -9,6 +9,7 @@
 from cryptography.hazmat.primitives.asymmetric import x25519
 
 from .general import KeyClass
+from .privatebytes import PrivateBytesMixin
 
 
 class X25519UsageError(Exception):
@@ -39,7 +40,7 @@
                 encoding=serialization.Encoding.PEM,
                 format=serialization.PublicFormat.SubjectPublicKeyInfo)
 
-    def get_private_bytes(self, minimal):
+    def get_private_bytes(self, minimal, format):
         self._unsupported('get_private_bytes')
 
     def export_private(self, path, passwd=None):
@@ -63,7 +64,7 @@
         return 32
 
 
-class X25519(X25519Public):
+class X25519(X25519Public, PrivateBytesMixin):
     """
     Wrapper around an X25519 private key.
     """
@@ -80,11 +81,15 @@
     def _get_public(self):
         return self.key.public_key()
 
-    def get_private_bytes(self, minimal):
-        return self.key.private_bytes(
-            encoding=serialization.Encoding.DER,
-            format=serialization.PrivateFormat.PKCS8,
-            encryption_algorithm=serialization.NoEncryption())
+    _VALID_FORMATS = {
+        'pkcs8': serialization.PrivateFormat.PKCS8
+    }
+    _DEFAULT_FORMAT = 'pkcs8'
+
+    def get_private_bytes(self, minimal, format):
+        _, priv = self._get_private_bytes(minimal, format,
+                                          X25519UsageError)
+        return priv
 
     def export_private(self, path, passwd=None):
         """