1 
2 /*
3  *  Copyright (c) 2017, The OpenThread Authors.
4  *  All rights reserved.
5  *
6  *  Redistribution and use in source and binary forms, with or without
7  *  modification, are permitted provided that the following conditions are met:
8  *  1. Redistributions of source code must retain the above copyright
9  *     notice, this list of conditions and the following disclaimer.
10  *  2. Redistributions in binary form must reproduce the above copyright
11  *     notice, this list of conditions and the following disclaimer in the
12  *     documentation and/or other materials provided with the distribution.
13  *  3. Neither the name of the copyright holder nor the
14  *     names of its contributors may be used to endorse or promote products
15  *     derived from this software without specific prior written permission.
16  *
17  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  *  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  *  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21  *  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  *  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  *  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  *  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  *  POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 /**
31  * @file
32  *   This file implements common MeshCoP utility functions.
33  */
34 
35 #include "meshcop.hpp"
36 #include <openthread/platform/toolchain.h>
37 
38 #include "common/clearable.hpp"
39 #include "common/crc16.hpp"
40 #include "common/debug.hpp"
41 #include "common/locator_getters.hpp"
42 #include "common/string.hpp"
43 #include "crypto/sha256.hpp"
44 #include "mac/mac_types.hpp"
45 #include "thread/thread_netif.hpp"
46 
47 namespace ot {
48 
49 RegisterLogModule("MeshCoP");
50 
51 namespace MeshCoP {
52 
SetFrom(const char * aPskdString)53 Error JoinerPskd::SetFrom(const char *aPskdString)
54 {
55     Error error = kErrorNone;
56 
57     VerifyOrExit(IsPskdValid(aPskdString), error = kErrorInvalidArgs);
58 
59     Clear();
60     memcpy(m8, aPskdString, StringLength(aPskdString, sizeof(m8)));
61 
62 exit:
63     return error;
64 }
65 
operator ==(const JoinerPskd & aOther) const66 bool JoinerPskd::operator==(const JoinerPskd &aOther) const
67 {
68     bool isEqual = true;
69 
70     for (size_t i = 0; i < sizeof(m8); i++)
71     {
72         if (m8[i] != aOther.m8[i])
73         {
74             isEqual = false;
75             ExitNow();
76         }
77 
78         if (m8[i] == '\0')
79         {
80             break;
81         }
82     }
83 
84 exit:
85     return isEqual;
86 }
87 
IsPskdValid(const char * aPskdString)88 bool JoinerPskd::IsPskdValid(const char *aPskdString)
89 {
90     bool     valid      = false;
91     uint16_t pskdLength = StringLength(aPskdString, kMaxLength + 1);
92 
93     VerifyOrExit(pskdLength >= kMinLength && pskdLength <= kMaxLength);
94 
95     for (uint16_t i = 0; i < pskdLength; i++)
96     {
97         char c = aPskdString[i];
98 
99         VerifyOrExit(IsDigit(c) || IsUppercase(c));
100         VerifyOrExit(c != 'I' && c != 'O' && c != 'Q' && c != 'Z');
101     }
102 
103     valid = true;
104 
105 exit:
106     return valid;
107 }
108 
GenerateJoinerId(Mac::ExtAddress & aJoinerId) const109 void JoinerDiscerner::GenerateJoinerId(Mac::ExtAddress &aJoinerId) const
110 {
111     aJoinerId.GenerateRandom();
112     CopyTo(aJoinerId);
113     aJoinerId.SetLocal(true);
114 }
115 
Matches(const Mac::ExtAddress & aJoinerId) const116 bool JoinerDiscerner::Matches(const Mac::ExtAddress &aJoinerId) const
117 {
118     uint64_t mask;
119 
120     OT_ASSERT(IsValid());
121 
122     mask = GetMask();
123 
124     return (BigEndian::ReadUint64(aJoinerId.m8) & mask) == (mValue & mask);
125 }
126 
CopyTo(Mac::ExtAddress & aExtAddress) const127 void JoinerDiscerner::CopyTo(Mac::ExtAddress &aExtAddress) const
128 {
129     // Copies the discerner value up to its bit length to `aExtAddress`
130     // array, assuming big-endian encoding (i.e., the discerner lowest bits
131     // are copied at end of `aExtAddress.m8[]` array). Any initial/remaining
132     // bits of `aExtAddress` array remain unchanged.
133 
134     uint8_t *cur       = &aExtAddress.m8[sizeof(Mac::ExtAddress) - 1];
135     uint8_t  remaining = mLength;
136     uint64_t value     = mValue;
137 
138     OT_ASSERT(IsValid());
139 
140     // Write full bytes
141     while (remaining >= kBitsPerByte)
142     {
143         *cur = static_cast<uint8_t>(value & 0xff);
144         value >>= kBitsPerByte;
145         cur--;
146         remaining -= kBitsPerByte;
147     }
148 
149     // Write any remaining bits (not a full byte)
150     if (remaining != 0)
151     {
152         uint8_t mask = static_cast<uint8_t>((1U << remaining) - 1);
153 
154         // `mask` has it lower (lsb) `remaining` bits as `1` and rest as `0`.
155         // Example with `remaining = 3` -> (1 << 3) - 1 = 0b1000 - 1 = 0b0111.
156 
157         *cur &= ~mask;
158         *cur |= static_cast<uint8_t>(value & mask);
159     }
160 }
161 
operator ==(const JoinerDiscerner & aOther) const162 bool JoinerDiscerner::operator==(const JoinerDiscerner &aOther) const
163 {
164     uint64_t mask = GetMask();
165 
166     return IsValid() && (mLength == aOther.mLength) && ((mValue & mask) == (aOther.mValue & mask));
167 }
168 
ToString(void) const169 JoinerDiscerner::InfoString JoinerDiscerner::ToString(void) const
170 {
171     InfoString string;
172 
173     if (mLength <= BitSizeOf(uint16_t))
174     {
175         string.Append("0x%04x", static_cast<uint16_t>(mValue));
176     }
177     else if (mLength <= BitSizeOf(uint32_t))
178     {
179         string.Append("0x%08lx", ToUlong(static_cast<uint32_t>(mValue)));
180     }
181     else
182     {
183         string.Append("0x%lx-%08lx", ToUlong(static_cast<uint32_t>(mValue >> 32)),
184                       ToUlong(static_cast<uint32_t>(mValue)));
185     }
186 
187     string.Append("/len:%d", mLength);
188 
189     return string;
190 }
191 
Init(uint8_t aLength)192 void SteeringData::Init(uint8_t aLength)
193 {
194     OT_ASSERT(aLength <= kMaxLength);
195     mLength = aLength;
196     ClearAllBytes(m8);
197 }
198 
SetToPermitAllJoiners(void)199 void SteeringData::SetToPermitAllJoiners(void)
200 {
201     Init(1);
202     m8[0] = kPermitAll;
203 }
204 
UpdateBloomFilter(const Mac::ExtAddress & aJoinerId)205 void SteeringData::UpdateBloomFilter(const Mac::ExtAddress &aJoinerId)
206 {
207     HashBitIndexes indexes;
208 
209     CalculateHashBitIndexes(aJoinerId, indexes);
210     UpdateBloomFilter(indexes);
211 }
212 
UpdateBloomFilter(const JoinerDiscerner & aDiscerner)213 void SteeringData::UpdateBloomFilter(const JoinerDiscerner &aDiscerner)
214 {
215     HashBitIndexes indexes;
216 
217     CalculateHashBitIndexes(aDiscerner, indexes);
218     UpdateBloomFilter(indexes);
219 }
220 
UpdateBloomFilter(const HashBitIndexes & aIndexes)221 void SteeringData::UpdateBloomFilter(const HashBitIndexes &aIndexes)
222 {
223     OT_ASSERT((mLength > 0) && (mLength <= kMaxLength));
224 
225     SetBit(aIndexes.mIndex[0] % GetNumBits());
226     SetBit(aIndexes.mIndex[1] % GetNumBits());
227 }
228 
Contains(const Mac::ExtAddress & aJoinerId) const229 bool SteeringData::Contains(const Mac::ExtAddress &aJoinerId) const
230 {
231     HashBitIndexes indexes;
232 
233     CalculateHashBitIndexes(aJoinerId, indexes);
234 
235     return Contains(indexes);
236 }
237 
Contains(const JoinerDiscerner & aDiscerner) const238 bool SteeringData::Contains(const JoinerDiscerner &aDiscerner) const
239 {
240     HashBitIndexes indexes;
241 
242     CalculateHashBitIndexes(aDiscerner, indexes);
243 
244     return Contains(indexes);
245 }
246 
Contains(const HashBitIndexes & aIndexes) const247 bool SteeringData::Contains(const HashBitIndexes &aIndexes) const
248 {
249     return (mLength > 0) && GetBit(aIndexes.mIndex[0] % GetNumBits()) && GetBit(aIndexes.mIndex[1] % GetNumBits());
250 }
251 
CalculateHashBitIndexes(const Mac::ExtAddress & aJoinerId,HashBitIndexes & aIndexes)252 void SteeringData::CalculateHashBitIndexes(const Mac::ExtAddress &aJoinerId, HashBitIndexes &aIndexes)
253 {
254     Crc16 ccitt(Crc16::kCcitt);
255     Crc16 ansi(Crc16::kAnsi);
256 
257     for (uint8_t b : aJoinerId.m8)
258     {
259         ccitt.Update(b);
260         ansi.Update(b);
261     }
262 
263     aIndexes.mIndex[0] = ccitt.Get();
264     aIndexes.mIndex[1] = ansi.Get();
265 }
266 
CalculateHashBitIndexes(const JoinerDiscerner & aDiscerner,HashBitIndexes & aIndexes)267 void SteeringData::CalculateHashBitIndexes(const JoinerDiscerner &aDiscerner, HashBitIndexes &aIndexes)
268 {
269     Mac::ExtAddress address;
270 
271     address.Clear();
272     aDiscerner.CopyTo(address);
273 
274     CalculateHashBitIndexes(address, aIndexes);
275 }
276 
DoesAllMatch(uint8_t aMatch) const277 bool SteeringData::DoesAllMatch(uint8_t aMatch) const
278 {
279     bool matches = true;
280 
281     for (uint8_t i = 0; i < mLength; i++)
282     {
283         if (m8[i] != aMatch)
284         {
285             matches = false;
286             break;
287         }
288     }
289 
290     return matches;
291 }
292 
ComputeJoinerId(const Mac::ExtAddress & aEui64,Mac::ExtAddress & aJoinerId)293 void ComputeJoinerId(const Mac::ExtAddress &aEui64, Mac::ExtAddress &aJoinerId)
294 {
295     Crypto::Sha256       sha256;
296     Crypto::Sha256::Hash hash;
297 
298     sha256.Start();
299     sha256.Update(aEui64);
300     sha256.Finish(hash);
301 
302     memcpy(&aJoinerId, hash.GetBytes(), sizeof(aJoinerId));
303     aJoinerId.SetLocal(true);
304 }
305 
306 #if OPENTHREAD_FTD
GeneratePskc(const char * aPassPhrase,const NetworkName & aNetworkName,const ExtendedPanId & aExtPanId,Pskc & aPskc)307 Error GeneratePskc(const char          *aPassPhrase,
308                    const NetworkName   &aNetworkName,
309                    const ExtendedPanId &aExtPanId,
310                    Pskc                &aPskc)
311 {
312     Error      error        = kErrorNone;
313     const char saltPrefix[] = "Thread";
314     uint8_t    salt[OT_CRYPTO_PBDKF2_MAX_SALT_SIZE];
315     uint16_t   saltLen = 0;
316     uint16_t   passphraseLen;
317     uint8_t    networkNameLen;
318 
319     VerifyOrExit(IsValidUtf8String(aPassPhrase), error = kErrorInvalidArgs);
320 
321     passphraseLen  = static_cast<uint16_t>(StringLength(aPassPhrase, OT_COMMISSIONING_PASSPHRASE_MAX_SIZE + 1));
322     networkNameLen = static_cast<uint8_t>(StringLength(aNetworkName.GetAsCString(), OT_NETWORK_NAME_MAX_SIZE + 1));
323 
324     VerifyOrExit((passphraseLen >= OT_COMMISSIONING_PASSPHRASE_MIN_SIZE) &&
325                      (passphraseLen <= OT_COMMISSIONING_PASSPHRASE_MAX_SIZE) &&
326                      (networkNameLen <= OT_NETWORK_NAME_MAX_SIZE),
327                  error = kErrorInvalidArgs);
328 
329     ClearAllBytes(salt);
330     memcpy(salt, saltPrefix, sizeof(saltPrefix) - 1);
331     saltLen += static_cast<uint16_t>(sizeof(saltPrefix) - 1);
332 
333     memcpy(salt + saltLen, aExtPanId.m8, sizeof(aExtPanId));
334     saltLen += OT_EXT_PAN_ID_SIZE;
335 
336     memcpy(salt + saltLen, aNetworkName.GetAsCString(), networkNameLen);
337     saltLen += networkNameLen;
338 
339     error = otPlatCryptoPbkdf2GenerateKey(reinterpret_cast<const uint8_t *>(aPassPhrase), passphraseLen, salt, saltLen,
340                                           16384, OT_PSKC_MAX_SIZE, aPskc.m8);
341 
342 exit:
343     return error;
344 }
345 #endif // OPENTHREAD_FTD
346 
347 } // namespace MeshCoP
348 } // namespace ot
349