rfc6979-ECDSA Signature Verification
rfc6979-ECDSA Signature Verification
Pornin
Request for Comments: 6979 August 2013
Category: Informational
ISSN: 2070-1721
Abstract
Copyright Notice
Copyright (c) 2013 IETF Trust and the persons identified as the
document authors. All rights reserved.
Table of Contents
1. Introduction ....................................................3
1.1. Requirements Language ......................................4
2. DSA and ECDSA Notations .........................................4
2.1. Key Parameters .............................................5
2.2. Key Pairs ..................................................5
2.3. Integer Conversions ........................................6
2.3.1. Bits and Octets .....................................6
2.3.2. Bit String to Integer ...............................6
2.3.3. Integer to Octet String .............................7
2.3.4. Bit String to Octet String ..........................7
2.3.5. Usage ...............................................8
2.4. Signature Generation .......................................8
3. Deterministic DSA and ECDSA ....................................10
3.1. Building Blocks ...........................................10
3.1.1. HMAC ...............................................10
3.2. Generation of k ...........................................10
3.3. Alternate Description of the Generation of k ..............12
3.4. Usage Notes ...............................................13
3.5. Rationale .................................................13
3.6. Variants ..................................................14
4. Security Considerations ........................................15
5. Intellectual Property Status ...................................17
6. References .....................................................17
6.1. Normative References ......................................17
6.2. Informative References ....................................18
Appendix A. Examples .............................................20
A.1. Detailed Example ..........................................20
A.1.1. Key Pair ..............................................20
A.1.2. Generation of k .......................................20
A.1.3. Signature .............................................23
A.2. Test Vectors ..............................................24
A.2.1. DSA, 1024 Bits ........................................25
A.2.2. DSA, 2048 Bits ........................................27
A.2.3. ECDSA, 192 Bits (Prime Field) .........................29
A.2.4. ECDSA, 224 Bits (Prime Field) .........................31
A.2.5. ECDSA, 256 Bits (Prime Field) .........................33
A.2.6. ECDSA, 384 Bits (Prime Field) .........................35
A.2.7. ECDSA, 521 Bits (Prime Field) .........................38
A.2.8. ECDSA, 163 Bits (Binary Field, Koblitz Curve) .........42
A.2.9. ECDSA, 233 Bits (Binary Field, Koblitz Curve) .........44
A.2.10. ECDSA, 283 Bits (Binary Field, Koblitz Curve) .........46
A.2.11. ECDSA, 409 Bits (Binary Field, Koblitz Curve) .........49
A.2.12. ECDSA, 571 Bits (Binary Field, Koblitz Curve) .........52
A.2.13. ECDSA, 163 Bits (Binary Field, Pseudorandom Curve) ....56
A.2.14. ECDSA, 233 Bits (Binary Field, Pseudorandom Curve) ....58
A.2.15. ECDSA, 283 Bits (Binary Field, Pseudorandom Curve) ....60
1. Introduction
DSA [FIPS-186-4] and ECDSA [X9.62] are two standard digital signature
schemes. They provide data integrity and verifiable authenticity in
various protocols.
One characteristic of DSA and ECDSA is that they need to produce, for
each signature generation, a fresh random value (hereafter designated
as k). For effective security, k must be chosen randomly and
uniformly from a set of modular integers, using a cryptographically
secure process. Even slight biases in that process may be turned
into attacks on the signature schemes.
o Deterministic DSA and ECDSA can be applied over the same inputs as
plain DSA and ECDSA, namely a hash value computed over the message
that is to be signed, with a cryptographically secure hash
function.
The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT",
"SHOULD", "SHOULD NOT", "RECOMMENDED", "MAY", and "OPTIONAL" in this
document are to be interpreted as described in RFC 2119 [RFC2119].
In this section, we succinctly describe DSA and ECDSA and define our
notations. The complete specifications for DSA and ECDSA can be
found in [FIPS-186-4] and [X9.62], respectively.
DSA and ECDSA work over a large group of prime size, in which the
group operation is easy to compute, but the discrete logarithm is
computationally infeasible with existing and foreseeable technology.
The definition of the group is called the "key parameters". Key
parameters may be shared between different key pairs with no ill
effect on security; this is the usual case with ECDSA in particular.
G a point of E, of order q
A DSA or ECDSA public key is computed from the private key x and the
key parameters:
* if qlen < blen, then the qlen leftmost bits are kept, and
subsequent bits are discarded;
z1 = bits2int(b)
z2 = z1 mod q
2.3.5. Usage
h = bits2int(H(m)) mod q
* For DSA:
s = (h+x*r)/k mod q
3.1.1. HMAC
We denote the process of applying HMAC with key K over data V by:
HMAC_K(V)
3.2. Generation of k
h1 = H(m)
b. Set:
c. Set:
d. Set:
e. Set:
V = HMAC_K(V)
f. Set:
g. Set:
V = HMAC_K(V)
V = HMAC_K(V)
T = T || V
3. Compute:
k = bits2int(T)
K = HMAC_K(V || 0x00)
V = HMAC_K(V)
requested_instantiation_security_strength
Set this parameter to any value that the HMAC_DRBG
implementation will accept, when using H as base hash
function.
prediction_resistance_flag
Set this parameter to "false".
personalization_string
Set this parameter to "Null" (the empty bit sequence).
entropy_input
Use int2octets(x) as entropy string.
nonce
Use bits2octets(H(m)) as nonce.
Note that the last two parameters are not parameters to the
HMAC_DRBG instantiation function per se; instead, those values
are requested from the internal Get_entropy_input function during
instantiation. For deterministic (EC)DSA, we want HMAC_DRBG to
run with the entropy string and nonce that we specify, without
accessing an actual entropy source.
With DSA or ECDSA, the value k is used to compute the first half of
the signature, dubbed r (see Section 2.4). The DSA and ECDSA
standards mandate that, if r is zero, then a new k should be
selected. In that situation, this document specifies that the value
k is "unsuitable", and the generation process shall keep on looping.
3.5. Rationale
that we use the concatenation of the private key x and the hashed
message H(m) as the pseudorandom number generator (PRNG) seed. If
using a "security level" of n bits, then HMAC_DRBG should be used
with seed entropy at least n+64 bits; however, the key x should also
have been generated with that much entropy, and the length of x is
qlen, which is at least equal to 2*n and thus larger than n+64 (DSA
and ECDSA, as specified by the standards, require qlen >= 160). It
can then be argued that deterministic ECDSA fulfills the entropy
requirements of Annex D of [X9.62].
3.6. Variants
4. Security Considerations
Both removal of the random source requirement and the ability to test
an implementation against test vectors enhance security of DSA and
ECDSA signer implementations, in that they help avoid hard-to-test
failure conditions. Deterministic signature schemes may also help in
other situations, e.g., to avoid spurious duplicates, when the same
data element is signed several times with the same key: with a
deterministic signature scheme, the same signature is generated every
time, making duplicate detection much easier.
the public key (which is computed from x) is also known. Given the
lack of common structure between HMAC and discrete logarithms, this
seems a reasonable assumption.
6. References
[B2006] Bellare, M., "New Proofs for NMAC and HMAC: Security
without Collision-Resistance", Crypto 2006, LNCS 4117,
August 2006.
[BDLSY2011] Bernstein, D., Duif, N., Lange, T., Schwabe, P., and B.
Yang, "High-speed high-security signatures", Cryptology
ePrint Archive Report 2011/368, September 2011.
Appendix A. Examples
q = 0x4000000000000000000020108A2E0CC0D99F8A5EF
x = 0x09A4D6792295A7F730FC3F2B49CBC0F62E862272F
The corresponding public key is the curve point U = xG. This point
has two coordinates, which are elements of the field GF(2^163).
These elements can be converted to integers using the procedure
described in Section A.5.6 of [X9.62], yielding the two public point
coordinates:
Ux = 0x79AEE090DB05EC252D5CB4452F356BE198A4FF96F
Uy = 0x782E29634DDC9A31EF40386E896BAA18B53AFA5A3
A.1.2. Generation of k
h1
AF 2B DB E1 AA 9B 6E C1 E2 AD E1 D6 94 F4 1F C7
1A 83 1D 02 68 E9 89 15 62 11 3D 8A 62 AD D1 BF
int2octets(x)
00 9A 4D 67 92 29 5A 7F 73 0F C3 F2 B4 9C BC 0F
62 E8 62 27 2F
bits2octets(h1)
01 79 5E DF 0D 54 DB 76 0F 15 6D 0D AC 04 C0 32
2B 3A 20 42 24
The steps b to g (see Section 3.2) then compute the values for the K
and V variables. These variables are sequences of 256 bits (the hash
function output length, rounded up to a multiple of 8). We reproduce
here the successive values:
V after step b:
01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01
01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01
K after step c:
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
K after step d:
09 99 9A 9B FE F9 72 D3 34 69 11 88 3F AD 79 51
D2 3F 2C 8B 47 F4 20 22 2D 11 71 EE EE AC 5A B8
V after step e:
D5 F4 03 0F 75 5E E8 6A A1 0B BA 8C 09 DF 11 4F
F6 B6 11 1C 23 85 00 D1 3C 73 43 A8 C0 1B EC F7
K after step f:
0C F2 FE 96 D5 61 9C 9E F5 3C B7 41 7D 49 D3 7E
A6 8A 4F FE D0 D7 E6 23 E3 86 89 28 99 11 BD 57
V after step g:
78 34 57 C1 CF 31 48 A8 F2 A9 AE 73 ED 47 2F A9
8E D9 CD 92 5D 8E 96 4C E0 76 4D EF 3F 84 2B 9A
In step h, we perform the final loop. Since we use HMAC with SHA-
256, which produces 256 bits worth of output, and we need only 163
bits for T, a single HMAC invocation yields the following T:
T (first try)
93 05 A4 6D E7 FF 8E B1 07 19 4D EB D3 FD 48 AA
20 D5 E7 65 6C BE 0E A6 9D 2A 8D 4E 7C 67 31 4A
k1 = 0x4982D236F3FFC758838CA6F5E9FEA455106AF3B2B
Since that value is greater than q-1, we have to loop. This first
entails computing new values for K and V:
new K
75 CB 5C 05 B2 A7 8C 3D 81 DF 12 D7 4D 7B E0 A0
E9 4A B1 98 15 78 1D 4D 8E 29 02 A7 9D 0A 66 99
new V
DC B9 CA 12 61 07 A9 C2 7C E7 7B A5 8E A8 71 C8
C9 12 D8 35 EA DD C3 05 F2 44 5D 88 F6 6C 4C 43
then a new T:
T (second try)
C7 0C 78 60 8A 3B 5B E9 28 9B E9 0E F6 E8 1A 9E
2C 15 16 D5 75 1D 2F 75 F5 00 33 E4 5F 73 BD EB
k2 = 0x63863C30451DADF4944DF4877B740D4F160A8B6AB
new K (2)
0A 5A 64 B9 9C 05 95 20 10 36 86 CB 6F 36 BC FC
A7 88 EB 3B CF 69 BA 66 A5 BB 08 0B 05 93 BA 53
new V (2)
0B 3B 19 68 11 B1 9F 6C 6F 72 9C 43 F3 5B CF 0D
FD 72 5F 17 CA 34 30 E8 72 14 53 E5 55 50 A1 8F
T (third try)
47 5E 80 E9 92 14 05 67 FC C3 A5 0D AB 90 FE 84
BC D7 BB 03 63 8E 9C 46 56 A0 6F 37 F6 50 8A 7C
k = 0x23AF4074C90A02B3FE61D286D5C87F425E6BDD81B
A.1.3. Signature
With our private key and the value of k that we just generated, we
can now compute the signature using the standard ECDSA mechanisms.
First, the point kG is computed, and the X coordinate of that point
is converted to an integer and then reduced modulo q, yielding the
first signature half:
r = 0x113A63990598A3828C407C0F4D2438D990DF99A7F
s = 0x1313A2E03F5412DDB296A22E2C455335545672D9F
30 2E 02 15 01 13 A6 39 90 59 8A 38 28 C4 07 C0
F4 D2 43 8D 99 0D F9 9A 7F 02 15 01 31 3A 2E 03
F5 41 2D DB 29 6A 22 E2 C4 55 33 55 45 67 2D 9F
In the following sections, we give test vectors for various key sizes
and hash functions, both for DSA and ECDSA.
Key pair:
key parameters:
p = 86F5CA03DCFEB225063FF830A0C769B9DD9D6153AD91D7CE27F787C43278B447
E6533B86B18BED6E8A48B784A14C252C5BE0DBF60B86D6385BD2F12FB763ED88
73ABFD3F5BA2E0A8C0A59082EAC056935E529DAF7C610467899C77ADEDFC846C
881870B7B19B2B58F9BE0521A17002E3BDD6B86685EE90B3D9A1B02B782B1779
q = 996F967F6C8E388D9E28D01E205FBA957A5698B1
g = 07B0F92546150B62514BB771E2A0C0CE387F03BDA6C56B505209FF25FD3C133D
89BBCD97E904E09114D9A7DEFDEADFC9078EA544D2E401AEECC40BB9FBBF78FD
87995A10A1C27CB7789B594BA7EFB5C4326A9FE59A070E136DB77175464ADCA4
17BE5DCE2F40D10A46A3A3943F26AB7FD9C0398FF8C76EE0A56826A8A88F1DBD
private key:
x = 411602CB19A6CCC34494D79D98EF1E7ED5AF25F7
public key:
y = 5DF5E01DED31D0297E274E1691C192FE5868FEF9E19A84776454B100CF16F653
92195A38B90523E2542EE61871C0440CB87C322FC4B4D2EC5E1E7EC766E1BE8D
4CE935437DC11C3C8FD426338933EBFE739CB3465F4D3668C5E473508253B1E6
82F65CBDC4FAE93C2EA212390E54905A86E2223170B44EAA7DA5DD9FFCFB7F3B
Signatures:
Key pair:
key parameters:
p = 9DB6FB5951B66BB6FE1E140F1D2CE5502374161FD6538DF1648218642F0B5C48
C8F7A41AADFA187324B87674FA1822B00F1ECF8136943D7C55757264E5A1A44F
FE012E9936E00C1D3E9310B01C7D179805D3058B2A9F4BB6F9716BFE6117C6B5
B3CC4D9BE341104AD4A80AD6C94E005F4B993E14F091EB51743BF33050C38DE2
35567E1B34C3D6A5C0CEAA1A0F368213C3D19843D0B4B09DCB9FC72D39C8DE41
F1BF14D4BB4563CA28371621CAD3324B6A2D392145BEBFAC748805236F5CA2FE
92B871CD8F9C36D3292B5509CA8CAA77A2ADFC7BFD77DDA6F71125A7456FEA15
3E433256A2261C6A06ED3693797E7995FAD5AABBCFBE3EDA2741E375404AE25B
q = F2C3119374CE76C9356990B465374A17F23F9ED35089BD969F61C6DDE9998C1F
g = 5C7FF6B06F8F143FE8288433493E4769C4D988ACE5BE25A0E24809670716C613
D7B0CEE6932F8FAA7C44D2CB24523DA53FBE4F6EC3595892D1AA58C4328A06C4
6A15662E7EAA703A1DECF8BBB2D05DBE2EB956C142A338661D10461C0D135472
085057F3494309FFA73C611F78B32ADBB5740C361C9F35BE90997DB2014E2EF5
AA61782F52ABEB8BD6432C4DD097BC5423B285DAFB60DC364E8161F4A2A35ACA
3A10B1C4D203CC76A470A33AFDCBDD92959859ABD8B56E1725252D78EAC66E71
BA9AE3F1DD2487199874393CD4D832186800654760E1E34C09E4D155179F9EC0
DC4473F996BDCE6EED1CABED8B6F116F7AD9CF505DF0F998E34AB27514B0FFE7
private key:
x = 69C7548C21D0DFEA6B9A51C9EAD4E27C33D3B3F180316E5BCAB92C933F0E4DBC
public key:
y = 667098C654426C78D7F8201EAC6C203EF030D43605032C2F1FA937E5237DBD94
9F34A0A2564FE126DC8B715C5141802CE0979C8246463C40E6B6BDAA2513FA61
1728716C2E4FD53BC95B89E69949D96512E873B9C8F8DFD499CC312882561ADE
CB31F658E934C0C197F2C4D96B05CBAD67381E7B768891E4DA3843D24D94CDFB
5126E9B8BF21E8358EE0E0A30EF13FD6A664C0DCE3731F7FB49A4845A4FD8254
687972A2D382599C9BAC4E0ED7998193078913032558134976410B89D2C171D1
23AC35FD977219597AA7D15C1A9A428E59194F75C721EBCBCFAE44696A499AFA
74E04299F132026601638CB87AB79190D4A0986315DA8EEC6561C938996BEADF
Signatures:
Key pair:
q = FFFFFFFFFFFFFFFFFFFFFFFF99DEF836146BC9B1B4D22831
(qlen = 192 bits)
private key:
x = 6FAB034934E4C0FC9AE67F5B5659A9D7D1FEFD187EE09FD4
public key: U = xG
Ux = AC2C77F529F91689FEA0EA5EFEC7F210D8EEA0B9E047ED56
Uy = 3BC723E57670BD4887EBC732C523063D0A7C957BC97C1C43
Signatures:
Key pair:
q = FFFFFFFFFFFFFFFFFFFFFFFFFFFF16A2E0B8F03E13DD29455C5C2A3D
(qlen = 224 bits)
private key:
x = F220266E1105BFE3083E03EC7A3A654651F45E37167E88600BF257C1
public key: U = xG
Ux = 00CF08DA5AD719E42707FA431292DEA11244D64FC51610D94B130D6C
Uy = EEAB6F3DEBE455E3DBF85416F7030CBD94F34F2D6F232C69F3C1385A
Signatures:
Key pair:
q = FFFFFFFF00000000FFFFFFFFFFFFFFFFBCE6FAADA7179E84F3B9CAC2FC632551
(qlen = 256 bits)
private key:
x = C9AFA9D845BA75166B5C215767B1D6934E50C3DB36E89B127B8A622B120F6721
public key: U = xG
Ux = 60FED4BA255A9D31C961EB74C6356D68C049B8923B61FA6CE669622E60F29FB6
Uy = 7903FE1008B8BC99A41AE9E95628BC64F2F1B20C2D7E9F5177A3C294D4462299
Signatures:
Key pair:
q = FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFC7634D81F4372DDF
581A0DB248B0A77AECEC196ACCC52973
(qlen = 384 bits)
private key:
x = 6B9D3DAD2E1B8C1C05B19875B6659F4DE23C3B667BF297BA9AA47740787137D8
96D5724E4C70A825F872C9EA60D2EDF5
public key: U = xG
Ux = EC3A4E415B4E19A4568618029F427FA5DA9A8BC4AE92E02E06AAE5286B300C64
DEF8F0EA9055866064A254515480BC13
Uy = 8015D9B72D7D57244EA8EF9AC0C621896708A59367F9DFB9F54CA84B3F1C9DB1
288B231C3AE0D4FE7344FD2533264720
Signatures:
Key pair:
q = 1FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
FFA51868783BF2F966B7FCC0148F709A5D03BB5C9B8899C47AEBB6FB71E91386
409
(qlen = 521 bits)
private key:
x = 0FAD06DAA62BA3B25D2FB40133DA757205DE67F5BB0018FEE8C86E1B68C7E75C
AA896EB32F1F47C70855836A6D16FCC1466F6D8FBEC67DB89EC0C08B0E996B83
538
public key: U = xG
Ux = 1894550D0785932E00EAA23B694F213F8C3121F86DC97A04E5A7167DB4E5BCD3
71123D46E45DB6B5D5370A7F20FB633155D38FFA16D2BD761DCAC474B9A2F502
3A4
Uy = 0493101C962CD4D2FDDF782285E64584139C2F91B47F87FF82354D6630F746A2
8A0DB25741B5B34A828008B22ACC23F924FAAFBD4D33F81EA66956DFEAA2BFDF
CF5
Signatures:
Key pair:
q = 4000000000000000000020108A2E0CC0D99F8A5EF
(qlen = 163 bits)
private key:
x = 09A4D6792295A7F730FC3F2B49CBC0F62E862272F
public key: U = xG
Ux = 79AEE090DB05EC252D5CB4452F356BE198A4FF96F
Uy = 782E29634DDC9A31EF40386E896BAA18B53AFA5A3
Signatures:
Key pair:
q = 8000000000000000000000000000069D5BB915BCD46EFB1AD5F173ABDF
(qlen = 232 bits)
private key:
x = 103B2142BDC2A3C3B55080D09DF1808F79336DA2399F5CA7171D1BE9B0
public key: U = xG
Ux = 0682886F36C68473C1A221720C2B12B9BE13458BA907E1C4736595779F2
Uy = 1B20639B41BE0927090999B7817A3B3928D20503A39546044EC13A10309
Signatures:
Key pair:
q = 1FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFE9AE2ED07577265DFF7F94451E061
E163C61
(qlen = 281 bits)
private key:
x = 06A0777356E87B89BA1ED3A3D845357BE332173C8F7A65BDC7DB4FAB3C4CC79A
CC8194E
public key: U = xG
Ux = 25330D0A651D5A20DC6389BC02345117725640AEC3C126612CE444EDD19649BD
ECC03D6
Uy = 505BD60A4B67182474EC4D1C668A73140F70504A68F39EFCD972487E9530E050
8A76193
Signatures:
Key pair:
q = 7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFE5F83B2D4EA20
400EC4557D5ED3E3E7CA5B4B5C83B8E01E5FCF
(qlen = 407 bits)
private key:
x = 29C16768F01D1B8A89FDA85E2EFD73A09558B92A178A2931F359E4D70AD853E5
69CDAF16DAA569758FB4E73089E4525D8BBFCF
public key: U = xG
Ux = 0CF923F523FE34A6E863D8BA45FB1FE6D784C8F219C414EEF4DB8362DBBD3CA7
1AEB28F568668D5D7A0093E2B84F6FAD759DB42
Uy = 13B1C374D5132978A1B1123EBBE9A5C54D1A9D56B09AFDB4ADE93CCD7C4D332E
2916F7D4B9D18578EE3C2E2DE4D2ECE0DE63549
Signatures:
Key pair:
q = 2000000000000000000000000000000000000000000000000000000000000000
0000000131850E1F19A63E4B391A8DB917F4138B630D84BE5D639381E91DEB45
CFE778F637C1001
(qlen = 570 bits)
private key:
x = 0C16F58550D824ED7B95569D4445375D3A490BC7E0194C41A39DEB732C29396C
DF1D66DE02DD1460A816606F3BEC0F32202C7BD18A32D87506466AA92032F131
4ED7B19762B0D22
public key: U = xG
Ux = 6CFB0DF7541CDD4C41EF319EA88E849EFC8605D97779148082EC991C463ED323
19596F9FDF4779C17CAF20EFD9BEB57E9F4ED55BFC52A2FA15CA23BC62B7BF01
9DB59793DD77318
Uy = 1CFC91102F7759A561BD8D5B51AAAEEC7F40E659D67870361990D6DE29F6B4F7
E18AE13BDE5EA5C1F77B23D676F44050C9DBFCCDD7B3756328DDA059779AAE84
46FC5158A75C227
Signatures:
Key pair:
q = 40000000000000000000292FE77E70C12A4234C33
(qlen = 163 bits)
private key:
x = 35318FC447D48D7E6BC93B48617DDDEDF26AA658F
public key: U = xG
Ux = 126CF562D95A1D77D387BA75A3EA3A1407F23425A
Uy = 7D7CB5273C94DA8CA93049AFDA18721C24672BD71
Signatures:
Key pair:
q = 1000000000000000000000000000013E974E72F8A6922031D2603CFE0D7
(qlen = 233 bits)
private key:
x = 07ADC13DD5BF34D1DDEEB50B2CE23B5F5E6D18067306D60C5F6FF11E5D3
public key: U = xG
Ux = 0FB348B3246B473AA7FBB2A01B78D61B62C4221D0F9AB55FC72DB3DF478
Uy = 1162FA1F6C6ACF7FD8D19FC7D74BDD9104076E833898BC4C042A6E6BEBF
Signatures:
Key pair:
q = 3FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEF90399660FC938A90165B042A7CE
FADB307
(qlen = 282 bits)
private key:
x = 14510D4BC44F2D26F4553942C98073C1BD35545CEABB5CC138853C5158D2729E
A408836
public key: U = xG
Ux = 17E3409A13C399F0CA8A192F028D46E3446BCFFCDF51FF8A905ED2DED786E74F
9C3E8A9
Uy = 47EFCBCC31C01D86D1992F7BFAC0277DBD02A6D289274099A2C0F039C8F59F31
8371B0E
Signatures:
Key pair:
q = 10000000000000000000000000000000000000000000000000001E2AAD6A612F
33307BE5FA47C3C9E052F838164CD37D9A21173
(qlen = 409 bits)
private key:
x = 0494994CC325B08E7B4CE038BD9436F90B5E59A2C13C3140CD3AE07C04A01FC4
89F572CE0569A6DB7B8060393DE76330C624177
public key: U = xG
Ux = 1A7055961CF1DA4B9A015B18B1524EF01FDD9B93FAEFC26FB1F2F828A7227B70
31925DA0AC1A8A075C3B33554B222EA859C17E7
Uy = 18105C042F290736088F30AEC7AE7732A45DE47BCE0940113AB8132516D1E059
B0F581FD581A9A3CB3A0AC42A1962738ADB86E6
Signatures:
Key pair:
q = 3FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
FFFFFFFE661CE18FF55987308059B186823851EC7DD9CA1161DE93D5174D66E8
382E9BB2FE84E47
(qlen = 570 bits)
private key:
x = 028A04857F24C1C082DF0D909C0E72F453F2E2340CCB071F0E389BCA2575DA19
124198C57174929AD26E348CF63F78D28021EF5A9BF2D5CBEAF6B7CCB6C4DA82
4DD5C82CFB24E11
public key: U = xG
Ux = 4B4B3CE9377550140B62C1061763AA524814DDCEF37B00CD5CDE94F7792BB0E9
6758E55DA2E9FEA8FF2A8B6830AE1D57A9CA7A77FCB0836BF43EA5454CDD9FEA
D5CCFE7375C6A83
Uy = 4453B18F261E7A0E7570CD72F235EA750438E43946FBEBD2518B696954767AA7
849C1719E18E1C51652C28CA853426F15C09AA4B579487338ABC7F33768FADD6
1B5A3A6443A8189
Signatures:
// ==================================================================
import java.math.BigInteger;
import java.security.InvalidKeyException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
/**
* Deterministic DSA signature generation. This is a sample
* implementation designed to illustrate how deterministic DSA
* chooses the pseudorandom value k when signing a given message.
* This implementation was NOT optimized or hardened against
* side-channel leaks.
*
* An instance is created with a hash function name, which must be
* supported by the underlying Java virtual machine ("SHA-1" and
* "SHA-256" should work everywhere). The data to sign is input
* through the {@code update()} methods. The private key is set with
* {@link #setPrivateKey}. The signature is obtained by calling
* {@link #sign}; alternatively, {@link #signHash} can be used to
* sign some data that has been externally hashed. The private key
* MUST be set before generating the signature itself, but message
* data can be input before setting the key.
*
* Instances are NOT thread-safe. However, once a signature has
* been generated, the same instance can be used again for another
* signature; {@link #setPrivateKey} need not be called again if the
* private key has not changed. {@link #reset} can also be called to
* cancel previously input data. Generating a signature with {@link
* #sign} (not {@link #signHash}) also implicitly causes a
* reset.
*
* ------------------------------------------------------------------
* Copyright (c) 2013 IETF Trust and the persons identified as
* authors of the code. All rights reserved.
*
/**
* Create an instance, using the specified hash function.
* The name is used to obtain from the JVM an implementation
* of the hash function and an implementation of HMAC.
*
* @param hashName the hash function name
* @throws IllegalArgumentException on unsupported name
*/
public DeterministicDSA(String hashName)
{
try {
dig = MessageDigest.getInstance(hashName);
} catch (NoSuchAlgorithmException nsae) {
throw new IllegalArgumentException(nsae);
}
if (hashName.indexOf(’-’) < 0) {
macName = "Hmac" + hashName;
} else {
StringBuilder sb = new StringBuilder();
sb.append("Hmac");
int n = hashName.length();
for (int i = 0; i < n; i ++) {
char c = hashName.charAt(i);
if (c != ’-’) {
sb.append(c);
}
}
macName = sb.toString();
}
try {
hmac = Mac.getInstance(macName);
} catch (NoSuchAlgorithmException nsae) {
throw new IllegalArgumentException(nsae);
}
holen = hmac.getMacLength();
}
/**
* Set the private key.
*
* @param p key parameter: field modulus
* @param q key parameter: subgroup order
* @param g key parameter: generator
* @param x private key
*/
public void setPrivateKey(BigInteger p, BigInteger q,
BigInteger g, BigInteger x)
{
/*
* Perform some basic sanity checks. We do not
* check primality of p or q because that would
* be too expensive.
*
* We reject keys where q is longer than 999 bits,
* because it would complicate signature encoding.
* Normal DSA keys do not have a q longer than 256
* bits anyway.
*/
if (p == null || q == null || g == null || x == null
|| p.signum() <= 0 || q.signum() <= 0
|| g.signum() <= 0 || x.signum() <= 0
|| x.compareTo(q) >= 0 || q.compareTo(p) >= 0
|| q.bitLength() > 999
|| g.compareTo(p) >= 0 || g.bitLength() == 1
|| g.modPow(q, p).bitLength() != 1) {
throw new IllegalArgumentException(
"invalid DSA private key");
}
this.p = p;
this.q = q;
this.g = g;
this.x = x;
qlen = q.bitLength();
if (q.signum() <= 0 || qlen < 8) {
throw new IllegalArgumentException(
"bad group order: " + q);
}
rolen = (qlen + 7) >>> 3;
rlen = rolen * 8;
/*
* Convert the private exponent (x) into a sequence
* of octets.
*/
bx = int2octets(x);
}
/**
/**
* Compute the pseudorandom k for signature generation,
* using the process specified for deterministic DSA.
*
* @param h1 the hashed message
* @return the pseudorandom k to use
*/
private BigInteger computek(byte[] h1)
{
/*
* Convert hash value into an appropriately truncated
* and/or expanded sequence of octets. The private
* key was already processed (into field bx[]).
*/
byte[] bh = bits2octets(h1);
/*
* HMAC is always used with K as key.
* Whenever K is updated, we reset the
* current HMAC key.
*/
/* step b. */
byte[] V = new byte[holen];
for (int i = 0; i < holen; i ++) {
V[i] = 0x01;
}
/* step c. */
byte[] K = new byte[holen];
setHmacKey(K);
/* step d. */
hmac.update(V);
hmac.update((byte)0x00);
hmac.update(bx);
hmac.update(bh);
K = hmac.doFinal();
setHmacKey(K);
/* step e. */
hmac.update(V);
V = hmac.doFinal();
/* step f. */
hmac.update(V);
hmac.update((byte)0x01);
hmac.update(bx);
hmac.update(bh);
K = hmac.doFinal();
setHmacKey(K);
/* step g. */
hmac.update(V);
V = hmac.doFinal();
/* step h. */
byte[] T = new byte[rolen];
for (;;) {
/*
* We want qlen bits, but we support only
* hash functions with an output length
* multiple of 8;acd hence, we will gather
* rlen bits, i.e., rolen octets.
*/
int toff = 0;
while (toff < rolen) {
hmac.update(V);
V = hmac.doFinal();
int cc = Math.min(V.length,
T.length - toff);
System.arraycopy(V, 0, T, toff, cc);
toff += cc;
}
BigInteger k = bits2int(T);
if (k.signum() > 0 && k.compareTo(q) < 0) {
return k;
}
/*
* k is not in the proper range; update
* K and V, and loop.
*/
hmac.update(V);
hmac.update((byte)0x00);
K = hmac.doFinal();
setHmacKey(K);
hmac.update(V);
V = hmac.doFinal();
}
}
/**
* Process one more byte of input data (message to sign).
*
* @param in the extra input byte
*/
public void update(byte in)
{
dig.update(in);
}
/**
* Process some extra bytes of input data (message to sign).
*
* @param in the extra input bytes
*/
public void update(byte[] in)
{
dig.update(in, 0, in.length);
}
/**
* Process some extra bytes of input data (message to sign).
*
* @param in the extra input buffer
* @param off the extra input offset
* @param len the extra input length (in bytes)
*/
public void update(byte[] in, int off, int len)
{
dig.update(in, off, len);
}
/**
* Produce the signature. {@link #setPrivateKey} MUST have
* been called. The signature is computed over the data
* that was input through the {@code update*()} methods.
* This engine is then reset (made ready for a new
* signature generation).
*
/**
* Produce the signature. {@link #setPrivateKey} MUST
* have been called. The signature is computed over the
* provided hash value (data is assumed to have been hashed
* externally). The data that was input through the
* {@code update*()} methods is ignored, but kept.
*
* If the hash output is longer than the subgroup order
* (the length of q, in bits, denoted ’qlen’), then the
* provided value {@code h1} can be truncated, provided that
* at least qlen leading bits are preserved. In other words,
* bit values in {@code h1} beyond the first qlen bits are
* ignored.
*
* @param h1 the hash value
* @return the signature
*/
public byte[] signHash(byte[] h1)
{
if (p == null) {
throw new IllegalStateException(
"no private key set");
}
try {
BigInteger k = computek(h1);
BigInteger r = g.modPow(k, p).mod(q);
BigInteger s = k.modInverse(q).multiply(
bits2int(h1).add(x.multiply(r)))
.mod(q);
/*
* Signature encoding: ASN.1 SEQUENCE of
* two INTEGERs. The conditions on q
* imply that the encoded version of r and
* s is no longer than 127 bytes for each,
* including DER tag and length.
*/
byte[] br = r.toByteArray();
byte[] bs = s.toByteArray();
int ulen = br.length + bs.length + 4;
int slen = ulen + (ulen >= 128 ? 3 : 2);
/**
* Reset this engine. Data input through the {@code
* update*()} methods is discarded. The current private key,
* if one was set, is kept unchanged.
*/
public void reset()
{
dig.reset();
}
}
// ==================================================================
Author’s Address
Thomas Pornin
Quebec, QC
Canada
EMail: [email protected]