1 /*
2 * Copyright (c) 2021, Nordic Semiconductor ASA
3 * All rights reserved.
4 *
5 * SPDX-License-Identifier: BSD-3-Clause
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright notice, this
11 * list of conditions and the following disclaimer.
12 *
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
18 * contributors may be used to endorse or promote products derived from this
19 * software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY, AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
25 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE.
32 *
33 */
34
35 #include "nrf_802154_assert.h"
36 #include <stddef.h>
37 #include <string.h>
38
39 #include "nrf_802154_const.h"
40 #include "nrf_802154_tx_work_buffer.h"
41
42 static uint8_t m_work_buffer[MAX_PACKET_SIZE + PHR_SIZE]; ///< Work buffer.
43 static uint8_t * mp_original_frame; ///< Pointer to the original frame the work buffer is currently bound to.
44 static uint8_t m_plain_text_offset; ///< Offset of encryption plain text.
45 static bool m_is_secured; ///< Flag that indicates if work buffer has been successfully secured.
46 static bool m_is_dynamic_data_updated; ///< Flag that indicates if work buffer has had dynamic data successfully updated.
47
nrf_802154_tx_work_buffer_reset(const nrf_802154_transmitted_frame_props_t * p_frame_props)48 void nrf_802154_tx_work_buffer_reset(const nrf_802154_transmitted_frame_props_t * p_frame_props)
49 {
50 mp_original_frame = NULL;
51 m_plain_text_offset = 0;
52
53 if (p_frame_props == NULL)
54 {
55 m_is_secured = false;
56 m_is_dynamic_data_updated = false;
57 }
58 else
59 {
60 m_is_secured = p_frame_props->is_secured;
61 m_is_dynamic_data_updated = p_frame_props->dynamic_data_is_set;
62 }
63 }
64
nrf_802154_tx_work_buffer_enable_for(uint8_t * p_original_frame)65 uint8_t * nrf_802154_tx_work_buffer_enable_for(uint8_t * p_original_frame)
66 {
67 mp_original_frame = p_original_frame;
68 return m_work_buffer;
69 }
70
nrf_802154_tx_work_buffer_get(const uint8_t * p_original_frame)71 const uint8_t * nrf_802154_tx_work_buffer_get(const uint8_t * p_original_frame)
72 {
73 return mp_original_frame ? m_work_buffer : p_original_frame;
74 }
75
nrf_802154_tx_work_buffer_original_frame_update(uint8_t * p_original_frame,nrf_802154_transmitted_frame_props_t * p_frame_props)76 void nrf_802154_tx_work_buffer_original_frame_update(
77 uint8_t * p_original_frame,
78 nrf_802154_transmitted_frame_props_t * p_frame_props)
79 {
80 NRF_802154_ASSERT(p_frame_props != NULL);
81
82 p_frame_props->is_secured = m_is_secured;
83 p_frame_props->dynamic_data_is_set = m_is_dynamic_data_updated;
84
85 if (mp_original_frame == NULL)
86 {
87 return;
88 }
89
90 uint8_t work_buffer_len = m_work_buffer[PHR_OFFSET] + PHR_SIZE;
91
92 if (m_is_dynamic_data_updated && m_is_secured)
93 {
94 memcpy(p_original_frame, m_work_buffer, work_buffer_len);
95 }
96 else if (m_is_dynamic_data_updated)
97 {
98 memcpy(p_original_frame, m_work_buffer, m_plain_text_offset);
99 }
100 else if (m_is_secured)
101 {
102 memcpy(p_original_frame, m_work_buffer, work_buffer_len - m_plain_text_offset);
103 }
104 else
105 {
106 // Intentionally empty.
107 }
108 }
109
nrf_802154_tx_work_buffer_is_secured_set(void)110 void nrf_802154_tx_work_buffer_is_secured_set(void)
111 {
112 m_is_secured = true;
113 }
114
nrf_802154_tx_work_buffer_is_dynamic_data_updated_set(void)115 void nrf_802154_tx_work_buffer_is_dynamic_data_updated_set(void)
116 {
117 m_is_dynamic_data_updated = true;
118 }
119
nrf_802154_tx_work_buffer_plain_text_offset_set(uint8_t offset)120 void nrf_802154_tx_work_buffer_plain_text_offset_set(uint8_t offset)
121 {
122 m_plain_text_offset = offset;
123 }
124