From 1699d8a90e988035b6450fd52945d68b49a46059 Mon Sep 17 00:00:00 2001 From: merlokk <807634+merlokk@users.noreply.github.com> Date: Fri, 16 Jul 2021 20:45:03 +0300 Subject: [PATCH] add des3 to libcrypto --- client/src/crypto/libpcrypto.c | 48 ++++++++++++++++++++++++++++++++++ client/src/crypto/libpcrypto.h | 2 ++ 2 files changed, 50 insertions(+) diff --git a/client/src/crypto/libpcrypto.c b/client/src/crypto/libpcrypto.c index 32686b199..8c4bdd8fc 100644 --- a/client/src/crypto/libpcrypto.c +++ b/client/src/crypto/libpcrypto.c @@ -65,6 +65,54 @@ void des_decrypt_cbc(void *out, const void *in, const int length, const void *ke mbedtls_des_crypt_cbc(&ctx, MBEDTLS_DES_DECRYPT, length, iv, in, out); } +void des3_encrypt(void *out, const void *in, const void *key, uint8_t keycount) { + switch (keycount) { + case 1: + des_encrypt(out, in, key); + break; + case 2: { + mbedtls_des3_context ctx3; + mbedtls_des3_set2key_enc(&ctx3, key); + mbedtls_des3_crypt_ecb(&ctx3, in, out); + mbedtls_des3_free(&ctx3); + break; + } + case 3: { + mbedtls_des3_context ctx3; + mbedtls_des3_set3key_enc(&ctx3, key); + mbedtls_des3_crypt_ecb(&ctx3, in, out); + mbedtls_des3_free(&ctx3); + break; + } + default: + break; + } +} + +void des3_decrypt(void *out, const void *in, const void *key, uint8_t keycount) { + switch (keycount) { + case 1: + des_encrypt(out, in, key); + break; + case 2: { + mbedtls_des3_context ctx3; + mbedtls_des3_set2key_dec(&ctx3, key); + mbedtls_des3_crypt_ecb(&ctx3, in, out); + mbedtls_des3_free(&ctx3); + break; + } + case 3: { + mbedtls_des3_context ctx3; + mbedtls_des3_set3key_dec(&ctx3, key); + mbedtls_des3_crypt_ecb(&ctx3, in, out); + mbedtls_des3_free(&ctx3); + break; + } + default: + break; + } +} + // NIST Special Publication 800-38A — Recommendation for block cipher modes of operation: methods and techniques, 2001. int aes_encode(uint8_t *iv, uint8_t *key, uint8_t *input, uint8_t *output, int length) { uint8_t iiv[16] = {0}; diff --git a/client/src/crypto/libpcrypto.h b/client/src/crypto/libpcrypto.h index 3f3bdb45b..0a9cb01ce 100644 --- a/client/src/crypto/libpcrypto.h +++ b/client/src/crypto/libpcrypto.h @@ -22,6 +22,8 @@ void des_encrypt_ecb(void *out, const void *in, const int length, const void *ke void des_decrypt_ecb(void *out, const void *in, const int length, const void *key); void des_encrypt_cbc(void *out, const void *in, const int length, const void *key, uint8_t *iv); void des_decrypt_cbc(void *out, const void *in, const int length, const void *key, uint8_t *iv); +void des3_encrypt(void *out, const void *in, const void *key, uint8_t keycount); +void des3_decrypt(void *out, const void *in, const void *key, uint8_t keycount); int aes_encode(uint8_t *iv, uint8_t *key, uint8_t *input, uint8_t *output, int length); int aes_decode(uint8_t *iv, uint8_t *key, uint8_t *input, uint8_t *output, int length);