Skip to content

Commit

Permalink
Integrate C. McEliece into FFI and Python bindings
Browse files Browse the repository at this point in the history
  • Loading branch information
reneme committed Oct 14, 2024
1 parent b60c6e1 commit f88fe6b
Show file tree
Hide file tree
Showing 5 changed files with 139 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/lib/ffi/ffi.h
Original file line number Diff line number Diff line change
Expand Up @@ -1544,6 +1544,22 @@ int botan_privkey_load_frodokem(botan_privkey_t* key, const uint8_t privkey[], s
BOTAN_FFI_EXPORT(3, 6)
int botan_pubkey_load_frodokem(botan_pubkey_t* key, const uint8_t pubkey[], size_t key_len, const char* frodo_mode);

/**
* Algorithm specific key operation: Classic McEliece
*/

BOTAN_FFI_EXPORT(3, 6)
int botan_privkey_load_classic_mceliece(botan_privkey_t* key,
const uint8_t privkey[],
size_t key_len,
const char* cmce_mode);

BOTAN_FFI_EXPORT(3, 6)
int botan_pubkey_load_classic_mceliece(botan_pubkey_t* key,
const uint8_t pubkey[],
size_t key_len,
const char* cmce_mode);

/*
* Algorithm specific key operations: ECDSA and ECDH
*/
Expand Down
54 changes: 54 additions & 0 deletions src/lib/ffi/ffi_pkey_algs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@
#include <botan/frodokem.h>
#endif

#if defined(BOTAN_HAS_CLASSICMCELIECE)
#include <botan/cmce.h>
#endif

namespace {

#if defined(BOTAN_HAS_ECC_PUBLIC_KEY_CRYPTO)
Expand Down Expand Up @@ -1031,6 +1035,56 @@ int botan_pubkey_view_kyber_raw_key(botan_pubkey_t key, botan_view_ctx ctx, bota
#endif
}

/*
* Algorithm specific key operations: Classic McEliece
*/

int botan_privkey_load_classic_mceliece(botan_privkey_t* key,
const uint8_t privkey[],
size_t key_len,
const char* cmce_mode) {
#if defined(BOTAN_HAS_CLASSICMCELIECE)
if(key == nullptr || privkey == nullptr || cmce_mode == nullptr) {
return BOTAN_FFI_ERROR_NULL_POINTER;
}

*key = nullptr;

return ffi_guard_thunk(__func__, [=]() -> int {
const auto mode = Botan::Classic_McEliece_Parameter_Set::from_string(cmce_mode);
auto cmce_key = std::make_unique<Botan::Classic_McEliece_PrivateKey>(std::span{privkey, key_len}, mode);
*key = new botan_privkey_struct(std::move(cmce_key));
return BOTAN_FFI_SUCCESS;
});
#else
BOTAN_UNUSED(key, privkey, key_len, cmce_mode);
return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
#endif
}

int botan_pubkey_load_classic_mceliece(botan_pubkey_t* key,
const uint8_t pubkey[],
size_t key_len,
const char* cmce_mode) {
#if defined(BOTAN_HAS_CLASSICMCELIECE)
if(key == nullptr || pubkey == nullptr || cmce_mode == nullptr) {
return BOTAN_FFI_ERROR_NULL_POINTER;
}

*key = nullptr;

return ffi_guard_thunk(__func__, [=]() -> int {
const auto mode = Botan::Classic_McEliece_Parameter_Set::from_string(cmce_mode);
auto cmce_key = std::make_unique<Botan::Classic_McEliece_PublicKey>(std::span{pubkey, key_len}, mode);
*key = new botan_pubkey_struct(std::move(cmce_key));
return BOTAN_FFI_SUCCESS;
});
#else
BOTAN_UNUSED(key, pubkey, key_len, cmce_mode);
return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
#endif
}

/*
* Algorithm specific key operations: FrodoKEM
*/
Expand Down
14 changes: 14 additions & 0 deletions src/python/botan3.py
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,8 @@ def ffi_api(fn, args, allowed_errors=None):
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_classic_mceliece, [c_void_p, c_void_p, c_int, c_char_p])
ffi_api(dll.botan_pubkey_load_classic_mceliece, [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 @@ -1240,6 +1242,12 @@ def load_frodokem(cls, frodo_mode, key):
_DLL.botan_pubkey_load_frodokem(byref(obj), key, len(key), _ctype_str(frodo_mode))
return PublicKey(obj)

@classmethod
def load_classic_mceliece(cls, cmce_mode, key):
obj = c_void_p(0)
_DLL.botan_pubkey_load_classic_mceliece(byref(obj), key, len(key), _ctype_str(cmce_mode))
return PublicKey(obj)

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

Expand Down Expand Up @@ -1404,6 +1412,12 @@ def load_frodokem(cls, frodo_mode, key):
_DLL.botan_privkey_load_frodokem(byref(obj), key, len(key), _ctype_str(frodo_mode))
return PrivateKey(obj)

@classmethod
def load_classic_mceliece(cls, cmce_mode, key):
obj = c_void_p(0)
_DLL.botan_privkey_load_classic_mceliece(byref(obj), key, len(key), _ctype_str(cmce_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 @@ -951,6 +951,20 @@ def test_frodokem_raw_keys(self):
self.assertEqual(sk_read.to_raw(), sk_bits)
self.assertEqual(pk_read.to_raw(), pk_bits)

def test_classic_mceliece_raw_keys(self):
cmce_mode = "mceliece348864f"
sk = botan.PrivateKey.create("ClassicMcEliece", cmce_mode, botan.RandomNumberGenerator("user"))
pk = sk.get_public_key()

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

sk_read = botan.PrivateKey.load_classic_mceliece(cmce_mode, sk_bits)
pk_read = botan.PublicKey.load_classic_mceliece(cmce_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
41 changes: 41 additions & 0 deletions src/tests/test_ffi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <botan/hex.h>
#include <botan/internal/fmt.h>
#include <botan/internal/loadstor.h>
#include <botan/internal/stl_util.h>
#include <set>
#endif

Expand Down Expand Up @@ -3632,6 +3633,45 @@ class FFI_FrodoKEM_Test final : public FFI_KEM_Roundtrip_Test {
}
};

class FFI_Classic_McEliece_Test final : public FFI_KEM_Roundtrip_Test {
public:
std::string name() const override { return "FFI Classic McEliece"; }

protected:
const char* algo() const override { return "ClassicMcEliece"; }

privkey_loader_fn_t private_key_load_function() const override { return botan_privkey_load_classic_mceliece; }

pubkey_loader_fn_t public_key_load_function() const override { return botan_pubkey_load_classic_mceliece; }

std::vector<const char*> modes() const override {
auto modes = std::vector{
"mceliece348864f",
"mceliece460896f",
};
if(Test::run_long_tests()) {
modes = Botan::concat(modes,
std::vector{
"mceliece348864",
"mceliece460896",
"mceliece6688128",
"mceliece6688128f",
"mceliece6688128pc",
"mceliece6688128pcf",
"mceliece6960119",
"mceliece6960119f",
"mceliece6960119pc",
"mceliece6960119pcf",
"mceliece8192128",
"mceliece8192128f",
"mceliece8192128pc",
"mceliece8192128pcf",
});
}
return modes;
}
};

class FFI_ElGamal_Test final : public FFI_Test {
public:
std::string name() const override { return "FFI ElGamal"; }
Expand Down Expand Up @@ -3875,6 +3915,7 @@ BOTAN_REGISTER_TEST("ffi", "ffi_kyber512", FFI_Kyber512_Test);
BOTAN_REGISTER_TEST("ffi", "ffi_kyber768", FFI_Kyber768_Test);
BOTAN_REGISTER_TEST("ffi", "ffi_kyber1024", FFI_Kyber1024_Test);
BOTAN_REGISTER_TEST("ffi", "ffi_frodokem", FFI_FrodoKEM_Test);
BOTAN_REGISTER_TEST("ffi", "ffi_cmce", FFI_Classic_McEliece_Test);
BOTAN_REGISTER_TEST("ffi", "ffi_elgamal", FFI_ElGamal_Test);
BOTAN_REGISTER_TEST("ffi", "ffi_dh", FFI_DH_Test);

Expand Down

0 comments on commit f88fe6b

Please sign in to comment.