Exemple #1
0
void *mul_nbr(void* arg) {
	packet* p = (packet*)arg;
	int i;
	int sum_of_mul = 0;
	for(i=0; i<dn1; i++) {
		
		int offset = p->dig - i; //Determines which element from n1 is to be multiplied with ith element from n2

		if(offset >= 0 && offset < dn1) {

			int d2 = get_dig(p->num2, i);
			int d1 = get_dig(p->num1, offset);

			sum_of_mul += d1 * d2;
		}
	}

	res[p->dig] = sum_of_mul;

	pthread_exit(0);
}
Exemple #2
0
crypto::Identity
generateIdentity(const std::string& name, crypto::Identity ca)
{
    int rc = gnutls_global_init();
    if (rc != GNUTLS_E_SUCCESS)
        return {};

    auto shared_key = std::make_shared<PrivateKey>(PrivateKey::generate());

    gnutls_x509_crt_t cert;
    if (gnutls_x509_crt_init(&cert) != GNUTLS_E_SUCCESS)
        return {};
    auto shared_crt = std::make_shared<Certificate>(cert);

    gnutls_x509_crt_set_activation_time(cert, time(NULL));
    gnutls_x509_crt_set_expiration_time(cert, time(NULL) + (700 * 24 * 60 * 60));
    if (gnutls_x509_crt_set_key(cert, shared_key->x509_key) != GNUTLS_E_SUCCESS) {
        std::cerr << "Error when setting certificate key" << std::endl;
        return {};
    }
    if (gnutls_x509_crt_set_version(cert, 3) != GNUTLS_E_SUCCESS) {
        std::cerr << "Error when setting certificate version" << std::endl;
        return {};
    }

    // TODO: compute the subject key using the recommended RFC method
    auto pk_id = shared_key->getPublicKey().getId();
    gnutls_x509_crt_set_subject_key_id(cert, &pk_id, sizeof(pk_id));

    gnutls_x509_crt_set_dn_by_oid(cert, GNUTLS_OID_X520_COMMON_NAME, 0, name.data(), name.length());

    const std::string& uid_str = shared_key->getPublicKey().getId().toString();
    gnutls_x509_crt_set_dn_by_oid(cert, GNUTLS_OID_LDAP_UID, 0, uid_str.data(), uid_str.length());

    {
        random_device rdev;
        std::uniform_int_distribution<uint64_t> dist{};
        uint64_t cert_serial = dist(rdev);
        gnutls_x509_crt_set_serial(cert, &cert_serial, sizeof(cert_serial));
    }

    if (ca.first && ca.second) {
        gnutls_x509_crt_set_key_usage (cert, GNUTLS_KEY_DIGITAL_SIGNATURE | GNUTLS_KEY_DATA_ENCIPHERMENT);
        //if (gnutls_x509_crt_sign2(cert, ca.second->cert, ca.first->x509_key, get_dig(cert), 0) != GNUTLS_E_SUCCESS) {
        if (gnutls_x509_crt_privkey_sign(cert, ca.second->cert, ca.first->key, get_dig(cert), 0) != GNUTLS_E_SUCCESS) {
            std::cerr << "Error when signing certificate" << std::endl;
            return {};
        }
        shared_crt->issuer = ca.second;
    } else {
        gnutls_x509_crt_set_ca_status(cert, 1);
        gnutls_x509_crt_set_key_usage (cert, GNUTLS_KEY_DIGITAL_SIGNATURE | GNUTLS_KEY_KEY_CERT_SIGN);
        //if (gnutls_x509_crt_sign2(cert, cert, key, get_dig(cert), 0) != GNUTLS_E_SUCCESS) {
        if (gnutls_x509_crt_privkey_sign(cert, cert, shared_key->key, get_dig(cert), 0) != GNUTLS_E_SUCCESS) {
            std::cerr << "Error when signing certificate" << std::endl;
            return {};
        }
    }

    gnutls_global_deinit();

    return {shared_key, shared_crt};
}