Merge remote-tracking branch 'teor/ticket28318-035' into maint-0.3.5
[tor.git] / src / test / test_crypto_ope.c
blob4e7b95232705d6933367d74b5333891f30b8ee4d
1 /* Copyright (c) 2001-2004, Roger Dingledine.
2 * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
3 * Copyright (c) 2007-2017, The Tor Project, Inc. */
4 /* See LICENSE for licensing information */
6 #include "orconfig.h"
8 #define CRYPTO_OPE_PRIVATE
10 #include "lib/cc/compat_compiler.h"
11 #include "lib/crypt_ops/crypto_ope.h"
12 #include "lib/crypt_ops/crypto_cipher.h"
13 #include "lib/encoding/binascii.h"
14 #include "lib/malloc/malloc.h"
15 #include "test/test.h"
16 #include "tinytest.h"
18 #include <stddef.h>
19 #include <string.h>
21 static void
22 test_crypto_ope_consistency(void *arg)
24 (void)arg;
26 crypto_ope_t *ope = NULL;
27 crypto_cipher_t *aes = NULL;
28 const int TEST_VALS[] = { 5, 500, 1023, 1024, 1025, 2046, 2047, 2048, 2049,
29 10000, OPE_INPUT_MAX };
30 unsigned i;
31 const uint8_t key[32] = "A fixed key, chosen arbitrarily.";
33 ope = crypto_ope_new(key);
34 tt_assert(ope);
36 uint64_t last_val = 0;
37 for (i = 0; i < ARRAY_LENGTH(TEST_VALS); ++i) {
38 aes = ope_get_cipher(ope, 0);
39 int val = TEST_VALS[i];
40 uint64_t v1 = crypto_ope_encrypt(ope, val);
41 uint64_t v2 = sum_values_from_cipher(aes, val);
42 tt_u64_op(v1, OP_EQ, v2);
43 tt_u64_op(v2, OP_GT, last_val);
44 last_val = v2;
45 crypto_cipher_free(aes);
48 done:
49 crypto_cipher_free(aes);
50 crypto_ope_free(ope);
53 static void
54 test_crypto_ope_oob(void *arg)
56 (void)arg;
58 crypto_ope_t *ope = NULL;
59 const uint8_t key[32] = "A fixed key, chosen arbitrarily.";
60 ope = crypto_ope_new(key);
62 tt_u64_op(UINT64_MAX, OP_EQ, crypto_ope_encrypt(ope,INT_MIN));
63 tt_u64_op(UINT64_MAX, OP_EQ, crypto_ope_encrypt(ope,-100));
64 tt_u64_op(UINT64_MAX, OP_EQ, crypto_ope_encrypt(ope,0));
65 tt_u64_op(UINT64_MAX, OP_NE, crypto_ope_encrypt(ope,1));
66 tt_u64_op(UINT64_MAX, OP_NE, crypto_ope_encrypt(ope,7000));
67 tt_u64_op(UINT64_MAX, OP_NE, crypto_ope_encrypt(ope,OPE_INPUT_MAX));
68 tt_u64_op(UINT64_MAX, OP_EQ, crypto_ope_encrypt(ope,OPE_INPUT_MAX+1));
69 tt_u64_op(UINT64_MAX, OP_EQ, crypto_ope_encrypt(ope,INT_MAX));
70 done:
71 crypto_ope_free(ope);
74 static const char OPE_TEST_KEY[] =
75 "19e05891d55232c08c2cad91d612fdb9cbd6691949a0742434a76c80bc6992fe";
77 /* generated by a separate python implementation. */
78 static const struct {
79 int v;
80 uint64_t r;
81 } OPE_TEST_VECTORS[] = {
82 { 121132, UINT64_C(3971694514) },
83 { 82283, UINT64_C(2695743564) },
84 { 72661, UINT64_C(2381548866) },
85 { 72941, UINT64_C(2390408421) },
86 { 123122, UINT64_C(4036781069) },
87 { 12154, UINT64_C(402067100) },
88 { 121574, UINT64_C(3986197593) },
89 { 11391, UINT64_C(376696838) },
90 { 65845, UINT64_C(2161801517) },
91 { 86301, UINT64_C(2828270975) },
92 { 61284, UINT64_C(2013616892) },
93 { 70505, UINT64_C(2313368870) },
94 { 30438, UINT64_C(1001394664) },
95 { 60150, UINT64_C(1977329668) },
96 { 114800, UINT64_C(3764946628) },
97 { 109403, UINT64_C(3585352477) },
98 { 21893, UINT64_C(721388468) },
99 { 123569, UINT64_C(4051780471) },
100 { 95617, UINT64_C(3134921876) },
101 { 48561, UINT64_C(1597596985) },
102 { 53334, UINT64_C(1753691710) },
103 { 92746, UINT64_C(3040874493) },
104 { 7110, UINT64_C(234966492) },
105 { 9612, UINT64_C(318326551) },
106 { 106958, UINT64_C(3506124249) },
107 { 46889, UINT64_C(1542219146) },
108 { 87790, UINT64_C(2877361609) },
109 { 68878, UINT64_C(2260369112) },
110 { 47917, UINT64_C(1576681737) },
111 { 121128, UINT64_C(3971553290) },
112 { 108602, UINT64_C(3559176081) },
113 { 28217, UINT64_C(929692460) },
114 { 69498, UINT64_C(2280554161) },
115 { 63870, UINT64_C(2098322675) },
116 { 57542, UINT64_C(1891698992) },
117 { 122148, UINT64_C(4004515805) },
118 { 46254, UINT64_C(1521227949) },
119 { 42850, UINT64_C(1408996941) },
120 { 92661, UINT64_C(3037901517) },
121 { 57720, UINT64_C(1897369989) },
124 static void
125 test_crypto_ope_vectors(void *arg)
127 (void)arg;
128 uint8_t key[32];
129 crypto_ope_t *ope = NULL, *ope2 = NULL;
131 base16_decode((char*)key, 32, OPE_TEST_KEY, strlen(OPE_TEST_KEY));
133 ope = crypto_ope_new(key);
134 key[8] += 1;
135 ope2 = crypto_ope_new(key);
136 unsigned i;
137 for (i = 0; i < ARRAY_LENGTH(OPE_TEST_VECTORS); ++i) {
138 int val = OPE_TEST_VECTORS[i].v;
139 uint64_t res = OPE_TEST_VECTORS[i].r;
141 tt_u64_op(crypto_ope_encrypt(ope, val), OP_EQ, res);
142 tt_u64_op(crypto_ope_encrypt(ope2, val), OP_NE, res);
144 done:
145 crypto_ope_free(ope);
146 crypto_ope_free(ope2);
149 struct testcase_t crypto_ope_tests[] = {
150 { "consistency", test_crypto_ope_consistency, 0, NULL, NULL },
151 { "oob", test_crypto_ope_oob, 0, NULL, NULL },
152 { "vectors", test_crypto_ope_vectors, 0, NULL, NULL },
153 END_OF_TESTCASES