xref: /aosp_15_r20/external/wycheproof/doc/dsa.md (revision 3d322d526be0ef039eebde10aa6ae75db8cc3a3d)
1*3d322d52SAndroid Build Coastguard Worker# DSA
2*3d322d52SAndroid Build Coastguard Worker
3*3d322d52SAndroid Build Coastguard Worker[TOC]
4*3d322d52SAndroid Build Coastguard Worker
5*3d322d52SAndroid Build Coastguard WorkerThe digital signature algorithm (DSA) is one of three signature schemes
6*3d322d52SAndroid Build Coastguard Workerdescripted in the digital signature standard [DSS].
7*3d322d52SAndroid Build Coastguard Worker
8*3d322d52SAndroid Build Coastguard Worker## Key generation
9*3d322d52SAndroid Build Coastguard Worker
10*3d322d52SAndroid Build Coastguard Worker4.2 Selection of Parameter Sizes and Hash Functions for DSA
11*3d322d52SAndroid Build Coastguard WorkerThe DSS specifies the following choices for the pair (L,N),
12*3d322d52SAndroid Build Coastguard Workerwhere L is the size of p in bits and N is the size of q in bits:
13*3d322d52SAndroid Build Coastguard Worker
14*3d322d52SAndroid Build Coastguard WorkerL   |  N
15*3d322d52SAndroid Build Coastguard Worker---:|----:
16*3d322d52SAndroid Build Coastguard Worker1024| 160
17*3d322d52SAndroid Build Coastguard Worker2048| 224
18*3d322d52SAndroid Build Coastguard Worker2048| 256
19*3d322d52SAndroid Build Coastguard Worker3072| 256
20*3d322d52SAndroid Build Coastguard Worker
21*3d322d52SAndroid Build Coastguard WorkerThe tests expect the following properties of the parameters used during
22*3d322d52SAndroid Build Coastguard Workerkey generation:
23*3d322d52SAndroid Build Coastguard Worker
24*3d322d52SAndroid Build Coastguard Worker* If only the parameter L is specified by the caller then N should be one
25*3d322d52SAndroid Build Coastguard Worker  of the options proposed in [DSS].
26*3d322d52SAndroid Build Coastguard Worker* If no size is specified then L should be at least 2048. This is the minimal
27*3d322d52SAndroid Build Coastguard Worker  key size recommended by NIST for the period up to the year 2030.
28*3d322d52SAndroid Build Coastguard Worker
29*3d322d52SAndroid Build Coastguard Worker## Signature generation
30*3d322d52SAndroid Build Coastguard Worker
31*3d322d52SAndroid Build Coastguard WorkerThe DSA signature algorithm requires that each signature is computed with a new
32*3d322d52SAndroid Build Coastguard Workerone-time secret k. This secret value should be close to uniformly distributed.
33*3d322d52SAndroid Build Coastguard WorkerIf that is not the case then DSA signatures can leak the private key that was
34*3d322d52SAndroid Build Coastguard Workerused to generate the signature. Two methods for generating the one-time secrets
35*3d322d52SAndroid Build Coastguard Workerare described in FIPS PUB 186-4, Section B.5.1 or B.5.2 [DSS]. There is also the
36*3d322d52SAndroid Build Coastguard Workerpossibility that the use of mismatched implementations for key generation and
37*3d322d52SAndroid Build Coastguard Workersignature generation are leaking the private keys.
38*3d322d52SAndroid Build Coastguard Worker
39*3d322d52SAndroid Build Coastguard Worker## Signature verification
40*3d322d52SAndroid Build Coastguard Worker
41*3d322d52SAndroid Build Coastguard WorkerA DSA signature is a DER encoded tuple of two integers (r,s). To verify a
42*3d322d52SAndroid Build Coastguard Workersignature the verifier first checks $$0 < r < q$$ and $$0 < s < q$$. The
43*3d322d52SAndroid Build Coastguard Workerverifier then computes:
44*3d322d52SAndroid Build Coastguard Worker
45*3d322d52SAndroid Build Coastguard Worker$$
46*3d322d52SAndroid Build Coastguard Worker\begin{array}{l}
47*3d322d52SAndroid Build Coastguard Workerw=s^{-1} \bmod q\\
48*3d322d52SAndroid Build Coastguard Workeru1 = w \cdot H(m) \bmod q\\
49*3d322d52SAndroid Build Coastguard Workeru2 = w \cdot r \bmod q\\
50*3d322d52SAndroid Build Coastguard Worker\end{array}
51*3d322d52SAndroid Build Coastguard Worker$$
52*3d322d52SAndroid Build Coastguard Worker
53*3d322d52SAndroid Build Coastguard Workerand then verifies that \\(r = (g^{u1}y^{u2} \bmod p) \bmod q\\)
54*3d322d52SAndroid Build Coastguard Worker
55*3d322d52SAndroid Build Coastguard Worker## Incorrect computations and range checks.
56*3d322d52SAndroid Build Coastguard Worker
57*3d322d52SAndroid Build Coastguard WorkerSome libraries return 0 as the modular inverse of 0 or q.
58*3d322d52SAndroid Build Coastguard WorkerThis can happen if the library computes the modular
59*3d322d52SAndroid Build Coastguard Workerinverse of s as \\(w=s^{q-2} \mod q\\) (gpg4browsers) of simply
60*3d322d52SAndroid Build Coastguard Workerif the implementations is buggy (pycrypto). if additionally to such
61*3d322d52SAndroid Build Coastguard Workera bug the range of r,s is not or incorrectly tested then it might
62*3d322d52SAndroid Build Coastguard Workerbe feasible to forge signatures with the values (r=1, s=0) or (r=1, s=q).
63*3d322d52SAndroid Build Coastguard WorkerIn particular, if a library can be forced to compute \\(s^{-1} \mod q = 0\\)
64*3d322d52SAndroid Build Coastguard Workerthen the verification would compute \\( w = u1 = u2 = 0 \\) and hence
65*3d322d52SAndroid Build Coastguard Worker\\( (g^{u1}y^{u2} \mod p) \mod q = 1 .\\)
66*3d322d52SAndroid Build Coastguard Worker
67*3d322d52SAndroid Build Coastguard Worker## Timing attacks
68*3d322d52SAndroid Build Coastguard Worker
69*3d322d52SAndroid Build Coastguard WorkerTBD
70*3d322d52SAndroid Build Coastguard Worker
71*3d322d52SAndroid Build Coastguard Worker# Some notable failures of crypto libraries.
72*3d322d52SAndroid Build Coastguard Worker
73*3d322d52SAndroid Build Coastguard Worker## JDK
74*3d322d52SAndroid Build Coastguard Worker
75*3d322d52SAndroid Build Coastguard WorkerThe  jdk8 implementation of SHA1withDSA previously checked the key size as follows:
76*3d322d52SAndroid Build Coastguard Worker
77*3d322d52SAndroid Build Coastguard Worker```java
78*3d322d52SAndroid Build Coastguard Worker@Override
79*3d322d52SAndroid Build Coastguard Worker  protected void checkKey(DSAParams params)
80*3d322d52SAndroid Build Coastguard Worker     throws InvalidKeyException {
81*3d322d52SAndroid Build Coastguard Worker    int valueL = params.getP().bitLength();
82*3d322d52SAndroid Build Coastguard Worker    if (valueL > 1024) {
83*3d322d52SAndroid Build Coastguard Worker       throw new InvalidKeyException("Key is too long for this algorithm");
84*3d322d52SAndroid Build Coastguard Worker   }
85*3d322d52SAndroid Build Coastguard Worker }
86*3d322d52SAndroid Build Coastguard Worker```
87*3d322d52SAndroid Build Coastguard Worker
88*3d322d52SAndroid Build Coastguard WorkerThis check was reasonable, it partially ensures conformance with the NIST
89*3d322d52SAndroid Build Coastguard Workerstandard. In most cases would prevent the attack described above.
90*3d322d52SAndroid Build Coastguard Worker
91*3d322d52SAndroid Build Coastguard WorkerHowever, Oracle released a patch that removed the length verification in DSA in
92*3d322d52SAndroid Build Coastguard Workerjdk9: http://hg.openjdk.java.net/jdk9/dev/jdk/rev/edd7a67585a5
93*3d322d52SAndroid Build Coastguard Workerhttps://bugs.openjdk.java.net/browse/JDK-8039921
94*3d322d52SAndroid Build Coastguard Worker
95*3d322d52SAndroid Build Coastguard WorkerThe new code is here:
96*3d322d52SAndroid Build Coastguard Workerhttp://hg.openjdk.java.net/jdk9/dev/jdk/file/edd7a67585a5/src/java.base/share/classes/sun/security/provider/DSA.java
97*3d322d52SAndroid Build Coastguard Worker
98*3d322d52SAndroid Build Coastguard WorkerThe change was further backported to jdk8:
99*3d322d52SAndroid Build Coastguard Workerhttp://hg.openjdk.java.net/jdk8u/jdk8u/jdk/rev/3212f1631643
100*3d322d52SAndroid Build Coastguard Worker
101*3d322d52SAndroid Build Coastguard WorkerDoing this was a serious mistake. It easily allowed incorrect implementations.
102*3d322d52SAndroid Build Coastguard WorkerWhile generating 2048 bit DSA keys in jdk7 was not yet supported, doing so in
103*3d322d52SAndroid Build Coastguard Workerjdk8 is. To trigger this bug in jdk7 an application had to use a key generated
104*3d322d52SAndroid Build Coastguard Workerby a third party library (e.g. OpenSSL). Now, it is possible to trigger the bug
105*3d322d52SAndroid Build Coastguard Workerjust using JCE. Moreover, the excessive use of default values in JCE makes it
106*3d322d52SAndroid Build Coastguard Workereasy to go wrong and rather difficult to spot the errors.
107*3d322d52SAndroid Build Coastguard Worker
108*3d322d52SAndroid Build Coastguard WorkerThe bug was for example triggered by the following code snippet:
109*3d322d52SAndroid Build Coastguard Worker
110*3d322d52SAndroid Build Coastguard Worker```java
111*3d322d52SAndroid Build Coastguard Worker    KeyPairGenerator keygen = KeyPairGenerator.getInstance("DSA");
112*3d322d52SAndroid Build Coastguard Worker    Keygen.initialize(2048);
113*3d322d52SAndroid Build Coastguard Worker    KeyPair keypair = keygen.genKeyPair();
114*3d322d52SAndroid Build Coastguard Worker    Signature s = Signature.getInstance("DSA");
115*3d322d52SAndroid Build Coastguard Worker    s.initSign(keypair.getPrivate());
116*3d322d52SAndroid Build Coastguard Worker```
117*3d322d52SAndroid Build Coastguard Worker
118*3d322d52SAndroid Build Coastguard WorkerThe first three lines generate a 2048 bit DSA key. 2048 bits is currently the
119*3d322d52SAndroid Build Coastguard Workersmallest key size recommended by NIST.
120*3d322d52SAndroid Build Coastguard Worker
121*3d322d52SAndroid Build Coastguard Worker```java
122*3d322d52SAndroid Build Coastguard Worker    KeyPairGenerator keygen = KeyPairGenerator.getInstance("DSA");
123*3d322d52SAndroid Build Coastguard Worker    Keygen.initialize(2048);
124*3d322d52SAndroid Build Coastguard Worker    KeyPair keypair = keygen.genKeyPair();
125*3d322d52SAndroid Build Coastguard Worker```
126*3d322d52SAndroid Build Coastguard Worker
127*3d322d52SAndroid Build Coastguard WorkerThe key size specifies the size of p but not the size of q. The NIST standard
128*3d322d52SAndroid Build Coastguard Workerallows either 224 or 256 bits for the size of q. The selection typically depends
129*3d322d52SAndroid Build Coastguard Workeron the library. The Sun provider uses 224. Other libraries e.g. OpenSSL
130*3d322d52SAndroid Build Coastguard Workergenerates by default a 256 bit q for 2048 bit DSA keys.
131*3d322d52SAndroid Build Coastguard Worker
132*3d322d52SAndroid Build Coastguard WorkerThe next line contains a default in the initialization
133*3d322d52SAndroid Build Coastguard Worker
134*3d322d52SAndroid Build Coastguard Worker```java
135*3d322d52SAndroid Build Coastguard Worker    Signature s = Signature.getInstance("DSA");
136*3d322d52SAndroid Build Coastguard Worker```
137*3d322d52SAndroid Build Coastguard WorkerThis line is equivalent to
138*3d322d52SAndroid Build Coastguard Worker
139*3d322d52SAndroid Build Coastguard Worker```java
140*3d322d52SAndroid Build Coastguard Worker    Signature s = Signature.getInstance("SHA1withDSA");
141*3d322d52SAndroid Build Coastguard Worker```
142*3d322d52SAndroid Build Coastguard WorkerHence the code above uses SHA1 but with DSA parameters generated for SHA-224
143*3d322d52SAndroid Build Coastguard Workeror SHA-256 hashes. Allowing this combination by itself is already a mistake,
144*3d322d52SAndroid Build Coastguard Workerbut a flawed implementaion made the situation even worse.
145*3d322d52SAndroid Build Coastguard Worker
146*3d322d52SAndroid Build Coastguard WorkerThe implementation of SHA1withDSA assumeed that the parameter q is 160 bits
147*3d322d52SAndroid Build Coastguard Workerlong and used this assumption to generate a random 160-bit k when generating a
148*3d322d52SAndroid Build Coastguard Workersignature instead of choosing it uniformly in the range (1,q-1).
149*3d322d52SAndroid Build Coastguard WorkerHence, k severely biased. Attacks against DSA with biased k are well known.
150*3d322d52SAndroid Build Coastguard WorkerHowgrave-Graham and Smart analyzed such a situation [HS99]. Their results
151*3d322d52SAndroid Build Coastguard Workershow that about 4 signatrues leak enough information to determine
152*3d322d52SAndroid Build Coastguard Workerthe private key in a few milliseconds.
153*3d322d52SAndroid Build Coastguard WorkerNguyen analyzed a similar flaw in GPG [N04].
154*3d322d52SAndroid Build Coastguard WorkerI.e., Section 3.2 of Nguyens paper describes essentially the same attack as
155*3d322d52SAndroid Build Coastguard Workerused here. More generally, attacks based on lattice reduction were developed
156*3d322d52SAndroid Build Coastguard Workerto break a variety of cryptosystems such as the knapsack cryptosystem [O90].
157*3d322d52SAndroid Build Coastguard Worker
158*3d322d52SAndroid Build Coastguard Worker## Further notes
159*3d322d52SAndroid Build Coastguard Worker
160*3d322d52SAndroid Build Coastguard WorkerThe short algorithm name “DSA” is misleading, since it hides the fact that
161*3d322d52SAndroid Build Coastguard Worker`Signature.getInstance(“DSA”)` is equivalent to
162*3d322d52SAndroid Build Coastguard Worker`Signature.getInstance(“SHA1withDSA”)`. To reduce the chance of a
163*3d322d52SAndroid Build Coastguard Workermisunderstanding short algorithm names should be deprecated. In JCE the hash
164*3d322d52SAndroid Build Coastguard Workeralgorithm is defined by the algorithm. I.e. depending on the hash algorithm to
165*3d322d52SAndroid Build Coastguard Workeruse one would call one of:
166*3d322d52SAndroid Build Coastguard Worker
167*3d322d52SAndroid Build Coastguard Worker```java
168*3d322d52SAndroid Build Coastguard Worker  Signature.getInstance(“SHA1withDSA”);
169*3d322d52SAndroid Build Coastguard Worker  Signature.getInstance(“SHA224withDSA”);
170*3d322d52SAndroid Build Coastguard Worker  Signature.getInstance(“SHA256withDSA”);
171*3d322d52SAndroid Build Coastguard Worker```
172*3d322d52SAndroid Build Coastguard Worker
173*3d322d52SAndroid Build Coastguard WorkerA possible way to push such a change are code analysis tools. "DSA" is in good
174*3d322d52SAndroid Build Coastguard Workercompany with other algorithm names “RSA”, “AES”, “DES”, all of which default to
175*3d322d52SAndroid Build Coastguard Workerweak algorithms.
176*3d322d52SAndroid Build Coastguard Worker
177*3d322d52SAndroid Build Coastguard Worker## References
178*3d322d52SAndroid Build Coastguard Worker
179*3d322d52SAndroid Build Coastguard Worker[HS99]: N.A. Howgrave-Graham, N.P. Smart,
180*3d322d52SAndroid Build Coastguard Worker    “Lattice Attacks on Digital Signature Schemes”
181*3d322d52SAndroid Build Coastguard Worker    http://www.hpl.hp.com/techreports/1999/HPL-1999-90.pdf
182*3d322d52SAndroid Build Coastguard Worker
183*3d322d52SAndroid Build Coastguard Worker[N04]: Phong Nguyen, “Can we trust cryptographic software? Cryptographic flaws
184*3d322d52SAndroid Build Coastguard Worker    in Gnu privacy guard 1.2.3”, Eurocrypt 2004,
185*3d322d52SAndroid Build Coastguard Worker    https://www.iacr.org/archive/eurocrypt2004/30270550/ProcEC04.pdf
186*3d322d52SAndroid Build Coastguard Worker
187*3d322d52SAndroid Build Coastguard Worker[O90]: A. M. Odlyzko, "The rise and fall of knapsack cryptosystems", Cryptology
188*3d322d52SAndroid Build Coastguard Worker    and Computational Number Theory, pp.75-88, 1990
189*3d322d52SAndroid Build Coastguard Worker
190*3d322d52SAndroid Build Coastguard Worker[DSS]: FIPS PUB 186-4, "Digital Signature Standard (DSS)", National Institute
191*3d322d52SAndroid Build Coastguard Worker    of Standards and Technology, July 2013
192*3d322d52SAndroid Build Coastguard Worker    http://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.186-4.pdf
193