summaryrefslogtreecommitdiff
path: root/pipermail/pycrypto/2015/000848.html
blob: 0d5b5327877fcd025102c93948dc43c881c9174b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML>
 <HEAD>
   <TITLE> [pycrypto] [PATCH] PEM: Decode AES-192-CBC and AES-256-CBC keys
   </TITLE>
   <LINK REL="Index" HREF="index.html" >
   <LINK REL="made" HREF="mailto:pycrypto%40lists.dlitz.net?Subject=Re%3A%20%5Bpycrypto%5D%20%5BPATCH%5D%20PEM%3A%20Decode%20AES-192-CBC%20and%20AES-256-CBC%20keys&In-Reply-To=%3C20150810140959.505F989282%40agraf.me%3E">
   <META NAME="robots" CONTENT="index,nofollow">
   <style type="text/css">
       pre {
           white-space: pre-wrap;       /* css-2.1, curent FF, Opera, Safari */
           }
   </style>
   <META http-equiv="Content-Type" content="text/html; charset=us-ascii">
   <LINK REL="Previous"  HREF="000847.html">
   <LINK REL="Next"  HREF="000849.html">
 </HEAD>
 <BODY BGCOLOR="#ffffff">
   <H1>[pycrypto] [PATCH] PEM: Decode AES-192-CBC and AES-256-CBC keys</H1>
    <B>Alexander Graf</B> 
    <A HREF="mailto:pycrypto%40lists.dlitz.net?Subject=Re%3A%20%5Bpycrypto%5D%20%5BPATCH%5D%20PEM%3A%20Decode%20AES-192-CBC%20and%20AES-256-CBC%20keys&In-Reply-To=%3C20150810140959.505F989282%40agraf.me%3E"
       TITLE="[pycrypto] [PATCH] PEM: Decode AES-192-CBC and AES-256-CBC keys">mail at agraf.me
       </A><BR>
    <I>Mon Aug 10 06:47:47 PDT 2015</I>
    <P><UL>
        <LI>Previous message: <A HREF="000847.html">[pycrypto] pycrypto Digest, Vol 75, Issue 2
</A></li>
        <LI>Next message: <A HREF="000849.html">[pycrypto] Documentation error in	&quot;https://www.dlitz.net/software/pycrypto/api/current/&quot;
</A></li>
         <LI> <B>Messages sorted by:</B> 
              <a href="date.html#848">[ date ]</a>
              <a href="thread.html#848">[ thread ]</a>
              <a href="subject.html#848">[ subject ]</a>
              <a href="author.html#848">[ author ]</a>
         </LI>
       </UL>
    <HR>  
<!--beginarticle-->
<PRE>Adds support for AES-192-CBC and AES-256-CBC ciphers to the PEM decode()
function. This also adds a deriveKey() function which stretches the
password to a key as required for decrypting, replacing PBKDF1 for 192
and 256 bits.
---
 lib/Crypto/IO/PEM.py | 27 +++++++++++++++++++++++++++
 1 file changed, 27 insertions(+)

diff --git a/lib/Crypto/IO/PEM.py b/lib/Crypto/IO/PEM.py
index 89a5689..11dd782 100644
--- a/lib/Crypto/IO/PEM.py
+++ b/lib/Crypto/IO/PEM.py
@@ -110,6 +110,27 @@ def decode(pem_data, passphrase=None):
       been provided or if the passphrase is incorrect.
     &quot;&quot;&quot;
 
+    def deriveKey(password, salt, keySize):
+        &quot;&quot;&quot;Stretch the password to a key as required for decrypting.
+
+        This algorithm was derived from the Go source, which itself derived it
+        from the OpenSSL source.
+
+        <A HREF="https://golang.org/src/crypto/x509/pem_decrypt.go">https://golang.org/src/crypto/x509/pem_decrypt.go</A>
+        &quot;&quot;&quot;
+        out = b''
+        digest = b''
+        i = 0
+        while i &lt; keySize:
+            md5 = MD5.new()
+            md5.update(digest)
+            md5.update(tobytes(password))
+            md5.update(salt)
+            digest = md5.digest()
+            out = out + digest
+            i = i + len(digest)
+        return out[:keySize]
+
     # Verify Pre-Encapsulation Boundary
     r = re.compile(&quot;\s*-----BEGIN (.*)-----\n&quot;)
     m = r.match(pem_data)
@@ -147,6 +168,12 @@ def decode(pem_data, passphrase=None):
         elif algo == &quot;AES-128-CBC&quot;:
             key = PBKDF1(passphrase, salt[:8], 16, 1, MD5)
             objdec = AES.new(key, AES.MODE_CBC, salt)
+        elif algo == &quot;AES-192-CBC&quot;:
+            key = deriveKey(passphrase, salt[:8], 24)
+            objdec = AES.new(key, AES.MODE_CBC, salt)
+        elif algo == &quot;AES-256-CBC&quot;:
+            key = deriveKey(passphrase, salt[:8], 32)
+            objdec = AES.new(key, AES.MODE_CBC, salt)
         else:
             raise ValueError(&quot;Unsupport PEM encryption algorithm.&quot;)
         lines = lines[2:]
-- 
2.1.4

</PRE>


<!--endarticle-->
    <HR>
    <P><UL>
        <!--threads-->
	<LI>Previous message: <A HREF="000847.html">[pycrypto] pycrypto Digest, Vol 75, Issue 2
</A></li>
	<LI>Next message: <A HREF="000849.html">[pycrypto] Documentation error in	&quot;https://www.dlitz.net/software/pycrypto/api/current/&quot;
</A></li>
         <LI> <B>Messages sorted by:</B> 
              <a href="date.html#848">[ date ]</a>
              <a href="thread.html#848">[ thread ]</a>
              <a href="subject.html#848">[ subject ]</a>
              <a href="author.html#848">[ author ]</a>
         </LI>
       </UL>

<hr>
<a href="http://lists.dlitz.net/cgi-bin/mailman/listinfo/pycrypto">More information about the pycrypto
mailing list</a><br>
</body></html>