From 0738c94bdfbac6ceefdb1080bbeede2ddaa5ed11 Mon Sep 17 00:00:00 2001 From: Arthur de Jong Date: Thu, 29 May 2014 14:33:03 +0200 Subject: Raise exception when key derivation fails This also renames the internal function that implements the derivation. --- pskc/encryption.py | 13 +++++++++++-- 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 -- cgit v1.2.3