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