Welcome to mirror list, hosted at ThFree Co, Russian Federation.

common.py « compatcrypto « potr « gotr - dev.gajim.org/gajim/gajim-plugins.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 5d6af40bc54cd07efc521dfbc407ffb72a1663c3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#    Copyright 2012 Kjell Braden <afflux@pentabarf.de>
#
#    This file is part of the python-potr library.
#
#    python-potr is free software; you can redistribute it and/or modify
#    it under the terms of the GNU Lesser General Public License as published by
#    the Free Software Foundation; either version 3 of the License, or
#    any later version.
#
#    python-potr is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Lesser General Public License for more details.
#
#    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/>.

# some python3 compatibilty
from __future__ import unicode_literals

import logging
import struct

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()')
    pkTypes[cls.keyType] = cls
    return cls

def generateDefaultKey():
    return pkTypes[DEFAULT_KEYTYPE].generate()

class PK(object):
    __slots__ = []

    @classmethod
    def generate(cls):
        raise NotImplementedError

    def sign(self, data):
        raise NotImplementedError
    def verify(self, data):
        raise NotImplementedError
    def fingerprint(self):
        raise NotImplementedError

    def serializePublicKey(self):
        return struct.pack(b'!H', self.keyType) \
                + self.getSerializedPublicPayload()

    def getSerializedPublicPayload(self):
        buf = b''
        for x in self.getPublicPayload():
            buf += pack_mpi(x)
        return buf

    def getPublicPayload(self):
        raise NotImplementedError

    def serializePrivateKey(self):
        return struct.pack(b'!H', self.keyType) \
                + self.getSerializedPrivatePayload()

    def getSerializedPrivatePayload(self):
        buf = b''
        for x in self.getPrivatePayload():
            buf += pack_mpi(x)
        return buf

    def getPrivatePayload(self):
        raise NotImplementedError

    def cfingerprint(self):
        return '{0:040x}'.format(bytes_to_long(self.fingerprint()))

    @classmethod
    def parsePrivateKey(cls, data):
        implCls, data = cls.getImplementation(data)
        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)
        return implCls.parsePayload(data)

    def __str__(self):
        return human_hash(self.cfingerprint())
    def __repr__(self):
        return '<{cls}(fpr=\'{fpr}\')>'.format(
                cls=self.__class__.__name__, fpr=str(self))

    @staticmethod
    def getImplementation(data):
        typeid, data = unpack(b'!H', data)
        cls = pkTypes.get(typeid, None)
        if cls is None:
            raise NotImplementedError('unknown typeid %r' % typeid)
        return cls, data