2009-06-12 21:57:31 +00:00
|
|
|
'''
|
2009-06-22 16:18:53 +00:00
|
|
|
pyecc - Copyright 2009 Slide, Inc.
|
|
|
|
|
|
|
|
This library 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 2.1 of
|
|
|
|
the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
This library 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, write to the
|
|
|
|
Free Software Foundation, Inc.,
|
|
|
|
59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
2009-06-12 21:57:31 +00:00
|
|
|
'''
|
2009-06-12 18:43:44 +00:00
|
|
|
|
|
|
|
import _pyecc
|
|
|
|
|
2009-06-23 19:35:42 +00:00
|
|
|
DEFAULT_CURVE = _pyecc.DEFAULT_CURVE
|
|
|
|
|
2009-06-12 18:43:44 +00:00
|
|
|
class ECC(object):
|
2009-06-12 21:57:31 +00:00
|
|
|
'''
|
|
|
|
The ECC object must be instantiated to work with
|
|
|
|
any encrypted data, as some amount of state is required
|
|
|
|
at once
|
|
|
|
'''
|
|
|
|
def __init__(self, *args, **kwargs):
|
2009-06-16 07:04:19 +00:00
|
|
|
self._private = kwargs.get('private')
|
|
|
|
self._public = kwargs.get('public')
|
2009-07-01 18:36:25 +00:00
|
|
|
self._curve = kwargs.get('curve')
|
2009-06-16 07:04:19 +00:00
|
|
|
self._state = _pyecc.new_state()
|
2009-06-17 18:22:52 +00:00
|
|
|
self._kp = _pyecc.new_keypair(self._public, self._private, self._state)
|
2009-06-12 21:57:31 +00:00
|
|
|
|
2009-06-29 18:37:10 +00:00
|
|
|
@classmethod
|
|
|
|
def generate(cls):
|
|
|
|
keys = _pyecc.keygen()
|
|
|
|
if keys:
|
2009-07-01 18:36:25 +00:00
|
|
|
return cls(public=keys[0], private=keys[1], curve=keys[2])
|
2009-06-29 18:37:10 +00:00
|
|
|
return None
|
2009-06-23 19:36:07 +00:00
|
|
|
|
|
|
|
|
2009-06-12 21:57:31 +00:00
|
|
|
def encrypt(self, plaintext):
|
2009-07-07 18:20:10 +00:00
|
|
|
return _pyecc.encrypt(plaintext, self._kp, self._state)
|
2009-06-12 21:57:31 +00:00
|
|
|
|
|
|
|
def decrypt(self, ciphertext):
|
2009-06-22 15:56:43 +00:00
|
|
|
assert ciphertext, 'You cannot decrypt "nothing"'
|
|
|
|
return _pyecc.decrypt(ciphertext, self._kp, self._state)
|
2009-06-12 21:57:31 +00:00
|
|
|
|
|
|
|
def sign(self, data):
|
2009-06-17 21:31:00 +00:00
|
|
|
if not self._kp:
|
|
|
|
print 'You need a keypair object to verify a signature'
|
|
|
|
return False
|
|
|
|
|
|
|
|
if not self._state:
|
|
|
|
print 'ECC object should have an internal _state member'
|
|
|
|
return False
|
|
|
|
|
|
|
|
return _pyecc.sign(data, self._kp, self._state)
|
2009-06-12 21:57:31 +00:00
|
|
|
|
|
|
|
def verify(self, data, signature):
|
2009-06-17 19:07:57 +00:00
|
|
|
if not self._kp:
|
|
|
|
print 'You need a keypair object to verify a signature'
|
2009-06-16 07:04:19 +00:00
|
|
|
return False
|
|
|
|
|
|
|
|
if not self._state:
|
|
|
|
print 'ECC object should have an internal _state member'
|
|
|
|
return False
|
|
|
|
|
2009-06-17 19:07:57 +00:00
|
|
|
return _pyecc.verify(data, signature, self._kp, self._state)
|