xref: /aosp_15_r20/external/ot-br-posix/src/utils/pskc.cpp (revision 4a64e381480ef79f0532b2421e44e6ee336b8e0d)
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     int         remainingSpace = 0;
48     int         networkNameLen = 0;
49 
50     if (strlen(saltPrefix) + OT_EXTENDED_PAN_ID_LENGTH + strlen(aNetworkName) >= sizeof(mSalt))
51     {
52         otbrLogWarning("Network name too long; will be truncated.");
53     }
54 
55     memset(mSalt, 0, sizeof(mSalt));
56     memcpy(mSalt, saltPrefix, strlen(saltPrefix));
57     cur += strlen(saltPrefix);
58 
59     memcpy(mSalt + cur, aExtPanId, OT_EXTENDED_PAN_ID_LENGTH);
60     cur += OT_EXTENDED_PAN_ID_LENGTH;
61 
62     VerifyOrExit(strlen(aNetworkName) > 0, ret = kPskcStatus_InvalidArgument);
63 
64     remainingSpace = sizeof(mSalt) - cur;
65     if (remainingSpace > 0)
66     {
67         networkNameLen = std::min(static_cast<int>(strlen(aNetworkName)), remainingSpace);
68         memcpy(mSalt + cur, aNetworkName, networkNameLen);
69         cur += networkNameLen;
70     }
71 
72     mSaltLen = static_cast<uint16_t>(cur);
73 
74 exit:
75     if (ret != kPskcStatus_Ok)
76     {
77         otbrLogErr("ExtPanId or NetworkName is nullptr");
78     }
79     return;
80 }
81 
ComputePskc(const uint8_t * aExtPanId,const char * aNetworkName,const char * aPassphrase)82 const uint8_t *Pskc::ComputePskc(const uint8_t *aExtPanId, const char *aNetworkName, const char *aPassphrase)
83 {
84     uint32_t blockCounter = 0;
85     uint16_t useLen       = 0;
86     uint16_t prfBlockLen  = MBEDTLS_CIPHER_BLKSIZE_MAX;
87     uint8_t  prfInput[OT_PBKDF2_SALT_MAX_LENGTH + 4];
88     uint8_t  prfOutput[MBEDTLS_CIPHER_BLKSIZE_MAX];
89     uint8_t  keyBlock[MBEDTLS_CIPHER_BLKSIZE_MAX];
90     uint16_t keyLen = OT_PSKC_LENGTH;
91     uint8_t *pskc   = mPskc;
92 
93     SetSalt(aExtPanId, aNetworkName);
94 
95     while (keyLen)
96     {
97         memcpy(prfInput, mSalt, mSaltLen);
98 
99         blockCounter++;
100         prfInput[mSaltLen + 0] = (uint8_t)(blockCounter >> 24);
101         prfInput[mSaltLen + 1] = (uint8_t)(blockCounter >> 16);
102         prfInput[mSaltLen + 2] = (uint8_t)(blockCounter >> 8);
103         prfInput[mSaltLen + 3] = (uint8_t)(blockCounter);
104         // Calculate U_1
105         mbedtls_aes_cmac_prf_128(reinterpret_cast<const uint8_t *>(aPassphrase), strlen(aPassphrase), prfInput,
106                                  mSaltLen + 4, prfOutput);
107         memcpy(keyBlock, prfOutput, prfBlockLen);
108 
109         for (uint32_t i = 1; i < OT_ITERATION_COUNTS; i++)
110         {
111             memcpy(prfInput, prfOutput, prfBlockLen);
112 
113             // Calculate U_i
114             mbedtls_aes_cmac_prf_128(reinterpret_cast<const uint8_t *>(aPassphrase), strlen(aPassphrase), prfInput,
115                                      prfBlockLen, prfOutput);
116 
117             // xor
118             for (uint32_t j = 0; j < prfBlockLen; j++)
119             {
120                 keyBlock[j] ^= prfOutput[j];
121             }
122         }
123 
124         useLen = (keyLen < prfBlockLen) ? keyLen : prfBlockLen;
125         memcpy(pskc, keyBlock, useLen);
126         pskc += useLen;
127         keyLen -= useLen;
128     }
129     return mPskc;
130 }
131 
132 } // namespace Psk
133 } // namespace otbr
134