blob: c47dcaee215a8cf88c86baeb4ca4b21782f7a564 [file] [log] [blame]
Neels Hofmeyrd48f0572015-10-12 11:57:33 +02001/* OpenBSC kitchen sink */
2
3/* (C) 2015 by sysmocom s.m.f.c GmbH <info@sysmocom.de>
4 * All Rights Reserved
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Affero General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Affero General Public License for more details.
15 *
16 * You should have received a copy of the GNU Affero General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 *
19 */
20
21#include <openbsc/utils.h>
22#include <osmocom/core/utils.h>
23#include <osmocom/core/bit64gen.h>
24
25/* Wishful thinking to generate a constant time compare */
26int constant_time_cmp(const uint8_t *exp, const uint8_t *rel, const int count)
27{
28 int x = 0, i;
29
30 for (i = 0; i < count; ++i)
31 x |= exp[i] ^ rel[i];
32
33 /* if x is zero, all data was identical */
34 return x? 1 : 0;
35}
36
37
38uint64_t decode_big_endian(const uint8_t *data, size_t data_len)
39{
40 uint64_t value = 0;
41
42 while (data_len > 0) {
43 value = (value << 8) + *data;
44 data += 1;
45 data_len -= 1;
46 }
47
48 return value;
49}
50
51uint8_t *encode_big_endian(uint64_t value, size_t data_len)
52{
53 static uint8_t buf[sizeof(uint64_t)];
54 OSMO_ASSERT(data_len <= ARRAY_SIZE(buf));
55 osmo_store64be_ext(value, buf, data_len);
56 return buf;
57}
58