summaryrefslogtreecommitdiffstats
path: root/web/server/h2o/libh2o/deps/picotls/deps/cifra/src/testsha.h
blob: 70075f016a8a83684908021f8b8cc38b863c589c (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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/*
 * cifra - embedded cryptography library
 * Written in 2014 by Joseph Birr-Pixton <jpixton@gmail.com>
 *
 * To the extent possible under law, the author(s) have dedicated all
 * copyright and related and neighboring rights to this software to the
 * public domain worldwide. This software is distributed without any
 * warranty.
 *
 * You should have received a copy of the CC0 Public Domain Dedication
 * along with this software. If not, see
 * <http://creativecommons.org/publicdomain/zero/1.0/>.
 */

#ifndef TESTSHA_H
#define TESTSHA_H

#include "hmac.h"

/* Common functions for testing hash functions.
 * You shouldn't use this file. */

static void vector(const cf_chash *hash,
                   const void *vmsg, size_t nmsg,
                   const char *expect, size_t nexpect)
{
  uint8_t digest[CF_MAXHASH];
  const uint8_t *msg = vmsg;
  size_t orig_nmsg = nmsg;

  cf_chash_ctx ctx;
  hash->init(&ctx);

  /* Input in carefully chosen chunk sizes to exercise blockwise code. */
  if (nmsg)
  {
    hash->update(&ctx, msg, 1);
    nmsg--;
    msg++;
  }

  hash->update(&ctx, msg, nmsg);
  hash->digest(&ctx, digest);
  TEST_CHECK(nexpect == hash->hashsz);
  TEST_CHECK(memcmp(digest, expect, nexpect) == 0);

  /* Now try with other arrangements. */
  msg = vmsg;
  nmsg = orig_nmsg;

  hash->init(&ctx);
  if (nmsg >= hash->blocksz)
  {
    hash->update(&ctx, msg, hash->blocksz - 1);
    nmsg -= hash->blocksz - 1;
    msg += hash->blocksz - 1;
  }

  hash->update(&ctx, msg, nmsg);
  hash->digest(&ctx, digest);
  TEST_CHECK(memcmp(digest, expect, nexpect) == 0);
}

/* These are shared between RFC2202 and RFC4231. */
static inline void hmac_test(const cf_chash *hash,
                             const void *hi_there,
                             const void *jefe,
                             const void *aa_dd,
                             const void *counter_key)
{
  uint8_t sig[CF_MAXHASH];
  uint8_t key[25], message[50];

  /* Key: 0x0b * 20
   * Message: "Hi There"
   */
  memset(key, 0x0b, 20);
  memcpy(message, "Hi There", 8);
  cf_hmac(key, 20, message, 8, sig, hash);

  TEST_CHECK(memcmp(sig, hi_there, hash->hashsz) == 0);

  /* Key: "Jefe"
   * Message: "what do ya want for nothing?"
   */
  memcpy(key, "Jefe", 4);
  memcpy(message, "what do ya want for nothing?", 28);
  cf_hmac(key, 4, message, 28, sig, hash);
  TEST_CHECK(memcmp(sig, jefe, hash->hashsz) == 0);

  /* Key: 0xaa * 20
   * Message: 0xdd * 50
   */
  memset(key, 0xaa, 20);
  memset(message, 0xdd, 50);
  cf_hmac(key, 20, message, 50, sig, hash);
  TEST_CHECK(memcmp(sig, aa_dd, hash->hashsz) == 0);

  /* Key: 0x01..0x19
   * Message: 0xcd * 50
   */
  for (uint8_t i = 1; i < 26; i++)
    key[i - 1] = i;
  memset(message, 0xcd, 50);
  cf_hmac(key, 25, message, 50, sig, hash);
  TEST_CHECK(memcmp(sig, counter_key, hash->hashsz) == 0);
}

/* These are specific to RFC4231. */
static inline void hmac_test_sha2(const cf_chash *hash,
                                  const char *long_key,
                                  const char *long_message)
{
  uint8_t sig[CF_MAXHASH];
  uint8_t key[131], message[152];

  /* Key: 0xaa * 131
   * Message: "Test Using Larger Than Block-Size Key - Hash Key First"
   */
  memset(key, 0xaa, 131);
  memcpy(message, "Test Using Larger Than Block-Size Key - Hash Key First", 54);
  cf_hmac(key, 131, message, 54, sig, hash);
  TEST_CHECK(memcmp(sig, long_key, hash->hashsz) == 0);

  /* Key: 0xaa * 131
   * Message: "This is a test using a larger than block-size key and a larger than block-size data. The key needs to be hashed before being used by the HMAC algorithm."
   */
  memset(key, 0xaa, 131);
  memcpy(message, "This is a test using a larger than block-size key and a larger than block-size data. The key needs to be hashed before being used by the HMAC algorithm.", 152);
  cf_hmac(key, 131, message, 152, sig, hash);
  TEST_CHECK(memcmp(sig, long_message, hash->hashsz) == 0);
}

/* This is as hmac_test_sha2, except the sizes are specific to
 * a 512-bit block.  This is from RFC2202. */
static inline void hmac_test_sha1(const cf_chash *hash,
                                  const char *long_key,
                                  const char *long_message)
{
  uint8_t sig[CF_MAXHASH];
  uint8_t key[80], message[73];

  /* Key: 0xaa * 80
   * Message: "Test Using Larger Than Block-Size Key - Hash Key First"
   */
  memset(key, 0xaa, 80);
  memcpy(message, "Test Using Larger Than Block-Size Key - Hash Key First", 54);
  cf_hmac(key, 80, message, 54, sig, hash);
  TEST_CHECK(memcmp(sig, long_key, hash->hashsz) == 0);

  /* Key: 0xaa * 80
   * Message: "Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data"
   */
  memset(key, 0xaa, 80);
  memcpy(message, "Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data", 73);
  cf_hmac(key, 80, message, 73, sig, hash);
  TEST_CHECK(memcmp(sig, long_message, hash->hashsz) == 0);
}

typedef void (*final_fn)(void *ctx, uint8_t *out);

/* Check incremental interface works, and final function likewise. */
static void vector_abc_final(const cf_chash *hash, const void *vfinal_fn,
                             const void *expect, size_t nexpect)
{
  uint8_t digest[CF_MAXHASH];

  final_fn final = vfinal_fn;
  cf_chash_ctx ctx;
  hash->init(&ctx);
  hash->update(&ctx, "a", 1);
  hash->digest(&ctx, digest);
  hash->update(&ctx, "b", 1);
  hash->digest(&ctx, digest);
  hash->update(&ctx, "c", 1);
  final(&ctx, digest);

  TEST_CHECK(hash->hashsz == nexpect);
  TEST_CHECK(memcmp(expect, digest, nexpect) == 0);
}

/* Check length-checking vectors work (generated by programs in ../extra_vecs) */
static inline void vector_length(const cf_chash *h,
                                 size_t max,
                                 const void *expect, size_t nexpect)
{
  cf_chash_ctx outer, inner;
  uint8_t digest[CF_MAXHASH];

  h->init(&outer);

  for (size_t n = 0; n < max; n++)
  {
    h->init(&inner);
    
    for (size_t i = 0; i < n; i++)
    {
      uint8_t byte = (uint8_t) n & 0xff;
      h->update(&inner, &byte, 1);
    }

    h->digest(&inner, digest);

    h->update(&outer, digest, h->hashsz);
  }

  h->digest(&outer, digest);

  TEST_CHECK(h->hashsz == nexpect);
  TEST_CHECK(memcmp(expect, digest, nexpect) == 0);
}

#endif