【发布时间】:2020-11-28 18:53:24
【问题描述】:
我想在带有 RNG 的代码中使用来自 Botan 密码库的 McEliece 函数。 Botan GitHub页面上的描述不充分,参考了一个老的外部使用例子,根本不行。
因此我是这个主题的新手,我尝试调用类并解析所需的参数,但我得到了错误。我认为类型一定有问题。
这是我的代码示例:
1 //#include <botan/botan.h>
2 #include <botan/rng.h>
3 #include <botan/system_rng.h>
4 #include <botan/mceies.h>
5 #include <botan/mceliece.h>
6 #include <iostream>
7
8 int main() {
9
10 Botan::size_t n = 6624;
11 Botan::size_t t = 115;
12
13 std::unique_ptr<Botan::RandomNumberGenerator> rng;
14 //RandomNumberGenerator rng = RandomNumberGenerator();
15
16 //McEliece_PrivateKey sk1();
17 //RandomNumberGenerator &system_rng();
18
19 Botan::McEliece_PrivateKey sk1(rng, n, t);
20
21 //std::cout << typeid(sk1).name() << std::endl;
22 std::cout << typeid(rng).name() << std::endl;
23 std::cout << rng->next_byte() << std::endl;
24
25 return 0;
以下是错误:
~/projects $ g++ mc_eliece2.cpp -o mc_eliece2 -I/usr/local/include/botan-2/ -L/usr/local/lib/
mc_eliece2.cpp: In function ‘int main()’:
mc_eliece2.cpp:19:44: error: no matching function for call to ‘Botan::McEliece_PrivateKey::McEliece_PrivateKey(std::unique_ptr<Botan::RandomNumberGenerator>&, std::size_t&, std::size_t&)’
Botan::McEliece_PrivateKey sk1(rng, n, t);
^
In file included from mc_eliece2.cpp:5:
/usr/local/include/botan-2/botan/mceliece.h:91:7: note: candidate: ‘Botan::McEliece_PrivateKey::McEliece_PrivateKey(const Botan::polyn_gf2m&, const std::vector<unsigned int>&, const std::vector<Botan::polyn_gf2m>&, const std::vector<short unsigned int>&, const std::vector<unsigned char>&)’
McEliece_PrivateKey(polyn_gf2m const& goppa_polyn,
^~~~~~~~~~~~~~~~~~~
/usr/local/include/botan-2/botan/mceliece.h:91:7: note: candidate expects 5 arguments, 3 provided
/usr/local/include/botan-2/botan/mceliece.h:89:16: note: candidate: ‘Botan::McEliece_PrivateKey::McEliece_PrivateKey(Botan::secure_vector<unsigned char>&)’
explicit McEliece_PrivateKey(const secure_vector<uint8_t>& key_bits);
^~~~~~~~~~~~~~~~~~~
/usr/local/include/botan-2/botan/mceliece.h:89:16: note: candidate expects 1 argument, 3 provided
/usr/local/include/botan-2/botan/mceliece.h:87:7: note: candidate: ‘Botan::McEliece_PrivateKey::McEliece_PrivateKey(Botan::RandomNumberGenerator&, std::size_t, std::size_t)’
McEliece_PrivateKey(RandomNumberGenerator& rng, size_t code_length, size_t t);
^~~~~~~~~~~~~~~~~~~
/usr/local/include/botan-2/botan/mceliece.h:87:7: note: no known conversion for argument 1 from ‘std::unique_ptr<Botan::RandomNumberGenerator>’ to ‘Botan::RandomNumberGenerator&’
/usr/local/include/botan-2/botan/mceliece.h:70:29: note: candidate: ‘Botan::McEliece_PrivateKey::McEliece_PrivateKey(const Botan::McEliece_PrivateKey&)’
class BOTAN_PUBLIC_API(2,0) McEliece_PrivateKey final : public virtual McEliece_PublicKey,
^~~~~~~~~~~~~~~~~~~
/usr/local/include/botan-2/botan/mceliece.h:70:29: note: candidate expects 1 argument, 3 provided
/usr/local/include/botan-2/botan/mceliece.h:70:29: note: candidate: ‘Botan::McEliece_PrivateKey::McEliece_PrivateKey(Botan::McEliece_PrivateKey&&)’
/usr/local/include/botan-2/botan/mceliece.h:70:29: note: candidate expects 1 argument, 3 provided
这是原始示例,但我提到过,这是过时的,根本不起作用:
#include <botan/mce_kem.h>
u32bit n = 6624;
u32bit t = 115;
// key generation:
// create a new McEliece private key with code length n and error weight t
McEliece_PrivateKey sk1(rng, n, t);
// derive the corresponding public key
McEliece_PublicKey pk1 (*dynamic_cast<McEliece_PublicKey*>(&sk1));
// encode the public key
std::vector<byte> pk_enc = pk1.x509_subject_public_key();
// encode the private key
secure_vector<byte> sk_enc = sk1.pkcs8_private_key();
// code used at the encrypting side:
// decode a serialized public key
McEliece_PublicKey pk(pk_enc);
McEliece_KEM_Encryptor enc(pk);
// perform encryption
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:
// decode a serialized private key
McEliece_PrivateKey sk(sk_enc);
McEliece_KEM_Decryptor dec(sk);
// perform decryption
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.
提前感谢您的帮助。
“@Swift - Friday Pie”是的,这是错误的。但现在还有另一种情况。因此 RandomNumberGenerator 是一个抽象类,我不能创建它的任何实例。因此我尝试使用一个继承自 RandomNumnerGenerator 的类。我的代码:
1 //#include <botan/botan.h>
2 #include <botan/rng.h>
3 #include <botan/system_rng.h>
4 #include <botan/auto_rng.h>
5 #include <botan/mceies.h>
6 #include <botan/mceliece.h>
7 #include <iostream>
8
9 int main() {
10
11 Botan::size_t n = 6624;
12 Botan::size_t t = 115;
13
14 //std::unique_ptr<Botan::RandomNumberGenerator> rng;
15 //RandomNumberGenerator rng = RandomNumberGenerator();
16
17 //McEliece_PrivateKey sk1();
18 Botan::System_RNG();
19
20 //Botan::McEliece_PrivateKey sk1(*system_rng, n, t);
21
22 //std::cout << typeid(sk1).name() << std::endl;
23 //std::cout << typeid(rng).name() << std::endl;
24 //std::cout << rng->next_byte() << std::endl;
25
26 return 0;
27 }
它输出以下错误:
:~/projects $ g++ mc_eliece2.cpp -o mc_eliece2 -I/usr/local/include/botan-2/ -L/usr/local/lib
/usr/bin/ld: /tmp/cc2YcGqo.o: in function `Botan::RandomNumberGenerator::~RandomNumberGenerator()':
mc_eliece2.cpp:(.text._ZN5Botan21RandomNumberGeneratorD2Ev[_ZN5Botan21RandomNumberGeneratorD5Ev]+0x30): undefined reference to `vtable for Botan::RandomNumberGenerator'
/usr/bin/ld: /tmp/cc2YcGqo.o: in function `Botan::System_RNG::name[abi:cxx11]() const':
mc_eliece2.cpp:(.text._ZNK5Botan10System_RNG4nameB5cxx11Ev[_ZNK5Botan10System_RNG4nameB5cxx11Ev]+0x14): undefined reference to `Botan::system_rng()'
/usr/bin/ld: /tmp/cc2YcGqo.o: in function `Botan::System_RNG::randomize(unsigned char*, unsigned int)':
mc_eliece2.cpp:(.text._ZN5Botan10System_RNG9randomizeEPhj[_ZN5Botan10System_RNG9randomizeEPhj]+0x18): undefined reference to `Botan::system_rng()'
/usr/bin/ld: /tmp/cc2YcGqo.o: in function `Botan::System_RNG::add_entropy(unsigned char const*, unsigned int)':
mc_eliece2.cpp:(.text._ZN5Botan10System_RNG11add_entropyEPKhj[_ZN5Botan10System_RNG11add_entropyEPKhj]+0x18): undefined reference to `Botan::system_rng()'
/usr/bin/ld: /tmp/cc2YcGqo.o: in function `Botan::System_RNG::is_seeded() const':
mc_eliece2.cpp:(.text._ZNK5Botan10System_RNG9is_seededEv[_ZNK5Botan10System_RNG9is_seededEv]+0x10): undefined reference to `Botan::system_rng()'
/usr/bin/ld: /tmp/cc2YcGqo.o: in function `Botan::System_RNG::accepts_input() const':
mc_eliece2.cpp:(.text._ZNK5Botan10System_RNG13accepts_inputEv[_ZNK5Botan10System_RNG13accepts_inputEv]+0x10): undefined reference to `Botan::system_rng()'
/usr/bin/ld: /tmp/cc2YcGqo.o:mc_eliece2.cpp:(.text._ZN5Botan10System_RNG5clearEv[_ZN5Botan10System_RNG5clearEv]+0x10): more undefined references to `Botan::system_rng()' follow
/usr/bin/ld: /tmp/cc2YcGqo.o:(.rodata._ZTVN5Botan10System_RNGE[_ZTVN5Botan10System_RNGE]+0x1c): undefined reference to `Botan::RandomNumberGenerator::randomize_with_input(unsigned char*, unsigned int, unsigned char const*, unsigned int)'
/usr/bin/ld: /tmp/cc2YcGqo.o:(.rodata._ZTVN5Botan10System_RNGE[_ZTVN5Botan10System_RNGE]+0x20): undefined reference to `Botan::RandomNumberGenerator::randomize_with_ts_input(unsigned char*, unsigned int)'
/usr/bin/ld: /tmp/cc2YcGqo.o:(.rodata._ZTVN5Botan10System_RNGE[_ZTVN5Botan10System_RNGE]+0x30): undefined reference to `Botan::RandomNumberGenerator::reseed(Botan::Entropy_Sources&, unsigned int, std::chrono::duration<long long, std::ratio<1ll, 1000ll> >)'
/usr/bin/ld: /tmp/cc2YcGqo.o:(.rodata._ZTVN5Botan10System_RNGE[_ZTVN5Botan10System_RNGE]+0x34): undefined reference to `Botan::RandomNumberGenerator::reseed_from_rng(Botan::RandomNumberGenerator&, unsigned int)'
/usr/bin/ld: /tmp/cc2YcGqo.o:(.rodata._ZTIN5Botan10System_RNGE[_ZTIN5Botan10System_RNGE]+0x8): undefined reference to `typeinfo for Botan::RandomNumberGenerator'
collect2: error: ld returned 1 exit status
它以某种方式找不到引用,但库已链接,并且包含标题。我不知道为什么会这样。
【问题讨论】:
-
这是第二个问题!并且可能是欺骗,链接器错误意味着您没有将某些内容与您的程序链接。确保它是正确的库和正确的格式,检查库文件以确保它导出了这些符号。如果没有,那就是有问题