Skip to content

Commit cc89a3d

Browse files
committed
Add test example for utility functions
1 parent 644796a commit cc89a3d

File tree

5 files changed

+58
-17
lines changed

5 files changed

+58
-17
lines changed

Cryptopal-Challenges/brice/Makefile

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
DEBUG=false
44
CC=gcc
55

6+
CCOPTS= -Wno-pointer-sign
7+
68
LIBS= -lssl -lcrypto
79
INCLUDES= -I/usr/local/opt/openssl/include -I./src/cryptolib
810
LDFLAGS= -L/usr/local/opt/openssl/lib
@@ -39,26 +41,26 @@ test: $(TEST_DIR) $(TESTS_EXE)
3941

4042
run-tests : test
4143
@echo "\n============= BEGIN TESTS =============\n"
42-
find $(TEST_DIR) -name "test*" -type file -exec {} \;
44+
@find $(TEST_DIR) -name "test*" -type file -exec {} \;
4345

4446
run-challenges : $(CHALLENGES_EXE)
4547
@echo "\n============= BEGIN CHALLENGES =============\n"
46-
find $(BUILD_DIR) -name "Challenge-*" -type file -exec {} \;
48+
@find $(BUILD_DIR) -name "Challenge-*" -type file -exec {} \;
4749

4850

4951
lib: $(BUILD_DIR)/cryptolib.a
5052

5153
$(TEST_DIR)/% : test/%.c test/common.c $(BUILD_DIR)/cryptolib.a
52-
$(CC) $(CFLAGS) $^ -o $@ $(LIBS) $(LDFLAGS) $(INCLUDES)
54+
$(CC) $(CCOPTS) $(CFLAGS) $^ -o $@ $(LIBS) $(LDFLAGS) $(INCLUDES)
5355

5456
Challenge-%.exe : src/Challenges/Challenge-%.c $(BUILD_DIR)/cryptolib.a
55-
$(CC) $(CFLAGS) $^ -o $@ $(LIBS) $(LDFLAGS) $(INCLUDES)
57+
$(CC) $(CCOPTS) $(CFLAGS) $^ -o $@ $(LIBS) $(LDFLAGS) $(INCLUDES)
5658

5759
$(BUILD_DIR)/cryptolib.a: $(CRYPTOLIB_OBJ)
5860
ar rcs $@ $^
5961

6062
$(BUILD_DIR)/%.o : src/cryptolib/%.c $(BUILD_DIR)
61-
$(CC) $(CFLAGS) $< -c -o $@ $(INCLUDES)
63+
$(CC) $(CCOPTS) $(CFLAGS) $< -c -o $@ $(INCLUDES)
6264

6365
clean:
6466
rm -rf $(BUILD_DIR)

Cryptopal-Challenges/brice/src/cryptolib/errors.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ typedef struct {
1111

1212
static ErrorMessage Errors[] = {
1313
{OK, "Everything OK"},
14+
{BAD_INPUT, "Bad input provided to function."},
1415
{OPENSSL_ERROR, "Caught openssl exception"},
1516
{IV_PROVIDED_IN_ERROR, "IV was provided for a block cipher mode that doesn't need it."},
1617
{LIMIT_TOO_SMALL, "Size limit of output buffer was too small. Try again with a bigger buffer."},

Cryptopal-Challenges/brice/src/cryptolib/errors.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
typedef enum {
55
OK,
6+
BAD_INPUT,
67
OPENSSL_ERROR,
78
IV_PROVIDED_IN_ERROR,
89
LIMIT_TOO_SMALL,

Cryptopal-Challenges/brice/src/cryptolib/utils.c

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include <stdlib.h>
2+
#include <stdbool.h>
23

34
#include "common.h"
45
#include "errors.h"
@@ -28,6 +29,15 @@ ERROR toHex(
2829
OUT int* out_length
2930
);
3031

32+
bool isHex(c){
33+
return ('0' <= c && c <= '9') || ( 'A' <= c && c <= 'F' ) || ('a' <= c && c <= 'f');
34+
}
35+
36+
37+
/**
38+
* Limitations: Won't handle badly formatted input properly.
39+
* bad input is not unsafe.
40+
*/
3141
ERROR fromHex(
3242
const unsigned char* hex,
3343
const int hex_length,
@@ -37,12 +47,15 @@ ERROR fromHex(
3747
){
3848
int raw_index = 0;
3949
for (int i = 0; i < (hex_length-1); i+=2){
40-
// grab two hex chars from string
50+
51+
if(!isHex(hex[i]) || !isHex(hex[i+1])){ return BAD_INPUT; }
52+
4153
char aByte[3] = {hex[i],hex[i+1],0};
4254
long c = strtol(aByte, NULL, 16);
4355

4456
if(raw_index>raw_limit){ return LIMIT_TOO_SMALL; }
4557
raw[raw_index] = (char) c;
58+
raw_index++;
4659

4760
}
4861
return OK;

Cryptopal-Challenges/brice/test/test_utils.c

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,50 @@
77
#include "common.h"
88

99
typedef struct {
10-
char* hex;
10+
char* in;
1111
char* expected;
12+
ERROR status;
1213
} Test ;
1314

14-
Test tests[] = {
15-
{"01", "\x01"},
16-
{NULL, NULL}
17-
};
1815

19-
int main(){
20-
for (int i = 0; tests[i].hex != NULL; i++){
16+
17+
18+
int test_utils_fromHex(void){
19+
Test tests[] = {
20+
{"00", "\x00", OK},
21+
{"01", "\x01", OK},
22+
{"ff", "\xff", OK},
23+
{"000x", "", BAD_INPUT},
24+
{"000", "", BAD_INPUT},
25+
{"0102030405060708090a0b0c0d0e0f10", "\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10", OK},
26+
{NULL, NULL}
27+
};
28+
printf("TESTING: fromHex()\n");
29+
for (int i = 0; tests[i].in != NULL; i++){
2130
ERROR status = OK;
31+
const bytes in = (const bytes) tests[i].in;
32+
const bytes expected = (const bytes) tests[i].expected;
33+
ERROR expected_status = tests[i].status;
2234
char buffer[256] = {0};
2335
int len = 0;
2436

25-
status = fromHex(tests[i].hex, strlen(tests[i].hex), buffer, 256, &len);
26-
if(status != OK){
27-
log_error(status); exit(1);
37+
status = fromHex(in, strlen(in), (bytes) buffer, 256, &len);
38+
39+
if(status != expected_status){
40+
printf(" FAIL: h(%s) unexpected status (got=%s, expected=%s)\n", in, error_string(status), error_string(expected_status));
41+
42+
}else if((strcmp(expected, buffer) !=0) || (strlen(expected) != strlen(buffer))){
43+
printf(" FAIL: h(%s)\n", in);
44+
printf(" expected: x(");print_hex(expected);printf(")%lu\n", strlen(expected));
45+
printf(" got: x(");print_hex((const bytes) buffer);printf(")%lu \n",strlen(buffer));
46+
}else{
47+
printf(" pass: h(%s)\n", in);
2848
}
29-
printf("fromHex(%s) -> x(", tests[i].hex);print_hex(buffer);printf(")\n");
49+
3050
}
3151
return 0;
3252
}
53+
54+
int main(){
55+
return test_utils_fromHex();
56+
}

0 commit comments

Comments
 (0)