summaryrefslogtreecommitdiff
path: root/testsuite/sha1-test.c
blob: 549363f8988016d590e6efa36bfaec5b889fef42 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include "testutils.h"

#include "nettle-write.h"

/* Test compression only. */
static void
test_sha1_compress(const struct tstring *input,
		   const struct tstring *expected) {
  size_t split;

  ASSERT (input->length % SHA1_BLOCK_SIZE == 0);
  ASSERT (expected->length == SHA1_DIGEST_SIZE);

  for (split = 0; split <= input->length; split += SHA1_BLOCK_SIZE)
    {
      struct sha1_ctx ctx;
      uint8_t digest[SHA1_DIGEST_SIZE];
      sha1_init (&ctx);
      sha1_update (&ctx, split, input->data);
      sha1_update (&ctx, input->length - split, input->data + split);

      _nettle_write_be32 (SHA1_DIGEST_SIZE, digest, ctx.state);
      if (!MEMEQ (SHA1_DIGEST_SIZE, digest, expected->data)) {
	fprintf (stderr, "sha1_compress failed: length %u, split %u \nInput:",
		 (unsigned) input->length, (unsigned) split);
	tstring_print_hex (input);
	fprintf (stderr, "\nOutput: ");
	print_hex (SHA1_DIGEST_SIZE, digest);
	fprintf(stdout, "\nExpected:\n");
	tstring_print_hex (expected);
	fprintf (stderr, "\n");
	abort ();
      }
    }
}

void
test_main(void)
{
  /* Initial state */
  test_sha1_compress (SDATA(""),
		      SHEX("67452301efcdab89 98badcfe10325476"
			   "c3d2e1f0"));

  /* Single block compressed */
  test_sha1_compress (SDATA("0123456789abcdefghijklmnopqrstuv"
			    "wxyzABCDEFGHIJKLMNOPQRSTUVWXYZZY"),
		      SHEX("005cf6fb02d9a17e f77d0b8eac9da60c"
			   "08ecaf1c"));
  /* Two blocks compressed */
  test_sha1_compress (SDATA("0123456789abcdefghijklmnopqrstuv"
			    "wxyzABCDEFGHIJKLMNOPQRSTUVWXYZZY"
			    "ABCDEFGHIJKLMNOPQRSTUVWXYZ012345"
			    "6789abcdefghijklmnopqrstuvwxyzzy"),
		      SHEX("3e0dd3db30fc4e45 c17a97f0c431f07b"
			   "6b4a5cc5"));

  test_hash(&nettle_sha1, SDATA(""),
	    SHEX("DA39A3EE5E6B4B0D 3255BFEF95601890 AFD80709")); 

  test_hash(&nettle_sha1, SDATA("a"),
	    SHEX("86F7E437FAA5A7FC E15D1DDCB9EAEAEA 377667B8")); 

  test_hash(&nettle_sha1, SDATA("abc"),
	    SHEX("A9993E364706816A BA3E25717850C26C 9CD0D89D"));
  
  test_hash(&nettle_sha1, SDATA("abcdefghijklmnopqrstuvwxyz"),
	    SHEX("32D10C7B8CF96570 CA04CE37F2A19D84 240D3A89"));
  
  test_hash(&nettle_sha1, SDATA("message digest"),
	    SHEX("C12252CEDA8BE899 4D5FA0290A47231C 1D16AAE3")); 

  test_hash(&nettle_sha1,
	    SDATA("ABCDEFGHIJKLMNOPQRSTUVWXYZ"
		  "abcdefghijklmnopqrstuvwxyz0123456789"),
	    SHEX("761C457BF73B14D2 7E9E9265C46F4B4D DA11F940"));
  
  test_hash(&nettle_sha1,
	    SDATA("1234567890123456789012345678901234567890"
		  "1234567890123456789012345678901234567890"),
	    SHEX("50ABF5706A150990 A08B2C5EA40FA0E5 85554732"));

  /* Additional test vector, from Daniel Kahn Gillmor */
  test_hash(&nettle_sha1, SDATA("38"),
	    SHEX("5b384ce32d8cdef02bc3a139d4cac0a22bb029e8"));
}

/* These are intermediate values for the single sha1_compress call
   that results from the first testcase, SHA1(""). Each row is the
   values for A, B, C, D, E after the i:th round. The row i = -1 gives
   the initial values, and i = 99 gives the output values.

      i         A        B        C        D        E
     -1: 67452301 efcdab89 98badcfe 10325476 c3d2e1f0
      0: 67452301 7bf36ae2 98badcfe 10325476 1fb498b3
      1: 59d148c0 7bf36ae2 98badcfe 5d43e370 1fb498b3
     15: 40182905 4544b22e a13017ac ab703832 d8fd6547
     16: 50060a41 4544b22e a13017ac  6bf9173 d8fd6547
     17: 50060a41 4544b22e 28a9520e  6bf9173 f63f5951
     18: 50060a41  b3088dd 28a9520e c1afe45c f63f5951
     19: e758e8da  b3088dd 8a2a5483 c1afe45c f63f5951
     20: e758e8da 42cc2237 8a2a5483 c1afe45c 90eb9850
     21: b9d63a36 42cc2237 8a2a5483 7dbb787d 90eb9850
     38:  e47bc31 62273351 b201788b 413c1d9a 2aeeae62
     39: 9bdbdd71 62273351 ec805e22 413c1d9a 2aeeae62
     40: 9bdbdd71 5889ccd4 ec805e22 413c1d9a 95aa398b
     41: 66f6f75c 5889ccd4 ec805e22 5e28e858 95aa398b
     58: 2164303a 982bcbca e1afab22 c5a3382e af9292fa
     59: 9b9d2913 982bcbca b86beac8 c5a3382e af9292fa
     60: 9b9d2913 a60af2f2 b86beac8 c5a3382e d37db937
     61: e6e74a44 a60af2f2 b86beac8 85b9d227 d37db937
     78: c57a6345 6e9d9f84 666b8bc6 852dc41a ec052519
     79: 72f480ed 6e9d9f84 999ae2f1 852dc41a ec052519
     99: da39a3ee 5e6b4b0d 3255bfef 95601890 afd80709
     
*/