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
214
215
216
217
218
219
220
221
222
|
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
/* test-padding.c: Test padding functionality
Copyright (C) 2007 Stefan Walter
The Gnome Keyring Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
The Gnome Keyring Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with the Gnome Library; see the file COPYING.LIB. If not,
write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.
Author: Stef Walter <stef@memberwebs.com>
*/
#include "config.h"
#include "egg/egg-padding.h"
#include "egg/egg-testing.h"
#include <gcrypt.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
static void
check_padding (EggPadding padding, gsize block, gconstpointer input,
gsize n_input, gconstpointer output, gsize n_output)
{
gpointer result;
gsize n_result;
if (!(padding) (NULL, block, input, n_input, &result, &n_result)) {
g_assert (output == NULL);
return;
}
g_assert (result != NULL);
egg_assert_cmpsize (n_output, ==, n_result);
g_assert (memcmp (output, result, n_output) == 0);
g_free (result);
/* Now make sure it can tell us the right length */
if (!(padding) (NULL, block, input, n_input, NULL, &n_result))
g_assert_not_reached ();
egg_assert_cmpsize (n_output, ==, n_result);
}
static void
test_zero_padding (void)
{
guchar padded[] = { 0x00, 0x00, 0x00, 0x00, 'T', 'E', 'S', 'T' };
gchar raw[] = "TEST";
check_padding (egg_padding_zero_pad, 8, raw, 4, padded, 8);
}
static void
test_zero_padding_no_data (void)
{
guchar padded[] = { };
gchar raw[] = "";
check_padding (egg_padding_zero_pad, 8, raw, 0, padded, 0);
}
static void
test_pkcs1_one_padding (void)
{
guchar padded[] = { 0x00, 0x01, 0xFF, 0x00, 'T', 'E', 'S', 'T' };
gchar raw[] = "TEST";
check_padding (egg_padding_pkcs1_pad_01, 8, raw, 4, padded, 8);
check_padding (egg_padding_pkcs1_unpad_01, 8, padded, 8, raw, 4);
}
static void
test_pkcs1_one_padding_no_data (void)
{
guchar padded[] = { 0x00, 0x01, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00 };
gchar raw[] = "";
check_padding (egg_padding_pkcs1_pad_01, 8, raw, 0, padded, 8);
check_padding (egg_padding_pkcs1_unpad_01, 8, padded, 8, raw, 0);
}
static void
test_pkcs1_two_padding (void)
{
guchar padded[] = { 0x00, 0x02, 0x77, 0x66, 0x55, 0x00, 'T', 'E', };
gchar raw[] = "TE";
guchar *result;
gpointer vesult;
gsize n_result;
check_padding (egg_padding_pkcs1_unpad_02, 8, padded, 8, raw, 2);
/* PKCS#1 02 padding is unpredictable */
if (!egg_padding_pkcs1_pad_02 (NULL, 8, raw, 2, &vesult, &n_result))
g_assert_not_reached ();
result = vesult;
g_assert (result != NULL);
egg_assert_cmpsize (n_result, ==, 8);
g_assert (result[0] == 0x00);
g_assert (result[1] == 0x02);
g_assert (result[2] != 0x00);
g_assert (result[3] != 0x00);
g_assert (result[4] != 0x00);
g_assert (result[5] == 0x00);
g_assert (result[6] == 'T');
g_assert (result[7] == 'E');
}
static void
test_pkcs1_padding_invalid_prefix (void)
{
guchar padded[] = { 0x01, 0x04, 0x04, 0x04 };
check_padding (egg_padding_pkcs1_unpad_01, 4, padded, 4, NULL, 0);
}
static void
test_pkcs1_padding_invalid_type (void)
{
guchar padded[] = { 0x00, 0x03, 0xFF, 0x00, 'T', 'E', 'S', 'T' };
check_padding (egg_padding_pkcs1_unpad_01, 8, padded, 8, NULL, 0);
}
static void
test_pkcs1_padding_invalid_no_zero (void)
{
guchar padded[] = { 0x00, 0x01, 0xFF, 0xFF, 'T', 'E', 'S', 'T' };
check_padding (egg_padding_pkcs1_unpad_01, 8, padded, 8, NULL, 0);
}
static void
test_pkcs1_padding_invalid_length (void)
{
guchar padded[] = { 0x00, 0x01, 0xFF, 0xFF, 'T', 'E', 'S' };
check_padding (egg_padding_pkcs1_unpad_01, 8, padded, 7, NULL, 0);
}
static void
test_pkcs7_padding (void)
{
guchar padded[] = { 'T', 'E', 'S', 'T', 0x04, 0x04, 0x04, 0x04 };
gchar raw[] = "TEST";
check_padding (egg_padding_pkcs7_pad, 8, raw, 4, padded, 8);
check_padding (egg_padding_pkcs7_unpad, 8, padded, 8, raw, 4);
}
static void
test_pkcs7_padding_equal_block (void)
{
guchar padded[] = { 'T', 'E', 'S', 'T', 'T', 'E', 'S', 'T', 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08 };
gchar raw[] = "TESTTEST";
check_padding (egg_padding_pkcs7_pad, 8, raw, 8, padded, 16);
check_padding (egg_padding_pkcs7_unpad, 8, padded, 16, raw, 8);
}
static void
test_pkcs7_padding_zero (void)
{
guchar padded[] = { 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08 };
gchar raw[] = "";
check_padding (egg_padding_pkcs7_pad, 8, raw, 0, padded, 8);
check_padding (egg_padding_pkcs7_unpad, 8, padded, 8, raw, 0);
}
static void
test_pkcs7_padding_invalid_zero (void)
{
guchar padded[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
check_padding (egg_padding_pkcs7_unpad, 8, padded, 8, NULL, 0);
}
static void
test_pkcs7_padding_invalid_too_long (void)
{
guchar padded[] = { 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08 };
check_padding (egg_padding_pkcs7_unpad, 4, padded, 8, NULL, 0);
check_padding (egg_padding_pkcs7_unpad, 4, padded, 4, NULL, 0);
}
static void
test_pkcs7_padding_invalid_different (void)
{
guchar padded[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
check_padding (egg_padding_pkcs7_unpad, 8, padded, 8, NULL, 0);
}
int
main (int argc, char **argv)
{
g_test_init (&argc, &argv, NULL);
g_test_add_func ("/padding/zero_padding", test_zero_padding);
g_test_add_func ("/padding/zero_padding_no_data", test_zero_padding_no_data);
g_test_add_func ("/padding/pkcs1_one_padding", test_pkcs1_one_padding);
g_test_add_func ("/padding/pkcs1_one_padding_no_data", test_pkcs1_one_padding_no_data);
g_test_add_func ("/padding/pkcs1_two_padding", test_pkcs1_two_padding);
g_test_add_func ("/padding/pkcs1_padding_invalid_prefix", test_pkcs1_padding_invalid_prefix);
g_test_add_func ("/padding/pkcs1_padding_invalid_type", test_pkcs1_padding_invalid_type);
g_test_add_func ("/padding/pkcs1_padding_invalid_no_zero", test_pkcs1_padding_invalid_no_zero);
g_test_add_func ("/padding/pkcs1_padding_invalid_length", test_pkcs1_padding_invalid_length);
g_test_add_func ("/padding/pkcs7_padding", test_pkcs7_padding);
g_test_add_func ("/padding/pkcs7_padding_equal_block", test_pkcs7_padding_equal_block);
g_test_add_func ("/padding/pkcs7_padding_zero", test_pkcs7_padding_zero);
g_test_add_func ("/padding/pkcs7_padding_invalid_zero", test_pkcs7_padding_invalid_zero);
g_test_add_func ("/padding/pkcs7_padding_invalid_too_long", test_pkcs7_padding_invalid_too_long);
g_test_add_func ("/padding/pkcs7_padding_invalid_different", test_pkcs7_padding_invalid_different);
return g_test_run ();
}
|