/** Frees the server * \relates adbus_Server */ void adbus_serv_free(adbus_Server* s) { if (s == NULL) return; adbus_Remote* r = s->remotes.next; while (r) { adbus_Remote* next = r->hl.next; adbus_remote_disconnect(r); r = next; } dl_clear(Remote, &s->remotes); for (dh_Iter ii = dh_begin(&s->services); ii != dh_end(&s->services); ++ii) { if (dh_exist(&s->services, ii)) { adbusI_serv_freeservice(dh_val(&s->services, ii)); } } dh_clear(Service, &s->services); dh_free(Service, &s->services); adbusI_serv_freebus(s); adbus_iface_deref(s->busInterface); free(s); }
/** Import DH public or private key part from raw numbers NB: The p & g parts must be set beforehand @param in The key-part to import, either public or private. @param inlen The key-part's length @param type Which type of key (PK_PRIVATE or PK_PUBLIC) @param key [out] the destination for the imported key @return CRYPT_OK if successful */ int dh_set_key(const unsigned char *in, unsigned long inlen, int type, dh_key *key) { int err; LTC_ARGCHK(key != NULL); LTC_ARGCHK(ltc_mp.name != NULL); if (type == PK_PRIVATE) { key->type = PK_PRIVATE; if ((err = mp_read_unsigned_bin(key->x, (unsigned char*)in, inlen)) != CRYPT_OK) { goto LBL_ERR; } if ((err = mp_exptmod(key->base, key->x, key->prime, key->y)) != CRYPT_OK) { goto LBL_ERR; } } else { key->type = PK_PUBLIC; if ((err = mp_read_unsigned_bin(key->y, (unsigned char*)in, inlen)) != CRYPT_OK) { goto LBL_ERR; } } /* check public key */ if ((err = dh_check_pubkey(key)) != CRYPT_OK) { goto LBL_ERR; } return CRYPT_OK; LBL_ERR: dh_free(key); return err; }
static void FreeMember(adbus_Member* m) { if (!m) return; if (m->release[0]) m->release[0](m->ruser[0]); if (m->release[1]) m->release[1](m->ruser[1]); for (dh_Iter ii = dh_begin(&m->annotations); ii != dh_end(&m->annotations); ++ii) { if (dh_exist(&m->annotations, ii)) { free((char*) dh_key(&m->annotations, ii)); free(dh_val(&m->annotations, ii)); } } dh_free(StringPair, &m->annotations); for (size_t i = 0; i < dv_size(&m->arguments); i++) { free(dv_a(&m->arguments, i)); } dv_free(String, &m->arguments); for (size_t j = 0; j < dv_size(&m->returns); j++) { free(dv_a(&m->returns, j)); } dv_free(String, &m->returns); free(m->propertyType); free((char*) m->name.str); free(m); }
void evrb_crypto_free(void* ctx) { EVRB_CRYPTO* evb_crypto=ctx; if(evb_crypto) { srtp_dealloc(evb_crypto->srtp_session); if (evb_crypto->key) free(evb_crypto->key); evb_crypto->key=0; if (evb_crypto->dh) dh_free(evb_crypto->dh); free(ctx); ctx=0; } }
/** Derefs an interface. * \relates adbus_Interface */ void adbus_iface_deref(adbus_Interface* i) { if (i && adbus_InterlockedDecrement(&i->ref) == 0) { if (ADBUS_TRACE_MEMORY) { adbusI_log("free interface %s", i->name); } for (dh_Iter ii = dh_begin(&i->members); ii != dh_end(&i->members); ++ii) { if (dh_exist(&i->members, ii)) FreeMember(dh_val(&i->members, ii)); } dh_free(MemberPtr, &i->members); free((char*) i->name.str); free(i); } }
int main(int argc, char **argv) { int fd; Elf *e; lock_descr_t *lock_tab = NULL; int nb_entry = 0; if (argc < 3) errx ( EXIT_FAILURE , " usage : %s file_name section_name1 [sct_name2 ...]" , argv [0]); char *elf = argv[1]; /* Init and open of the elf file */ if ( elf_version ( EV_CURRENT ) == EV_NONE ) errx ( EXIT_FAILURE , " ELF library initialization failed : %s." , elf_errmsg ( -1)); if (( fd = open ( elf , O_RDONLY , 0)) < 0) errx ( EXIT_FAILURE , " open \" %s \" failed " , elf); if (( e = elf_begin ( fd , ELF_C_READ , NULL )) == NULL ) errx ( EXIT_FAILURE , " elf_begin () failed : %s.", elf_errmsg ( -1)); if ( elf_kind ( e ) != ELF_K_ELF ) errx ( EXIT_FAILURE , " %s is not an ELF object.", elf); /* init debug helper */ debug_helper_t *dh = dh_init(elf); int i; for (i=2 ; i<argc ; i++) { extract_lock_descr(e, argv[i], &lock_tab, &nb_entry); get_dwarf_location(dh, lock_tab, nb_entry); printf("%s:\n", argv[i]); print_lock_descr(lock_tab, nb_entry); printf("\n"); free(lock_tab); lock_tab = NULL; nb_entry = 0; } /* close */ dh_free(dh); elf_end(e); close(fd); return 0; }
/** Import DH key parts p and g from built-in DH groups @param groupsize The size of the DH group to use @param key [out] Where the newly created DH key will be stored @return CRYPT_OK if successful, note: on error all allocated memory will be freed automatically. */ int dh_set_pg_groupsize(int groupsize, dh_key *key) { int err, i; LTC_ARGCHK(key != NULL); LTC_ARGCHK(ltc_mp.name != NULL); LTC_ARGCHK(groupsize > 0); for (i = 0; (groupsize > ltc_dh_sets[i].size) && (ltc_dh_sets[i].size != 0); i++); if (ltc_dh_sets[i].size == 0) return CRYPT_INVALID_KEYSIZE; if ((err = mp_init_multi(&key->x, &key->y, &key->base, &key->prime, NULL)) != CRYPT_OK) { return err; } if ((err = mp_read_radix(key->base, ltc_dh_sets[i].base, 16)) != CRYPT_OK) { goto LBL_ERR; } if ((err = mp_read_radix(key->prime, ltc_dh_sets[i].prime, 16)) != CRYPT_OK) { goto LBL_ERR; } return CRYPT_OK; LBL_ERR: dh_free(key); return err; }
/** Import DH key parts p and g from raw numbers @param p DH's p (prime) @param plen DH's p's length @param g DH's g (group) @param glen DH's g's length @param key [out] the destination for the imported key @return CRYPT_OK if successful */ int dh_set_pg(const unsigned char *p, unsigned long plen, const unsigned char *g, unsigned long glen, dh_key *key) { int err; LTC_ARGCHK(key != NULL); LTC_ARGCHK(p != NULL); LTC_ARGCHK(g != NULL); LTC_ARGCHK(ltc_mp.name != NULL); if ((err = mp_init_multi(&key->x, &key->y, &key->base, &key->prime, NULL)) != CRYPT_OK) { return err; } if ((err = mp_read_unsigned_bin(key->base, (unsigned char*)g, glen)) != CRYPT_OK) { goto LBL_ERR; } if ((err = mp_read_unsigned_bin(key->prime, (unsigned char*)p, plen)) != CRYPT_OK) { goto LBL_ERR; } return CRYPT_OK; LBL_ERR: dh_free(key); return err; }
int dh_tests (void) { unsigned char buf[3][4096]; unsigned long x, y, z; int stat, stat2; dh_key usera, userb; DO(dh_test()); /* make up two keys */ DO(dh_make_key (&test_yarrow, find_prng ("yarrow"), 96, &usera)); DO(dh_make_key (&test_yarrow, find_prng ("yarrow"), 96, &userb)); /* make the shared secret */ x = 4096; DO(dh_shared_secret (&usera, &userb, buf[0], &x)); y = 4096; DO(dh_shared_secret (&userb, &usera, buf[1], &y)); if (y != x) { printf ("DH Shared keys are not same size.\n"); return 1; } if (memcmp (buf[0], buf[1], x)) { printf ("DH Shared keys not same contents.\n"); return 1; } /* now export userb */ y = 4096; DO(dh_export (buf[1], &y, PK_PUBLIC, &userb)); dh_free (&userb); /* import and make the shared secret again */ DO(dh_import (buf[1], y, &userb)); z = 4096; DO(dh_shared_secret (&usera, &userb, buf[2], &z)); if (z != x) { printf ("failed. Size don't match?\n"); return 1; } if (memcmp (buf[0], buf[2], x)) { printf ("Failed. Content didn't match.\n"); return 1; } dh_free (&usera); dh_free (&userb); /* test encrypt_key */ dh_make_key (&test_yarrow, find_prng ("yarrow"), 128, &usera); for (x = 0; x < 16; x++) { buf[0][x] = x; } y = sizeof (buf[1]); DO(dh_encrypt_key (buf[0], 16, buf[1], &y, &test_yarrow, find_prng ("yarrow"), find_hash ("md5"), &usera)); zeromem (buf[0], sizeof (buf[0])); x = sizeof (buf[0]); DO(dh_decrypt_key (buf[1], y, buf[0], &x, &usera)); if (x != 16) { printf ("Failed (length)\n"); return 1; } for (x = 0; x < 16; x++) if (buf[0][x] != x) { printf ("Failed (contents)\n"); return 1; } /* test sign_hash */ for (x = 0; x < 16; x++) { buf[0][x] = x; } x = sizeof (buf[1]); DO(dh_sign_hash (buf[0], 16, buf[1], &x, &test_yarrow , find_prng ("yarrow"), &usera)); DO(dh_verify_hash (buf[1], x, buf[0], 16, &stat, &usera)); buf[0][0] ^= 1; DO(dh_verify_hash (buf[1], x, buf[0], 16, &stat2, &usera)); if (!(stat == 1 && stat2 == 0)) { printf("dh_sign/verify_hash %d %d", stat, stat2); return 1; } dh_free (&usera); return 0; }
static int _dhparam_test(void) { dh_key k; unsigned char buf[1024]; /* generated by: openssl dhparam -outform der -out dhparam.der 2048 */ unsigned char dhparam_der[] = { 0x30, 0x82, 0x01, 0x08, 0x02, 0x82, 0x01, 0x01, 0x00, 0xae, 0xfe, 0x78, 0xce, 0x80, 0xd5, 0xd7, 0x8e, 0xcc, 0x4f, 0x0c, 0x1b, 0xb0, 0x95, 0x10, 0xe1, 0x41, 0x15, 0x53, 0x4d, 0x0e, 0x68, 0xb0, 0xf8, 0x5a, 0x41, 0x0e, 0x65, 0x2f, 0x9f, 0xac, 0x9c, 0x30, 0xb0, 0x76, 0xec, 0x02, 0xe9, 0x43, 0x55, 0x08, 0xb4, 0x20, 0x60, 0xd9, 0x52, 0xda, 0x2d, 0xab, 0x9a, 0xba, 0xe6, 0xcf, 0x11, 0xa7, 0x00, 0x44, 0xc2, 0x5e, 0xd1, 0xba, 0x9b, 0xaa, 0xfe, 0x03, 0xdd, 0xdc, 0xef, 0x41, 0x89, 0x9c, 0xac, 0x64, 0x13, 0xd9, 0x6a, 0x8a, 0x55, 0xa0, 0x5b, 0xff, 0x12, 0x92, 0x37, 0x52, 0x6a, 0x91, 0xa4, 0x6e, 0x9e, 0x61, 0xb7, 0xfe, 0xb0, 0x17, 0x8e, 0x67, 0x0f, 0x88, 0x46, 0xa7, 0x9e, 0xb1, 0xdb, 0x68, 0x77, 0x70, 0xb5, 0x77, 0xf2, 0x7e, 0x33, 0xb1, 0x3e, 0x10, 0xc4, 0x63, 0x36, 0xd0, 0x13, 0x27, 0xd3, 0x29, 0xc3, 0xb6, 0x5d, 0xf6, 0x5d, 0xa7, 0xd8, 0x25, 0x5c, 0x0b, 0x65, 0x99, 0xfa, 0xf9, 0x5f, 0x1d, 0xee, 0xd1, 0x86, 0x64, 0x7c, 0x44, 0xcb, 0xa0, 0x12, 0x52, 0x4c, 0xd4, 0x46, 0x81, 0xae, 0x07, 0xdb, 0xc7, 0x13, 0x29, 0xce, 0x9b, 0xcf, 0x1c, 0x06, 0xd2, 0x0f, 0x2d, 0xbb, 0x12, 0x33, 0xb9, 0xb1, 0x0f, 0x67, 0x5d, 0x3f, 0x0c, 0xe4, 0xfa, 0x67, 0x26, 0xe2, 0x89, 0xa2, 0xd5, 0x66, 0x29, 0x1c, 0xe2, 0x8e, 0xbb, 0x7b, 0xcb, 0xcc, 0x70, 0x7e, 0x4f, 0x0e, 0xd3, 0x5d, 0x64, 0x64, 0x1b, 0x27, 0xbb, 0xda, 0xa9, 0x08, 0x2b, 0x62, 0xd4, 0xca, 0xc3, 0x3a, 0x23, 0x39, 0x58, 0x57, 0xaf, 0x7b, 0x8b, 0x0c, 0x5b, 0x2e, 0xfc, 0x42, 0x57, 0x59, 0x39, 0x2e, 0x6d, 0x39, 0x97, 0xdb, 0x5b, 0x5c, 0xb9, 0x59, 0x71, 0x42, 0xf3, 0xcd, 0xea, 0xda, 0x86, 0x54, 0x86, 0x61, 0x8d, 0x93, 0x66, 0xc7, 0x65, 0xd1, 0x98, 0xcb, 0x02, 0x01, 0x02 }; /* text dump: openssl dh -inform DER -in dhparam.der -text DH Parameters: (2048 bit) prime: 00:ae:fe:78:ce:80:d5:d7:8e:cc:4f:0c:1b:b0:95: 10:e1:41:15:53:4d:0e:68:b0:f8:5a:41:0e:65:2f: 9f:ac:9c:30:b0:76:ec:02:e9:43:55:08:b4:20:60: d9:52:da:2d:ab:9a:ba:e6:cf:11:a7:00:44:c2:5e: d1:ba:9b:aa:fe:03:dd:dc:ef:41:89:9c:ac:64:13: d9:6a:8a:55:a0:5b:ff:12:92:37:52:6a:91:a4:6e: 9e:61:b7:fe:b0:17:8e:67:0f:88:46:a7:9e:b1:db: 68:77:70:b5:77:f2:7e:33:b1:3e:10:c4:63:36:d0: 13:27:d3:29:c3:b6:5d:f6:5d:a7:d8:25:5c:0b:65: 99:fa:f9:5f:1d:ee:d1:86:64:7c:44:cb:a0:12:52: 4c:d4:46:81:ae:07:db:c7:13:29:ce:9b:cf:1c:06: d2:0f:2d:bb:12:33:b9:b1:0f:67:5d:3f:0c:e4:fa: 67:26:e2:89:a2:d5:66:29:1c:e2:8e:bb:7b:cb:cc: 70:7e:4f:0e:d3:5d:64:64:1b:27:bb:da:a9:08:2b: 62:d4:ca:c3:3a:23:39:58:57:af:7b:8b:0c:5b:2e: fc:42:57:59:39:2e:6d:39:97:db:5b:5c:b9:59:71: 42:f3:cd:ea:da:86:54:86:61:8d:93:66:c7:65:d1: 98:cb generator: 2 (0x2) */ unsigned char prime[] = { 0xae, 0xfe, 0x78, 0xce, 0x80, 0xd5, 0xd7, 0x8e, 0xcc, 0x4f, 0x0c, 0x1b, 0xb0, 0x95, 0x10, 0xe1, 0x41, 0x15, 0x53, 0x4d, 0x0e, 0x68, 0xb0, 0xf8, 0x5a, 0x41, 0x0e, 0x65, 0x2f, 0x9f, 0xac, 0x9c, 0x30, 0xb0, 0x76, 0xec, 0x02, 0xe9, 0x43, 0x55, 0x08, 0xb4, 0x20, 0x60, 0xd9, 0x52, 0xda, 0x2d, 0xab, 0x9a, 0xba, 0xe6, 0xcf, 0x11, 0xa7, 0x00, 0x44, 0xc2, 0x5e, 0xd1, 0xba, 0x9b, 0xaa, 0xfe, 0x03, 0xdd, 0xdc, 0xef, 0x41, 0x89, 0x9c, 0xac, 0x64, 0x13, 0xd9, 0x6a, 0x8a, 0x55, 0xa0, 0x5b, 0xff, 0x12, 0x92, 0x37, 0x52, 0x6a, 0x91, 0xa4, 0x6e, 0x9e, 0x61, 0xb7, 0xfe, 0xb0, 0x17, 0x8e, 0x67, 0x0f, 0x88, 0x46, 0xa7, 0x9e, 0xb1, 0xdb, 0x68, 0x77, 0x70, 0xb5, 0x77, 0xf2, 0x7e, 0x33, 0xb1, 0x3e, 0x10, 0xc4, 0x63, 0x36, 0xd0, 0x13, 0x27, 0xd3, 0x29, 0xc3, 0xb6, 0x5d, 0xf6, 0x5d, 0xa7, 0xd8, 0x25, 0x5c, 0x0b, 0x65, 0x99, 0xfa, 0xf9, 0x5f, 0x1d, 0xee, 0xd1, 0x86, 0x64, 0x7c, 0x44, 0xcb, 0xa0, 0x12, 0x52, 0x4c, 0xd4, 0x46, 0x81, 0xae, 0x07, 0xdb, 0xc7, 0x13, 0x29, 0xce, 0x9b, 0xcf, 0x1c, 0x06, 0xd2, 0x0f, 0x2d, 0xbb, 0x12, 0x33, 0xb9, 0xb1, 0x0f, 0x67, 0x5d, 0x3f, 0x0c, 0xe4, 0xfa, 0x67, 0x26, 0xe2, 0x89, 0xa2, 0xd5, 0x66, 0x29, 0x1c, 0xe2, 0x8e, 0xbb, 0x7b, 0xcb, 0xcc, 0x70, 0x7e, 0x4f, 0x0e, 0xd3, 0x5d, 0x64, 0x64, 0x1b, 0x27, 0xbb, 0xda, 0xa9, 0x08, 0x2b, 0x62, 0xd4, 0xca, 0xc3, 0x3a, 0x23, 0x39, 0x58, 0x57, 0xaf, 0x7b, 0x8b, 0x0c, 0x5b, 0x2e, 0xfc, 0x42, 0x57, 0x59, 0x39, 0x2e, 0x6d, 0x39, 0x97, 0xdb, 0x5b, 0x5c, 0xb9, 0x59, 0x71, 0x42, 0xf3, 0xcd, 0xea, 0xda, 0x86, 0x54, 0x86, 0x61, 0x8d, 0x93, 0x66, 0xc7, 0x65, 0xd1, 0x98, 0xcb }; DO(dh_set_pg_dhparam(dhparam_der, sizeof(dhparam_der), &k)); DO(dh_generate_key(&yarrow_prng, find_prng ("yarrow"), &k)); if (mp_unsigned_bin_size(k.prime) > sizeof(buf)) { printf("dhparam_test: short buf\n"); dh_free(&k); return CRYPT_ERROR; } DO(mp_to_unsigned_bin(k.prime, buf)); if (compare_testvector(buf, sizeof(prime), prime, sizeof(prime), "dhparam_test", 1)) { printf("dhparam_test: prime mismatch\n"); dh_free(&k); return CRYPT_ERROR; } if (mp_cmp_d(k.base, 2) != LTC_MP_EQ) { printf("dhparam_test: base mismatch\n"); dh_free(&k); return CRYPT_ERROR; } dh_free(&k); return CRYPT_OK; }
static int _basic_test(void) { unsigned char buf[3][4096]; unsigned long x, y, z; int size; dh_key usera, userb; /* make up two keys */ DO(dh_set_pg_groupsize(KEYSIZE/8, &usera)); DO(dh_generate_key(&yarrow_prng, find_prng ("yarrow"), &usera)); DO(dh_set_pg_groupsize(KEYSIZE/8, &userb)); DO(dh_generate_key(&yarrow_prng, find_prng ("yarrow"), &userb)); /* make the shared secret */ x = KEYSIZE; DO(dh_shared_secret (&usera, &userb, buf[0], &x)); y = KEYSIZE; DO(dh_shared_secret (&userb, &usera, buf[1], &y)); if (y != x) { fprintf(stderr, "DH Shared keys are not same size.\n"); dh_free (&usera); dh_free (&userb); return CRYPT_ERROR; } if (memcmp (buf[0], buf[1], x)) { fprintf(stderr, "DH Shared keys not same contents.\n"); dh_free (&usera); dh_free (&userb); return CRYPT_ERROR; } /* now export userb */ y = KEYSIZE; DO(dh_export (buf[1], &y, PK_PUBLIC, &userb)); dh_free (&userb); /* import and make the shared secret again */ DO(dh_import (buf[1], y, &userb)); z = KEYSIZE; DO(dh_shared_secret (&usera, &userb, buf[2], &z)); dh_free (&usera); dh_free (&userb); if (z != x) { fprintf(stderr, "failed. Size don't match?\n"); return CRYPT_ERROR; } if (memcmp (buf[0], buf[2], x)) { fprintf(stderr, "Failed. Content didn't match.\n"); return CRYPT_ERROR; } for (x = 0; ltc_dh_sets[x].size != 0; x++) { DO(dh_set_pg_groupsize(ltc_dh_sets[x].size, &usera)); DO(dh_generate_key(&yarrow_prng, find_prng ("yarrow"), &usera)); size = dh_get_groupsize(&usera); dh_free(&usera); if (size != ltc_dh_sets[x].size) { fprintf(stderr, "dh_groupsize mismatch %d %d\n", size, ltc_dh_sets[x].size); return CRYPT_ERROR; } dh_free(&usera); } return CRYPT_OK; }
static int _set_test(void) { dh_key k1, k2, k3; unsigned char buf[4096]; unsigned long len; int i; unsigned char gbin[] = { 0x02 }; unsigned char pbin[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xC9, 0x0F, 0xDA, 0xA2, 0x21, 0x68, 0xC2, 0x34, 0xC4, 0xC6, 0x62, 0x8B, 0x80, 0xDC, 0x1C, 0xD1, 0x29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 0x52, 0x66, 0x3D, 0x34, 0xE0, 0xE8, 0x09, 0xD4, 0xF6, 0xBA, 0xDB, 0xB3, 0x6F, 0x80, 0xB6, 0x81, 0x3E, 0xBF, 0x7C, 0x32, 0x81, 0xB8, 0x62, 0x20, 0x9E, 0x56, 0x04, 0xBD, 0xEA, 0x8B, 0x8F, 0x5F, 0x7B, 0xFD, 0xC3, 0xEE, 0xB7, 0xAD, 0xB7, 0x30, 0x48, 0x28, 0x9B, 0xCE, 0xA0, 0xF5, 0xA5, 0xCD, 0xEE, 0x7D, 0xF9, 0x1C, 0xD1, 0xF0, 0xBA, 0x63, 0x2F, 0x06, 0xDB, 0xE9, 0xBA, 0x7E, 0xF0, 0x14, 0xB8, 0x4B, 0x02, 0xD4, 0x97, 0xCA, 0x7D, 0x0C, 0x60, 0xF7, 0x34, 0x75, 0x2A, 0x64, 0x9D, 0xA4, 0x96, 0x94, 0x6B, 0x4E, 0x53, 0x1B, 0x30, 0xD9, 0xF8, 0x2E, 0xDD, 0x85, 0x56, 0x36, 0xC0, 0xB0, 0xF2, 0xAE, 0x23, 0x2E, 0x41, 0x86, 0x45, 0x4E, 0x88, 0x87, 0xBB, 0x42, 0x3E, 0x32, 0xA5, 0xA2, 0x49, 0x5E, 0xAC, 0xBA, 0x99, 0x62, 0x0A, 0xCD, 0x03, 0xA3, 0x83, 0x45, 0xEB, 0xB6, 0x73, 0x5E, 0x62, 0x33, 0x0A, 0x8E, 0xE9, 0xAA, 0x6C, 0x83, 0x70, 0x41, 0x0F, 0x5C, 0xD4, 0x5A, 0xF3, 0x7E, 0xE9, 0x0A, 0x0D, 0xA9, 0x5B, 0xE9, 0x6F, 0xC9, 0x39, 0xE8, 0x8F, 0xE0, 0xBD, 0x2C, 0xD0, 0x9F, 0xC8, 0xF5, 0x24, 0x20, 0x8C }; struct { int radix; void* g; int glen; void* p; int plen; void* x; int xlen; void* y; int ylen; } test[1] = { { 256, gbin, sizeof(gbin), pbin, sizeof(pbin), xbin, sizeof(xbin), ybin, sizeof(ybin) } }; unsigned char export_private[] = { 0x30, 0x82, 0x01, 0x3A, 0x02, 0x01, 0x00, 0x03, 0x02, 0x07, 0x80, 0x02, 0x82, 0x01, 0x01, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xC9, 0x0F, 0xDA, 0xA2, 0x21, 0x68, 0xC2, 0x34, 0xC4, 0xC6, 0x62, 0x8B, 0x80, 0xDC, 0x1C, 0xD1, 0x29, 0x02, 0x4E, 0x08, 0x8A, 0x67, 0xCC, 0x74, 0x02, 0x0B, 0xBE, 0xA6, 0x3B, 0x13, 0x9B, 0x22, 0x51, 0x4A, 0x08, 0x79, 0x8E, 0x34, 0x04, 0xDD, 0xEF, 0x95, 0x19, 0xB3, 0xCD, 0x3A, 0x43, 0x1B, 0x30, 0x2B, 0x0A, 0x6D, 0xF2, 0x5F, 0x14, 0x37, 0x4F, 0xE1, 0x35, 0x6D, 0x6D, 0x51, 0xC2, 0x45, 0xE4, 0x85, 0xB5, 0x76, 0x62, 0x5E, 0x7E, 0xC6, 0xF4, 0x4C, 0x42, 0xE9, 0xA6, 0x37, 0xED, 0x6B, 0x0B, 0xFF, 0x5C, 0xB6, 0xF4, 0x06, 0xB7, 0xED, 0xEE, 0x38, 0x6B, 0xFB, 0x5A, 0x89, 0x9F, 0xA5, 0xAE, 0x9F, 0x24, 0x11, 0x7C, 0x4B, 0x1F, 0xE6, 0x49, 0x28, 0x66, 0x51, 0xEC, 0xE4, 0x5B, 0x3D, 0xC2, 0x00, 0x7C, 0xB8, 0xA1, 0x63, 0xBF, 0x05, 0x98, 0xDA, 0x48, 0x36, 0x1C, 0x55, 0xD3, 0x9A, 0x69, 0x16, 0x3F, 0xA8, 0xFD, 0x24, 0xCF, 0x5F, 0x83, 0x65, 0x5D, 0x23, 0xDC, 0xA3, 0xAD, 0x96, 0x1C, 0x62, 0xF3, 0x56, 0x20, 0x85, 0x52, 0xBB, 0x9E, 0xD5, 0x29, 0x07, 0x70, 0x96, 0x96, 0x6D, 0x67, 0x0C, 0x35, 0x4E, 0x4A, 0xBC, 0x98, 0x04, 0xF1, 0x74, 0x6C, 0x08, 0xCA, 0x18, 0x21, 0x7C, 0x32, 0x90, 0x5E, 0x46, 0x2E, 0x36, 0xCE, 0x3B, 0xE3, 0x9E, 0x77, 0x2C, 0x18, 0x0E, 0x86, 0x03, 0x9B, 0x27, 0x83, 0xA2, 0xEC, 0x07, 0xA2, 0x8F, 0xB5, 0xC5, 0x5D, 0xF0, 0x6F, 0x4C, 0x52, 0xC9, 0xDE, 0x2B, 0xCB, 0xF6, 0x95, 0x58, 0x17, 0x18, 0x39, 0x95, 0x49, 0x7C, 0xEA, 0x95, 0x6A, 0xE5, 0x15, 0xD2, 0x26, 0x18, 0x98, 0xFA, 0x05, 0x10, 0x15, 0x72, 0x8E, 0x5A, 0x8A, 0xAC, 0xAA, 0x68, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x02, 0x01, 0x02, 0x02, 0x29, 0x00, 0xA6, 0x68, 0x1A, 0xDC, 0x38, 0x6C, 0xE9, 0x44, 0xC3, 0xDE, 0xD9, 0xA7, 0x30, 0x1D, 0xCC, 0x9C, 0x51, 0x82, 0x50, 0xE3, 0xED, 0xB6, 0x2F, 0x95, 0x91, 0x98, 0xF8, 0xDC, 0x00, 0x57, 0xDD, 0x6F, 0xB5, 0x7A, 0xBA, 0xFD, 0x78, 0x81, 0x98, 0xB1 }; unsigned char export_public[] = { 0x30, 0x82, 0x02, 0x13, 0x02, 0x01, 0x00, 0x03, 0x02, 0x07, 0x00, 0x02, 0x82, 0x01, 0x01, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xC9, 0x0F, 0xDA, 0xA2, 0x21, 0x68, 0xC2, 0x34, 0xC4, 0xC6, 0x62, 0x8B, 0x80, 0xDC, 0x1C, 0xD1, 0x29, 0x02, 0x4E,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0x48, 0x28, 0x9B, 0xCE, 0xA0, 0xF5, 0xA5, 0xCD, 0xEE, 0x7D, 0xF9, 0x1C, 0xD1, 0xF0, 0xBA, 0x63, 0x2F, 0x06, 0xDB, 0xE9, 0xBA, 0x7E, 0xF0, 0x14, 0xB8, 0x4B, 0x02, 0xD4, 0x97, 0xCA, 0x7D, 0x0C, 0x60, 0xF7, 0x34, 0x75, 0x2A, 0x64, 0x9D, 0xA4, 0x96, 0x94, 0x6B, 0x4E, 0x53, 0x1B, 0x30, 0xD9, 0xF8, 0x2E, 0xDD, 0x85, 0x56, 0x36, 0xC0, 0xB0, 0xF2, 0xAE, 0x23, 0x2E, 0x41, 0x86, 0x45, 0x4E, 0x88, 0x87, 0xBB, 0x42, 0x3E, 0x32, 0xA5, 0xA2, 0x49, 0x5E, 0xAC, 0xBA, 0x99, 0x62, 0x0A, 0xCD, 0x03, 0xA3, 0x83, 0x45, 0xEB, 0xB6, 0x73, 0x5E, 0x62, 0x33, 0x0A, 0x8E, 0xE9, 0xAA, 0x6C, 0x83, 0x70, 0x41, 0x0F, 0x5C, 0xD4, 0x5A, 0xF3, 0x7E, 0xE9, 0x0A, 0x0D, 0xA9, 0x5B, 0xE9, 0x6F, 0xC9, 0x39, 0xE8, 0x8F, 0xE0, 0xBD, 0x2C, 0xD0, 0x9F, 0xC8, 0xF5, 0x24, 0x20, 0x8C }; for (i = 0; i < 1; i++) { DO(dh_set_pg(test[i].p, test[i].plen, test[i].g, test[i].glen, &k1)); DO(dh_set_key(test[i].x, test[i].xlen, PK_PRIVATE, &k1)); len = sizeof(buf); DO(dh_export(buf, &len, PK_PRIVATE, &k1)); if (compare_testvector(buf, len, export_private, sizeof(export_private), "radix_test", i*10 + 0)) { printf("radix_test: dh_export+PK_PRIVATE mismatch\n"); dh_free(&k1); return CRYPT_ERROR; } len = sizeof(buf); DO(dh_export(buf, &len, PK_PUBLIC, &k1)); if (compare_testvector(buf, len, export_public, sizeof(export_public), "radix_test", i*10 + 1)) { printf("radix_test: dh_export+PK_PUBLIC mismatch\n"); dh_free(&k1); return CRYPT_ERROR; } len = sizeof(buf); DO(dh_export_key(buf, &len, PK_PRIVATE, &k1)); if (compare_testvector(buf, len, xbin, sizeof(xbin), "radix_test", i*10 + 2)) { printf("radix_test: dh_export+PK_PRIVATE mismatch\n"); dh_free(&k1); return CRYPT_ERROR; } len = sizeof(buf); DO(dh_export_key(buf, &len, PK_PUBLIC, &k1)); if (compare_testvector(buf, len, ybin, sizeof(ybin), "radix_test", i*10 + 3)) { printf("radix_test: dh_export+PK_PUBLIC mismatch\n"); dh_free(&k1); return CRYPT_ERROR; } dh_free(&k1); DO(dh_set_pg(test[i].p, test[i].plen, test[i].g, test[i].glen, &k1)); DO(dh_set_key(test[i].x, test[i].xlen, PK_PRIVATE, &k1)); len = sizeof(buf); DO(dh_export(buf, &len, PK_PRIVATE, &k1)); if (compare_testvector(buf, len, export_private, sizeof(export_private), "radix_test", i*10 + 4)) { printf("radix_test: dh_export+PK_PRIVATE mismatch\n"); dh_free(&k1); return CRYPT_ERROR; } len = sizeof(buf); DO(dh_export(buf, &len, PK_PUBLIC, &k1)); if (compare_testvector(buf, len, export_public, sizeof(export_public), "radix_test", i*10 + 5)) { printf("radix_test: dh_export+PK_PUBLIC mismatch\n"); dh_free(&k1); return CRYPT_ERROR; } dh_free(&k1); DO(dh_set_pg(test[i].p, test[i].plen, test[i].g, test[i].glen, &k2)); DO(dh_set_key(test[i].y, test[i].ylen, PK_PUBLIC, &k2)); len = sizeof(buf); DO(dh_export(buf, &len, PK_PUBLIC, &k2)); if (compare_testvector(buf, len, export_public, sizeof(export_public), "radix_test", i*10 + 6)) { printf("radix_test: dh_export+PK_PUBLIC mismatch\n"); dh_free(&k2); return CRYPT_ERROR; } len = sizeof(buf); DO(dh_export_key(buf, &len, PK_PUBLIC, &k2)); if (compare_testvector(buf, len, ybin, sizeof(ybin), "radix_test", i*10 + 7)) { printf("radix_test: dh_export+PK_PUBLIC mismatch\n"); dh_free(&k2); return CRYPT_ERROR; } dh_free(&k2); DO(dh_set_pg(test[i].p, test[i].plen, test[i].g, test[i].glen, &k3)); DO(dh_generate_key(&yarrow_prng, find_prng("yarrow"), &k3)); len = mp_unsigned_bin_size(k3.prime); DO(mp_to_unsigned_bin(k3.prime, buf)); if (compare_testvector(buf, len, pbin, sizeof(pbin), "radix_test", i*10 + 8)) { printf("radix_test: dh_make_key_ex prime mismatch\n"); dh_free(&k3); return CRYPT_ERROR; } len = mp_unsigned_bin_size(k3.base); DO(mp_to_unsigned_bin(k3.base, buf)); if (compare_testvector(buf, len, gbin, sizeof(gbin), "radix_test", i*10 + 9)) { printf("radix_test: dh_make_key_ex base mismatch\n"); dh_free(&k3); return CRYPT_ERROR; } dh_free(&k3); } return CRYPT_OK; }
void test_data(void) { const unsigned int NUM_OF_DATA = 10; size_t i = 0; void* data = NULL; unsigned int dataLength = 0; char* strInt = NULL; int loaded = FALSE; const char* const* names = NULL; unsigned int namesLength = 0; DataHandler* dm = NULL; g_print("\n\rCreate data manager...\n\r"); dm = dh_new(open_file_callback, save_file_callback, cmp_data_callback); g_print("Adding event handlers...\n\r"); dh_add_load_event(dm, load_event_callback); dh_add_unload_event(dm, unload_event_callback); dh_add_save_event(dm, save_event_callback); dh_add_clear_event(dm, clear_event_callback); g_print("Start adding random data...\n\r"); for (i = 0; i < NUM_OF_DATA; i++) { strInt = g_strdup_printf("%zu", i); dh_load_data(dm, strInt, strInt); } g_print("Force to load data out of bounds...\n\r"); dh_set_max_data(dm, 0); dh_set_max_data(dm, G_MAXUINT); g_print("Max data is fixed to '%i'\n\r", dh_get_max_data(dm)); dataLength = dh_get_data_size(dm); g_assert(dataLength == NUM_OF_DATA); g_print("The number of data loaded is %i...\n\r", dataLength); g_print("Check if data is loaded...\n\r"); for (i = 0; i < NUM_OF_DATA; i++) { strInt = g_strdup_printf("%zu", i); loaded = dh_data_is_loaded(dm, strInt); g_assert(loaded == TRUE); if (loaded == TRUE) { g_print("'%s' is loaded\n\r", strInt); } } g_print("Simulate file saving...\n\r"); for (i = 0; i < NUM_OF_DATA; i++) { strInt = g_strdup_printf("%zu", i); dh_save_data_on_file(dm, strInt, "test_path"); loaded = dh_data_is_loaded_from_file(dm, strInt); g_assert(loaded == TRUE); if (loaded == TRUE) { g_print("'%s' is correctly saved on file\n\r", strInt); } } g_print("Simulate auto-save...\n\r"); for (i = 0; i < NUM_OF_DATA; i++) { strInt = g_strdup_printf("%zu", i); dh_save_data(dm, strInt); } g_print("Simulate file opening...\n\r"); dh_open_data(dm, "test_path"); dataLength = dh_get_data_size(dm); g_assert(dataLength == NUM_OF_DATA + 1); g_print("The number of data loaded is %i\n\r", dataLength); names = dh_get_data_names(dm, &namesLength); g_assert(dataLength == namesLength); g_print("This is the list of the loaded items...\n\r"); for (i = 0; i < namesLength; i++) { g_print("Item: %s\n\r", names[i]); } g_print("Now get 7 data...\n\r"); data = dh_get_data_by_name(dm, "7"); g_assert(data != NULL); g_print("The data of the 7 key is '%s'\n\r", (char*)data); g_print("Unload the 7 data...\n\r"); dh_unload_data(dm,"7"); dataLength = dh_get_data_size(dm); g_assert(dataLength == NUM_OF_DATA); names = dh_get_data_names(dm, &namesLength); g_assert(dataLength == namesLength); g_print("This is the list of the loaded items...\n\r"); for (i = 0; i < namesLength; i++) { g_print("Item: %s\n\r", names[i]); } g_print("Clear all items...\n\r"); dh_clear_data(dm); dataLength = dh_get_data_size(dm); g_assert(dataLength == 0); g_print("The number of data loaded is %i\n\r", dataLength); g_print("Free up data manager resources..."); dh_free(dm); }
/** Encrypt a short symmetric key with a public DH key @param in The symmetric key to encrypt @param inlen The length of the key (octets) @param out [out] The ciphertext @param outlen [in/out] The max size and resulting size of the ciphertext @param prng An active PRNG state @param wprng The index of the PRNG desired @param hash The index of the hash desired (must produce a digest of size >= the size of the plaintext) @param key The public key you wish to encrypt with. @return CRYPT_OK if successful */ int dh_encrypt_key(const unsigned char *in, unsigned long inlen, unsigned char *out, unsigned long *outlen, prng_state *prng, int wprng, int hash, dh_key *key) { unsigned char *pub_expt, *dh_shared, *skey; dh_key pubkey; unsigned long x, y, z, hashsize, pubkeysize; int err; LTC_ARGCHK(in != NULL); LTC_ARGCHK(out != NULL); LTC_ARGCHK(outlen != NULL); LTC_ARGCHK(key != NULL); /* check that wprng/hash are not invalid */ if ((err = prng_is_valid(wprng)) != CRYPT_OK) { return err; } if ((err = hash_is_valid(hash)) != CRYPT_OK) { return err; } if (inlen > hash_descriptor[hash].hashsize) { return CRYPT_INVALID_HASH; } /* allocate memory */ pub_expt = XMALLOC(DH_BUF_SIZE); dh_shared = XMALLOC(DH_BUF_SIZE); skey = XMALLOC(MAXBLOCKSIZE); if (pub_expt == NULL || dh_shared == NULL || skey == NULL) { if (pub_expt != NULL) { XFREE(pub_expt); } if (dh_shared != NULL) { XFREE(dh_shared); } if (skey != NULL) { XFREE(skey); } return CRYPT_MEM; } /* make a random key and export the public copy */ if ((err = dh_make_key(prng, wprng, dh_get_size(key), &pubkey)) != CRYPT_OK) { goto LBL_ERR; } pubkeysize = DH_BUF_SIZE; if ((err = dh_export(pub_expt, &pubkeysize, PK_PUBLIC, &pubkey)) != CRYPT_OK) { dh_free(&pubkey); goto LBL_ERR; } /* now check if the out buffer is big enough */ if (*outlen < (1 + 4 + 4 + PACKET_SIZE + pubkeysize + inlen)) { dh_free(&pubkey); err = CRYPT_BUFFER_OVERFLOW; goto LBL_ERR; } /* make random key */ hashsize = hash_descriptor[hash].hashsize; x = DH_BUF_SIZE; if ((err = dh_shared_secret(&pubkey, key, dh_shared, &x)) != CRYPT_OK) { dh_free(&pubkey); goto LBL_ERR; } dh_free(&pubkey); z = MAXBLOCKSIZE; if ((err = hash_memory(hash, dh_shared, x, skey, &z)) != CRYPT_OK) { goto LBL_ERR; } /* store header */ packet_store_header(out, PACKET_SECT_DH, PACKET_SUB_ENC_KEY); /* output header */ y = PACKET_SIZE; /* size of hash name and the name itself */ out[y++] = hash_descriptor[hash].ID; /* length of DH pubkey and the key itself */ STORE32L(pubkeysize, out+y); y += 4; for (x = 0; x < pubkeysize; x++, y++) { out[y] = pub_expt[x]; } /* Store the encrypted key */ STORE32L(inlen, out+y); y += 4; for (x = 0; x < inlen; x++, y++) { out[y] = skey[x] ^ in[x]; } *outlen = y; err = CRYPT_OK; LBL_ERR: #ifdef LTC_CLEAN_STACK /* clean up */ zeromem(pub_expt, DH_BUF_SIZE); zeromem(dh_shared, DH_BUF_SIZE); zeromem(skey, MAXBLOCKSIZE); #endif XFREE(skey); XFREE(dh_shared); XFREE(pub_expt); return err; }
/** Decrypt a DH encrypted symmetric key @param in The DH encrypted packet @param inlen The length of the DH encrypted packet @param out The plaintext @param outlen [in/out] The max size and resulting size of the plaintext @param key The private DH key corresponding to the public key that encrypted the plaintext @return CRYPT_OK if successful */ int dh_decrypt_key(const unsigned char *in, unsigned long inlen, unsigned char *out, unsigned long *outlen, dh_key *key) { unsigned char *shared_secret, *skey; unsigned long x, y, z, hashsize, keysize; int hash, err; dh_key pubkey; LTC_ARGCHK(in != NULL); LTC_ARGCHK(out != NULL); LTC_ARGCHK(outlen != NULL); LTC_ARGCHK(key != NULL); /* right key type? */ if (key->type != PK_PRIVATE) { return CRYPT_PK_NOT_PRIVATE; } /* allocate ram */ shared_secret = XMALLOC(DH_BUF_SIZE); skey = XMALLOC(MAXBLOCKSIZE); if (shared_secret == NULL || skey == NULL) { if (shared_secret != NULL) { XFREE(shared_secret); } if (skey != NULL) { XFREE(skey); } return CRYPT_MEM; } /* check if initial header should fit */ if (inlen < PACKET_SIZE+1+4+4) { err = CRYPT_INVALID_PACKET; goto LBL_ERR; } else { inlen -= PACKET_SIZE+1+4+4; } /* is header correct? */ if ((err = packet_valid_header((unsigned char *)in, PACKET_SECT_DH, PACKET_SUB_ENC_KEY)) != CRYPT_OK) { goto LBL_ERR; } /* now lets get the hash name */ y = PACKET_SIZE; hash = find_hash_id(in[y++]); if (hash == -1) { err = CRYPT_INVALID_HASH; goto LBL_ERR; } /* common values */ hashsize = hash_descriptor[hash].hashsize; /* get public key */ LOAD32L(x, in+y); /* now check if the imported key will fit */ if (inlen < x) { err = CRYPT_INVALID_PACKET; goto LBL_ERR; } else { inlen -= x; } y += 4; if ((err = dh_import(in+y, x, &pubkey)) != CRYPT_OK) { goto LBL_ERR; } y += x; /* make shared key */ x = DH_BUF_SIZE; if ((err = dh_shared_secret(key, &pubkey, shared_secret, &x)) != CRYPT_OK) { dh_free(&pubkey); goto LBL_ERR; } dh_free(&pubkey); z = MAXBLOCKSIZE; if ((err = hash_memory(hash, shared_secret, x, skey, &z)) != CRYPT_OK) { goto LBL_ERR; } /* load in the encrypted key */ LOAD32L(keysize, in+y); /* will the out fit as part of the input */ if (inlen < keysize) { err = CRYPT_INVALID_PACKET; goto LBL_ERR; } else { inlen -= keysize; } if (keysize > *outlen) { err = CRYPT_BUFFER_OVERFLOW; goto LBL_ERR; } y += 4; *outlen = keysize; for (x = 0; x < keysize; x++, y++) { out[x] = skey[x] ^ in[y]; } err = CRYPT_OK; LBL_ERR: #ifdef LTC_CLEAN_STACK zeromem(shared_secret, DH_BUF_SIZE); zeromem(skey, MAXBLOCKSIZE); #endif XFREE(skey); XFREE(shared_secret); return err; }