| 1 |
DSA |
| 2 |
=== |
| 3 |
|
| 4 |
DSA_ is a widespread public key signature algorithm. Its security is |
| 5 |
based on the discrete logarithm problem (DLP_). Given a cyclic |
| 6 |
group, a generator *g*, and an element *h*, it is hard |
| 7 |
to find an integer *x* such that :math:`g^x = h`. The problem is believed |
| 8 |
to be difficult, and it has been proved such (and therefore secure) for |
| 9 |
more than 30 years. |
| 10 |
|
| 11 |
The group is actually a sub-group over the integers modulo *p*, with *p* prime. |
| 12 |
The sub-group order is *q*, which is prime too; it always holds that *(p-1)* is a multiple of *q*. |
| 13 |
The cryptographic strength is linked to the magnitude of *p* and *q*. |
| 14 |
The signer holds a value *x* (*0<x<q-1*) as private key, and its public |
| 15 |
key (*y* where :math:`y=g^x \text{ mod } p`) is distributed. |
| 16 |
|
| 17 |
In 2017, a sufficient size is deemed to be 2048 bits for *p* and 256 bits for *q*. |
| 18 |
For more information, see the most recent ECRYPT_ report. |
| 19 |
|
| 20 |
The algorithm can only be used for authentication (digital signature). |
| 21 |
DSA cannot be used for confidentiality (encryption). |
| 22 |
|
| 23 |
The values *(p,q,g)* are called *domain parameters*; |
| 24 |
they are not sensitive but must be shared by both parties (the signer and the verifier). |
| 25 |
Different signers can share the same domain parameters with no security |
| 26 |
concerns. |
| 27 |
|
| 28 |
The DSA signature is twice as big as the size of *q* (64 bytes if *q* is 256 bit |
| 29 |
long). |
| 30 |
|
| 31 |
This module provides facilities for generating new DSA keys and for constructing |
| 32 |
them from known components. |
| 33 |
|
| 34 |
As an example, this is how you generate a new DSA key pair, save the public |
| 35 |
key in a file called ``public_key.pem``, sign a message (with |
| 36 |
:mod:`Crypto.Signature.DSS`), and verify it:: |
| 37 |
|
| 38 |
>>> from Crypto.PublicKey import DSA |
| 39 |
>>> from Crypto.Signature import DSS |
| 40 |
>>> from Crypto.Hash import SHA256 |
| 41 |
>>> |
| 42 |
>>> # Create a new DSA key |
| 43 |
>>> key = DSA.generate(2048) |
| 44 |
>>> f = open("public_key.pem", "w") |
| 45 |
>>> f.write(key.publickey().export_key()) |
| 46 |
>>> f.close() |
| 47 |
>>> |
| 48 |
>>> # Sign a message |
| 49 |
>>> message = b"Hello" |
| 50 |
>>> hash_obj = SHA256.new(message) |
| 51 |
>>> signer = DSS.new(key, 'fips-186-3') |
| 52 |
>>> signature = signer.sign(hash_obj) |
| 53 |
>>> |
| 54 |
>>> # Load the public key |
| 55 |
>>> f = open("public_key.pem", "r") |
| 56 |
>>> hash_obj = SHA256.new(message) |
| 57 |
>>> pub_key = DSA.import_key(f.read()) |
| 58 |
>>> verifier = DSS.new(pub_key, 'fips-186-3') |
| 59 |
>>> |
| 60 |
>>> # Verify the authenticity of the message |
| 61 |
>>> try: |
| 62 |
>>> verifier.verify(hash_obj, signature) |
| 63 |
>>> print "The message is authentic." |
| 64 |
>>> except ValueError: |
| 65 |
>>> print "The message is not authentic." |
| 66 |
|
| 67 |
.. _DSA: http://en.wikipedia.org/wiki/Digital_Signature_Algorithm |
| 68 |
.. _DLP: http://www.cosic.esat.kuleuven.be/publications/talk-78.pdf |
| 69 |
.. _ECRYPT: http://www.ecrypt.eu.org/documents/D.SPA.17.pdf |
| 70 |
|
| 71 |
.. automodule:: Crypto.PublicKey.DSA |
| 72 |
:members: |