Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AES-CMAC 128 Message [CLDAPPS-1503] #1423

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions c/include/libsbp/cpp/message_traits.h
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,43 @@ struct MessageTraits<sbp_msg_acq_sv_profile_t> {
}
};

template <>
struct MessageTraits<sbp_msg_aes_cmac_signature_t> {
static constexpr sbp_msg_type_t id = SbpMsgAesCmacSignature;
static constexpr const char *name = "MSG_AES_CMAC_SIGNATURE";
static const sbp_msg_aes_cmac_signature_t &get(const sbp_msg_t &msg) {
return msg.aes_cmac_signature;
}
static sbp_msg_aes_cmac_signature_t &get(sbp_msg_t &msg) {
return msg.aes_cmac_signature;
}
static void to_sbp_msg(const sbp_msg_aes_cmac_signature_t &msg,
sbp_msg_t *sbp_msg) {
sbp_msg->aes_cmac_signature = msg;
}
static sbp_msg_t to_sbp_msg(const sbp_msg_aes_cmac_signature_t &msg) {
sbp_msg_t sbp_msg;
sbp_msg.aes_cmac_signature = msg;
return sbp_msg;
}
static s8 send(sbp_state_t *state, u16 sender_id,
const sbp_msg_aes_cmac_signature_t &msg,
sbp_write_fn_t write) {
return sbp_msg_aes_cmac_signature_send(state, sender_id, &msg, write);
}
static s8 encode(uint8_t *buf, uint8_t len, uint8_t *n_written,
const sbp_msg_aes_cmac_signature_t &msg) {
return sbp_msg_aes_cmac_signature_encode(buf, len, n_written, &msg);
}
static s8 decode(const uint8_t *buf, uint8_t len, uint8_t *n_read,
sbp_msg_aes_cmac_signature_t *msg) {
return sbp_msg_aes_cmac_signature_decode(buf, len, n_read, msg);
}
static size_t encoded_len(const sbp_msg_aes_cmac_signature_t &msg) {
return sbp_msg_aes_cmac_signature_encoded_len(&msg);
}
};

template <>
struct MessageTraits<sbp_msg_age_corrections_t> {
static constexpr sbp_msg_type_t id = SbpMsgAgeCorrections;
Expand Down
12 changes: 12 additions & 0 deletions c/include/libsbp/sbp_msg.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ typedef union {
sbp_msg_acq_result_t acq_result;
sbp_msg_acq_sv_profile_dep_t acq_sv_profile_dep;
sbp_msg_acq_sv_profile_t acq_sv_profile;
sbp_msg_aes_cmac_signature_t aes_cmac_signature;
sbp_msg_age_corrections_t age_corrections;
sbp_msg_almanac_glo_dep_t almanac_glo_dep;
sbp_msg_almanac_glo_t almanac_glo;
Expand Down Expand Up @@ -336,6 +337,9 @@ static inline s8 sbp_message_encode(uint8_t *buf, uint8_t len,
case SbpMsgAcqSvProfile:
return sbp_msg_acq_sv_profile_encode(buf, len, n_written,
&msg->acq_sv_profile);
case SbpMsgAesCmacSignature:
return sbp_msg_aes_cmac_signature_encode(buf, len, n_written,
&msg->aes_cmac_signature);
case SbpMsgAgeCorrections:
return sbp_msg_age_corrections_encode(buf, len, n_written,
&msg->age_corrections);
Expand Down Expand Up @@ -1008,6 +1012,9 @@ static inline s8 sbp_message_decode(const uint8_t *buf, uint8_t len,
case SbpMsgAcqSvProfile:
return sbp_msg_acq_sv_profile_decode(buf, len, n_read,
&msg->acq_sv_profile);
case SbpMsgAesCmacSignature:
return sbp_msg_aes_cmac_signature_decode(buf, len, n_read,
&msg->aes_cmac_signature);
case SbpMsgAgeCorrections:
return sbp_msg_age_corrections_decode(buf, len, n_read,
&msg->age_corrections);
Expand Down Expand Up @@ -1659,6 +1666,8 @@ static inline size_t sbp_message_encoded_len(sbp_msg_type_t msg_type,
return sbp_msg_acq_sv_profile_dep_encoded_len(&msg->acq_sv_profile_dep);
case SbpMsgAcqSvProfile:
return sbp_msg_acq_sv_profile_encoded_len(&msg->acq_sv_profile);
case SbpMsgAesCmacSignature:
return sbp_msg_aes_cmac_signature_encoded_len(&msg->aes_cmac_signature);
case SbpMsgAgeCorrections:
return sbp_msg_age_corrections_encoded_len(&msg->age_corrections);
case SbpMsgAlmanacGloDep:
Expand Down Expand Up @@ -2215,6 +2224,9 @@ static inline int sbp_message_cmp(sbp_msg_type_t msg_type, const sbp_msg_t *a,
&b->acq_sv_profile_dep);
case SbpMsgAcqSvProfile:
return sbp_msg_acq_sv_profile_cmp(&a->acq_sv_profile, &b->acq_sv_profile);
case SbpMsgAesCmacSignature:
return sbp_msg_aes_cmac_signature_cmp(&a->aes_cmac_signature,
&b->aes_cmac_signature);
case SbpMsgAgeCorrections:
return sbp_msg_age_corrections_cmp(&a->age_corrections,
&b->age_corrections);
Expand Down
3 changes: 3 additions & 0 deletions c/include/libsbp/sbp_msg_type.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ typedef enum {
SbpMsgAcqResult = 0x002F,
SbpMsgAcqSvProfileDep = 0x001E,
SbpMsgAcqSvProfile = 0x002E,
SbpMsgAesCmacSignature = 0x0C10,
SbpMsgAgeCorrections = 0x0210,
SbpMsgAlmanacGloDep = 0x0071,
SbpMsgAlmanacGlo = 0x0073,
Expand Down Expand Up @@ -308,6 +309,8 @@ static inline const char *sbp_msg_type_to_string(sbp_msg_type_t msg_type) {
return "MSG_ACQ_SV_PROFILE_DEP";
case SbpMsgAcqSvProfile:
return "MSG_ACQ_SV_PROFILE";
case SbpMsgAesCmacSignature:
return "MSG_AES_CMAC_SIGNATURE";
case SbpMsgAgeCorrections:
return "MSG_AGE_CORRECTIONS";
case SbpMsgAlmanacGloDep:
Expand Down
1 change: 1 addition & 0 deletions c/include/libsbp/signing.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#ifndef LIBSBP_SIGNING_MESSAGES_H
#define LIBSBP_SIGNING_MESSAGES_H
#include <libsbp/signing/ECDSASignature.h>
#include <libsbp/signing/MSG_AES_CMAC_SIGNATURE.h>
#include <libsbp/signing/MSG_CERTIFICATE_CHAIN.h>
#include <libsbp/signing/MSG_CERTIFICATE_CHAIN_DEP.h>
#include <libsbp/signing/MSG_ECDSA_CERTIFICATE.h>
Expand Down
226 changes: 226 additions & 0 deletions c/include/libsbp/signing/MSG_AES_CMAC_SIGNATURE.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,226 @@
/*
* Copyright (C) 2015-2021 Swift Navigation Inc.
* Contact: https://support.swiftnav.com
*
* This source is subject to the license found in the file 'LICENSE' which must
* be distributed together with this source. All other rights reserved.
*
* THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
* EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
*/

/*****************************************************************************
* Automatically generated from yaml/swiftnav/sbp/signing.yaml
* with generate.py. Please do not hand edit!
*****************************************************************************/

#ifndef LIBSBP_SIGNING_MSG_AES_CMAC_SIGNATURE_H
#define LIBSBP_SIGNING_MSG_AES_CMAC_SIGNATURE_H

#include <math.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <string.h>

#include <libsbp/common.h>
#include <libsbp/signing_macros.h>
#include <libsbp/string/sbp_string.h>

#ifdef __cplusplus
extern "C" {
#endif

/******************************************************************************
*
* SBP_MSG_AES_CMAC_SIGNATURE
*
*****************************************************************************/
/** AES-CMAC 128 digital signature
*
* Digital signature using AES-CMAC 128 algorithm used for data integrity.
*/
typedef struct {
/**
* Signature message counter. Zero indexed and incremented with each signature
* message. The counter will not increment if this message was in response to
* an on demand request. The counter will roll over after 256 messages. Upon
* connection, the value of the counter may not initially be zero.
*/
u8 stream_counter;

/**
* On demand message counter. Zero indexed and incremented with each signature
* message sent in response to an on demand message. The counter will roll
* over after 256 messages. Upon connection, the value of the counter may not
* initially be zero.
*/
u8 on_demand_counter;

/**
* The last 4 bytes of the certificate's SHA-1 fingerprint
*/
u8 certificate_id[SBP_MSG_AES_CMAC_SIGNATURE_CERTIFICATE_ID_MAX];

/**
* Signature (CMAC tag value)
*/
u8 signature[SBP_MSG_AES_CMAC_SIGNATURE_SIGNATURE_MAX];

/**
* Describes the format of the 'signed messages' field below.
*/
u8 flags;

/**
* CRCs of the messages covered by this signature. For Skylark, which
* delivers SBP messages wrapped in Swift's proprietary RTCM message, these
* are the 24-bit CRCs from the RTCM message framing. For SBP only streams,
* this will be 16-bit CRCs from the SBP framing. See the `flags` field to
* determine the type of CRCs covered.
*/
u8 signed_messages[SBP_MSG_AES_CMAC_SIGNATURE_SIGNED_MESSAGES_MAX];
/**
* Number of elements in signed_messages
*
* When sending a message fill in this field with the number elements set in
* signed_messages before calling an appropriate libsbp send function
*
* When receiving a message query this field for the number of elements in
* signed_messages. The value of any elements beyond the index specified in
* this field is undefined
*/
u8 n_signed_messages;
} sbp_msg_aes_cmac_signature_t;

/**
* Get encoded size of an instance of sbp_msg_aes_cmac_signature_t
*
* @param msg sbp_msg_aes_cmac_signature_t instance
* @return Length of on-wire representation
*/
static inline size_t sbp_msg_aes_cmac_signature_encoded_len(
const sbp_msg_aes_cmac_signature_t *msg) {
return SBP_MSG_AES_CMAC_SIGNATURE_ENCODED_OVERHEAD +
(msg->n_signed_messages * SBP_ENCODED_LEN_U8);
}

/**
* Encode an instance of sbp_msg_aes_cmac_signature_t to wire representation
*
* This function encodes the given instance in to the user provided buffer. The
* buffer provided to this function must be large enough to store the encoded
* message otherwise it will return SBP_ENCODE_ERROR without writing anything to
* the buffer.
*
* Specify the length of the destination buffer in the \p len parameter. If
* non-null the number of bytes written to the buffer will be returned in \p
* n_written.
*
* @param buf Destination buffer
* @param len Length of \p buf
* @param n_written If not null, on success will be set to the number of bytes
* written to \p buf
* @param msg Instance of sbp_msg_aes_cmac_signature_t to encode
* @return SBP_OK on success, or other libsbp error code
*/
SBP_EXPORT s8
sbp_msg_aes_cmac_signature_encode(uint8_t *buf, uint8_t len, uint8_t *n_written,
const sbp_msg_aes_cmac_signature_t *msg);

/**
* Decode an instance of sbp_msg_aes_cmac_signature_t from wire representation
*
* This function decodes the wire representation of a
* sbp_msg_aes_cmac_signature_t message to the given instance. The caller must
* specify the length of the buffer in the \p len parameter. If non-null the
* number of bytes read from the buffer will be returned in \p n_read.
*
* @param buf Wire representation of the sbp_msg_aes_cmac_signature_t instance
* @param len Length of \p buf
* @param n_read If not null, on success will be set to the number of bytes read
* from \p buf
* @param msg Destination
* @return SBP_OK on success, or other libsbp error code
*/
SBP_EXPORT s8 sbp_msg_aes_cmac_signature_decode(
const uint8_t *buf, uint8_t len, uint8_t *n_read,
sbp_msg_aes_cmac_signature_t *msg);
/**
* Send an instance of sbp_msg_aes_cmac_signature_t with the given write
* function
*
* An equivalent of #sbp_message_send which operates specifically on
* sbp_msg_aes_cmac_signature_t
*
* The given message will be encoded to wire representation and passed in to the
* given write function callback. The write callback will be called several
* times for each invocation of this function.
*
* @param s SBP state
* @param sender_id SBP sender id
* @param msg Message to send
* @param write Write function
* @return SBP_OK on success, or other libsbp error code
*/
SBP_EXPORT s8 sbp_msg_aes_cmac_signature_send(
sbp_state_t *s, u16 sender_id, const sbp_msg_aes_cmac_signature_t *msg,
sbp_write_fn_t write);

/**
* Compare two instances of sbp_msg_aes_cmac_signature_t
*
* The two instances will be compared and a value returned consistent with the
* return codes of comparison functions from the C standard library
*
* 0 will be returned if \p a and \p b are considered equal
* A value less than 0 will be returned if \p a is considered to be less than \p
* b A value greater than 0 will be returned if \p b is considered to be greater
* than \p b
*
* @param a sbp_msg_aes_cmac_signature_t instance
* @param b sbp_msg_aes_cmac_signature_t instance
* @return 0, <0, >0
*/
SBP_EXPORT int sbp_msg_aes_cmac_signature_cmp(
const sbp_msg_aes_cmac_signature_t *a,
const sbp_msg_aes_cmac_signature_t *b);

#ifdef __cplusplus
}

static inline bool operator==(const sbp_msg_aes_cmac_signature_t &lhs,
const sbp_msg_aes_cmac_signature_t &rhs) {
return sbp_msg_aes_cmac_signature_cmp(&lhs, &rhs) == 0;
}

static inline bool operator!=(const sbp_msg_aes_cmac_signature_t &lhs,
const sbp_msg_aes_cmac_signature_t &rhs) {
return sbp_msg_aes_cmac_signature_cmp(&lhs, &rhs) != 0;
}

static inline bool operator<(const sbp_msg_aes_cmac_signature_t &lhs,
const sbp_msg_aes_cmac_signature_t &rhs) {
return sbp_msg_aes_cmac_signature_cmp(&lhs, &rhs) < 0;
}

static inline bool operator<=(const sbp_msg_aes_cmac_signature_t &lhs,
const sbp_msg_aes_cmac_signature_t &rhs) {
return sbp_msg_aes_cmac_signature_cmp(&lhs, &rhs) <= 0;
}

static inline bool operator>(const sbp_msg_aes_cmac_signature_t &lhs,
const sbp_msg_aes_cmac_signature_t &rhs) {
return sbp_msg_aes_cmac_signature_cmp(&lhs, &rhs) > 0;
}

static inline bool operator>=(const sbp_msg_aes_cmac_signature_t &lhs,
const sbp_msg_aes_cmac_signature_t &rhs) {
return sbp_msg_aes_cmac_signature_cmp(&lhs, &rhs) >= 0;
}

#endif // ifdef __cplusplus

#endif /* LIBSBP_SIGNING_MSG_AES_CMAC_SIGNATURE_H */
49 changes: 49 additions & 0 deletions c/include/libsbp/signing_macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,55 @@
*/
#define SBP_MSG_CERTIFICATE_CHAIN_DEP_ENCODED_LEN 135u

/**
* The maximum number of items that can be stored in
* sbp_msg_aes_cmac_signature_t::certificate_id before the maximum SBP message
* size is exceeded
*/
#define SBP_MSG_AES_CMAC_SIGNATURE_CERTIFICATE_ID_MAX 4u

/**
* The maximum number of items that can be stored in
* sbp_msg_aes_cmac_signature_t::signature before the maximum SBP message size
* is exceeded
*/
#define SBP_MSG_AES_CMAC_SIGNATURE_SIGNATURE_MAX 16u

#define SBP_AES_CMAC_SIGNATURE_CRC_TYPE_MASK (0x3u)
#define SBP_AES_CMAC_SIGNATURE_CRC_TYPE_SHIFT (0u)
#define SBP_AES_CMAC_SIGNATURE_CRC_TYPE_GET(flags) \
((u8)((u8)((flags) >> SBP_AES_CMAC_SIGNATURE_CRC_TYPE_SHIFT) & \
SBP_AES_CMAC_SIGNATURE_CRC_TYPE_MASK))
#define SBP_AES_CMAC_SIGNATURE_CRC_TYPE_SET(flags, val) \
do { \
(flags) = (u8)((flags & (~(SBP_AES_CMAC_SIGNATURE_CRC_TYPE_MASK \
<< SBP_AES_CMAC_SIGNATURE_CRC_TYPE_SHIFT))) | \
(((val) & (SBP_AES_CMAC_SIGNATURE_CRC_TYPE_MASK)) \
<< (SBP_AES_CMAC_SIGNATURE_CRC_TYPE_SHIFT))); \
} while (0)

#define SBP_AES_CMAC_SIGNATURE_CRC_TYPE_24_BIT_CRCS_FROM_RTCM_FRAMING (0)
#define SBP_AES_CMAC_SIGNATURE_CRC_TYPE_16_BIT_CRCS_FROM_SBP_FRAMING (1)
/**
* The maximum number of items that can be stored in
* sbp_msg_aes_cmac_signature_t::signed_messages before the maximum SBP message
* size is exceeded
*/
#define SBP_MSG_AES_CMAC_SIGNATURE_SIGNED_MESSAGES_MAX 232u

/**
* Encoded length of sbp_msg_aes_cmac_signature_t
*
* This type is not fixed size and an instance of this message may be longer
* than the value indicated by this symbol. Users call
* #sbp_msg_aes_cmac_signature_encoded_len to determine the actual size of an
* instance of this message.
*
* See the documentation for libsbp for more details regarding the message
* structure and its variable length component(s)
*/
#define SBP_MSG_AES_CMAC_SIGNATURE_ENCODED_OVERHEAD 23u

#define SBP_ECDSA_SIGNATURE_CRC_TYPE_MASK (0x3u)
#define SBP_ECDSA_SIGNATURE_CRC_TYPE_SHIFT (0u)
#define SBP_ECDSA_SIGNATURE_CRC_TYPE_GET(flags) \
Expand Down
Loading
Loading