testutils.h 3.22 KB
Newer Older
Niels Möller's avatar
Niels Möller committed
1 2 3
#ifndef NETTLE_TESTUTILS_H_INCLUDED
#define NETTLE_TESTUTILS_H_INCLUDED

4 5 6 7
#if HAVE_CONFIG_H
# include "config.h"
#endif

8
#include "nettle-types.h"
Niels Möller's avatar
Niels Möller committed
9

10 11
#include <string.h>
#include <stdlib.h>
12 13 14
#include <stdio.h>

#if HAVE_LIBGMP
15 16 17 18
# include "bignum.h"
#endif

#if WITH_PUBLIC_KEY
19
# include "rsa.h"
Niels Möller's avatar
Niels Möller committed
20
# include "dsa.h"
21
#endif
22 23 24

#include "nettle-meta.h"

Niels Möller's avatar
Niels Möller committed
25 26 27 28
#ifdef __cplusplus
extern "C" {
#endif

29 30 31
void *
xalloc(size_t size);

Niels Möller's avatar
Niels Möller committed
32
/* Decodes a NUL-terminated hex string. */
33 34 35 36

unsigned
decode_hex_length(const char *hex);

Niels Möller's avatar
Niels Möller committed
37 38 39 40 41 42 43
int
decode_hex(uint8_t *dst, const char *hex);

/* Allocates space */
const uint8_t *
decode_hex_dup(const char *hex);

44
void
Niels Möller's avatar
Niels Möller committed
45
print_hex(unsigned length, const uint8_t *data);
46

Niels Möller's avatar
Niels Möller committed
47 48 49 50 51 52
/* The main program */
int
test_main(void);

extern int verbose;

53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
void
test_cipher(const struct nettle_cipher *cipher,
	    unsigned key_length,
	    const uint8_t *key,
	    unsigned length,
	    const uint8_t *cleartext,
	    const uint8_t *ciphertext);

void
test_cipher_cbc(const struct nettle_cipher *cipher,
		unsigned key_length,
		const uint8_t *key,
		unsigned length,
		const uint8_t *cleartext,
		const uint8_t *ciphertext,
		const uint8_t *iv);

70 71 72 73 74 75 76 77 78
void
test_cipher_ctr(const struct nettle_cipher *cipher,
		unsigned key_length,
		const uint8_t *key,
		unsigned length,
		const uint8_t *cleartext,
		const uint8_t *ciphertext,
		const uint8_t *iv);

79 80 81 82 83 84 85 86
void
test_cipher_stream(const struct nettle_cipher *cipher,
		   unsigned key_length,
		   const uint8_t *key,
		   unsigned length,
		   const uint8_t *cleartext,
		   const uint8_t *ciphertext);

87 88 89 90 91
void
test_hash(const struct nettle_hash *hash,
	  unsigned length,
	  const uint8_t *data,
	  const uint8_t *digest);
Niels Möller's avatar
Niels Möller committed
92

93 94 95 96 97 98
void
test_hash_large(const struct nettle_hash *hash,
		unsigned count, unsigned length,
		uint8_t c,
		const uint8_t *digest);

Niels Möller's avatar
Niels Möller committed
99 100 101 102 103 104
void
test_armor(const struct nettle_armor *armor,
           unsigned data_length,
           const uint8_t *data,
           const uint8_t *ascii);

105
#if WITH_PUBLIC_KEY
106 107 108 109
void
test_rsa_set_key_1(struct rsa_public_key *pub,
		   struct rsa_private_key *key);

110 111 112 113 114 115 116 117 118
void
test_rsa_md5(struct rsa_public_key *pub,
	     struct rsa_private_key *key,
	     mpz_t expected);

void
test_rsa_sha1(struct rsa_public_key *pub,
	      struct rsa_private_key *key,
	      mpz_t expected);
Niels Möller's avatar
Niels Möller committed
119

120 121 122 123 124
void
test_rsa_sha256(struct rsa_public_key *pub,
		struct rsa_private_key *key,
		mpz_t expected);

Niels Möller's avatar
Niels Möller committed
125 126 127 128
void
test_rsa_key(struct rsa_public_key *pub,
	     struct rsa_private_key *key);

Niels Möller's avatar
Niels Möller committed
129
void
130 131
test_dsa(const struct dsa_public_key *pub,
	 const struct dsa_private_key *key);
Niels Möller's avatar
Niels Möller committed
132

133 134 135 136
void
test_dsa_key(struct dsa_public_key *pub,
	     struct dsa_private_key *key);

137
#endif /* WITH_PUBLIC_KEY */
138

Niels Möller's avatar
Niels Möller committed
139 140 141 142
#ifdef __cplusplus
}
#endif

143 144 145 146
#define H2(d, s) decode_hex((d), (s))
#define H(x) decode_hex_dup(x)
#define HL(x) decode_hex_length(x), decode_hex_dup(x)

147 148 149
/* LDATA needs to handle NUL characters. */
#define LLENGTH(x) (sizeof(x) - 1)
#define LDATA(x) (sizeof(x) - 1), x
Niels Möller's avatar
Niels Möller committed
150
#define LDUP(x) strlen(x), strdup(x)
151

152
#define MEMEQ(length, a, b) (!memcmp((a), (b), (length)))
Niels Möller's avatar
Niels Möller committed
153 154 155
#define MEMEQH(length, a, b) \
((length) == decode_hex_length((b)) \
 && !memcmp((a), decode_hex_dup((b)), (length)))
156 157 158 159

#define FAIL() abort()
#define SKIP() exit(77)
#define SUCCESS() return EXIT_SUCCESS
Niels Möller's avatar
Niels Möller committed
160

Niels Möller's avatar
Niels Möller committed
161 162
#define ASSERT(x) do { if (!(x)) FAIL(); } while(0)

Niels Möller's avatar
Niels Möller committed
163
#endif /* NETTLE_TESTUTILS_H_INCLUDED */