diff options
author | dec05eba <dec05eba@protonmail.com> | 2020-11-05 01:45:06 +0100 |
---|---|---|
committer | dec05eba <dec05eba@protonmail.com> | 2020-11-05 01:45:06 +0100 |
commit | 2a8202e74846d191a321cca1202175af9db6107d (patch) | |
tree | a6f455caf07da1186851f343a237a4c4e4484f46 /lib/crypto-algorithms | |
parent | 8efa0ec17d8c262f9c3fd7603e8074f74a053708 (diff) |
Diffstat (limited to 'lib/crypto-algorithms')
-rw-r--r-- | lib/crypto-algorithms/.gitignore | 5 | ||||
-rw-r--r-- | lib/crypto-algorithms/aes_test.c | 276 | ||||
-rw-r--r-- | lib/crypto-algorithms/arcfour_test.c | 47 | ||||
-rw-r--r-- | lib/crypto-algorithms/base64_test.c | 54 | ||||
-rw-r--r-- | lib/crypto-algorithms/blowfish_test.c | 68 | ||||
-rw-r--r-- | lib/crypto-algorithms/des_test.c | 83 | ||||
-rw-r--r-- | lib/crypto-algorithms/md2_test.c | 58 | ||||
-rw-r--r-- | lib/crypto-algorithms/md5_test.c | 60 | ||||
-rw-r--r-- | lib/crypto-algorithms/project.conf | 5 | ||||
-rw-r--r-- | lib/crypto-algorithms/rot-13_test.c | 44 | ||||
-rw-r--r-- | lib/crypto-algorithms/sha1_test.c | 58 | ||||
-rw-r--r-- | lib/crypto-algorithms/sha256_test.c | 61 |
12 files changed, 10 insertions, 809 deletions
diff --git a/lib/crypto-algorithms/.gitignore b/lib/crypto-algorithms/.gitignore new file mode 100644 index 0000000..636c6b9 --- /dev/null +++ b/lib/crypto-algorithms/.gitignore @@ -0,0 +1,5 @@ +# Compiled sibs files +sibs-build/ +compile_commands.json +tests/sibs-build/ +tests/compile_commands.json diff --git a/lib/crypto-algorithms/aes_test.c b/lib/crypto-algorithms/aes_test.c deleted file mode 100644 index d49726d..0000000 --- a/lib/crypto-algorithms/aes_test.c +++ /dev/null @@ -1,276 +0,0 @@ -/*********************************************************************
-* Filename: aes_test.c
-* Author: Brad Conte (brad AT bradconte.com)
-* Copyright:
-* Disclaimer: This code is presented "as is" without any guarantees.
-* Details: Performs known-answer tests on the corresponding AES
- implementation. These tests do not encompass the full
- range of available test vectors and are not sufficient
- for FIPS-140 certification. However, if the tests pass
- it is very, very likely that the code is correct and was
- compiled properly. This code also serves as
- example usage of the functions.
-*********************************************************************/
-
-/*************************** HEADER FILES ***************************/
-#include <stdio.h>
-#include <memory.h>
-#include "aes.h"
-
-/*********************** FUNCTION DEFINITIONS ***********************/
-void print_hex(BYTE str[], int len)
-{
- int idx;
-
- for(idx = 0; idx < len; idx++)
- printf("%02x", str[idx]);
-}
-
-int aes_ecb_test()
-{
- WORD key_schedule[60], idx;
- BYTE enc_buf[128];
- BYTE plaintext[2][16] = {
- {0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a},
- {0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51}
- };
- BYTE ciphertext[2][16] = {
- {0xf3,0xee,0xd1,0xbd,0xb5,0xd2,0xa0,0x3c,0x06,0x4b,0x5a,0x7e,0x3d,0xb1,0x81,0xf8},
- {0x59,0x1c,0xcb,0x10,0xd4,0x10,0xed,0x26,0xdc,0x5b,0xa7,0x4a,0x31,0x36,0x28,0x70}
- };
- BYTE key[1][32] = {
- {0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4}
- };
- int pass = 1;
-
- // Raw ECB mode.
- //printf("* ECB mode:\n");
- aes_key_setup(key[0], key_schedule, 256);
- //printf( "Key : ");
- //print_hex(key[0], 32);
-
- for(idx = 0; idx < 2; idx++) {
- aes_encrypt(plaintext[idx], enc_buf, key_schedule, 256);
- //printf("\nPlaintext : ");
- //print_hex(plaintext[idx], 16);
- //printf("\n-encrypted to: ");
- //print_hex(enc_buf, 16);
- pass = pass && !memcmp(enc_buf, ciphertext[idx], 16);
-
- aes_decrypt(ciphertext[idx], enc_buf, key_schedule, 256);
- //printf("\nCiphertext : ");
- //print_hex(ciphertext[idx], 16);
- //printf("\n-decrypted to: ");
- //print_hex(enc_buf, 16);
- pass = pass && !memcmp(enc_buf, plaintext[idx], 16);
-
- //printf("\n\n");
- }
-
- return(pass);
-}
-
-int aes_cbc_test()
-{
- WORD key_schedule[60];
- BYTE enc_buf[128];
- BYTE plaintext[1][32] = {
- {0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51}
- };
- BYTE ciphertext[2][32] = {
- {0xf5,0x8c,0x4c,0x04,0xd6,0xe5,0xf1,0xba,0x77,0x9e,0xab,0xfb,0x5f,0x7b,0xfb,0xd6,0x9c,0xfc,0x4e,0x96,0x7e,0xdb,0x80,0x8d,0x67,0x9f,0x77,0x7b,0xc6,0x70,0x2c,0x7d}
- };
- BYTE iv[1][16] = {
- {0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f}
- };
- BYTE key[1][32] = {
- {0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4}
- };
- int pass = 1;
-
- //printf("* CBC mode:\n");
- aes_key_setup(key[0], key_schedule, 256);
-
- //printf( "Key : ");
- //print_hex(key[0], 32);
- //printf("\nIV : ");
- //print_hex(iv[0], 16);
-
- aes_encrypt_cbc(plaintext[0], 32, enc_buf, key_schedule, 256, iv[0]);
- //printf("\nPlaintext : ");
- //print_hex(plaintext[0], 32);
- //printf("\n-encrypted to: ");
- //print_hex(enc_buf, 32);
- //printf("\nCiphertext : ");
- //print_hex(ciphertext[0], 32);
- pass = pass && !memcmp(enc_buf, ciphertext[0], 32);
-
- //printf("\n\n");
- return(pass);
-}
-
-int aes_ctr_test()
-{
- WORD key_schedule[60];
- BYTE enc_buf[128];
- BYTE plaintext[1][32] = {
- {0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51}
- };
- BYTE ciphertext[1][32] = {
- {0x60,0x1e,0xc3,0x13,0x77,0x57,0x89,0xa5,0xb7,0xa7,0xf5,0x04,0xbb,0xf3,0xd2,0x28,0xf4,0x43,0xe3,0xca,0x4d,0x62,0xb5,0x9a,0xca,0x84,0xe9,0x90,0xca,0xca,0xf5,0xc5}
- };
- BYTE iv[1][16] = {
- {0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff},
- };
- BYTE key[1][32] = {
- {0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4}
- };
- int pass = 1;
-
- //printf("* CTR mode:\n");
- aes_key_setup(key[0], key_schedule, 256);
-
- //printf( "Key : ");
- //print_hex(key[0], 32);
- //printf("\nIV : ");
- //print_hex(iv[0], 16);
-
- aes_encrypt_ctr(plaintext[0], 32, enc_buf, key_schedule, 256, iv[0]);
- //printf("\nPlaintext : ");
- //print_hex(plaintext[0], 32);
- //printf("\n-encrypted to: ");
- //print_hex(enc_buf, 32);
- pass = pass && !memcmp(enc_buf, ciphertext[0], 32);
-
- aes_decrypt_ctr(ciphertext[0], 32, enc_buf, key_schedule, 256, iv[0]);
- //printf("\nCiphertext : ");
- //print_hex(ciphertext[0], 32);
- //printf("\n-decrypted to: ");
- //print_hex(enc_buf, 32);
- pass = pass && !memcmp(enc_buf, plaintext[0], 32);
-
- //printf("\n\n");
- return(pass);
-}
-
-int aes_ccm_test()
-{
- int mac_auth;
- WORD enc_buf_len;
- BYTE enc_buf[128];
- BYTE plaintext[3][32] = {
- {0x20,0x21,0x22,0x23},
- {0x20,0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x28,0x29,0x2a,0x2b,0x2c,0x2d,0x2e,0x2f},
- {0x20,0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x28,0x29,0x2a,0x2b,0x2c,0x2d,0x2e,0x2f,0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37}
- };
- BYTE assoc[3][32] = {
- {0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07},
- {0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f},
- {0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f,0x10,0x11,0x12,0x13}
- };
- BYTE ciphertext[3][32 + 16] = {
- {0x71,0x62,0x01,0x5b,0x4d,0xac,0x25,0x5d},
- {0xd2,0xa1,0xf0,0xe0,0x51,0xea,0x5f,0x62,0x08,0x1a,0x77,0x92,0x07,0x3d,0x59,0x3d,0x1f,0xc6,0x4f,0xbf,0xac,0xcd},
- {0xe3,0xb2,0x01,0xa9,0xf5,0xb7,0x1a,0x7a,0x9b,0x1c,0xea,0xec,0xcd,0x97,0xe7,0x0b,0x61,0x76,0xaa,0xd9,0xa4,0x42,0x8a,0xa5,0x48,0x43,0x92,0xfb,0xc1,0xb0,0x99,0x51}
- };
- BYTE iv[3][16] = {
- {0x10,0x11,0x12,0x13,0x14,0x15,0x16},
- {0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17},
- {0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19,0x1a,0x1b}
- };
- BYTE key[1][32] = {
- {0x40,0x41,0x42,0x43,0x44,0x45,0x46,0x47,0x48,0x49,0x4a,0x4b,0x4c,0x4d,0x4e,0x4f}
- };
- int pass = 1;
-
- //printf("* CCM mode:\n");
- //printf("Key : ");
- //print_hex(key[0], 16);
-
- //print_hex(plaintext[0], 4);
- //print_hex(assoc[0], 8);
- //print_hex(ciphertext[0], 8);
- //print_hex(iv[0], 7);
- //print_hex(key[0], 16);
-
- aes_encrypt_ccm(plaintext[0], 4, assoc[0], 8, iv[0], 7, enc_buf, &enc_buf_len, 4, key[0], 128);
- //printf("\nNONCE : ");
- //print_hex(iv[0], 7);
- //printf("\nAssoc. Data : ");
- //print_hex(assoc[0], 8);
- //printf("\nPayload : ");
- //print_hex(plaintext[0], 4);
- //printf("\n-encrypted to: ");
- //print_hex(enc_buf, enc_buf_len);
- pass = pass && !memcmp(enc_buf, ciphertext[0], enc_buf_len);
-
- aes_decrypt_ccm(ciphertext[0], 8, assoc[0], 8, iv[0], 7, enc_buf, &enc_buf_len, 4, &mac_auth, key[0], 128);
- //printf("\n-Ciphertext : ");
- //print_hex(ciphertext[0], 8);
- //printf("\n-decrypted to: ");
- //print_hex(enc_buf, enc_buf_len);
- //printf("\nAuthenticated: %d ", mac_auth);
- pass = pass && !memcmp(enc_buf, plaintext[0], enc_buf_len) && mac_auth;
-
-
- aes_encrypt_ccm(plaintext[1], 16, assoc[1], 16, iv[1], 8, enc_buf, &enc_buf_len, 6, key[0], 128);
- //printf("\n\nNONCE : ");
- //print_hex(iv[1], 8);
- //printf("\nAssoc. Data : ");
- //print_hex(assoc[1], 16);
- //printf("\nPayload : ");
- //print_hex(plaintext[1], 16);
- //printf("\n-encrypted to: ");
- //print_hex(enc_buf, enc_buf_len);
- pass = pass && !memcmp(enc_buf, ciphertext[1], enc_buf_len);
-
- aes_decrypt_ccm(ciphertext[1], 22, assoc[1], 16, iv[1], 8, enc_buf, &enc_buf_len, 6, &mac_auth, key[0], 128);
- //printf("\n-Ciphertext : ");
- //print_hex(ciphertext[1], 22);
- //printf("\n-decrypted to: ");
- //print_hex(enc_buf, enc_buf_len);
- //printf("\nAuthenticated: %d ", mac_auth);
- pass = pass && !memcmp(enc_buf, plaintext[1], enc_buf_len) && mac_auth;
-
-
- aes_encrypt_ccm(plaintext[2], 24, assoc[2], 20, iv[2], 12, enc_buf, &enc_buf_len, 8, key[0], 128);
- //printf("\n\nNONCE : ");
- //print_hex(iv[2], 12);
- //printf("\nAssoc. Data : ");
- //print_hex(assoc[2], 20);
- //printf("\nPayload : ");
- //print_hex(plaintext[2], 24);
- //printf("\n-encrypted to: ");
- //print_hex(enc_buf, enc_buf_len);
- pass = pass && !memcmp(enc_buf, ciphertext[2], enc_buf_len);
-
- aes_decrypt_ccm(ciphertext[2], 32, assoc[2], 20, iv[2], 12, enc_buf, &enc_buf_len, 8, &mac_auth, key[0], 128);
- //printf("\n-Ciphertext : ");
- //print_hex(ciphertext[2], 32);
- //printf("\n-decrypted to: ");
- //print_hex(enc_buf, enc_buf_len);
- //printf("\nAuthenticated: %d ", mac_auth);
- pass = pass && !memcmp(enc_buf, plaintext[2], enc_buf_len) && mac_auth;
-
- //printf("\n\n");
- return(pass);
-}
-
-int aes_test()
-{
- int pass = 1;
-
- pass = pass && aes_ecb_test();
- pass = pass && aes_cbc_test();
- pass = pass && aes_ctr_test();
- pass = pass && aes_ccm_test();
-
- return(pass);
-}
-
-int main(int argc, char *argv[])
-{
- printf("AES Tests: %s\n", aes_test() ? "SUCCEEDED" : "FAILED");
-
- return(0);
-}
diff --git a/lib/crypto-algorithms/arcfour_test.c b/lib/crypto-algorithms/arcfour_test.c deleted file mode 100644 index 985f8a7..0000000 --- a/lib/crypto-algorithms/arcfour_test.c +++ /dev/null @@ -1,47 +0,0 @@ -/********************************************************************* -* Filename: arcfour_test.c -* Author: Brad Conte (brad AT bradconte.com) -* Copyright: -* Disclaimer: This code is presented "as is" without any guarantees. -* Details: Performs known-answer tests on the corresponding ARCFOUR - implementation. These tests do not encompass the full - range of available test vectors, however, if the tests - pass it is very, very likely that the code is correct - and was compiled properly. This code also serves as - example usage of the functions. -*********************************************************************/ - -/*************************** HEADER FILES ***************************/ -#include <stdio.h> -#include <memory.h> -#include "arcfour.h" - -/*********************** FUNCTION DEFINITIONS ***********************/ -int rc4_test() -{ - BYTE state[256]; - BYTE key[3][10] = {{"Key"}, {"Wiki"}, {"Secret"}}; - BYTE stream[3][10] = {{0xEB,0x9F,0x77,0x81,0xB7,0x34,0xCA,0x72,0xA7,0x19}, - {0x60,0x44,0xdb,0x6d,0x41,0xb7}, - {0x04,0xd4,0x6b,0x05,0x3c,0xa8,0x7b,0x59}}; - int stream_len[3] = {10,6,8}; - BYTE buf[1024]; - int idx; - int pass = 1; - - // Only test the output stream. Note that the state can be reused. - for (idx = 0; idx < 3; idx++) { - arcfour_key_setup(state, key[idx], strlen(key[idx])); - arcfour_generate_stream(state, buf, stream_len[idx]); - pass = pass && !memcmp(stream[idx], buf, stream_len[idx]); - } - - return(pass); -} - -int main() -{ - printf("ARCFOUR tests: %s\n", rc4_test() ? "SUCCEEDED" : "FAILED"); - - return(0); -} diff --git a/lib/crypto-algorithms/base64_test.c b/lib/crypto-algorithms/base64_test.c deleted file mode 100644 index c59cc98..0000000 --- a/lib/crypto-algorithms/base64_test.c +++ /dev/null @@ -1,54 +0,0 @@ -/********************************************************************* -* Filename: blowfish_test.c -* Author: Brad Conte (brad AT bradconte.com) -* Copyright: -* Disclaimer: This code is presented "as is" without any guarantees. -* Details: Performs known-answer tests on the corresponding Base64 - implementation. These tests do not encompass the full - range of available test vectors, however, if the tests - pass it is very, very likely that the code is correct - and was compiled properly. This code also serves as - example usage of the functions. -*********************************************************************/ - -/*************************** HEADER FILES ***************************/ -#include <stdio.h> -#include <memory.h> -#include "base64.h" - -/*********************** FUNCTION DEFINITIONS ***********************/ -int base64_test() -{ - BYTE text[3][1024] = {{"fo"}, - {"foobar"}, - {"Man is distinguished, not only by his reason, but by this singular passion from other animals, which is a lust of the mind, that by a perseverance of delight in the continued and indefatigable generation of knowledge, exceeds the short vehemence of any carnal pleasure."}}; - BYTE code[3][1024] = {{"Zm8="}, - {"Zm9vYmFy"}, - {"TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1dCBieSB0aGlz\nIHNpbmd1bGFyIHBhc3Npb24gZnJvbSBvdGhlciBhbmltYWxzLCB3aGljaCBpcyBhIGx1c3Qgb2Yg\ndGhlIG1pbmQsIHRoYXQgYnkgYSBwZXJzZXZlcmFuY2Ugb2YgZGVsaWdodCBpbiB0aGUgY29udGlu\ndWVkIGFuZCBpbmRlZmF0aWdhYmxlIGdlbmVyYXRpb24gb2Yga25vd2xlZGdlLCBleGNlZWRzIHRo\nZSBzaG9ydCB2ZWhlbWVuY2Ugb2YgYW55IGNhcm5hbCBwbGVhc3VyZS4="}}; - BYTE buf[1024]; - size_t buf_len; - int pass = 1; - int idx; - - for (idx = 0; idx < 3; idx++) { - buf_len = base64_encode(text[idx], buf, strlen(text[idx]), 1); - pass = pass && ((buf_len == strlen(code[idx])) && - (buf_len == base64_encode(text[idx], NULL, strlen(text[idx]), 1))); - pass = pass && !strcmp(code[idx], buf); - - memset(buf, 0, sizeof(buf)); - buf_len = base64_decode(code[idx], buf, strlen(code[idx])); - pass = pass && ((buf_len == strlen(text[idx])) && - (buf_len == base64_decode(code[idx], NULL, strlen(code[idx])))); - pass = pass && !strcmp(text[idx], buf); - } - - return(pass); -} - -int main() -{ - printf("Base64 tests: %s\n", base64_test() ? "PASSED" : "FAILED"); - - return 0; -} diff --git a/lib/crypto-algorithms/blowfish_test.c b/lib/crypto-algorithms/blowfish_test.c deleted file mode 100644 index 0f0aa38..0000000 --- a/lib/crypto-algorithms/blowfish_test.c +++ /dev/null @@ -1,68 +0,0 @@ -/********************************************************************* -* Filename: blowfish_test.c -* Author: Brad Conte (brad AT bradconte.com) -* Copyright: -* Disclaimer: This code is presented "as is" without any guarantees. -* Details: Performs known-answer tests on the corresponding Blowfish - implementation. These tests do not encompass the full - range of available test vectors, however, if the tests - pass it is very, very likely that the code is correct - and was compiled properly. This code also serves as - example usage of the functions. -*********************************************************************/ - -/*************************** HEADER FILES ***************************/ -#include <stdio.h> -#include <memory.h> -#include "blowfish.h" - -/*********************** FUNCTION DEFINITIONS ***********************/ -int blowfish_test() -{ - BYTE key1[8] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}; - BYTE key2[8] = {0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff}; - BYTE key3[24] = {0xF0,0xE1,0xD2,0xC3,0xB4,0xA5,0x96,0x87, - 0x78,0x69,0x5A,0x4B,0x3C,0x2D,0x1E,0x0F, - 0x00,0x11,0x22,0x33,0x44,0x55,0x66,0x77}; - BYTE p1[BLOWFISH_BLOCK_SIZE] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}; - BYTE p2[BLOWFISH_BLOCK_SIZE] = {0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff}; - BYTE p3[BLOWFISH_BLOCK_SIZE] = {0xFE,0xDC,0xBA,0x98,0x76,0x54,0x32,0x10}; - - BYTE c1[BLOWFISH_BLOCK_SIZE] = {0x4e,0xf9,0x97,0x45,0x61,0x98,0xdd,0x78}; - BYTE c2[BLOWFISH_BLOCK_SIZE] = {0x51,0x86,0x6f,0xd5,0xb8,0x5e,0xcb,0x8a}; - BYTE c3[BLOWFISH_BLOCK_SIZE] = {0x05,0x04,0x4b,0x62,0xfa,0x52,0xd0,0x80}; - - BYTE enc_buf[BLOWFISH_BLOCK_SIZE]; - BLOWFISH_KEY key; - int pass = 1; - - // Test vector 1. - blowfish_key_setup(key1, &key, BLOWFISH_BLOCK_SIZE); - blowfish_encrypt(p1, enc_buf, &key); - pass = pass && !memcmp(c1, enc_buf, BLOWFISH_BLOCK_SIZE); - blowfish_decrypt(c1, enc_buf, &key); - pass = pass && !memcmp(p1, enc_buf, BLOWFISH_BLOCK_SIZE); - - // Test vector 2. - blowfish_key_setup(key2, &key, BLOWFISH_BLOCK_SIZE); - blowfish_encrypt(p2, enc_buf, &key); - pass = pass && !memcmp(c2, enc_buf, BLOWFISH_BLOCK_SIZE); - blowfish_decrypt(c2, enc_buf, &key); - pass = pass && !memcmp(p2, enc_buf, BLOWFISH_BLOCK_SIZE); - - // Test vector 3. - blowfish_key_setup(key3, &key, 24); - blowfish_encrypt(p3, enc_buf, &key); - pass = pass && !memcmp(c3, enc_buf, BLOWFISH_BLOCK_SIZE); - blowfish_decrypt(c3, enc_buf, &key); - pass = pass && !memcmp(p3, enc_buf, BLOWFISH_BLOCK_SIZE); - - return(pass); -} - -int main() -{ - printf("Blowfish tests: %s\n", blowfish_test() ? "SUCCEEDED" : "FAILED"); - - return(0); -} diff --git a/lib/crypto-algorithms/des_test.c b/lib/crypto-algorithms/des_test.c deleted file mode 100644 index 3e46134..0000000 --- a/lib/crypto-algorithms/des_test.c +++ /dev/null @@ -1,83 +0,0 @@ -/********************************************************************* -* Filename: des_test.c -* Author: Brad Conte (brad AT bradconte.com) -* Copyright: -* Disclaimer: This code is presented "as is" without any guarantees. -* Details: Performs known-answer tests on the corresponding DES - implementation. These tests do not encompass the full - range of available test vectors, however, if the tests - pass it is very, very likely that the code is correct - and was compiled properly. This code also serves as - example usage of the functions. -*********************************************************************/ - -/*************************** HEADER FILES ***************************/ -#include <stdio.h> -#include <memory.h> -#include "des.h" - -/*********************** FUNCTION DEFINITIONS ***********************/ -int des_test() -{ - BYTE pt1[DES_BLOCK_SIZE] = {0x01,0x23,0x45,0x67,0x89,0xAB,0xCD,0xE7}; - BYTE pt2[DES_BLOCK_SIZE] = {0x01,0x23,0x45,0x67,0x89,0xAB,0xCD,0xEF}; - BYTE pt3[DES_BLOCK_SIZE] = {0x54,0x68,0x65,0x20,0x71,0x75,0x66,0x63}; - BYTE ct1[DES_BLOCK_SIZE] = {0xc9,0x57,0x44,0x25,0x6a,0x5e,0xd3,0x1d}; - BYTE ct2[DES_BLOCK_SIZE] = {0x85,0xe8,0x13,0x54,0x0f,0x0a,0xb4,0x05}; - BYTE ct3[DES_BLOCK_SIZE] = {0xc9,0x57,0x44,0x25,0x6a,0x5e,0xd3,0x1d}; - BYTE ct4[DES_BLOCK_SIZE] = {0xA8,0x26,0xFD,0x8C,0xE5,0x3B,0x85,0x5F}; - BYTE key1[DES_BLOCK_SIZE] = {0x01,0x23,0x45,0x67,0x89,0xAB,0xCD,0xEF}; - BYTE key2[DES_BLOCK_SIZE] = {0x13,0x34,0x57,0x79,0x9B,0xBC,0xDF,0xF1}; - BYTE three_key1[DES_BLOCK_SIZE * 3] = {0x01,0x23,0x45,0x67,0x89,0xAB,0xCD,0xEF, - 0x01,0x23,0x45,0x67,0x89,0xAB,0xCD,0xEF, - 0x01,0x23,0x45,0x67,0x89,0xAB,0xCD,0xEF}; - BYTE three_key2[DES_BLOCK_SIZE * 3] = {0x01,0x23,0x45,0x67,0x89,0xAB,0xCD,0xEF, - 0x23,0x45,0x67,0x89,0xAB,0xCD,0xEF,0x01, - 0x45,0x67,0x89,0xAB,0xCD,0xEF,0x01,0x23}; - - BYTE schedule[16][6]; - BYTE three_schedule[3][16][6]; - BYTE buf[DES_BLOCK_SIZE]; - int pass = 1; - - des_key_setup(key1, schedule, DES_ENCRYPT); - des_crypt(pt1, buf, schedule); - pass = pass && !memcmp(ct1, buf, DES_BLOCK_SIZE); - - des_key_setup(key1, schedule, DES_DECRYPT); - des_crypt(ct1, buf, schedule); - pass = pass && !memcmp(pt1, buf, DES_BLOCK_SIZE); - - des_key_setup(key2, schedule, DES_ENCRYPT); - des_crypt(pt2, buf, schedule); - pass = pass && !memcmp(ct2, buf, DES_BLOCK_SIZE); - - des_key_setup(key2, schedule, DES_DECRYPT); - des_crypt(ct2, buf, schedule); - pass = pass && !memcmp(pt2, buf, DES_BLOCK_SIZE); - - three_des_key_setup(three_key1, three_schedule, DES_ENCRYPT); - three_des_crypt(pt1, buf, three_schedule); - pass = pass && !memcmp(ct3, buf, DES_BLOCK_SIZE); - - three_des_key_setup(three_key1, three_schedule, DES_DECRYPT); - three_des_crypt(ct3, buf, three_schedule); - pass = pass && !memcmp(pt1, buf, DES_BLOCK_SIZE); - - three_des_key_setup(three_key2, three_schedule, DES_ENCRYPT); - three_des_crypt(pt3, buf, three_schedule); - pass = pass && !memcmp(ct4, buf, DES_BLOCK_SIZE); - - three_des_key_setup(three_key2, three_schedule, DES_DECRYPT); - three_des_crypt(ct4, buf, three_schedule); - pass = pass && !memcmp(pt3, buf, DES_BLOCK_SIZE); - - return(pass); -} - -int main() -{ - printf("DES test: %s\n", des_test() ? "SUCCEEDED" : "FAILED"); - - return(0); -} diff --git a/lib/crypto-algorithms/md2_test.c b/lib/crypto-algorithms/md2_test.c deleted file mode 100644 index 883f20a..0000000 --- a/lib/crypto-algorithms/md2_test.c +++ /dev/null @@ -1,58 +0,0 @@ -/********************************************************************* -* Filename: md2_test.c -* Author: Brad Conte (brad AT bradconte.com) -* Copyright: -* Disclaimer: This code is presented "as is" without any guarantees. -* Details: Performs known-answer tests on the corresponding MD2 - implementation. These tests do not encompass the full - range of available test vectors, however, if the tests - pass it is very, very likely that the code is correct - and was compiled properly. This code also serves as - example usage of the functions. -*********************************************************************/ - -/*************************** HEADER FILES ***************************/ -#include <stdio.h> -#include <string.h> -#include <memory.h> -#include "md2.h" - -/*********************** FUNCTION DEFINITIONS ***********************/ -int md2_test() -{ - BYTE text1[] = {"abc"}; - BYTE text2[] = {"abcdefghijklmnopqrstuvwxyz"}; - BYTE text3_1[] = {"ABCDEFGHIJKLMNOPQRSTUVWXYZabcde"}; - BYTE text3_2[] = {"fghijklmnopqrstuvwxyz0123456789"}; - BYTE hash1[MD2_BLOCK_SIZE] = {0xda,0x85,0x3b,0x0d,0x3f,0x88,0xd9,0x9b,0x30,0x28,0x3a,0x69,0xe6,0xde,0xd6,0xbb}; - BYTE hash2[MD2_BLOCK_SIZE] = {0x4e,0x8d,0xdf,0xf3,0x65,0x02,0x92,0xab,0x5a,0x41,0x08,0xc3,0xaa,0x47,0x94,0x0b}; - BYTE hash3[MD2_BLOCK_SIZE] = {0xda,0x33,0xde,0xf2,0xa4,0x2d,0xf1,0x39,0x75,0x35,0x28,0x46,0xc3,0x03,0x38,0xcd}; - BYTE buf[16]; - MD2_CTX ctx; - int pass = 1; - - md2_init(&ctx); - md2_update(&ctx, text1, strlen(text1)); - md2_final(&ctx, buf); - pass = pass && !memcmp(hash1, buf, MD2_BLOCK_SIZE); - - // Note that the MD2 object can be re-used. - md2_init(&ctx); - md2_update(&ctx, text2, strlen(text2)); - md2_final(&ctx, buf); - pass = pass && !memcmp(hash2, buf, MD2_BLOCK_SIZE); - - // Note that the data is added in two chunks. - md2_init(&ctx); - md2_update(&ctx, text3_1, strlen(text3_1)); - md2_update(&ctx, text3_2, strlen(text3_2)); - md2_final(&ctx, buf); - pass = pass && !memcmp(hash3, buf, MD2_BLOCK_SIZE); - - return(pass); -} - -int main() -{ - printf("MD2 tests: %s\n", md2_test() ? "SUCCEEDED" : "FAILED"); -} diff --git a/lib/crypto-algorithms/md5_test.c b/lib/crypto-algorithms/md5_test.c deleted file mode 100644 index e945c8b..0000000 --- a/lib/crypto-algorithms/md5_test.c +++ /dev/null @@ -1,60 +0,0 @@ -/********************************************************************* -* Filename: md5_test.c -* Author: Brad Conte (brad AT bradconte.com) -* Copyright: -* Disclaimer: This code is presented "as is" without any guarantees. -* Details: Performs known-answer tests on the corresponding MD5 - implementation. These tests do not encompass the full - range of available test vectors, however, if the tests - pass it is very, very likely that the code is correct - and was compiled properly. This code also serves as - example usage of the functions. -*********************************************************************/ - -/*************************** HEADER FILES ***************************/ -#include <stdio.h> -#include <memory.h> -#include <string.h> -#include "md5.h" - -/*********************** FUNCTION DEFINITIONS ***********************/ -int md5_test() -{ - BYTE text1[] = {""}; - BYTE text2[] = {"abc"}; - BYTE text3_1[] = {"ABCDEFGHIJKLMNOPQRSTUVWXYZabcde"}; - BYTE text3_2[] = {"fghijklmnopqrstuvwxyz0123456789"}; - BYTE hash1[MD5_BLOCK_SIZE] = {0xd4,0x1d,0x8c,0xd9,0x8f,0x00,0xb2,0x04,0xe9,0x80,0x09,0x98,0xec,0xf8,0x42,0x7e}; - BYTE hash2[MD5_BLOCK_SIZE] = {0x90,0x01,0x50,0x98,0x3c,0xd2,0x4f,0xb0,0xd6,0x96,0x3f,0x7d,0x28,0xe1,0x7f,0x72}; - BYTE hash3[MD5_BLOCK_SIZE] = {0xd1,0x74,0xab,0x98,0xd2,0x77,0xd9,0xf5,0xa5,0x61,0x1c,0x2c,0x9f,0x41,0x9d,0x9f}; - BYTE buf[16]; - MD5_CTX ctx; - int pass = 1; - - md5_init(&ctx); - md5_update(&ctx, text1, strlen(text1)); - md5_final(&ctx, buf); - pass = pass && !memcmp(hash1, buf, MD5_BLOCK_SIZE); - - // Note the MD5 object can be reused. - md5_init(&ctx); - md5_update(&ctx, text2, strlen(text2)); - md5_final(&ctx, buf); - pass = pass && !memcmp(hash2, buf, MD5_BLOCK_SIZE); - - // Note the data is being added in two chunks. - md5_init(&ctx); - md5_update(&ctx, text3_1, strlen(text3_1)); - md5_update(&ctx, text3_2, strlen(text3_2)); - md5_final(&ctx, buf); - pass = pass && !memcmp(hash3, buf, MD5_BLOCK_SIZE); - - return(pass); -} - -int main() -{ - printf("MD5 tests: %s\n", md5_test() ? "SUCCEEDED" : "FAILED"); - - return(0); -} diff --git a/lib/crypto-algorithms/project.conf b/lib/crypto-algorithms/project.conf new file mode 100644 index 0000000..776dc58 --- /dev/null +++ b/lib/crypto-algorithms/project.conf @@ -0,0 +1,5 @@ +[package] +name = "crypto-algorithms" +type = "static" +version = "0.1.0" +platforms = ["any"] diff --git a/lib/crypto-algorithms/rot-13_test.c b/lib/crypto-algorithms/rot-13_test.c deleted file mode 100644 index a6fd01d..0000000 --- a/lib/crypto-algorithms/rot-13_test.c +++ /dev/null @@ -1,44 +0,0 @@ -/********************************************************************* -* Filename: rot-13_test.c -* Author: Brad Conte (brad AT bradconte.com) -* Copyright: -* Disclaimer: This code is presented "as is" without any guarantees. -* Details: Performs known-answer tests on the corresponding ROT-13 - implementation. These tests do not encompass the full - range of available test vectors, however, if the tests - pass it is very, very likely that the code is correct - and was compiled properly. This code also serves as - example usage of the functions. -*********************************************************************/ - -/*************************** HEADER FILES ***************************/ -#include <stdio.h> -#include <string.h> -#include "rot-13.h" - -/*********************** FUNCTION DEFINITIONS ***********************/ -int rot13_test() -{ - char text[] = {"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"}; - char code[] = {"NOPQRSTUVWXYZABCDEFGHIJKLMnopqrstuvwxyzabcdefghijklm"}; - char buf[1024]; - int pass = 1; - - // To encode, just apply ROT-13. - strcpy(buf, text); - rot13(buf); - pass = pass && !strcmp(code, buf); - - // To decode, just re-apply ROT-13. - rot13(buf); - pass = pass && !strcmp(text, buf); - - return(pass); -} - -int main() -{ - printf("ROT-13 tests: %s\n", rot13_test() ? "SUCCEEDED" : "FAILED"); - - return(0); -} diff --git a/lib/crypto-algorithms/sha1_test.c b/lib/crypto-algorithms/sha1_test.c deleted file mode 100644 index 6c78f7d..0000000 --- a/lib/crypto-algorithms/sha1_test.c +++ /dev/null @@ -1,58 +0,0 @@ -/********************************************************************* -* Filename: sha1_test.c -* Author: Brad Conte (brad AT bradconte.com) -* Copyright: -* Disclaimer: This code is presented "as is" without any guarantees. -* Details: Performs known-answer tests on the corresponding SHA1 - implementation. These tests do not encompass the full - range of available test vectors, however, if the tests - pass it is very, very likely that the code is correct - and was compiled properly. This code also serves as - example usage of the functions. -*********************************************************************/ - -/*************************** HEADER FILES ***************************/ -#include <stdio.h> -#include <memory.h> -#include <string.h> -#include "sha1.h" - -/*********************** FUNCTION DEFINITIONS ***********************/ -int sha1_test() -{ - BYTE text1[] = {"abc"}; - BYTE text2[] = {"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"}; - BYTE text3[] = {"aaaaaaaaaa"}; - BYTE hash1[SHA1_BLOCK_SIZE] = {0xa9,0x99,0x3e,0x36,0x47,0x06,0x81,0x6a,0xba,0x3e,0x25,0x71,0x78,0x50,0xc2,0x6c,0x9c,0xd0,0xd8,0x9d}; - BYTE hash2[SHA1_BLOCK_SIZE] = {0x84,0x98,0x3e,0x44,0x1c,0x3b,0xd2,0x6e,0xba,0xae,0x4a,0xa1,0xf9,0x51,0x29,0xe5,0xe5,0x46,0x70,0xf1}; - BYTE hash3[SHA1_BLOCK_SIZE] = {0x34,0xaa,0x97,0x3c,0xd4,0xc4,0xda,0xa4,0xf6,0x1e,0xeb,0x2b,0xdb,0xad,0x27,0x31,0x65,0x34,0x01,0x6f}; - BYTE buf[SHA1_BLOCK_SIZE]; - int idx; - SHA1_CTX ctx; - int pass = 1; - - sha1_init(&ctx); - sha1_update(&ctx, text1, strlen(text1)); - sha1_final(&ctx, buf); - pass = pass && !memcmp(hash1, buf, SHA1_BLOCK_SIZE); - - sha1_init(&ctx); - sha1_update(&ctx, text2, strlen(text2)); - sha1_final(&ctx, buf); - pass = pass && !memcmp(hash2, buf, SHA1_BLOCK_SIZE); - - sha1_init(&ctx); - for (idx = 0; idx < 100000; ++idx) - sha1_update(&ctx, text3, strlen(text3)); - sha1_final(&ctx, buf); - pass = pass && !memcmp(hash3, buf, SHA1_BLOCK_SIZE); - - return(pass); -} - -int main() -{ - printf("SHA1 tests: %s\n", sha1_test() ? "SUCCEEDED" : "FAILED"); - - return(0); -} diff --git a/lib/crypto-algorithms/sha256_test.c b/lib/crypto-algorithms/sha256_test.c deleted file mode 100644 index 6951c51..0000000 --- a/lib/crypto-algorithms/sha256_test.c +++ /dev/null @@ -1,61 +0,0 @@ -/********************************************************************* -* Filename: sha256.c -* Author: Brad Conte (brad AT bradconte.com) -* Copyright: -* Disclaimer: This code is presented "as is" without any guarantees. -* Details: Performs known-answer tests on the corresponding SHA1 - implementation. These tests do not encompass the full - range of available test vectors, however, if the tests - pass it is very, very likely that the code is correct - and was compiled properly. This code also serves as - example usage of the functions. -*********************************************************************/ - -/*************************** HEADER FILES ***************************/ -#include <stdio.h> -#include <memory.h> -#include <string.h> -#include "sha256.h" - -/*********************** FUNCTION DEFINITIONS ***********************/ -int sha256_test() -{ - BYTE text1[] = {"abc"}; - BYTE text2[] = {"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"}; - BYTE text3[] = {"aaaaaaaaaa"}; - BYTE hash1[SHA256_BLOCK_SIZE] = {0xba,0x78,0x16,0xbf,0x8f,0x01,0xcf,0xea,0x41,0x41,0x40,0xde,0x5d,0xae,0x22,0x23, - 0xb0,0x03,0x61,0xa3,0x96,0x17,0x7a,0x9c,0xb4,0x10,0xff,0x61,0xf2,0x00,0x15,0xad}; - BYTE hash2[SHA256_BLOCK_SIZE] = {0x24,0x8d,0x6a,0x61,0xd2,0x06,0x38,0xb8,0xe5,0xc0,0x26,0x93,0x0c,0x3e,0x60,0x39, - 0xa3,0x3c,0xe4,0x59,0x64,0xff,0x21,0x67,0xf6,0xec,0xed,0xd4,0x19,0xdb,0x06,0xc1}; - BYTE hash3[SHA256_BLOCK_SIZE] = {0xcd,0xc7,0x6e,0x5c,0x99,0x14,0xfb,0x92,0x81,0xa1,0xc7,0xe2,0x84,0xd7,0x3e,0x67, - 0xf1,0x80,0x9a,0x48,0xa4,0x97,0x20,0x0e,0x04,0x6d,0x39,0xcc,0xc7,0x11,0x2c,0xd0}; - BYTE buf[SHA256_BLOCK_SIZE]; - SHA256_CTX ctx; - int idx; - int pass = 1; - - sha256_init(&ctx); - sha256_update(&ctx, text1, strlen(text1)); - sha256_final(&ctx, buf); - pass = pass && !memcmp(hash1, buf, SHA256_BLOCK_SIZE); - - sha256_init(&ctx); - sha256_update(&ctx, text2, strlen(text2)); - sha256_final(&ctx, buf); - pass = pass && !memcmp(hash2, buf, SHA256_BLOCK_SIZE); - - sha256_init(&ctx); - for (idx = 0; idx < 100000; ++idx) - sha256_update(&ctx, text3, strlen(text3)); - sha256_final(&ctx, buf); - pass = pass && !memcmp(hash3, buf, SHA256_BLOCK_SIZE); - - return(pass); -} - -int main() -{ - printf("SHA-256 tests: %s\n", sha256_test() ? "SUCCEEDED" : "FAILEd"); - - return(0); -} |