+/***************************************************************************
+ * mbedcrypto: mbed TLS
+ ***************************************************************************/
+
+#if (USE_EXT_LIBS) & USE_mbedcrypto
+
+#include <mbedtls/aes.h>
+#include <mbedtls/arc4.h>
+#include <mbedtls/blowfish.h>
+#include <mbedtls/camellia.h>
+#include <mbedtls/des.h>
+#include <mbedtls/md5.h>
+#include <mbedtls/ripemd160.h>
+#include <mbedtls/sha1.h>
+#include <mbedtls/sha256.h>
+#include <mbedtls/sha512.h>
+#include <mbedtls/xtea.h>
+
+#define DEFINE_MBEDCRYPTO_WRAPPER(suffix) \
+static void run_mbedcrypto_ ## suffix(uint8_t *output, \
+ const uint8_t *input, unsigned size) \
+{ \
+ mbedtls_ ## suffix ## _ret(input, size, output); \
+}
+
+#define DEFINE_MBEDCRYPTO_WRAPPER_SHA2(suffix) \
+static void run_mbedcrypto_ ## suffix(uint8_t *output, \
+ const uint8_t *input, unsigned size) \
+{ \
+ mbedtls_ ## suffix ## _ret(input, size, output, 0); \
+}
+
+DEFINE_MBEDCRYPTO_WRAPPER(md5)
+DEFINE_MBEDCRYPTO_WRAPPER(ripemd160)
+DEFINE_MBEDCRYPTO_WRAPPER(sha1)
+DEFINE_MBEDCRYPTO_WRAPPER_SHA2(sha256)
+DEFINE_MBEDCRYPTO_WRAPPER_SHA2(sha512)
+
+
+#define DEFINE_MBEDCRYPTO_CYPHER_WRAPPER(suffix, cypher, algo) \
+static void run_mbedcrypto_ ## suffix(uint8_t *output, \
+ const uint8_t *input, unsigned size) \
+{ \
+ mbedtls_ ## cypher ## _context cypher; \
+ \
+ mbedtls_ ## cypher ## _init(&cypher); \
+ mbedtls_ ## cypher ## _setkey_enc(&cypher, hardcoded_key, 128); \
+ for (int i = 0; i < size; i += 16) \
+ mbedtls_ ## cypher ## _crypt_ecb(&cypher, MBEDTLS_ ## algo ## _ENCRYPT, \
+ input + i, output + i); \
+ mbedtls_ ## cypher ## _free(&cypher); \
+}
+
+DEFINE_MBEDCRYPTO_CYPHER_WRAPPER(aes128, aes, AES)
+DEFINE_MBEDCRYPTO_CYPHER_WRAPPER(camellia, camellia, CAMELLIA)
+
+static void run_mbedcrypto_blowfish(uint8_t *output,
+ const uint8_t *input, unsigned size)
+{
+ mbedtls_blowfish_context blowfish;
+
+ mbedtls_blowfish_init(&blowfish);
+ mbedtls_blowfish_setkey(&blowfish, hardcoded_key, 128);
+ for (int i = 0; i < size; i += 8)
+ mbedtls_blowfish_crypt_ecb(&blowfish, MBEDTLS_BLOWFISH_ENCRYPT,
+ input + i, output + i);
+ mbedtls_blowfish_free(&blowfish);
+}
+
+static void run_mbedcrypto_des(uint8_t *output,
+ const uint8_t *input, unsigned size)
+{
+ mbedtls_des_context des;
+
+ mbedtls_des_init(&des);
+ mbedtls_des_setkey_enc(&des, hardcoded_key);
+ for (int i = 0; i < size; i += 8)
+ mbedtls_des_crypt_ecb(&des, input + i, output + i);
+ mbedtls_des_free(&des);
+}
+
+static void run_mbedcrypto_rc4(uint8_t *output,
+ const uint8_t *input, unsigned size)
+{
+ mbedtls_arc4_context rc4;
+
+ mbedtls_arc4_init(&rc4);
+ mbedtls_arc4_setup(&rc4, hardcoded_key, 16);
+ mbedtls_arc4_crypt(&rc4, size, input, output);
+ mbedtls_arc4_free(&rc4);
+}
+
+static void run_mbedcrypto_xtea(uint8_t *output,
+ const uint8_t *input, unsigned size)
+{
+ mbedtls_xtea_context xtea;
+
+ mbedtls_xtea_init(&xtea);
+ mbedtls_xtea_setup(&xtea, hardcoded_key);
+ for (int i = 0; i < size; i += 8)
+ mbedtls_xtea_crypt_ecb(&xtea, MBEDTLS_XTEA_ENCRYPT,
+ input + i, output + i);
+ mbedtls_xtea_free(&xtea);
+}
+
+#define IMPL_USE_mbedcrypto(...) IMPL_USE(__VA_ARGS__)
+#else
+#define IMPL_USE_mbedcrypto(...) /* ignore */
+#endif
+