botan icon indicating copy to clipboard operation
botan copied to clipboard

Extend/Update (public) APIs with `std::span` overloads

Open reneme opened this issue 2 years ago • 8 comments

Alongside existing std::vector<T, Alloc> and C-style ptr-length APIs we're gradually introducing overloads taking a std::span from C++20. This ticket is an attempt to create/maintain a list of interfaces that should get such overloads.

Public APIs and Interfaces

  • [x] RandomNumberGenerator (https://github.com/randombit/botan/pull/3195)
  • [x] Buffered_Computation (https://github.com/randombit/botan/pull/3294, https://github.com/randombit/botan/pull/3681)
  • [x] Symmetric_Algorithm (https://github.com/randombit/botan/pull/3297)
  • [ ] Encoding
    • [x] https://github.com/randombit/botan/pull/3349
    • [x] https://github.com/randombit/botan/pull/3350
    • [x] base32 (#3399)
    • [x] base58 (#3399)
    • [ ] DER/BER
  • [x] TLS (Client, Callbacks, ...) (#3363)
  • [x] Key Derivation Function (#3398)
  • [x] Message Authentication Code (#3397)
  • [x] Cipher Mode
    • [x] Cipher_Mode (https://github.com/randombit/botan/pull/3392)
    • [x] StreamCipher (for set_iv(), write_keystream()) (#3395)
    • [x] AEAD_Mode (https://github.com/randombit/botan/pull/3317)
    • [x] BlockCipher (https://github.com/randombit/botan/pull/3394)
  • [x] pubkey.h (and friends) (https://github.com/randombit/botan/pull/3400)
  • [ ] BigInt (eg. c'tors, ::encode, ::binary_encode, ...)
    • [ ] Public Interface (https://github.com/randombit/botan/pull/3855)
    • [ ] Internal implementations (big_rand.cpp, big_ops*.cpp, big_code.cpp)
  • [ ] Compression_Algorithm/Decompression_Algorithm
  • [x] ec_group.h
  • [x] HashFunction (https://github.com/randombit/botan/pull/3681)
  • [x] mem_ops.h (https://github.com/randombit/botan/pull/3715)

Internal APIs and Interfaces

  • [x] EMSA (https://github.com/randombit/botan/pull/4635)
  • [x] Buffered_Computation (https://github.com/randombit/botan/pull/3681)
  • [x] SymmetricAlgorithm (https://github.com/randombit/botan/pull/3684)
  • [ ] Cipher_Mode
    • [ ] TLS_CBC_HMAC_AEAD_Mode
    • [ ] BlockCipherModePaddingMethod (https://github.com/randombit/botan/pull/4873)
  • [ ] BlockCipher (encrypt_n(), decrypt_n()) (https://github.com/randombit/botan/pull/3870)
    • Those methods are public API and they are virtual methods! Hence, we cannot easily override them without breaking API changes.
    • Also reconsider this patch: https://github.com/randombit/botan/pull/3885#issuecomment-2017704248
    • [ ] Tweakable_Block_Cipher (set_tweak()) (https://github.com/randombit/botan/pull/3872)
  • [x] MessageAuthenticationCode (https://github.com/randombit/botan/pull/3713)
  • [x] MDx_HashFunction (https://github.com/randombit/botan/pull/3705)
  • [ ] StreamCipher (cipher_bytes(), generate_keystream(), set_iv_bytes())
  • Header: pk_ops.h
    • [x] Encryption (#4239)
    • [x] Decryption (#4239)
    • [x] Verification (#4239)
    • [x] Signature (#4239)
    • [x] Key_Agreement (#4239)
    • [x] KEM_Encryption (#3611)
    • [x] KEM_Decryption (#3611)
  • [ ] SQL_Database (has methods returning std::pair<uint8_t*, size_t>)
  • [x] load_be / store_be (https://github.com/randombit/botan/pull/3707, https://github.com/randombit/botan/pull/3869)
  • [x] ct_utils.h (https://github.com/randombit/botan/pull/4197)

reneme avatar Feb 23 '23 08:02 reneme

Another one, from #3411

      Kyber_PublicKey(const AlgorithmIdentifier& alg_id,
-                      const std::vector<uint8_t>& key_bits);
+                      std::span<const uint8_t> key_bits);

Would be nice to do this for all of the key types.

(This might require some work on BER_Decoder though ...)

randombit avatar Mar 24 '23 08:03 randombit

It would be nice if Botan detected the C++ version in the headers and didn't expose unsupported/new features to C++ compilers that didn't support them.

I am currently trying to integrate Botan into a codebase that can't be built with C++20 (because other libraries we're using don't support it yet) and having to wrap everything is becoming a royal pain in the ass.

crazydef avatar Apr 18 '23 07:04 crazydef

@crazydef Don't do that. Use Botan2, which is still supported and probably will be for years, and is C++11.

Or (for some functionality) you can use the C89 API in ffi.h

randombit avatar Apr 18 '23 12:04 randombit

because other libraries we're using don't support it yet

Do this libraries really break if compiled with C++20? I would expect most older code to work without a problem if compiled with a newer C++ standard.

lieser avatar Apr 18 '23 12:04 lieser

@lieser C++17 deprecated a lot of stuff and removed even more. One particular library I have to deal with still only supports C++14 because of the mess that is C++17.

crazydef avatar Apr 18 '23 12:04 crazydef

I'm just trying to build from tag 3.2.0, and I'm seeing the #include <span> breaking compilation for C++17. make is building with the following options:

g++ -fPIC -fvisibility=hidden -fstack-protector -m64 -pthread -std=c++17 -D_REENTRANT -O3 -DBOTAN_IS_BEING_BUILT -Wall -Wextra -Wpedantic -Wstrict-aliasing -Wcast-align -Wmissing-declarations -Wpointer-arith -Wcast-qual -Wzero-as-null-pointer-constant -Wnon-virtual-dtor -Wold-style-cast -Wsuggest-override -Wshadow -Wextra-semi -I build/include -isystem build/include/external

As I'm sure you are aware, #include <span> is only supported for C++20. Not sure if this is somehow supported as an extension by other compilers but on g++ 8.4.0 it is certainly not.

CharlesJQuarra avatar Dec 30 '23 13:12 CharlesJQuarra

@CharlesJQuarra Botan 3.0 and higher requires C++20 and minimum GCC at least 11.2

That you're seeing precisely this error is surprising in that configure.py should have detected that the version of GCC is too old, and immediately errored out.

randombit avatar Dec 30 '23 17:12 randombit

@randombit my bad, I ran make directly, as it worked on previous pulls, once I ran configure.py I saw the updated warnings that C++17 is no longer supported for v3

CharlesJQuarra avatar Dec 30 '23 17:12 CharlesJQuarra