test_anastasis_crypto.c (12452B)
1 /* 2 This file is part of Anastasis 3 Copyright (C) 2014-2020 Anastasis SARL 4 5 Anastasis is free software; you can redistribute it and/or modify 6 it under the terms of the GNU General Public License as 7 published by the Free Software Foundation; either version 3, or 8 (at your option) any later version. 9 10 Anastasis is distributed in the hope that it will be useful, but 11 WITHOUT ANY WARRANTY; without even the implied warranty of 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 GNU General Public License for more details. 14 15 You should have received a copy of the GNU General Public 16 License along with Anastasis; see the file COPYING. If not, see 17 <http://www.gnu.org/licenses/> 18 */ 19 20 /** 21 * @file lib/test_anastasis_api.c 22 * @brief testcase to test anastasis' HTTP API interface 23 * @author Christian Grothoff 24 * @author Dennis Neufeld 25 * @author Dominik Meister 26 */ 27 #include "platform.h" 28 #include <taler/taler_util.h> 29 #include <gnunet/gnunet_util_lib.h> 30 #include "anastasis_crypto_lib.h" 31 32 /** 33 * Testing derivation of the user identifier 34 */ 35 static int 36 test_user_identifier_derive (void) 37 { 38 json_t *id_data_1; 39 json_t *id_data_2; 40 json_t *id_data_3; 41 struct ANASTASIS_CRYPTO_UserIdentifierP id_1; 42 struct ANASTASIS_CRYPTO_UserIdentifierP id_2; 43 struct ANASTASIS_CRYPTO_UserIdentifierP id_3; 44 struct ANASTASIS_CRYPTO_ProviderSaltP provider_salt; 45 46 const char *salt_str = "Server-Salt-Test"; 47 48 GNUNET_memcpy (&provider_salt, 49 salt_str, 50 strlen (salt_str)); 51 // sample data 1 52 id_data_1 = json_object (); 53 json_object_set_new (id_data_1, "arg1", json_string ("Hallo")); 54 // sample data 2, equal to sample data 1 55 id_data_2 = json_object (); 56 json_object_set_new (id_data_2, "arg1", json_string ("Hallo")); 57 // sample data 3, differs 58 id_data_3 = json_object (); 59 json_object_set_new (id_data_3, "arg1", json_string ("Hallo2")); 60 61 ANASTASIS_CRYPTO_user_identifier_derive (id_data_1, 62 &provider_salt, 63 &id_1); 64 ANASTASIS_CRYPTO_user_identifier_derive (id_data_2, 65 &provider_salt, 66 &id_2); 67 ANASTASIS_CRYPTO_user_identifier_derive (id_data_3, 68 &provider_salt, 69 &id_3); 70 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 71 "UserIdentifier_1: %s\n", 72 TALER_B2S (&id_1)); 73 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 74 "UserIdentifier_2: %s\n", 75 TALER_B2S (&id_2)); 76 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 77 "UserIdentifier_3: %s\n", 78 TALER_B2S (&id_3)); 79 GNUNET_assert (0 == GNUNET_memcmp (&id_1, &id_2)); 80 GNUNET_assert (0 != GNUNET_memcmp (&id_1, &id_3)); 81 json_decref (id_data_1); 82 json_decref (id_data_2); 83 json_decref (id_data_3); 84 return 0; 85 } 86 87 88 /** 89 * Testing the encryption of an recovery document and the 90 * decryption of the encrypted recovery document 91 */ 92 static int 93 test_recovery_document (void) 94 { 95 void *ciphertext; 96 size_t size_ciphertext; 97 void *plaintext; 98 size_t size_plaintext; 99 struct ANASTASIS_CRYPTO_UserIdentifierP id; 100 struct ANASTASIS_CRYPTO_ProviderSaltP provider_salt; 101 int ret; 102 103 json_t *id_data = json_object (); 104 const char *test = "TEST_ERD"; 105 const char *salt_str = "Server-Salt-Test"; 106 107 GNUNET_memcpy (&provider_salt, 108 salt_str, 109 strlen (salt_str)); 110 json_object_set_new (id_data, "arg1", json_string ("ID_DATA")); 111 ANASTASIS_CRYPTO_user_identifier_derive (id_data, 112 &provider_salt, 113 &id); 114 ANASTASIS_CRYPTO_recovery_document_encrypt (&id, 115 test, 116 strlen (test), 117 &ciphertext, 118 &size_ciphertext); 119 120 GNUNET_assert (GNUNET_OK == 121 ANASTASIS_CRYPTO_recovery_document_decrypt (&id, 122 ciphertext, 123 size_ciphertext, 124 &plaintext, 125 &size_plaintext)); 126 GNUNET_assert (strlen (test) == size_plaintext); 127 ret = strncmp (plaintext, test, strlen (test)); 128 json_decref (id_data); 129 GNUNET_free (ciphertext); 130 GNUNET_free (plaintext); 131 return ret; 132 } 133 134 135 static int 136 test_key_share (void) 137 { 138 struct ANASTASIS_CRYPTO_EncryptedKeyShareP ciphertext; 139 struct ANASTASIS_CRYPTO_KeyShareP plaintext; 140 struct ANASTASIS_CRYPTO_UserIdentifierP id; 141 struct ANASTASIS_CRYPTO_KeyShareP key_share; 142 struct ANASTASIS_CRYPTO_KeyShareP key_share_1; 143 struct ANASTASIS_CRYPTO_KeyShareP key_share_2; 144 145 // testing creation of keyshares 146 ANASTASIS_CRYPTO_keyshare_create (&key_share_1); 147 ANASTASIS_CRYPTO_keyshare_create (&key_share_2); 148 GNUNET_assert (0 != 149 GNUNET_memcmp (&key_share_1, 150 &key_share_2)); 151 152 // testing of enc-/decryption of a keyshare 153 GNUNET_CRYPTO_random_block (&id, 154 sizeof (struct ANASTASIS_CRYPTO_UserIdentifierP)); 155 ANASTASIS_CRYPTO_keyshare_create (&key_share); 156 ANASTASIS_CRYPTO_keyshare_encrypt (&key_share, 157 &id, 158 NULL, 159 &ciphertext); 160 GNUNET_assert (GNUNET_OK == 161 ANASTASIS_CRYPTO_keyshare_decrypt (&ciphertext, 162 &id, 163 NULL, 164 &plaintext)); 165 return GNUNET_memcmp (&key_share, 166 &plaintext); 167 } 168 169 170 static int 171 test_truth (void) 172 { 173 const char *test = "TEST_TRUTH"; 174 void *ciphertext; 175 size_t size_ciphertext; 176 void *plaintext; 177 size_t size_plaintext; 178 struct ANASTASIS_CRYPTO_TruthKeyP truth_enc_key; 179 int ret; 180 struct ANASTASIS_CRYPTO_NonceP nonce; 181 182 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 183 "TRUTH_BEFORE: %s\n", 184 TALER_b2s (test, 185 strlen (test))); 186 GNUNET_CRYPTO_random_block (&truth_enc_key, 187 sizeof (struct ANASTASIS_CRYPTO_TruthKeyP)); 188 GNUNET_CRYPTO_random_block (&nonce, 189 sizeof (nonce)); 190 ANASTASIS_CRYPTO_truth_encrypt (&nonce, 191 &truth_enc_key, 192 test, 193 strlen (test), 194 &ciphertext, 195 &size_ciphertext); 196 197 GNUNET_assert (GNUNET_OK == 198 ANASTASIS_CRYPTO_truth_decrypt (&truth_enc_key, 199 ciphertext, 200 size_ciphertext, 201 &plaintext, 202 &size_plaintext)); 203 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 204 "TRUTH_AFTER: %s\n", 205 TALER_b2s (plaintext, size_plaintext)); 206 GNUNET_assert (strlen (test) == size_plaintext); 207 ret = strncmp (plaintext, test, strlen (test)); 208 GNUNET_free (ciphertext); 209 GNUNET_free (plaintext); 210 return ret; 211 } 212 213 214 static int 215 test_core_secret (void) 216 { 217 const char *test = "TEST_CORE_SECRET"; 218 const char *test_wrong = "TEST_CORE_WRONG"; 219 unsigned int policy_keys_length = 5; 220 struct ANASTASIS_CRYPTO_MasterSaltP salt; 221 struct ANASTASIS_CoreSecretEncryptionResult *cser; 222 struct ANASTASIS_CRYPTO_PolicyKeyP policy_keys[policy_keys_length]; 223 224 GNUNET_CRYPTO_random_block (&salt, 225 sizeof (salt)); 226 // construction of PolicyKey-array 227 for (unsigned int i = 0; i < policy_keys_length; i++) 228 { 229 // construction of KeyShare-array 230 unsigned int keyshare_length = 5; 231 struct ANASTASIS_CRYPTO_KeyShareP keyshares[keyshare_length]; 232 for (unsigned int j = 0; j < keyshare_length; j++) 233 { 234 ANASTASIS_CRYPTO_keyshare_create (&keyshares[j]); 235 if (j > 0) 236 GNUNET_assert (0 != 237 GNUNET_memcmp (&keyshares[j - 1], &keyshares[j])); 238 } 239 240 // derive policy-keys 241 ANASTASIS_CRYPTO_policy_key_derive ((struct 242 ANASTASIS_CRYPTO_KeyShareP *) 243 keyshares, 244 keyshare_length, 245 &salt, 246 &policy_keys[i]); 247 if (i > 0) 248 GNUNET_assert (0 != 249 GNUNET_memcmp (&policy_keys[i - 1], &policy_keys[i])); 250 } 251 252 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 253 "CORE_SECRET_BEFORE: %s\n", 254 TALER_b2s (test, strlen (test))); 255 256 // test encryption of core_secret 257 cser = ANASTASIS_CRYPTO_core_secret_encrypt (policy_keys, 258 policy_keys_length, 259 test, 260 strlen (test)); 261 262 // test recover of core secret 263 for (unsigned int k = 0; k < policy_keys_length; k++) 264 { 265 void *dec_core_secret; 266 size_t core_secret_size; 267 268 GNUNET_assert (GNUNET_OK == 269 ANASTASIS_CRYPTO_core_secret_recover ( 270 cser->enc_master_keys[k], 271 cser->enc_master_key_sizes[k], 272 &policy_keys[k], 273 cser->enc_core_secret, 274 cser->enc_core_secret_size, 275 &dec_core_secret, 276 &core_secret_size)); 277 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 278 "CORE_SECRET_AFTER_%i: %s\n", 279 k, 280 TALER_b2s (dec_core_secret, strlen (test))); 281 GNUNET_assert (strlen (test) == core_secret_size); 282 GNUNET_assert (0 == 283 strncmp (dec_core_secret, test, strlen (test))); 284 GNUNET_assert (0 != 285 strncmp (dec_core_secret, test_wrong, strlen ( 286 test))); 287 GNUNET_free (dec_core_secret); 288 } 289 /* recovering with a key that does not belong to the ciphertext must fail 290 instead of returning garbage as if it had succeeded */ 291 { 292 void *dec_core_secret; 293 size_t core_secret_size; 294 struct ANASTASIS_CRYPTO_PolicyKeyP wrong_key; 295 296 GNUNET_CRYPTO_random_block (&wrong_key, 297 sizeof (wrong_key)); 298 GNUNET_assert (GNUNET_OK != 299 ANASTASIS_CRYPTO_core_secret_recover ( 300 cser->enc_master_keys[0], 301 cser->enc_master_key_sizes[0], 302 &wrong_key, 303 cser->enc_core_secret, 304 cser->enc_core_secret_size, 305 &dec_core_secret, 306 &core_secret_size)); 307 GNUNET_assert (NULL == dec_core_secret); 308 } 309 ANASTASIS_CRYPTO_destroy_encrypted_core_secret (cser); 310 return 0; 311 } 312 313 314 static int 315 test_public_key_derive (void) 316 { 317 struct ANASTASIS_CRYPTO_UserIdentifierP id; 318 struct ANASTASIS_CRYPTO_AccountPublicKeyP pub_key; 319 struct ANASTASIS_CRYPTO_ProviderSaltP provider_salt; 320 json_t *id_data = json_object (); 321 const char *salt_str = "Server-Salt-Test"; 322 323 GNUNET_memcpy (&provider_salt, 324 salt_str, 325 strlen (salt_str)); 326 327 json_object_set_new (id_data, "arg1", json_string ("ID_DATA")); 328 ANASTASIS_CRYPTO_user_identifier_derive (id_data, 329 &provider_salt, 330 &id); 331 332 ANASTASIS_CRYPTO_account_public_key_derive (&id, 333 &pub_key); 334 // FIXME: write a real test, e.g. signing and verification 335 json_decref (id_data); 336 return 0; 337 } 338 339 340 int 341 main (int argc, 342 const char *const argv[]) 343 { 344 GNUNET_log_setup (argv[0], "DEBUG", NULL); 345 if (0 != test_recovery_document ()) 346 return 1; 347 if (0 != test_user_identifier_derive ()) 348 return 1; 349 if (0 != test_key_share ()) 350 return 1; 351 if (0 != test_truth ()) 352 return 1; 353 if (0 != test_core_secret ()) 354 return 1; 355 if (0 != test_public_key_derive ()) 356 return 1; 357 return 0; 358 } 359 360 361 /* end of test_anastasis_crypto.c */