1*cfb92d14SAndroid Build Coastguard Worker /*
2*cfb92d14SAndroid Build Coastguard Worker * Copyright (c) 2020, The OpenThread Authors.
3*cfb92d14SAndroid Build Coastguard Worker * All rights reserved.
4*cfb92d14SAndroid Build Coastguard Worker *
5*cfb92d14SAndroid Build Coastguard Worker * Redistribution and use in source and binary forms, with or without
6*cfb92d14SAndroid Build Coastguard Worker * modification, are permitted provided that the following conditions are met:
7*cfb92d14SAndroid Build Coastguard Worker * 1. Redistributions of source code must retain the above copyright
8*cfb92d14SAndroid Build Coastguard Worker * notice, this list of conditions and the following disclaimer.
9*cfb92d14SAndroid Build Coastguard Worker * 2. Redistributions in binary form must reproduce the above copyright
10*cfb92d14SAndroid Build Coastguard Worker * notice, this list of conditions and the following disclaimer in the
11*cfb92d14SAndroid Build Coastguard Worker * documentation and/or other materials provided with the distribution.
12*cfb92d14SAndroid Build Coastguard Worker * 3. Neither the name of the copyright holder nor the
13*cfb92d14SAndroid Build Coastguard Worker * names of its contributors may be used to endorse or promote products
14*cfb92d14SAndroid Build Coastguard Worker * derived from this software without specific prior written permission.
15*cfb92d14SAndroid Build Coastguard Worker *
16*cfb92d14SAndroid Build Coastguard Worker * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17*cfb92d14SAndroid Build Coastguard Worker * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18*cfb92d14SAndroid Build Coastguard Worker * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19*cfb92d14SAndroid Build Coastguard Worker * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20*cfb92d14SAndroid Build Coastguard Worker * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21*cfb92d14SAndroid Build Coastguard Worker * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22*cfb92d14SAndroid Build Coastguard Worker * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23*cfb92d14SAndroid Build Coastguard Worker * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24*cfb92d14SAndroid Build Coastguard Worker * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25*cfb92d14SAndroid Build Coastguard Worker * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26*cfb92d14SAndroid Build Coastguard Worker * POSSIBILITY OF SUCH DAMAGE.
27*cfb92d14SAndroid Build Coastguard Worker */
28*cfb92d14SAndroid Build Coastguard Worker
29*cfb92d14SAndroid Build Coastguard Worker #include <openthread/config.h>
30*cfb92d14SAndroid Build Coastguard Worker
31*cfb92d14SAndroid Build Coastguard Worker #include "test_platform.h"
32*cfb92d14SAndroid Build Coastguard Worker #include "test_util.hpp"
33*cfb92d14SAndroid Build Coastguard Worker
34*cfb92d14SAndroid Build Coastguard Worker #include "common/debug.hpp"
35*cfb92d14SAndroid Build Coastguard Worker #include "common/random.hpp"
36*cfb92d14SAndroid Build Coastguard Worker #include "crypto/ecdsa.hpp"
37*cfb92d14SAndroid Build Coastguard Worker
38*cfb92d14SAndroid Build Coastguard Worker #include <mbedtls/ctr_drbg.h>
39*cfb92d14SAndroid Build Coastguard Worker #include <mbedtls/ecdsa.h>
40*cfb92d14SAndroid Build Coastguard Worker #include <mbedtls/pk.h>
41*cfb92d14SAndroid Build Coastguard Worker
42*cfb92d14SAndroid Build Coastguard Worker #if OPENTHREAD_CONFIG_ECDSA_ENABLE
43*cfb92d14SAndroid Build Coastguard Worker
44*cfb92d14SAndroid Build Coastguard Worker namespace ot {
45*cfb92d14SAndroid Build Coastguard Worker namespace Crypto {
46*cfb92d14SAndroid Build Coastguard Worker
TestEcdsaVector(void)47*cfb92d14SAndroid Build Coastguard Worker void TestEcdsaVector(void)
48*cfb92d14SAndroid Build Coastguard Worker {
49*cfb92d14SAndroid Build Coastguard Worker // This case is derived from the test vector from RFC 6979 - section A.2.5 (for NIST P-256 and SHA-256 hash).
50*cfb92d14SAndroid Build Coastguard Worker
51*cfb92d14SAndroid Build Coastguard Worker const uint8_t kKeyPairInfo[] = {
52*cfb92d14SAndroid Build Coastguard Worker 0x30, 0x78, 0x02, 0x01, 0x01, 0x04, 0x21, 0x00, 0xC9, 0xAF, 0xA9, 0xD8, 0x45, 0xBA, 0x75, 0x16, 0x6B, 0x5C,
53*cfb92d14SAndroid Build Coastguard Worker 0x21, 0x57, 0x67, 0xB1, 0xD6, 0x93, 0x4E, 0x50, 0xC3, 0xDB, 0x36, 0xE8, 0x9B, 0x12, 0x7B, 0x8A, 0x62, 0x2B,
54*cfb92d14SAndroid Build Coastguard Worker 0x12, 0x0F, 0x67, 0x21, 0xA0, 0x0A, 0x06, 0x08, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x03, 0x01, 0x07, 0xA1, 0x44,
55*cfb92d14SAndroid Build Coastguard Worker 0x03, 0x42, 0x00, 0x04, 0x60, 0xFE, 0xD4, 0xBA, 0x25, 0x5A, 0x9D, 0x31, 0xC9, 0x61, 0xEB, 0x74, 0xC6, 0x35,
56*cfb92d14SAndroid Build Coastguard Worker 0x6D, 0x68, 0xC0, 0x49, 0xB8, 0x92, 0x3B, 0x61, 0xFA, 0x6C, 0xE6, 0x69, 0x62, 0x2E, 0x60, 0xF2, 0x9F, 0xB6,
57*cfb92d14SAndroid Build Coastguard Worker 0x79, 0x03, 0xFE, 0x10, 0x08, 0xB8, 0xBC, 0x99, 0xA4, 0x1A, 0xE9, 0xE9, 0x56, 0x28, 0xBC, 0x64, 0xF2, 0xF1,
58*cfb92d14SAndroid Build Coastguard Worker 0xB2, 0x0C, 0x2D, 0x7E, 0x9F, 0x51, 0x77, 0xA3, 0xC2, 0x94, 0xD4, 0x46, 0x22, 0x99};
59*cfb92d14SAndroid Build Coastguard Worker
60*cfb92d14SAndroid Build Coastguard Worker const uint8_t kPublicKey[] = {
61*cfb92d14SAndroid Build Coastguard Worker 0x60, 0xFE, 0xD4, 0xBA, 0x25, 0x5A, 0x9D, 0x31, 0xC9, 0x61, 0xEB, 0x74, 0xC6, 0x35, 0x6D, 0x68,
62*cfb92d14SAndroid Build Coastguard Worker 0xC0, 0x49, 0xB8, 0x92, 0x3B, 0x61, 0xFA, 0x6C, 0xE6, 0x69, 0x62, 0x2E, 0x60, 0xF2, 0x9F, 0xB6,
63*cfb92d14SAndroid Build Coastguard Worker 0x79, 0x03, 0xFE, 0x10, 0x08, 0xB8, 0xBC, 0x99, 0xA4, 0x1A, 0xE9, 0xE9, 0x56, 0x28, 0xBC, 0x64,
64*cfb92d14SAndroid Build Coastguard Worker 0xF2, 0xF1, 0xB2, 0x0C, 0x2D, 0x7E, 0x9F, 0x51, 0x77, 0xA3, 0xC2, 0x94, 0xD4, 0x46, 0x22, 0x99,
65*cfb92d14SAndroid Build Coastguard Worker };
66*cfb92d14SAndroid Build Coastguard Worker
67*cfb92d14SAndroid Build Coastguard Worker const uint8_t kMessage[] = {'s', 'a', 'm', 'p', 'l', 'e'};
68*cfb92d14SAndroid Build Coastguard Worker
69*cfb92d14SAndroid Build Coastguard Worker #if OPENTHREAD_CONFIG_DETERMINISTIC_ECDSA_ENABLE
70*cfb92d14SAndroid Build Coastguard Worker const uint8_t kExpectedSignature[] = {
71*cfb92d14SAndroid Build Coastguard Worker 0xEF, 0xD4, 0x8B, 0x2A, 0xAC, 0xB6, 0xA8, 0xFD, 0x11, 0x40, 0xDD, 0x9C, 0xD4, 0x5E, 0x81, 0xD6,
72*cfb92d14SAndroid Build Coastguard Worker 0x9D, 0x2C, 0x87, 0x7B, 0x56, 0xAA, 0xF9, 0x91, 0xC3, 0x4D, 0x0E, 0xA8, 0x4E, 0xAF, 0x37, 0x16,
73*cfb92d14SAndroid Build Coastguard Worker 0xF7, 0xCB, 0x1C, 0x94, 0x2D, 0x65, 0x7C, 0x41, 0xD4, 0x36, 0xC7, 0xA1, 0xB6, 0xE2, 0x9F, 0x65,
74*cfb92d14SAndroid Build Coastguard Worker 0xF3, 0xE9, 0x00, 0xDB, 0xB9, 0xAF, 0xF4, 0x06, 0x4D, 0xC4, 0xAB, 0x2F, 0x84, 0x3A, 0xCD, 0xA8,
75*cfb92d14SAndroid Build Coastguard Worker };
76*cfb92d14SAndroid Build Coastguard Worker #endif
77*cfb92d14SAndroid Build Coastguard Worker
78*cfb92d14SAndroid Build Coastguard Worker Instance *instance = testInitInstance();
79*cfb92d14SAndroid Build Coastguard Worker
80*cfb92d14SAndroid Build Coastguard Worker Ecdsa::P256::KeyPair keyPair;
81*cfb92d14SAndroid Build Coastguard Worker Ecdsa::P256::PublicKey publicKey;
82*cfb92d14SAndroid Build Coastguard Worker Ecdsa::P256::Signature signature;
83*cfb92d14SAndroid Build Coastguard Worker Sha256 sha256;
84*cfb92d14SAndroid Build Coastguard Worker Sha256::Hash hash;
85*cfb92d14SAndroid Build Coastguard Worker
86*cfb92d14SAndroid Build Coastguard Worker VerifyOrQuit(instance != nullptr, "Null OpenThread instance");
87*cfb92d14SAndroid Build Coastguard Worker
88*cfb92d14SAndroid Build Coastguard Worker printf("\n===========================================================================\n");
89*cfb92d14SAndroid Build Coastguard Worker printf("Test ECDA with test vector from RFC 6979 (A.2.5)\n");
90*cfb92d14SAndroid Build Coastguard Worker
91*cfb92d14SAndroid Build Coastguard Worker printf("\nLoading key-pair ----------------------------------------------------------\n");
92*cfb92d14SAndroid Build Coastguard Worker memcpy(keyPair.GetDerBytes(), kKeyPairInfo, sizeof(kKeyPairInfo));
93*cfb92d14SAndroid Build Coastguard Worker keyPair.SetDerLength(sizeof(kKeyPairInfo));
94*cfb92d14SAndroid Build Coastguard Worker
95*cfb92d14SAndroid Build Coastguard Worker DumpBuffer("KeyPair", keyPair.GetDerBytes(), keyPair.GetDerLength());
96*cfb92d14SAndroid Build Coastguard Worker
97*cfb92d14SAndroid Build Coastguard Worker SuccessOrQuit(keyPair.GetPublicKey(publicKey));
98*cfb92d14SAndroid Build Coastguard Worker DumpBuffer("PublicKey", publicKey.GetBytes(), Ecdsa::P256::PublicKey::kSize);
99*cfb92d14SAndroid Build Coastguard Worker
100*cfb92d14SAndroid Build Coastguard Worker VerifyOrQuit(sizeof(kPublicKey) == Ecdsa::P256::PublicKey::kSize, "Example public key is invalid");
101*cfb92d14SAndroid Build Coastguard Worker VerifyOrQuit(memcmp(publicKey.GetBytes(), kPublicKey, sizeof(kPublicKey)) == 0,
102*cfb92d14SAndroid Build Coastguard Worker "KeyPair::GetPublicKey() did not return the expected key");
103*cfb92d14SAndroid Build Coastguard Worker
104*cfb92d14SAndroid Build Coastguard Worker printf("\nHash the message ----------------------------------------------------------\n");
105*cfb92d14SAndroid Build Coastguard Worker sha256.Start();
106*cfb92d14SAndroid Build Coastguard Worker sha256.Update(kMessage);
107*cfb92d14SAndroid Build Coastguard Worker sha256.Finish(hash);
108*cfb92d14SAndroid Build Coastguard Worker
109*cfb92d14SAndroid Build Coastguard Worker DumpBuffer("Hash", hash.GetBytes(), sizeof(hash));
110*cfb92d14SAndroid Build Coastguard Worker
111*cfb92d14SAndroid Build Coastguard Worker printf("\nSign the message ----------------------------------------------------------\n");
112*cfb92d14SAndroid Build Coastguard Worker SuccessOrQuit(keyPair.Sign(hash, signature));
113*cfb92d14SAndroid Build Coastguard Worker DumpBuffer("Signature", signature.GetBytes(), sizeof(signature));
114*cfb92d14SAndroid Build Coastguard Worker
115*cfb92d14SAndroid Build Coastguard Worker #if OPENTHREAD_CONFIG_DETERMINISTIC_ECDSA_ENABLE
116*cfb92d14SAndroid Build Coastguard Worker printf("\nCheck signature against expected sequence----------------------------------\n");
117*cfb92d14SAndroid Build Coastguard Worker DumpBuffer("Expected signature", kExpectedSignature, sizeof(kExpectedSignature));
118*cfb92d14SAndroid Build Coastguard Worker
119*cfb92d14SAndroid Build Coastguard Worker VerifyOrQuit(sizeof(kExpectedSignature) == sizeof(signature));
120*cfb92d14SAndroid Build Coastguard Worker VerifyOrQuit(memcmp(signature.GetBytes(), kExpectedSignature, sizeof(kExpectedSignature)) == 0);
121*cfb92d14SAndroid Build Coastguard Worker
122*cfb92d14SAndroid Build Coastguard Worker printf("Signature matches expected sequence.\n");
123*cfb92d14SAndroid Build Coastguard Worker #endif
124*cfb92d14SAndroid Build Coastguard Worker
125*cfb92d14SAndroid Build Coastguard Worker printf("\nVerify the signature ------------------------------------------------------\n");
126*cfb92d14SAndroid Build Coastguard Worker SuccessOrQuit(publicKey.Verify(hash, signature));
127*cfb92d14SAndroid Build Coastguard Worker printf("\nSignature was verified successfully.\n\n");
128*cfb92d14SAndroid Build Coastguard Worker
129*cfb92d14SAndroid Build Coastguard Worker testFreeInstance(instance);
130*cfb92d14SAndroid Build Coastguard Worker }
131*cfb92d14SAndroid Build Coastguard Worker
TestEcdsaKeyGenerationSignAndVerify(void)132*cfb92d14SAndroid Build Coastguard Worker void TestEcdsaKeyGenerationSignAndVerify(void)
133*cfb92d14SAndroid Build Coastguard Worker {
134*cfb92d14SAndroid Build Coastguard Worker Instance *instance = testInitInstance();
135*cfb92d14SAndroid Build Coastguard Worker
136*cfb92d14SAndroid Build Coastguard Worker const char kMessage[] = "You are not a drop in the ocean. You are the entire ocean in a drop.";
137*cfb92d14SAndroid Build Coastguard Worker
138*cfb92d14SAndroid Build Coastguard Worker Ecdsa::P256::KeyPair keyPair;
139*cfb92d14SAndroid Build Coastguard Worker Ecdsa::P256::PublicKey publicKey;
140*cfb92d14SAndroid Build Coastguard Worker Ecdsa::P256::Signature signature;
141*cfb92d14SAndroid Build Coastguard Worker Sha256 sha256;
142*cfb92d14SAndroid Build Coastguard Worker Sha256::Hash hash;
143*cfb92d14SAndroid Build Coastguard Worker
144*cfb92d14SAndroid Build Coastguard Worker VerifyOrQuit(instance != nullptr, "Null OpenThread instance");
145*cfb92d14SAndroid Build Coastguard Worker
146*cfb92d14SAndroid Build Coastguard Worker printf("\n===========================================================================\n");
147*cfb92d14SAndroid Build Coastguard Worker printf("Test ECDA key generation, signing and verification\n");
148*cfb92d14SAndroid Build Coastguard Worker
149*cfb92d14SAndroid Build Coastguard Worker printf("\nGenerating key-pair -------------------------------------------------------\n");
150*cfb92d14SAndroid Build Coastguard Worker SuccessOrQuit(keyPair.Generate());
151*cfb92d14SAndroid Build Coastguard Worker
152*cfb92d14SAndroid Build Coastguard Worker DumpBuffer("KeyPair", keyPair.GetDerBytes(), keyPair.GetDerLength());
153*cfb92d14SAndroid Build Coastguard Worker
154*cfb92d14SAndroid Build Coastguard Worker SuccessOrQuit(keyPair.GetPublicKey(publicKey));
155*cfb92d14SAndroid Build Coastguard Worker DumpBuffer("PublicKey", publicKey.GetBytes(), Ecdsa::P256::PublicKey::kSize);
156*cfb92d14SAndroid Build Coastguard Worker
157*cfb92d14SAndroid Build Coastguard Worker printf("\nHash the message ----------------------------------------------------------\n");
158*cfb92d14SAndroid Build Coastguard Worker sha256.Start();
159*cfb92d14SAndroid Build Coastguard Worker sha256.Update(kMessage, sizeof(kMessage) - 1);
160*cfb92d14SAndroid Build Coastguard Worker sha256.Finish(hash);
161*cfb92d14SAndroid Build Coastguard Worker
162*cfb92d14SAndroid Build Coastguard Worker DumpBuffer("Hash", hash.GetBytes(), sizeof(hash));
163*cfb92d14SAndroid Build Coastguard Worker
164*cfb92d14SAndroid Build Coastguard Worker printf("\nSign the message ----------------------------------------------------------\n");
165*cfb92d14SAndroid Build Coastguard Worker SuccessOrQuit(keyPair.Sign(hash, signature));
166*cfb92d14SAndroid Build Coastguard Worker DumpBuffer("Signature", signature.GetBytes(), sizeof(signature));
167*cfb92d14SAndroid Build Coastguard Worker
168*cfb92d14SAndroid Build Coastguard Worker printf("\nVerify the signature ------------------------------------------------------\n");
169*cfb92d14SAndroid Build Coastguard Worker SuccessOrQuit(publicKey.Verify(hash, signature));
170*cfb92d14SAndroid Build Coastguard Worker printf("\nSignature was verified successfully.");
171*cfb92d14SAndroid Build Coastguard Worker
172*cfb92d14SAndroid Build Coastguard Worker sha256.Start();
173*cfb92d14SAndroid Build Coastguard Worker sha256.Update(kMessage, sizeof(kMessage)); // include null char
174*cfb92d14SAndroid Build Coastguard Worker sha256.Finish(hash);
175*cfb92d14SAndroid Build Coastguard Worker VerifyOrQuit(publicKey.Verify(hash, signature) != kErrorNone, "PublicKey::Verify() passed for invalid signature");
176*cfb92d14SAndroid Build Coastguard Worker printf("\nSignature verification correctly failed with incorrect hash/signature.\n\n");
177*cfb92d14SAndroid Build Coastguard Worker
178*cfb92d14SAndroid Build Coastguard Worker testFreeInstance(instance);
179*cfb92d14SAndroid Build Coastguard Worker }
180*cfb92d14SAndroid Build Coastguard Worker
181*cfb92d14SAndroid Build Coastguard Worker } // namespace Crypto
182*cfb92d14SAndroid Build Coastguard Worker } // namespace ot
183*cfb92d14SAndroid Build Coastguard Worker
184*cfb92d14SAndroid Build Coastguard Worker #endif // OPENTHREAD_CONFIG_ECDSA_ENABLE
185*cfb92d14SAndroid Build Coastguard Worker
main(void)186*cfb92d14SAndroid Build Coastguard Worker int main(void)
187*cfb92d14SAndroid Build Coastguard Worker {
188*cfb92d14SAndroid Build Coastguard Worker #if OPENTHREAD_CONFIG_ECDSA_ENABLE
189*cfb92d14SAndroid Build Coastguard Worker ot::Crypto::TestEcdsaVector();
190*cfb92d14SAndroid Build Coastguard Worker ot::Crypto::TestEcdsaKeyGenerationSignAndVerify();
191*cfb92d14SAndroid Build Coastguard Worker printf("All tests passed\n");
192*cfb92d14SAndroid Build Coastguard Worker #else
193*cfb92d14SAndroid Build Coastguard Worker printf("ECDSA feature is not enabled\n");
194*cfb92d14SAndroid Build Coastguard Worker #endif
195*cfb92d14SAndroid Build Coastguard Worker
196*cfb92d14SAndroid Build Coastguard Worker return 0;
197*cfb92d14SAndroid Build Coastguard Worker }
198