summaryrefslogtreecommitdiff
path: root/test/boringssl_crypto.cc
blob: c04b391854e0071f68b6182dcf507eeb5c9dbded (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/* Copyright 2023 The ChromiumOS Authors
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include "common.h"
#include "crypto/elliptic_curve_key.h"
#include "openssl/bn.h"
#include "openssl/ec.h"
#include "openssl/mem.h"
#include "openssl/obj_mac.h"
#include "openssl/rand.h"
#include "test_util.h"
#include "util.h"

test_static enum ec_error_list test_rand(void)
{
	constexpr uint8_t zero[256] = { 0 };
	uint8_t buf1[256];
	uint8_t buf2[256];

	RAND_bytes(buf1, sizeof(buf1));
	RAND_bytes(buf2, sizeof(buf2));

	TEST_ASSERT_ARRAY_NE(buf1, zero, sizeof(zero));
	TEST_ASSERT_ARRAY_NE(buf2, zero, sizeof(zero));
	TEST_ASSERT_ARRAY_NE(buf1, buf2, sizeof(buf1));

	return EC_SUCCESS;
}

test_static enum ec_error_list test_ecc_keygen(void)
{
	bssl::UniquePtr<EC_KEY> key1 = generate_elliptic_curve_key();

	TEST_NE(key1.get(), nullptr, "%p");

	/* The generated key should be valid.*/
	TEST_EQ(EC_KEY_check_key(key1.get()), 1, "%d");

	bssl::UniquePtr<EC_KEY> key2 = generate_elliptic_curve_key();

	TEST_NE(key2.get(), nullptr, "%p");

	/* The generated key should be valid. */
	TEST_EQ(EC_KEY_check_key(key2.get()), 1, "%d");

	const BIGNUM *priv1 = EC_KEY_get0_private_key(key1.get());
	const BIGNUM *priv2 = EC_KEY_get0_private_key(key2.get());

	/* The generated keys should not be the same. */
	TEST_NE(BN_cmp(priv1, priv2), 0, "%d");

	/* The generated keys should not be zero. */
	TEST_EQ(BN_is_zero(priv1), 0, "%d");
	TEST_EQ(BN_is_zero(priv2), 0, "%d");

	return EC_SUCCESS;
}

extern "C" void run_test(int argc, const char **argv)
{
	RUN_TEST(test_rand);
	RUN_TEST(test_ecc_keygen);
	test_print_result();
}