macros.m4 5.15 KB
Newer Older
Niels Möller's avatar
Niels Möller committed
1
2
3
4
5
6
7
8
9
m4_dnl LSH testsuite driver
m4_dnl (progn (modify-syntax-entry ? "(") (modify-syntax-entry ? ")"))
m4_dnl (progn (modify-syntax-entry 187 "(") (modify-syntax-entry 170 ")"))
m4_dnl (progn (modify-syntax-entry ?{ "(}") (modify-syntax-entry ?} "){"))
m4_changequote(, )
m4_changecom(/*, */)
m4_define(TS_DEFINE, m4_defn(m4_define))

TS_DEFINE(TS_WRITE, fputs("$1", stderr);)
10
TS_DEFINE(TS_MESSAGE, TS_WRITE($1 ... ))
11
12
TS_DEFINE(TS_OK, TS_WRITE(ok.\n))
TS_DEFINE(TS_FAIL, { TS_WRITE(failed.\n); exit(1); })
Niels Möller's avatar
Niels Möller committed
13

Niels Möller's avatar
Niels Möller committed
14
15
TS_DEFINE(TS_STRING,
m4_ifelse(m4_index($1, "), 0,
16
17
  ssh_format("%lz", $1), simple_decode_hex("m4_translit($1, 0-9a-zA-Z
# 	, 0-9a-zA-Z)")) )
Niels Möller's avatar
Niels Möller committed
18

19
20
21
22
TS_DEFINE(TS_SEXP, string_to_sexp(SEXP_TRANSPORT, TS_STRING($1), 1))

m4_dnl TS_DEFINE(TS_SEXP_L, sexp_l($#, $@, -1))
m4_dnl TS_DEFINE(TS_SEXP_A, sexp_a(TS_STRING($1)))
Niels Möller's avatar
Niels Möller committed
23
24
25

TS_DEFINE(TS_TEST_STRING_EQ,

Niels Möller's avatar
Niels Möller committed
26
27
28
29
30
  {
    struct lsh_string *a, *b;
    TS_MESSAGE($1)
    a = $2;
    b = $3;
31
    if (!lsh_string_eq(a, b))
Niels Möller's avatar
Niels Möller committed
32
33
34
35
36
37
38
39
40
      TS_FAIL
    TS_OK
    lsh_string_free(a);
    lsh_string_free(b);
  }
)

m4_dnl TS_TEST_HASH(name, algorithm, data, digest)
TS_DEFINE(TS_TEST_HASH,
41
42
43
44
45
46
47
48
49
50
51
52
53
  TS_TEST_STRING_EQ($1, hash_string($2, TS_STRING($3), 1), TS_STRING($4)))

m4_dnl TS_TEST_HMAC(name, algorithm, key, data, digest)
TS_DEFINE(TS_TEST_HMAC, 
  {
    struct mac_algorithm *hmac = make_hmac_algorithm($2);
    struct lsh_string *key = TS_STRING($3);

    TS_TEST_STRING_EQ($1, mac_string(hmac, key, 1,
                                       TS_STRING($4), 1),
                      TS_STRING($5));
  }
)
Niels Möller's avatar
Niels Möller committed
54
55
56
57
58
59

m4_dnl TS_TEST_CRYPTO(name, algorithm, key, clear, cipher)
TS_DEFINE(TS_TEST_CRYPTO, 
  {
    struct crypto_algorithm *algorithm = $2;
    struct lsh_string *key = TS_STRING($3);
60
61
62
    struct lsh_string *plain = TS_STRING($4);
    struct lsh_string *cipher = TS_STRING($5);
    struct crypto_instance *c;
Niels Möller's avatar
Niels Möller committed
63
64
65
66

    assert(key->length == algorithm->key_size);
    assert(!algorithm->iv_size);

67
    c = MAKE_ENCRYPT(algorithm, key->data, NULL);
Niels Möller's avatar
Niels Möller committed
68
    TS_TEST_STRING_EQ(Encrypting with $1,
69
70
71
72
	  	      crypt_string(c, plain, 0),
		      lsh_string_dup(cipher))
    KILL(c);
    c = MAKE_DECRYPT(algorithm, key->data, NULL);
Niels Möller's avatar
Niels Möller committed
73
    TS_TEST_STRING_EQ(Decrypting with $1,
74
75
76
77
         	      crypt_string(c, cipher, 0),
		      plain)
    KILL(c);
    
Niels Möller's avatar
Niels Möller committed
78
    lsh_string_free(key);
79
    lsh_string_free(cipher);
Niels Möller's avatar
Niels Möller committed
80
81
82
  }
)    

83
84
85
86
m4_dnl TS_TEST_VERIFY(name, key, msg, signature)
TS_DEFINE(TS_TEST_VERIFY,

{
87
  struct alist *algorithms = all_signature_algorithms(make_bad_random());
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
  struct sexp *key = TS_SEXP($2);
  struct lsh_string *msg = TS_STRING($3);
  struct sexp *sign = TS_SEXP($4);
  struct verifier *v = spki_make_verifier(algorithms, key);

  TS_MESSAGE($1);
  if (!v)
    /* Invalid key. */
    TS_FAIL; 

  if (!VERIFY_SPKI(v, msg->length, msg->data, sign))
    /* Unexpected verification failure. */
    TS_FAIL;

  /* Modify message slightly. */
  assert(msg->length > 10);

  msg->data[5] ^= 0x40;

  if (VERIFY_SPKI(v, msg->length, msg->data, sign))
    /* Unexpected verification success. */
    TS_FAIL;

  TS_OK;
})

m4_dnl TS_TEST_SIGN(name, key, msg [, signature])
TS_DEFINE(TS_TEST_SIGN,

{
  struct alist *algorithms = all_signature_algorithms(make_reasonably_random());
  struct sexp *key = TS_SEXP($2);
  struct lsh_string *msg = TS_STRING($3);
  struct sexp *sign;
  struct signer *s = spki_make_signer(algorithms, key, NULL);
  struct verifier *v;

  TS_MESSAGE($1);
  if (!s)
    /* Invalid key. */
    TS_FAIL; 

  sign = SIGN_SPKI(s, msg->length, msg->data);

  m4_ifelse($4,,,
  
  {
    struct lsh_string *s2 = TS_STRING($4);
    if (!lsh_string_eq(s2, sexp_format(sign, SEXP_CANONICAL, 0)))
      TS_FAIL
  }
  )
  v = spki_make_verifier(algorithms, SIGNER_PUBLIC(s));
  if (!v)
    /* Can't create verifier */
    TS_FAIL

  if (!VERIFY_SPKI(v, msg->length, msg->data, sign))
    /* Unexpected verification failure. */
    TS_FAIL;

  /* Modify message slightly. */
  assert(msg->length > 10);

  msg->data[5] ^= 0x40;

  if (VERIFY_SPKI(v, msg->length, msg->data, sign))
    /* Unexpected verification success. */
    TS_FAIL;

  TS_OK
})
Niels Möller's avatar
Niels Möller committed
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

m4_dnl TS_TAG_GRANT(msg, tag-set, access)
TS_DEFINE(TS_TAG_GRANT,

{
  struct spki_tag *tag = spki_sexp_to_tag(TS_SEXP($2), 17);
  struct sexp *access = TS_SEXP($3);
  TS_MESSAGE(Granting access $1)
  assert(tag);
  assert(access);
  
  if (SPKI_TAG_MATCH(tag, access))
    TS_OK
  else
    TS_FAIL
  KILL(tag);
  KILL(access);
})

m4_dnl TS_TAG_DENY(msg, tag-set, access)
TS_DEFINE(TS_TAG_DENY,

{
  struct spki_tag *tag = spki_sexp_to_tag(TS_SEXP($2), 17);
  struct sexp *access = TS_SEXP($3);
  TS_MESSAGE(Denying access $1)
  assert(tag);
  assert(access);
  
  if (!SPKI_TAG_MATCH(tag, access))
    TS_OK
  else
    TS_FAIL
  KILL(tag);
  KILL(access);
})



Niels Möller's avatar
Niels Möller committed
199
200
m4_divert(1)
  return 0;
Niels Möller's avatar
Niels Möller committed
201
202
}
m4_divert
Niels Möller's avatar
Niels Möller committed
203
204
205
206

m4_dnl C code
#include "lsh.h"

207
#include "algorithms.h"
Niels Möller's avatar
Niels Möller committed
208
209
210
#include "crypto.h"
#include "digits.h"
#include "format.h"
211
#include "randomness.h"
Niels Möller's avatar
Niels Möller committed
212
213
#include "sexp.h"
#include "spki.h"
214
#include "werror.h"
Niels Möller's avatar
Niels Möller committed
215
216
217
218
219
#include "xalloc.h"

#include <assert.h>
#include <stdio.h>

220
int main(int argc, char **argv)
Niels Möller's avatar
Niels Möller committed
221
{
222
  argp_parse(&werror_argp, argc, argv, 0, NULL, NULL);