OpenSSLSymmetricCipherContext OpenSSLSymmetricCipher::start(OpenSSLSymmetricCipher::Method method, const SymmetricKey& key, const InitializationVector& iv, bool padding) { OpenSSLSymmetricCipherContext context(method); int r = 0; const EVP_CIPHER* evpCipher = getEVPCipher(key); EXCEPTION_ASSERT(evpCipher, std::invalid_argument, "No cipher found that can use the supplied key"); switch (context.method()) { case M_ENCRYPT: { r = EVP_EncryptInit_ex(context.ctx(), evpCipher, NULL, &key.data()[0], &iv.data()[0]); break; } case M_DECRYPT: { r = EVP_DecryptInit_ex(context.ctx(), evpCipher, NULL, &key.data()[0], &iv.data()[0]); break; } default: { THROW_EXCEPTION_WITH_LOG(std::runtime_error, "Unhandled method"); } } EXCEPTION_ASSERT_WITH_LOG(r == 1, OpenSSLException, ""); context.data().clear(); context.setPadding(padding); return context; }
/** * \brief Comparaison operator. * \param lhs The left argument. * \param rhs The right argument. * \return true if lhs is less than rhs. */ inline bool operator<(const SymmetricKey& lhs, const SymmetricKey& rhs) { return (lhs.data() < rhs.data()); }