wbond / oscrypto

Compiler-free Python crypto library backed by the OS, supporting CPython and PyPy
MIT License
320 stars 70 forks source link

Bug in _openssl's aes_cbc_no_padding_encrypt? #24

Closed nathforge closed 5 years ago

nathforge commented 6 years ago

I'm trying to call oscrypto.symmetric.aes_cbc_no_padding_encrypt with a 16 byte key, 16 byte IV, and 64 bytes of data.

This throws an error:

>>> import oscrypto.symmetric
>>>
>>> key  = [0]   * 16
>>> iv   = [1]   * 16
>>> data = [255] * 64
>>>
>>> oscrypto.symmetric.aes_cbc_no_padding_encrypt(key=bytes(key), data=bytes(data), iv=bytes(iv))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "oscrypto/_openssl/symmetric.py", line 74, in aes_cbc_no_padding_encrypt
    return (iv, _encrypt(cipher, key, data, iv, False))
  File "oscrypto/_openssl/symmetric.py", line 606, in _encrypt
    raise ValueError('padding must be specified')
ValueError: padding must be specified

Here's the abridged source of oscrypto._openssl.symmetric:

def aes_cbc_no_padding_encrypt(key, data, iv):
    cipher = _calculate_aes_cipher(key)
    return (iv, _encrypt(cipher, key, data, iv, False))

def _calculate_aes_cipher(key):
    if len(key) == 16:
        return 'aes128'

def _encrypt(cipher, key, data, iv, padding):
    if cipher != 'rc4' and not padding:
        raise ValueError('padding must be specified')
    # ...

As I read it, aes_cbc_no_padding_encrypt always passes cipher != rc4 and padding = False to _encrypt, resulting in the exception.


Happy to help with a fix, just wanted to check I'm not doing anything crazy?

wbond commented 6 years ago

Sorry that I didn't notice this issue for so long. I'm hoping to have a few minutes to look into it over the weekend.

alanlonglong commented 5 years ago

how to solve it ?

wbond commented 5 years ago

This should be fixed by 67d0017957ef024e18cbe5522e365771ed1b0e94

wbond commented 4 years ago

This is part of 1.0.0 which is now available on PyPi https://pypi.org/project/oscrypto/