Skip to content

Commit

Permalink
Expose FrodoKEM loading in Python
Browse files Browse the repository at this point in the history
  • Loading branch information
reneme committed Oct 14, 2024
1 parent 51cf4fe commit cb9b92b
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/python/botan3.py
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,8 @@ def ffi_api(fn, args, allowed_errors=None):
ffi_api(dll.botan_pubkey_load_kyber, [c_void_p, c_char_p, c_int])
ffi_api(dll.botan_privkey_view_kyber_raw_key, [c_void_p, c_void_p, VIEW_BIN_CALLBACK])
ffi_api(dll.botan_pubkey_view_kyber_raw_key, [c_void_p, c_void_p, VIEW_BIN_CALLBACK])
ffi_api(dll.botan_privkey_load_frodokem, [c_void_p, c_void_p, c_int, c_char_p])
ffi_api(dll.botan_pubkey_load_frodokem, [c_void_p, c_void_p, c_int, c_char_p])
ffi_api(dll.botan_privkey_load_ecdsa, [c_void_p, c_void_p, c_char_p])
ffi_api(dll.botan_pubkey_load_ecdsa, [c_void_p, c_void_p, c_void_p, c_char_p])
ffi_api(dll.botan_pubkey_load_ecdh, [c_void_p, c_void_p, c_void_p, c_char_p])
Expand Down Expand Up @@ -1232,6 +1234,12 @@ def load_kyber(cls, key):
_DLL.botan_pubkey_load_kyber(byref(obj), key, len(key))
return PublicKey(obj)

@classmethod
def load_frodokem(cls, frodo_mode, key):
obj = c_void_p(0)
_DLL.botan_pubkey_load_frodokem(byref(obj), key, len(key), _ctype_str(frodo_mode))
return PublicKey(obj)

def __del__(self):
_DLL.botan_pubkey_destroy(self.__obj)

Expand Down Expand Up @@ -1390,6 +1398,12 @@ def load_kyber(cls, key):
_DLL.botan_privkey_load_kyber(byref(obj), key, len(key))
return PrivateKey(obj)

@classmethod
def load_frodokem(cls, frodo_mode, key):
obj = c_void_p(0)
_DLL.botan_privkey_load_frodokem(byref(obj), key, len(key), _ctype_str(frodo_mode))
return PrivateKey(obj)

def __del__(self):
_DLL.botan_privkey_destroy(self.__obj)

Expand Down
14 changes: 14 additions & 0 deletions src/scripts/test_python.py
Original file line number Diff line number Diff line change
Expand Up @@ -937,6 +937,20 @@ def test_kyber_raw_keys(self):
pubkey_read = a_pub.view_kyber_raw_key()
self.assertEqual(pubkey_read, a_pub_bits)

def test_frodokem_raw_keys(self):
frodo_mode = "FrodoKEM-640-SHAKE"
sk = botan.PrivateKey.create("FrodoKEM", frodo_mode, botan.RandomNumberGenerator("user"))
pk = sk.get_public_key()

sk_bits = sk.to_raw()
pk_bits = pk.to_raw()

sk_read = botan.PrivateKey.load_frodokem(frodo_mode, sk_bits)
pk_read = botan.PublicKey.load_frodokem(frodo_mode, pk_bits)

self.assertEqual(sk_read.to_raw(), sk_bits)
self.assertEqual(pk_read.to_raw(), pk_bits)


class BotanPythonZfecTests(unittest.TestCase):
"""
Expand Down

0 comments on commit cb9b92b

Please sign in to comment.