为什么 botan 要求提供一个随机数生成器,但它为了兼容性而忽略了它?

Why does botan ask for a random number generator that it ignores for compatibility?

我开始使用 botan 密码库并且我 运行 变成了一个奇怪的函数签名:

/**
* Load an encrypted key from a data source.
* @param source the data source providing the encoded key
* @param rng ignored for compatability
* @param get_passphrase a function that returns passphrases
* @return loaded private key object
*/
BOTAN_DLL Private_Key* load_key(DataSource& source,
                                RandomNumberGenerator& rng,
                                std::function<std::string ()> get_passphrase);

我想知道为什么我应该将 RandomNumberGenerator 传递给承诺忽略它的函数?

文档上说是为了兼容,但我想不通他们说的是什么兼容?如果是backwards/forwards兼容性,则意味着在past/future函数has/will中接受一个运行dom数生成器来做一个确定性的操作。

拜托,我在这里错过了什么?

它看起来是为了向后兼容,不为用户更改 API 并且可能不破坏应用程序。
下面开始看看master分支:
https://github.com/randombit/botan/blob/master/src/lib/pubkey/pkcs8.cpp

/*
* Extract an encrypted private key and return it
*/
Private_Key* load_key(DataSource& source,
                      RandomNumberGenerator& rng,
                      std::function<std::string ()> get_pass)
   {
   return load_key(source, rng, get_pass, true);
   } 

来电

/*
* Extract a private key (encrypted/unencrypted) and return it
*/
Private_Key* load_key(DataSource& source,
                      RandomNumberGenerator& /*rng*/,
                      std::function<std::string ()> get_pass,
                      bool is_encrypted)
   {
   AlgorithmIdentifier alg_id;
   secure_vector<uint8_t> pkcs8_key = PKCS8_decode(source, get_pass, alg_id, is_encrypted);

   const std::string alg_name = OIDS::lookup(alg_id.oid);
   if(alg_name.empty() || alg_name == alg_id.oid.as_string())
      throw PKCS8_Exception("Unknown algorithm OID: " +
                            alg_id.oid.as_string());

   return load_private_key(alg_id, pkcs8_key).release();
   }

}

完成这项工作,而这又不使用 rng
现在,让我们看看以前版本的库。版本 2.0.0 (https://github.com/randombit/botan/blob/master/src/lib/pubkey/pkcs8.cpp)
也是如此 在版本(GitHub 中的标签)1.11.33 (https://github.com/randombit/botan/blob/1.11.33/src/lib/pubkey/pkcs8.cpp) 中,可以看到使用了 rng 并且看起来这个版本删除后的版本:

/*
* Extract a private key (encrypted/unencrypted) and return it
*/
Private_Key* load_key(DataSource& source,
                      RandomNumberGenerator& rng,
                      std::function<std::string ()> get_pass,
                      bool is_encrypted)
   {
   AlgorithmIdentifier alg_id;
   secure_vector<byte> pkcs8_key = PKCS8_decode(source, get_pass, alg_id, is_encrypted);

   const std::string alg_name = OIDS::lookup(alg_id.oid);
   if(alg_name.empty() || alg_name == alg_id.oid.as_string())
      throw PKCS8_Exception("Unknown algorithm OID: " +
                            alg_id.oid.as_string());

   return load_private_key(alg_id, pkcs8_key, rng).release();
   }

}

load_private_key(alg_id, pkcs8_key, rng).release();

似乎在 https://github.com/randombit/botan/blob/1.11.33/src/lib/pubkey/pk_algs.cpp 中定义并以

开头
std::unique_ptr<Private_Key>
load_private_key(const AlgorithmIdentifier& alg_id,
                 const secure_vector<byte>& key_bits,
                 RandomNumberGenerator& rng)
   {
   const std::string alg_name = OIDS::lookup(alg_id.oid);
   if(alg_name == "")
      throw Decoding_Error("Unknown algorithm OID: " + alg_id.oid.as_string());

#if defined(BOTAN_HAS_RSA)
   if(alg_name == "RSA")
      return std::unique_ptr<Private_Key>(new RSA_PrivateKey(alg_id, key_bits, rng));
#endif

#if defined(BOTAN_HAS_CURVE_25519)
   if(alg_name == "Curve25519")
      return std::unique_ptr<Private_Key>(new Curve25519_PrivateKey(alg_id, key_bits, rng));
#endif

现在,如果将它与 master 分支或版本 2.0.0 进行比较,您会发现 rng 从相同的调用中删除了

https://github.com/randombit/botan/blob/master/src/lib/pubkey/pk_algs.cpp

std::unique_ptr<Private_Key>
load_private_key(const AlgorithmIdentifier& alg_id,
                 const secure_vector<uint8_t>& key_bits)
   {
   const std::string alg_name = OIDS::lookup(alg_id.oid);
   if(alg_name == "")
      throw Decoding_Error("Unknown algorithm OID: " + alg_id.oid.as_string());

#if defined(BOTAN_HAS_RSA)
   if(alg_name == "RSA")
      return std::unique_ptr<Private_Key>(new RSA_PrivateKey(alg_id, key_bits));
#endif  

在您使用的版本中,它看起来也被删除了,您的版本与 master 中的版本相似:

std::unique_ptr<Private_Key>
load_private_key(const AlgorithmIdentifier& alg_id,
                 const secure_vector<uint8_t>& key_bits)
   {
   const std::string alg_name = OIDS::lookup(alg_id.oid);
   if(alg_name == "")
      throw Decoding_Error("Unknown algorithm OID: " + alg_id.oid.as_string());

#if defined(BOTAN_HAS_RSA)
   if(alg_name == "RSA")
      return std::unique_ptr<Private_Key>(new RSA_PrivateKey(alg_id, key_bits));
#endif

所以看起来为了让用户保持相同 API 他们没有更改签名但他们更改了实现。