1 /*
2  *  Copyright (c) 2017, 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 #include <openthread/config.h>
29 
30 #include "meshcop/commissioner.hpp"
31 #include "meshcop/meshcop.hpp"
32 
33 #include "test_platform.h"
34 #include "test_util.h"
35 
36 namespace ot {
37 
38 #if OPENTHREAD_FTD
39 
TestMinimumPassphrase(void)40 void TestMinimumPassphrase(void)
41 {
42     ot::Pskc              pskc;
43     const uint8_t         expectedPskc[] = {0x44, 0x98, 0x8e, 0x22, 0xcf, 0x65, 0x2e, 0xee,
44                                             0xcc, 0xd1, 0xe4, 0xc0, 0x1d, 0x01, 0x54, 0xf8};
45     const otExtendedPanId xpanid         = {{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08}};
46     const char            passphrase[]   = "123456";
47     otInstance           *instance       = testInitInstance();
48     SuccessOrQuit(ot::MeshCoP::GeneratePskc(passphrase,
49                                             *reinterpret_cast<const ot::MeshCoP::NetworkName *>("OpenThread"),
50                                             static_cast<const ot::MeshCoP::ExtendedPanId &>(xpanid), pskc));
51     VerifyOrQuit(memcmp(pskc.m8, expectedPskc, OT_PSKC_MAX_SIZE) == 0);
52     testFreeInstance(instance);
53 }
54 
TestMaximumPassphrase(void)55 void TestMaximumPassphrase(void)
56 {
57     ot::Pskc              pskc;
58     const uint8_t         expectedPskc[] = {0x9e, 0x81, 0xbd, 0x35, 0xa2, 0x53, 0x76, 0x2f,
59                                             0x80, 0xee, 0x04, 0xff, 0x2f, 0xa2, 0x85, 0xe9};
60     const otExtendedPanId xpanid         = {{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08}};
61     const char            passphrase[]   = "1234567812345678"
62                                            "1234567812345678"
63                                            "1234567812345678"
64                                            "1234567812345678"
65                                            "1234567812345678"
66                                            "1234567812345678"
67                                            "1234567812345678"
68                                            "1234567812345678"
69                                            "1234567812345678"
70                                            "1234567812345678"
71                                            "1234567812345678"
72                                            "1234567812345678"
73                                            "1234567812345678"
74                                            "1234567812345678"
75                                            "1234567812345678"
76                                            "123456781234567";
77 
78     otInstance *instance = testInitInstance();
79     SuccessOrQuit(ot::MeshCoP::GeneratePskc(passphrase,
80                                             *reinterpret_cast<const ot::MeshCoP::NetworkName *>("OpenThread"),
81                                             static_cast<const ot::MeshCoP::ExtendedPanId &>(xpanid), pskc));
82     VerifyOrQuit(memcmp(pskc.m8, expectedPskc, sizeof(pskc.m8)) == 0);
83     testFreeInstance(instance);
84 }
85 
TestExampleInSpec(void)86 void TestExampleInSpec(void)
87 {
88     ot::Pskc              pskc;
89     const uint8_t         expectedPskc[] = {0xc3, 0xf5, 0x93, 0x68, 0x44, 0x5a, 0x1b, 0x61,
90                                             0x06, 0xbe, 0x42, 0x0a, 0x70, 0x6d, 0x4c, 0xc9};
91     const otExtendedPanId xpanid         = {{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07}};
92     const char            passphrase[]   = "12SECRETPASSWORD34";
93 
94     otInstance *instance = testInitInstance();
95     SuccessOrQuit(ot::MeshCoP::GeneratePskc(passphrase,
96                                             *reinterpret_cast<const ot::MeshCoP::NetworkName *>("Test Network"),
97                                             static_cast<const ot::MeshCoP::ExtendedPanId &>(xpanid), pskc));
98     VerifyOrQuit(memcmp(pskc.m8, expectedPskc, sizeof(pskc.m8)) == 0);
99     testFreeInstance(instance);
100 }
101 
102 } // namespace ot
103 
104 #endif // OPENTHREAD_FTD
105 
main(void)106 int main(void)
107 {
108 #if OPENTHREAD_FTD
109     ot::TestMinimumPassphrase();
110     ot::TestMaximumPassphrase();
111     ot::TestExampleInSpec();
112     printf("All tests passed\n");
113 #else
114     printf("PSKc generation is not supported on non-ftd build\n");
115 #endif
116     return 0;
117 }
118