From cb9b92ba07eaa26ffff59000fab2da394b41fbfa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Meusel?= Date: Mon, 14 Oct 2024 15:02:54 +0200 Subject: [PATCH] Expose FrodoKEM loading in Python --- src/python/botan3.py | 14 ++++++++++++++ src/scripts/test_python.py | 14 ++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/src/python/botan3.py b/src/python/botan3.py index 94b99124b8..d4d9a540ce 100755 --- a/src/python/botan3.py +++ b/src/python/botan3.py @@ -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]) @@ -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) @@ -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) diff --git a/src/scripts/test_python.py b/src/scripts/test_python.py index 80c14fddc3..6b4762db35 100644 --- a/src/scripts/test_python.py +++ b/src/scripts/test_python.py @@ -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): """