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
|
#include <config.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <utils.h>
#include <stdlib.h>
#include <gnutls/gnutls.h>
#include <gnutls/crypto.h>
#include <gnutls/self-test.h>
#ifndef HAVE_LIBNETTLE
int main(int argc, char **argv)
{
exit(77);
}
#else
# include <nettle/aes.h>
# include <nettle/cbc.h>
# include <nettle/gcm.h>
/* this tests whether the API to override ciphers works sanely,
* when GNUTLS_E_NEED_FALLBACK is used.
*/
static void tls_log_func(int level, const char *str)
{
fprintf(stderr, "<%d>| %s", level, str);
}
#ifndef ENABLE_SELF_CHECKS
# define AVOID_INTERNALS
# include "../../lib/crypto-selftests.c"
#endif
struct myaes_ctx {
unsigned char iv[16];
};
static int
myaes_init(gnutls_cipher_algorithm_t algorithm, void **_ctx, int enc)
{
return GNUTLS_E_NEED_FALLBACK;
}
static int
myaes_setkey(void *_ctx, const void *userkey, size_t keysize)
{
abort();
}
static int myaes_setiv(void *_ctx, const void *iv, size_t iv_size)
{
abort();
}
static int
myaes_encrypt(void *_ctx, const void *src, size_t src_size,
void *dst, size_t dst_size)
{
abort();
}
static int
myaes_decrypt(void *_ctx, const void *src, size_t src_size,
void *dst, size_t dst_size)
{
abort();
}
static void myaes_deinit(void *_ctx)
{
abort();
}
/* AES-GCM */
struct myaes_gcm_ctx {
char xx[32];
};
static int
myaes_gcm_init(gnutls_cipher_algorithm_t algorithm, void **_ctx, int enc)
{
return GNUTLS_E_NEED_FALLBACK;
}
static int
myaes_gcm_setkey(void *_ctx, const void *userkey, size_t keysize)
{
abort();
}
static void myaes_gcm_deinit(void *_ctx)
{
abort();
}
static int
myaes_gcm_encrypt(void *_ctx,
const void *nonce, size_t nonce_size,
const void *auth, size_t auth_size,
size_t tag_size,
const void *plain, size_t plain_size,
void *encr, size_t encr_size)
{
abort();
}
static int
myaes_gcm_decrypt(void *_ctx,
const void *nonce, size_t nonce_size,
const void *auth, size_t auth_size,
size_t tag_size,
const void *encr, size_t encr_size,
void *plain, size_t plain_size)
{
abort();
}
int main(int argc, char **argv)
{
int ret;
gnutls_global_set_log_function(tls_log_func);
if (argc > 1)
gnutls_global_set_log_level(4711);
ret = gnutls_crypto_register_cipher(GNUTLS_CIPHER_AES_128_CBC, 1,
myaes_init,
myaes_setkey,
myaes_setiv,
myaes_encrypt,
myaes_decrypt,
myaes_deinit);
if (ret < 0) {
fprintf(stderr, "%d: cannot register cipher\n", __LINE__);
exit(1);
}
ret = gnutls_crypto_register_aead_cipher(GNUTLS_CIPHER_AES_128_GCM, 1,
myaes_gcm_init,
myaes_gcm_setkey,
myaes_gcm_encrypt,
myaes_gcm_decrypt,
myaes_gcm_deinit);
if (ret < 0) {
fprintf(stderr, "%d: cannot register cipher\n", __LINE__);
exit(1);
}
global_init();
if (gnutls_cipher_self_test(1, 0) < 0)
return 1;
gnutls_global_deinit();
return 0;
}
#endif
|