| 40 | } // namespace |
| 41 | |
| 42 | WasiCryptoExpect<StateVariant> |
| 43 | openState(Algorithm Alg, OptionalRef<const KeyVariant> OptKeyVariant, |
| 44 | OptionalRef<const Options> OptOptions) noexcept { |
| 45 | return std::visit( |
| 46 | [=](auto Factory) noexcept -> WasiCryptoExpect<StateVariant> { |
| 47 | using StateOpen = GetStateOpenTrait<decltype(Factory)>; |
| 48 | if constexpr (StateOpen::NeedKey) { |
| 49 | using RequiredKeyType = typename StateOpen::Key; |
| 50 | // Need key. Not have key, fail. |
| 51 | if (unlikely(!OptKeyVariant)) { |
| 52 | return WasiCryptoUnexpect(__WASI_CRYPTO_ERRNO_KEY_REQUIRED); |
| 53 | } |
| 54 | return std::visit( |
| 55 | [OptOptions](const auto &Key) -> WasiCryptoExpect<StateVariant> { |
| 56 | using InKeyType = std::decay_t<decltype(Key)>; |
| 57 | if constexpr (!std::is_same_v<InKeyType, RequiredKeyType>) { |
| 58 | // Key types do not match. |
| 59 | return WasiCryptoUnexpect(__WASI_CRYPTO_ERRNO_INVALID_KEY); |
| 60 | } else { |
| 61 | // Key type fitted. |
| 62 | return decltype(Factory)::State::open(Key, OptOptions) |
| 63 | .map([](auto &&State) noexcept { |
| 64 | return StateVariant{ |
| 65 | std::forward<decltype(State)>(State)}; |
| 66 | }); |
| 67 | } |
| 68 | }, |
| 69 | *OptKeyVariant); |
| 70 | |
| 71 | } else { |
| 72 | // Not need key. Have key, fail. |
| 73 | if (unlikely(!!OptKeyVariant)) { |
| 74 | return WasiCryptoUnexpect(__WASI_CRYPTO_ERRNO_KEY_NOT_SUPPORTED); |
| 75 | } |
| 76 | return decltype(Factory)::State::open(OptOptions) |
| 77 | .map([](auto &&State) noexcept { |
| 78 | return StateVariant{std::forward<decltype(State)>(State)}; |
| 79 | }); |
| 80 | } |
| 81 | }, |
| 82 | Alg); |
| 83 | } |
| 84 | |
| 85 | WasiCryptoExpect<size_t> stateOptionsGet(const StateVariant &StateVariant, |
| 86 | std::string_view Name, |
no test coverage detected