From fcc8e7dca4065d3c73bf6fb29a6762885cc30fba Mon Sep 17 00:00:00 2001 From: Gregory Date: Sat, 6 Apr 2019 16:14:39 +0300 Subject: [PATCH] pre openssl integration --- inc/ft_base64.h | 2 +- openssl_cases.txt | 29 +++++++++++++++++++++++++++++ src/base64/ft_base64_init.c | 1 + src/des/ft_des_ecb.c | 2 -- src/des/ft_des_ecb_decrypt.c | 1 + src/des/ft_des_ecb_encrypt.c | 1 + t/base64_tests.c | 4 ++-- watch.sh | 4 ---- 8 files changed, 35 insertions(+), 9 deletions(-) create mode 100644 openssl_cases.txt delete mode 100755 watch.sh diff --git a/inc/ft_base64.h b/inc/ft_base64.h index 336debfa..e8febcaf 100644 --- a/inc/ft_base64.h +++ b/inc/ft_base64.h @@ -18,7 +18,7 @@ # define FT_BASE64_READ_SIZE 1024 # define FT_BASE64_ENCODE_BLOCK_SIZE 3 # define FT_BASE64_DECODE_BLOCK_SIZE 4 -# define FT_BASE64_ALPHABET_LENGTH 64 +# define FT_BASE64_ALPHABET_LENGTH 65 typedef uint64_t t_byte8; typedef unsigned char t_byte1; diff --git a/openssl_cases.txt b/openssl_cases.txt new file mode 100644 index 00000000..67af1a06 --- /dev/null +++ b/openssl_cases.txt @@ -0,0 +1,29 @@ +// ENCRYPTING + +1. "openssl des-ecb -S a" + prompts to enter password, generate key with given pass and salt, + prepend salt header. + +2. "openssl des-ecb -pass pass:asd" + generate salt and key, prepend salt header. + +3. "openssl des-ecb -S a -pass:asd" + generate key from givent salt and pass, prepend header. + +4. "openssl des-ecb -S a -pass:asd -K 1" + encrypt with GIVEN key, prepend GIVEN salt hedaer. + +5. "openssl des-ecb -K 1" + encrypt with given key, doesn't generate and prepend salt. + +6. "openssl des-ecb -S 0 -pass pass:asd -P -pbkdf2" + generate key using PKCS5_PBKDF2_HMAC with 10000 iteration, + 8 byte salt, 3 byte key, sha256 hash function + +// DECRYPTING + +7. "echo -n Salted__ | openssl des-ecb -d" + prompts to enter password, generate key but fails to decode. + +8. "echo -n Salted__aaaaaaaa | openssl des-ecb -d" + prompts to enter password, generate key but fails to validate padding. diff --git a/src/base64/ft_base64_init.c b/src/base64/ft_base64_init.c index f38a6c96..e8e86417 100644 --- a/src/base64/ft_base64_init.c +++ b/src/base64/ft_base64_init.c @@ -21,4 +21,5 @@ void ft_base64_init(t_base64_ctx *ctx) ft_memcpy(ctx->alphabet, "ABCDEFGHIJKLMNOPQRSTUVWXYZ", 26); ft_memcpy(ctx->alphabet + 26, "abcdefghijklmnopqrstuvwxyz", 26); ft_memcpy(ctx->alphabet + 52, "0123456789+/", 12); + ctx->alphabet[64] = 0; } diff --git a/src/des/ft_des_ecb.c b/src/des/ft_des_ecb.c index 37c547be..ebbc7404 100644 --- a/src/des/ft_des_ecb.c +++ b/src/des/ft_des_ecb.c @@ -81,7 +81,6 @@ void ft_des_ecb ft_des_derive_key(&ctx); if (ctx.decode) { - ft_des_generate_decryption_round_keys(ctx.key, ctx.round_keys); if (ctx.b64) ft_des_ecb_decrypt_b64(&ctx); else @@ -89,7 +88,6 @@ void ft_des_ecb } else { - ft_des_generate_encryption_round_keys(ctx.key, ctx.round_keys); if (ctx.b64) ft_des_ecb_encrypt_b64(&ctx); else diff --git a/src/des/ft_des_ecb_decrypt.c b/src/des/ft_des_ecb_decrypt.c index 87413770..c8953489 100644 --- a/src/des/ft_des_ecb_decrypt.c +++ b/src/des/ft_des_ecb_decrypt.c @@ -35,6 +35,7 @@ void ft_des_ecb_decrypt t_byte1 buffer[FT_DES_BYTE_BLOCK_SIZE]; t_byte1 message[FT_DES_BYTE_BLOCK_SIZE]; + ft_des_generate_decryption_round_keys(c->key, c->round_keys); last_read = 0; while((readed = buffered_read(c, buffer))) { diff --git a/src/des/ft_des_ecb_encrypt.c b/src/des/ft_des_ecb_encrypt.c index d7df0b3c..a8bd9bb5 100644 --- a/src/des/ft_des_ecb_encrypt.c +++ b/src/des/ft_des_ecb_encrypt.c @@ -10,6 +10,7 @@ void ft_des_ecb_encrypt t_byte1 buffer[FT_DES_READ_SIZE]; t_byte8 readed; + ft_des_generate_encryption_round_keys(ctx->key, ctx->round_keys); while((readed = read(ctx->input_fd, buffer, FT_DES_READ_SIZE)) > 0) ft_des_ecb_encode_process_chunk(ctx, readed, buffer); ft_des_ecb_finish_encrypt(ctx); diff --git a/t/base64_tests.c b/t/base64_tests.c index 72d03856..60a1ad1b 100644 --- a/t/base64_tests.c +++ b/t/base64_tests.c @@ -28,10 +28,10 @@ static int transform_block() ft_base64_init(&ctx); ft_base64_encode_transform(&ctx, (t_byte1 *)"Man", buff); - _is(ft_strcmp((char *)buff, "TWFu") == 0); + _is(ft_strncmp((char *)buff, "TWFu", 4) == 0); ft_base64_encode_transform(&ctx, (t_byte1 *)"LOL", buff); - _is(ft_strcmp((char *)buff, "TE9M") == 0); + _is(ft_strncmp((char *)buff, "TE9M", 4) == 0); _end("transform block"); } diff --git a/watch.sh b/watch.sh deleted file mode 100755 index 7468039e..00000000 --- a/watch.sh +++ /dev/null @@ -1,4 +0,0 @@ -#!/usr/bin/env sh -while true; do - find src t inc Makefile | entr -cd make check -j8 -done