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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
|
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import absolute_import, division, print_function
import os
import pytest
from cryptography.exceptions import (
AlreadyFinalized, InvalidSignature, _Reasons)
from cryptography.hazmat.primitives import hashes, interfaces
from cryptography.hazmat.primitives.asymmetric import dsa
from cryptography.utils import bit_length
from .fixtures_dsa import (
DSA_KEY_1024, DSA_KEY_2048, DSA_KEY_3072
)
from ...utils import (
der_encode_dsa_signature, load_fips_dsa_key_pair_vectors,
load_fips_dsa_sig_vectors, load_vectors_from_file,
raises_unsupported_algorithm
)
def _check_dsa_private_key(skey):
assert skey
assert skey.x
assert skey.y
assert skey.key_size
skey_parameters = skey.parameters()
assert skey_parameters
assert skey_parameters.modulus
assert skey_parameters.subgroup_order
assert skey_parameters.generator
assert skey_parameters.modulus == skey_parameters.p
assert skey_parameters.subgroup_order == skey_parameters.q
assert skey_parameters.generator == skey_parameters.g
pkey = skey.public_key()
assert pkey
assert skey.y == pkey.y
assert skey.key_size == pkey.key_size
pkey_parameters = pkey.parameters()
assert pkey_parameters
assert pkey_parameters.modulus
assert pkey_parameters.subgroup_order
assert pkey_parameters.generator
assert pkey_parameters.modulus == pkey_parameters.p
assert pkey_parameters.subgroup_order == pkey_parameters.q
assert pkey_parameters.generator == pkey_parameters.g
assert skey_parameters.modulus == pkey_parameters.modulus
assert skey_parameters.subgroup_order == pkey_parameters.subgroup_order
assert skey_parameters.generator == pkey_parameters.generator
@pytest.mark.dsa
class TestDSA(object):
def test_generate_dsa_parameters_class_method(self, backend):
parameters = dsa.DSAParameters.generate(1024, backend)
assert bit_length(parameters.p) == 1024
def test_generate_dsa_parameters(self, backend):
parameters = dsa.generate_parameters(1024, backend)
assert isinstance(parameters, interfaces.DSAParameters)
# TODO: withnumbers check like RSA
def test_generate_invalid_dsa_parameters(self, backend):
with pytest.raises(ValueError):
dsa.DSAParameters.generate(1, backend)
@pytest.mark.parametrize(
"vector",
load_vectors_from_file(
os.path.join(
"asymmetric", "DSA", "FIPS_186-3", "KeyPair.rsp"),
load_fips_dsa_key_pair_vectors
)
)
def test_generate_dsa_keys(self, vector, backend):
parameters = dsa.DSAParameterNumbers(
p=vector['p'],
q=vector['q'],
g=vector['g']
).parameters(backend)
skey = dsa.generate_private_key(parameters)
if isinstance(skey, interfaces.DSAPrivateKeyWithNumbers):
numbers = skey.private_numbers()
skey_parameters = numbers.public_numbers.parameter_numbers
pkey = skey.public_key()
parameters = pkey.parameters()
parameter_numbers = parameters.parameter_numbers()
assert parameter_numbers.p == skey_parameters.p
assert parameter_numbers.q == skey_parameters.q
assert parameter_numbers.g == skey_parameters.g
assert skey_parameters.p == vector['p']
assert skey_parameters.q == vector['q']
assert skey_parameters.g == vector['g']
assert skey.key_size == bit_length(vector['p'])
assert pkey.key_size == skey.key_size
public_numbers = pkey.public_numbers()
assert numbers.public_numbers.y == public_numbers.y
assert numbers.public_numbers.y == pow(
skey_parameters.g, numbers.x, skey_parameters.p
)
def test_invalid_parameters_argument_types(self):
with pytest.raises(TypeError):
dsa.DSAParameters(None, None, None)
def test_invalid_private_key_argument_types(self):
with pytest.raises(TypeError):
dsa.DSAPrivateKey(None, None, None, None, None)
def test_invalid_public_key_argument_types(self):
with pytest.raises(TypeError):
dsa.DSAPublicKey(None, None, None, None)
def test_load_dsa_example_keys(self):
parameters = dsa.DSAParameters(
modulus=DSA_KEY_1024.public_numbers.parameter_numbers.p,
subgroup_order=DSA_KEY_1024.public_numbers.parameter_numbers.q,
generator=DSA_KEY_1024.public_numbers.parameter_numbers.g
)
assert parameters
assert parameters.modulus
assert parameters.subgroup_order
assert parameters.generator
assert parameters.modulus == parameters.p
assert parameters.subgroup_order == parameters.q
assert parameters.generator == parameters.g
pub_key = dsa.DSAPublicKey(
modulus=DSA_KEY_1024.public_numbers.parameter_numbers.p,
subgroup_order=DSA_KEY_1024.public_numbers.parameter_numbers.q,
generator=DSA_KEY_1024.public_numbers.parameter_numbers.g,
y=DSA_KEY_1024.public_numbers.y
)
assert pub_key
assert pub_key.key_size
assert pub_key.y
pub_key_parameters = pub_key.parameters()
assert pub_key_parameters
assert pub_key_parameters.modulus
assert pub_key_parameters.subgroup_order
assert pub_key_parameters.generator
skey = dsa.DSAPrivateKey(
modulus=DSA_KEY_1024.public_numbers.parameter_numbers.p,
subgroup_order=DSA_KEY_1024.public_numbers.parameter_numbers.q,
generator=DSA_KEY_1024.public_numbers.parameter_numbers.g,
y=DSA_KEY_1024.public_numbers.y,
x=DSA_KEY_1024.x
)
assert skey
_check_dsa_private_key(skey)
skey_parameters = skey.parameters()
assert skey_parameters
assert skey_parameters.modulus
assert skey_parameters.subgroup_order
assert skey_parameters.generator
pkey = dsa.DSAPublicKey(
modulus=DSA_KEY_1024.public_numbers.parameter_numbers.p,
subgroup_order=DSA_KEY_1024.public_numbers.parameter_numbers.q,
generator=DSA_KEY_1024.public_numbers.parameter_numbers.g,
y=DSA_KEY_1024.public_numbers.y
)
assert pkey
pkey_parameters = pkey.parameters()
assert pkey_parameters
assert pkey_parameters.modulus
assert pkey_parameters.subgroup_order
assert pkey_parameters.generator
pkey2 = skey.public_key()
assert pkey2
pkey2_parameters = pkey.parameters()
assert pkey2_parameters
assert pkey2_parameters.modulus
assert pkey2_parameters.subgroup_order
assert pkey2_parameters.generator
assert skey_parameters.modulus == pkey_parameters.modulus
assert skey_parameters.subgroup_order == pkey_parameters.subgroup_order
assert skey_parameters.generator == pkey_parameters.generator
assert skey.y == pkey.y
assert skey.key_size == pkey.key_size
assert pkey_parameters.modulus == pkey2_parameters.modulus
assert pkey_parameters.subgroup_order == \
pkey2_parameters.subgroup_order
assert pkey_parameters.generator == pkey2_parameters.generator
assert pkey.y == pkey2.y
assert pkey.key_size == pkey2.key_size
def test_invalid_parameters_values(self):
# Test a modulus < 1024 bits in length
with pytest.raises(ValueError):
dsa.DSAParameters(
modulus=2 ** 1000,
subgroup_order=DSA_KEY_1024.public_numbers.parameter_numbers.q,
generator=DSA_KEY_1024.public_numbers.parameter_numbers.g,
)
# Test a modulus < 2048 bits in length
with pytest.raises(ValueError):
dsa.DSAParameters(
modulus=2 ** 2000,
subgroup_order=DSA_KEY_2048.public_numbers.parameter_numbers.q,
generator=DSA_KEY_2048.public_numbers.parameter_numbers.g,
)
# Test a modulus < 3072 bits in length
with pytest.raises(ValueError):
dsa.DSAParameters(
modulus=2 ** 3000,
subgroup_order=DSA_KEY_3072.public_numbers.parameter_numbers.q,
generator=DSA_KEY_3072.public_numbers.parameter_numbers.g,
)
# Test a modulus > 3072 bits in length
with pytest.raises(ValueError):
dsa.DSAParameters(
modulus=2 ** 3100,
subgroup_order=DSA_KEY_3072.public_numbers.parameter_numbers.q,
generator=DSA_KEY_3072.public_numbers.parameter_numbers.g,
)
# Test a subgroup_order < 160 bits in length
with pytest.raises(ValueError):
dsa.DSAParameters(
modulus=DSA_KEY_1024.public_numbers.parameter_numbers.p,
subgroup_order=2 ** 150,
generator=DSA_KEY_1024.public_numbers.parameter_numbers.g,
)
# Test a subgroup_order < 256 bits in length
with pytest.raises(ValueError):
dsa.DSAParameters(
modulus=DSA_KEY_2048.public_numbers.parameter_numbers.p,
subgroup_order=2 ** 250,
generator=DSA_KEY_2048.public_numbers.parameter_numbers.g
)
# Test a subgroup_order > 256 bits in length
with pytest.raises(ValueError):
dsa.DSAParameters(
modulus=DSA_KEY_3072.public_numbers.parameter_numbers.p,
subgroup_order=2 ** 260,
generator=DSA_KEY_3072.public_numbers.parameter_numbers.g,
)
# Test a modulus, subgroup_order pair of (1024, 256) bit lengths
with pytest.raises(ValueError):
dsa.DSAParameters(
modulus=DSA_KEY_1024.public_numbers.parameter_numbers.p,
subgroup_order=DSA_KEY_2048.public_numbers.parameter_numbers.q,
generator=DSA_KEY_1024.public_numbers.parameter_numbers.g,
)
# Test a modulus, subgroup_order pair of (2048, 160) bit lengths
with pytest.raises(ValueError):
dsa.DSAParameters(
modulus=DSA_KEY_2048.public_numbers.parameter_numbers.p,
subgroup_order=DSA_KEY_1024.public_numbers.parameter_numbers.q,
generator=DSA_KEY_2048.public_numbers.parameter_numbers.g
)
# Test a modulus, subgroup_order pair of (3072, 160) bit lengths
with pytest.raises(ValueError):
dsa.DSAParameters(
modulus=DSA_KEY_3072.public_numbers.parameter_numbers.p,
subgroup_order=DSA_KEY_1024.public_numbers.parameter_numbers.q,
generator=DSA_KEY_3072.public_numbers.parameter_numbers.g,
)
# Test a generator < 1
with pytest.raises(ValueError):
dsa.DSAParameters(
modulus=DSA_KEY_1024.public_numbers.parameter_numbers.p,
subgroup_order=DSA_KEY_1024.public_numbers.parameter_numbers.q,
generator=0
)
# Test a generator = 1
with pytest.raises(ValueError):
dsa.DSAParameters(
modulus=DSA_KEY_1024.public_numbers.parameter_numbers.p,
subgroup_order=DSA_KEY_1024.public_numbers.parameter_numbers.q,
generator=1
)
# Test a generator > modulus
with pytest.raises(ValueError):
dsa.DSAParameters(
modulus=DSA_KEY_1024.public_numbers.parameter_numbers.p,
subgroup_order=DSA_KEY_1024.public_numbers.parameter_numbers.q,
generator=2 ** 1200
)
def test_invalid_dsa_private_key_arguments(self):
# Test a modulus < 1024 bits in length
with pytest.raises(ValueError):
dsa.DSAPrivateKey(
modulus=2 ** 1000,
subgroup_order=DSA_KEY_1024.public_numbers.parameter_numbers.q,
generator=DSA_KEY_1024.public_numbers.parameter_numbers.g,
x=DSA_KEY_1024.x,
y=DSA_KEY_1024.public_numbers.y
)
# Test a modulus < 2048 bits in length
with pytest.raises(ValueError):
dsa.DSAPrivateKey(
modulus=2 ** 2000,
subgroup_order=DSA_KEY_2048.public_numbers.parameter_numbers.q,
generator=DSA_KEY_2048.public_numbers.parameter_numbers.g,
x=DSA_KEY_2048.x,
y=DSA_KEY_2048.public_numbers.y
)
# Test a modulus < 3072 bits in length
with pytest.raises(ValueError):
dsa.DSAPrivateKey(
modulus=2 ** 3000,
subgroup_order=DSA_KEY_3072.public_numbers.parameter_numbers.q,
generator=DSA_KEY_3072.public_numbers.parameter_numbers.g,
x=DSA_KEY_3072.x,
y=DSA_KEY_3072.public_numbers.y
)
# Test a modulus > 3072 bits in length
with pytest.raises(ValueError):
dsa.DSAPrivateKey(
modulus=2 ** 3100,
subgroup_order=DSA_KEY_3072.public_numbers.parameter_numbers.q,
generator=DSA_KEY_3072.public_numbers.parameter_numbers.g,
x=DSA_KEY_3072.x,
y=DSA_KEY_3072.public_numbers.y
)
# Test a subgroup_order < 160 bits in length
with pytest.raises(ValueError):
dsa.DSAPrivateKey(
modulus=DSA_KEY_1024.public_numbers.parameter_numbers.p,
subgroup_order=2 ** 150,
generator=DSA_KEY_1024.public_numbers.parameter_numbers.g,
x=DSA_KEY_1024.x,
y=DSA_KEY_1024.public_numbers.y
)
# Test a subgroup_order < 256 bits in length
with pytest.raises(ValueError):
dsa.DSAPrivateKey(
modulus=DSA_KEY_2048.public_numbers.parameter_numbers.p,
subgroup_order=2 ** 250,
generator=DSA_KEY_2048.public_numbers.parameter_numbers.g,
x=DSA_KEY_2048.x,
y=DSA_KEY_2048.public_numbers.y
)
# Test a subgroup_order > 256 bits in length
with pytest.raises(ValueError):
dsa.DSAPrivateKey(
modulus=DSA_KEY_3072.public_numbers.parameter_numbers.p,
subgroup_order=2 ** 260,
generator=DSA_KEY_3072.public_numbers.parameter_numbers.g,
x=DSA_KEY_3072.x,
y=DSA_KEY_3072.public_numbers.y
)
# Test a modulus, subgroup_order pair of (1024, 256) bit lengths
with pytest.raises(ValueError):
dsa.DSAPrivateKey(
modulus=DSA_KEY_1024.public_numbers.parameter_numbers.p,
subgroup_order=DSA_KEY_2048.public_numbers.parameter_numbers.q,
generator=DSA_KEY_1024.public_numbers.parameter_numbers.g,
x=DSA_KEY_1024.x,
y=DSA_KEY_1024.public_numbers.y
)
# Test a modulus, subgroup_order pair of (2048, 160) bit lengths
with pytest.raises(ValueError):
dsa.DSAPrivateKey(
modulus=DSA_KEY_2048.public_numbers.parameter_numbers.p,
subgroup_order=DSA_KEY_1024.public_numbers.parameter_numbers.q,
generator=DSA_KEY_2048.public_numbers.parameter_numbers.g,
x=DSA_KEY_2048.x,
y=DSA_KEY_2048.public_numbers.y
)
# Test a modulus, subgroup_order pair of (3072, 160) bit lengths
with pytest.raises(ValueError):
dsa.DSAPrivateKey(
modulus=DSA_KEY_3072.public_numbers.parameter_numbers.p,
subgroup_order=DSA_KEY_1024.public_numbers.parameter_numbers.q,
generator=DSA_KEY_3072.public_numbers.parameter_numbers.g,
x=DSA_KEY_3072.x,
y=DSA_KEY_3072.public_numbers.y
)
# Test a generator < 1
with pytest.raises(ValueError):
dsa.DSAPrivateKey(
modulus=DSA_KEY_1024.public_numbers.parameter_numbers.p,
subgroup_order=DSA_KEY_1024.public_numbers.parameter_numbers.q,
generator=0,
x=DSA_KEY_1024.x,
y=DSA_KEY_1024.public_numbers.y
)
# Test a generator = 1
with pytest.raises(ValueError):
dsa.DSAPrivateKey(
modulus=DSA_KEY_1024.public_numbers.parameter_numbers.p,
subgroup_order=DSA_KEY_1024.public_numbers.parameter_numbers.q,
generator=1,
x=DSA_KEY_1024.x,
y=DSA_KEY_1024.public_numbers.y
)
# Test a generator > modulus
with pytest.raises(ValueError):
dsa.DSAPrivateKey(
modulus=DSA_KEY_1024.public_numbers.parameter_numbers.p,
subgroup_order=DSA_KEY_1024.public_numbers.parameter_numbers.q,
generator=2 ** 1200,
x=DSA_KEY_1024.x,
y=DSA_KEY_1024.public_numbers.y
)
# Test x = 0
with pytest.raises(ValueError):
dsa.DSAPrivateKey(
modulus=DSA_KEY_1024.public_numbers.parameter_numbers.p,
subgroup_order=DSA_KEY_1024.public_numbers.parameter_numbers.q,
generator=DSA_KEY_1024.public_numbers.parameter_numbers.g,
x=0,
y=DSA_KEY_1024.public_numbers.y
)
# Test x < 0
with pytest.raises(ValueError):
dsa.DSAPrivateKey(
modulus=DSA_KEY_1024.public_numbers.parameter_numbers.p,
subgroup_order=DSA_KEY_1024.public_numbers.parameter_numbers.q,
generator=DSA_KEY_1024.public_numbers.parameter_numbers.g,
x=-2,
y=DSA_KEY_1024.public_numbers.y
)
# Test x = subgroup_order
with pytest.raises(ValueError):
dsa.DSAPrivateKey(
modulus=DSA_KEY_1024.public_numbers.parameter_numbers.p,
subgroup_order=DSA_KEY_1024.public_numbers.parameter_numbers.q,
generator=DSA_KEY_1024.public_numbers.parameter_numbers.g,
x=2 ** 159,
y=DSA_KEY_1024.public_numbers.y
)
# Test x > subgroup_order
with pytest.raises(ValueError):
dsa.DSAPrivateKey(
modulus=DSA_KEY_1024.public_numbers.parameter_numbers.p,
subgroup_order=DSA_KEY_1024.public_numbers.parameter_numbers.q,
generator=DSA_KEY_1024.public_numbers.parameter_numbers.g,
x=2 ** 200,
y=DSA_KEY_1024.public_numbers.y
)
# Test y != (generator ** x) % modulus
with pytest.raises(ValueError):
dsa.DSAPrivateKey(
modulus=DSA_KEY_1024.public_numbers.parameter_numbers.p,
subgroup_order=DSA_KEY_1024.public_numbers.parameter_numbers.q,
generator=DSA_KEY_1024.public_numbers.parameter_numbers.g,
x=DSA_KEY_1024.x,
y=2 ** 100
)
# Test a non-integer y value
with pytest.raises(TypeError):
dsa.DSAPrivateKey(
modulus=DSA_KEY_1024.public_numbers.parameter_numbers.p,
subgroup_order=DSA_KEY_1024.public_numbers.parameter_numbers.q,
generator=DSA_KEY_1024.public_numbers.parameter_numbers.g,
x=DSA_KEY_1024.x,
y=None
)
# Test a non-integer x value
with pytest.raises(TypeError):
dsa.DSAPrivateKey(
modulus=DSA_KEY_1024.public_numbers.parameter_numbers.p,
subgroup_order=DSA_KEY_1024.public_numbers.parameter_numbers.q,
generator=DSA_KEY_1024.public_numbers.parameter_numbers.g,
x=None,
y=DSA_KEY_1024.public_numbers.y
)
def test_invalid_dsa_public_key_arguments(self):
# Test a modulus < 1024 bits in length
with pytest.raises(ValueError):
dsa.DSAPublicKey(
modulus=2 ** 1000,
subgroup_order=DSA_KEY_1024.public_numbers.parameter_numbers.q,
generator=DSA_KEY_1024.public_numbers.parameter_numbers.g,
y=DSA_KEY_1024.public_numbers.y
)
# Test a modulus < 2048 bits in length
with pytest.raises(ValueError):
dsa.DSAPublicKey(
modulus=2 ** 2000,
subgroup_order=DSA_KEY_2048.public_numbers.parameter_numbers.q,
generator=DSA_KEY_2048.public_numbers.parameter_numbers.g,
y=DSA_KEY_2048.public_numbers.y
)
# Test a modulus < 3072 bits in length
with pytest.raises(ValueError):
dsa.DSAPublicKey(
modulus=2 ** 3000,
subgroup_order=DSA_KEY_3072.public_numbers.parameter_numbers.q,
generator=DSA_KEY_3072.public_numbers.parameter_numbers.g,
y=DSA_KEY_3072.public_numbers.y
)
# Test a modulus > 3072 bits in length
with pytest.raises(ValueError):
dsa.DSAPublicKey(
modulus=2 ** 3100,
subgroup_order=DSA_KEY_3072.public_numbers.parameter_numbers.q,
generator=DSA_KEY_3072.public_numbers.parameter_numbers.g,
y=DSA_KEY_3072.public_numbers.y
)
# Test a subgroup_order < 160 bits in length
with pytest.raises(ValueError):
dsa.DSAPublicKey(
modulus=DSA_KEY_1024.public_numbers.parameter_numbers.p,
subgroup_order=2 ** 150,
generator=DSA_KEY_1024.public_numbers.parameter_numbers.g,
y=DSA_KEY_1024.public_numbers.y
)
# Test a subgroup_order < 256 bits in length
with pytest.raises(ValueError):
dsa.DSAPublicKey(
modulus=DSA_KEY_2048.public_numbers.parameter_numbers.p,
subgroup_order=2 ** 250,
generator=DSA_KEY_2048.public_numbers.parameter_numbers.g,
y=DSA_KEY_2048.public_numbers.y
)
# Test a subgroup_order > 256 bits in length
with pytest.raises(ValueError):
dsa.DSAPublicKey(
modulus=DSA_KEY_3072.public_numbers.parameter_numbers.p,
subgroup_order=2 ** 260,
generator=DSA_KEY_3072.public_numbers.parameter_numbers.g,
y=DSA_KEY_3072.public_numbers.y
)
# Test a modulus, subgroup_order pair of (1024, 256) bit lengths
with pytest.raises(ValueError):
dsa.DSAPublicKey(
modulus=DSA_KEY_1024.public_numbers.parameter_numbers.p,
subgroup_order=DSA_KEY_2048.public_numbers.parameter_numbers.q,
generator=DSA_KEY_1024.public_numbers.parameter_numbers.g,
y=DSA_KEY_1024.public_numbers.y
)
# Test a modulus, subgroup_order pair of (2048, 160) bit lengths
with pytest.raises(ValueError):
dsa.DSAPublicKey(
modulus=DSA_KEY_2048.public_numbers.parameter_numbers.p,
subgroup_order=DSA_KEY_1024.public_numbers.parameter_numbers.q,
generator=DSA_KEY_2048.public_numbers.parameter_numbers.g,
y=DSA_KEY_2048.public_numbers.y
)
# Test a modulus, subgroup_order pair of (3072, 160) bit lengths
with pytest.raises(ValueError):
dsa.DSAPublicKey(
modulus=DSA_KEY_3072.public_numbers.parameter_numbers.p,
subgroup_order=DSA_KEY_1024.public_numbers.parameter_numbers.q,
generator=DSA_KEY_3072.public_numbers.parameter_numbers.g,
y=DSA_KEY_3072.public_numbers.y
)
# Test a generator < 1
with pytest.raises(ValueError):
dsa.DSAPublicKey(
modulus=DSA_KEY_1024.public_numbers.parameter_numbers.p,
subgroup_order=DSA_KEY_1024.public_numbers.parameter_numbers.q,
generator=0,
y=DSA_KEY_1024.public_numbers.y
)
# Test a generator = 1
with pytest.raises(ValueError):
dsa.DSAPublicKey(
modulus=DSA_KEY_1024.public_numbers.parameter_numbers.p,
subgroup_order=DSA_KEY_1024.public_numbers.parameter_numbers.q,
generator=1,
y=DSA_KEY_1024.public_numbers.y
)
# Test a generator > modulus
with pytest.raises(ValueError):
dsa.DSAPublicKey(
modulus=DSA_KEY_1024.public_numbers.parameter_numbers.p,
subgroup_order=DSA_KEY_1024.public_numbers.parameter_numbers.q,
generator=2 ** 1200,
y=DSA_KEY_1024.public_numbers.y
)
# Test a non-integer y value
with pytest.raises(TypeError):
dsa.DSAPublicKey(
modulus=DSA_KEY_1024.public_numbers.parameter_numbers.p,
subgroup_order=DSA_KEY_1024.public_numbers.parameter_numbers.q,
generator=DSA_KEY_1024.public_numbers.parameter_numbers.g,
y=None
)
@pytest.mark.dsa
class TestDSAVerification(object):
_algorithms_dict = {
'SHA1': hashes.SHA1,
'SHA224': hashes.SHA224,
'SHA256': hashes.SHA256,
'SHA384': hashes.SHA384,
'SHA512': hashes.SHA512
}
@pytest.mark.parametrize(
"vector",
load_vectors_from_file(
os.path.join(
"asymmetric", "DSA", "FIPS_186-3", "SigVer.rsp"),
load_fips_dsa_sig_vectors
)
)
def test_dsa_verification(self, vector, backend):
digest_algorithm = vector['digest_algorithm'].replace("-", "")
algorithm = self._algorithms_dict[digest_algorithm]
if (
not backend.dsa_parameters_supported(
vector['p'], vector['q'], vector['g']
) or not backend.dsa_hash_supported(algorithm)
):
pytest.skip(
"{0} does not support the provided parameters".format(backend)
)
public_key = dsa.DSAPublicNumbers(
parameter_numbers=dsa.DSAParameterNumbers(
vector['p'], vector['q'], vector['g']
),
y=vector['y']
).public_key(backend)
sig = der_encode_dsa_signature(vector['r'], vector['s'])
verifier = public_key.verifier(sig, algorithm())
verifier.update(vector['msg'])
if vector['result'] == "F":
with pytest.raises(InvalidSignature):
verifier.verify()
else:
verifier.verify()
def test_dsa_verify_invalid_asn1(self, backend):
parameters = dsa.DSAParameters.generate(1024, backend)
private_key = dsa.DSAPrivateKey.generate(parameters, backend)
public_key = private_key.public_key()
verifier = public_key.verifier(b'fakesig', hashes.SHA1(), backend)
verifier.update(b'fakesig')
with pytest.raises(InvalidSignature):
verifier.verify()
def test_use_after_finalize(self, backend):
parameters = dsa.DSAParameters.generate(1024, backend)
private_key = dsa.DSAPrivateKey.generate(parameters, backend)
public_key = private_key.public_key()
verifier = public_key.verifier(b'fakesig', hashes.SHA1(), backend)
verifier.update(b'irrelevant')
with pytest.raises(InvalidSignature):
verifier.verify()
with pytest.raises(AlreadyFinalized):
verifier.verify()
with pytest.raises(AlreadyFinalized):
verifier.update(b"more data")
def test_dsa_verifier_invalid_backend(self, backend):
pretend_backend = object()
params = dsa.DSAParameters.generate(1024, backend)
private_key = dsa.DSAPrivateKey.generate(params, backend)
public_key = private_key.public_key()
with raises_unsupported_algorithm(
_Reasons.BACKEND_MISSING_INTERFACE):
public_key.verifier(b"sig", hashes.SHA1(), pretend_backend)
@pytest.mark.dsa
class TestDSASignature(object):
_algorithms_dict = {
'SHA1': hashes.SHA1,
'SHA224': hashes.SHA224,
'SHA256': hashes.SHA256,
'SHA384': hashes.SHA384,
'SHA512': hashes.SHA512}
@pytest.mark.parametrize(
"vector",
load_vectors_from_file(
os.path.join(
"asymmetric", "DSA", "FIPS_186-3", "SigGen.txt"),
load_fips_dsa_sig_vectors
)
)
def test_dsa_signing(self, vector, backend):
digest_algorithm = vector['digest_algorithm'].replace("-", "")
algorithm = self._algorithms_dict[digest_algorithm]
if (
not backend.dsa_parameters_supported(
vector['p'], vector['q'], vector['g']
) or not backend.dsa_hash_supported(algorithm)
):
pytest.skip(
"{0} does not support the provided parameters".format(backend)
)
private_key = dsa.DSAPrivateNumbers(
public_numbers=dsa.DSAPublicNumbers(
parameter_numbers=dsa.DSAParameterNumbers(
vector['p'], vector['q'], vector['g']
),
y=vector['y']
),
x=vector['x']
).private_key(backend)
signer = private_key.signer(algorithm())
signer.update(vector['msg'])
signature = signer.finalize()
assert signature
public_key = private_key.public_key()
verifier = public_key.verifier(signature, algorithm())
verifier.update(vector['msg'])
verifier.verify()
def test_use_after_finalize(self, backend):
parameters = dsa.DSAParameters.generate(1024, backend)
private_key = dsa.DSAPrivateKey.generate(parameters, backend)
signer = private_key.signer(hashes.SHA1(), backend)
signer.update(b"data")
signer.finalize()
with pytest.raises(AlreadyFinalized):
signer.finalize()
with pytest.raises(AlreadyFinalized):
signer.update(b"more data")
def test_dsa_signer_invalid_backend(self, backend):
pretend_backend = object()
params = dsa.DSAParameters.generate(1024, backend)
private_key = dsa.DSAPrivateKey.generate(params, backend)
with raises_unsupported_algorithm(
_Reasons.BACKEND_MISSING_INTERFACE):
private_key.signer(hashes.SHA1(), pretend_backend)
def test_dsa_generate_invalid_backend():
pretend_backend = object()
with raises_unsupported_algorithm(
_Reasons.BACKEND_MISSING_INTERFACE):
dsa.DSAParameters.generate(1024, pretend_backend)
pretend_parameters = object()
with raises_unsupported_algorithm(
_Reasons.BACKEND_MISSING_INTERFACE):
dsa.DSAPrivateKey.generate(pretend_parameters, pretend_backend)
class TestDSANumbers(object):
def test_dsa_parameter_numbers(self):
parameter_numbers = dsa.DSAParameterNumbers(p=1, q=2, g=3)
assert parameter_numbers.p == 1
assert parameter_numbers.q == 2
assert parameter_numbers.g == 3
def test_dsa_parameter_numbers_invalid_types(self):
with pytest.raises(TypeError):
dsa.DSAParameterNumbers(p=None, q=2, g=3)
with pytest.raises(TypeError):
dsa.DSAParameterNumbers(p=1, q=None, g=3)
with pytest.raises(TypeError):
dsa.DSAParameterNumbers(p=1, q=2, g=None)
def test_dsa_public_numbers(self):
parameter_numbers = dsa.DSAParameterNumbers(p=1, q=2, g=3)
public_numbers = dsa.DSAPublicNumbers(
y=4,
parameter_numbers=parameter_numbers
)
assert public_numbers.y == 4
assert public_numbers.parameter_numbers == parameter_numbers
def test_dsa_public_numbers_invalid_types(self):
with pytest.raises(TypeError):
dsa.DSAPublicNumbers(y=4, parameter_numbers=None)
with pytest.raises(TypeError):
parameter_numbers = dsa.DSAParameterNumbers(p=1, q=2, g=3)
dsa.DSAPublicNumbers(y=None, parameter_numbers=parameter_numbers)
def test_dsa_private_numbers(self):
parameter_numbers = dsa.DSAParameterNumbers(p=1, q=2, g=3)
public_numbers = dsa.DSAPublicNumbers(
y=4,
parameter_numbers=parameter_numbers
)
private_numbers = dsa.DSAPrivateNumbers(
x=5,
public_numbers=public_numbers
)
assert private_numbers.x == 5
assert private_numbers.public_numbers == public_numbers
def test_dsa_private_numbers_invalid_types(self):
parameter_numbers = dsa.DSAParameterNumbers(p=1, q=2, g=3)
public_numbers = dsa.DSAPublicNumbers(
y=4,
parameter_numbers=parameter_numbers
)
with pytest.raises(TypeError):
dsa.DSAPrivateNumbers(x=4, public_numbers=None)
with pytest.raises(TypeError):
dsa.DSAPrivateNumbers(x=None, public_numbers=public_numbers)
|