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 namespace MeshCoP {
38 
39 #if OPENTHREAD_FTD
40 
TestMinimumPassphrase(void)41 void TestMinimumPassphrase(void)
42 {
43     static const otExtendedPanId kExtPanId     = {{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08}};
44     static const otNetworkName   kNetworkName  = {{'O', 'p', 'e', 'n', 'T', 'h', 'r', 'e', 'a', 'd', '\0'}};
45     static const char            kPassphrase[] = "123456";
46 
47     static const otPskc kExpectedPskc = {
48         {0x44, 0x98, 0x8e, 0x22, 0xcf, 0x65, 0x2e, 0xee, 0xcc, 0xd1, 0xe4, 0xc0, 0x1d, 0x01, 0x54, 0xf8}};
49 
50     Instance *instance = testInitInstance();
51     Pskc      pskc;
52 
53     SuccessOrQuit(GeneratePskc(kPassphrase, AsCoreType(&kNetworkName), AsCoreType(&kExtPanId), pskc));
54     VerifyOrQuit(pskc == AsCoreType(&kExpectedPskc));
55 
56     testFreeInstance(instance);
57 }
58 
TestMaximumPassphrase(void)59 void TestMaximumPassphrase(void)
60 {
61     static const otExtendedPanId kExtPanId    = {{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08}};
62     static const otNetworkName   kNetworkName = {{'O', 'p', 'e', 'n', 'T', 'h', 'r', 'e', 'a', 'd', '\0'}};
63 
64     static const char kPassphrase[] = "1234567812345678"
65                                       "1234567812345678"
66                                       "1234567812345678"
67                                       "1234567812345678"
68                                       "1234567812345678"
69                                       "1234567812345678"
70                                       "1234567812345678"
71                                       "1234567812345678"
72                                       "1234567812345678"
73                                       "1234567812345678"
74                                       "1234567812345678"
75                                       "1234567812345678"
76                                       "1234567812345678"
77                                       "1234567812345678"
78                                       "1234567812345678"
79                                       "123456781234567";
80 
81     static const otPskc kExpectedPskc = {
82         {0x9e, 0x81, 0xbd, 0x35, 0xa2, 0x53, 0x76, 0x2f, 0x80, 0xee, 0x04, 0xff, 0x2f, 0xa2, 0x85, 0xe9}};
83 
84     Instance *instance = testInitInstance();
85     Pskc      pskc;
86 
87     SuccessOrQuit(GeneratePskc(kPassphrase, AsCoreType(&kNetworkName), AsCoreType(&kExtPanId), pskc));
88     VerifyOrQuit(pskc == AsCoreType(&kExpectedPskc));
89 
90     testFreeInstance(instance);
91 }
92 
TestExampleInSpec(void)93 void TestExampleInSpec(void)
94 {
95     static const otExtendedPanId kExtPanId     = {{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07}};
96     static const otNetworkName   kNetworkName  = {{'T', 'e', 's', 't', ' ', 'N', 'e', 't', 'w', 'o', 'r', 'k', '\0'}};
97     static const char            kPassphrase[] = "12SECRETPASSWORD34";
98 
99     static const otPskc kExpectedPskc = {
100         {0xc3, 0xf5, 0x93, 0x68, 0x44, 0x5a, 0x1b, 0x61, 0x06, 0xbe, 0x42, 0x0a, 0x70, 0x6d, 0x4c, 0xc9}};
101 
102     Instance *instance = testInitInstance();
103     Pskc      pskc;
104 
105     SuccessOrQuit(GeneratePskc(kPassphrase, AsCoreType(&kNetworkName), AsCoreType(&kExtPanId), pskc));
106     VerifyOrQuit(pskc == AsCoreType(&kExpectedPskc));
107 
108     testFreeInstance(instance);
109 }
110 
111 } // namespace MeshCoP
112 } // namespace ot
113 
114 #endif // OPENTHREAD_FTD
115 
main(void)116 int main(void)
117 {
118 #if OPENTHREAD_FTD
119     ot::MeshCoP::TestMinimumPassphrase();
120     ot::MeshCoP::TestMaximumPassphrase();
121     ot::MeshCoP::TestExampleInSpec();
122     printf("All tests passed\n");
123 #else
124     printf("PSKc generation is not supported on non-ftd build\n");
125 #endif
126     return 0;
127 }
128