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