Botan MC-Eliece 实施失败,因为实施示例已弃用

Botan MC-Eliece implementation fails because of deprecated implementation example

我在使用 Botan 加密库的 C++ mc-eliece 实现时遇到问题。整个互联网上似乎只有一个这样的例子,link。

https://www.cryptosource.de/docs/mceliece_in_botan.pdf

但是这个例子已经有 6 年历史了,因此它已经完全过时了,Botan 文档没有提供任何其他内容。

问题基本上是,不幸的是函数名称和规范随着时间的推移发生了变化,因此我在尝试使用它们时遇到了几个编译器错误。通过研究 header 实现,我设法揭开了其中一些的神秘面纱。但现在,坦率地说,我在墙前。

如果任何熟悉 Botan MC-Eliece 实现的人能给我提示,如何调用当前函数,那就太好了。

这是我带标记的代码。我删除了很多不必要的代码和其他实现,以使其更具可读性。如果没有必要的模块,你也将无法运行,但我会尝试以某种方式写下来,拥有 Botan 库的人应该能够运行它。

//to compile: g++ -o mc_eliece mc_eliece.cpp -Wall -I/usr/local/include/botan-2/ -I/home/pi/projects/RNG_final/ -ltss2-esys -ltss2-rc -lbotan-2

#include <iostream>
#include <botan/rng.h>
#include <botan/system_rng.h>
#include <botan/mceies.h>
#include <botan/mceliece.h>

int main() {

   Botan::size_t n = 1632; // Parameters for key generation
   Botan::size_t t = 33;


   // initialize  RNG type
   Botan::System_RNG rng; // is a standard Botan RNG


   // create a new MCEliece private key with code length n and error weigth t  
   Botan::McEliece_PrivateKey sk1(rng, n, t);  // actually works!


   // derive the corresponding public key
   Botan::McEliece_PublicKey pk1(*dynamic_cast<Botan::McEliece_PublicKey*>(&sk1)); // actually works!


   // encode the public key
   std::vector<uint8_t> pk_enc = pk1.subject_public_key(); // actually works!


   // encode the private key
   Botan::secure_vector<uint8_t> sk_enc = sk1.private_key_bits(); // had to replace sk1.pkcs8_private_key()


   // encryption side: decode a serialized public key
   Botan::McEliece_PublicKey pk(pk_enc);
   McEliece_KEM_Encryptor enc(pk); // does not work, can't find a working corresponding function in the header


   // perform encryption -> will find out if it works after upper case had been solved
   std::pair<secure_vector<Botan::byte>,secure_vector<Botan::byte> > ciphertext__sym_key = enc.encrypt(rng);
   secure_vector<Botan::byte> sym_key_encr = ciphertext__sym_key.second;
   secure_vector<Botan::byte> ciphertext = ciphertext__sym_key.first;


   // code used at the decrypting side: -> will find out if it works after upper case had been solved
   // decode a serialized private key
   McEliece_PrivateKey sk(sk_enc);
   McEliece_KEM_Decryptor dec(sk);
   
   
   // perform decryption -> will find out if it works after upper case had been solved
   secure_vector<Botan::byte> sym_key_decr = dec.decrypt(&ciphertext[0],
   ciphertext.size() );

   // both sides now have the same 64-byte symmetric key.
   // use this key to instantiate an authenticated encryption scheme.
   // in case shorter keys are needed, they can simple be cut off.

   return 0;
}

感谢您提前提供帮助。

McEliece单元测试可以作为参考(link)。

基于该代码,您的示例可以重写如下:

#include <botan/auto_rng.h>
#include <botan/data_src.h>
#include <botan/hex.h>
#include <botan/mceies.h>
#include <botan/mceliece.h>
#include <botan/pkcs8.h>
#include <botan/pubkey.h>
#include <botan/x509_key.h>

#include <cassert>
#include <iostream>

int main() {

  // Parameters for McEliece key
  // Reference: https://en.wikipedia.org/wiki/McEliece_cryptosystem#Key_sizes
  Botan::size_t n = 1632;
  Botan::size_t t = 33;

  // Size of the symmetric key in bytes
  Botan::size_t shared_key_size = 64;

  // Key-derivation function to be used for key encapsulation
  // Reference: https://botan.randombit.net/handbook/api_ref/kdf.html
  std::string kdf{"KDF1(SHA-512)"};

  // Salt to be used for key derivation
  // NOTE: Pick salt dynamically, Botan recommds for example using a session ID.
  std::vector<Botan::byte> salt{0x01, 0x02, 0x03};

  Botan::AutoSeeded_RNG rng{};

  // Generate private key
  Botan::McEliece_PrivateKey priv{rng, n, t};

  std::vector<Botan::byte> pub_enc{priv.subject_public_key()};
  Botan::secure_vector<Botan::byte> priv_enc{Botan::PKCS8::BER_encode(priv)};

  // Encrypting side: Create encapsulated symmetric key
  std::unique_ptr<Botan::Public_Key> pub{Botan::X509::load_key(pub_enc)};
  Botan::PK_KEM_Encryptor encryptor{*pub, rng, kdf};

  Botan::secure_vector<Botan::byte> encapsulated_key{};
  Botan::secure_vector<Botan::byte> shared_key1{};

  encryptor.encrypt(encapsulated_key, shared_key1, shared_key_size, rng, salt);

  std::cout << "Shared key 1: " << Botan::hex_encode(shared_key1) << std::endl;

  // Decrypting side: Unpack encapsulated symmetric key
  Botan::DataSource_Memory priv_enc_src{priv_enc};
  std::unique_ptr<Botan::Private_Key> priv2{
      Botan::PKCS8::load_key(priv_enc_src)};
  Botan::PK_KEM_Decryptor decryptor{*priv2, rng, kdf};

  Botan::secure_vector<Botan::byte> shared_key2{
      decryptor.decrypt(encapsulated_key, shared_key_size, salt)};

  std::cout << "Shared key 2: " << Botan::hex_encode(shared_key2) << std::endl;

  assert(shared_key1 == shared_key2);

  return 0;
}

我针对 2.15.0 测试了这段代码。示例输出:

$ g++ -g $(pkg-config --cflags --libs botan-2) test.cpp
$ ./a.out
Shared key 1: 32177925CE5F3D607BA45575195F13B9E0123BD739580DFCF9AE53D417C530DB115867E5E377735CB405CDA6DF7866C647F85FDAC5C407BB2E2C3A8E7D41A5CC
Shared key 2: 32177925CE5F3D607BA45575195F13B9E0123BD739580DFCF9AE53D417C530DB115867E5E377735CB405CDA6DF7866C647F85FDAC5C407BB2E2C3A8E7D41A5CC

与您提供的代码相比,我所做的更改的一些注释:

  • 每个 Botan::Private_Key 也是一个 Botan::Public_Key (reference)。因此,我们不需要将生成的私钥转换为 public 密钥。相反,我们可以直接在私钥上调用 subject_public_key 以获得 public 密钥编码。

  • 使用 private_key_bits 获取用于序列化的原始私钥位是可行的,但使用 PKCS8 在互操作性方面可能更稳健。因此,出于这个原因,我会在私钥上使用 Botan::PKCS8::BER_encode

  • 直接从 public 键编码构造 McEliece_PublicKey 对我不起作用,因为构造函数需要原始 public 键位而不是 x509 public 键编码。为此,我不得不使用 Botan::X509::load_key.

  • 已删除McEliece_KEM_Encryptor(reference):

    Add generalized interface for KEM (key encapsulation) techniques. Convert McEliece KEM to use it. The previous interfaces McEliece_KEM_Encryptor and McEliece_KEM_Decryptor have been removed. The new KEM interface now uses a KDF to hash the resulting keys; to get the same output as previously provided by McEliece_KEM_Encryptor, use "KDF1(SHA-512)" and request exactly 64 bytes.

    相反,您可以使用 Botan::PK_KEM_Encryptor,它将 public 密钥作为参数并从 public 密钥推断出要使用的加密算法。从代码中可以看出,这种方式变得更加灵活,生成密钥后我们不必再引用McEliece。如果我们想切换到不同的算法,我们只需要改变密钥生成部分,而不是密钥封装部分。

  • 同样适用于McEliece_KEM_Decryptor

  • 如上文所述,新的 KEM 界面允许指定用于 KEM 的 key-derivation 函数,以及所需的对称密钥大小。此外,您可以传递将用于密钥派生的盐。对于 salt,您将使用一些对您的应用程序唯一的值,甚至更好地对当前会话 (reference):

    Typically salt is a label or identifier, such as a session id.

我现在已经更新了 https://www.cryptosource.de/docs/mceliece_in_botan.pdf 中的示例代码,以反映对 Botan 的新 KEM API 的这些更改。

请注意,当在 KEM 上下文中用于 public 密钥方案(如 McEliece)时,无需为 KDF 提供加盐值。 KDF 可以在这里接受盐值只是 API 的人工制品,因为 KDF 也可以在其他上下文中使用。具体来说,只有在派生可能缺乏熵的秘密密钥(例如密码)时才需要加盐值。然后它根据预先计算的表减轻攻击。