ffi-hydrogen 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +13 -0
- data/.rspec +3 -0
- data/.rubocop.yml +30 -0
- data/.travis.yml +10 -0
- data/Gemfile +6 -0
- data/LICENSE.txt +21 -0
- data/README.md +72 -0
- data/Rakefile +46 -0
- data/bench/both.rb +86 -0
- data/bench/encode.rb +57 -0
- data/bench/encrypt.rb +80 -0
- data/bench/init.rb +5 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/ffi-hydrogen.gemspec +31 -0
- data/lib/ffi/hydrogen.rb +216 -0
- data/vendor/.clang-format +2 -0
- data/vendor/.gitignore +3 -0
- data/vendor/README.md +2 -0
- data/vendor/libhydrogen/.clang-format +95 -0
- data/vendor/libhydrogen/.gitignore +32 -0
- data/vendor/libhydrogen/.travis.yml +22 -0
- data/vendor/libhydrogen/LICENSE +18 -0
- data/vendor/libhydrogen/Makefile +61 -0
- data/vendor/libhydrogen/Makefile.arduino +51 -0
- data/vendor/libhydrogen/README.md +29 -0
- data/vendor/libhydrogen/hydrogen.c +18 -0
- data/vendor/libhydrogen/hydrogen.h +317 -0
- data/vendor/libhydrogen/impl/common.h +316 -0
- data/vendor/libhydrogen/impl/core.h +220 -0
- data/vendor/libhydrogen/impl/gimli-core/portable.h +39 -0
- data/vendor/libhydrogen/impl/gimli-core/sse2.h +97 -0
- data/vendor/libhydrogen/impl/gimli-core.h +25 -0
- data/vendor/libhydrogen/impl/hash.h +138 -0
- data/vendor/libhydrogen/impl/hydrogen_p.h +83 -0
- data/vendor/libhydrogen/impl/kdf.h +20 -0
- data/vendor/libhydrogen/impl/kx.h +441 -0
- data/vendor/libhydrogen/impl/pwhash.h +281 -0
- data/vendor/libhydrogen/impl/random.h +376 -0
- data/vendor/libhydrogen/impl/secretbox.h +236 -0
- data/vendor/libhydrogen/impl/sign.h +207 -0
- data/vendor/libhydrogen/impl/x25519.h +383 -0
- data/vendor/libhydrogen/library.properties +10 -0
- data/vendor/libhydrogen/logo.png +0 -0
- data/vendor/libhydrogen/tests/tests.c +431 -0
- data/vendor/main.c +140 -0
- data/vendor/stringencoders/.gitignore +25 -0
- data/vendor/stringencoders/.travis.yml +13 -0
- data/vendor/stringencoders/AUTHORS +1 -0
- data/vendor/stringencoders/COPYING +2 -0
- data/vendor/stringencoders/ChangeLog +170 -0
- data/vendor/stringencoders/Doxyfile +276 -0
- data/vendor/stringencoders/INSTALL +119 -0
- data/vendor/stringencoders/LICENSE +22 -0
- data/vendor/stringencoders/Makefile.am +3 -0
- data/vendor/stringencoders/NEWS +3 -0
- data/vendor/stringencoders/README +2 -0
- data/vendor/stringencoders/README.md +32 -0
- data/vendor/stringencoders/bootstrap.sh +3 -0
- data/vendor/stringencoders/configure-gcc-hardened.sh +16 -0
- data/vendor/stringencoders/configure.ac +44 -0
- data/vendor/stringencoders/doxy/footer.html +34 -0
- data/vendor/stringencoders/doxy/header.html +85 -0
- data/vendor/stringencoders/indent.sh +9 -0
- data/vendor/stringencoders/javascript/base64-speed.html +43 -0
- data/vendor/stringencoders/javascript/base64-test.html +209 -0
- data/vendor/stringencoders/javascript/base64.html +18 -0
- data/vendor/stringencoders/javascript/base64.js +176 -0
- data/vendor/stringencoders/javascript/qunit.css +119 -0
- data/vendor/stringencoders/javascript/qunit.js +1062 -0
- data/vendor/stringencoders/javascript/urlparse-test.html +367 -0
- data/vendor/stringencoders/javascript/urlparse.js +328 -0
- data/vendor/stringencoders/make-ci.sh +13 -0
- data/vendor/stringencoders/makerelease.sh +16 -0
- data/vendor/stringencoders/python/b85.py +176 -0
- data/vendor/stringencoders/src/Makefile.am +134 -0
- data/vendor/stringencoders/src/arraytoc.c +85 -0
- data/vendor/stringencoders/src/arraytoc.h +43 -0
- data/vendor/stringencoders/src/extern_c_begin.h +3 -0
- data/vendor/stringencoders/src/extern_c_end.h +3 -0
- data/vendor/stringencoders/src/html_named_entities_generator.py +203 -0
- data/vendor/stringencoders/src/modp_ascii.c +159 -0
- data/vendor/stringencoders/src/modp_ascii.h +162 -0
- data/vendor/stringencoders/src/modp_ascii_data.h +84 -0
- data/vendor/stringencoders/src/modp_ascii_gen.c +55 -0
- data/vendor/stringencoders/src/modp_b16.c +125 -0
- data/vendor/stringencoders/src/modp_b16.h +148 -0
- data/vendor/stringencoders/src/modp_b16_data.h +104 -0
- data/vendor/stringencoders/src/modp_b16_gen.c +65 -0
- data/vendor/stringencoders/src/modp_b2.c +69 -0
- data/vendor/stringencoders/src/modp_b2.h +130 -0
- data/vendor/stringencoders/src/modp_b2_data.h +44 -0
- data/vendor/stringencoders/src/modp_b2_gen.c +36 -0
- data/vendor/stringencoders/src/modp_b36.c +108 -0
- data/vendor/stringencoders/src/modp_b36.h +170 -0
- data/vendor/stringencoders/src/modp_b64.c +254 -0
- data/vendor/stringencoders/src/modp_b64.h +236 -0
- data/vendor/stringencoders/src/modp_b64_data.h +477 -0
- data/vendor/stringencoders/src/modp_b64_gen.c +168 -0
- data/vendor/stringencoders/src/modp_b64r.c +254 -0
- data/vendor/stringencoders/src/modp_b64r.h +242 -0
- data/vendor/stringencoders/src/modp_b64r_data.h +477 -0
- data/vendor/stringencoders/src/modp_b64w.c +254 -0
- data/vendor/stringencoders/src/modp_b64w.h +231 -0
- data/vendor/stringencoders/src/modp_b64w_data.h +477 -0
- data/vendor/stringencoders/src/modp_b85.c +109 -0
- data/vendor/stringencoders/src/modp_b85.h +171 -0
- data/vendor/stringencoders/src/modp_b85_data.h +36 -0
- data/vendor/stringencoders/src/modp_b85_gen.c +65 -0
- data/vendor/stringencoders/src/modp_bjavascript.c +65 -0
- data/vendor/stringencoders/src/modp_bjavascript.h +105 -0
- data/vendor/stringencoders/src/modp_bjavascript_data.h +84 -0
- data/vendor/stringencoders/src/modp_bjavascript_gen.c +58 -0
- data/vendor/stringencoders/src/modp_burl.c +228 -0
- data/vendor/stringencoders/src/modp_burl.h +259 -0
- data/vendor/stringencoders/src/modp_burl_data.h +136 -0
- data/vendor/stringencoders/src/modp_burl_gen.c +121 -0
- data/vendor/stringencoders/src/modp_html.c +128 -0
- data/vendor/stringencoders/src/modp_html.h +53 -0
- data/vendor/stringencoders/src/modp_html_named_entities.h +9910 -0
- data/vendor/stringencoders/src/modp_json.c +315 -0
- data/vendor/stringencoders/src/modp_json.h +103 -0
- data/vendor/stringencoders/src/modp_json_data.h +57 -0
- data/vendor/stringencoders/src/modp_json_gen.py +60 -0
- data/vendor/stringencoders/src/modp_mainpage.h +120 -0
- data/vendor/stringencoders/src/modp_numtoa.c +350 -0
- data/vendor/stringencoders/src/modp_numtoa.h +100 -0
- data/vendor/stringencoders/src/modp_qsiter.c +76 -0
- data/vendor/stringencoders/src/modp_qsiter.h +71 -0
- data/vendor/stringencoders/src/modp_stdint.h +43 -0
- data/vendor/stringencoders/src/modp_utf8.c +88 -0
- data/vendor/stringencoders/src/modp_utf8.h +38 -0
- data/vendor/stringencoders/src/modp_xml.c +311 -0
- data/vendor/stringencoders/src/modp_xml.h +166 -0
- data/vendor/stringencoders/src/stringencoders.pc +10 -0
- data/vendor/stringencoders/src/stringencoders.pc.in +10 -0
- data/vendor/stringencoders/test/Makefile.am +113 -0
- data/vendor/stringencoders/test/apr_base64.c +262 -0
- data/vendor/stringencoders/test/apr_base64.h +120 -0
- data/vendor/stringencoders/test/cxx_test.cc +482 -0
- data/vendor/stringencoders/test/minunit.h +82 -0
- data/vendor/stringencoders/test/modp_ascii_test.c +281 -0
- data/vendor/stringencoders/test/modp_b16_test.c +288 -0
- data/vendor/stringencoders/test/modp_b2_test.c +250 -0
- data/vendor/stringencoders/test/modp_b64_test.c +266 -0
- data/vendor/stringencoders/test/modp_b85_test.c +130 -0
- data/vendor/stringencoders/test/modp_bjavascript_test.c +137 -0
- data/vendor/stringencoders/test/modp_burl_test.c +423 -0
- data/vendor/stringencoders/test/modp_html_test.c +296 -0
- data/vendor/stringencoders/test/modp_json_test.c +336 -0
- data/vendor/stringencoders/test/modp_numtoa_test.c +545 -0
- data/vendor/stringencoders/test/modp_qsiter_test.c +280 -0
- data/vendor/stringencoders/test/modp_utf8_test.c +188 -0
- data/vendor/stringencoders/test/modp_xml_test.c +339 -0
- data/vendor/stringencoders/test/speedtest.c +241 -0
- data/vendor/stringencoders/test/speedtest_ascii.c +345 -0
- data/vendor/stringencoders/test/speedtest_msg.c +78 -0
- data/vendor/stringencoders/test/speedtest_numtoa.c +276 -0
- metadata +314 -0
@@ -0,0 +1,250 @@
|
|
1
|
+
/* we compile as C90 but use snprintf */
|
2
|
+
#define _ISOC99_SOURCE
|
3
|
+
|
4
|
+
#include "modp_b2.h"
|
5
|
+
#include "minunit.h"
|
6
|
+
#include "modp_b16.h"
|
7
|
+
#include <stdio.h>
|
8
|
+
#include <stdlib.h>
|
9
|
+
#include <string.h>
|
10
|
+
|
11
|
+
static char* testEndian(void)
|
12
|
+
{
|
13
|
+
/* this test that "0001" is "0...1" */
|
14
|
+
char buf[100];
|
15
|
+
char result[100];
|
16
|
+
char endian[] = { (char)0, (char)0, (char)0, (char)1 };
|
17
|
+
size_t d = modp_b2_encode(buf, endian, (size_t)4);
|
18
|
+
mu_assert_int_equals(32, d);
|
19
|
+
mu_assert_str_equals("00000000000000000000000000000001", buf);
|
20
|
+
mu_assert_int_equals('0', buf[0]);
|
21
|
+
mu_assert_int_equals('1', buf[31]);
|
22
|
+
|
23
|
+
memset(result, 255, sizeof(result));
|
24
|
+
d = modp_b2_decode(result, buf, (size_t)32);
|
25
|
+
mu_assert_int_equals(4, d);
|
26
|
+
mu_assert_int_equals(endian[0], result[0]);
|
27
|
+
mu_assert_int_equals(endian[1], result[1]);
|
28
|
+
mu_assert_int_equals(endian[2], result[2]);
|
29
|
+
mu_assert_int_equals(endian[3], result[3]);
|
30
|
+
|
31
|
+
return 0;
|
32
|
+
}
|
33
|
+
|
34
|
+
static char* testEncodeDecode(void)
|
35
|
+
{
|
36
|
+
/* 2 bytes == 4 bytes out */
|
37
|
+
char ibuf[2];
|
38
|
+
char obuf[17];
|
39
|
+
char rbuf[17];
|
40
|
+
char msg[100]; /* for test messages output */
|
41
|
+
msg[0] = 0; /* make msg an empty string */
|
42
|
+
unsigned int i, j;
|
43
|
+
size_t d;
|
44
|
+
|
45
|
+
for (i = 0; i < 256; ++i) {
|
46
|
+
for (j = 0; j < 256; ++j) {
|
47
|
+
/* comment this out.. it really slows down the test */
|
48
|
+
sprintf(msg, "(i,j) = (%u,%u):", i, j);
|
49
|
+
ibuf[0] = (char)((unsigned char)i);
|
50
|
+
ibuf[1] = (char)((unsigned char)j);
|
51
|
+
|
52
|
+
memset(obuf, 0, sizeof(obuf));
|
53
|
+
d = modp_b16_encode(obuf, ibuf, (size_t)2);
|
54
|
+
mu_assert_int_equals_msg(msg, 4, d);
|
55
|
+
d = modp_b16_decode(rbuf, obuf, d);
|
56
|
+
mu_assert_int_equals_msg(msg, 2, d);
|
57
|
+
mu_assert_int_equals_msg(msg, ibuf[0], rbuf[0]);
|
58
|
+
mu_assert_int_equals_msg(msg, ibuf[1], rbuf[1]);
|
59
|
+
}
|
60
|
+
}
|
61
|
+
return 0;
|
62
|
+
}
|
63
|
+
|
64
|
+
static char* testOddDecode(void)
|
65
|
+
{
|
66
|
+
char obuf[100];
|
67
|
+
char ibuf[100];
|
68
|
+
|
69
|
+
memset(obuf, 0, sizeof(obuf));
|
70
|
+
memset(ibuf, 0, sizeof(ibuf));
|
71
|
+
|
72
|
+
/* Test Odd Number of Input bytes
|
73
|
+
* Should be error
|
74
|
+
*/
|
75
|
+
obuf[0] = 1;
|
76
|
+
mu_assert_int_equals(-1, modp_b2_decode(obuf, ibuf, (size_t)1));
|
77
|
+
mu_assert_int_equals(1, obuf[0]);
|
78
|
+
|
79
|
+
obuf[0] = 1;
|
80
|
+
mu_assert_int_equals(-1, modp_b2_decode(obuf, ibuf, (size_t)3));
|
81
|
+
mu_assert_int_equals(1, obuf[0]);
|
82
|
+
|
83
|
+
obuf[0] = 1;
|
84
|
+
mu_assert_int_equals(-1, modp_b2_decode(obuf, ibuf, (size_t)7));
|
85
|
+
mu_assert_int_equals(1, obuf[0]);
|
86
|
+
|
87
|
+
return 0;
|
88
|
+
}
|
89
|
+
|
90
|
+
/** \brief test input that is a multiple of 2 (special case in code)
|
91
|
+
*/
|
92
|
+
static char* testDecodeMutlipleOf2(void)
|
93
|
+
{
|
94
|
+
char obuf[100];
|
95
|
+
memset(obuf, 0xff, sizeof(obuf));
|
96
|
+
|
97
|
+
mu_assert_int_equals(1, modp_b16_decode(obuf, "01", (size_t)2));
|
98
|
+
mu_assert_int_equals(1, obuf[0]);
|
99
|
+
return 0;
|
100
|
+
}
|
101
|
+
|
102
|
+
static char* testOddEncode(void)
|
103
|
+
{
|
104
|
+
char obuf[100];
|
105
|
+
char ibuf[100];
|
106
|
+
|
107
|
+
/* oddball 1 char. */
|
108
|
+
ibuf[0] = 1;
|
109
|
+
mu_assert_int_equals(2, modp_b16_encode(obuf, ibuf, (size_t)1));
|
110
|
+
mu_assert_int_equals(obuf[0], '0');
|
111
|
+
mu_assert_int_equals(obuf[1], '1');
|
112
|
+
|
113
|
+
/* oddball 2 char.*/
|
114
|
+
ibuf[0] = 0;
|
115
|
+
ibuf[1] = 1;
|
116
|
+
mu_assert_int_equals(4, modp_b16_encode(obuf, ibuf, (size_t)2));
|
117
|
+
mu_assert_int_equals(obuf[0], '0');
|
118
|
+
mu_assert_int_equals(obuf[1], '0');
|
119
|
+
mu_assert_int_equals(obuf[2], '0');
|
120
|
+
mu_assert_int_equals(obuf[3], '1');
|
121
|
+
|
122
|
+
/* oddball 1 char. */
|
123
|
+
ibuf[0] = 0;
|
124
|
+
ibuf[1] = 0;
|
125
|
+
ibuf[2] = 1;
|
126
|
+
mu_assert_int_equals(6, modp_b16_encode(obuf, ibuf, (size_t)3));
|
127
|
+
mu_assert_int_equals(obuf[0], '0');
|
128
|
+
mu_assert_int_equals(obuf[1], '0');
|
129
|
+
mu_assert_int_equals(obuf[2], '0');
|
130
|
+
mu_assert_int_equals(obuf[3], '0');
|
131
|
+
mu_assert_int_equals(obuf[4], '0');
|
132
|
+
mu_assert_int_equals(obuf[5], '1');
|
133
|
+
return 0;
|
134
|
+
}
|
135
|
+
|
136
|
+
static char* testBadDecode(void)
|
137
|
+
{
|
138
|
+
char obuf[100];
|
139
|
+
char ibuf[100];
|
140
|
+
|
141
|
+
memset(obuf, 0, sizeof(obuf));
|
142
|
+
memset(ibuf, 0, sizeof(ibuf));
|
143
|
+
|
144
|
+
/* we are testing input of 2 bytes, 4 bytes
|
145
|
+
* to test all possible screwups
|
146
|
+
*/
|
147
|
+
strcpy(ibuf, "X1");
|
148
|
+
mu_assert_int_equals(-1, modp_b2_decode(obuf, ibuf, strlen(ibuf)));
|
149
|
+
strcpy(ibuf, "1X");
|
150
|
+
mu_assert_int_equals(-1, modp_b2_decode(obuf, ibuf, strlen(ibuf)));
|
151
|
+
strcpy(ibuf, "XX");
|
152
|
+
mu_assert_int_equals(-1, modp_b2_decode(obuf, ibuf, strlen(ibuf)));
|
153
|
+
|
154
|
+
/* 1 bad char */
|
155
|
+
strcpy(ibuf, "X111");
|
156
|
+
mu_assert_int_equals(-1, modp_b2_decode(obuf, ibuf, strlen(ibuf)));
|
157
|
+
strcpy(ibuf, "1X11");
|
158
|
+
mu_assert_int_equals(-1, modp_b2_decode(obuf, ibuf, strlen(ibuf)));
|
159
|
+
strcpy(ibuf, "11X1");
|
160
|
+
mu_assert_int_equals(-1, modp_b2_decode(obuf, ibuf, strlen(ibuf)));
|
161
|
+
strcpy(ibuf, "111X");
|
162
|
+
mu_assert_int_equals(-1, modp_b2_decode(obuf, ibuf, strlen(ibuf)));
|
163
|
+
|
164
|
+
/* 2 bad chars */
|
165
|
+
strcpy(ibuf, "XX11");
|
166
|
+
mu_assert_int_equals(-1, modp_b2_decode(obuf, ibuf, strlen(ibuf)));
|
167
|
+
strcpy(ibuf, "1XX1");
|
168
|
+
mu_assert_int_equals(-1, modp_b2_decode(obuf, ibuf, strlen(ibuf)));
|
169
|
+
strcpy(ibuf, "11XX");
|
170
|
+
mu_assert_int_equals(-1, modp_b2_decode(obuf, ibuf, strlen(ibuf)));
|
171
|
+
strcpy(ibuf, "X1X1");
|
172
|
+
mu_assert_int_equals(-1, modp_b2_decode(obuf, ibuf, strlen(ibuf)));
|
173
|
+
strcpy(ibuf, "1X1X");
|
174
|
+
mu_assert_int_equals(-1, modp_b2_decode(obuf, ibuf, strlen(ibuf)));
|
175
|
+
strcpy(ibuf, "X11X");
|
176
|
+
mu_assert_int_equals(-1, modp_b2_decode(obuf, ibuf, strlen(ibuf)));
|
177
|
+
|
178
|
+
/* 3 bad chars */
|
179
|
+
strcpy(ibuf, "1XXX");
|
180
|
+
mu_assert_int_equals(-1, modp_b2_decode(obuf, ibuf, strlen(ibuf)));
|
181
|
+
strcpy(ibuf, "X1XX");
|
182
|
+
mu_assert_int_equals(-1, modp_b2_decode(obuf, ibuf, strlen(ibuf)));
|
183
|
+
strcpy(ibuf, "XX1X");
|
184
|
+
mu_assert_int_equals(-1, modp_b2_decode(obuf, ibuf, strlen(ibuf)));
|
185
|
+
strcpy(ibuf, "XXX1");
|
186
|
+
mu_assert_int_equals(-1, modp_b2_decode(obuf, ibuf, strlen(ibuf)));
|
187
|
+
|
188
|
+
/* 4 bad chars */
|
189
|
+
strcpy(ibuf, "XXXX");
|
190
|
+
mu_assert_int_equals(-1, modp_b2_decode(obuf, ibuf, strlen(ibuf)));
|
191
|
+
|
192
|
+
return 0;
|
193
|
+
}
|
194
|
+
|
195
|
+
static char* testEmptyInput(void)
|
196
|
+
{
|
197
|
+
char obuf[100];
|
198
|
+
char ibuf[100];
|
199
|
+
|
200
|
+
memset(obuf, 0, sizeof(obuf));
|
201
|
+
memset(ibuf, 0, sizeof(ibuf));
|
202
|
+
|
203
|
+
/* encode 0 bytes, get a null byte back */
|
204
|
+
obuf[0] = 1;
|
205
|
+
mu_assert_int_equals(0, modp_b16_encode(obuf, ibuf, (size_t)0));
|
206
|
+
mu_assert_int_equals(0, obuf[0]);
|
207
|
+
|
208
|
+
/* decode 0 bytes, buffer is untouched */
|
209
|
+
obuf[0] = 1;
|
210
|
+
mu_assert_int_equals(0, modp_b16_decode(obuf, ibuf, (size_t)0));
|
211
|
+
mu_assert_int_equals(1, obuf[0]);
|
212
|
+
return 0;
|
213
|
+
}
|
214
|
+
|
215
|
+
static char* testLengths(void)
|
216
|
+
{
|
217
|
+
/* Decode Len
|
218
|
+
* 2 input -> 1 output. No added NULL byte
|
219
|
+
*/
|
220
|
+
mu_assert_int_equals(0, modp_b16_decode_len(0));
|
221
|
+
mu_assert_int_equals(1, modp_b16_decode_len(1));
|
222
|
+
mu_assert_int_equals(1, modp_b16_decode_len(2));
|
223
|
+
mu_assert_int_equals(2, modp_b16_decode_len(3));
|
224
|
+
mu_assert_int_equals(2, modp_b16_decode_len(4));
|
225
|
+
|
226
|
+
/* Encode Len
|
227
|
+
* 1 byte -> 2 output + 1 null byte
|
228
|
+
*/
|
229
|
+
mu_assert_int_equals(1, modp_b16_encode_len(0));
|
230
|
+
mu_assert_int_equals(3, modp_b16_encode_len(1));
|
231
|
+
mu_assert_int_equals(5, modp_b16_encode_len(2));
|
232
|
+
mu_assert_int_equals(7, modp_b16_encode_len(3));
|
233
|
+
mu_assert_int_equals(9, modp_b16_encode_len(4));
|
234
|
+
return 0;
|
235
|
+
}
|
236
|
+
|
237
|
+
static char* all_tests(void)
|
238
|
+
{
|
239
|
+
mu_run_test(testEndian);
|
240
|
+
mu_run_test(testEncodeDecode);
|
241
|
+
mu_run_test(testLengths);
|
242
|
+
mu_run_test(testEmptyInput);
|
243
|
+
mu_run_test(testBadDecode);
|
244
|
+
mu_run_test(testOddDecode);
|
245
|
+
mu_run_test(testOddEncode);
|
246
|
+
mu_run_test(testDecodeMutlipleOf2);
|
247
|
+
return 0;
|
248
|
+
}
|
249
|
+
|
250
|
+
UNITTESTS
|
@@ -0,0 +1,266 @@
|
|
1
|
+
/* we compile as C90 but use snprintf */
|
2
|
+
#define _ISOC99_SOURCE
|
3
|
+
|
4
|
+
#include <stdio.h>
|
5
|
+
#include <stdlib.h>
|
6
|
+
#include <string.h>
|
7
|
+
|
8
|
+
#include "minunit.h"
|
9
|
+
|
10
|
+
#include "modp_b64.h"
|
11
|
+
/**
|
12
|
+
* checks to make sure results are the same reguardless of
|
13
|
+
* CPU endian type (i.e. Intel vs. Sparc, etc)
|
14
|
+
*
|
15
|
+
*/
|
16
|
+
static char* testEndian(void)
|
17
|
+
{
|
18
|
+
/* this test that "1" is "AAAB" */
|
19
|
+
char buf[100];
|
20
|
+
char result[10];
|
21
|
+
char endian[] = { (char)0, (char)0, (char)1 };
|
22
|
+
size_t d = modp_b64_encode(buf, endian, (size_t)3);
|
23
|
+
mu_assert_int_equals(4, d);
|
24
|
+
mu_assert_int_equals('A', buf[0]);
|
25
|
+
mu_assert_int_equals('A', buf[1]);
|
26
|
+
mu_assert_int_equals('A', buf[2]);
|
27
|
+
mu_assert_int_equals('B', buf[3]);
|
28
|
+
|
29
|
+
memset(result, 255, sizeof(result));
|
30
|
+
d = modp_b64_decode(result, "AAAB", (size_t)4);
|
31
|
+
mu_assert_int_equals(3, d);
|
32
|
+
mu_assert_int_equals(0, result[0]);
|
33
|
+
mu_assert_int_equals(0, result[1]);
|
34
|
+
mu_assert_int_equals(1, result[2]);
|
35
|
+
mu_assert_int_equals(-1, result[3]);
|
36
|
+
|
37
|
+
return 0;
|
38
|
+
}
|
39
|
+
|
40
|
+
/**
|
41
|
+
* sending 0 as length to encode and decode
|
42
|
+
* should basically do nothing
|
43
|
+
*/
|
44
|
+
static char* testEmpty(void)
|
45
|
+
{
|
46
|
+
char buf[10];
|
47
|
+
const char* input = 0; /* null */
|
48
|
+
size_t d;
|
49
|
+
|
50
|
+
memset(buf, 1, sizeof(buf));
|
51
|
+
d = modp_b64_encode(buf, input, (size_t)0);
|
52
|
+
mu_assert_int_equals(0, d);
|
53
|
+
mu_assert_int_equals(0, buf[0]);
|
54
|
+
mu_assert_int_equals(1, buf[1]);
|
55
|
+
|
56
|
+
memset(buf, 1, sizeof(buf));
|
57
|
+
d = modp_b64_decode(buf, input, (size_t)0);
|
58
|
+
mu_assert_int_equals(0, d);
|
59
|
+
mu_assert_int_equals(1, buf[0]);
|
60
|
+
mu_assert_int_equals(1, buf[1]);
|
61
|
+
|
62
|
+
return 0;
|
63
|
+
}
|
64
|
+
|
65
|
+
/**
|
66
|
+
* Test 1-6 bytes input and decode
|
67
|
+
*
|
68
|
+
*/
|
69
|
+
static char* testPadding(void)
|
70
|
+
{
|
71
|
+
char msg[100];
|
72
|
+
const char ibuf[6] = { 1, 1, 1, 1, 1, 1 };
|
73
|
+
char obuf[10];
|
74
|
+
char rbuf[10];
|
75
|
+
size_t d = 0;
|
76
|
+
|
77
|
+
/* 1 in, 4 out */
|
78
|
+
memset(obuf, 255, sizeof(obuf));
|
79
|
+
d = modp_b64_encode(obuf, ibuf, (size_t)1);
|
80
|
+
sprintf(msg, "b64='%s', d=%d", obuf, (int)d);
|
81
|
+
mu_assert_int_equals_msg(msg, 4, d);
|
82
|
+
mu_assert_int_equals_msg(msg, 0, obuf[4]);
|
83
|
+
memset(rbuf, 255, sizeof(rbuf));
|
84
|
+
d = modp_b64_decode(rbuf, obuf, d);
|
85
|
+
mu_assert_int_equals_msg(msg, 1, d);
|
86
|
+
mu_assert_int_equals(1, rbuf[0]);
|
87
|
+
mu_assert_int_equals(-1, rbuf[1]);
|
88
|
+
|
89
|
+
/* 2 in, 4 out */
|
90
|
+
memset(obuf, 255, sizeof(obuf));
|
91
|
+
d = modp_b64_encode(obuf, ibuf, (size_t)2);
|
92
|
+
sprintf(msg, "b64='%s', d=%d", obuf, (int)d);
|
93
|
+
mu_assert_int_equals_msg(msg, 4, d);
|
94
|
+
mu_assert_int_equals_msg(msg, 0, obuf[4]);
|
95
|
+
memset(rbuf, 255, sizeof(rbuf));
|
96
|
+
d = modp_b64_decode(rbuf, obuf, d);
|
97
|
+
mu_assert_int_equals_msg(msg, 2, d);
|
98
|
+
mu_assert_int_equals_msg(msg, 1, rbuf[0]);
|
99
|
+
mu_assert_int_equals_msg(msg, 1, rbuf[1]);
|
100
|
+
mu_assert_int_equals_msg(msg, -1, rbuf[2]);
|
101
|
+
|
102
|
+
/* 3 in, 4 out */
|
103
|
+
memset(obuf, 255, sizeof(obuf));
|
104
|
+
d = modp_b64_encode(obuf, ibuf, (size_t)3);
|
105
|
+
sprintf(msg, "b64='%s', d=%d", obuf, (int)d);
|
106
|
+
mu_assert_int_equals_msg(msg, 4, d);
|
107
|
+
mu_assert_int_equals_msg(msg, 0, obuf[4]);
|
108
|
+
memset(rbuf, 255, sizeof(rbuf));
|
109
|
+
d = modp_b64_decode(rbuf, obuf, d);
|
110
|
+
mu_assert_int_equals_msg(msg, 3, d);
|
111
|
+
mu_assert_int_equals_msg(msg, 1, rbuf[0]);
|
112
|
+
mu_assert_int_equals_msg(msg, 1, rbuf[1]);
|
113
|
+
mu_assert_int_equals_msg(msg, 1, rbuf[2]);
|
114
|
+
mu_assert_int_equals_msg(msg, -1, rbuf[3]);
|
115
|
+
|
116
|
+
/* 4 in, 8 out */
|
117
|
+
memset(obuf, 255, sizeof(obuf));
|
118
|
+
d = modp_b64_encode(obuf, ibuf, (size_t)4);
|
119
|
+
sprintf(msg, "b64='%s', d=%d", obuf, (int)d);
|
120
|
+
mu_assert_int_equals_msg(msg, 8, d);
|
121
|
+
mu_assert_int_equals_msg(msg, 0, obuf[8]);
|
122
|
+
memset(rbuf, 255, sizeof(rbuf));
|
123
|
+
d = modp_b64_decode(rbuf, obuf, d);
|
124
|
+
mu_assert_int_equals(4, d);
|
125
|
+
mu_assert_int_equals(1, rbuf[0]);
|
126
|
+
mu_assert_int_equals(1, rbuf[1]);
|
127
|
+
mu_assert_int_equals(1, rbuf[2]);
|
128
|
+
mu_assert_int_equals(1, rbuf[3]);
|
129
|
+
mu_assert_int_equals(-1, rbuf[4]);
|
130
|
+
|
131
|
+
/* 5 in, 8 out */
|
132
|
+
memset(obuf, 255, sizeof(obuf));
|
133
|
+
d = modp_b64_encode(obuf, ibuf, (size_t)5);
|
134
|
+
sprintf(msg, "b64='%s', d=%d", obuf, (int)d);
|
135
|
+
mu_assert_int_equals_msg(msg, 8, d);
|
136
|
+
mu_assert_int_equals_msg(msg, 0, obuf[8]);
|
137
|
+
memset(rbuf, 255, sizeof(rbuf));
|
138
|
+
d = modp_b64_decode(rbuf, obuf, d);
|
139
|
+
mu_assert_int_equals(5, d);
|
140
|
+
mu_assert_int_equals(1, rbuf[0]);
|
141
|
+
mu_assert_int_equals(1, rbuf[1]);
|
142
|
+
mu_assert_int_equals(1, rbuf[2]);
|
143
|
+
mu_assert_int_equals(1, rbuf[3]);
|
144
|
+
mu_assert_int_equals(1, rbuf[4]);
|
145
|
+
mu_assert_int_equals(-1, rbuf[5]);
|
146
|
+
|
147
|
+
/* 6 in, 8 out */
|
148
|
+
memset(obuf, 255, sizeof(obuf));
|
149
|
+
d = modp_b64_encode(obuf, ibuf, (size_t)6);
|
150
|
+
sprintf(msg, "b64='%s', d=%d", obuf, (int)d);
|
151
|
+
mu_assert_int_equals_msg(msg, 8, d);
|
152
|
+
mu_assert_int_equals_msg(msg, 0, obuf[8]);
|
153
|
+
memset(rbuf, 255, sizeof(rbuf));
|
154
|
+
d = modp_b64_decode(rbuf, obuf, d);
|
155
|
+
mu_assert_int_equals(6, d);
|
156
|
+
mu_assert_int_equals(1, rbuf[0]);
|
157
|
+
mu_assert_int_equals(1, rbuf[1]);
|
158
|
+
mu_assert_int_equals(1, rbuf[2]);
|
159
|
+
mu_assert_int_equals(1, rbuf[3]);
|
160
|
+
mu_assert_int_equals(1, rbuf[4]);
|
161
|
+
mu_assert_int_equals(1, rbuf[5]);
|
162
|
+
mu_assert_int_equals(-1, rbuf[6]);
|
163
|
+
|
164
|
+
return 0;
|
165
|
+
}
|
166
|
+
|
167
|
+
/**
|
168
|
+
* Test all 17M 3 bytes inputs to encoder, decode
|
169
|
+
* and make sure it's equal.
|
170
|
+
*/
|
171
|
+
static char* testEncodeDecode(void)
|
172
|
+
{
|
173
|
+
char ibuf[4];
|
174
|
+
char obuf[5];
|
175
|
+
char rbuf[4];
|
176
|
+
char msg[100];
|
177
|
+
msg[0] = 0; /* make msg an empty string */
|
178
|
+
unsigned int i, j, k;
|
179
|
+
size_t d = 0;
|
180
|
+
for (i = 0; i < 256; ++i) {
|
181
|
+
for (j = 0; j < 256; ++j) {
|
182
|
+
for (k = 0; k < 256; ++k) {
|
183
|
+
/* comment this out.. it really slows down the test */
|
184
|
+
/* sprintf(msg, "(i,j,k) = (%d,%d,%d):", i,j,k); */
|
185
|
+
ibuf[0] = (char)((unsigned char)i);
|
186
|
+
ibuf[1] = (char)((unsigned char)j);
|
187
|
+
ibuf[2] = (char)((unsigned char)k);
|
188
|
+
ibuf[3] = 0;
|
189
|
+
|
190
|
+
memset(obuf, 1, sizeof(obuf));
|
191
|
+
d = modp_b64_encode(obuf, ibuf, (size_t)3);
|
192
|
+
mu_assert_int_equals_msg(msg, 4, d);
|
193
|
+
mu_assert_int_equals_msg(msg, 0, obuf[4]);
|
194
|
+
|
195
|
+
memset(rbuf, 1, sizeof(rbuf));
|
196
|
+
d = modp_b64_decode(rbuf, obuf, (size_t)4);
|
197
|
+
mu_assert_int_equals_msg(msg, 3, d);
|
198
|
+
mu_assert_int_equals_msg(msg, ibuf[0], rbuf[0]);
|
199
|
+
mu_assert_int_equals_msg(msg, ibuf[1], rbuf[1]);
|
200
|
+
mu_assert_int_equals_msg(msg, ibuf[2], rbuf[2]);
|
201
|
+
mu_assert_int_equals_msg(msg, 1, rbuf[3]);
|
202
|
+
}
|
203
|
+
}
|
204
|
+
}
|
205
|
+
return 0;
|
206
|
+
}
|
207
|
+
|
208
|
+
static char* testDecodeErrors(void)
|
209
|
+
{
|
210
|
+
int i;
|
211
|
+
size_t y;
|
212
|
+
char out[1000];
|
213
|
+
char decode[5];
|
214
|
+
char msg[100];
|
215
|
+
|
216
|
+
/* negative length */
|
217
|
+
memset(decode, 1, sizeof(decode));
|
218
|
+
y = modp_b64_decode(out, decode, (size_t)-1);
|
219
|
+
mu_assert_int_equals(-1, y);
|
220
|
+
|
221
|
+
/* test bad input - all combinations */
|
222
|
+
char goodchar = 'A';
|
223
|
+
char badchar = '~';
|
224
|
+
for (i = 1; i < 16; ++i) {
|
225
|
+
decode[0] = (char)(((i & 0x01) == 0) ? goodchar : badchar);
|
226
|
+
decode[1] = (char)(((i & 0x02) == 0) ? goodchar : badchar);
|
227
|
+
decode[2] = (char)(((i & 0x04) == 0) ? goodchar : badchar);
|
228
|
+
decode[3] = (char)(((i & 0x08) == 0) ? goodchar : badchar);
|
229
|
+
decode[4] = '\0';
|
230
|
+
|
231
|
+
sprintf(msg, "i = %d, %s", i, decode);
|
232
|
+
y = modp_b64_decode(out, decode, (size_t)4);
|
233
|
+
mu_assert_int_equals_msg(msg, (size_t)-1, y);
|
234
|
+
}
|
235
|
+
|
236
|
+
/* test just 1-4 padchars */
|
237
|
+
for (i = 0; i < 4; ++i) {
|
238
|
+
decode[i] = '=';
|
239
|
+
decode[i + 1] = '\0';
|
240
|
+
y = modp_b64_decode(out, decode, (size_t)(i + 1));
|
241
|
+
sprintf(msg, "i=%d, b64=%s", i, decode);
|
242
|
+
mu_assert_int_equals_msg(msg, (size_t)-1, y);
|
243
|
+
}
|
244
|
+
|
245
|
+
/* Test good+3 pad chars (should be impossible) */
|
246
|
+
decode[0] = 'A';
|
247
|
+
decode[1] = '=';
|
248
|
+
decode[2] = '=';
|
249
|
+
decode[3] = '=';
|
250
|
+
y = modp_b64_decode(out, decode, (size_t)4);
|
251
|
+
mu_assert_int_equals(-1, y);
|
252
|
+
|
253
|
+
return 0;
|
254
|
+
}
|
255
|
+
|
256
|
+
static char* all_tests(void)
|
257
|
+
{
|
258
|
+
mu_run_test(testEndian);
|
259
|
+
mu_run_test(testEmpty);
|
260
|
+
mu_run_test(testPadding);
|
261
|
+
mu_run_test(testEncodeDecode);
|
262
|
+
mu_run_test(testDecodeErrors);
|
263
|
+
return 0;
|
264
|
+
}
|
265
|
+
|
266
|
+
UNITTESTS
|
@@ -0,0 +1,130 @@
|
|
1
|
+
/* we compile as C90 but use snprintf */
|
2
|
+
#define _ISOC99_SOURCE
|
3
|
+
|
4
|
+
#include <stdio.h>
|
5
|
+
#include <stdlib.h>
|
6
|
+
#include <string.h>
|
7
|
+
|
8
|
+
#include "minunit.h"
|
9
|
+
|
10
|
+
#include "modp_b85.h"
|
11
|
+
|
12
|
+
/**
|
13
|
+
* checks to make sure results are the same reguardless of
|
14
|
+
* CPU endian type (i.e. Intel vs. Sparc, etc)
|
15
|
+
*
|
16
|
+
*/
|
17
|
+
static char* testEndian(void)
|
18
|
+
{
|
19
|
+
/* this test that "1" is "!!!!#" */
|
20
|
+
char buf[100];
|
21
|
+
char result[10];
|
22
|
+
char endian[] = { (char)0, (char)0, (char)0, (char)1 };
|
23
|
+
size_t d = modp_b85_encode(buf, endian, (size_t)4);
|
24
|
+
mu_assert_int_equals(5, d);
|
25
|
+
mu_assert_int_equals('!', buf[0]);
|
26
|
+
mu_assert_int_equals('!', buf[1]);
|
27
|
+
mu_assert_int_equals('!', buf[2]);
|
28
|
+
mu_assert_int_equals('!', buf[3]);
|
29
|
+
mu_assert_int_equals('#', buf[4]);
|
30
|
+
|
31
|
+
memset(result, 255, sizeof(result));
|
32
|
+
d = modp_b85_decode(result, "!!!!#", (size_t)5);
|
33
|
+
mu_assert_int_equals(4, d);
|
34
|
+
mu_assert_int_equals(0, result[0]);
|
35
|
+
mu_assert_int_equals(0, result[1]);
|
36
|
+
mu_assert_int_equals(0, result[2]);
|
37
|
+
mu_assert_int_equals(1, result[3]);
|
38
|
+
mu_assert_int_equals(-1, result[4]);
|
39
|
+
|
40
|
+
return 0;
|
41
|
+
}
|
42
|
+
|
43
|
+
static char* testLength(void)
|
44
|
+
{
|
45
|
+
/**
|
46
|
+
* Decode Len
|
47
|
+
* 5 bytes -> 4 in, no null
|
48
|
+
*/
|
49
|
+
mu_assert_int_equals(0, modp_b85_decode_len(0));
|
50
|
+
mu_assert_int_equals(4, modp_b85_decode_len(5));
|
51
|
+
mu_assert_int_equals(8, modp_b85_decode_len(10));
|
52
|
+
|
53
|
+
/* Encode Len
|
54
|
+
* 4 byte -> 5 output + 1 null byte
|
55
|
+
*/
|
56
|
+
mu_assert_int_equals(1, modp_b85_encode_len(0));
|
57
|
+
mu_assert_int_equals(6, modp_b85_encode_len(4));
|
58
|
+
mu_assert_int_equals(11, modp_b85_encode_len(8));
|
59
|
+
return 0;
|
60
|
+
}
|
61
|
+
|
62
|
+
/** \brief test bad input lengths
|
63
|
+
* The b85 encode only accepts multiples of 4.
|
64
|
+
*
|
65
|
+
* The b85 decoder only accepts a multiple of 5.
|
66
|
+
*
|
67
|
+
*/
|
68
|
+
static char* testBadInputLength(void)
|
69
|
+
{
|
70
|
+
char buf[100];
|
71
|
+
mu_assert_int_equals(-1, modp_b85_encode(buf, buf, (size_t)5));
|
72
|
+
mu_assert_int_equals(-1, modp_b85_decode(buf, buf, (size_t)11));
|
73
|
+
return 0;
|
74
|
+
}
|
75
|
+
|
76
|
+
/** \brief test decoding invalid b85 chars
|
77
|
+
*
|
78
|
+
*/
|
79
|
+
static char* testBadCharDecode(void)
|
80
|
+
{
|
81
|
+
char buf[] = { 'A', 'B', 'C', 'D', '\n', '\0' };
|
82
|
+
mu_assert_int_equals(-1, modp_b85_decode(buf, buf, (size_t)5));
|
83
|
+
return 0;
|
84
|
+
}
|
85
|
+
|
86
|
+
static char* testEncodeDecode(void)
|
87
|
+
{
|
88
|
+
char ibuf[10]; /* input */
|
89
|
+
char obuf[10]; /* output */
|
90
|
+
char rbuf[10]; /* final result */
|
91
|
+
size_t d;
|
92
|
+
int i, j, k, l;
|
93
|
+
for (i = 0; i < 256; ++i) {
|
94
|
+
for (j = 0; j < 256; j += 16) { /* save some time +=16 */
|
95
|
+
for (k = 0; k < 256; k += 8) { /* save some time += 8 */
|
96
|
+
for (l = 0; l < 256; ++l) {
|
97
|
+
ibuf[0] = (char)i;
|
98
|
+
ibuf[1] = (char)j;
|
99
|
+
ibuf[2] = (char)k;
|
100
|
+
ibuf[3] = (char)l;
|
101
|
+
memset(obuf, 255, sizeof(obuf));
|
102
|
+
d = modp_b85_encode(obuf, ibuf, (size_t)4);
|
103
|
+
mu_assert_int_equals(5, d);
|
104
|
+
mu_assert_int_equals(0, obuf[5]);
|
105
|
+
memset(rbuf, 255, sizeof(rbuf));
|
106
|
+
d = modp_b85_decode(rbuf, obuf, (size_t)5);
|
107
|
+
mu_assert_int_equals(4, d);
|
108
|
+
mu_assert_int_equals(ibuf[0], rbuf[0]);
|
109
|
+
mu_assert_int_equals(ibuf[1], rbuf[1]);
|
110
|
+
mu_assert_int_equals(ibuf[2], rbuf[2]);
|
111
|
+
mu_assert_int_equals(ibuf[3], rbuf[3]);
|
112
|
+
mu_assert_int_equals(-1, rbuf[4]);
|
113
|
+
}
|
114
|
+
}
|
115
|
+
}
|
116
|
+
}
|
117
|
+
return 0;
|
118
|
+
}
|
119
|
+
|
120
|
+
static char* all_tests(void)
|
121
|
+
{
|
122
|
+
mu_run_test(testEndian);
|
123
|
+
mu_run_test(testLength);
|
124
|
+
mu_run_test(testBadInputLength);
|
125
|
+
mu_run_test(testBadCharDecode);
|
126
|
+
mu_run_test(testEncodeDecode);
|
127
|
+
return 0;
|
128
|
+
}
|
129
|
+
|
130
|
+
UNITTESTS
|