summaryrefslogtreecommitdiff
path: root/pipermail/pycrypto/2014q2/000796.html
blob: 7e76eb0d1cd3d8a0dbcabb857ec77db54c03211d (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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML>
 <HEAD>
   <TITLE> [pycrypto] Verify DSA bytestring signature
   </TITLE>
   <LINK REL="Index" HREF="index.html" >
   <LINK REL="made" HREF="mailto:pycrypto%40lists.dlitz.net?Subject=Re%3A%20%5Bpycrypto%5D%20Verify%20DSA%20bytestring%20signature&In-Reply-To=%3CCAGfyce0fCJCuwudpMhZ4Sg4yfmSqy-eCAb4ojv5-K2tGj_wRgA%40mail.gmail.com%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="000795.html">
   <LINK REL="Next"  HREF="000797.html">
 </HEAD>
 <BODY BGCOLOR="#ffffff">
   <H1>[pycrypto] Verify DSA bytestring signature</H1>
    <B>Legrandin</B> 
    <A HREF="mailto:pycrypto%40lists.dlitz.net?Subject=Re%3A%20%5Bpycrypto%5D%20Verify%20DSA%20bytestring%20signature&In-Reply-To=%3CCAGfyce0fCJCuwudpMhZ4Sg4yfmSqy-eCAb4ojv5-K2tGj_wRgA%40mail.gmail.com%3E"
       TITLE="[pycrypto] Verify DSA bytestring signature">helderijs at gmail.com
       </A><BR>
    <I>Tue Apr  8 08:44:19 PDT 2014</I>
    <P><UL>
        <LI>Previous message: <A HREF="000795.html">[pycrypto] Verify DSA bytestring signature
</A></li>
        <LI>Next message: <A HREF="000797.html">[pycrypto] Linking Against Non-Sytem libgmp
</A></li>
         <LI> <B>Messages sorted by:</B> 
              <a href="date.html#796">[ date ]</a>
              <a href="thread.html#796">[ thread ]</a>
              <a href="subject.html#796">[ subject ]</a>
              <a href="author.html#796">[ author ]</a>
         </LI>
       </UL>
    <HR>  
<!--beginarticle-->
<PRE>I am glad I could help.

To be honest, I find that the DSA.verify() should be completely avoided. It
is a really primitive API.

The proposal I captured in the pull request below, accepts DER and pure big
endian and takes care of hashing internally (plus it allows for
Deterministic DSA, which is safer than standard DSA).

2014-04-08 13:45 GMT+02:00 Winston Weinert &lt;<A HREF="http://lists.dlitz.net/cgi-bin/mailman/listinfo/pycrypto">winston at ml1.net</A>&gt;:

&gt;<i> Thank you for your patience.
</I>&gt;<i>
</I>&gt;<i> Unbeknownst to me what I wanted is indeed the sha1 digest of the zipfile's
</I>&gt;<i> sha1 digest. (So the first suggestion you wrote is what I needed.) It
</I>&gt;<i> sounds funny, though is is how the library (the Sparkle Update Framework)
</I>&gt;<i> I'm writing tools for does it.
</I>&gt;<i>
</I>&gt;<i> Is it worthwhile for me to open a pull request enabling DSA.verify() to
</I>&gt;<i> accept a base64 or the ASN.1 DER bytestring instead of the (r, s) tuple?
</I>&gt;<i>
</I>&gt;<i> --
</I>&gt;<i> Winston
</I>&gt;<i>
</I>&gt;<i>
</I>&gt;<i> On Apr 7, 2014, at 6:50, Legrandin &lt;<A HREF="http://lists.dlitz.net/cgi-bin/mailman/listinfo/pycrypto">helderijs at gmail.com</A>&gt; wrote:
</I>&gt;<i>
</I>&gt;<i> The openssl code is using SHA-1 twice: once to create the digest of the
</I>&gt;<i> archive (dgst -sha1) and a second time when computing the DSA signature
</I>&gt;<i> (dgst -dss1).
</I>&gt;<i>
</I>&gt;<i> If your goal is to sign the hash, the Python code should actually read:
</I>&gt;<i>
</I>&gt;<i> &gt;&gt; return pubkey.verify(SHA1.new(zipfile_digest).digest(), signature)
</I>&gt;<i>
</I>&gt;<i> If your goal is to sign only the archive, the openssl code should be:
</I>&gt;<i>
</I>&gt;<i> &gt;&gt; | openssl dgst -dss1 -sign &quot;$DSA_PRIVKEY&quot;  &lt; &quot;$RELEASE_ARCHIVE&quot; \
</I>&gt;<i> &gt;&gt; | openssl enc -base64
</I>&gt;<i>
</I>&gt;<i> 2014-04-07 0:49 GMT+02:00 Winston Weinert &lt;<A HREF="http://lists.dlitz.net/cgi-bin/mailman/listinfo/pycrypto">winston at ml1.net</A>&gt;:
</I>&gt;<i>
</I>&gt;&gt;<i> The signature is created using the openssl(1) command-line tool like this:
</I>&gt;&gt;<i>
</I>&gt;&gt;<i> openssl dgst -sha1 -binary &lt; &quot;$RELEASE_ARCHIVE&quot; \
</I>&gt;&gt;<i> | openssl dgst -dss1 -sign &quot;$DSA_PRIVKEY&quot; \
</I>&gt;&gt;<i> | openssl enc -base64
</I>&gt;&gt;<i>
</I>&gt;&gt;<i> It verifies correctly using this command-line:
</I>&gt;&gt;<i>
</I>&gt;&gt;<i> echo &quot;$SIGNATURE&quot; | openssl enc -base64 -d &gt; /tmp/decoded_signature
</I>&gt;&gt;<i> openssl dgst -sha1 -binary &lt; &quot;$RELEASE_ARCHIVE&quot; &gt;
</I>&gt;&gt;<i> /tmp/release_archive_sha1
</I>&gt;&gt;<i> openssl dgst -dss1 -verify &quot;$DSA_PUBKEY&quot; -signature
</I>&gt;&gt;<i> /tmp/decoded_signature /tmp/release_archive_sha1
</I>&gt;&gt;<i>
</I>&gt;&gt;<i> After I wrote my email, I dug around for awhile. After a lot of research
</I>&gt;&gt;<i> I learned
</I>&gt;&gt;<i> about ASN.1 DER's usage in Dss-Sig-Value (
</I>&gt;&gt;<i> <A HREF="http://www.ietf.org/rfc/rfc2459.txt">http://www.ietf.org/rfc/rfc2459.txt</A>). I
</I>&gt;&gt;<i> wrote this code that appeared to decode my Base64 encoded signature
</I>&gt;&gt;<i> correctly (I
</I>&gt;&gt;<i> checked against <A HREF="http://lapo.it/asn1js/">http://lapo.it/asn1js/</A>):
</I>&gt;&gt;<i>
</I>&gt;&gt;<i> def decode_DSA_signature(signature):
</I>&gt;&gt;<i>     raw_signature = base64.b64decode(signature)
</I>&gt;&gt;<i>     der = DerSequence()
</I>&gt;&gt;<i>     der.decode(raw_signature)
</I>&gt;&gt;<i>     return (der[0], der[1])
</I>&gt;&gt;<i>
</I>&gt;&gt;<i> Unfortunately .verify() returns False on correctly verified signature and
</I>&gt;&gt;<i> hash
</I>&gt;&gt;<i> pairs. I am using this new function like so:
</I>&gt;&gt;<i>
</I>&gt;&gt;<i>
</I>&gt;&gt;<i> def validate(dsa_pubkey, signature, zipfile):
</I>&gt;&gt;<i>     with open(dsa_pubkey, 'rb') as f:
</I>&gt;&gt;<i>         pubkey = DSA.importKey(f.read())
</I>&gt;&gt;<i>     with open(zipfile, 'rb') as f:
</I>&gt;&gt;<i>         h = SHA1.new()
</I>&gt;&gt;<i>         h.update(f.read())
</I>&gt;&gt;<i>         zipfile_digest = h.digest()
</I>&gt;&gt;<i>     signature = decode_DSA_signature(signature)
</I>&gt;&gt;<i>
</I>&gt;&gt;<i>     return pubkey.verify(zipfile_digest, signature)
</I>&gt;&gt;<i>
</I>&gt;&gt;<i> Maybe there is a problem with PyCrypto DSA and my environment?
</I>&gt;&gt;<i> &gt;&gt;&gt; sys.version
</I>&gt;&gt;<i> '2.7.6 (default, Feb  7 2014, 12:51:34) \n[GCC 4.2.1 Compatible Apple
</I>&gt;&gt;<i> LLVM 5.0 (clang-500.2.79)]'
</I>&gt;&gt;<i>
</I>&gt;&gt;<i> For the time being I'm invoking openssl(1) for this task.
</I>&gt;&gt;<i>
</I>&gt;&gt;<i> Thank you for the reply!
</I>&gt;&gt;<i> Winston Weinert
</I>&gt;&gt;<i>
</I>&gt;&gt;<i>
</I>&gt;&gt;<i> On Apr 6, 2014, at 4:50, Legrandin &lt;<A HREF="http://lists.dlitz.net/cgi-bin/mailman/listinfo/pycrypto">helderijs at gmail.com</A>&gt; wrote:
</I>&gt;&gt;<i>
</I>&gt;&gt;<i> &gt; How was the signature created exactly?
</I>&gt;&gt;<i> &gt;
</I>&gt;&gt;<i> &gt; The .verify() method of a DSA object requires two integers, and there
</I>&gt;&gt;<i> are several ways to encode them into a bytestring. It's very hard to guess
</I>&gt;&gt;<i> the correct one for your case.
</I>&gt;&gt;<i> &gt;
</I>&gt;&gt;<i> &gt; FYI, there is a long standing pull request I created to add a saner DSA
</I>&gt;&gt;<i> API:
</I>&gt;&gt;<i> &gt;
</I>&gt;&gt;<i> &gt; <A HREF="https://github.com/dlitz/pycrypto/pull/53">https://github.com/dlitz/pycrypto/pull/53</A>
</I>&gt;&gt;<i> &gt;
</I>&gt;&gt;<i> &gt; The verification method accepts DER or big-endian encoded signatures.
</I>&gt;&gt;<i> &gt;
</I>&gt;&gt;<i> &gt;
</I>&gt;&gt;<i> &gt;
</I>&gt;&gt;<i> &gt; 2014-04-05 21:03 GMT+02:00 Winston Weinert &lt;<A HREF="http://lists.dlitz.net/cgi-bin/mailman/listinfo/pycrypto">winston at ml1.net</A>&gt;:
</I>&gt;&gt;<i> &gt; Hello,
</I>&gt;&gt;<i> &gt;
</I>&gt;&gt;<i> &gt; I noticed in Git there is a &quot;verify&quot; method on Crypto.PublicKey.DSA.
</I>&gt;&gt;<i> How do
</I>&gt;&gt;<i> &gt; I go about using this method? It wants a tuple, but unsure how to create
</I>&gt;&gt;<i> &gt; the appropriate tuple from my bytestring (which is decoded base64 text).
</I>&gt;&gt;<i> &gt; This is git revision 2d1aecd. The relevant code and error:
</I>&gt;&gt;<i> &gt;
</I>&gt;&gt;<i> &gt; Code:
</I>&gt;&gt;<i> &gt;
</I>&gt;&gt;<i> &gt; def validate(dsa_pubkey, signature, zipfile):
</I>&gt;&gt;<i> &gt;     with open(dsa_pubkey, 'rb') as f:
</I>&gt;&gt;<i> &gt;         pubkey = DSA.importKey(f.read())
</I>&gt;&gt;<i> &gt;     with open(zipfile, 'rb') as f:
</I>&gt;&gt;<i> &gt;         h = SHA1.new()
</I>&gt;&gt;<i> &gt;         h.update(f.read())
</I>&gt;&gt;<i> &gt;         zipfile_digest = h.digest()
</I>&gt;&gt;<i> &gt;     decoded_signature = base64.b64decode(signature)
</I>&gt;&gt;<i> &gt;
</I>&gt;&gt;<i> &gt;     return pubkey.verify(zipfile_digest, decoded_signature)
</I>&gt;&gt;<i> &gt;
</I>&gt;&gt;<i> &gt; Error:
</I>&gt;&gt;<i> &gt;
</I>&gt;&gt;<i> &gt; Traceback (most recent call last):
</I>&gt;&gt;<i> &gt;   File &quot;sparkle_tool.py&quot;, line 67, in &lt;module&gt;
</I>&gt;&gt;<i> &gt;     validate_files(appcast, dsa_pubkey)
</I>&gt;&gt;<i> &gt;   File &quot;sparkle_tool.py&quot;, line 55, in validate_files
</I>&gt;&gt;<i> &gt;     if validate(dsa_pubkey, signature, local_file):
</I>&gt;&gt;<i> &gt;   File &quot;sparkle_tool.py&quot;, line 33, in validate
</I>&gt;&gt;<i> &gt;     return pubkey.verify(zipfile_digest, decoded_signature)
</I>&gt;&gt;<i> &gt;   File
</I>&gt;&gt;<i> &quot;/home/winston/jobber/venv/local/lib/python2.7/site-packages/Crypto/PublicKey/DSA.py&quot;,
</I>&gt;&gt;<i> line 222, in verify
</I>&gt;&gt;<i> &gt;     return pubkey.pubkey.verify(self, M, signature)
</I>&gt;&gt;<i> &gt;   File
</I>&gt;&gt;<i> &quot;/home/winston/jobber/venv/local/lib/python2.7/site-packages/Crypto/PublicKey/pubkey.py&quot;,
</I>&gt;&gt;<i> line 126, in verify
</I>&gt;&gt;<i> &gt;     return self._verify(M, signature)
</I>&gt;&gt;<i> &gt;   File
</I>&gt;&gt;<i> &quot;/home/winston/jobber/venv/local/lib/python2.7/site-packages/Crypto/PublicKey/DSA.py&quot;,
</I>&gt;&gt;<i> line 240, in _verify
</I>&gt;&gt;<i> &gt;     (r, s) = sig
</I>&gt;&gt;<i> &gt; ValueError: too many values to unpack
</I>&gt;&gt;<i> &gt;
</I>&gt;&gt;<i> &gt; Thanks a bunch!
</I>&gt;&gt;<i> &gt; --
</I>&gt;&gt;<i> &gt; Winston Weinert
</I>&gt;&gt;<i> &gt; <A HREF="http://lists.dlitz.net/cgi-bin/mailman/listinfo/pycrypto">winston at ml1.net</A>
</I>&gt;&gt;<i> &gt; _______________________________________________
</I>&gt;&gt;<i> &gt; pycrypto mailing list
</I>&gt;&gt;<i> &gt; <A HREF="http://lists.dlitz.net/cgi-bin/mailman/listinfo/pycrypto">pycrypto at lists.dlitz.net</A>
</I>&gt;&gt;<i> &gt; <A HREF="http://lists.dlitz.net/cgi-bin/mailman/listinfo/pycrypto">http://lists.dlitz.net/cgi-bin/mailman/listinfo/pycrypto</A>
</I>&gt;&gt;<i> &gt;
</I>&gt;&gt;<i> &gt; _______________________________________________
</I>&gt;&gt;<i> &gt; pycrypto mailing list
</I>&gt;&gt;<i> &gt; <A HREF="http://lists.dlitz.net/cgi-bin/mailman/listinfo/pycrypto">pycrypto at lists.dlitz.net</A>
</I>&gt;&gt;<i> &gt; <A HREF="http://lists.dlitz.net/cgi-bin/mailman/listinfo/pycrypto">http://lists.dlitz.net/cgi-bin/mailman/listinfo/pycrypto</A>
</I>&gt;&gt;<i>
</I>&gt;&gt;<i> _______________________________________________
</I>&gt;&gt;<i> pycrypto mailing list
</I>&gt;&gt;<i> <A HREF="http://lists.dlitz.net/cgi-bin/mailman/listinfo/pycrypto">pycrypto at lists.dlitz.net</A>
</I>&gt;&gt;<i> <A HREF="http://lists.dlitz.net/cgi-bin/mailman/listinfo/pycrypto">http://lists.dlitz.net/cgi-bin/mailman/listinfo/pycrypto</A>
</I>&gt;&gt;<i>
</I>&gt;<i>
</I>&gt;<i> _______________________________________________
</I>&gt;<i> pycrypto mailing list
</I>&gt;<i> <A HREF="http://lists.dlitz.net/cgi-bin/mailman/listinfo/pycrypto">pycrypto at lists.dlitz.net</A>
</I>&gt;<i> <A HREF="http://lists.dlitz.net/cgi-bin/mailman/listinfo/pycrypto">http://lists.dlitz.net/cgi-bin/mailman/listinfo/pycrypto</A>
</I>&gt;<i>
</I>&gt;<i>
</I>&gt;<i>
</I>&gt;<i> _______________________________________________
</I>&gt;<i> pycrypto mailing list
</I>&gt;<i> <A HREF="http://lists.dlitz.net/cgi-bin/mailman/listinfo/pycrypto">pycrypto at lists.dlitz.net</A>
</I>&gt;<i> <A HREF="http://lists.dlitz.net/cgi-bin/mailman/listinfo/pycrypto">http://lists.dlitz.net/cgi-bin/mailman/listinfo/pycrypto</A>
</I>&gt;<i>
</I>&gt;<i>
</I>-------------- next part --------------
An HTML attachment was scrubbed...
URL: &lt;<A HREF="http://lists.dlitz.net/pipermail/pycrypto/attachments/20140408/670eecf2/attachment-0001.html">http://lists.dlitz.net/pipermail/pycrypto/attachments/20140408/670eecf2/attachment-0001.html</A>&gt;
</PRE>


<!--endarticle-->
    <HR>
    <P><UL>
        <!--threads-->
	<LI>Previous message: <A HREF="000795.html">[pycrypto] Verify DSA bytestring signature
</A></li>
	<LI>Next message: <A HREF="000797.html">[pycrypto] Linking Against Non-Sytem libgmp
</A></li>
         <LI> <B>Messages sorted by:</B> 
              <a href="date.html#796">[ date ]</a>
              <a href="thread.html#796">[ thread ]</a>
              <a href="subject.html#796">[ subject ]</a>
              <a href="author.html#796">[ 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>