Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Add Makefile and fix all c99 warnings
  • Loading branch information
lcn2 committed May 18, 2017
commit 1d975f5d6ee2fb11a185a041dd2a81f871c23e5b
10 changes: 10 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
aes_test
arcfour_test
base64_test
blowfish_test
des_test
md2_test
md5_test
rot-13_test
sha1_test
sha256_test
133 changes: 133 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
#!/bin/make
# @(#)Makefile 1.2 04 May 1995 02:06:57
#
# crypto-algorithms-master - Brad Conte's basic implementations of standard cryptography algorithms
#
# @(#) $Revision$
# @(#) $Id$
# @(#) $Source$
#
# chongo (Landon Curt Noll, http://www.isthe.com/chongo/index.html) /\oo/\
#
# Share and enjoy! :-)

SHELL= /bin/bash
CC= cc
CFLAGS= -O3 -g3 -std=c99 -Wall -pedantic

INSTALL= install
DESTDIR= /usr/global/bin

TEST_TARGETS= aes_test arcfour_test base64_test blowfish_test des_test md2_test md5_test rot-13_test sha1_test sha256_test
TARGETS= ${TEST_TARGETS}

all: ${TARGETS}

aes_test: aes_test.o aes.o
${CC} ${CFLAGS} aes_test.o aes.o -o aes_test

aes.o: aes.c aes.h
${CC} ${CFLAGS} aes.c -c

aes_test.o: aes_test.c aes.h
${CC} ${CFLAGS} aes_test.c -c

arcfour_test: arcfour_test.o arcfour.o
${CC} ${CFLAGS} arcfour_test.o arcfour.o -o arcfour_test

arcfour.o: arcfour.c arcfour.h
${CC} ${CFLAGS} arcfour.c -c

arcfour_test.o: arcfour_test.c arcfour.h
${CC} ${CFLAGS} arcfour_test.c -c

base64_test: base64_test.o base64.o
${CC} ${CFLAGS} base64_test.o base64.o -o base64_test

base64.o: base64.c base64.h
${CC} ${CFLAGS} base64.c -c

base64_test.o: base64_test.c base64.h
${CC} ${CFLAGS} base64_test.c -c

blowfish_test: blowfish_test.o blowfish.o
${CC} ${CFLAGS} blowfish_test.o blowfish.o -o blowfish_test

blowfish.o: blowfish.c blowfish.h
${CC} ${CFLAGS} blowfish.c -c

blowfish_test.o: blowfish_test.c blowfish.h
${CC} ${CFLAGS} blowfish_test.c -c

des_test: des_test.o des.o
${CC} ${CFLAGS} des_test.o des.o -o des_test

des.o: des.c des.h
${CC} ${CFLAGS} des.c -c

des_test.o: des_test.c des.h
${CC} ${CFLAGS} des_test.c -c

md2_test: md2_test.o md2.o
${CC} ${CFLAGS} md2_test.o md2.o -o md2_test

md2.o: md2.c md2.h
${CC} ${CFLAGS} md2.c -c

md2_test.o: md2_test.c md2.h
${CC} ${CFLAGS} md2_test.c -c

md5_test: md5_test.o md5.o
${CC} ${CFLAGS} md5_test.o md5.o -o md5_test

md5.o: md5.c md5.h
${CC} ${CFLAGS} md5.c -c

md5_test.o: md5_test.c md5.h
${CC} ${CFLAGS} md5_test.c -c

rot-13_test: rot-13_test.o rot-13.o
${CC} ${CFLAGS} rot-13_test.o rot-13.o -o rot-13_test

rot-13.o: rot-13.c rot-13.h
${CC} ${CFLAGS} rot-13.c -c

rot-13_test.o: rot-13_test.c rot-13.h
${CC} ${CFLAGS} rot-13_test.c -c

sha1_test: sha1_test.o sha1.o
${CC} ${CFLAGS} sha1_test.o sha1.o -o sha1_test

sha1.o: sha1.c sha1.h
${CC} ${CFLAGS} sha1.c -c

sha1_test.o: sha1_test.c sha1.h
${CC} ${CFLAGS} sha1_test.c -c

sha256_test: sha256_test.o sha256.o
${CC} ${CFLAGS} sha256_test.o sha256.o -o sha256_test

sha256.o: sha256.c sha256.h
${CC} ${CFLAGS} sha256.c -c

sha256_test.o: sha256_test.c sha256.h
${CC} ${CFLAGS} sha256_test.c -c

test: ${TEST_TARGETS}
@-for i in ${TEST_TARGETS}; do \
echo running '=-=' $$i '=-='; \
./$$i; \
done

configure:
@echo nothing to configure

clean quick_clean quick_distclean distclean:
rm -f *.o

clobber quick_clobber: clean
rm -f ${TARGETS}

install: all
@echo not configured to install, perhaps ${INSTALL} -m 0555 ${TARGETS} ${DESTDIR}

2 changes: 2 additions & 0 deletions aes_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
#include <memory.h>
#include "aes.h"

extern int aes_decrypt_cbc(const BYTE in[], size_t in_len, BYTE out[], const WORD key[], int keysize, const BYTE iv[]);

/*********************** FUNCTION DEFINITIONS ***********************/
void print_hex(BYTE str[], int len)
{
Expand Down
2 changes: 1 addition & 1 deletion arcfour_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ int rc4_test()

// 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_key_setup(state, key[idx], strlen((char *)key[idx]));
arcfour_generate_stream(state, buf, stream_len[idx]);
pass = pass && !memcmp(stream[idx], buf, stream_len[idx]);
}
Expand Down
1 change: 0 additions & 1 deletion base64.c
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ size_t base64_encode(const BYTE in[], BYTE out[], size_t len, int newline_flag)

size_t base64_decode(const BYTE in[], BYTE out[], size_t len)
{
BYTE ch;
size_t idx, idx2, blks, blk_ceiling, left_over;

if (in[len - 1] == '=')
Expand Down
16 changes: 8 additions & 8 deletions base64_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,16 @@ int base64_test()
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);
buf_len = base64_encode(text[idx], buf, strlen((const char *)text[idx]), 1);
pass = pass && ((buf_len == strlen((const char *)code[idx])) &&
(buf_len == base64_encode(text[idx], NULL, strlen((const char *)text[idx]), 1)));
pass = pass && !strcmp((const char *)code[idx], (const char *)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);
buf_len = base64_decode(code[idx], buf, strlen((const char *)code[idx]));
pass = pass && ((buf_len == strlen((const char *)text[idx])) &&
(buf_len == base64_decode(code[idx], NULL, strlen((const char *)code[idx]))));
pass = pass && !strcmp((const char *)text[idx], (const char *)buf);
}

return(pass);
Expand Down
16 changes: 8 additions & 8 deletions des_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,35 +41,35 @@ int des_test()
int pass = 1;

des_key_setup(key1, schedule, DES_ENCRYPT);
des_crypt(pt1, buf, schedule);
des_crypt(pt1, buf, (const BYTE (*)[6]) schedule);
pass = pass && !memcmp(ct1, buf, DES_BLOCK_SIZE);

des_key_setup(key1, schedule, DES_DECRYPT);
des_crypt(ct1, buf, schedule);
des_crypt(ct1, buf, (const BYTE (*)[6]) schedule);
pass = pass && !memcmp(pt1, buf, DES_BLOCK_SIZE);

des_key_setup(key2, schedule, DES_ENCRYPT);
des_crypt(pt2, buf, schedule);
des_crypt(pt2, buf, (const BYTE (*)[6]) schedule);
pass = pass && !memcmp(ct2, buf, DES_BLOCK_SIZE);

des_key_setup(key2, schedule, DES_DECRYPT);
des_crypt(ct2, buf, schedule);
des_crypt(ct2, buf, (const BYTE (*)[6]) 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);
three_des_crypt(pt1, buf, (const BYTE (*)[16][6]) 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);
three_des_crypt(ct3, buf, (const BYTE (*)[16][6]) 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);
three_des_crypt(pt3, buf, (const BYTE (*)[16][6]) 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);
three_des_crypt(ct4, buf, (const BYTE (*)[16][6]) three_schedule);
pass = pass && !memcmp(pt3, buf, DES_BLOCK_SIZE);

return(pass);
Expand Down
8 changes: 4 additions & 4 deletions md2_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,20 @@ int md2_test()
int pass = 1;

md2_init(&ctx);
md2_update(&ctx, text1, strlen(text1));
md2_update(&ctx, text1, strlen((const char *)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_update(&ctx, text2, strlen((const char *)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_update(&ctx, text3_1, strlen((const char *)text3_1));
md2_update(&ctx, text3_2, strlen((const char *)text3_2));
md2_final(&ctx, buf);
pass = pass && !memcmp(hash3, buf, MD2_BLOCK_SIZE);

Expand Down
8 changes: 4 additions & 4 deletions md5_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,20 @@ int md5_test()
int pass = 1;

md5_init(&ctx);
md5_update(&ctx, text1, strlen(text1));
md5_update(&ctx, text1, strlen((const char *)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_update(&ctx, text2, strlen((const char *)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_update(&ctx, text3_1, strlen((const char *)text3_1));
md5_update(&ctx, text3_2, strlen((const char *)text3_2));
md5_final(&ctx, buf);
pass = pass && !memcmp(hash3, buf, MD5_BLOCK_SIZE);

Expand Down
6 changes: 3 additions & 3 deletions sha1_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,18 @@ int sha1_test()
int pass = 1;

sha1_init(&ctx);
sha1_update(&ctx, text1, strlen(text1));
sha1_update(&ctx, text1, strlen((const char *)text1));
sha1_final(&ctx, buf);
pass = pass && !memcmp(hash1, buf, SHA1_BLOCK_SIZE);

sha1_init(&ctx);
sha1_update(&ctx, text2, strlen(text2));
sha1_update(&ctx, text2, strlen((const char *)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_update(&ctx, text3, strlen((const char *)text3));
sha1_final(&ctx, buf);
pass = pass && !memcmp(hash3, buf, SHA1_BLOCK_SIZE);

Expand Down
6 changes: 3 additions & 3 deletions sha256_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,18 @@ int sha256_test()
int pass = 1;

sha256_init(&ctx);
sha256_update(&ctx, text1, strlen(text1));
sha256_update(&ctx, text1, strlen((const char *)text1));
sha256_final(&ctx, buf);
pass = pass && !memcmp(hash1, buf, SHA256_BLOCK_SIZE);

sha256_init(&ctx);
sha256_update(&ctx, text2, strlen(text2));
sha256_update(&ctx, text2, strlen((const char *)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_update(&ctx, text3, strlen((const char *)text3));
sha256_final(&ctx, buf);
pass = pass && !memcmp(hash3, buf, SHA256_BLOCK_SIZE);

Expand Down