Current File : //proc/self/root/proc/self/root/lib64/python2.7/site-packages/Crypto/PublicKey/DSA.pyo |
�
Bd\Rc @ s d Z d Z d d d d d g Z d d l Z e j d d
k r` e j d d k r` d d l Tn d d
l m Z m Z m Z d d l
m Z y d d l m Z Wn e
k
r� d Z n Xd e j f d � � YZ d e f d � � YZ e � Z e j Z e j Z e j Z d S( sx DSA public-key signature algorithm.
DSA_ is a widespread public-key signature algorithm. Its security is
based on the discrete logarithm problem (DLP_). Given a cyclic
group, a generator *g*, and an element *h*, it is hard
to find an integer *x* such that *g^x = h*. The problem is believed
to be difficult, and it has been proved such (and therefore secure) for
more than 30 years.
The group is actually a sub-group over the integers modulo *p*, with *p* prime.
The sub-group order is *q*, which is prime too; it always holds that *(p-1)* is a multiple of *q*.
The cryptographic strength is linked to the magnitude of *p* and *q*.
The signer holds a value *x* (*0<x<q-1*) as private key, and its public
key (*y* where *y=g^x mod p*) is distributed.
In 2012, a sufficient size is deemed to be 2048 bits for *p* and 256 bits for *q*.
For more information, see the most recent ECRYPT_ report.
DSA is reasonably secure for new designs.
The algorithm can only be used for authentication (digital signature).
DSA cannot be used for confidentiality (encryption).
The values *(p,q,g)* are called *domain parameters*;
they are not sensitive but must be shared by both parties (the signer and the verifier).
Different signers can share the same domain parameters with no security
concerns.
The DSA signature is twice as big as the size of *q* (64 bytes if *q* is 256 bit
long).
This module provides facilities for generating new DSA keys and for constructing
them from known components. DSA keys allows you to perform basic signing and
verification.
>>> from Crypto.Random import random
>>> from Crypto.PublicKey import DSA
>>> from Crypto.Hash import SHA
>>>
>>> message = "Hello"
>>> key = DSA.generate(1024)
>>> h = SHA.new(message).digest()
>>> k = random.StrongRandom().randint(1,key.q-1)
>>> sig = key.sign(h,k)
>>> ...
>>> if key.verify(h,sig):
>>> print "OK"
>>> else:
>>> print "Incorrect signature"
.. _DSA: http://en.wikipedia.org/wiki/Digital_Signature_Algorithm
.. _DLP: http://www.cosic.esat.kuleuven.be/publications/talk-78.pdf
.. _ECRYPT: http://www.ecrypt.eu.org/documents/D.SPA.17.pdf
s $Id$t generatet constructt errort DSAImplementationt _DSAobji����Ni i i ( t *( t _DSAt _slowmatht pubkey( t Random( t _fastmathc B s� e Z d Z d d d d d g Z d � Z d � Z d � Z d � Z d
� Z d � Z d � Z
d
� Z d � Z d � Z
d � Z d � Z d � Z d � Z d � Z d � Z d � Z d � Z d � Z RS( sl Class defining an actual DSA key.
:undocumented: __getstate__, __setstate__, __repr__, __getattr__
t yt gt pt qt xc C s | | _ | | _ d S( N( t implementationt key( t selfR R ( ( s: /usr/lib64/python2.7/site-packages/Crypto/PublicKey/DSA.pyt __init__s s c C s? | | j k r t | j | � St d | j j | f � � d S( Ns %s object has no %r attribute( t keydatat getattrR t AttributeErrort __class__t __name__( R t attrname( ( s: /usr/lib64/python2.7/site-packages/Crypto/PublicKey/DSA.pyt __getattr__w s c C s t j j | | | � S( s� Sign a piece of data with DSA.
:Parameter M: The piece of data to sign with DSA. It may
not be longer in bit size than the sub-group order (*q*).
:Type M: byte string or long
:Parameter K: A secret number, chosen randomly in the closed
range *[1,q-1]*.
:Type K: long (recommended) or byte string (not recommended)
:attention: selection of *K* is crucial for security. Generating a
random number larger than *q* and taking the modulus by *q* is
**not** secure, since smaller values will occur more frequently.
Generating a random number systematically smaller than *q-1*
(e.g. *floor((q-1)/8)* random bytes) is also **not** secure. In general,
it shall not be possible for an attacker to know the value of `any
bit of K`__.
:attention: The number *K* shall not be reused for any other
operation and shall be discarded immediately.
:attention: M must be a digest cryptographic hash, otherwise
an attacker may mount an existential forgery attack.
:Return: A tuple with 2 longs.
.. __: http://www.di.ens.fr/~pnguyen/pub_NgSh00.htm
( R t sign( R t Mt K( ( s: /usr/lib64/python2.7/site-packages/Crypto/PublicKey/DSA.pyR s c C s t j j | | | � S( sE Verify the validity of a DSA signature.
:Parameter M: The expected message.
:Type M: byte string or long
:Parameter signature: The DSA signature to verify.
:Type signature: A tuple with 2 longs as return by `sign`
:Return: True if the signature is correct, False otherwise.
( R t verify( R R t signature( ( s: /usr/lib64/python2.7/site-packages/Crypto/PublicKey/DSA.pyR � s c C s t d � � d S( Ns DSA cannot encrypt( t TypeError( R t cR ( ( s: /usr/lib64/python2.7/site-packages/Crypto/PublicKey/DSA.pyt _encrypt� s c C s t d � � d S( Ns DSA cannot decrypt( R ( R R! ( ( s: /usr/lib64/python2.7/site-packages/Crypto/PublicKey/DSA.pyt _decrypt� s c C s t d � � d S( Ns DSA cannot blind( R ( R t mt r( ( s: /usr/lib64/python2.7/site-packages/Crypto/PublicKey/DSA.pyt _blind� s c C s t d � � d S( Ns DSA cannot unblind( R ( R R$ R% ( ( s: /usr/lib64/python2.7/site-packages/Crypto/PublicKey/DSA.pyt _unblind� s c C s | j j | | � S( N( R t _sign( R R$ t k( ( s: /usr/lib64/python2.7/site-packages/Crypto/PublicKey/DSA.pyR( � s c C s"