-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
2,165 additions
and
109 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,22 @@ | ||
[0.8] - 2018-02-24 | ||
|
||
Support X.509 Certificates | ||
|
||
* x509/Certificate: X.509 certificate writer and parser. | ||
* x509/CSR: X.509 certificate signing request writer and parser. | ||
* x509/CRL: X.509 certificate revocation list and validation. | ||
|
||
API Changes | ||
|
||
* CipherBase/RSA: import_() method renamed from_buffer() for PEP 543. | ||
* CipherBase/RSA: export(format="PEM") method renamed to_PEM() | ||
* CipherBase/RSA: export(format="DER") method renamed to_DER() | ||
* CipherBase/RSA: from_DER(), from_PEM() to import from DER or PEM. | ||
* CipherBase/RSA: to_bytes() alias to_DER() | ||
|
||
|
||
[0.7] - 2018-02-04 | ||
|
||
- Add support for Python 2.7 | ||
- Tests ported from nosetest to pytest | ||
- Setup continuous integration | ||
* Add support for Python 2.7, 3.5, and 3.6. | ||
* Tests ported from nosetest to pytest. | ||
* Setup continuous integration. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
"""Declaration from `mbedtls/bignum.h`.""" | ||
|
||
__author__ = "Mathias Laurin" | ||
__copyright__ = "Copyright 2018, Mathias Laurin" | ||
__license__ = "MIT License" | ||
|
||
|
||
cdef extern from "mbedtls/bignum.h": | ||
# Multi-precision integer library | ||
# ------------------------------- | ||
ctypedef enum mbedtls_mpi: pass | ||
ctypedef enum mbedtls_mpi_sint: pass | ||
|
||
# mbedtls_mpi | ||
# ----------- | ||
void mbedtls_mpi_init( mbedtls_mpi *X ) | ||
void mbedtls_mpi_free( mbedtls_mpi *X ); | ||
|
||
# mbedtls_mpi_grow | ||
# mbedtls_mpi_shrink | ||
# mbedtls_mpi_copy | ||
# mbedtls_mpi_swap | ||
# mbedtls_mpi_safe_cond_assign | ||
# mbedtls_mpi_safe_cond_swap | ||
# mbedtls_mpi_lset // limited to 64-bits | ||
# mbedtls_mpi_get_bit | ||
# mbedtls_mpi_set_bit | ||
# mbedtls_mpi_lsb | ||
|
||
size_t mbedtls_mpi_bitlen(const mbedtls_mpi *X) | ||
size_t mbedtls_mpi_size(const mbedtls_mpi *X) | ||
|
||
# mbedtls_mpi_read_string | ||
# mbedtls_mpi_write_string | ||
# mbedtls_mpi_read_file | ||
# mbedtls_mpi_write_file | ||
|
||
int mbedtls_mpi_read_binary( | ||
mbedtls_mpi *X, | ||
const unsigned char *buf, | ||
size_t buflen) | ||
int mbedtls_mpi_write_binary( | ||
mbedtls_mpi *X, | ||
unsigned char *buf, | ||
size_t buflen) | ||
|
||
# mbedtls_mpi_shift_l | ||
# mbedtls_mpi_shift_r | ||
# mbedtls_mpi_cmp_abs | ||
# mbedtls_mpi_cmp_mpi | ||
# mbedtls_mpi_cmp_int | ||
# mbedtls_mpi_add_abs | ||
# mbedtls_mpi_sub_abs | ||
# mbedtls_mpi_add_mpi | ||
# mbedtls_mpi_sub_mpi | ||
# mbedtls_mpi_add_int | ||
# mbedtls_mpi_sub_int | ||
# mbedtls_mpi_mul_mpi | ||
# mbedtls_mpi_mul_int | ||
# mbedtls_mpi_div_mpi | ||
# mbedtls_mpi_div_int | ||
# mbedtls_mpi_mod_mpi | ||
# mbedtls_mpi_mod_int | ||
# mbedtls_mpi_exp_mod | ||
# mbedtls_mpi_fill_random | ||
# mbedtls_mpi_gcd | ||
# mbedtls_mpi_inv_mod | ||
# mbedtls_mpi_is_prime | ||
# mbedtls_mpi_gen_prime | ||
|
||
|
||
cdef class MPI: | ||
cdef mbedtls_mpi _ctx | ||
cdef _len(self) | ||
cpdef _from_bytes(self, const unsigned char[:] bytes) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
"""Multi-precision integer library (MPI).""" | ||
|
||
__author__ = "Mathias Laurin" | ||
__copyright__ = "Copyright 2018, Mathias Laurin" | ||
__license__ = "MIT License" | ||
|
||
|
||
cimport _mpi | ||
from libc.stdlib cimport malloc, free | ||
|
||
import numbers | ||
from binascii import hexlify, unhexlify | ||
|
||
from mbedtls.exceptions import * | ||
|
||
try: | ||
long | ||
except NameError: | ||
long = int | ||
|
||
|
||
cdef to_bytes(value): | ||
return unhexlify("{0:02x}".format(value).encode("ascii")) | ||
|
||
|
||
cdef from_bytes(value): | ||
return long(hexlify(value), 16) | ||
|
||
|
||
cdef class MPI: | ||
"""Multi-precision integer. | ||
Only minimal bindings here because Python already has | ||
arbitrary-precision integers. | ||
""" | ||
def __init__(self, value): | ||
if value is None: | ||
return # Implementation detail. | ||
try: | ||
value = to_bytes(value) | ||
except TypeError: | ||
pass | ||
self._from_bytes(bytearray(value)) | ||
|
||
def __cinit__(self): | ||
"""Initialize one MPI.""" | ||
_mpi.mbedtls_mpi_init(&self._ctx) | ||
|
||
def __dealloc__(self): | ||
"""Unallocate one MPI.""" | ||
_mpi.mbedtls_mpi_free(&self._ctx) | ||
|
||
cdef _len(self): | ||
"""Return the total size in bytes.""" | ||
return _mpi.mbedtls_mpi_size(&self._ctx) | ||
|
||
cpdef _from_bytes(self, const unsigned char[:] bytes): | ||
check_error( | ||
_mpi.mbedtls_mpi_read_binary(&self._ctx, &bytes[0], bytes.shape[0])) | ||
return self | ||
|
||
def __str__(self): | ||
return "%i" % long(self) | ||
|
||
def bit_length(self): | ||
"""Return the number of bits necessary to represent MPI in binary.""" | ||
return _mpi.mbedtls_mpi_bitlen(&self._ctx) | ||
|
||
def __eq__(self, other): | ||
if not isinstance(other, numbers.Integral): | ||
raise NotImplemented | ||
return long(self) == other | ||
|
||
@classmethod | ||
def from_int(cls, value): | ||
# mbedtls_mpi_lset is 'limited' to 64 bits. | ||
return cls.from_bytes(to_bytes(value), byteorder="big") | ||
|
||
def __int__(self): | ||
return from_bytes(self.to_bytes(self._len(), byteorder="big")) | ||
|
||
@classmethod | ||
def from_bytes(cls, bytes, byteorder): | ||
assert byteorder in {"big", "little"} | ||
order = slice(None, None, -1 if byteorder is "little" else None) | ||
return cls(None)._from_bytes(bytearray(bytes[order])) | ||
|
||
def to_bytes(self, length, byteorder): | ||
assert byteorder in {"big", "little"} | ||
order = slice(None, None, -1 if byteorder is "little" else None) | ||
cdef unsigned char* output = <unsigned char*>malloc( | ||
length * sizeof(unsigned char)) | ||
if not output: | ||
raise MemoryError() | ||
try: | ||
check_error(_mpi.mbedtls_mpi_write_binary( | ||
&self._ctx, output, length)) | ||
return bytes(output[:length])[order] | ||
except Exception as exc: | ||
raise OverflowError from exc | ||
finally: | ||
free(output) | ||
|
||
__bytes__ = to_bytes |
Oops, something went wrong.