Bitcoin ABC 0.26.3
P2P Digital Currency
Loading...
Searching...
No Matches
test.c
Go to the documentation of this file.
1/*********************************************************************
2 * Copyright (c) 2016 Pieter Wuille *
3 * Distributed under the MIT software license, see the accompanying *
4 * file COPYING or http://www.opensource.org/licenses/mit-license.php.*
5 **********************************************************************/
6
7#include "ctaes.h"
8
9#include <assert.h>
10#include <stdio.h>
11#include <string.h>
12
13typedef struct {
15 const char *key;
16 const char *plain;
17 const char *cipher;
19
20static const ctaes_test ctaes_tests[] = {
21 /* AES test vectors from FIPS 197. */
22 {128, "000102030405060708090a0b0c0d0e0f",
23 "00112233445566778899aabbccddeeff", "69c4e0d86a7b0430d8cdb78070b4c55a"},
24 {192, "000102030405060708090a0b0c0d0e0f1011121314151617",
25 "00112233445566778899aabbccddeeff", "dda97ca4864cdfe06eaf70a0ec0d7191"},
26 {256, "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f",
27 "00112233445566778899aabbccddeeff", "8ea2b7ca516745bfeafc49904b496089"},
28
29 /* AES-ECB test vectors from NIST sp800-38a. */
30 {128, "2b7e151628aed2a6abf7158809cf4f3c",
31 "6bc1bee22e409f96e93d7e117393172a", "3ad77bb40d7a3660a89ecaf32466ef97"},
32 {128, "2b7e151628aed2a6abf7158809cf4f3c",
33 "ae2d8a571e03ac9c9eb76fac45af8e51", "f5d3d58503b9699de785895a96fdbaaf"},
34 {128, "2b7e151628aed2a6abf7158809cf4f3c",
35 "30c81c46a35ce411e5fbc1191a0a52ef", "43b1cd7f598ece23881b00e3ed030688"},
36 {128, "2b7e151628aed2a6abf7158809cf4f3c",
37 "f69f2445df4f9b17ad2b417be66c3710", "7b0c785e27e8ad3f8223207104725dd4"},
38 {192, "8e73b0f7da0e6452c810f32b809079e562f8ead2522c6b7b",
39 "6bc1bee22e409f96e93d7e117393172a", "bd334f1d6e45f25ff712a214571fa5cc"},
40 {192, "8e73b0f7da0e6452c810f32b809079e562f8ead2522c6b7b",
41 "ae2d8a571e03ac9c9eb76fac45af8e51", "974104846d0ad3ad7734ecb3ecee4eef"},
42 {192, "8e73b0f7da0e6452c810f32b809079e562f8ead2522c6b7b",
43 "30c81c46a35ce411e5fbc1191a0a52ef", "ef7afd2270e2e60adce0ba2face6444e"},
44 {192, "8e73b0f7da0e6452c810f32b809079e562f8ead2522c6b7b",
45 "f69f2445df4f9b17ad2b417be66c3710", "9a4b41ba738d6c72fb16691603c18e0e"},
46 {256, "603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4",
47 "6bc1bee22e409f96e93d7e117393172a", "f3eed1bdb5d2a03c064b5a7e3db181f8"},
48 {256, "603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4",
49 "ae2d8a571e03ac9c9eb76fac45af8e51", "591ccb10d410ed26dc5ba74a31362870"},
50 {256, "603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4",
51 "30c81c46a35ce411e5fbc1191a0a52ef", "b6ed21b99ca6f4f9f153e7b1beafed1d"},
52 {256, "603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4",
53 "f69f2445df4f9b17ad2b417be66c3710", "23304b7a39f9f3ff067d8d8f9e24ecc7"}};
54
55static void from_hex(uint8_t *data, int len, const char *hex) {
56 int p;
57 for (p = 0; p < len; p++) {
58 int v = 0;
59 int n;
60 for (n = 0; n < 2; n++) {
61 assert((*hex >= '0' && *hex <= '9') ||
62 (*hex >= 'a' && *hex <= 'f'));
63 if (*hex >= '0' && *hex <= '9') {
64 v |= (*hex - '0') << (4 * (1 - n));
65 } else {
66 v |= (*hex - 'a' + 10) << (4 * (1 - n));
67 }
68 hex++;
69 }
70 *(data++) = v;
71 }
72 assert(*hex == 0);
73}
74
75int main(void) {
76 int i;
77 int fail = 0;
78 for (i = 0; i < sizeof(ctaes_tests) / sizeof(ctaes_tests[0]); i++) {
79 uint8_t key[32], plain[16], cipher[16], ciphered[16], deciphered[16];
80 const ctaes_test *test = &ctaes_tests[i];
81 assert(test->keysize == 128 || test->keysize == 192 ||
82 test->keysize == 256);
83 from_hex(plain, 16, test->plain);
84 from_hex(cipher, 16, test->cipher);
85 switch (test->keysize) {
86 case 128: {
88 from_hex(key, 16, test->key);
89 AES128_init(&ctx, key);
90 AES128_encrypt(&ctx, 1, ciphered, plain);
91 AES128_decrypt(&ctx, 1, deciphered, cipher);
92 break;
93 }
94 case 192: {
96 from_hex(key, 24, test->key);
97 AES192_init(&ctx, key);
98 AES192_encrypt(&ctx, 1, ciphered, plain);
99 AES192_decrypt(&ctx, 1, deciphered, cipher);
100 break;
101 }
102 case 256: {
104 from_hex(key, 32, test->key);
105 AES256_init(&ctx, key);
106 AES256_encrypt(&ctx, 1, ciphered, plain);
107 AES256_decrypt(&ctx, 1, deciphered, cipher);
108 break;
109 }
110 }
111 if (memcmp(cipher, ciphered, 16)) {
112 fprintf(stderr, "E(key=\"%s\", plain=\"%s\") != \"%s\"\n",
113 test->key, test->plain, test->cipher);
114 fail++;
115 }
116 if (memcmp(plain, deciphered, 16)) {
117 fprintf(stderr, "D(key=\"%s\", cipher=\"%s\") != \"%s\"\n",
118 test->key, test->cipher, test->plain);
119 fail++;
120 }
121 }
122 if (fail == 0) {
123 fprintf(stderr, "All tests successful\n");
124 } else {
125 fprintf(stderr, "%i tests failed\n", fail);
126 }
127 return (fail != 0);
128}
secp256k1_context * ctx
void AES192_decrypt(const AES192_ctx *ctx, size_t blocks, uint8_t *plain16, const uint8_t *cipher16)
Definition ctaes.c:553
void AES128_decrypt(const AES128_ctx *ctx, size_t blocks, uint8_t *plain16, const uint8_t *cipher16)
Definition ctaes.c:531
void AES256_decrypt(const AES256_ctx *ctx, size_t blocks, uint8_t *plain16, const uint8_t *cipher16)
Definition ctaes.c:575
void AES128_init(AES128_ctx *ctx, const uint8_t *key16)
Definition ctaes.c:518
void AES256_encrypt(const AES256_ctx *ctx, size_t blocks, uint8_t *cipher16, const uint8_t *plain16)
Definition ctaes.c:566
void AES192_encrypt(const AES192_ctx *ctx, size_t blocks, uint8_t *cipher16, const uint8_t *plain16)
Definition ctaes.c:544
void AES256_init(AES256_ctx *ctx, const uint8_t *key32)
Definition ctaes.c:562
void AES128_encrypt(const AES128_ctx *ctx, size_t blocks, uint8_t *cipher16, const uint8_t *plain16)
Definition ctaes.c:522
void AES192_init(AES192_ctx *ctx, const uint8_t *key24)
Definition ctaes.c:540
T GetRand(T nMax=std::numeric_limits< T >::max()) noexcept
Generate a uniform random integer of type T in the range [0..nMax) nMax defaults to std::numeric_limi...
Definition random.h:85
const char * cipher
Definition test.c:17
const char * plain
Definition test.c:16
int keysize
Definition test.c:14
const char * key
Definition test.c:15
static const ctaes_test ctaes_tests[]
Definition test.c:20
static void from_hex(uint8_t *data, int len, const char *hex)
Definition test.c:55
int main(void)
Definition test.c:75
assert(!tx.IsCoinBase())