2 * Copyright (c) 2017, The OpenThread Authors.
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.
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.
31 * This file implements the generating pskc function.
34 #include "utils/pskc.hpp"
36 #include "common/code_utils.hpp"
37 #include "common/logging.hpp"
42 void Pskc::SetSalt(const uint8_t *aExtPanId, const char *aNetworkName)
44 const char *saltPrefix = "Thread";
46 int ret = kPskcStatus_Ok;
48 memset(mSalt, 0, sizeof(mSalt));
49 memcpy(mSalt, saltPrefix, strlen(saltPrefix));
50 cur += strlen(saltPrefix);
52 memcpy(mSalt + cur, aExtPanId, OT_EXTENDED_PAN_ID_LENGTH);
53 cur += OT_EXTENDED_PAN_ID_LENGTH;
55 VerifyOrExit(strlen(aNetworkName) > 0, ret = kPskcStatus_InvalidArgument);
56 memcpy(mSalt + cur, aNetworkName, strlen(aNetworkName));
57 cur += strlen(aNetworkName);
59 mSaltLen = static_cast<uint16_t>(cur);
62 if (ret != kPskcStatus_Ok)
64 otbrLog(OTBR_LOG_ERR, "ExtPanId or NetworkName is nullptr");
69 const uint8_t *Pskc::ComputePskc(const uint8_t *aExtPanId, const char *aNetworkName, const char *aPassphrase)
71 uint32_t blockCounter = 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;
80 SetSalt(aExtPanId, aNetworkName);
84 memcpy(prfInput, mSalt, mSaltLen);
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);
92 mbedtls_aes_cmac_prf_128(reinterpret_cast<const uint8_t *>(aPassphrase), strlen(aPassphrase), prfInput,
93 mSaltLen + 4, prfOutput);
94 memcpy(keyBlock, prfOutput, prfBlockLen);
96 for (uint32_t i = 1; i < OT_ITERATION_COUNTS; i++)
98 memcpy(prfInput, prfOutput, prfBlockLen);
101 mbedtls_aes_cmac_prf_128(reinterpret_cast<const uint8_t *>(aPassphrase), strlen(aPassphrase), prfInput,
102 prfBlockLen, prfOutput);
105 for (uint32_t j = 0; j < prfBlockLen; j++)
107 keyBlock[j] ^= prfOutput[j];
111 useLen = (keyLen < prfBlockLen) ? keyLen : prfBlockLen;
112 memcpy(pskc, keyBlock, useLen);