1*4a64e381SAndroid Build Coastguard Worker /*
2*4a64e381SAndroid Build Coastguard Worker * Copyright (c) 2018, The OpenThread Authors.
3*4a64e381SAndroid Build Coastguard Worker * All rights reserved.
4*4a64e381SAndroid Build Coastguard Worker *
5*4a64e381SAndroid Build Coastguard Worker * Redistribution and use in source and binary forms, with or without
6*4a64e381SAndroid Build Coastguard Worker * modification, are permitted provided that the following conditions are met:
7*4a64e381SAndroid Build Coastguard Worker * 1. Redistributions of source code must retain the above copyright
8*4a64e381SAndroid Build Coastguard Worker * notice, this list of conditions and the following disclaimer.
9*4a64e381SAndroid Build Coastguard Worker * 2. Redistributions in binary form must reproduce the above copyright
10*4a64e381SAndroid Build Coastguard Worker * notice, this list of conditions and the following disclaimer in the
11*4a64e381SAndroid Build Coastguard Worker * documentation and/or other materials provided with the distribution.
12*4a64e381SAndroid Build Coastguard Worker * 3. Neither the name of the copyright holder nor the
13*4a64e381SAndroid Build Coastguard Worker * names of its contributors may be used to endorse or promote products
14*4a64e381SAndroid Build Coastguard Worker * derived from this software without specific prior written permission.
15*4a64e381SAndroid Build Coastguard Worker *
16*4a64e381SAndroid Build Coastguard Worker * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17*4a64e381SAndroid Build Coastguard Worker * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18*4a64e381SAndroid Build Coastguard Worker * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19*4a64e381SAndroid Build Coastguard Worker * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20*4a64e381SAndroid Build Coastguard Worker * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21*4a64e381SAndroid Build Coastguard Worker * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22*4a64e381SAndroid Build Coastguard Worker * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23*4a64e381SAndroid Build Coastguard Worker * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24*4a64e381SAndroid Build Coastguard Worker * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25*4a64e381SAndroid Build Coastguard Worker * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26*4a64e381SAndroid Build Coastguard Worker * POSSIBILITY OF SUCH DAMAGE.
27*4a64e381SAndroid Build Coastguard Worker */
28*4a64e381SAndroid Build Coastguard Worker
29*4a64e381SAndroid Build Coastguard Worker /**
30*4a64e381SAndroid Build Coastguard Worker * @file
31*4a64e381SAndroid Build Coastguard Worker * This file implements a simple tool to compute pskc.
32*4a64e381SAndroid Build Coastguard Worker */
33*4a64e381SAndroid Build Coastguard Worker
34*4a64e381SAndroid Build Coastguard Worker #include <mbedtls/sha256.h>
35*4a64e381SAndroid Build Coastguard Worker #include <stdio.h>
36*4a64e381SAndroid Build Coastguard Worker #include <stdlib.h>
37*4a64e381SAndroid Build Coastguard Worker #include <sysexits.h>
38*4a64e381SAndroid Build Coastguard Worker
39*4a64e381SAndroid Build Coastguard Worker #include "common/code_utils.hpp"
40*4a64e381SAndroid Build Coastguard Worker #include "utils/hex.hpp"
41*4a64e381SAndroid Build Coastguard Worker #include "utils/steering_data.hpp"
42*4a64e381SAndroid Build Coastguard Worker
help(void)43*4a64e381SAndroid Build Coastguard Worker void help(void)
44*4a64e381SAndroid Build Coastguard Worker {
45*4a64e381SAndroid Build Coastguard Worker printf("steering-data - compute steering data\n"
46*4a64e381SAndroid Build Coastguard Worker "SYNTAX:\n"
47*4a64e381SAndroid Build Coastguard Worker " steering-data [LENGTH] <JOINER_ID> ...\n"
48*4a64e381SAndroid Build Coastguard Worker "EXAMPLE:\n"
49*4a64e381SAndroid Build Coastguard Worker " steering-data 18b4300000000001\n"
50*4a64e381SAndroid Build Coastguard Worker " steering-data 15 18b4300000000001\n"
51*4a64e381SAndroid Build Coastguard Worker " steering-data 18b4300000000001 18b4300000000002\n");
52*4a64e381SAndroid Build Coastguard Worker }
53*4a64e381SAndroid Build Coastguard Worker
ComputeJoinerId(const char * aEui64,uint8_t * aJoinerId)54*4a64e381SAndroid Build Coastguard Worker int ComputeJoinerId(const char *aEui64, uint8_t *aJoinerId)
55*4a64e381SAndroid Build Coastguard Worker {
56*4a64e381SAndroid Build Coastguard Worker int ret = -1;
57*4a64e381SAndroid Build Coastguard Worker
58*4a64e381SAndroid Build Coastguard Worker VerifyOrExit(strlen(aEui64) == otbr::SteeringData::kSizeJoinerId * 2);
59*4a64e381SAndroid Build Coastguard Worker VerifyOrExit(otbr::Utils::Hex2Bytes(aEui64, aJoinerId, otbr::SteeringData::kSizeJoinerId) ==
60*4a64e381SAndroid Build Coastguard Worker otbr::SteeringData::kSizeJoinerId);
61*4a64e381SAndroid Build Coastguard Worker otbr::SteeringData::ComputeJoinerId(aJoinerId, aJoinerId);
62*4a64e381SAndroid Build Coastguard Worker ret = 0;
63*4a64e381SAndroid Build Coastguard Worker
64*4a64e381SAndroid Build Coastguard Worker exit:
65*4a64e381SAndroid Build Coastguard Worker if (ret != 0)
66*4a64e381SAndroid Build Coastguard Worker {
67*4a64e381SAndroid Build Coastguard Worker fprintf(stderr, "Invalid EUI64: %s", aEui64);
68*4a64e381SAndroid Build Coastguard Worker }
69*4a64e381SAndroid Build Coastguard Worker
70*4a64e381SAndroid Build Coastguard Worker return ret;
71*4a64e381SAndroid Build Coastguard Worker }
72*4a64e381SAndroid Build Coastguard Worker
main(int argc,char * argv[])73*4a64e381SAndroid Build Coastguard Worker int main(int argc, char *argv[])
74*4a64e381SAndroid Build Coastguard Worker {
75*4a64e381SAndroid Build Coastguard Worker otbr::SteeringData computer;
76*4a64e381SAndroid Build Coastguard Worker int ret = EX_USAGE;
77*4a64e381SAndroid Build Coastguard Worker int length = 16;
78*4a64e381SAndroid Build Coastguard Worker int i = 1;
79*4a64e381SAndroid Build Coastguard Worker
80*4a64e381SAndroid Build Coastguard Worker if (argc < 2)
81*4a64e381SAndroid Build Coastguard Worker {
82*4a64e381SAndroid Build Coastguard Worker ExitNow(help());
83*4a64e381SAndroid Build Coastguard Worker }
84*4a64e381SAndroid Build Coastguard Worker
85*4a64e381SAndroid Build Coastguard Worker if (strlen(argv[i]) != otbr::SteeringData::kSizeJoinerId * 2)
86*4a64e381SAndroid Build Coastguard Worker {
87*4a64e381SAndroid Build Coastguard Worker length = atoi(argv[i]);
88*4a64e381SAndroid Build Coastguard Worker VerifyOrExit(length > 0 && length <= otbr::SteeringData::kMaxSizeOfBloomFilter,
89*4a64e381SAndroid Build Coastguard Worker fprintf(stderr, "Invalid bloom filter length: %d\n", length));
90*4a64e381SAndroid Build Coastguard Worker
91*4a64e381SAndroid Build Coastguard Worker ++i;
92*4a64e381SAndroid Build Coastguard Worker }
93*4a64e381SAndroid Build Coastguard Worker
94*4a64e381SAndroid Build Coastguard Worker computer.Init(static_cast<uint8_t>(length));
95*4a64e381SAndroid Build Coastguard Worker
96*4a64e381SAndroid Build Coastguard Worker for (; i < argc; ++i)
97*4a64e381SAndroid Build Coastguard Worker {
98*4a64e381SAndroid Build Coastguard Worker uint8_t joinerId[otbr::SteeringData::kSizeJoinerId];
99*4a64e381SAndroid Build Coastguard Worker
100*4a64e381SAndroid Build Coastguard Worker VerifyOrExit(ComputeJoinerId(argv[i], joinerId) == 0, fprintf(stderr, "Invalid EUI64 : %s\n", argv[i]));
101*4a64e381SAndroid Build Coastguard Worker computer.ComputeBloomFilter(joinerId);
102*4a64e381SAndroid Build Coastguard Worker }
103*4a64e381SAndroid Build Coastguard Worker
104*4a64e381SAndroid Build Coastguard Worker for (i = 0; i < length; i++)
105*4a64e381SAndroid Build Coastguard Worker {
106*4a64e381SAndroid Build Coastguard Worker printf("%02x", computer.GetBloomFilter()[i]);
107*4a64e381SAndroid Build Coastguard Worker }
108*4a64e381SAndroid Build Coastguard Worker printf("\n");
109*4a64e381SAndroid Build Coastguard Worker
110*4a64e381SAndroid Build Coastguard Worker ret = EX_OK;
111*4a64e381SAndroid Build Coastguard Worker
112*4a64e381SAndroid Build Coastguard Worker exit:
113*4a64e381SAndroid Build Coastguard Worker return ret;
114*4a64e381SAndroid Build Coastguard Worker }
115