Program Listing for File crypto_impl.hpp

Return to documentation for file (libspookyaction/include/desfire/esp32/crypto_impl.hpp)

//
// Created by spak on 5/4/21.
//

#ifndef DESFIRE_ESP32_CRYPTO_IMPL_HPP
#define DESFIRE_ESP32_CRYPTO_IMPL_HPP

#include <desfire/crypto.hpp>

#include <sdkconfig.h>

#if CONFIG_LIBSPOOKY_SSL_WOLFSSL || (CONFIG_LIBSPOOKY_SSL_AUTO && CONFIG_ESP_TLS_USING_WOLFSSL)
#define LIBSPOOKY_USE_MBEDTLS 0
#define LIBSPOOKY_USE_WOLFSSL 1
#else
// Fallback to mbedtls
#define LIBSPOOKY_USE_MBEDTLS 1
#define LIBSPOOKY_USE_WOLFSSL 0
#endif

#if LIBSPOOKY_USE_MBEDTLS

#include <mbedtls/esp_config.h>
#include <mbedtls/aes.h>
#include <mbedtls/des.h>

#ifndef MBEDTLS_DES_C
#error "libSpookyAction: config macro CONFIG_MBEDTLS_DES_C not found; make sure you have CONFIG_MBEDTLS_DES_C=y in your sdkconfig!"
#endif

#elif LIBSPOOKY_USE_WOLFSSL

#include <wolfssl/wolfcrypt/aes.h>
#include <wolfssl/wolfcrypt/des3.h>

#ifdef NO_DES3
#error "libSpookyAction: NO_DES3 macro defined -- update your user_settings.h for WolfSSL and remove the definition. We need DES3 ciphers."
#endif

#undef LOG_LOCAL_LEVEL
#define LOG_LOCAL_LEVEL CONFIG_LOG_MAXIMUM_LEVEL
#endif

namespace desfire::esp32 {

    class crypto_des final : public crypto_des_base {
#if LIBSPOOKY_USE_MBEDTLS
        mbedtls_des_context _enc_context;
        mbedtls_des_context _dec_context;
#elif LIBSPOOKY_USE_WOLFSSL
        Des _enc_context{};
        Des _dec_context{};
#endif

    public:
        void setup_with_key(range<std::uint8_t const *> key) override;
        void do_crypto(range<std::uint8_t *> data, range<std::uint8_t *> iv, crypto_operation op) override;
#if LIBSPOOKY_USE_MBEDTLS
        crypto_des();
        ~crypto_des() override;
#endif//LIBSPOOKY_USE_MBEDTLS
    };

    class crypto_2k3des final : public crypto_2k3des_base {
#if LIBSPOOKY_USE_MBEDTLS
        mbedtls_des3_context _enc_context;
        mbedtls_des3_context _dec_context;
#elif LIBSPOOKY_USE_WOLFSSL
        Des3 _enc_context{};
        Des3 _dec_context{};
#endif
    protected:
        void setup_primitives_with_key(range<std::uint8_t const *> key) override;

    public:
        void do_crypto(range<std::uint8_t *> data, range<std::uint8_t *> iv, crypto_operation op) override;
#if LIBSPOOKY_USE_MBEDTLS
        crypto_2k3des();
        ~crypto_2k3des() override;
#endif//LIBSPOOKY_USE_MBEDTLS
    };

    class crypto_3k3des final : public crypto_3k3des_base {
#if LIBSPOOKY_USE_MBEDTLS
        mbedtls_des3_context _enc_context;
        mbedtls_des3_context _dec_context;
#elif LIBSPOOKY_USE_WOLFSSL
        Des3 _enc_context{};
        Des3 _dec_context{};
#endif

    public:
        void do_crypto(range<std::uint8_t *> data, range<std::uint8_t *> iv, crypto_operation op) override;

#if LIBSPOOKY_USE_MBEDTLS
        crypto_3k3des();
        ~crypto_3k3des() override;
#endif//LIBSPOOKY_USE_MBEDTLS

    protected:
        void setup_primitives_with_key(range<std::uint8_t const *> key) override;
    };

    class crypto_aes final : public crypto_aes_base {
#if LIBSPOOKY_USE_MBEDTLS
        mbedtls_aes_context _enc_context;
        mbedtls_aes_context _dec_context;
#elif LIBSPOOKY_USE_WOLFSSL
        Aes _enc_context{};
        Aes _dec_context{};
#endif

    public:
        void do_crypto(range<std::uint8_t *> data, range<std::uint8_t *> iv, crypto_operation op) override;

#if LIBSPOOKY_USE_MBEDTLS
        crypto_aes();
        ~crypto_aes() override;
#endif//LIBSPOOKY_USE_MBEDTLS

    protected:
        void setup_primitives_with_key(range<std::uint8_t const *> key) override;
    };

}// namespace desfire::esp32

#endif//DESFIRE_ESP32_CRYPTO_IMPL_HPP