Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed compiler issues, fixed several algorithms, 64-bit machine fix, add Makefile to build and run all tests, rollup of all previous (to #15) pull requests #15

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
Open
11 changes: 11 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
aes_test
arcfour_test
base64_test
blowfish_test
des_test
md2_test
md5_test
rot-13_test
sha1_test
sha256_test
speck128128_test
156 changes: 156 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
#!/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 \
speck128128_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

speck128128_test: speck128128_test.o speck128128.o
${CC} ${CFLAGS} speck128128_test.o speck128128.o -o speck128128_test

speck128128_test.o: speck128128_test.c speck128128.h
${CC} ${CFLAGS} speck128128_test.c -c

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

test: ${TEST_TARGETS}
@export last_error="0"; \
for i in ${TEST_TARGETS}; do \
echo running '=-=' $$i '=-='; \
./$$i; \
status="$$?"; \
if [ "$$status" != 0 ]; then \
echo "WARNING: test $$i failed!!"; \
last_error="$$status"; \
fi; \
done; \
if [ "$$last_error" = 0 ]; then \
echo "=-=-= All tests PASSED =-=-="; \
else \
echo "=-=-= Some tests FAILED =-=-="; \
fi; \
exit "$$last_error"

configure:
@echo nothing to configure

clean quick_clean:
rm -f *.o

clobber quick_clobber quick_distclean distclean:
rm -f *.o
rm -f ${TARGETS}

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

10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,12 @@ These algorithms are not optimized for speed or space. They are primarily design

Building
---
The source code for each algorithm will come in a pair of a source code file and a header file. There should be no inter-header file dependencies, no additional libraries, no platform-specific header files, or any other complicating matters. Compiling them should be as easy as adding the relevent source code to the project.
The source code for each algorithm will come in a pair of a source code file and a header file. There should be no inter-header file dependencies, no additional libraries, no platform-specific header files, or any other complicating matters. Compiling them should be as easy as adding the relevent source code to the project.

For systems have make:

make clobber all

To run all of the test:

make test
8 changes: 6 additions & 2 deletions aes.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,17 @@

/*************************** HEADER FILES ***************************/
#include <stddef.h>
#include <stdint.h>

/****************************** MACROS ******************************/
#define AES_BLOCK_SIZE 16 // AES operates on 16 bytes at a time

/**************************** DATA TYPES ****************************/
typedef unsigned char BYTE; // 8-bit byte
typedef unsigned int WORD; // 32-bit word, change to "long" for 16-bit machines
#if !defined(CRYPTO_TYPES)
typedef uint8_t BYTE; // 8-bit byte
typedef uint32_t WORD; // 32-bit word, change to "long" for 16-bit machines
#define CRYPTO_TYPES
#endif

/*********************** FUNCTION DECLARATIONS **********************/
///////////////////
Expand Down
10 changes: 8 additions & 2 deletions aes_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@
/*************************** HEADER FILES ***************************/
#include <stdio.h>
#include <memory.h>
#include <stdlib.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 Expand Up @@ -279,7 +282,10 @@ int aes_test()

int main(int argc, char *argv[])
{
printf("AES Tests: %s\n", aes_test() ? "SUCCEEDED" : "FAILED");
int ret; // 0 ==> test failed, != 0 ==> test suceeded

ret = aes_test();
printf("AES Tests: %s\n", ret ? "SUCCEEDED" : "FAILED");

return(0);
exit(ret == 0 ? 1 : 0);
}
13 changes: 9 additions & 4 deletions arcfour.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,27 @@ void arcfour_key_setup(BYTE state[], const BYTE key[], int len)
state[i] = state[j];
state[j] = t;
}
state[ARCFOUR_I] = 0;
state[ARCFOUR_J] = 0;
}

// This does not hold state between calls. It always generates the
// stream starting from the first output byte.
void arcfour_generate_stream(BYTE state[], BYTE out[], size_t len)
{
int i, j;
int i;
int j;
size_t idx;
BYTE t;

for (idx = 0, i = 0, j = 0; idx < len; ++idx) {
i = state[ARCFOUR_I];
j = state[ARCFOUR_J];
for (idx = 0; idx < len; ++idx) {
i = (i + 1) % 256;
j = (j + state[i]) % 256;
t = state[i];
state[i] = state[j];
state[j] = t;
out[idx] = state[(state[i] + state[j]) % 256];
}
state[ARCFOUR_I] = i;
state[ARCFOUR_J] = j;
}
13 changes: 12 additions & 1 deletion arcfour.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,13 @@

/*************************** HEADER FILES ***************************/
#include <stddef.h>
#include <stdint.h>

/**************************** DATA TYPES ****************************/
typedef unsigned char BYTE; // 8-bit byte
#if !defined(CRYPTO_TYPES)
typedef uint8_t BYTE; // 8-bit byte
#define CRYPTO_TYPES
#endif

/*********************** FUNCTION DECLARATIONS **********************/
// Input: state - the state used to generate the keystream
Expand All @@ -27,4 +31,11 @@ void arcfour_key_setup(BYTE state[], const BYTE key[], int len);
// len - number of bytes to generate
void arcfour_generate_stream(BYTE state[], BYTE out[], size_t len);

#if !defined(ARCFOUR_I)
# define ARCFOUR_I (256)
#endif
#if !defined(ARCFOUR_J)
# define ARCFOUR_J (257)
#endif

#endif // ARCFOUR_H
12 changes: 8 additions & 4 deletions arcfour_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@
/*************************** HEADER FILES ***************************/
#include <stdio.h>
#include <memory.h>
#include <stdlib.h>
#include "arcfour.h"

/*********************** FUNCTION DEFINITIONS ***********************/
int rc4_test()
{
BYTE state[256];
BYTE state[256+2];
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},
Expand All @@ -31,7 +32,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 All @@ -41,7 +42,10 @@ int rc4_test()

int main()
{
printf("ARCFOUR tests: %s\n", rc4_test() ? "SUCCEEDED" : "FAILED");
int ret; // 0 ==> test failed, != 0 ==> test suceeded

return(0);
ret = rc4_test();
printf("ARCFOUR Tests: %s\n", ret ? "SUCCEEDED" : "FAILED");

exit(ret == 0 ? 1 : 0);
}
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
6 changes: 5 additions & 1 deletion base64.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,13 @@

/*************************** HEADER FILES ***************************/
#include <stddef.h>
#include <stdint.h>

/**************************** DATA TYPES ****************************/
typedef unsigned char BYTE; // 8-bit byte
#if !defined(CRYPTO_TYPES)
typedef uint8_t BYTE; // 8-bit byte
#define CRYPTO_TYPES
#endif

/*********************** FUNCTION DECLARATIONS **********************/
// Returns the size of the output. If called with out = NULL, will just return
Expand Down
25 changes: 15 additions & 10 deletions base64_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
/*************************** HEADER FILES ***************************/
#include <stdio.h>
#include <memory.h>
#include <stdlib.h>
#include "base64.h"

/*********************** FUNCTION DEFINITIONS ***********************/
Expand All @@ -31,24 +32,28 @@ 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);
memset(buf, 0, sizeof(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);
}

int main()
{
printf("Base64 tests: %s\n", base64_test() ? "PASSED" : "FAILED");
int ret; // 0 ==> test failed, != 0 ==> test suceeded

ret = base64_test();
printf("Base64 Tests: %s\n", ret ? "SUCCEEDED" : "FAILED");

return 0;
exit(ret == 0 ? 1 : 0);
}
Loading