gotr: update provided potr to 1.0.0beta7
This commit is contained in:
@@ -26,8 +26,8 @@ from potr.utils import human_hash, bytes_to_long, unpack, pack_mpi
|
||||
DEFAULT_KEYTYPE = 0x0000
|
||||
pkTypes = {}
|
||||
def registerkeytype(cls):
|
||||
if not hasattr(cls, 'parsePayload'):
|
||||
raise TypeError('registered key types need parsePayload()')
|
||||
if cls.keyType is None:
|
||||
raise TypeError('registered key class needs a type value')
|
||||
pkTypes[cls.keyType] = cls
|
||||
return cls
|
||||
|
||||
@@ -35,12 +35,16 @@ def generateDefaultKey():
|
||||
return pkTypes[DEFAULT_KEYTYPE].generate()
|
||||
|
||||
class PK(object):
|
||||
__slots__ = []
|
||||
keyType = None
|
||||
|
||||
@classmethod
|
||||
def generate(cls):
|
||||
raise NotImplementedError
|
||||
|
||||
@classmethod
|
||||
def parsePayload(cls, data, private=False):
|
||||
raise NotImplementedError
|
||||
|
||||
def sign(self, data):
|
||||
raise NotImplementedError
|
||||
def verify(self, data):
|
||||
@@ -80,13 +84,13 @@ class PK(object):
|
||||
@classmethod
|
||||
def parsePrivateKey(cls, data):
|
||||
implCls, data = cls.getImplementation(data)
|
||||
logging.debug('Got privkey of type %r' % implCls)
|
||||
logging.debug('Got privkey of type %r', implCls)
|
||||
return implCls.parsePayload(data, private=True)
|
||||
|
||||
@classmethod
|
||||
def parsePublicKey(cls, data):
|
||||
implCls, data = cls.getImplementation(data)
|
||||
logging.debug('Got pubkey of type %r' % implCls)
|
||||
logging.debug('Got pubkey of type %r', implCls)
|
||||
return implCls.parsePayload(data)
|
||||
|
||||
def __str__(self):
|
||||
|
||||
@@ -15,18 +15,16 @@
|
||||
# You should have received a copy of the GNU Lesser General Public License
|
||||
# along with this library. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
from Crypto import Cipher, Random
|
||||
from Crypto import Cipher
|
||||
from Crypto.Hash import SHA256 as _SHA256
|
||||
from Crypto.Hash import SHA as _SHA1
|
||||
from Crypto.Hash import SHA as _SHA1
|
||||
from Crypto.Hash import HMAC as _HMAC
|
||||
from Crypto.PublicKey import DSA
|
||||
from Crypto.Random import random
|
||||
from numbers import Number
|
||||
|
||||
from potr.compatcrypto import common
|
||||
from potr.utils import pack_mpi, read_mpi, bytes_to_long, long_to_bytes
|
||||
|
||||
# XXX atfork?
|
||||
RNG = Random.new()
|
||||
from potr.utils import read_mpi, bytes_to_long, long_to_bytes
|
||||
|
||||
def SHA256(data):
|
||||
return _SHA256.new(data).digest()
|
||||
@@ -54,7 +52,6 @@ def AESCTR(key, counter=0):
|
||||
return Cipher.AES.new(key, Cipher.AES.MODE_CTR, counter=counter)
|
||||
|
||||
class Counter(object):
|
||||
__slots__ = ['prefix', 'val']
|
||||
def __init__(self, prefix):
|
||||
self.prefix = prefix
|
||||
self.val = 0
|
||||
@@ -72,17 +69,15 @@ class Counter(object):
|
||||
return '<Counter(p={p!r},v={v!r})>'.format(p=self.prefix, v=self.val)
|
||||
|
||||
def byteprefix(self):
|
||||
return long_to_bytes(self.prefix).rjust(8, b'\0')
|
||||
return long_to_bytes(self.prefix, 8)
|
||||
|
||||
def __call__(self):
|
||||
val = long_to_bytes(self.val)
|
||||
prefix = long_to_bytes(self.prefix)
|
||||
bytesuffix = long_to_bytes(self.val, 8)
|
||||
self.val += 1
|
||||
return self.byteprefix() + val.rjust(8, b'\0')
|
||||
return self.byteprefix() + bytesuffix
|
||||
|
||||
@common.registerkeytype
|
||||
class DSAKey(common.PK):
|
||||
__slots__ = ['priv', 'pub']
|
||||
keyType = 0x0000
|
||||
|
||||
def __init__(self, key=None, private=False):
|
||||
@@ -111,10 +106,10 @@ class DSAKey(common.PK):
|
||||
return SHA1(self.getSerializedPublicPayload())
|
||||
|
||||
def sign(self, data):
|
||||
# 2 <= K <= q = 160bit = 20 byte
|
||||
K = bytes_to_long(RNG.read(19)) + 2
|
||||
# 2 <= K <= q
|
||||
K = random.randrange(2, self.priv.q)
|
||||
r, s = self.priv.sign(data, K)
|
||||
return long_to_bytes(r) + long_to_bytes(s)
|
||||
return long_to_bytes(r, 20) + long_to_bytes(s, 20)
|
||||
|
||||
def verify(self, data, sig):
|
||||
r, s = bytes_to_long(sig[:20]), bytes_to_long(sig[20:])
|
||||
|
||||
Reference in New Issue
Block a user