DSA signature verification with OpenSSL[OpenSSL Home][Home]
In case we will generate a signature with a private key, and then verify it with the associated public key. This then verifies the creator of the signature and that the message has not been changed. The file used in this case contains the text of "This is a test."
|
Method
With a discrete logarithm method, we typically have a base generator (\(g\)) and a prime number (\(p\)). We can then perform operations such as:
\(v=g^x. g^y \pmod p = g^{x+y} \pmod p\)
and:
\(v={(g^x)}^y \pmod p = g^{x.y} \pmod p\)
The Digital Signature Algorithm (DSA) is a standard defined in Federal Information Processing Standard (as FIPS 186) for digital signatures, and is based on discrete logarithms. It was outlined by NIST is 1991, and proposed within the Digital Signature Standard (DSS). This was then standardized with FIPS 186 in 1994, and FIPS 186-4 in 2013. Within FIPS 186-5, it is defined that DSA should not be used for the generation of signatures, but can be used for signature verification. Although DSA has a patent (Patent 5,231,668 by David W. Kravitz, who had previously worked for the NSA), NIST published the standard as a royality-free. The ECDSA method is an extension of DSA, but implemented with elliptic curve (EC) methods. As with most public key signing methods, we take a hash of a message, and then apply a private key to create a signature (\(r,s\)). This is done by creating a random value (k) to produce the signature. The signature is then verified using the associated public key. This then verifies the creator of the signature and that the message has not been changed.
Initially, Bob creates two prime numbers (\(p\) and \(q\)) and generates a generator value of \(g\). Next, he generates his secret key (\(x\)) and then computes his public key:
\(Y=g^{x} \pmod p\)
To create a signature for a message (\(M\)), he creates a random value (\(k\)) and then computes two values for the signature:
\(r = g^{k} \pmod{p} \pmod{q}\)
\(s=(k^{-1}.(H(m)+x.r)) \pmod {q}\)
When Alice receives this signature, she takes Bob's public key \((p,q,g,Y)\) and the message can computes:
\(w = s^{-1} \pmod q\)
\(u_1 = H(M).w \pmod q\)
\(u_2 = r.w \pmod q\)
\(v = (g^{u_1} . y^{u_2}) \pmod {p} \pmod {q}\)
She then checks that \(v\) is equal to \(r\). If so, the signature checks out. This works because:
\(v = g^{h.w}.y^{r.w} = g^{h.w}.g^{x.r.w} = g^{h.w+x.r.w} = g^{h/s+x.r/s} = g^{(H+x.r)/(k^{-1}(H+x.r))} = g^k = r\)
Coding
We can create a keypair and signature for a file named test.txt. In this case, the file contains "This is a test.". Finally we can check the signature with "openssl dgst -sha256 -verify pubkey.pem -signature 1.sig test.txt", and where pubkey.pem is the exported public key:
openssl dsaparam -out dsaparam.pem 1024 openssl gendsa -out 1.pem dsaparam.pem openssl dgst -sha256 -sign 1.pem -out 1.sig test.txt type 1.sig type 1.sig | openssl enc -base64 openssl dsa -in 1.pem - pubout > pubkey.pem type pubkey.pem openssl dgst -sha256 -verify pubkey.pem -signature 1.sig test.txt openssl dsa -in 1.pem - text type dsaparam
and a sample test:
Signature: MD0CHBjsYaB2mnffoEn0ENMbxgQ0wN/vG7FP5x9jCFsCHQCBQgacNRJU4iZrw1Ey IEVna9CslaxQupGw1b60 Public key: -----BEGIN PUBLIC KEY----- MIIBvjCCATMGByqGSM44BAEwggEmAoGBAOtK1Prn07V2idF+jZAe4HIlq7j0muf7 MyXKH8ol220Q7apOP3MrsOpAj4ZE7Wuw5mYlCt7QGCESd+fh4fUA8m6mjnibcsNc DbEkNyajYtWDKQh6BOdhb+Vx2dDXAdYz5iTV4q5yLvXWKxsyraSD4Aqp1Yy6AWr0 /uGBCiOu8/8XAh0AjFrUfCdv1iXLpEp4CbkNUmKHVHezndWeMDvqIQKBgD9slEHP F/KloNGrtA8rXbwVg9BxAWMF9xz0H+t4YE2Z0Ky+pqH2S5w6cIJrHJ24xAkmL6Tg k9g9Jsg83HFv3RPlnuqQJJUiR9KacU9Lwzt4Z1YhiLg8ooRGY4L45tQo1so1YqUL qUuXiAyWqIpQf4FJE4V98yuByNcUPfM+yoAdA4GEAAKBgGOgugGhXLdXqQoYJvTz h84DD9GQoapcM3Nk78M8VWo+fxKtzumcmWwy1G3JgsZqbA0zTF6kmeaKWP3G+oUL fZhWF1hk9HfxNVJM7fX5VgbSROHCG4h6tmAhbvPmlagwdgXbPFMNGqMrbXLLoud3 zHVKFYYyj9ZF1kRZBaRtTb1/ -----END PUBLIC KEY----- Signature check: Verified OK Key pair: Private-Key: (1024 bit) priv: 0f:bc:d0:83:9a:c4:0b:41:bc:5c:ba:59:47:08:b8: fb:2a:5b:82:b5:8f:42:49:bf:0a:42:39:29 pub: 63:a0:ba:01:a1:5c:b7:57:a9:0a:18:26:f4:f3:87: ce:03:0f:d1:90:a1:aa:5c:33:73:64:ef:c3:3c:55: 6a:3e:7f:12:ad:ce:e9:9c:99:6c:32:d4:6d:c9:82: c6:6a:6c:0d:33:4c:5e:a4:99:e6:8a:58:fd:c6:fa: 85:0b:7d:98:56:17:58:64:f4:77:f1:35:52:4c:ed: f5:f9:56:06:d2:44:e1:c2:1b:88:7a:b6:60:21:6e: f3:e6:95:a8:30:76:05:db:3c:53:0d:1a:a3:2b:6d: 72:cb:a2:e7:77:cc:75:4a:15:86:32:8f:d6:45:d6: 44:59:05:a4:6d:4d:bd:7f P: 00:eb:4a:d4:fa:e7:d3:b5:76:89:d1:7e:8d:90:1e: e0:72:25:ab:b8:f4:9a:e7:fb:33:25:ca:1f:ca:25: db:6d:10:ed:aa:4e:3f:73:2b:b0:ea:40:8f:86:44: ed:6b:b0:e6:66:25:0a:de:d0:18:21:12:77:e7:e1: e1:f5:00:f2:6e:a6:8e:78:9b:72:c3:5c:0d:b1:24: 37:26:a3:62:d5:83:29:08:7a:04:e7:61:6f:e5:71: d9:d0:d7:01:d6:33:e6:24:d5:e2:ae:72:2e:f5:d6: 2b:1b:32:ad:a4:83:e0:0a:a9:d5:8c:ba:01:6a:f4: fe:e1:81:0a:23:ae:f3:ff:17 Q: 00:8c:5a:d4:7c:27:6f:d6:25:cb:a4:4a:78:09:b9: 0d:52:62:87:54:77:b3:9d:d5:9e:30:3b:ea:21 G: 3f:6c:94:41:cf:17:f2:a5:a0:d1:ab:b4:0f:2b:5d: bc:15:83:d0:71:01:63:05:f7:1c:f4:1f:eb:78:60: 4d:99:d0:ac:be:a6:a1:f6:4b:9c:3a:70:82:6b:1c: 9d:b8:c4:09:26:2f:a4:e0:93:d8:3d:26:c8:3c:dc: 71:6f:dd:13:e5:9e:ea:90:24:95:22:47:d2:9a:71: 4f:4b:c3:3b:78:67:56:21:88:b8:3c:a2:84:46:63: 82:f8:e6:d4:28:d6:ca:35:62:a5:0b:a9:4b:97:88: 0c:96:a8:8a:50:7f:81:49:13:85:7d:f3:2b:81:c8: d7:14:3d:f3:3e:ca:80:1d -----BEGIN DSA PRIVATE KEY----- MIIBygIBAAKBgQDrStT659O1donRfo2QHuByJau49Jrn+zMlyh/KJdttEO2qTj9z K7DqQI+GRO1rsOZmJQre0BghEnfn4eH1APJupo54m3LDXA2xJDcmo2LVgykIegTn YW/lcdnQ1wHWM+Yk1eKuci711isbMq2kg+AKqdWMugFq9P7hgQojrvP/FwIdAIxa 1Hwnb9Yly6RKeAm5DVJih1R3s53VnjA76iECgYA/bJRBzxfypaDRq7QPK128FYPQ cQFjBfcc9B/reGBNmdCsvqah9kucOnCCaxyduMQJJi+k4JPYPSbIPNxxb90T5Z7q kCSVIkfSmnFPS8M7eGdWIYi4PKKERmOC+ObUKNbKNWKlC6lLl4gMlqiKUH+BSROF ffMrgcjXFD3zPsqAHQKBgGOgugGhXLdXqQoYJvTzh84DD9GQoapcM3Nk78M8VWo+ fxKtzumcmWwy1G3JgsZqbA0zTF6kmeaKWP3G+oULfZhWF1hk9HfxNVJM7fX5VgbS ROHCG4h6tmAhbvPmlagwdgXbPFMNGqMrbXLLoud3zHVKFYYyj9ZF1kRZBaRtTb1/ AhwPvNCDmsQLQbxcullHCLj7KluCtY9CSb8KQjkp -----END DSA PRIVATE KEY----- -----BEGIN DSA PARAMETERS----- MIIBJgKBgQDrStT659O1donRfo2QHuByJau49Jrn+zMlyh/KJdttEO2qTj9zK7Dq QI+GRO1rsOZmJQre0BghEnfn4eH1APJupo54m3LDXA2xJDcmo2LVgykIegTnYW/l cdnQ1wHWM+Yk1eKuci711isbMq2kg+AKqdWMugFq9P7hgQojrvP/FwIdAIxa1Hwn b9Yly6RKeAm5DVJih1R3s53VnjA76iECgYA/bJRBzxfypaDRq7QPK128FYPQcQFj Bfcc9B/reGBNmdCsvqah9kucOnCCaxyduMQJJi+k4JPYPSbIPNxxb90T5Z7qkCSV IkfSmnFPS8M7eGdWIYi4PKKERmOC+ObUKNbKNWKlC6lLl4gMlqiKUH+BSROFffMr gcjXFD3zPsqAHQ== -----END DSA PARAMETERS-----