Skip to content

Commit e41b7a0

Browse files
committed
sha1: moved self test to separated test
1 parent 0994022 commit e41b7a0

File tree

4 files changed

+85
-152
lines changed

4 files changed

+85
-152
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ tests/test_jid
4848
tests/test_rand
4949
tests/test_sasl
5050
tests/test_scram
51+
tests/test_sha1
5152
tests/test_sock
5253
m4/
5354
libstrophe.project

Makefile.am

+5-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ examples_uuid_LDADD = $(STROPHE_LIBS)
6565

6666

6767
## Tests
68-
TESTS = tests/check_parser tests/test_rand tests/test_scram tests/test_base64
68+
TESTS = tests/check_parser tests/test_sha1 tests/test_rand tests/test_scram \
69+
tests/test_base64
6970
check_PROGRAMS = $(TESTS)
7071

7172
tests_check_parser_SOURCES = tests/check_parser.c tests/test.h
@@ -84,3 +85,6 @@ tests_test_rand_CFLAGS = $(STROPHE_FLAGS) -I$(top_srcdir)/src
8485

8586
tests_test_scram_SOURCES = tests/test_scram.c tests/test.c src/sha1.c
8687
tests_test_scram_CFLAGS = $(STROPHE_FLAGS) -I$(top_srcdir)/src
88+
89+
tests_test_sha1_SOURCES = tests/test_sha1.c src/sha1.c
90+
tests_test_sha1_CFLAGS = -I$(top_srcdir)/src

src/sha1.c

-151
Original file line numberDiff line numberDiff line change
@@ -70,24 +70,11 @@ use SHA1_ prefix for public api
7070
move public api to sha1.h
7171
*/
7272

73-
/*
74-
Test Vectors (from FIPS PUB 180-1)
75-
"abc"
76-
A9993E36 4706816A BA3E2571 7850C26C 9CD0D89D
77-
"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"
78-
84983E44 1C3BD26E BAAE4AA1 F95129E5 E54670F1
79-
A million repetitions of "a"
80-
34AA973C D4C4DAA4 F61EEB2B DBAD2731 6534016F
81-
*/
82-
8373
/* #define SHA1HANDSOFF */
8474

85-
#include <stdio.h>
8675
#include <string.h>
8776

88-
/* make sure the stdint.h types are available */
8977
#include "ostypes.h"
90-
9178
#include "sha1.h"
9279

9380
static uint32_t host_to_be(uint32_t i);
@@ -109,19 +96,6 @@ static void SHA1_Transform(uint32_t state[5], const uint8_t buffer[64]);
10996
#define R4(v,w,x,y,z,i) z+=(w^x^y)+blk(i)+0xCA62C1D6+rol(v,5);w=rol(w,30);
11097

11198

112-
#ifdef VERBOSE /* SAK */
113-
void SHAPrintContext(SHA1_CTX *context, char *msg){
114-
printf("%s (%d,%d) %x %x %x %x %x\n",
115-
msg,
116-
context->count[0], context->count[1],
117-
context->state[0],
118-
context->state[1],
119-
context->state[2],
120-
context->state[3],
121-
context->state[4]);
122-
}
123-
#endif /* VERBOSE */
124-
12599
static uint32_t host_to_be(uint32_t i)
126100
{
127101
#define le_to_be(i) ((rol((i),24) & 0xFF00FF00) | (rol((i),8) & 0x00FF00FF))
@@ -220,10 +194,6 @@ void crypto_SHA1_Update(SHA1_CTX* context, const uint8_t* data,
220194
{
221195
size_t i, j;
222196

223-
#ifdef VERBOSE
224-
SHAPrintContext(context, "before");
225-
#endif
226-
227197
j = (context->count[0] >> 3) & 63;
228198
if ((context->count[0] += len << 3) < (len << 3)) context->count[1]++;
229199
context->count[1] += (len >> 29);
@@ -237,10 +207,6 @@ void crypto_SHA1_Update(SHA1_CTX* context, const uint8_t* data,
237207
}
238208
else i = 0;
239209
memcpy(&context->buffer[j], &data[i], len - i);
240-
241-
#ifdef VERBOSE
242-
SHAPrintContext(context, "after ");
243-
#endif
244210
}
245211

246212

@@ -284,120 +250,3 @@ void crypto_SHA1(const uint8_t* data, size_t len, uint8_t* digest)
284250
crypto_SHA1_Update(&ctx, data, len);
285251
crypto_SHA1_Final(&ctx, digest);
286252
}
287-
288-
/*************************************************************/
289-
290-
#if 0
291-
int main(int argc, char** argv)
292-
{
293-
int i, j;
294-
SHA1_CTX context;
295-
unsigned char digest[SHA1_DIGEST_SIZE], buffer[16384];
296-
FILE* file;
297-
298-
if (argc > 2) {
299-
puts("Public domain SHA-1 implementation - by Steve Reid <sreid@sea-to-sky.net>");
300-
puts("Modified for 16 bit environments 7/98 - by James H. Brown <jbrown@burgoyne.com>"); /* JHB */
301-
puts("Produces the SHA-1 hash of a file, or stdin if no file is specified.");
302-
return(0);
303-
}
304-
if (argc < 2) {
305-
file = stdin;
306-
}
307-
else {
308-
if (!(file = fopen(argv[1], "rb"))) {
309-
fputs("Unable to open file.", stderr);
310-
return(-1);
311-
}
312-
}
313-
crypto_SHA1_Init(&context);
314-
while (!feof(file)) { /* note: what if ferror(file) */
315-
i = fread(buffer, 1, 16384, file);
316-
crypto_SHA1_Update(&context, buffer, i);
317-
}
318-
crypto_SHA1_Final(&context, digest);
319-
fclose(file);
320-
for (i = 0; i < SHA1_DIGEST_SIZE/4; i++) {
321-
for (j = 0; j < 4; j++) {
322-
printf("%02X", digest[i*4+j]);
323-
}
324-
putchar(' ');
325-
}
326-
putchar('\n');
327-
return(0); /* JHB */
328-
}
329-
#endif
330-
331-
/* self test */
332-
333-
#ifdef TEST
334-
335-
static char *test_data[] = {
336-
"abc",
337-
"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
338-
"A million repetitions of 'a'"};
339-
static char *test_results[] = {
340-
"A9993E36 4706816A BA3E2571 7850C26C 9CD0D89D",
341-
"84983E44 1C3BD26E BAAE4AA1 F95129E5 E54670F1",
342-
"34AA973C D4C4DAA4 F61EEB2B DBAD2731 6534016F"};
343-
344-
345-
void digest_to_hex(const uint8_t digest[SHA1_DIGEST_SIZE], char *output)
346-
{
347-
int i,j;
348-
char *c = output;
349-
350-
for (i = 0; i < SHA1_DIGEST_SIZE/4; i++) {
351-
for (j = 0; j < 4; j++) {
352-
sprintf(c,"%02X", digest[i*4+j]);
353-
c += 2;
354-
}
355-
sprintf(c, " ");
356-
c += 1;
357-
}
358-
*(c - 1) = '\0';
359-
}
360-
361-
int main(int argc, char** argv)
362-
{
363-
int k;
364-
SHA1_CTX context;
365-
uint8_t digest[20];
366-
char output[80];
367-
368-
fprintf(stdout, "verifying SHA-1 implementation... ");
369-
370-
for (k = 0; k < 2; k++){
371-
crypto_SHA1_Init(&context);
372-
crypto_SHA1_Update(&context, (uint8_t*)test_data[k],
373-
strlen(test_data[k]));
374-
crypto_SHA1_Final(&context, digest);
375-
digest_to_hex(digest, output);
376-
377-
if (strcmp(output, test_results[k])) {
378-
fprintf(stdout, "FAIL\n");
379-
fprintf(stderr,"* hash of \"%s\" incorrect:\n", test_data[k]);
380-
fprintf(stderr,"\t%s returned\n", output);
381-
fprintf(stderr,"\t%s is correct\n", test_results[k]);
382-
return (1);
383-
}
384-
}
385-
/* million 'a' vector we feed separately */
386-
crypto_SHA1_Init(&context);
387-
for (k = 0; k < 1000000; k++)
388-
crypto_SHA1_Update(&context, (uint8_t*)"a", 1);
389-
crypto_SHA1_Final(&context, digest);
390-
digest_to_hex(digest, output);
391-
if (strcmp(output, test_results[2])) {
392-
fprintf(stdout, "FAIL\n");
393-
fprintf(stderr,"* hash of \"%s\" incorrect:\n", test_data[2]);
394-
fprintf(stderr,"\t%s returned\n", output);
395-
fprintf(stderr,"\t%s is correct\n", test_results[2]);
396-
return (1);
397-
}
398-
399-
/* success */
400-
fprintf(stdout, "ok\n");
401-
return(0);
402-
}
403-
#endif /* TEST */

tests/test_sha1.c

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/* Tests for Steve Reid's public domain SHA-1 implementation */
2+
/* This file is in the public domain */
3+
4+
/* gcc -o test_sha1 -I./src tests/test_sha1.c src/sha1.c */
5+
6+
#include <stdio.h>
7+
#include <string.h>
8+
9+
#include "sha1.h"
10+
11+
/* Test Vectors (from FIPS PUB 180-1) */
12+
static char *test_data[] = {
13+
"abc",
14+
"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
15+
"A million repetitions of 'a'"};
16+
static char *test_results[] = {
17+
"A9993E36 4706816A BA3E2571 7850C26C 9CD0D89D",
18+
"84983E44 1C3BD26E BAAE4AA1 F95129E5 E54670F1",
19+
"34AA973C D4C4DAA4 F61EEB2B DBAD2731 6534016F"};
20+
21+
22+
static void digest_to_hex(const uint8_t *digest, char *output)
23+
{
24+
int i,j;
25+
char *c = output;
26+
27+
for (i = 0; i < SHA1_DIGEST_SIZE/4; i++) {
28+
for (j = 0; j < 4; j++) {
29+
sprintf(c,"%02X", digest[i*4+j]);
30+
c += 2;
31+
}
32+
sprintf(c, " ");
33+
c += 1;
34+
}
35+
*(c - 1) = '\0';
36+
}
37+
38+
int main(int argc, char** argv)
39+
{
40+
int k;
41+
SHA1_CTX context;
42+
uint8_t digest[20];
43+
char output[80];
44+
45+
fprintf(stdout, "verifying SHA-1 implementation... ");
46+
47+
for (k = 0; k < 2; k++){
48+
crypto_SHA1_Init(&context);
49+
crypto_SHA1_Update(&context, (uint8_t*)test_data[k],
50+
strlen(test_data[k]));
51+
crypto_SHA1_Final(&context, digest);
52+
digest_to_hex(digest, output);
53+
54+
if (strcmp(output, test_results[k])) {
55+
fprintf(stdout, "FAIL\n");
56+
fprintf(stderr,"* hash of \"%s\" incorrect:\n", test_data[k]);
57+
fprintf(stderr,"\t%s returned\n", output);
58+
fprintf(stderr,"\t%s is correct\n", test_results[k]);
59+
return (1);
60+
}
61+
}
62+
/* million 'a' vector we feed separately */
63+
crypto_SHA1_Init(&context);
64+
for (k = 0; k < 1000000; k++)
65+
crypto_SHA1_Update(&context, (uint8_t*)"a", 1);
66+
crypto_SHA1_Final(&context, digest);
67+
digest_to_hex(digest, output);
68+
if (strcmp(output, test_results[2])) {
69+
fprintf(stdout, "FAIL\n");
70+
fprintf(stderr,"* hash of \"%s\" incorrect:\n", test_data[2]);
71+
fprintf(stderr,"\t%s returned\n", output);
72+
fprintf(stderr,"\t%s is correct\n", test_results[2]);
73+
return (1);
74+
}
75+
76+
/* success */
77+
fprintf(stdout, "ok\n");
78+
return(0);
79+
}

0 commit comments

Comments
 (0)