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 
37 #include "common/crc16.hpp"
38 #include "common/debug.hpp"
39 #include "common/locator_getters.hpp"
40 #include "common/string.hpp"
41 #include "crypto/sha256.hpp"
42 #include "mac/mac_types.hpp"
43 #include "thread/thread_netif.hpp"
44 
45 namespace ot {
46 
47 RegisterLogModule("MeshCoP");
48 
49 namespace MeshCoP {
50 
SetFrom(const char * aPskdString)51 Error JoinerPskd::SetFrom(const char *aPskdString)
52 {
53     Error error = kErrorNone;
54 
55     VerifyOrExit(IsPskdValid(aPskdString), error = kErrorInvalidArgs);
56 
57     Clear();
58     memcpy(m8, aPskdString, StringLength(aPskdString, sizeof(m8)));
59 
60 exit:
61     return error;
62 }
63 
operator ==(const JoinerPskd & aOther) const64 bool JoinerPskd::operator==(const JoinerPskd &aOther) const
65 {
66     bool isEqual = true;
67 
68     for (size_t i = 0; i < sizeof(m8); i++)
69     {
70         if (m8[i] != aOther.m8[i])
71         {
72             isEqual = false;
73             ExitNow();
74         }
75 
76         if (m8[i] == '\0')
77         {
78             break;
79         }
80     }
81 
82 exit:
83     return isEqual;
84 }
85 
IsPskdValid(const char * aPskdString)86 bool JoinerPskd::IsPskdValid(const char *aPskdString)
87 {
88     bool     valid      = false;
89     uint16_t pskdLength = StringLength(aPskdString, kMaxLength + 1);
90 
91     VerifyOrExit(pskdLength >= kMinLength && pskdLength <= kMaxLength);
92 
93     for (uint16_t i = 0; i < pskdLength; i++)
94     {
95         char c = aPskdString[i];
96 
97         VerifyOrExit(isdigit(c) || isupper(c));
98         VerifyOrExit(c != 'I' && c != 'O' && c != 'Q' && c != 'Z');
99     }
100 
101     valid = true;
102 
103 exit:
104     return valid;
105 }
106 
GenerateJoinerId(Mac::ExtAddress & aJoinerId) const107 void JoinerDiscerner::GenerateJoinerId(Mac::ExtAddress &aJoinerId) const
108 {
109     aJoinerId.GenerateRandom();
110     CopyTo(aJoinerId);
111     aJoinerId.SetLocal(true);
112 }
113 
Matches(const Mac::ExtAddress & aJoinerId) const114 bool JoinerDiscerner::Matches(const Mac::ExtAddress &aJoinerId) const
115 {
116     uint64_t mask;
117 
118     OT_ASSERT(IsValid());
119 
120     mask = GetMask();
121 
122     return (BigEndian::ReadUint64(aJoinerId.m8) & mask) == (mValue & mask);
123 }
124 
CopyTo(Mac::ExtAddress & aExtAddress) const125 void JoinerDiscerner::CopyTo(Mac::ExtAddress &aExtAddress) const
126 {
127     // Copies the discerner value up to its bit length to `aExtAddress`
128     // array, assuming big-endian encoding (i.e., the discerner lowest bits
129     // are copied at end of `aExtAddress.m8[]` array). Any initial/remaining
130     // bits of `aExtAddress` array remain unchanged.
131 
132     uint8_t *cur       = &aExtAddress.m8[sizeof(Mac::ExtAddress) - 1];
133     uint8_t  remaining = mLength;
134     uint64_t value     = mValue;
135 
136     OT_ASSERT(IsValid());
137 
138     // Write full bytes
139     while (remaining >= kBitsPerByte)
140     {
141         *cur = static_cast<uint8_t>(value & 0xff);
142         value >>= kBitsPerByte;
143         cur--;
144         remaining -= kBitsPerByte;
145     }
146 
147     // Write any remaining bits (not a full byte)
148     if (remaining != 0)
149     {
150         uint8_t mask = static_cast<uint8_t>((1U << remaining) - 1);
151 
152         // `mask` has it lower (lsb) `remaining` bits as `1` and rest as `0`.
153         // Example with `remaining = 3` -> (1 << 3) - 1 = 0b1000 - 1 = 0b0111.
154 
155         *cur &= ~mask;
156         *cur |= static_cast<uint8_t>(value & mask);
157     }
158 }
159 
operator ==(const JoinerDiscerner & aOther) const160 bool JoinerDiscerner::operator==(const JoinerDiscerner &aOther) const
161 {
162     uint64_t mask = GetMask();
163 
164     return IsValid() && (mLength == aOther.mLength) && ((mValue & mask) == (aOther.mValue & mask));
165 }
166 
ToString(void) const167 JoinerDiscerner::InfoString JoinerDiscerner::ToString(void) const
168 {
169     InfoString string;
170 
171     if (mLength <= BitSizeOf(uint16_t))
172     {
173         string.Append("0x%04x", static_cast<uint16_t>(mValue));
174     }
175     else if (mLength <= BitSizeOf(uint32_t))
176     {
177         string.Append("0x%08lx", ToUlong(static_cast<uint32_t>(mValue)));
178     }
179     else
180     {
181         string.Append("0x%lx-%08lx", ToUlong(static_cast<uint32_t>(mValue >> 32)),
182                       ToUlong(static_cast<uint32_t>(mValue)));
183     }
184 
185     string.Append("/len:%d", mLength);
186 
187     return string;
188 }
189 
Init(uint8_t aLength)190 void SteeringData::Init(uint8_t aLength)
191 {
192     OT_ASSERT(aLength <= kMaxLength);
193     mLength = aLength;
194     memset(m8, 0, sizeof(m8));
195 }
196 
SetToPermitAllJoiners(void)197 void SteeringData::SetToPermitAllJoiners(void)
198 {
199     Init(1);
200     m8[0] = kPermitAll;
201 }
202 
UpdateBloomFilter(const Mac::ExtAddress & aJoinerId)203 void SteeringData::UpdateBloomFilter(const Mac::ExtAddress &aJoinerId)
204 {
205     HashBitIndexes indexes;
206 
207     CalculateHashBitIndexes(aJoinerId, indexes);
208     UpdateBloomFilter(indexes);
209 }
210 
UpdateBloomFilter(const JoinerDiscerner & aDiscerner)211 void SteeringData::UpdateBloomFilter(const JoinerDiscerner &aDiscerner)
212 {
213     HashBitIndexes indexes;
214 
215     CalculateHashBitIndexes(aDiscerner, indexes);
216     UpdateBloomFilter(indexes);
217 }
218 
UpdateBloomFilter(const HashBitIndexes & aIndexes)219 void SteeringData::UpdateBloomFilter(const HashBitIndexes &aIndexes)
220 {
221     OT_ASSERT((mLength > 0) && (mLength <= kMaxLength));
222 
223     SetBit(aIndexes.mIndex[0] % GetNumBits());
224     SetBit(aIndexes.mIndex[1] % GetNumBits());
225 }
226 
Contains(const Mac::ExtAddress & aJoinerId) const227 bool SteeringData::Contains(const Mac::ExtAddress &aJoinerId) const
228 {
229     HashBitIndexes indexes;
230 
231     CalculateHashBitIndexes(aJoinerId, indexes);
232 
233     return Contains(indexes);
234 }
235 
Contains(const JoinerDiscerner & aDiscerner) const236 bool SteeringData::Contains(const JoinerDiscerner &aDiscerner) const
237 {
238     HashBitIndexes indexes;
239 
240     CalculateHashBitIndexes(aDiscerner, indexes);
241 
242     return Contains(indexes);
243 }
244 
Contains(const HashBitIndexes & aIndexes) const245 bool SteeringData::Contains(const HashBitIndexes &aIndexes) const
246 {
247     return (mLength > 0) && GetBit(aIndexes.mIndex[0] % GetNumBits()) && GetBit(aIndexes.mIndex[1] % GetNumBits());
248 }
249 
CalculateHashBitIndexes(const Mac::ExtAddress & aJoinerId,HashBitIndexes & aIndexes)250 void SteeringData::CalculateHashBitIndexes(const Mac::ExtAddress &aJoinerId, HashBitIndexes &aIndexes)
251 {
252     Crc16 ccitt(Crc16::kCcitt);
253     Crc16 ansi(Crc16::kAnsi);
254 
255     for (uint8_t b : aJoinerId.m8)
256     {
257         ccitt.Update(b);
258         ansi.Update(b);
259     }
260 
261     aIndexes.mIndex[0] = ccitt.Get();
262     aIndexes.mIndex[1] = ansi.Get();
263 }
264 
CalculateHashBitIndexes(const JoinerDiscerner & aDiscerner,HashBitIndexes & aIndexes)265 void SteeringData::CalculateHashBitIndexes(const JoinerDiscerner &aDiscerner, HashBitIndexes &aIndexes)
266 {
267     Mac::ExtAddress address;
268 
269     address.Clear();
270     aDiscerner.CopyTo(address);
271 
272     CalculateHashBitIndexes(address, aIndexes);
273 }
274 
DoesAllMatch(uint8_t aMatch) const275 bool SteeringData::DoesAllMatch(uint8_t aMatch) const
276 {
277     bool matches = true;
278 
279     for (uint8_t i = 0; i < mLength; i++)
280     {
281         if (m8[i] != aMatch)
282         {
283             matches = false;
284             break;
285         }
286     }
287 
288     return matches;
289 }
290 
ComputeJoinerId(const Mac::ExtAddress & aEui64,Mac::ExtAddress & aJoinerId)291 void ComputeJoinerId(const Mac::ExtAddress &aEui64, Mac::ExtAddress &aJoinerId)
292 {
293     Crypto::Sha256       sha256;
294     Crypto::Sha256::Hash hash;
295 
296     sha256.Start();
297     sha256.Update(aEui64);
298     sha256.Finish(hash);
299 
300     memcpy(&aJoinerId, hash.GetBytes(), sizeof(aJoinerId));
301     aJoinerId.SetLocal(true);
302 }
303 
304 #if OPENTHREAD_FTD
GeneratePskc(const char * aPassPhrase,const NetworkName & aNetworkName,const ExtendedPanId & aExtPanId,Pskc & aPskc)305 Error GeneratePskc(const char          *aPassPhrase,
306                    const NetworkName   &aNetworkName,
307                    const ExtendedPanId &aExtPanId,
308                    Pskc                &aPskc)
309 {
310     Error      error        = kErrorNone;
311     const char saltPrefix[] = "Thread";
312     uint8_t    salt[OT_CRYPTO_PBDKF2_MAX_SALT_SIZE];
313     uint16_t   saltLen = 0;
314     uint16_t   passphraseLen;
315     uint8_t    networkNameLen;
316 
317     VerifyOrExit(IsValidUtf8String(aPassPhrase), error = kErrorInvalidArgs);
318 
319     passphraseLen  = static_cast<uint16_t>(StringLength(aPassPhrase, OT_COMMISSIONING_PASSPHRASE_MAX_SIZE + 1));
320     networkNameLen = static_cast<uint8_t>(StringLength(aNetworkName.GetAsCString(), OT_NETWORK_NAME_MAX_SIZE + 1));
321 
322     VerifyOrExit((passphraseLen >= OT_COMMISSIONING_PASSPHRASE_MIN_SIZE) &&
323                      (passphraseLen <= OT_COMMISSIONING_PASSPHRASE_MAX_SIZE) &&
324                      (networkNameLen <= OT_NETWORK_NAME_MAX_SIZE),
325                  error = kErrorInvalidArgs);
326 
327     memset(salt, 0, sizeof(salt));
328     memcpy(salt, saltPrefix, sizeof(saltPrefix) - 1);
329     saltLen += static_cast<uint16_t>(sizeof(saltPrefix) - 1);
330 
331     memcpy(salt + saltLen, aExtPanId.m8, sizeof(aExtPanId));
332     saltLen += OT_EXT_PAN_ID_SIZE;
333 
334     memcpy(salt + saltLen, aNetworkName.GetAsCString(), networkNameLen);
335     saltLen += networkNameLen;
336 
337     error = otPlatCryptoPbkdf2GenerateKey(reinterpret_cast<const uint8_t *>(aPassPhrase), passphraseLen, salt, saltLen,
338                                           16384, OT_PSKC_MAX_SIZE, aPskc.m8);
339 
340 exit:
341     return error;
342 }
343 #endif // OPENTHREAD_FTD
344 
345 #if OT_SHOULD_LOG_AT(OT_LOG_LEVEL_WARN)
LogError(const char * aActionText,Error aError)346 void LogError(const char *aActionText, Error aError)
347 {
348     if (aError != kErrorNone && aError != kErrorAlready)
349     {
350         LogWarn("Failed to %s: %s", aActionText, ErrorToString(aError));
351     }
352 }
353 #endif
354 
355 } // namespace MeshCoP
356 } // namespace ot
357