|
13 | 13 | #endif |
14 | 14 |
|
15 | 15 | #include "crypto/crypto_param.h" |
| 16 | +#define CSP_ID2_HEADER_SIZE 6 |
16 | 17 |
|
17 | 18 | #define NONCE_SIZE (sizeof(uint64_t) + sizeof(uint8_t)) |
18 | 19 |
|
| 20 | +_Static_assert(CSP_PACKET_PADDING_BYTES > crypto_secretbox_ZEROBYTES + CSP_ID2_HEADER_SIZE, "Not enough padding before csp packet for in-place encryption!"); |
| 21 | + |
19 | 22 | uint8_t _crypto_beforenm[CRYPTO_NUM_KEYS][crypto_secretbox_KEYBYTES]; |
20 | 23 |
|
21 | 24 | void crypto_key_generate(param_t * param, int idx) { |
@@ -107,6 +110,79 @@ int16_t crypto_encrypt(uint8_t * msg_out, uint8_t * msg_in, uint16_t msg_len) { |
107 | 110 | return msg_len + crypto_secretbox_KEYBYTES + NONCE_SIZE; |
108 | 111 | } |
109 | 112 |
|
| 113 | +/** |
| 114 | + * @brief Encrypts a CSP packet payload in-place using NaCl/libsodium. |
| 115 | + * |
| 116 | + * This function performs authenticated encryption on the data contained in the |
| 117 | + * packet structure. It modifies the packet's data buffer directly, adjusting |
| 118 | + * the `frame_begin` pointer and `frame_length` to account for the prepended |
| 119 | + * Message Authentication Code (MAC) and the appended Nonce. |
| 120 | + * |
| 121 | + * **Memory Layout Transformation:** |
| 122 | + * |
| 123 | + * **Before:** |
| 124 | + * @code |
| 125 | + * [ Headroom ] [ Payload (N) ] [ Tailroom ] |
| 126 | + * ^ |
| 127 | + * frame_begin |
| 128 | + * @endcode |
| 129 | + * |
| 130 | + * **After:** |
| 131 | + * @code |
| 132 | + * [ MAC (16) ] [ Encrypted Payload (N) ] [ 0x00 (16) ] [ Nonce (8) ] [ TX ID (1) ] |
| 133 | + * ^ |
| 134 | + * frame_begin |
| 135 | + * @endcode |
| 136 | + * |
| 137 | + * @pre **Compile-time Check:** `CSP_PACKET_PADDING_BYTES` must be greater than |
| 138 | + * `crypto_secretbox_ZEROBYTES + CSP_ID2_HEADER_SIZE`. This is enforced by a |
| 139 | + * `_Static_assert` to ensure safe headless padding. |
| 140 | + * |
| 141 | + * @param[in,out] packet Pointer to the CSP packet structure. The `frame_begin`, |
| 142 | + * `frame_length`, and buffer contents will be modified. |
| 143 | + * |
| 144 | + * @return |
| 145 | + * - \b CSP_ERR_NONE: Encryption successful. |
| 146 | + * - \b CSP_ERR_INVAL: Packet buffer too small for prepending nonce and zerofill. |
| 147 | + */ |
| 148 | +int16_t crypto_encrypt_inplace(csp_packet_t * packet) { |
| 149 | + |
| 150 | + /* Check that there is enough space to postpend nonce and 16 byte zerofill */ |
| 151 | + if(packet->length + NONCE_SIZE + crypto_secretbox_BOXZEROBYTES > CSP_BUFFER_SIZE) { |
| 152 | + return CSP_ERR_INVAL; |
| 153 | + } |
| 154 | + |
| 155 | + /* Update and get transmit nonce */ |
| 156 | + uint64_t tx_nonce = param_get_uint64(&crypto_nonce_tx_count) + 1; |
| 157 | + param_set_uint64(&crypto_nonce_tx_count, tx_nonce); |
| 158 | + |
| 159 | + /* Pack nonce into 24-bytes format, expected by NaCl */ |
| 160 | + unsigned char nonce[crypto_box_NONCEBYTES] = {}; |
| 161 | + memcpy(nonce, &tx_nonce, sizeof(uint64_t)); |
| 162 | + /* Add nonce ID to nonce */ |
| 163 | + nonce[sizeof(uint64_t)] = param_get_uint8(&crypto_nonce_tx_id); |
| 164 | + |
| 165 | + /* Make room for zerofill at the beginning of message */ |
| 166 | + uint8_t * padding_begin = packet->frame_begin - crypto_secretbox_ZEROBYTES; |
| 167 | + memset(padding_begin, 0, crypto_secretbox_ZEROBYTES); |
| 168 | + |
| 169 | + /* Encryption only returns -1 if mlen < 32 */ |
| 170 | + crypto_box_afternm(padding_begin, padding_begin, packet->frame_length + crypto_secretbox_ZEROBYTES, nonce, _crypto_beforenm[param_get_uint8(&tx_encrypt)-1]); |
| 171 | + |
| 172 | + /* Adjust packet pointers and length for the prepended MAC */ |
| 173 | + packet->frame_begin -= crypto_secretbox_MACBYTES; |
| 174 | + packet->frame_length += crypto_secretbox_MACBYTES; |
| 175 | + |
| 176 | + /* Zero out the 16 bytes between the end of the encrypted data and the nonce for backwards compatibility */ |
| 177 | + memset(packet->frame_begin + packet->frame_length, 0, crypto_secretbox_BOXZEROBYTES); |
| 178 | + |
| 179 | + /* Add nonce at the end of the packet plus 16 bytes for backwards compatibility */ |
| 180 | + memcpy(packet->frame_begin + (crypto_secretbox_BOXZEROBYTES + packet->frame_length), nonce, NONCE_SIZE); |
| 181 | + packet->frame_length += NONCE_SIZE + crypto_secretbox_BOXZEROBYTES; |
| 182 | + |
| 183 | + return CSP_ERR_NONE; |
| 184 | +} |
| 185 | + |
110 | 186 | void crypto_init() { |
111 | 187 |
|
112 | 188 | crypto_key_generate(NULL, -1); |
|
0 commit comments