Skip to content

Commit

Permalink
lint: Do not import * from exceptions
Browse files Browse the repository at this point in the history
Either import `TLSError` (`check_error()` remains low level)
or import as `_exc`.
  • Loading branch information
Synss committed May 24, 2020
1 parent 967fdfd commit 0b6b612
Show file tree
Hide file tree
Showing 19 changed files with 132 additions and 133 deletions.
17 changes: 9 additions & 8 deletions src/mbedtls/_md.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ cimport mbedtls._md as _md
from libc.stdlib cimport malloc, free

import binascii
from mbedtls.exceptions import *

import mbedtls.exceptions as _exc


__MD_NAME = (
Expand Down Expand Up @@ -81,7 +82,7 @@ cdef class MDBase:
raise ValueError("{} not available".format(name))
cdef char *c_name = name_
self._info = _md.mbedtls_md_info_from_string(c_name)
check_error(_md.mbedtls_md_setup(&self._ctx, self._info, _hmac))
_exc.check_error(_md.mbedtls_md_setup(&self._ctx, self._info, _hmac))

def __cinit__(self):
"""Initialize an `md_context` (as NONE)."""
Expand Down Expand Up @@ -129,7 +130,7 @@ cdef class MDBase:
if not output:
raise MemoryError()
try:
check_error(self._finish(output))
_exc.check_error(self._finish(output))
return output[:self.digest_size]
finally:
free(output)
Expand Down Expand Up @@ -166,14 +167,14 @@ cdef class Hash(_md.MDBase):
"""
def __init__(self, name, buffer=None):
super().__init__(name, 0)
check_error(_md.mbedtls_md_starts(&self._ctx))
_exc.check_error(_md.mbedtls_md_starts(&self._ctx))
self.update(buffer)

def update(self, const unsigned char[:] buffer):
"""Update the hash object with the `buffer`."""
if buffer is None or buffer.size == 0:
return
check_error(
_exc.check_error(
_md.mbedtls_md_update(&self._ctx, &buffer[0], buffer.size))

cdef _finish(self, unsigned char *output):
Expand All @@ -183,7 +184,7 @@ cdef class Hash(_md.MDBase):
def copy(self):
"""Return a copy ("clone") of the hash object."""
obj = Hash(self.name)
check_error(_md.mbedtls_md_clone(&obj._ctx, &self._ctx))
_exc.check_error(_md.mbedtls_md_clone(&obj._ctx, &self._ctx))
return obj


Expand Down Expand Up @@ -216,14 +217,14 @@ cdef class Hmac(_md.MDBase):
super().__init__(name, 1)
if not key.size:
key = b"\0"
check_error(_md.mbedtls_md_hmac_starts(&self._ctx, &key[0], key.size))
_exc.check_error(_md.mbedtls_md_hmac_starts(&self._ctx, &key[0], key.size))
self.update(buffer)

def update(self, const unsigned char[:] buffer):
"""Update the HMAC object with `buffer`."""
if buffer is None or buffer.size == 0:
return
check_error(
_exc.check_error(
_md.mbedtls_md_hmac_update(&self._ctx, &buffer[0], buffer.size))

cdef _finish(self, unsigned char *output):
Expand Down
2 changes: 1 addition & 1 deletion src/mbedtls/cipher/AES.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"""


from mbedtls.exceptions import *
from mbedtls.exceptions import TLSError

from . import _cipher

Expand Down
2 changes: 1 addition & 1 deletion src/mbedtls/cipher/ARC4.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
at RSA Security."""


from mbedtls.exceptions import *
from mbedtls.exceptions import TLSError

from . import _cipher

Expand Down
2 changes: 1 addition & 1 deletion src/mbedtls/cipher/ARIA.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"""


from mbedtls.exceptions import *
from mbedtls.exceptions import TLSError

from . import _cipher

Expand Down
2 changes: 1 addition & 1 deletion src/mbedtls/cipher/Blowfish.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"""Blowfish cipher designed by Bruce Schneier in 1993."""


from mbedtls.exceptions import *
from mbedtls.exceptions import TLSError

from . import _cipher

Expand Down
2 changes: 1 addition & 1 deletion src/mbedtls/cipher/CHACHA20.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"""


from mbedtls.exceptions import *
from mbedtls.exceptions import TLSError

from . import _cipher

Expand Down
2 changes: 1 addition & 1 deletion src/mbedtls/cipher/Camellia.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"""Camellia cipher developed by Japan's Mitsubishi an NTT in 2000."""


from mbedtls.exceptions import *
from mbedtls.exceptions import TLSError

from . import _cipher

Expand Down
2 changes: 1 addition & 1 deletion src/mbedtls/cipher/DES.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
in the 70's."""


from mbedtls.exceptions import *
from mbedtls.exceptions import TLSError

from . import _cipher

Expand Down
2 changes: 1 addition & 1 deletion src/mbedtls/cipher/DES3.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
Triple DES, or DES-EDE3)."""


from mbedtls.exceptions import *
from mbedtls.exceptions import TLSError

from . import _cipher

Expand Down
2 changes: 1 addition & 1 deletion src/mbedtls/cipher/DES3dbl.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
or DES-EDE)."""


from mbedtls.exceptions import *
from mbedtls.exceptions import TLSError

from . import _cipher

Expand Down
24 changes: 12 additions & 12 deletions src/mbedtls/cipher/_cipher.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ except ImportError:

import enum

from mbedtls.exceptions import *
import mbedtls.exceptions as _exc


CIPHER_NAME = (
Expand Down Expand Up @@ -186,20 +186,20 @@ cdef class _CipherBase:
if mode in {Mode.CBC, Mode.CFB} and iv.size == 0:
raise ValueError("mode requires an IV")
if cipher_name not in get_supported_ciphers():
raise TLSError(msg="unsupported cipher: %r" % cipher_name)
raise _exc.TLSError(msg="unsupported cipher: %r" % cipher_name)

check_error(_cipher.mbedtls_cipher_setup(
_exc.check_error(_cipher.mbedtls_cipher_setup(
&self._enc_ctx,
_cipher.mbedtls_cipher_info_from_string(cipher_name)))
check_error(_cipher.mbedtls_cipher_setup(
_exc.check_error(_cipher.mbedtls_cipher_setup(
&self._dec_ctx,
_cipher.mbedtls_cipher_info_from_string(cipher_name)))

if key is not None:
check_error(_cipher.mbedtls_cipher_setkey(
_exc.check_error(_cipher.mbedtls_cipher_setkey(
&self._enc_ctx, &key[0], 8 * key.size,
_cipher.MBEDTLS_ENCRYPT))
check_error(_cipher.mbedtls_cipher_setkey(
_exc.check_error(_cipher.mbedtls_cipher_setkey(
&self._dec_ctx, &key[0], 8 * key.size,
_cipher.MBEDTLS_DECRYPT))
self._iv = iv
Expand Down Expand Up @@ -263,7 +263,7 @@ cdef class Cipher(_CipherBase):
const _cipher.mbedtls_operation_t operation):
"""Generic all-in-one encryption/decryption."""
if input.size == 0:
check_error(-0x6280) # Raise full block expected error.
_exc.check_error(-0x6280) # Raise full block expected error.
cdef size_t olen
cdef size_t sz = input.size + self.block_size
cdef unsigned char* output = <unsigned char*>malloc(
Expand All @@ -273,7 +273,7 @@ cdef class Cipher(_CipherBase):
try:
# We can call `check_error` directly here because we return a
# python object.
check_error(_cipher.mbedtls_cipher_crypt(
_exc.check_error(_cipher.mbedtls_cipher_crypt(
&self._enc_ctx if operation is _cipher.MBEDTLS_ENCRYPT else
&self._dec_ctx,
&iv[0] if iv.size else NULL, iv.size,
Expand Down Expand Up @@ -304,7 +304,7 @@ cdef class AEADCipher(_CipherBase):
const unsigned char[:] ad,
const unsigned char[:] input):
if input.size == 0:
check_error(-0x6280) # Raise full block expected error.
_exc.check_error(-0x6280) # Raise full block expected error.
assert iv.size != 0
cdef size_t olen
cdef size_t sz = input.size + self.block_size
Expand All @@ -319,7 +319,7 @@ cdef class AEADCipher(_CipherBase):
pad = <const unsigned char*> &ad[0]
else:
pad = NULL
check_error(_cipher.mbedtls_cipher_auth_encrypt(
_exc.check_error(_cipher.mbedtls_cipher_auth_encrypt(
&self._enc_ctx,
&iv[0], iv.size, pad, ad.size,
&input[0], input.size, output, &olen,
Expand All @@ -334,7 +334,7 @@ cdef class AEADCipher(_CipherBase):
const unsigned char[:] input,
const unsigned char[:] tag):
if input.size == 0:
check_error(-0x6280) # Raise full block expected error.
_exc.check_error(-0x6280) # Raise full block expected error.
assert iv.size != 0
assert tag.size == 16
cdef size_t olen
Expand All @@ -348,7 +348,7 @@ cdef class AEADCipher(_CipherBase):
pad = <const unsigned char*> &ad[0]
else:
pad = NULL
check_error(_cipher.mbedtls_cipher_auth_decrypt(
_exc.check_error(_cipher.mbedtls_cipher_auth_decrypt(
&self._dec_ctx,
&iv[0], iv.size, pad, ad.size,
&input[0], input.size, output, &olen,
Expand Down
6 changes: 3 additions & 3 deletions src/mbedtls/exceptions.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@

from libc.stdlib cimport malloc, free

cimport mbedtls.exceptions as _err
cimport mbedtls.exceptions as _exc


__all__ = ("TLSError", "check_error")
__all__ = ("TLSError",)


class TLSError(Exception):
Expand All @@ -33,7 +33,7 @@ class TLSError(Exception):
if not buffer:
raise MemoryError()
try:
_err.mbedtls_strerror(self.err, &buffer[0], buflen)
_exc.mbedtls_strerror(self.err, &buffer[0], buflen)
output = bytes(buffer[:buflen])
try:
olen = output.index(b"\0")
Expand Down
1 change: 0 additions & 1 deletion src/mbedtls/hash.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@


import mbedtls._md as _md
from mbedtls.exceptions import *

algorithms_guaranteed = _md.algorithms_guaranteed
algorithms_available = _md.algorithms_available
Expand Down
8 changes: 4 additions & 4 deletions src/mbedtls/hkdf.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ from libc.stdlib cimport malloc, free
cimport mbedtls._md as _hmac
cimport mbedtls.hkdf as _hkdf

import mbedtls.exceptions as _exc
import mbedtls.hmac as _hmac
from mbedtls.exceptions import *


__all__ = ("hkdf", "extract", "expand")
Expand Down Expand Up @@ -50,7 +50,7 @@ def hkdf(
if not okm:
raise MemoryError()
try:
check_error(_hkdf.mbedtls_hkdf(
_exc.check_error(_hkdf.mbedtls_hkdf(
hmac._info,
NULL if salt is None or salt.size == 0 else &salt[0],
0 if salt is None else salt.size,
Expand Down Expand Up @@ -96,7 +96,7 @@ def extract(
if not prk:
raise MemoryError()
try:
check_error(_hkdf.mbedtls_hkdf_extract(
_exc.check_error(_hkdf.mbedtls_hkdf_extract(
hmac._info,
NULL if salt is None or not salt.size else &salt[0],
0 if salt is None else salt.size,
Expand Down Expand Up @@ -141,7 +141,7 @@ def expand(
if not okm:
raise MemoryError()
try:
check_error(_hkdf.mbedtls_hkdf_expand(
_exc.check_error(_hkdf.mbedtls_hkdf_expand(
hmac._info,
&prk[0], prk.size,
NULL if not info.size else &info[0],
Expand Down
1 change: 0 additions & 1 deletion src/mbedtls/hmac.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@


import mbedtls._md as _md
from mbedtls.exceptions import *

algorithms_guaranteed = _md.algorithms_guaranteed
algorithms_available = _md.algorithms_available
Expand Down

0 comments on commit 0b6b612

Please sign in to comment.