aboutsummaryrefslogtreecommitdiff
path: root/src/util/tv_age_restriction.c
blob: 9fc2b48238daae7737ee2011e9ae8988e6d626d9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
/**
 * @file util/tv_age_restriction.c
 * @brief Generate test vectors for age restriction
 * @author Özgür Kesim
 *
 * compile in exchange/src/util with
 *
 * gcc tv_age_restriction.c \
 *    -lgnunetutil -lgnunetjson -lsodium -ljansson \
 *    -L/usr/lib/x86_64-linux-gnu -lmicrohttpd -ltalerutil \
 *    -I../include \
 *    -o tv_age_restriction
 *
 */
#include "platform.h"
#include <gnunet/gnunet_json_lib.h>
#include <gnunet/gnunet_util_lib.h>
#include <jansson.h>
#include <taler/taler_util.h>
#include <taler/taler_crypto_lib.h>

static struct TALER_AgeMask age_masks[] = {
  { .bits = 1
            | 1 << 8 | 1 << 14 | 1 << 18 },
  { .bits = 1
            | 1 << 8 | 1 << 10 | 1 << 12
            | 1 << 14 | 1 << 16 | 1 << 18 | 1 << 21 },
};

extern uint8_t
get_age_group (
  const struct TALER_AgeMask *mask,
  uint8_t age);

/**
 * Encodes the age mask into a string, like "8:10:12:14:16:18:21"
 */
char *
age_mask_to_string (
  const struct TALER_AgeMask *mask)
{
  uint32_t bits = mask->bits;
  unsigned int n = 0;
  char *buf = GNUNET_malloc (32 * 3); // max characters possible
  char *pos = buf;

  if (NULL == buf)
  {
    return buf;
  }

  while (bits != 0)
  {
    bits >>= 1;
    n++;
    if (0 == (bits & 1))
    {
      continue;
    }

    if (n > 9)
    {
      *(pos++) = '0' + n / 10;
    }
    *(pos++) = '0' + n % 10;

    if (0 != (bits >> 1))
    {
      *(pos++) = ':';
    }
  }
  return buf;
}


static json_t *
cp_to_j (
  const struct GNUNET_HashCode *seed,
  struct TALER_AgeCommitmentProof *acp,
  uint8_t seq)
{
  json_t *j_commitment;
  json_t *j_proof;
  json_t *j_pubs;
  json_t *j_privs;
  struct TALER_AgeCommitmentHash hac = {0};
  char buf[256] = {0};

  TALER_age_commitment_hash (&acp->commitment, &hac);

  j_pubs = json_array ();
  GNUNET_assert (NULL != j_pubs);
  for (unsigned int i = 0; i < acp->commitment.num; i++)
  {
    json_t *j_pub = GNUNET_JSON_PACK (
      GNUNET_JSON_pack_data_auto (NULL,
                                  &acp->commitment.keys[i]));
    json_array_append_new (j_pubs, j_pub);
  }

  j_commitment = GNUNET_JSON_PACK (
    GNUNET_JSON_pack_uint64 ("num", acp->commitment.num),
    GNUNET_JSON_pack_array_steal ("edx25519_pubs", j_pubs),
    GNUNET_JSON_pack_data_auto ("h_age_commitment", &hac));


  j_privs = json_array ();
  GNUNET_assert (NULL != j_privs);
  for (unsigned int i = 0; i < acp->proof.num; i++)
  {
    json_t *j_priv = GNUNET_JSON_PACK (
      GNUNET_JSON_pack_data_auto (NULL,
                                  &acp->proof.keys[i]));
    json_array_append_new (j_privs, j_priv);
  }
  j_proof = GNUNET_JSON_PACK (
    GNUNET_JSON_pack_uint64 ("num", acp->proof.num),
    GNUNET_JSON_pack_array_steal ("edx25519_privs", j_privs));

  if (0 == seq)
  {
    strcpy (buf, "commit()");
  }
  else
  {
    sprintf (buf,
             "derive_from(%d)",
             seq);
  }

  return GNUNET_JSON_PACK (
    GNUNET_JSON_pack_string ("generated_by", buf),
    GNUNET_JSON_pack_data_auto ("seed", seed),
    GNUNET_JSON_pack_object_steal ("proof", j_proof),
    GNUNET_JSON_pack_object_steal ("commitment", j_commitment));

};

static json_t *
generate (
  struct TALER_AgeMask *mask)
{
  uint8_t age;
  json_t *j_commitproofs;
  j_commitproofs = json_array ();

  for (age = 0; age < 24; age += 2)
  {
    json_t *j_top = json_object ();
    json_t *j_seq = json_array ();
    enum GNUNET_GenericReturnValue ret;
    struct TALER_AgeCommitmentProof acp[3] = {0};
    uint8_t age_group = get_age_group (mask, age);
    struct GNUNET_HashCode seed;

    GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_WEAK,
                                &seed,
                                sizeof(seed));

    json_object_set (j_top,
                     "committed_age",
                     json_integer (age));

    ret = TALER_age_restriction_commit (mask,
                                        age,
                                        &seed,
                                        &acp[0]);

    GNUNET_assert (GNUNET_OK == ret);

    /* Also derive two more commitments right away */
    for (uint8_t i = 0; i<2; i++)
    {
      struct GNUNET_HashCode salt;
      GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_WEAK,
                                  &salt,
                                  sizeof (salt));
      GNUNET_assert (GNUNET_OK ==
                     TALER_age_commitment_derive (&acp[i],
                                                  &salt,
                                                  &acp[i + 1]));
    }

    for (uint8_t i = 0; i < 3; i++)
    {
      json_t *j_cp = cp_to_j (&seed, &acp[i], i);
      json_t *j_attestations = json_array ();

      for (uint8_t min = 0; min < 22; min++)
      {
        json_t *j_attest = json_object ();
        json_t *j_reason;
        uint8_t min_group = get_age_group (mask, min);
        struct TALER_AgeAttestation at = {0};

        json_object_set (j_attest,
                         "required_minimum_age",
                         json_integer (min));
        json_object_set (j_attest,
                         "calculated_age_group",
                         json_integer (min_group));

        ret = TALER_age_commitment_attest (&acp[i],
                                           min,
                                           &at);


        if (0 == min_group)
          j_reason =  json_string (
            "not required: age group is 0");
        else if (min_group > age_group)
          j_reason = json_string (
            "not applicable: committed age too small");
        else
          j_reason = GNUNET_JSON_PACK (
            GNUNET_JSON_pack_data_auto (NULL, &at));

        json_object_set (j_attest,
                         "attestation",
                         j_reason);

        json_array_append_new (j_attestations,
                               j_attest);

      }

      json_object_set (j_cp, "attestations", j_attestations);
      json_array_append (j_seq,  j_cp);

      TALER_age_commitment_proof_free (&acp[i]);
    }

    json_object_set (j_top, "commitment_proof_attestation_seq", j_seq);
    json_array_append_new (j_commitproofs, j_top);
  }

  return j_commitproofs;
}


int
main (int argc,
      const char *const argv[])
{
  (void) argc;
  (void) argv;
  json_t *j_data = json_array ();
  for (unsigned int i = 0; i < 2; i++)
  {
    struct TALER_AgeMask mask = age_masks[i];
    json_t *j_test = json_object ();
    json_object_set (j_test,
                     "age_groups",
                     json_string (age_mask_to_string (&mask)));
    json_object_set (j_test,
                     "age_mask",
                     json_integer (mask.bits));
    json_object_set (j_test,
                     "test_data",
                     generate (&mask));
    json_array_append_new (j_data, j_test);
  }
  printf ("%s\n", json_dumps (j_data, JSON_INDENT (2)
                              | JSON_COMPACT));

  json_decref (j_data);
  return 0;
}


/* end of tv_age_restriction.c */