Exemplo n.º 1
0
END_TEST


START_TEST(check_ec_serialization)
{
	EC_KEY *pair, *pair2;
	unsigned char *sbuf, *sbuf2;
	int res;
	size_t ssize, ssize2;

	res = crypto_init();
	ck_assert_msg(!res, "Crypto initialization routine failed.\n");

	for (size_t i = 0; i < N_SERIALIZATION_TESTS; i++) {
		pair = _generate_ec_keypair(0);
		ck_assert_msg((pair != NULL), "EC serialization check failed: could not generate key pair.\n");

		sbuf = _serialize_ec_pubkey(pair, &ssize);
		ck_assert_msg((sbuf != NULL), "EC serialization check failed: pubkey serialization error.\n");

		pair2 = _deserialize_ec_pubkey(sbuf, ssize, 0);
		ck_assert_msg((pair2 != NULL), "EC serialization check failed: pubkey deserialization error.\n");

		sbuf2 = _serialize_ec_pubkey(pair, &ssize2);
		ck_assert_msg((sbuf2 != NULL), "EC serialization check failed: pubkey serialization error [2].\n");

		ck_assert_msg((ssize == ssize2), "EC serialization check failed: serialized pubkeys had different serialized lengths {%u vs %u}\n", ssize, ssize2);

		res = memcmp(sbuf, sbuf2, ssize);
		ck_assert_msg(!res, "EC serialization check failed: serialized pubkeys had different data.\n");

		free(sbuf);
		free(sbuf2);

		_free_ec_key(pair2);

		sbuf = _serialize_ec_privkey(pair, &ssize);
		ck_assert_msg((sbuf != NULL), "EC serialization check failed: pubkey serialization error.\n");

		pair2 = _deserialize_ec_privkey(sbuf, ssize, 0);
		ck_assert_msg((pair2 != NULL), "EC serialization check failed: pubkey deserialization error.\n");

		sbuf2 = _serialize_ec_privkey(pair, &ssize2);
		ck_assert_msg((sbuf2 != NULL), "EC serialization check failed: pubkey serialization error [2].\n");

		ck_assert_msg((ssize == ssize2), "EC serialization check failed: serialized pubkeys had different serialized lengths {%u vs %u}\n", ssize, ssize2);

		res = memcmp(sbuf, sbuf2, ssize);
		ck_assert_msg(!res, "EC serialization check failed: serialized pubkeys had different data.\n");

		free(sbuf);
		free(sbuf2);
		free_ec_key(pair);
	}

	fprintf(stderr, "EC serialization check completed.\n");
}
Exemplo n.º 2
0
/**
 * @brief	Retrieves the encryption key from the keys binary.
 * @param	bin_keys        Pointer to the keys buffer.
 * @param	len		Length of the keys buffer.
 * @return	Pointer to elliptic curve key, NULL if an error occurred.
 * @free_using{free_ec_key}
*/
static EC_KEY *keys_serial_get_enc_key(const unsigned char *bin_keys, size_t len) {

        /* unsigned char sign_fid, enc_fid; sign_fid is unused causing errors on 
           compilation */
        unsigned char enc_fid;
	size_t at = 0, privkeylen;
	EC_KEY *enc_key = NULL;

	if(!bin_keys) {
		RET_ERROR_PTR(ERR_BAD_PARAM, NULL);
	} else if(keys_check_length(bin_keys, len) < 0) {
		RET_ERROR_PTR(ERR_BAD_PARAM, NULL);
	}

	switch(keys_type_get(bin_keys, len)) {

	case KEYS_TYPE_ORG:
		/* sign_fid = KEYS_ORG_PRIVATE_POK; */
		enc_fid = KEYS_ORG_PRIVATE_ENC;
		break;
	case KEYS_TYPE_USER:
		/* sign_fid = KEYS_USER_PRIVATE_SIGN; */
		enc_fid = KEYS_USER_PRIVATE_ENC;
		break;
	default:
		RET_ERROR_PTR(ERR_UNSPEC, "invalid keys type");
		break;

	}

	at = KEYS_HEADER_SIZE;

	while(bin_keys[at++] != enc_fid) {
		at += bin_keys[at] + 1;

		if(len <= at) {
			RET_ERROR_PTR(ERR_UNSPEC, "no private encryption key in keys file");
		}
	}

	privkeylen = _int_no_get_2b(bin_keys+at);
	at += 2;

	if(at + privkeylen > len) {
		RET_ERROR_PTR(ERR_UNSPEC, "invalid encryption key size");
	}

	if(!(enc_key = _deserialize_ec_privkey(bin_keys + at, privkeylen, 0))) {
		RET_ERROR_PTR(ERR_UNSPEC, "could not deserialize private EC encryption key");
	}

	return enc_key;
}