|
| 1 | +/* |
| 2 | + * Copyright (c) 2026, 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 | +#include "common/code_utils.hpp" |
| 30 | +#include "common/debug.hpp" |
| 31 | +#include "thread/key_manager.hpp" |
| 32 | + |
| 33 | +#include "test_platform.h" |
| 34 | +#include "test_util.hpp" |
| 35 | + |
| 36 | +namespace ot { |
| 37 | + |
| 38 | +#if OPENTHREAD_CONFIG_THREAD_DIRECT_WAKE_INITIATOR_ENABLE || OPENTHREAD_CONFIG_THREAD_DIRECT_WAKE_LISTENER_ENABLE |
| 39 | + |
| 40 | +void TestWakeKeyDerivation(void) |
| 41 | +{ |
| 42 | + // `KeyManager::GetDefaultWakeKey()` matches the first 16 bytes of |
| 43 | + // HMAC-SHA256 over the literal "Thread-Wake" using the Thread Network Key |
| 44 | + // as the HMAC key (see `KeyManager::GetDefaultWakeKey()`). |
| 45 | + // |
| 46 | + // Test vector (network key is non-zero on purpose): |
| 47 | + // Network Key : 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f |
| 48 | + // Wake Key : f5 ca 7d 94 57 48 c4 55 e8 ad 69 6a 76 fb 18 d0 |
| 49 | + // |
| 50 | + // Recompute with: |
| 51 | + // python3 -c "import |
| 52 | + // hmac,hashlib;k=bytes(range(16));print(hmac.new(k,b'Thread-Wake',hashlib.sha256).digest()[:16].hex())" |
| 53 | + |
| 54 | + constexpr uint8_t kNetworkKeyBytes[OT_NETWORK_KEY_SIZE] = { |
| 55 | + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, |
| 56 | + }; |
| 57 | + |
| 58 | + constexpr uint8_t kExpectedWakeKey[Mac::Key::kSize] = { |
| 59 | + 0xf5, 0xca, 0x7d, 0x94, 0x57, 0x48, 0xc4, 0x55, 0xe8, 0xad, 0x69, 0x6a, 0x76, 0xfb, 0x18, 0xd0, |
| 60 | + }; |
| 61 | + |
| 62 | + Instance *instance; |
| 63 | + NetworkKey networkKey; |
| 64 | + Mac::Key derivedKey; |
| 65 | + |
| 66 | + instance = testInitInstance(); |
| 67 | + VerifyOrQuit(instance != nullptr); |
| 68 | + |
| 69 | + memcpy(networkKey.m8, kNetworkKeyBytes, sizeof(networkKey.m8)); |
| 70 | + instance->Get<KeyManager>().SetNetworkKey(networkKey); |
| 71 | + |
| 72 | + const Mac::KeyMaterial &wakeMaterial = instance->Get<KeyManager>().GetDefaultWakeKey(); |
| 73 | + |
| 74 | + wakeMaterial.ExtractKey(derivedKey); |
| 75 | + VerifyOrQuit(memcmp(derivedKey.GetBytes(), kExpectedWakeKey, Mac::Key::kSize) == 0, "Wake key derivation mismatch"); |
| 76 | + |
| 77 | + testFreeInstance(instance); |
| 78 | +} |
| 79 | + |
| 80 | +#endif // OPENTHREAD_CONFIG_THREAD_DIRECT_WAKE_INITIATOR_ENABLE || OPENTHREAD_CONFIG_THREAD_DIRECT_WAKE_LISTENER_ENABLE |
| 81 | + |
| 82 | +} // namespace ot |
| 83 | + |
| 84 | +int main(void) |
| 85 | +{ |
| 86 | +#if OPENTHREAD_CONFIG_THREAD_DIRECT_WAKE_INITIATOR_ENABLE || OPENTHREAD_CONFIG_THREAD_DIRECT_WAKE_LISTENER_ENABLE |
| 87 | + ot::TestWakeKeyDerivation(); |
| 88 | +#endif |
| 89 | + printf("All tests passed\n"); |
| 90 | + return 0; |
| 91 | +} |
0 commit comments