diff options
Diffstat (limited to '')
| -rw-r--r-- | src/regress/lib/libcrypto/chacha/chachatest.c | 121 |
1 files changed, 99 insertions, 22 deletions
diff --git a/src/regress/lib/libcrypto/chacha/chachatest.c b/src/regress/lib/libcrypto/chacha/chachatest.c index aa10939d7e..33652ee14f 100644 --- a/src/regress/lib/libcrypto/chacha/chachatest.c +++ b/src/regress/lib/libcrypto/chacha/chachatest.c | |||
| @@ -222,40 +222,117 @@ struct chacha_tv chacha_test_vectors[] = { | |||
| 222 | 222 | ||
| 223 | #define N_VECTORS (sizeof(chacha_test_vectors) / sizeof(*chacha_test_vectors)) | 223 | #define N_VECTORS (sizeof(chacha_test_vectors) / sizeof(*chacha_test_vectors)) |
| 224 | 224 | ||
| 225 | /* Single-shot ChaCha20 using CRYPTO_chacha_20 interface. */ | ||
| 226 | static void | ||
| 227 | crypto_chacha_20_test(struct chacha_tv *tv, unsigned char *out, | ||
| 228 | unsigned char *in) | ||
| 229 | { | ||
| 230 | CRYPTO_chacha_20(out, in, tv->len, tv->key, tv->iv, 0); | ||
| 231 | } | ||
| 232 | |||
| 233 | /* Single-shot ChaCha20 using the ChaCha interface. */ | ||
| 234 | static void | ||
| 235 | chacha_ctx_full_test(struct chacha_tv *tv, unsigned char *out, | ||
| 236 | unsigned char *in) | ||
| 237 | { | ||
| 238 | ChaCha_ctx ctx; | ||
| 239 | |||
| 240 | ChaCha_set_key(&ctx, tv->key, 256); | ||
| 241 | ChaCha_set_iv(&ctx, tv->iv, NULL); | ||
| 242 | ChaCha(&ctx, out, in, tv->len); | ||
| 243 | } | ||
| 244 | |||
| 245 | /* ChaCha20 with partial writes using the Chacha interface. */ | ||
| 246 | static void | ||
| 247 | chacha_ctx_partial_test(struct chacha_tv *tv, unsigned char *out, | ||
| 248 | unsigned char *in) | ||
| 249 | { | ||
| 250 | ChaCha_ctx ctx; | ||
| 251 | int len, size = 0; | ||
| 252 | |||
| 253 | ChaCha_set_key(&ctx, tv->key, 256); | ||
| 254 | ChaCha_set_iv(&ctx, tv->iv, NULL); | ||
| 255 | len = tv->len - 1; | ||
| 256 | while (len > 1) { | ||
| 257 | size = len / 2; | ||
| 258 | ChaCha(&ctx, out, in, size); | ||
| 259 | in += size; | ||
| 260 | out += size; | ||
| 261 | len -= size; | ||
| 262 | } | ||
| 263 | ChaCha(&ctx, out, in, len + 1); | ||
| 264 | } | ||
| 265 | |||
| 266 | /* ChaCha20 with single byte writes using the Chacha interface. */ | ||
| 267 | static void | ||
| 268 | chacha_ctx_single_test(struct chacha_tv *tv, unsigned char *out, | ||
| 269 | unsigned char *in) | ||
| 270 | { | ||
| 271 | ChaCha_ctx ctx; | ||
| 272 | size_t i; | ||
| 273 | |||
| 274 | ChaCha_set_key(&ctx, tv->key, 256); | ||
| 275 | ChaCha_set_iv(&ctx, tv->iv, NULL); | ||
| 276 | for (i = 0; i < tv->len; i++) | ||
| 277 | ChaCha(&ctx, out + i, in + i, 1); | ||
| 278 | } | ||
| 279 | |||
| 280 | struct chacha_test_function { | ||
| 281 | char *name; | ||
| 282 | void (*func)(struct chacha_tv *, unsigned char *, unsigned char *); | ||
| 283 | }; | ||
| 284 | |||
| 285 | struct chacha_test_function chacha_test_functions[] = { | ||
| 286 | {"crypto_chacha_20_test", crypto_chacha_20_test}, | ||
| 287 | {"chacha_ctx_full_test", chacha_ctx_full_test}, | ||
| 288 | {"chacha_ctx_partial_test", chacha_ctx_partial_test}, | ||
| 289 | {"chacha_ctx_single_test", chacha_ctx_single_test}, | ||
| 290 | }; | ||
| 291 | |||
| 292 | #define N_FUNCS (sizeof(chacha_test_functions) / sizeof(*chacha_test_functions)) | ||
| 293 | |||
| 225 | int | 294 | int |
| 226 | main(int argc, char **argv) | 295 | main(int argc, char **argv) |
| 227 | { | 296 | { |
| 228 | struct chacha_tv *tv; | 297 | struct chacha_tv *tv; |
| 229 | unsigned char *in, *out; | 298 | unsigned char *in, *out; |
| 230 | size_t i, j; | 299 | size_t i, j, k; |
| 300 | int failed = 0; | ||
| 231 | 301 | ||
| 232 | for (i = 0; i < N_VECTORS; i++) { | 302 | for (i = 0; i < N_VECTORS; i++) { |
| 233 | tv = &chacha_test_vectors[i]; | 303 | tv = &chacha_test_vectors[i]; |
| 234 | 304 | ||
| 235 | in = malloc(tv->len); | 305 | for (j = 0; j < N_FUNCS; j++) { |
| 236 | if (in == NULL) | 306 | in = calloc(1, tv->len); |
| 237 | errx(1, "malloc in"); | 307 | if (in == NULL) |
| 238 | out = malloc(tv->len); | 308 | errx(1, "calloc in"); |
| 239 | if (out == NULL) | 309 | out = calloc(1, tv->len); |
| 240 | errx(1, "malloc out"); | 310 | if (out == NULL) |
| 241 | memset(in, 0, tv->len); | 311 | errx(1, "calloc out"); |
| 242 | 312 | ||
| 243 | CRYPTO_chacha_20(out, in, tv->len, tv->key, tv->iv, 0); | 313 | chacha_test_functions[j].func(tv, out, in); |
| 244 | 314 | ||
| 245 | if (memcmp(out, tv->out, tv->len) != 0) { | 315 | if (memcmp(out, tv->out, tv->len) != 0) { |
| 246 | printf("ChaCha %s failed!\n", tv->desc); | 316 | printf("ChaCha %s failed for \"%s\"!\n", |
| 247 | for (j = 0; j < tv->len; j++) | 317 | chacha_test_functions[j].name, tv->desc); |
| 248 | printf("%2.2x", out[j]); | 318 | |
| 249 | printf("\n"); | 319 | printf("Got:\t"); |
| 250 | for (j = 0; j < tv->len; j++) | 320 | for (k = 0; k < tv->len; k++) |
| 251 | printf("%2.2x", tv->out[j]); | 321 | printf("%2.2x", out[k]); |
| 252 | printf("\n"); | 322 | printf("\n"); |
| 253 | return 1; | ||
| 254 | } | ||
| 255 | 323 | ||
| 256 | free(in); | 324 | printf("Want:\t"); |
| 257 | free(out); | 325 | for (k = 0; k < tv->len; k++) |
| 326 | printf("%2.2x", tv->out[k]); | ||
| 327 | printf("\n"); | ||
| 328 | |||
| 329 | failed = 1; | ||
| 330 | } | ||
| 331 | |||
| 332 | free(in); | ||
| 333 | free(out); | ||
| 334 | } | ||
| 258 | } | 335 | } |
| 259 | 336 | ||
| 260 | return 0; | 337 | return failed; |
| 261 | } | 338 | } |
