/work/toxcore/crypto_core_pack.c
Line | Count | Source |
1 | | /* SPDX-License-Identifier: GPL-3.0-or-later |
2 | | * Copyright © 2016-2026 The TokTok team. |
3 | | * Copyright © 2013 Tox project. |
4 | | */ |
5 | | |
6 | | #include "crypto_core_pack.h" |
7 | | |
8 | | #include <string.h> |
9 | | |
10 | | #include "bin_pack.h" |
11 | | #include "bin_unpack.h" |
12 | | #include "ccompat.h" |
13 | | #include "crypto_core.h" |
14 | | |
15 | | bool pack_extended_public_key(const Extended_Public_Key *key, Bin_Pack *bp) |
16 | 780 | { |
17 | 780 | uint8_t ext_key[EXT_PUBLIC_KEY_SIZE]; |
18 | 780 | static_assert(sizeof(ext_key) == sizeof(key->enc) + sizeof(key->sig), |
19 | 780 | "extended public key size is not the sum of the encryption and sign public key sizes"); |
20 | 780 | memcpy(ext_key, key->enc, sizeof(key->enc)); |
21 | 780 | memcpy(&ext_key[sizeof(key->enc)], key->sig, sizeof(key->sig)); |
22 | | |
23 | 780 | return bin_pack_bin(bp, ext_key, sizeof(ext_key)); |
24 | 780 | } |
25 | | |
26 | | bool pack_extended_secret_key(const Extended_Secret_Key *key, Bin_Pack *bp) |
27 | 520 | { |
28 | 520 | uint8_t ext_key[EXT_SECRET_KEY_SIZE]; |
29 | 520 | static_assert(sizeof(ext_key) == sizeof(key->enc) + sizeof(key->sig), |
30 | 520 | "extended secret key size is not the sum of the encryption and sign secret key sizes"); |
31 | 520 | memcpy(ext_key, key->enc, sizeof(key->enc)); |
32 | 520 | memcpy(&ext_key[sizeof(key->enc)], key->sig, sizeof(key->sig)); |
33 | | |
34 | 520 | const bool result = bin_pack_bin(bp, ext_key, sizeof(ext_key)); |
35 | 520 | crypto_memzero(ext_key, sizeof(ext_key)); |
36 | 520 | return result; |
37 | 520 | } |
38 | | |
39 | | bool unpack_extended_public_key(Extended_Public_Key *key, Bin_Unpack *bu) |
40 | 894 | { |
41 | 894 | uint8_t ext_key[EXT_PUBLIC_KEY_SIZE]; |
42 | | |
43 | 894 | if (!bin_unpack_bin_fixed(bu, ext_key, sizeof(ext_key))) { |
44 | 84 | return false; |
45 | 84 | } |
46 | | |
47 | 810 | memcpy(key->enc, ext_key, sizeof(key->enc)); |
48 | 810 | memcpy(key->sig, &ext_key[sizeof(key->enc)], sizeof(key->sig)); |
49 | | |
50 | 810 | return true; |
51 | 894 | } |
52 | | |
53 | | bool unpack_extended_secret_key(Extended_Secret_Key *key, Bin_Unpack *bu) |
54 | 407 | { |
55 | 407 | uint8_t ext_key[EXT_SECRET_KEY_SIZE]; |
56 | | |
57 | 407 | if (!bin_unpack_bin_fixed(bu, ext_key, sizeof(ext_key))) { |
58 | 27 | return false; |
59 | 27 | } |
60 | | |
61 | 380 | memcpy(key->enc, ext_key, sizeof(key->enc)); |
62 | 380 | memcpy(key->sig, &ext_key[sizeof(key->enc)], sizeof(key->sig)); |
63 | 380 | crypto_memzero(ext_key, sizeof(ext_key)); |
64 | | |
65 | 380 | return true; |
66 | 407 | } |