• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
29 /**
30  * @file
31  *   This file implements the generating pskc function.
32  */
33 
34 #include "utils/pskc.hpp"
35 
36 #include "common/code_utils.hpp"
37 #include "common/logging.hpp"
38 
39 namespace otbr {
40 namespace Psk {
41 
SetSalt(const uint8_t * aExtPanId,const char * aNetworkName)42 void Pskc::SetSalt(const uint8_t *aExtPanId, const char *aNetworkName)
43 {
44     const char *saltPrefix = "Thread";
45     int         cur        = 0;
46     int         ret        = kPskcStatus_Ok;
47 
48     memset(mSalt, 0, sizeof(mSalt));
49     memcpy(mSalt, saltPrefix, strlen(saltPrefix));
50     cur += strlen(saltPrefix);
51 
52     memcpy(mSalt + cur, aExtPanId, OT_EXTENDED_PAN_ID_LENGTH);
53     cur += OT_EXTENDED_PAN_ID_LENGTH;
54 
55     VerifyOrExit(strlen(aNetworkName) > 0, ret = kPskcStatus_InvalidArgument);
56     memcpy(mSalt + cur, aNetworkName, strlen(aNetworkName));
57     cur += strlen(aNetworkName);
58 
59     mSaltLen = static_cast<uint16_t>(cur);
60 
61 exit:
62     if (ret != kPskcStatus_Ok)
63     {
64         otbrLogErr("ExtPanId or NetworkName is nullptr");
65     }
66     return;
67 }
68 
ComputePskc(const uint8_t * aExtPanId,const char * aNetworkName,const char * aPassphrase)69 const uint8_t *Pskc::ComputePskc(const uint8_t *aExtPanId, const char *aNetworkName, const char *aPassphrase)
70 {
71     uint32_t blockCounter = 0;
72     uint16_t useLen       = 0;
73     uint16_t prfBlockLen  = MBEDTLS_CIPHER_BLKSIZE_MAX;
74     uint8_t  prfInput[OT_PBKDF2_SALT_MAX_LENGTH + 4];
75     uint8_t  prfOutput[MBEDTLS_CIPHER_BLKSIZE_MAX];
76     uint8_t  keyBlock[MBEDTLS_CIPHER_BLKSIZE_MAX];
77     uint16_t keyLen = OT_PSKC_LENGTH;
78     uint8_t *pskc   = mPskc;
79 
80     SetSalt(aExtPanId, aNetworkName);
81 
82     while (keyLen)
83     {
84         memcpy(prfInput, mSalt, mSaltLen);
85 
86         blockCounter++;
87         prfInput[mSaltLen + 0] = (uint8_t)(blockCounter >> 24);
88         prfInput[mSaltLen + 1] = (uint8_t)(blockCounter >> 16);
89         prfInput[mSaltLen + 2] = (uint8_t)(blockCounter >> 8);
90         prfInput[mSaltLen + 3] = (uint8_t)(blockCounter);
91         // Calculate U_1
92         mbedtls_aes_cmac_prf_128(reinterpret_cast<const uint8_t *>(aPassphrase), strlen(aPassphrase), prfInput,
93                                  mSaltLen + 4, prfOutput);
94         memcpy(keyBlock, prfOutput, prfBlockLen);
95 
96         for (uint32_t i = 1; i < OT_ITERATION_COUNTS; i++)
97         {
98             memcpy(prfInput, prfOutput, prfBlockLen);
99 
100             // Calculate U_i
101             mbedtls_aes_cmac_prf_128(reinterpret_cast<const uint8_t *>(aPassphrase), strlen(aPassphrase), prfInput,
102                                      prfBlockLen, prfOutput);
103 
104             // xor
105             for (uint32_t j = 0; j < prfBlockLen; j++)
106             {
107                 keyBlock[j] ^= prfOutput[j];
108             }
109         }
110 
111         useLen = (keyLen < prfBlockLen) ? keyLen : prfBlockLen;
112         memcpy(pskc, keyBlock, useLen);
113         pskc += useLen;
114         keyLen -= useLen;
115     }
116     return mPskc;
117 }
118 
119 } // namespace Psk
120 } // namespace otbr
121