Skip to content

Commit f8a76bf

Browse files
authored
Meson option nacl_impl allows selecting libsodium as NaCl implementation.
* initial sodium impl, using meson extern prj mod * moveing debug prints verbosity, idx counter dont use signed * add mutex around TX * remove old prints * unlock before packet free to avoid context switch * enforce lock implemented * fix missing strlcpy for test
1 parent 2b84b25 commit f8a76bf

8 files changed

Lines changed: 107 additions & 27 deletions

File tree

.github/workflows/build.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ jobs:
1212
steps:
1313
- name: Setup build system packages on Linux
1414
run: |
15-
sudo apt-get install ninja-build meson tree gcc-arm-none-eabi python3-pip
15+
sudo apt-get install ninja-build meson tree gcc-arm-none-eabi python3-pip libbsd-dev
1616
- name: Checkout
1717
uses: actions/checkout@v4
1818
with:

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,6 @@
44
[submodule "subprojects/csp"]
55
path = subprojects/csp
66
url = https://github.com/spaceinventor/libcsp
7+
[submodule "src/crypto/sodium"]
8+
path = src/crypto/sodium
9+
url = https://github.com/jedisct1/libsodium.git

include/cblk/csp_if_cblk.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ typedef struct {
4646
/* Function provided by implementation to send a CCSDS frame */
4747
int (*cblk_tx_send)(csp_iface_t* iface, cblk_frame_t* frame);
4848

49+
void (*cblk_tx_lock)(csp_iface_t* iface);
50+
void (*cblk_tx_unlock)(csp_iface_t* iface);
51+
4952
/* Variables for internal use */
5053
uint8_t rx_packet_idx;
5154
uint8_t rx_frame_idx;

meson.build

Lines changed: 68 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
project('cblk', ['c', 'cpp'])
22

3+
cblk_deps = []
4+
cblk_args = []
5+
36
if not meson.is_subproject()
47
csp_dep = dependency('csp', fallback : ['csp', 'csp_dep'], required: true).partial_dependency(links: true, includes: true)
58
param_dep = dependency('param', fallback: ['param', 'param_dep'], required: true).partial_dependency(links: true, includes: true)
@@ -8,21 +11,83 @@ else
811
param_dep = dependency('param', fallback: ['param', 'param_dep'], required: true).partial_dependency(links: false, includes: true)
912
endif
1013

14+
cblk_deps += [csp_dep, param_dep]
15+
1116
cblk_src = files([
1217
'src/csp_if_cblk.c',
1318
'src/crypto/crypto.c',
1419
'src/crypto/crypto_param.c',
15-
'src/crypto/tweetnacl.c',
1620
])
1721

22+
nacl_impl = get_option('nacl_impl')
23+
24+
if nacl_impl == 'tweetnacl'
25+
cblk_src += 'src/crypto/tweetnacl.c'
26+
cblk_args += '-DUSE_TWEETNACL'
27+
elif nacl_impl == 'sodium'
28+
29+
# we only support cross compiling to arm-none-eabi targets
30+
if meson.is_cross_build() == true
31+
32+
mod = import('unstable-external_project')
33+
34+
configure_opts = [
35+
'--enable-minimal',
36+
'--disable-ssp',
37+
'--disable-pie',
38+
'--enable-static',
39+
'--disable-shared',
40+
'--without-pthreads',
41+
'--prefix=@PREFIX@',
42+
'--libdir=@PREFIX@/@LIBDIR@',
43+
'--includedir=@PREFIX@/@INCLUDEDIR@',
44+
]
45+
sodium_env = environment()
46+
target_c_args = get_option('c_args')
47+
48+
c_flags_str = '-O3 '
49+
foreach arg : target_c_args
50+
c_flags_str += arg + ' '
51+
endforeach
52+
53+
sodium_env.set('CFLAGS', c_flags_str)
54+
sodium_env.set('LDFLAGS', '-specs=nosys.specs')
55+
56+
cross_configure_opt = [
57+
'--host=arm-none-eabi',
58+
]
59+
60+
libsodium_proj = mod.add_project('src/crypto/sodium/configure',
61+
configure_options : configure_opts,
62+
verbose : false,
63+
cross_configure_options : cross_configure_opt,
64+
env : sodium_env,
65+
)
66+
67+
sodium_dep = libsodium_proj.dependency('sodium')
68+
69+
else
70+
sodium_dep = dependency('sodium', required: true)
71+
endif
72+
73+
74+
cblk_deps += sodium_dep
75+
cblk_args += '-DUSE_SODIUM'
76+
endif
77+
1878
cblk_inc = include_directories('src')
1979
api = include_directories('include')
2080

2181
cblk_lib = static_library('cblk',
2282
sources: [cblk_src],
2383
include_directories : [cblk_inc, api],
24-
dependencies : [csp_dep, param_dep],
84+
dependencies : cblk_deps,
85+
c_args : cblk_args,
2586
install : false
2687
)
2788

28-
cblk_dep = declare_dependency(include_directories : api, link_with : cblk_lib, dependencies: [csp_dep, param_dep])
89+
cblk_dep = declare_dependency(
90+
include_directories : api,
91+
link_with : cblk_lib,
92+
dependencies: cblk_deps,
93+
)

meson_options.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
option('nacl_impl',
2+
type: 'combo',
3+
choices: ['tweetnacl', 'sodium'],
4+
value: 'tweetnacl',
5+
description: 'Which NaCl implementation to use'
6+
)

src/crypto/crypto.c

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,14 @@
44
#include <stdlib.h>
55
#include <stdio.h>
66

7+
#ifdef USE_TWEETNACL
78
#include "tweetnacl.h"
9+
#endif
10+
11+
#ifdef USE_SODIUM
12+
#include <sodium.h>
13+
#endif
14+
815
#include "crypto/crypto_param.h"
916

1017
#define NONCE_SIZE (sizeof(uint64_t) + sizeof(uint8_t))
@@ -18,17 +25,6 @@ void crypto_key_generate(param_t * param, int idx) {
1825
param_get_data(&crypto_key3, _crypto_beforenm[2], sizeof(_crypto_beforenm[2]));
1926
}
2027

21-
/* Required tweetnacl.c */
22-
void randombytes(unsigned char * a, unsigned long long c) {
23-
// Note: Pseudo random since we are not initializing random!
24-
unsigned int seed = csp_get_ms();
25-
while(c > 0) {
26-
*a = rand_r(&seed) & 0xFF;
27-
a++;
28-
c--;
29-
}
30-
}
31-
3228
/*
3329
There is a 32-octet padding requirement on the plaintext buffer that you pass to crypto_box.
3430
Internally, the NaCl implementation uses this space to avoid having to allocate memory or

src/crypto/sodium

Submodule sodium added at e98f210

src/csp_if_cblk.c

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ int csp_if_cblk_tx(csp_iface_t * iface, uint16_t via, csp_packet_t *packet, int
6262
uint16_t frame_length = packet->frame_length;
6363
uint8_t* frame_begin = packet->frame_begin;
6464

65+
ifdata->cblk_tx_lock(iface);
66+
6567
if (param_get_uint8(&tx_encrypt)) {
6668
frame_length = crypto_encrypt(ifdata->packet_enc, packet->frame_begin, packet->frame_length);
6769
frame_begin = &ifdata->packet_enc[CRYPTO_PREAMP];
@@ -76,6 +78,7 @@ int csp_if_cblk_tx(csp_iface_t * iface, uint16_t via, csp_packet_t *packet, int
7678

7779
cblk_frame_t * tx_ccsds_buf = ifdata->cblk_tx_buffer_get(iface);
7880
if (tx_ccsds_buf == NULL) {
81+
ifdata->cblk_tx_unlock(iface);
7982
csp_buffer_free(packet);
8083
return CSP_ERR_NOBUFS;
8184
}
@@ -96,11 +99,13 @@ int csp_if_cblk_tx(csp_iface_t * iface, uint16_t via, csp_packet_t *packet, int
9699
bytes_remain -= segment_len;
97100

98101
if (ifdata->cblk_tx_send(iface, tx_ccsds_buf) < 0) {
102+
ifdata->cblk_tx_unlock(iface);
99103
csp_buffer_free(packet);
100104
return CSP_ERR_NOBUFS;
101105
}
102106
}
103107

108+
ifdata->cblk_tx_unlock(iface);
104109
csp_buffer_free(packet);
105110

106111
return CSP_ERR_NONE;
@@ -112,7 +117,7 @@ int csp_if_cblk_rx(csp_iface_t * iface, cblk_frame_t *frame, uint32_t len, uint8
112117

113118
uint16_t frame_length = be16toh(frame->hdr.data_length);
114119

115-
if (_cblk_rx_debug >= 1) {
120+
if (_cblk_rx_debug >= 3) {
116121
printf("RX %p chain %u CCSDS header: %u %u %u\n", frame, group, frame->hdr.csp_packet_idx, frame->hdr.ccsds_frame_idx, frame_length);
117122
}
118123

@@ -126,7 +131,7 @@ int csp_if_cblk_rx(csp_iface_t * iface, cblk_frame_t *frame, uint32_t len, uint8
126131
} else if (ifdata->rx_packet_idx == frame->hdr.csp_packet_idx && ifdata->rx_frame_idx == frame->hdr.ccsds_frame_idx) {
127132

128133
/* We already handled this frame */
129-
if (_cblk_rx_debug >= 1) printf("Discarding dublicated frame\n");
134+
if (_cblk_rx_debug >= 2) printf("Discarding dublicated frame\n");
130135
return CSP_ERR_NONE;
131136

132137
} else if (frame->hdr.ccsds_frame_idx == 0) {
@@ -139,7 +144,7 @@ int csp_if_cblk_rx(csp_iface_t * iface, cblk_frame_t *frame, uint32_t len, uint8
139144

140145
/* We are missing part of the received CSP frame */
141146
if (_cblk_rx_debug >= 1) {
142-
printf("Part of CSP frame is missing: Received part %d of %d, expected part %d of %d\n",
147+
printf("Part of CSP frame is missing: Received part %"PRIu8" of %"PRIu8", expected part %"PRIu8" of %"PRIu8"\n",
143148
frame->hdr.ccsds_frame_idx, frame->hdr.csp_packet_idx, ifdata->rx_frame_idx+1, ifdata->rx_packet_idx);
144149
}
145150
iface->frame++;
@@ -166,7 +171,7 @@ int csp_if_cblk_rx(csp_iface_t * iface, cblk_frame_t *frame, uint32_t len, uint8
166171

167172
if (frame->hdr.nacl_crypto_key > 0) {
168173

169-
if (_cblk_rx_debug >= 3) {
174+
if (_cblk_rx_debug >= 4) {
170175
csp_hex_dump("-rx_enc", &ifdata->packet_dec[CRYPTO_PREAMP], frame_length);
171176
}
172177

@@ -192,7 +197,7 @@ int csp_if_cblk_rx(csp_iface_t * iface, cblk_frame_t *frame, uint32_t len, uint8
192197
rx_packet->frame_length = frame_length;
193198
}
194199

195-
if (_cblk_rx_debug >= 3) {
200+
if (_cblk_rx_debug >= 5) {
196201
csp_hex_dump("-rx_dec", rx_packet->frame_begin, rx_packet->frame_length);
197202
}
198203

@@ -203,24 +208,25 @@ int csp_if_cblk_rx(csp_iface_t * iface, cblk_frame_t *frame, uint32_t len, uint8
203208
return CSP_ERR_INVAL;
204209
}
205210

206-
if (_cblk_rx_debug >= 2) {
211+
if (_cblk_rx_debug >= 4) {
207212
csp_hex_dump("packet", rx_packet->data, rx_packet->length);
208213
}
209214

210215
csp_qfifo_write(rx_packet, iface, NULL);
211-
/* We have succesfully transmitted a full packet,
212-
reset our internal index counters for the next ones */
213-
ifdata->rx_packet_idx = -1;
214-
ifdata->rx_frame_idx = -1;
215216
return CSP_ERR_NONE;
216217
}
217218

218219
void csp_if_cblk_init(csp_iface_t * iface) {
219220

220221
csp_cblk_interface_data_t * ifdata = iface->interface_data;
221222

222-
ifdata->rx_frame_idx = -1;
223-
ifdata->rx_packet_idx = -1;
223+
ifdata->rx_frame_idx = UINT8_MAX;
224+
ifdata->rx_packet_idx = UINT8_MAX;
225+
226+
if(ifdata->cblk_tx_lock == NULL || ifdata->cblk_tx_unlock == NULL) {
227+
printf("csp_if_cblk_init: lock function pointers must be set!\n");
228+
return;
229+
}
224230

225231
iface->nexthop = csp_if_cblk_tx;
226232
}

0 commit comments

Comments
 (0)