Skip to content

Commit

Permalink
mypy: fixing p256keypair.py
Browse files Browse the repository at this point in the history
  • Loading branch information
Alami-Amine committed Jul 19, 2024
1 parent f28ddb5 commit 12bb98e
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions src/controller/python/chip/crypto/p256keypair.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@

import abc
import hashlib
from ctypes import CFUNCTYPE, POINTER, c_bool, c_char, c_size_t, c_uint8, c_uint32, c_void_p, memmove, py_object, string_at
from ctypes import (CFUNCTYPE, POINTER, c_bool, c_char, c_size_t, c_uint8, c_uint32, c_void_p, cast, memmove, pointer, py_object,
string_at)

from chip import native
from ecdsa import ECDH, NIST256p, SigningKey # type: ignore
Expand All @@ -31,18 +32,18 @@


@ _pychip_P256Keypair_ECDSA_sign_msg_func
def _pychip_ECDSA_sign_msg(self_: 'P256Keypair', message_buf: POINTER(c_uint8), message_size: int, signature_buf: POINTER(c_uint8), signature_buf_size: POINTER(c_size_t)) -> bool:
def _pychip_ECDSA_sign_msg(self_: 'P256Keypair', message_buf: pointer[c_uint8], message_size: int, signature_buf: pointer[c_uint8], signature_buf_size: pointer[c_size_t]) -> bool:
res = self_.ECDSA_sign_msg(string_at(message_buf, message_size)[:])
memmove(signature_buf, res, len(res))
signature_buf_size.content = len(res)
signature_buf_size.contents.value = len(res)
return True


@ _pychip_P256Keypair_ECDH_derive_secret_func
def _pychip_ECDH_derive_secret(self_: 'P256Keypair', remote_pubkey: POINTER(c_uint8), out_secret_buf: POINTER(c_uint8), out_secret_buf_size: POINTER(c_uint32)) -> bool:
def _pychip_ECDH_derive_secret(self_: 'P256Keypair', remote_pubkey: pointer[c_uint8], out_secret_buf: pointer[c_uint8], out_secret_buf_size: pointer[c_uint32]) -> bool:
res = self_.ECDH_derive_secret(string_at(remote_pubkey, P256_PUBLIC_KEY_LENGTH)[:])
memmove(out_secret_buf, res, len(res))
out_secret_buf_size.content = len(res)
out_secret_buf_size.contents.value = len(res)
return True


Expand Down Expand Up @@ -79,7 +80,7 @@ def _create_native_object(self) -> c_void_p:
def __del__(self):
if self._native_obj is not None:
handle = native.GetLibraryHandle()
handle.pychip_DeleteP256Keypair(c_void_p(self._native_obj))
handle.pychip_DeleteP256Keypair(cast(self._native_obj, c_void_p))
self._native_obj = None

@property
Expand All @@ -95,7 +96,8 @@ def UpdatePublicKey(self) -> None:
generates a new keypair.
'''
handle = native.GetLibraryHandle()
handle.pychip_P256Keypair_UpdatePubkey(c_void_p(self.native_object), self.public_key, len(self.public_key)).raise_on_error()
handle.pychip_P256Keypair_UpdatePubkey(cast(self._native_obj, c_void_p),
self.public_key, len(self.public_key)).raise_on_error()

@abc.abstractproperty
def public_key(self) -> bytes:
Expand Down

0 comments on commit 12bb98e

Please sign in to comment.