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