libqaeda

Unnamed repository; edit this file 'description' to name the repository.
Info | Log | Files | Refs | README | LICENSE

test_crypto.c (9680B)


      1 #include <check.h>
      2 #include <stdlib.h>
      3 #include <string.h>
      4 
      5 #include <rerr.h>
      6 
      7 #include "lq/crypto.h"
      8 #include "lq/config.h"
      9 #include "lq/io.h"
     10 #include "lq/base.h"
     11 #include "lq/err.h"
     12 
     13 
     14 const char *data = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.";
     15 const char *salt = "spamspamspamspamspamspamspamspam";
     16 
     17 //// sha256sum "foo" 2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae
     18 //static const char privkeydata[32] = {
     19 //	0x2c, 0x26, 0xb4, 0x6b, 0x68, 0xff, 0xc6, 0x8f,
     20 //	0xf9, 0x9b, 0x45, 0x3c, 0x1d, 0x30, 0x41, 0x34,
     21 //	0x13, 0x42, 0x2d, 0x70, 0x64, 0x83, 0xbf, 0xa0,
     22 //	0xf9, 0x8a, 0x5e, 0x88, 0x62, 0x66, 0xe7, 0xae,
     23 //};
     24 
     25 // "1234"
     26 static const size_t passphrase_len = 4;
     27 static const char passphrase[5] = {
     28 	0x31, 0x32, 0x33, 0x34, 0x00,
     29 };
     30 // "11111"
     31 static const size_t passphrase_two_len = 4;
     32 static const char passphrase_two[6] = {
     33 	0x31, 0x31, 0x31, 0x31, 0x31, 0x00,
     34 };
     35 
     36 
     37 struct dummycrypto {
     38 	void *data; ///< Literal private key data.
     39 	size_t len; ///< Length of private key data.
     40 };
     41 
     42 START_TEST(check_digest) {
     43 	int r;
     44 	char out[LQ_DIGEST_LEN];
     45 
     46 	r = lq_digest(data, strlen(data), (char*)out);
     47 	ck_assert(r == 0);
     48 }
     49 END_TEST
     50 
     51 START_TEST(check_privatekey) {
     52 	int r;
     53 	char path[LQ_PATH_MAX];
     54 	LQPrivKey *pk;
     55 
     56 	lq_cpy(path, "/tmp/lqcrypto_test_XXXXXX", 26);
     57 	r = lq_crypto_init(mktempdir(path));
     58 	ck_assert_int_eq(r, ERR_OK);
     59 
     60 	pk = lq_privatekey_new(passphrase, passphrase_len);
     61 	ck_assert_ptr_nonnull(pk);
     62 	lq_privatekey_free(pk);
     63 
     64 	lq_crypto_free();
     65 }
     66 END_TEST
     67 
     68 START_TEST(check_privatekey_lock) {
     69 	int r;
     70 	char path[LQ_PATH_MAX];
     71 	LQPrivKey *pk;
     72 	
     73 	lq_cpy(path, "/tmp/lqcrypto_test_XXXXXX", 26);
     74 	r = lq_crypto_init(mktempdir(path));
     75 	ck_assert_int_eq(r, ERR_OK);
     76 
     77 	pk = lq_privatekey_new(passphrase, passphrase_len);
     78 	ck_assert_ptr_nonnull(pk);
     79 
     80 	lq_privatekey_lock(pk, passphrase, passphrase_len);
     81 	r = lq_privatekey_lock(pk, passphrase, passphrase_len);
     82 	ck_assert_int_eq(r, ERR_NOOP);
     83 
     84 	r = lq_privatekey_unlock(pk, passphrase, passphrase_len);
     85 	ck_assert_int_eq(r, ERR_OK);
     86 
     87 	r = lq_privatekey_unlock(pk, passphrase, passphrase_len);
     88 	ck_assert_int_eq(r, ERR_NOOP);
     89 
     90 	r = lq_privatekey_lock(pk, passphrase, passphrase_len);
     91 	ck_assert_int_eq(r, ERR_OK);
     92 
     93 	r = lq_privatekey_lock(pk, passphrase, passphrase_len);
     94 	ck_assert_int_eq(r, ERR_NOOP);
     95 
     96 	lq_privatekey_free(pk);
     97 	lq_crypto_free();
     98 }
     99 END_TEST
    100 
    101 START_TEST(check_publickey) {
    102 	int r;
    103 	char path[LQ_PATH_MAX];
    104 	LQPrivKey *pk;
    105 	LQPubKey *pubk;
    106 	LQPubKey *pubk_manual;
    107 	char *keydata;
    108 	char *keydata_manual;
    109 
    110 	lq_cpy(path, "/tmp/lqcrypto_test_XXXXXX", 26);
    111 	r = lq_crypto_init(mktempdir(path));
    112 	ck_assert_int_eq(r, ERR_OK);
    113 
    114 	pk = lq_privatekey_new(passphrase, passphrase_len);
    115 	ck_assert_ptr_nonnull(pk);
    116 	pubk = lq_publickey_from_privatekey(pk);
    117 	lq_publickey_bytes(pubk, &keydata);
    118 	pubk_manual = lq_publickey_new(keydata);
    119 	lq_publickey_bytes(pubk_manual, &keydata_manual);
    120 	ck_assert_mem_eq(keydata_manual, keydata, 65);
    121 	lq_publickey_free(pubk_manual);
    122 	lq_publickey_free(pubk);
    123 	lq_privatekey_free(pk);
    124 
    125 	lq_crypto_free();
    126 }
    127 END_TEST
    128 
    129 START_TEST(check_signature) {
    130 	int r;
    131 	char path[LQ_PATH_MAX];
    132 	char digest[32];
    133 	LQPrivKey *pk;
    134 	LQSig *sig;
    135 	char *sigdata;
    136 
    137 	lq_cpy(path, "/tmp/lqcrypto_test_XXXXXX", 26);
    138 	r = lq_crypto_init(mktempdir(path));
    139 	ck_assert_int_eq(r, ERR_OK);
    140 
    141 	pk = lq_privatekey_new(passphrase, passphrase_len);
    142 	ck_assert_ptr_nonnull(pk);
    143 	sig = lq_privatekey_sign(pk, data, strlen(data), salt);
    144 	ck_assert_ptr_null(sig);
    145 
    146 	r = lq_privatekey_unlock(pk, passphrase, 32);
    147 	ck_assert_int_eq(r, 0);
    148 
    149 	sig = lq_privatekey_sign(pk, digest, 32, salt);
    150 	ck_assert_ptr_nonnull(sig);
    151 
    152 	r = lq_signature_bytes(sig, &sigdata);
    153 	ck_assert_mem_eq(sig->impl, sigdata, r);
    154 
    155 	lq_signature_free(sig);
    156 	lq_privatekey_free(pk);
    157 
    158 	lq_crypto_free();
    159 }
    160 END_TEST
    161 
    162 START_TEST(check_verify) {
    163 	int r;
    164 	char path[LQ_PATH_MAX];
    165 	LQPrivKey *pk;
    166 	LQSig *sig;
    167 
    168 	lq_cpy(path, "/tmp/lqcrypto_test_XXXXXX", 26);
    169 	r = lq_crypto_init(mktempdir(path));
    170 	ck_assert_int_eq(r, ERR_OK);
    171 
    172 	pk = lq_privatekey_new(passphrase, 32);
    173 	ck_assert_ptr_nonnull(pk);
    174 	sig = lq_privatekey_sign(pk, data, strlen(data), salt);
    175 	ck_assert_ptr_null(sig);
    176 
    177 	r = lq_privatekey_unlock(pk, passphrase, 32);
    178 	ck_assert_int_eq(r, 0);
    179 
    180 	sig = lq_privatekey_sign(pk, data, strlen(data), salt);
    181 	ck_assert_ptr_nonnull(sig);
    182 
    183 	r = lq_signature_verify(sig, data, strlen(data));
    184 	ck_assert_int_eq(r, 0);
    185 
    186 	lq_signature_free(sig);
    187 	lq_privatekey_free(pk);
    188 
    189 	lq_crypto_free();
    190 }
    191 END_TEST
    192 
    193 START_TEST(check_create_load) {
    194 	int r;
    195 	char path[LQ_PATH_MAX];
    196 	LQPrivKey *pk;
    197 	LQPrivKey *pk_load;
    198 
    199 	lq_cpy(path, "/tmp/lqcrypto_test_XXXXXX", 26);
    200 	r = lq_crypto_init(mktempdir(path));
    201 	ck_assert_int_eq(r, ERR_OK);
    202 
    203 	pk = lq_privatekey_new(passphrase, passphrase_len);
    204 	ck_assert_ptr_nonnull(pk);
    205 	pk_load = lq_privatekey_load(passphrase, passphrase_len, NULL);
    206 	ck_assert_ptr_nonnull(pk_load);
    207 
    208 	lq_privatekey_free(pk);
    209 
    210 	lq_crypto_free();
    211 }
    212 END_TEST
    213 
    214 START_TEST(check_load_specific) {
    215 	int r;
    216 	char path[LQ_PATH_MAX];
    217 	LQPrivKey *pk;
    218 	LQPubKey *pubk;
    219 	LQPrivKey *pk_load;
    220 	char *p;
    221 	size_t c;
    222 
    223 	lq_cpy(path, "/tmp/lqcrypto_test_XXXXXX", 26);
    224 	r = lq_crypto_init(mktempdir(path));
    225 	ck_assert_int_eq(r, ERR_OK);
    226 
    227 	pk = lq_privatekey_new(passphrase, passphrase_len);
    228 	ck_assert_ptr_nonnull(pk);
    229 	pubk = lq_publickey_from_privatekey(pk);
    230 	ck_assert_ptr_nonnull(pubk);
    231 	c = lq_publickey_fingerprint(pubk, &p);
    232 	ck_assert_int_gt(c, 0);
    233 	//pk_load = lq_privatekey_load(passphrase, passphrase_len, NULL);
    234 	//ck_assert_ptr_nonnull(pk_load);
    235 	pk_load = lq_privatekey_load(passphrase, passphrase_len, p);
    236 	ck_assert_ptr_nonnull(pk_load);
    237 
    238 	lq_privatekey_free(pk);
    239 
    240 	lq_crypto_free();
    241 }
    242 END_TEST
    243 
    244 START_TEST(check_many) {
    245 	int r;
    246 	char path[LQ_PATH_MAX];
    247 	LQPrivKey *pk;
    248 	LQPubKey *pubk;
    249 	LQPubKey *pubk_manual;
    250 	char *keydata;
    251 	char *keydata_manual;
    252 
    253 	lq_cpy(path, "/tmp/lqcrypto_test_XXXXXX", 26);
    254 	r = lq_crypto_init(mktempdir(path));
    255 	ck_assert_int_eq(r, ERR_OK);
    256 
    257 	pk = lq_privatekey_new(passphrase, passphrase_len);
    258 	ck_assert_ptr_nonnull(pk);
    259 	pubk = lq_publickey_from_privatekey(pk);
    260 	lq_publickey_bytes(pubk, &keydata);
    261 	pubk_manual = lq_publickey_new(keydata);
    262 	lq_publickey_bytes(pubk_manual, &keydata_manual);
    263 	ck_assert_mem_eq(keydata_manual, keydata, 65);
    264 	lq_publickey_free(pubk_manual);
    265 	lq_publickey_free(pubk);
    266 	lq_privatekey_free(pk);
    267 
    268 	lq_crypto_free();
    269 }
    270 END_TEST
    271 
    272 START_TEST(check_open_more) {
    273 	int r;
    274 	char path[LQ_PATH_MAX];
    275 	LQPrivKey *pk_alice;
    276 	LQPrivKey *pk_bob;
    277 	LQPubKey *pubk_before;
    278 	LQPubKey *pubk_before_bob;
    279 	LQPubKey *pubk_after;
    280 	char *fp_alice;
    281 	char *fp_bob;
    282 
    283 	lq_cpy(path, "/tmp/lqcrypto_test_XXXXXX", 26);
    284 	r = lq_crypto_init(mktempdir(path));
    285 	ck_assert_int_eq(r, ERR_OK);
    286 
    287 	// create alice keypair
    288 	pk_alice = lq_privatekey_new(passphrase, passphrase_len);
    289 	ck_assert_ptr_nonnull(pk_alice);
    290 	pubk_before = lq_publickey_from_privatekey(pk_alice);
    291 	r = lq_publickey_fingerprint(pubk_before, &fp_alice);
    292 	ck_assert_int_eq(r, LQ_FP_LEN);
    293 	lq_privatekey_free(pk_alice);
    294 
    295 	// create bob keypair
    296 	pk_bob = lq_privatekey_new(passphrase_two, passphrase_two_len);
    297 	ck_assert_ptr_nonnull(pk_bob);
    298 	pubk_before_bob = lq_publickey_from_privatekey(pk_bob);
    299 	r = lq_publickey_fingerprint(pubk_before_bob, &fp_bob);
    300 	ck_assert_int_eq(r, LQ_FP_LEN);
    301 
    302 	// load alice key as default and check match public key from create
    303 	pk_alice = lq_privatekey_load(passphrase, passphrase_len, NULL);
    304 	ck_assert_ptr_nonnull(pk_alice);
    305 	pubk_after = lq_publickey_from_privatekey(pk_alice);
    306 	ck_assert_int_eq(lq_publickey_match(pubk_before, pubk_after), ERR_OK);
    307 	lq_publickey_free(pubk_after);
    308 	lq_privatekey_free(pk_alice);
    309 
    310 	// load alice key explicit and check match public key from create
    311 	pk_alice = lq_privatekey_load(passphrase, passphrase_len, fp_alice);
    312 	ck_assert_ptr_nonnull(pk_alice);
    313 	pubk_after = lq_publickey_from_privatekey(pk_alice);
    314 	ck_assert_int_eq(lq_publickey_match(pubk_before, pubk_after), ERR_OK);
    315 	lq_publickey_free(pubk_after);
    316 
    317 	// load bob key explicit and check match public key from create
    318 	pubk_after = lq_publickey_from_privatekey(pk_bob);
    319 	r = lq_publickey_fingerprint(pubk_after, &fp_bob);
    320 	ck_assert_int_eq(r, LQ_FP_LEN);
    321 	ck_assert_int_ne(lq_publickey_match(pubk_before, pubk_after), ERR_OK);
    322 	ck_assert_int_eq(lq_publickey_match(pubk_before_bob, pubk_after), ERR_OK);
    323 
    324 	lq_publickey_free(pubk_after);
    325 	lq_publickey_free(pubk_before_bob);
    326 	lq_publickey_free(pubk_before);
    327 
    328 	lq_privatekey_free(pk_bob);
    329 	lq_privatekey_free(pk_alice);
    330 
    331 	lq_crypto_free();
    332 }
    333 END_TEST
    334 
    335 Suite * common_suite(void) {
    336 	Suite *s;
    337 	TCase *tc;
    338 
    339 	s = suite_create("crypto");
    340 	tc = tcase_create("file");
    341 	tcase_add_test(tc, check_digest);
    342 	tcase_add_test(tc, check_privatekey);
    343 	tcase_add_test(tc, check_privatekey_lock);
    344 	tcase_add_test(tc, check_publickey);
    345 	tcase_add_test(tc, check_signature);
    346 	tcase_add_test(tc, check_verify);
    347 	tcase_add_test(tc, check_create_load);
    348 	tcase_add_test(tc, check_load_specific);
    349 	tcase_add_test(tc, check_many);
    350 	tcase_add_test(tc, check_open_more);
    351 	suite_add_tcase(s, tc);
    352 
    353 	return s;
    354 }
    355 
    356 int main(void) {
    357 	int r;
    358 	int n_fail;
    359 
    360 	Suite *s;
    361 	SRunner *sr;
    362 
    363 	r = lq_init();
    364 	if (r) {
    365 		return 1;
    366 	}
    367 
    368 	s = common_suite();	
    369 	sr = srunner_create(s);
    370 
    371 	srunner_run_all(sr, CK_VERBOSE);
    372 	n_fail = srunner_ntests_failed(sr);
    373 	srunner_free(sr);
    374 
    375 	return (n_fail == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
    376 }