Expose the `EVP_BytesToKey` implementation at class level
Marco Ricci

Marco Ricci commited on 2025-01-19 21:10:38
Zeige 1 geänderte Dateien mit 32 Einfügungen und 23 Löschungen.


This implementation ought to appear in the documentation, which it
cannot if it stays an inner function.
... ...
@@ -548,28 +548,8 @@ class VaultNativeV02ConfigParser(VaultNativeConfigParser):
548 548
         """
549 549
         return base64.standard_b64encode(self._message)
550 550
 
551
-    def _make_decryptor(self) -> ciphers.CipherContext:
552
-        """Return the cipher context object used for decryption.
553
-
554
-        This is a standard AES256-CBC cipher context. The encryption key
555
-        and the IV are derived via the OpenSSL `EVP_BytesToKey` function
556
-        (using MD5, no salt, and one iteration).  This is what the
557
-        Node.js `crypto` library (v21 series and older) used in its
558
-        implementation of `crypto.createCipher("aes256", password)`.
559
-
560
-        Danger: Insecure use of cryptography
561
-            This function makes use of (an implementation of) the
562
-            OpenSSL function `EVP_BytesToKey`, which generates
563
-            cryptographically weak keys, without any attempts at
564
-            mitigating its insecurity.  We provide this function for the
565
-            purpose of interoperability with existing vault
566
-            installations.  Do not rely on this system to keep your
567
-            vault configuration secure against access by even moderately
568
-            determined attackers!
569
-
570
-        """
571
-
572
-        def evp_bytestokey_md5_one_iteration_no_salt(
551
+    @staticmethod
552
+    def _evp_bytestokey_md5_one_iteration_no_salt(
573 553
         data: bytes, key_size: int, iv_size: int
574 554
     ) -> tuple[bytes, bytes]:
575 555
         """Reimplement OpenSSL's `EVP_BytesToKey` with fixed parameters.
... ...
@@ -619,6 +600,15 @@ class VaultNativeV02ConfigParser(VaultNativeConfigParser):
619 600
             A 2-tuple containing the derived encryption key and the
620 601
             derived initialization vector.
621 602
 
603
+        Danger: Insecure use of cryptography
604
+            This function reimplements the OpenSSL function
605
+            `EVP_BytesToKey`, which generates cryptographically weak
606
+            keys, without any attempts at mitigating its insecurity.  We
607
+            provide this function for the purpose of interoperability
608
+            with existing vault installations.  Do not rely on this
609
+            system to keep your vault configuration secure against
610
+            access by even moderately determined attackers!
611
+
622 612
         """
623 613
         total_size = key_size + iv_size
624 614
         buffer = bytearray()
... ...
@@ -662,8 +652,28 @@ class VaultNativeV02ConfigParser(VaultNativeConfigParser):
662 652
         )
663 653
         return bytes(buffer[:key_size]), bytes(buffer[key_size:total_size])
664 654
 
655
+    def _make_decryptor(self) -> ciphers.CipherContext:
656
+        """Return the cipher context object used for decryption.
657
+
658
+        This is a standard AES256-CBC cipher context. The encryption key
659
+        and the IV are derived via the OpenSSL `EVP_BytesToKey` function
660
+        (using MD5, no salt, and one iteration).  This is what the
661
+        Node.js `crypto` library (v21 series and older) used in its
662
+        implementation of `crypto.createCipher("aes256", password)`.
663
+
664
+        Danger: Insecure use of cryptography
665
+            This function makes use of (an implementation of) the
666
+            OpenSSL function `EVP_BytesToKey`, which generates
667
+            cryptographically weak keys, without any attempts at
668
+            mitigating its insecurity.  We provide this function for the
669
+            purpose of interoperability with existing vault
670
+            installations.  Do not rely on this system to keep your
671
+            vault configuration secure against access by even moderately
672
+            determined attackers!
673
+
674
+        """
665 675
         data = base64.standard_b64encode(self._iv + self._encryption_key)
666
-        encryption_key, iv = evp_bytestokey_md5_one_iteration_no_salt(
676
+        encryption_key, iv = self._evp_bytestokey_md5_one_iteration_no_salt(
667 677
             data, key_size=32, iv_size=16
668 678
         )
669 679
         return ciphers.Cipher(
670 680