From 9d42cec98b4bc866229abda3d28d76e1d3b5f78f Mon Sep 17 00:00:00 2001 From: Simon Warta Date: Mon, 1 May 2017 16:17:32 +0200 Subject: Break long lines --- src/python/botan2.py | 43 +++++++++++++++++++++++++++++-------------- 1 file changed, 29 insertions(+), 14 deletions(-) (limited to 'src/python/botan2.py') diff --git a/src/python/botan2.py b/src/python/botan2.py index 9d04b83db..f28638334 100755 --- a/src/python/botan2.py +++ b/src/python/botan2.py @@ -338,17 +338,21 @@ def pbkdf_timed(algo, password, out_len, ms_to_run=300, salt=rng().get(12)): c_void_p, c_size_t, c_size_t, POINTER(c_size_t)] out_buf = create_string_buffer(out_len) iterations = c_size_t(0) - botan.botan_pbkdf_timed(_ctype_str(algo), out_buf, out_len, _ctype_str(password), salt, len(salt), ms_to_run, byref(iterations)) + botan.botan_pbkdf_timed( + _ctype_str(algo), out_buf, out_len, _ctype_str(password), + salt, len(salt), ms_to_run, byref(iterations)) return (salt, iterations.value, out_buf.raw) """ KDF """ def kdf(algo, secret, out_len, salt, label): - botan.botan_kdf.argtypes = [c_char_p, POINTER(c_char), c_size_t, POINTER(c_char), c_size_t, POINTER(c_char), c_size_t, POINTER(c_char), c_size_t] + botan.botan_kdf.argtypes = [c_char_p, POINTER(c_char), c_size_t, POINTER(c_char), c_size_t, + POINTER(c_char), c_size_t, POINTER(c_char), c_size_t] out_buf = create_string_buffer(out_len) out_sz = c_size_t(out_len) - botan.botan_kdf(_ctype_str(algo), out_buf, out_sz, secret, len(secret), salt, len(salt), label, len(label)) + botan.botan_kdf(_ctype_str(algo), out_buf, out_sz, secret, len(secret), + salt, len(salt), label, len(label)) return out_buf.raw[0:out_sz.value] """ @@ -582,7 +586,8 @@ class pk_op_key_agreement(object): if not self.op: raise Exception("No key agreement for you") - self.m_public_value = _call_fn_returning_vec(0, lambda b, bl: botan.botan_pk_op_key_agreement_export_public(key.privkey, b, bl)) + self.m_public_value = _call_fn_returning_vec( + 0, lambda b, bl: botan.botan_pk_op_key_agreement_export_public(key.privkey, b, bl)) def __del__(self): botan.botan_pk_op_key_agreement_destroy.argtypes = [c_void_p] @@ -623,7 +628,8 @@ class x509_cert(object): def time_starts(self): botan.botan_x509_cert_get_time_starts.argtypes = [c_void_p, POINTER(c_char), POINTER(c_size_t)] - starts = _call_fn_returning_string(16, lambda b, bl: botan.botan_x509_cert_get_time_starts(self.x509_cert, b, bl)) + starts = _call_fn_returning_string( + 16, lambda b, bl: botan.botan_x509_cert_get_time_starts(self.x509_cert, b, bl)) if len(starts) == 13: # UTC time struct_time = time.strptime(starts, "%y%m%d%H%M%SZ") @@ -637,7 +643,8 @@ class x509_cert(object): def time_expires(self): botan.botan_x509_cert_get_time_expires.argtypes = [c_void_p, POINTER(c_char), POINTER(c_size_t)] - expires = _call_fn_returning_string(16, lambda b, bl: botan.botan_x509_cert_get_time_expires(self.x509_cert, b, bl)) + expires = _call_fn_returning_string( + 16, lambda b, bl: botan.botan_x509_cert_get_time_expires(self.x509_cert, b, bl)) if len(expires) == 13: # UTC time struct_time = time.strptime(expires, "%y%m%d%H%M%SZ") @@ -650,30 +657,36 @@ class x509_cert(object): def to_string(self): botan.botan_x509_cert_to_string.argtypes = [c_void_p, POINTER(c_char), POINTER(c_size_t)] - return _call_fn_returning_string(0, lambda b, bl: botan.botan_x509_cert_to_string(self.x509_cert, b, bl)) + return _call_fn_returning_string( + 0, lambda b, bl: botan.botan_x509_cert_to_string(self.x509_cert, b, bl)) def fingerprint(self, hash_algo='SHA-256'): botan.botan_x509_cert_get_fingerprint.argtypes = [c_void_p, c_char_p, POINTER(c_char), POINTER(c_size_t)] n = hash_function(hash_algo).output_length() * 3 - return _call_fn_returning_string(n, lambda b, bl: botan.botan_x509_cert_get_fingerprint(self.x509_cert, _ctype_str(hash_algo), b, bl)) + return _call_fn_returning_string( + n, lambda b, bl: botan.botan_x509_cert_get_fingerprint(self.x509_cert, _ctype_str(hash_algo), b, bl)) def serial_number(self): botan.botan_x509_cert_get_serial_number.argtypes = [c_void_p, POINTER(c_char), POINTER(c_size_t)] - return _call_fn_returning_vec(0, lambda b, bl: botan.botan_x509_cert_get_serial_number(self.x509_cert, b, bl)) + return _call_fn_returning_vec( + 0, lambda b, bl: botan.botan_x509_cert_get_serial_number(self.x509_cert, b, bl)) def authority_key_id(self): botan.botan_x509_cert_get_authority_key_id.argtypes = [c_void_p, POINTER(c_char), POINTER(c_size_t)] - return _call_fn_returning_vec(0, lambda b, bl: botan.botan_x509_cert_get_authority_key_id(self.x509_cert, b, bl)) + return _call_fn_returning_vec( + 0, lambda b, bl: botan.botan_x509_cert_get_authority_key_id(self.x509_cert, b, bl)) def subject_key_id(self): botan.botan_x509_cert_get_subject_key_id.argtypes = [c_void_p, POINTER(c_char), POINTER(c_size_t)] - return _call_fn_returning_vec(0, lambda b, bl: botan.botan_x509_cert_get_subject_key_id(self.x509_cert, b, bl)) + return _call_fn_returning_vec( + 0, lambda b, bl: botan.botan_x509_cert_get_subject_key_id(self.x509_cert, b, bl)) def subject_public_key_bits(self): botan.botan_x509_cert_get_public_key_bits.argtypes = [c_void_p, POINTER(c_char), POINTER(c_size_t)] - return _call_fn_returning_vec(0, lambda b, bl: botan.botan_x509_cert_get_public_key_bits(self.x509_cert, b, bl)) + return _call_fn_returning_vec( + 0, lambda b, bl: botan.botan_x509_cert_get_public_key_bits(self.x509_cert, b, bl)) def subject_public_key(self): botan.botan_x509_cert_get_public_key.argtypes = [c_void_p, c_void_p] @@ -683,8 +696,10 @@ class x509_cert(object): return public_key(pub) def subject_dn(self, key, index): - botan.botan_x509_cert_get_subject_dn.argtypes = [c_void_p, c_char_p, c_size_t, POINTER(c_char), POINTER(c_size_t)] - return _call_fn_returning_string(0, lambda b, bl: botan.botan_x509_cert_get_subject_dn(self.x509_cert, _ctype_str(key), index, b, bl)) + botan.botan_x509_cert_get_subject_dn.argtypes = [ + c_void_p, c_char_p, c_size_t, POINTER(c_char), POINTER(c_size_t)] + return _call_fn_returning_string( + 0, lambda b, bl: botan.botan_x509_cert_get_subject_dn(self.x509_cert, _ctype_str(key), index, b, bl)) """ -- cgit v1.2.3 From 4726af876fa18c78b984d51da527016677fdbe5c Mon Sep 17 00:00:00 2001 From: Simon Warta Date: Mon, 1 May 2017 16:21:52 +0200 Subject: Remove trailing whitespace --- src/python/botan2.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/python/botan2.py') diff --git a/src/python/botan2.py b/src/python/botan2.py index f28638334..2a1131df5 100755 --- a/src/python/botan2.py +++ b/src/python/botan2.py @@ -814,7 +814,7 @@ def test(): mce_decrypt = mceies_decrypt(mce_priv, 'ChaCha20Poly1305', mce_ciphertext, mce_ad) print(" mceies plaintext \'%s\' (%d)" % (mce_plaintext, len(mce_plaintext))) - + # Since mceies_decrypt() returns bytes in Python3, the following line # needs .decode('utf-8') to convert mce_decrypt from bytes to a # text string (Unicode). -- cgit v1.2.3 From f6cdd9e57e9a39ec8b41d0ee529f38867aa29330 Mon Sep 17 00:00:00 2001 From: Simon Warta Date: Mon, 1 May 2017 16:31:30 +0200 Subject: Use comments to organize code sections --- src/python/botan2.py | 75 ++++++++++++++++++++++++++++------------------------ 1 file changed, 40 insertions(+), 35 deletions(-) (limited to 'src/python/botan2.py') diff --git a/src/python/botan2.py b/src/python/botan2.py index 2a1131df5..5272b35e8 100755 --- a/src/python/botan2.py +++ b/src/python/botan2.py @@ -1,6 +1,7 @@ #!/usr/bin/env python -"""Python wrapper of the botan crypto library +""" +Python wrapper of the botan crypto library http://botan.randombit.net (C) 2015,2017 Jack Lloyd @@ -21,9 +22,9 @@ from binascii import hexlify, unhexlify, b2a_base64 from datetime import datetime import time -""" -Module initialization -""" +# +# Module initialization +# if sys.platform == 'darwin': botan = CDLL('libbotan-2.dylib') else: @@ -32,7 +33,9 @@ else: if botan.botan_ffi_supports_api(20151015) == False: raise Exception("The Botan library does not support the FFI API expected by this version of the Python module") +# # Internal utilities +# def _call_fn_returning_vec(guess, fn): buf = create_string_buffer(guess) @@ -86,9 +89,9 @@ def hex_encode(buf): def hex_decode(buf): return unhexlify(buf.encode('ascii')) -""" -Versions -""" +# +# Versions +# def version_major(): return botan.botan_version_major() @@ -102,9 +105,9 @@ def version_string(): botan.botan_version_string.restype = c_char_p return botan.botan_version_string().decode('ascii') -""" -RNG -""" +# +# RNG +# class rng(object): # Can also use type "system" def __init__(self, rng_type='system'): @@ -132,9 +135,9 @@ class rng(object): else: return None -""" -Hash function -""" +# +# Hash function +# class hash_function(object): def __init__(self, algo): botan.botan_hash_init.argtypes = [c_void_p, c_char_p, c_uint32] @@ -170,9 +173,9 @@ class hash_function(object): botan.botan_hash_final(self.hash, out) return _ctype_bufout(out) -""" -Message authentication codes -""" +# +# Message authentication codes +# class message_authentication_code(object): def __init__(self, algo): botan.botan_mac_init.argtypes = [c_void_p, c_char_p, c_uint32] @@ -324,9 +327,9 @@ def check_bcrypt(passwd, bcrypt): rc = botan.botan_bcrypt_is_valid(_ctype_str(passwd), bcrypt) return rc == 0 -""" -PBKDF -""" +# +# PBKDF +# def pbkdf(algo, password, out_len, iterations=10000, salt=rng().get(12)): botan.botan_pbkdf.argtypes = [c_char_p, POINTER(c_char), c_size_t, c_char_p, c_void_p, c_size_t, c_size_t] out_buf = create_string_buffer(out_len) @@ -343,9 +346,9 @@ def pbkdf_timed(algo, password, out_len, ms_to_run=300, salt=rng().get(12)): salt, len(salt), ms_to_run, byref(iterations)) return (salt, iterations.value, out_buf.raw) -""" -KDF -""" +# +# KDF +# def kdf(algo, secret, out_len, salt, label): botan.botan_kdf.argtypes = [c_char_p, POINTER(c_char), c_size_t, POINTER(c_char), c_size_t, POINTER(c_char), c_size_t, POINTER(c_char), c_size_t] @@ -355,9 +358,9 @@ def kdf(algo, secret, out_len, salt, label): salt, len(salt), label, len(label)) return out_buf.raw[0:out_sz.value] -""" -Public and private keys -""" +# +# Public and private keys +# class public_key(object): def __init__(self, obj=c_void_p(0)): self.pubkey = obj @@ -542,10 +545,11 @@ class pk_op_verify(object): return True return False -""" -MCEIES encryption -Must be used with McEliece keys -""" + +# +# MCEIES encryption +# Must be used with McEliece keys +# def mceies_encrypt(mce, rng, aead, pt, ad): botan.botan_mceies_encrypt.argtypes = [c_void_p, c_void_p, c_char_p, POINTER(c_char), c_size_t, POINTER(c_char), c_size_t, POINTER(c_char), POINTER(c_size_t)] @@ -604,9 +608,10 @@ class pk_op_key_agreement(object): botan.botan_pk_op_key_agreement(self.op, b, bl, other, len(other), salt, len(salt))) -""" -X.509 certificates -""" + +# +# X.509 certificates +# class x509_cert(object): def __init__(self, filename=None, buf=None): if filename is None and buf is None: @@ -702,9 +707,9 @@ class x509_cert(object): 0, lambda b, bl: botan.botan_x509_cert_get_subject_dn(self.x509_cert, _ctype_str(key), index, b, bl)) -""" -Tests and examples -""" +# +# Tests and examples +# def test(): def test_version(): -- cgit v1.2.3 From 8ac62d948e4255176015e0c61a4200958822384a Mon Sep 17 00:00:00 2001 From: Simon Warta Date: Mon, 1 May 2017 16:35:46 +0200 Subject: Silence pylint invalid class name warnings --- src/python/botan2.py | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) (limited to 'src/python/botan2.py') diff --git a/src/python/botan2.py b/src/python/botan2.py index 5272b35e8..43b2aba3e 100755 --- a/src/python/botan2.py +++ b/src/python/botan2.py @@ -108,7 +108,7 @@ def version_string(): # # RNG # -class rng(object): +class rng(object): # pylint: disable=invalid-name # Can also use type "system" def __init__(self, rng_type='system'): botan.botan_rng_init.argtypes = [c_void_p, c_char_p] @@ -138,7 +138,7 @@ class rng(object): # # Hash function # -class hash_function(object): +class hash_function(object): # pylint: disable=invalid-name def __init__(self, algo): botan.botan_hash_init.argtypes = [c_void_p, c_char_p, c_uint32] flags = c_uint32(0) # always zero in this API version @@ -176,7 +176,7 @@ class hash_function(object): # # Message authentication codes # -class message_authentication_code(object): +class message_authentication_code(object): # pylint: disable=invalid-name def __init__(self, algo): botan.botan_mac_init.argtypes = [c_void_p, c_char_p, c_uint32] flags = c_uint32(0) # always zero in this API version @@ -215,7 +215,7 @@ class message_authentication_code(object): botan.botan_mac_final(self.mac, out) return _ctype_bufout(out) -class cipher(object): +class cipher(object): # pylint: disable=invalid-name def __init__(self, algo, encrypt=True): botan.botan_cipher_init.argtypes = [c_void_p, c_char_p, c_uint32] flags = 0 if encrypt else 1 @@ -361,7 +361,7 @@ def kdf(algo, secret, out_len, salt, label): # # Public and private keys # -class public_key(object): +class public_key(object): # pylint: disable=invalid-name def __init__(self, obj=c_void_p(0)): self.pubkey = obj @@ -395,7 +395,7 @@ class public_key(object): botan.botan_pubkey_fingerprint(self.pubkey, _ctype_str(hash), buf, byref(buf_len)) return hex_encode(buf[0:buf_len.value]) -class private_key(object): +class private_key(object): # pylint: disable=invalid-name def __init__(self, alg, param, rng): botan.botan_privkey_create_rsa.argtypes = [c_void_p, c_void_p, c_size_t] botan.botan_privkey_create_ecdsa.argtypes = [c_void_p, c_void_p, c_char_p] @@ -442,7 +442,7 @@ class private_key(object): botan.botan_privkey_export(self.privkey, buf, byref(buf_len)) return buf[0:buf_len.value] -class pk_op_encrypt(object): +class pk_op_encrypt(object): # pylint: disable=invalid-name def __init__(self, key, padding): botan.botan_pk_op_encrypt_create.argtypes = [c_void_p, c_void_p, c_char_p, c_uint32] self.op = c_void_p(0) @@ -473,7 +473,7 @@ class pk_op_encrypt(object): return outbuf.raw[0:outbuf_sz.value] -class pk_op_decrypt(object): +class pk_op_decrypt(object): # pylint: disable=invalid-name def __init__(self, key, padding): botan.botan_pk_op_decrypt_create.argtypes = [c_void_p, c_void_p, c_char_p, c_uint32] self.op = c_void_p(0) @@ -497,7 +497,7 @@ class pk_op_decrypt(object): botan.botan_pk_op_decrypt(self.op, outbuf, byref(outbuf_sz), _ctype_bits(msg), ll) return outbuf.raw[0:outbuf_sz.value] -class pk_op_sign(object): +class pk_op_sign(object): # pylint: disable=invalid-name def __init__(self, key, padding): botan.botan_pk_op_sign_create.argtypes = [c_void_p, c_void_p, c_char_p, c_uint32] self.op = c_void_p(0) @@ -521,7 +521,7 @@ class pk_op_sign(object): botan.botan_pk_op_sign_finish(self.op, rng.rng, outbuf, byref(outbuf_sz)) return outbuf.raw[0:outbuf_sz.value] -class pk_op_verify(object): +class pk_op_verify(object): # pylint: disable=invalid-name def __init__(self, key, padding): botan.botan_pk_op_verify_create.argtypes = [c_void_p, c_void_p, c_char_p, c_uint32] self.op = c_void_p(0) @@ -580,7 +580,7 @@ def mceies_decrypt(mce, aead, pt, ad): len(ad), b, bl)) -class pk_op_key_agreement(object): +class pk_op_key_agreement(object): # pylint: disable=invalid-name def __init__(self, key, kdf): botan.botan_pk_op_key_agreement_create.argtypes = [c_void_p, c_void_p, c_char_p, c_uint32] botan.botan_pk_op_key_agreement_export_public.argtypes = [c_void_p, POINTER(c_char), POINTER(c_size_t)] @@ -612,7 +612,7 @@ class pk_op_key_agreement(object): # # X.509 certificates # -class x509_cert(object): +class x509_cert(object): # pylint: disable=invalid-name def __init__(self, filename=None, buf=None): if filename is None and buf is None: raise Exception("No filename or buf given") -- cgit v1.2.3 From e3e4cd7052b4081c792e65b0970d77c34f3c61eb Mon Sep 17 00:00:00 2001 From: Simon Warta Date: Thu, 4 May 2017 00:18:49 +0200 Subject: Silence invalid name warning for module "botan" --- src/python/botan2.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/python/botan2.py') diff --git a/src/python/botan2.py b/src/python/botan2.py index 43b2aba3e..8c7259f4f 100755 --- a/src/python/botan2.py +++ b/src/python/botan2.py @@ -26,9 +26,9 @@ import time # Module initialization # if sys.platform == 'darwin': - botan = CDLL('libbotan-2.dylib') + botan = CDLL('libbotan-2.dylib') # pylint: disable=invalid-name else: - botan = CDLL('libbotan-2.so') + botan = CDLL('libbotan-2.so') # pylint: disable=invalid-name if botan.botan_ffi_supports_api(20151015) == False: raise Exception("The Botan library does not support the FFI API expected by this version of the Python module") -- cgit v1.2.3 From cf5a80e57362763b035fab1f7dda1294b84a23e0 Mon Sep 17 00:00:00 2001 From: Simon Warta Date: Mon, 1 May 2017 16:40:22 +0200 Subject: Avoid redefining build-in name "hash" this breaks applications using a parameter labels, e.g. pubkey.fingerprint(hash='SHA-512') --- src/python/botan2.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'src/python/botan2.py') diff --git a/src/python/botan2.py b/src/python/botan2.py index 8c7259f4f..1a140a8c9 100755 --- a/src/python/botan2.py +++ b/src/python/botan2.py @@ -384,15 +384,15 @@ class public_key(object): # pylint: disable=invalid-name flag = 1 if pem else 0 return _call_fn_returning_vec(0, lambda b, bl: botan.botan_pubkey_export(self.pubkey, b, bl, flag)) - def fingerprint(self, hash='SHA-256'): + def fingerprint(self, hash_algorithm='SHA-256'): botan.botan_pubkey_fingerprint.argtypes = [c_void_p, c_char_p, POINTER(c_char), POINTER(c_size_t)] - n = hash_function(hash).output_length() + n = hash_function(hash_algorithm).output_length() buf = create_string_buffer(n) buf_len = c_size_t(n) - botan.botan_pubkey_fingerprint(self.pubkey, _ctype_str(hash), buf, byref(buf_len)) + botan.botan_pubkey_fingerprint(self.pubkey, _ctype_str(hash_algorithm), buf, byref(buf_len)) return hex_encode(buf[0:buf_len.value]) class private_key(object): # pylint: disable=invalid-name -- cgit v1.2.3 From 12678c3d409c958be1f02177608fb7a13d5d5247 Mon Sep 17 00:00:00 2001 From: Simon Warta Date: Mon, 1 May 2017 16:57:03 +0200 Subject: Fix various pylint warnings --- src/python/botan2.py | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) (limited to 'src/python/botan2.py') diff --git a/src/python/botan2.py b/src/python/botan2.py index 1a140a8c9..8576fc6a6 100755 --- a/src/python/botan2.py +++ b/src/python/botan2.py @@ -30,7 +30,7 @@ if sys.platform == 'darwin': else: botan = CDLL('libbotan-2.so') # pylint: disable=invalid-name -if botan.botan_ffi_supports_api(20151015) == False: +if botan.botan_ffi_supports_api(20151015) is False: raise Exception("The Botan library does not support the FFI API expected by this version of the Python module") # @@ -59,16 +59,18 @@ def _call_fn_returning_string(guess, fn): return v.decode('ascii')[:-1] def _ctype_str(s): - assert type(s) == type("") + assert isinstance(s, str) if sys.version_info[0] < 3: return s else: return s.encode('utf-8') def _ctype_bits(s): - # TODO typecheck for bytes in python3? if sys.version_info[0] < 3: - return s + if isinstance(s, str): + return s + else: + assert False else: if isinstance(s, bytes): return s @@ -323,8 +325,8 @@ def bcrypt(passwd, rng, work_factor=10): b = b[:-1] return b -def check_bcrypt(passwd, bcrypt): - rc = botan.botan_bcrypt_is_valid(_ctype_str(passwd), bcrypt) +def check_bcrypt(passwd, passwd_hash): + rc = botan.botan_bcrypt_is_valid(_ctype_str(passwd), passwd_hash) return rc == 0 # @@ -581,12 +583,12 @@ def mceies_decrypt(mce, aead, pt, ad): b, bl)) class pk_op_key_agreement(object): # pylint: disable=invalid-name - def __init__(self, key, kdf): + def __init__(self, key, kdf_name): botan.botan_pk_op_key_agreement_create.argtypes = [c_void_p, c_void_p, c_char_p, c_uint32] botan.botan_pk_op_key_agreement_export_public.argtypes = [c_void_p, POINTER(c_char), POINTER(c_size_t)] self.op = c_void_p(0) flags = c_uint32(0) # always zero in this ABI - botan.botan_pk_op_key_agreement_create(byref(self.op), key.privkey, kdf, flags) + botan.botan_pk_op_key_agreement_create(byref(self.op), key.privkey, kdf_name, flags) if not self.op: raise Exception("No key agreement for you") -- cgit v1.2.3 From aa6a4bd9a67d06258771b716508479e8c04c66f2 Mon Sep 17 00:00:00 2001 From: Simon Warta Date: Thu, 4 May 2017 00:24:42 +0200 Subject: Add base exception type: BotanException --- src/python/botan2.py | 52 ++++++++++++++++++++++++++++++---------------------- 1 file changed, 30 insertions(+), 22 deletions(-) (limited to 'src/python/botan2.py') diff --git a/src/python/botan2.py b/src/python/botan2.py index 8576fc6a6..3d4ae034a 100755 --- a/src/python/botan2.py +++ b/src/python/botan2.py @@ -22,6 +22,14 @@ from binascii import hexlify, unhexlify, b2a_base64 from datetime import datetime import time + +# +# Base exception for all exceptions raised from this module +# +class BotanException(Exception): + pass + + # # Module initialization # @@ -31,7 +39,7 @@ else: botan = CDLL('libbotan-2.so') # pylint: disable=invalid-name if botan.botan_ffi_supports_api(20151015) is False: - raise Exception("The Botan library does not support the FFI API expected by this version of the Python module") + raise BotanException("The Botan library does not support the FFI API expected by this version of the Python module") # # Internal utilities @@ -47,7 +55,7 @@ def _call_fn_returning_vec(guess, fn): #print("Calling again with %d" % (buf_len.value)) return _call_fn_returning_vec(buf_len.value, fn) else: - raise Exception("Call failed: %d" % (rc)) + raise BotanException("Call failed: %d" % (rc)) assert buf_len.value <= len(buf) return buf.raw[0:buf_len.value] @@ -117,7 +125,7 @@ class rng(object): # pylint: disable=invalid-name self.rng = c_void_p(0) rc = botan.botan_rng_init(byref(self.rng), _ctype_str(rng_type)) if rc != 0 or self.rng is None: - raise Exception("No rng " + rng_type + " available") + raise BotanException("No rng " + rng_type + " available") def __del__(self): botan.botan_rng_destroy.argtypes = [c_void_p] @@ -147,7 +155,7 @@ class hash_function(object): # pylint: disable=invalid-name self.hash = c_void_p(0) rc = botan.botan_hash_init(byref(self.hash), _ctype_str(algo), flags) if rc != 0 or self.hash is None: - raise Exception("No hash " + algo + " for you!") + raise BotanException("No hash " + algo + " for you!") def __del__(self): botan.botan_hash_destroy.argtypes = [c_void_p] @@ -163,7 +171,7 @@ class hash_function(object): # pylint: disable=invalid-name rc = botan.botan_hash_output_length(self.hash, byref(l)) if rc == 0: return l.value - raise Exception("botan_hash_output_length failed") + raise BotanException("botan_hash_output_length failed") def update(self, x): botan.botan_hash_update.argtypes = [c_void_p, POINTER(c_char), c_size_t] @@ -185,7 +193,7 @@ class message_authentication_code(object): # pylint: disable=invalid-name self.mac = c_void_p(0) rc = botan.botan_mac_init(byref(self.mac), _ctype_str(algo), flags) if rc != 0 or self.mac is None: - raise Exception("No mac " + algo + " for you!") + raise BotanException("No mac " + algo + " for you!") def __del__(self): botan.botan_mac_destroy.argtypes = [c_void_p] @@ -201,7 +209,7 @@ class message_authentication_code(object): # pylint: disable=invalid-name rc = botan.botan_mac_output_length(self.mac, byref(l)) if rc == 0: return l.value - raise Exception("botan_mac_output_length failed") + raise BotanException("botan_mac_output_length failed") def set_key(self, key): botan.botan_mac_set_key.argtypes = [c_void_p, POINTER(c_char), c_size_t] @@ -224,7 +232,7 @@ class cipher(object): # pylint: disable=invalid-name self.cipher = c_void_p(0) rc = botan.botan_cipher_init(byref(self.cipher), _ctype_str(algo), flags) if rc != 0 or self.cipher is None: - raise Exception("No cipher " + algo + " for you!") + raise BotanException("No cipher " + algo + " for you!") def __del__(self): botan.botan_cipher_destroy.argtypes = [c_void_p] @@ -261,7 +269,7 @@ class cipher(object): # pylint: disable=invalid-name botan.botan_cipher_valid_nonce_length.argtypes = [c_void_p, c_size_t] rc = botan.botan_cipher_valid_nonce_length(self.cipher, nonce_len) if rc < 0: - raise Exception('Error calling valid_nonce_length') + raise BotanException('Error calling valid_nonce_length') return True if rc == 1 else False def clear(self): @@ -319,7 +327,7 @@ def bcrypt(passwd, rng, work_factor=10): flags = c_uint32(0) rc = botan.botan_bcrypt_generate(out, byref(out_len), _ctype_str(passwd), rng.rng, c_size_t(work_factor), flags) if rc != 0: - raise Exception('botan bcrypt failed, error %s' % (rc)) + raise BotanException('botan bcrypt failed, error %s' % (rc)) b = out.raw[0:out_len.value-1] if b[-1] == '\x00': b = b[:-1] @@ -415,10 +423,10 @@ class private_key(object): # pylint: disable=invalid-name elif alg in ['mce', 'mceliece']: botan.botan_privkey_create_mceliece(byref(self.privkey), rng.rng, param[0], param[1]) else: - raise Exception('Unknown public key algo ' + alg) + raise BotanException('Unknown public key algo ' + alg) if self.privkey is None: - raise Exception('Error creating ' + alg + ' key') + raise BotanException('Error creating ' + alg + ' key') def __del__(self): botan.botan_privkey_destroy.argtypes = [c_void_p] @@ -452,7 +460,7 @@ class pk_op_encrypt(object): # pylint: disable=invalid-name print("Padding is ", padding) botan.botan_pk_op_encrypt_create(byref(self.op), key.pubkey, _ctype_str(padding), flags) if not self.op: - raise Exception("No pk op for you") + raise BotanException("No pk op for you") def __del__(self): botan.botan_pk_op_encrypt_destroy.argtypes = [c_void_p] @@ -482,7 +490,7 @@ class pk_op_decrypt(object): # pylint: disable=invalid-name flags = c_uint32(0) # always zero in this ABI botan.botan_pk_op_decrypt_create(byref(self.op), key.privkey, _ctype_str(padding), flags) if not self.op: - raise Exception("No pk op for you") + raise BotanException("No pk op for you") def __del__(self): botan.botan_pk_op_decrypt_destroy.argtypes = [c_void_p] @@ -506,7 +514,7 @@ class pk_op_sign(object): # pylint: disable=invalid-name flags = c_uint32(0) # always zero in this ABI botan.botan_pk_op_sign_create(byref(self.op), key.privkey, _ctype_str(padding), flags) if not self.op: - raise Exception("No pk op for you") + raise BotanException("No pk op for you") def __del__(self): botan.botan_pk_op_sign_destroy.argtypes = [c_void_p] @@ -530,7 +538,7 @@ class pk_op_verify(object): # pylint: disable=invalid-name flags = c_uint32(0) # always zero in this ABI botan.botan_pk_op_verify_create(byref(self.op), key.pubkey, _ctype_str(padding), flags) if not self.op: - raise Exception("No pk op for you") + raise BotanException("No pk op for you") def __del__(self): botan.botan_pk_op_verify_destroy.argtypes = [c_void_p] @@ -590,7 +598,7 @@ class pk_op_key_agreement(object): # pylint: disable=invalid-name flags = c_uint32(0) # always zero in this ABI botan.botan_pk_op_key_agreement_create(byref(self.op), key.privkey, kdf_name, flags) if not self.op: - raise Exception("No key agreement for you") + raise BotanException("No key agreement for you") self.m_public_value = _call_fn_returning_vec( 0, lambda b, bl: botan.botan_pk_op_key_agreement_export_public(key.privkey, b, bl)) @@ -617,9 +625,9 @@ class pk_op_key_agreement(object): # pylint: disable=invalid-name class x509_cert(object): # pylint: disable=invalid-name def __init__(self, filename=None, buf=None): if filename is None and buf is None: - raise Exception("No filename or buf given") + raise BotanException("No filename or buf given") if filename is not None and buf is not None: - raise Exception("Both filename and buf given") + raise BotanException("Both filename and buf given") elif filename is not None: botan.botan_x509_cert_load_file.argtypes = [POINTER(c_void_p), c_char_p] self.x509_cert = c_void_p(0) @@ -644,7 +652,7 @@ class x509_cert(object): # pylint: disable=invalid-name # Generalized time struct_time = time.strptime(starts, "%Y%m%d%H%M%SZ") else: - raise Exception("Wrong date/time format") + raise BotanException("Wrong date/time format") return datetime.fromtimestamp(time.mktime(struct_time)) @@ -659,7 +667,7 @@ class x509_cert(object): # pylint: disable=invalid-name # Generalized time struct_time = time.strptime(expires, "%Y%m%d%H%M%SZ") else: - raise Exception("Wrong date/time format") + raise BotanException("Wrong date/time format") return datetime.fromtimestamp(time.mktime(struct_time)) def to_string(self): @@ -927,7 +935,7 @@ def test(): for field in dn_fields: try: print("%s: %s" % (field, cert.subject_dn(field, 0))) - except Exception: + except BotanException: print("Field: %s not found in certificate" % field) print(cert.to_string()) -- cgit v1.2.3 From 2e8f6126be89c67a1f28af6c62d26c806c14b29d Mon Sep 17 00:00:00 2001 From: Simon Warta Date: Thu, 4 May 2017 00:29:41 +0200 Subject: Work around name conflict of symbol "rng" --- src/python/botan2.py | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) (limited to 'src/python/botan2.py') diff --git a/src/python/botan2.py b/src/python/botan2.py index 3d4ae034a..92194dfb7 100755 --- a/src/python/botan2.py +++ b/src/python/botan2.py @@ -316,7 +316,7 @@ class cipher(object): # pylint: disable=invalid-name return self._update(txt, True) -def bcrypt(passwd, rng, work_factor=10): +def bcrypt(passwd, rng_instance, work_factor=10): """ Bcrypt password hashing """ @@ -325,7 +325,8 @@ def bcrypt(passwd, rng, work_factor=10): out_len = c_size_t(64) out = create_string_buffer(out_len.value) flags = c_uint32(0) - rc = botan.botan_bcrypt_generate(out, byref(out_len), _ctype_str(passwd), rng.rng, c_size_t(work_factor), flags) + rc = botan.botan_bcrypt_generate(out, byref(out_len), _ctype_str(passwd), + rng_instance.rng, c_size_t(work_factor), flags) if rc != 0: raise BotanException('botan bcrypt failed, error %s' % (rc)) b = out.raw[0:out_len.value-1] @@ -406,7 +407,7 @@ class public_key(object): # pylint: disable=invalid-name return hex_encode(buf[0:buf_len.value]) class private_key(object): # pylint: disable=invalid-name - def __init__(self, alg, param, rng): + def __init__(self, alg, param, rng_instance): botan.botan_privkey_create_rsa.argtypes = [c_void_p, c_void_p, c_size_t] botan.botan_privkey_create_ecdsa.argtypes = [c_void_p, c_void_p, c_char_p] botan.botan_privkey_create_ecdh.argtypes = [c_void_p, c_void_p, c_char_p] @@ -415,13 +416,13 @@ class private_key(object): # pylint: disable=invalid-name self.privkey = c_void_p(0) if alg == 'rsa': - botan.botan_privkey_create_rsa(byref(self.privkey), rng.rng, param) + botan.botan_privkey_create_rsa(byref(self.privkey), rng_instance.rng, param) elif alg == 'ecdsa': - botan.botan_privkey_create_ecdsa(byref(self.privkey), rng.rng, _ctype_str(param)) + botan.botan_privkey_create_ecdsa(byref(self.privkey), rng_instance.rng, _ctype_str(param)) elif alg == 'ecdh': - botan.botan_privkey_create_ecdh(byref(self.privkey), rng.rng, _ctype_str(param)) + botan.botan_privkey_create_ecdh(byref(self.privkey), rng_instance.rng, _ctype_str(param)) elif alg in ['mce', 'mceliece']: - botan.botan_privkey_create_mceliece(byref(self.privkey), rng.rng, param[0], param[1]) + botan.botan_privkey_create_mceliece(byref(self.privkey), rng_instance.rng, param[0], param[1]) else: raise BotanException('Unknown public key algo ' + alg) @@ -466,7 +467,7 @@ class pk_op_encrypt(object): # pylint: disable=invalid-name botan.botan_pk_op_encrypt_destroy.argtypes = [c_void_p] botan.botan_pk_op_encrypt_destroy(self.op) - def encrypt(self, msg, rng): + def encrypt(self, msg, rng_instance): botan.botan_pk_op_encrypt.argtypes = [c_void_p, c_void_p, POINTER(c_char), POINTER(c_size_t), POINTER(c_char), c_size_t] @@ -478,7 +479,7 @@ class pk_op_encrypt(object): # pylint: disable=invalid-name #if sys.version_info[0] > 2: # msg = cast(msg, c_char_p) # ll = c_size_t(ll) - botan.botan_pk_op_encrypt(self.op, rng.rng, outbuf, byref(outbuf_sz), msg, ll) + botan.botan_pk_op_encrypt(self.op, rng_instance.rng, outbuf, byref(outbuf_sz), msg, ll) #print("encrypt: outbuf_sz.value=%d" % outbuf_sz.value) return outbuf.raw[0:outbuf_sz.value] @@ -524,11 +525,11 @@ class pk_op_sign(object): # pylint: disable=invalid-name botan.botan_pk_op_sign_update.argtypes = [c_void_p, POINTER(c_char), c_size_t] botan.botan_pk_op_sign_update(self.op, _ctype_str(msg), len(msg)) - def finish(self, rng): + def finish(self, rng_instance): botan.botan_pk_op_sign_finish.argtypes = [c_void_p, c_void_p, POINTER(c_char), POINTER(c_size_t)] outbuf_sz = c_size_t(4096) #?!?! outbuf = create_string_buffer(outbuf_sz.value) - botan.botan_pk_op_sign_finish(self.op, rng.rng, outbuf, byref(outbuf_sz)) + botan.botan_pk_op_sign_finish(self.op, rng_instance.rng, outbuf, byref(outbuf_sz)) return outbuf.raw[0:outbuf_sz.value] class pk_op_verify(object): # pylint: disable=invalid-name @@ -560,13 +561,13 @@ class pk_op_verify(object): # pylint: disable=invalid-name # MCEIES encryption # Must be used with McEliece keys # -def mceies_encrypt(mce, rng, aead, pt, ad): +def mceies_encrypt(mce, rng_instance, aead, pt, ad): botan.botan_mceies_encrypt.argtypes = [c_void_p, c_void_p, c_char_p, POINTER(c_char), c_size_t, POINTER(c_char), c_size_t, POINTER(c_char), POINTER(c_size_t)] return _call_fn_returning_vec(0, lambda b, bl: botan.botan_mceies_encrypt(mce.pubkey, - rng.rng, + rng_instance.rng, _ctype_str(aead), _ctype_bits(pt), len(pt), -- cgit v1.2.3 From 6c06b1fe1fea3d333522bf83688de6d34ee94604 Mon Sep 17 00:00:00 2001 From: Simon Warta Date: Thu, 4 May 2017 01:25:51 +0200 Subject: Remove comment: FIXME without further description --- src/python/botan2.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/python/botan2.py') diff --git a/src/python/botan2.py b/src/python/botan2.py index 92194dfb7..6f9d98a3d 100755 --- a/src/python/botan2.py +++ b/src/python/botan2.py @@ -83,7 +83,7 @@ def _ctype_bits(s): if isinstance(s, bytes): return s elif isinstance(s, str): - return s.encode('utf-8') # FIXME + return s.encode('utf-8') else: assert False -- cgit v1.2.3 From b9900ce421a5553d47f79a6e53348871da1ed4f0 Mon Sep 17 00:00:00 2001 From: Simon Warta Date: Thu, 4 May 2017 01:32:36 +0200 Subject: Don't use len() to verify emptyness --- src/python/botan2.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'src/python/botan2.py') diff --git a/src/python/botan2.py b/src/python/botan2.py index 6f9d98a3d..6d1ee25fe 100755 --- a/src/python/botan2.py +++ b/src/python/botan2.py @@ -806,7 +806,10 @@ def test(): enc.set_key(key) enc.start(iv) - assert len(enc.update('')) == 0 + + update_result = enc.update('') + assert not update_result + ct = enc.finish(pt) print(" ciphertext %s (%d)" % (hex_encode(ct), len(ct))) -- cgit v1.2.3