Solaris Software
Toggle main menu visibility
Loading...
Searching...
No Matches
cipher.c
Go to the documentation of this file.
1
/**
2
* @file cipher.c
3
* @brief SPP packet AES-128-GCM cipher context implementation.
4
*
5
* Encrypts a raw SPP packet buffer in-place, producing the output frame:
6
* [ nonce (12 B) | ciphertext (sizeof SPP_Packet_t) | tag (16 B) ]
7
*/
8
9
/* ----------------------------------------------------------------
10
* INCLUDES
11
* ---------------------------------------------------------------- */
12
13
#include "
external/encryption/cipher.h
"
14
#include "
external/encryption/encryption.h
"
15
16
#include <string.h>
17
18
/* ----------------------------------------------------------------
19
* VARIABLES
20
* ---------------------------------------------------------------- */
21
22
static
const
spp_uint8_t
s_key
[
K_SPP_CIPHER_KEY_LEN
] =
K_SPP_CIPHER_KEY
;
23
24
/* ----------------------------------------------------------------
25
* STATIC FUNCTIONS
26
* ---------------------------------------------------------------- */
27
28
/**
29
* @brief Derive the 12-byte GCM nonce from an SPP packet header.
30
*
31
* @param[in] p_packet Packet to derive nonce from.
32
* @param[out] p_nonce 12-byte output buffer.
33
*/
34
static
void
s_buildNonce
(
const
SPP_Packet_t
*p_packet,
35
spp_uint8_t
p_nonce[
K_SPP_CIPHER_NONCE_LEN
])
36
{
37
memset(p_nonce, 0,
K_SPP_CIPHER_NONCE_LEN
);
38
39
/* Bytes 0-3: timestampMs little-endian */
40
p_nonce[0] = (
spp_uint8_t
)( p_packet->secondaryHeader.timestampMs & 0xFFU);
41
p_nonce[1] = (
spp_uint8_t
)((p_packet->secondaryHeader.timestampMs >> 8U) & 0xFFU);
42
p_nonce[2] = (
spp_uint8_t
)((p_packet->secondaryHeader.timestampMs >> 16U) & 0xFFU);
43
p_nonce[3] = (
spp_uint8_t
)((p_packet->secondaryHeader.timestampMs >> 24U) & 0xFFU);
44
45
/* Bytes 4-5: seq little-endian */
46
p_nonce[4] = (
spp_uint8_t
)( p_packet->primaryHeader.seq & 0xFFU);
47
p_nonce[5] = (
spp_uint8_t
)((p_packet->primaryHeader.seq >> 8U) & 0xFFU);
48
49
/* Bytes 6-11: reserved, already zeroed */
50
}
51
52
/* ----------------------------------------------------------------
53
* PUBLIC FUNCTIONS
54
* ---------------------------------------------------------------- */
55
56
SPP_RetVal_t
SPP_EXTERNAL_ENCRYPTION_CYPHER_encryptPacket
(
spp_uint8_t
*p_buffer,
spp_uint16_t
*p_outSize)
57
{
58
if
(p_buffer == NULL || p_outSize == NULL)
59
{
60
return
K_SPP_ERROR_NULL_POINTER
;
61
}
62
63
spp_uint8_t
nonce[
K_SPP_CIPHER_NONCE_LEN
];
64
spp_uint8_t
packet_copy[
sizeof
(
SPP_Packet_t
)];
65
66
/* Save a copy before we overwrite the buffer */
67
memcpy(packet_copy, p_buffer,
sizeof
(
SPP_Packet_t
));
68
69
/* Derive nonce from the original packet header fields */
70
s_buildNonce
((
const
SPP_Packet_t
*)packet_copy, nonce);
71
72
/* Encrypt into buffer at offset NONCE_LEN, leaving room for the nonce prefix.
73
* Output layout: p_buffer[ 0..11 ] = nonce (written below)
74
* p_buffer[12..73 ] = ciphertext (sizeof SPP_Packet_t = 62 B)
75
* p_buffer[74..89 ] = GCM tag (16 B) */
76
SPP_EXTERNAL_ENCRYPTION_gcmEncrypt
(
77
s_key
,
78
nonce,
79
NULL, 0,
80
packet_copy,
81
sizeof
(
SPP_Packet_t
),
82
p_buffer +
K_SPP_CIPHER_NONCE_LEN
83
);
84
85
/* Prepend nonce at the start of the frame */
86
memcpy(p_buffer, nonce,
K_SPP_CIPHER_NONCE_LEN
);
87
88
*p_outSize = (
spp_uint16_t
)
K_SPP_CIPHER_FRAME_LEN
;
89
return
K_SPP_OK
;
90
}
s_key
static const spp_uint8_t s_key[K_SPP_CIPHER_KEY_LEN]
Definition
cipher.c:22
s_buildNonce
static void s_buildNonce(const SPP_Packet_t *p_packet, spp_uint8_t p_nonce[K_SPP_CIPHER_NONCE_LEN])
Derive the 12-byte GCM nonce from an SPP packet header.
Definition
cipher.c:34
SPP_EXTERNAL_ENCRYPTION_CYPHER_encryptPacket
SPP_RetVal_t SPP_EXTERNAL_ENCRYPTION_CYPHER_encryptPacket(spp_uint8_t *p_buffer, spp_uint16_t *p_outSize)
Encrypt an SPP packet buffer in-place with AES-128-GCM.
Definition
cipher.c:56
cipher.h
SPP packet AES-128-GCM cipher context.
K_SPP_CIPHER_NONCE_LEN
#define K_SPP_CIPHER_NONCE_LEN
Definition
cipher.h:33
K_SPP_CIPHER_KEY_LEN
#define K_SPP_CIPHER_KEY_LEN
Definition
cipher.h:32
K_SPP_CIPHER_KEY
#define K_SPP_CIPHER_KEY
AES-128 symmetric key shared between transmitter and receiver.
Definition
cipher.h:53
K_SPP_CIPHER_FRAME_LEN
#define K_SPP_CIPHER_FRAME_LEN
Total output frame size after encryption.
Definition
cipher.h:45
SPP_EXTERNAL_ENCRYPTION_gcmEncrypt
void SPP_EXTERNAL_ENCRYPTION_gcmEncrypt(const uint8_t *key, const uint8_t *nonce, const void *aData, const size_t aDataLen, const void *pntxt, const size_t ptextLen, void *crtxt)
encrypt the input plaintext using GCM-AES block-cipher method
Definition
encryption.c:1165
encryption.h
SPP_Packet_t
SPP_Packet_t
Definition
packet.h:79
SPP_RetVal_t
SPP_RetVal_t
Unified return type for all SPP API functions.
Definition
returnTypes.h:24
K_SPP_OK
@ K_SPP_OK
Definition
returnTypes.h:25
K_SPP_ERROR_NULL_POINTER
@ K_SPP_ERROR_NULL_POINTER
Definition
returnTypes.h:30
spp_uint16_t
uint16_t spp_uint16_t
Definition
types.h:29
spp_uint8_t
uint8_t spp_uint8_t
Definition
types.h:27
solaris-v2
spp
external
encryption
cipher.c
Generated by
1.18.0