1 /*
2 * Copyright 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <keymaster/km_openssl/ecies_kem.h>
18
19 #include <gtest/gtest.h>
20 #include <openssl/evp.h>
21
22 #include <hardware/keymaster_defs.h>
23
24 #include <keymaster/android_keymaster_utils.h>
25 #include <keymaster/km_openssl/nist_curve_key_exchange.h>
26
27 #include "android_keymaster_test_utils.h"
28
29 using std::string;
30
31 namespace keymaster {
32 namespace test {
33
34 static const keymaster_ec_curve_t kEcCurves[] = {KM_EC_CURVE_P_224, KM_EC_CURVE_P_256,
35 KM_EC_CURVE_P_384, KM_EC_CURVE_P_521};
36
37 /**
38 * TestConsistency just tests that the basic key encapsulation hold.
39 */
TEST(EciesKem,TestConsistency)40 TEST(EciesKem, TestConsistency) {
41 static const uint32_t kKeyLen = 32;
42 for (auto& curve : kEcCurves) {
43 AuthorizationSet kem_description(AuthorizationSetBuilder()
44 .Authorization(TAG_EC_CURVE, curve)
45 .Authorization(TAG_KDF, KM_KDF_RFC5869_SHA256)
46 .Authorization(TAG_ECIES_SINGLE_HASH_MODE)
47 .Authorization(TAG_KEY_SIZE, kKeyLen));
48 keymaster_error_t error;
49 EciesKem* kem = new EciesKem(kem_description, &error);
50 ASSERT_EQ(KM_ERROR_OK, error);
51
52 NistCurveKeyExchange* key_exchange = NistCurveKeyExchange::GenerateKeyExchange(curve);
53 Buffer peer_public_value;
54 ASSERT_TRUE(key_exchange->public_value(&peer_public_value));
55
56 Buffer output_clear_key;
57 Buffer output_encrypted_key;
58 ASSERT_TRUE(kem->Encrypt(peer_public_value, &output_clear_key, &output_encrypted_key));
59 ASSERT_EQ(kKeyLen, output_clear_key.available_read());
60 ASSERT_EQ(peer_public_value.available_read(), output_encrypted_key.available_read());
61
62 Buffer decrypted_clear_key;
63 ASSERT_TRUE(
64 kem->Decrypt(key_exchange->private_key(), output_encrypted_key, &decrypted_clear_key));
65 ASSERT_EQ(kKeyLen, decrypted_clear_key.available_read());
66 EXPECT_EQ(0, memcmp(output_clear_key.peek_read(), decrypted_clear_key.peek_read(),
67 output_clear_key.available_read()));
68 }
69 }
70
71 } // namespace test
72 } // namespace keymaster
73