LCOV - code coverage report
Current view: top level - test - test_crypto_ope.c (source / functions) Hit Total Coverage
Test: lcov.info Lines: 56 56 100.0 %
Date: 2021-11-24 03:28:48 Functions: 3 3 100.0 %

          Line data    Source code
       1             : /* Copyright (c) 2001-2004, Roger Dingledine.
       2             :  * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
       3             :  * Copyright (c) 2007-2021, The Tor Project, Inc. */
       4             : /* See LICENSE for licensing information */
       5             : 
       6             : #include "orconfig.h"
       7             : 
       8             : #define CRYPTO_OPE_PRIVATE
       9             : 
      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"
      17             : 
      18             : #include <stddef.h>
      19             : #include <string.h>
      20             : 
      21             : static void
      22           1 : test_crypto_ope_consistency(void *arg)
      23             : {
      24           1 :   (void)arg;
      25             : 
      26           1 :   crypto_ope_t *ope = NULL;
      27           1 :   crypto_cipher_t *aes = NULL;
      28           1 :   const int TEST_VALS[] = { 5, 500, 1023, 1024, 1025, 2046, 2047, 2048, 2049,
      29             :                             10000, OPE_INPUT_MAX };
      30           1 :   unsigned i;
      31           1 :   const uint8_t key[32] = "A fixed key, chosen arbitrarily.";
      32             : 
      33           1 :   ope = crypto_ope_new(key);
      34           1 :   tt_assert(ope);
      35             : 
      36             :   uint64_t last_val = 0;
      37          12 :   for (i = 0; i < ARRAY_LENGTH(TEST_VALS); ++i) {
      38          11 :     aes = ope_get_cipher(ope, 0);
      39          11 :     int val = TEST_VALS[i];
      40          11 :     uint64_t v1 = crypto_ope_encrypt(ope, val);
      41          11 :     uint64_t v2 = sum_values_from_cipher(aes, val);
      42          11 :     tt_u64_op(v1, OP_EQ, v2);
      43          11 :     tt_u64_op(v2, OP_GT, last_val);
      44          11 :     last_val = v2;
      45          11 :     crypto_cipher_free(aes);
      46             :   }
      47             : 
      48           1 :  done:
      49           1 :   crypto_cipher_free(aes);
      50           1 :   crypto_ope_free(ope);
      51           1 : }
      52             : 
      53             : static void
      54           1 : test_crypto_ope_oob(void *arg)
      55             : {
      56           1 :   (void)arg;
      57             : 
      58           1 :   crypto_ope_t *ope = NULL;
      59           1 :   const uint8_t key[32] = "A fixed key, chosen arbitrarily.";
      60           1 :   ope = crypto_ope_new(key);
      61             : 
      62           1 :   tt_u64_op(UINT64_MAX, OP_EQ, crypto_ope_encrypt(ope,INT_MIN));
      63           1 :   tt_u64_op(UINT64_MAX, OP_EQ, crypto_ope_encrypt(ope,-100));
      64           1 :   tt_u64_op(UINT64_MAX, OP_EQ, crypto_ope_encrypt(ope,0));
      65           1 :   tt_u64_op(UINT64_MAX, OP_NE, crypto_ope_encrypt(ope,1));
      66           1 :   tt_u64_op(UINT64_MAX, OP_NE, crypto_ope_encrypt(ope,7000));
      67           1 :   tt_u64_op(UINT64_MAX, OP_NE, crypto_ope_encrypt(ope,OPE_INPUT_MAX));
      68           1 :   tt_u64_op(UINT64_MAX, OP_EQ, crypto_ope_encrypt(ope,OPE_INPUT_MAX+1));
      69           1 :   tt_u64_op(UINT64_MAX, OP_EQ, crypto_ope_encrypt(ope,INT_MAX));
      70           1 :  done:
      71           1 :   crypto_ope_free(ope);
      72           1 : }
      73             : 
      74             : static const char OPE_TEST_KEY[] =
      75             :   "19e05891d55232c08c2cad91d612fdb9cbd6691949a0742434a76c80bc6992fe";
      76             : 
      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) },
     122             : };
     123             : 
     124             : static void
     125           1 : test_crypto_ope_vectors(void *arg)
     126             : {
     127           1 :   (void)arg;
     128           1 :   uint8_t key[32];
     129           1 :   crypto_ope_t *ope = NULL, *ope2 = NULL;
     130             : 
     131           1 :   base16_decode((char*)key, 32, OPE_TEST_KEY, strlen(OPE_TEST_KEY));
     132             : 
     133           1 :   ope = crypto_ope_new(key);
     134           1 :   key[8] += 1;
     135           1 :   ope2 = crypto_ope_new(key);
     136           1 :   unsigned i;
     137          42 :   for (i = 0; i < ARRAY_LENGTH(OPE_TEST_VECTORS); ++i) {
     138          40 :     int val = OPE_TEST_VECTORS[i].v;
     139          40 :     uint64_t res = OPE_TEST_VECTORS[i].r;
     140             : 
     141          40 :     tt_u64_op(crypto_ope_encrypt(ope, val), OP_EQ, res);
     142          40 :     tt_u64_op(crypto_ope_encrypt(ope2, val), OP_NE, res);
     143             :   }
     144           1 :  done:
     145           1 :   crypto_ope_free(ope);
     146           1 :   crypto_ope_free(ope2);
     147           1 : }
     148             : 
     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
     154             : };

Generated by: LCOV version 1.14