test_crypto.c (5409B)
1 #include <check.h> 2 #include <stdlib.h> 3 #include <string.h> 4 5 #include "lq/crypto.h" 6 #include "lq/config.h" 7 #include "lq/io.h" 8 #include "lq/base.h" 9 10 11 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."; 12 const char *salt = "spamspamspamspamspamspamspamspam"; 13 14 //// sha256sum "foo" 2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae 15 //static const char privkeydata[32] = { 16 // 0x2c, 0x26, 0xb4, 0x6b, 0x68, 0xff, 0xc6, 0x8f, 17 // 0xf9, 0x9b, 0x45, 0x3c, 0x1d, 0x30, 0x41, 0x34, 18 // 0x13, 0x42, 0x2d, 0x70, 0x64, 0x83, 0xbf, 0xa0, 19 // 0xf9, 0x8a, 0x5e, 0x88, 0x62, 0x66, 0xe7, 0xae, 20 //}; 21 22 // "1233" 23 static const size_t passphrase_len = 4; 24 static const char passphrase[5] = { 25 0x31, 0x32, 0x33, 0x34, 0x00, 26 }; 27 28 29 struct dummycrypto { 30 void *data; ///< Literal private key data. 31 size_t len; ///< Length of private key data. 32 }; 33 34 START_TEST(check_digest) { 35 int r; 36 char out[LQ_DIGEST_LEN]; 37 38 r = lq_digest(data, strlen(data), (char*)out); 39 ck_assert(r == 0); 40 } 41 END_TEST 42 43 START_TEST(check_privatekey) { 44 LQPrivKey *pk; 45 46 pk = lq_privatekey_new(passphrase, passphrase_len); 47 ck_assert_ptr_nonnull(pk); 48 lq_privatekey_free(pk); 49 } 50 END_TEST 51 52 START_TEST(check_publickey) { 53 LQPrivKey *pk; 54 LQPubKey *pubk; 55 LQPubKey *pubk_manual; 56 char *keydata; 57 char *keydata_manual; 58 59 pk = lq_privatekey_new(passphrase, passphrase_len); 60 ck_assert_ptr_nonnull(pk); 61 pubk = lq_publickey_from_privatekey(pk); 62 lq_publickey_bytes(pubk, &keydata); 63 pubk_manual = lq_publickey_new(keydata); 64 lq_publickey_bytes(pubk_manual, &keydata_manual); 65 ck_assert_mem_eq(keydata_manual, keydata, 65); 66 lq_publickey_free(pubk_manual); 67 lq_publickey_free(pubk); 68 lq_privatekey_free(pk); 69 } 70 END_TEST 71 72 START_TEST(check_signature) { 73 char r; 74 char digest[32]; 75 LQPrivKey *pk; 76 LQSig *sig; 77 char *sigdata; 78 79 pk = lq_privatekey_new(passphrase, passphrase_len); 80 ck_assert_ptr_nonnull(pk); 81 sig = lq_privatekey_sign(pk, data, strlen(data), salt); 82 ck_assert_ptr_null(sig); 83 84 r = lq_privatekey_unlock(pk, passphrase, 32); 85 ck_assert_int_eq(r, 0); 86 87 sig = lq_privatekey_sign(pk, digest, 32, salt); 88 ck_assert_ptr_nonnull(sig); 89 90 r = lq_signature_bytes(sig, &sigdata); 91 ck_assert_mem_eq(sig->impl, sigdata, r); 92 93 lq_signature_free(sig); 94 lq_privatekey_free(pk); 95 } 96 END_TEST 97 98 START_TEST(check_verify) { 99 char r; 100 LQPrivKey *pk; 101 LQSig *sig; 102 103 pk = lq_privatekey_new(passphrase, 32); 104 ck_assert_ptr_nonnull(pk); 105 sig = lq_privatekey_sign(pk, data, strlen(data), salt); 106 ck_assert_ptr_null(sig); 107 108 r = lq_privatekey_unlock(pk, passphrase, 32); 109 ck_assert_int_eq(r, 0); 110 111 sig = lq_privatekey_sign(pk, data, strlen(data), salt); 112 ck_assert_ptr_nonnull(sig); 113 114 r = lq_signature_verify(sig, data, strlen(data)); 115 116 lq_signature_free(sig); 117 lq_privatekey_free(pk); 118 } 119 END_TEST 120 121 START_TEST(check_create_load) { 122 LQPrivKey *pk; 123 LQPrivKey *pk_load; 124 125 pk = lq_privatekey_new(passphrase, passphrase_len); 126 ck_assert_ptr_nonnull(pk); 127 pk_load = lq_privatekey_load(passphrase, passphrase_len, NULL); 128 ck_assert_ptr_nonnull(pk_load); 129 130 lq_privatekey_free(pk); 131 } 132 END_TEST 133 134 START_TEST(check_load_specific) { 135 LQPrivKey *pk; 136 LQPubKey *pubk; 137 LQPrivKey *pk_load; 138 char *p; 139 size_t c; 140 141 pk = lq_privatekey_new(passphrase, passphrase_len); 142 ck_assert_ptr_nonnull(pk); 143 pubk = lq_publickey_from_privatekey(pk); 144 ck_assert_ptr_nonnull(pubk); 145 c = lq_publickey_fingerprint(pubk, &p); 146 ck_assert_int_gt(c, 0); 147 pk_load = lq_privatekey_load(passphrase, passphrase_len, NULL); 148 ck_assert_ptr_nonnull(pk_load); 149 pk_load = lq_privatekey_load(passphrase, passphrase_len, p); 150 ck_assert_ptr_nonnull(pk_load); 151 152 lq_privatekey_free(pk); 153 } 154 END_TEST 155 156 START_TEST(check_many) { 157 LQPrivKey *pk; 158 LQPubKey *pubk; 159 LQPubKey *pubk_manual; 160 char *keydata; 161 char *keydata_manual; 162 163 pk = lq_privatekey_new(passphrase, passphrase_len); 164 ck_assert_ptr_nonnull(pk); 165 pubk = lq_publickey_from_privatekey(pk); 166 lq_publickey_bytes(pubk, &keydata); 167 pubk_manual = lq_publickey_new(keydata); 168 lq_publickey_bytes(pubk_manual, &keydata_manual); 169 ck_assert_mem_eq(keydata_manual, keydata, 65); 170 lq_publickey_free(pubk_manual); 171 lq_publickey_free(pubk); 172 lq_privatekey_free(pk); 173 } 174 END_TEST 175 176 Suite * common_suite(void) { 177 Suite *s; 178 TCase *tc; 179 180 s = suite_create("crypto"); 181 tc = tcase_create("file"); 182 tcase_add_test(tc, check_digest); 183 tcase_add_test(tc, check_privatekey); 184 tcase_add_test(tc, check_publickey); 185 tcase_add_test(tc, check_signature); 186 tcase_add_test(tc, check_verify); 187 tcase_add_test(tc, check_create_load); 188 tcase_add_test(tc, check_load_specific); 189 tcase_add_test(tc, check_many); 190 suite_add_tcase(s, tc); 191 192 return s; 193 } 194 195 int main(void) { 196 int r; 197 int n_fail; 198 char path[LQ_PATH_MAX]; 199 200 Suite *s; 201 SRunner *sr; 202 203 r = lq_init(); 204 if (r) { 205 return 1; 206 } 207 208 lq_cpy(path, "/tmp/lqcrypto_test_XXXXXX", 26); 209 r = lq_crypto_init(mktempdir(path)); 210 if (r) { 211 return 1; 212 } 213 214 s = common_suite(); 215 sr = srunner_create(s); 216 217 srunner_run_all(sr, CK_VERBOSE); 218 n_fail = srunner_ntests_failed(sr); 219 srunner_free(sr); 220 221 lq_crypto_free(); 222 223 return (n_fail == 0) ? EXIT_SUCCESS : EXIT_FAILURE; 224 }