diff options
author | Arthur de Jong <arthur@arthurdejong.org> | 2014-05-29 14:33:03 +0200 |
---|---|---|
committer | Arthur de Jong <arthur@arthurdejong.org> | 2014-05-29 15:10:25 +0200 |
commit | 0738c94bdfbac6ceefdb1080bbeede2ddaa5ed11 (patch) | |
tree | ed1bf41f34c23e7d59210fb1670baa2786d23c9a | |
parent | 76ef42bf1009e542b90814dc0b629e0ee5b0356c (diff) |
Raise exception when key derivation fails
This also renames the internal function that implements the derivation.
-rw-r--r-- | pskc/encryption.py | 13 | ||||
-rw-r--r-- | pskc/exceptions.py | 5 |
2 files changed, 16 insertions, 2 deletions
diff --git a/pskc/encryption.py b/pskc/encryption.py index cd5720a..3700d17 100644 --- a/pskc/encryption.py +++ b/pskc/encryption.py @@ -136,14 +136,23 @@ class KeyDerivation(object): if prf is not None: self.pbkdf2_prf = prf.attrib.get('Algorithm') - def generate(self, password): + def derive(self, password): """Derive a key from the password.""" + from pskc.exceptions import KeyDerivationError + if self.algorithm is None: + raise KeyDerivationError('No algorithm specified') if self.algorithm.endswith('#pbkdf2'): from Crypto.Protocol.KDF import PBKDF2 # TODO: support pseudorandom function (prf) + if self.pbkdf2_prf: + raise KeyDerivationError( + 'Pseudorandom function unsupported: %r' % self.pbkdf2_prf) return PBKDF2( password, self.pbkdf2_salt, dkLen=self.pbkdf2_key_length, count=self.pbkdf2_iterations, prf=None) + else: + raise KeyDerivationError( + 'Unsupported algorithm: %r' % self.algorithm) class Encryption(object): @@ -193,4 +202,4 @@ class Encryption(object): def derive_key(self, password): """Derive a key from the password.""" - self.key = self.derivation.generate(password) + self.key = self.derivation.derive(password) diff --git a/pskc/exceptions.py b/pskc/exceptions.py index 801de20..7fde416 100644 --- a/pskc/exceptions.py +++ b/pskc/exceptions.py @@ -42,3 +42,8 @@ class DecryptionError(PSKCError): The encrypted value as available but something went wrong with decrypting it.""" pass + + +class KeyDerivationError(PSKCError): + """There was a problem performing the key derivation.""" + pass |