Skip to content

Commit

Permalink
push
Browse files Browse the repository at this point in the history
  • Loading branch information
DmitriyMusatkin committed Nov 18, 2024
1 parent 084e141 commit 4a5f9c5
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 2 deletions.
25 changes: 24 additions & 1 deletion awscrt/crypto.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,12 @@ class RSASignatureAlgorithm(IntEnum):
PKCSv1.5 padding with sha256 hash function
"""

PSS_SHA256 = 1
PKCS1_5_SHA1 = 1
"""
PKCSv1.5 padding with sha1 hash function
"""

PSS_SHA256 = 2
"""
PSS padding with sha256 hash function
"""
Expand All @@ -117,6 +122,24 @@ def new_public_key_from_pem_data(pem_data: Union[str, bytes, bytearray, memoryvi
Raises ValueError if pem does not have public key object.
"""
return RSA(binding=_awscrt.rsa_public_key_from_pem_data(pem_data))

@staticmethod
def new_private_key_from_der_data(pem_data: Union[str, bytes, bytearray, memoryview]) -> 'RSA':
"""
Creates a new instance of private RSA key pair from der data.
Expects key in PKCS1 format.
Raises ValueError if pem does not have private key object.
"""
return RSA(binding=_awscrt.rsa_private_key_from_der_data(pem_data))

@staticmethod
def new_public_key_from_der_data(pem_data: Union[str, bytes, bytearray, memoryview]) -> 'RSA':
"""
Creates a new instance of public RSA key pair from der data.
Expects key in PKCS1 format.
Raises ValueError if pem does not have public key object.
"""
return RSA(binding=_awscrt.rsa_public_key_from_der_data(pem_data))

def encrypt(self, encryption_algorithm: RSAEncryptionAlgorithm,
plaintext: Union[bytes, bytearray, memoryview]) -> bytes:
Expand Down
25 changes: 25 additions & 0 deletions source/crypto.c
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,31 @@ PyObject *aws_py_rsa_public_key_from_pem_data(PyObject *self, PyObject *args) {
return capsule;
}

PyObject *aws_py_rsa_private_key_from_der_data(PyObject *self, PyObject *args) {
(void)self;

struct aws_byte_cursor der_data_cur;
if (!PyArg_ParseTuple(args, "s#", &pem_data_cur.ptr, &pem_data_cur.len)) {
return NULL;
}

struct aws_rsa_key_pair *key_pair =
aws_rsa_key_pair_new_from_private_key_pkcs1(allocator, &der_data_cur);

if (key_pair == NULL) {
PyErr_AwsLastError();
goto on_done;
}

capsule = PyCapsule_New(key_pair, s_capsule_name_rsa, s_rsa_destructor);

if (capsule == NULL) {
aws_rsa_key_pair_release(key_pair);
}

return capsule;
}

PyObject *aws_py_rsa_encrypt(PyObject *self, PyObject *args) {
(void)self;

Expand Down
3 changes: 3 additions & 0 deletions source/crypto.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ PyObject *aws_py_sha256_hmac_compute(PyObject *self, PyObject *args);
PyObject *aws_py_rsa_private_key_from_pem_data(PyObject *self, PyObject *args);
PyObject *aws_py_rsa_public_key_from_pem_data(PyObject *self, PyObject *args);

PyObject *aws_py_rsa_private_key_from_der_data(PyObject *self, PyObject *args);
PyObject *aws_py_rsa_public_key_from_der_data(PyObject *self, PyObject *args);

PyObject *aws_py_rsa_encrypt(PyObject *self, PyObject *args);
PyObject *aws_py_rsa_decrypt(PyObject *self, PyObject *args);
PyObject *aws_py_rsa_sign(PyObject *self, PyObject *args);
Expand Down

0 comments on commit 4a5f9c5

Please sign in to comment.