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

variable length ecdsa signature [GV2-193] #1306

Merged
merged 6 commits into from
Mar 17, 2023
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 @@ -1475,6 +1475,43 @@ struct MessageTraits<sbp_msg_ecdsa_certificate_t> {
}
};

template <>
struct MessageTraits<sbp_msg_ecdsa_signature_dep_t> {
static constexpr sbp_msg_type_t id = SbpMsgEcdsaSignatureDep;
static constexpr const char *name = "MSG_ECDSA_SIGNATURE_DEP";
static const sbp_msg_ecdsa_signature_dep_t &get(const sbp_msg_t &msg) {
return msg.ecdsa_signature_dep;
}
static sbp_msg_ecdsa_signature_dep_t &get(sbp_msg_t &msg) {
return msg.ecdsa_signature_dep;
}
static void to_sbp_msg(const sbp_msg_ecdsa_signature_dep_t &msg,
sbp_msg_t *sbp_msg) {
sbp_msg->ecdsa_signature_dep = msg;
}
static sbp_msg_t to_sbp_msg(const sbp_msg_ecdsa_signature_dep_t &msg) {
sbp_msg_t sbp_msg;
sbp_msg.ecdsa_signature_dep = msg;
return sbp_msg;
}
static s8 send(sbp_state_t *state, u16 sender_id,
const sbp_msg_ecdsa_signature_dep_t &msg,
sbp_write_fn_t write) {
return sbp_msg_ecdsa_signature_dep_send(state, sender_id, &msg, write);
}
static s8 encode(uint8_t *buf, uint8_t len, uint8_t *n_written,
const sbp_msg_ecdsa_signature_dep_t &msg) {
return sbp_msg_ecdsa_signature_dep_encode(buf, len, n_written, &msg);
}
static s8 decode(const uint8_t *buf, uint8_t len, uint8_t *n_read,
sbp_msg_ecdsa_signature_dep_t *msg) {
return sbp_msg_ecdsa_signature_dep_decode(buf, len, n_read, msg);
}
static size_t encoded_len(const sbp_msg_ecdsa_signature_dep_t &msg) {
return sbp_msg_ecdsa_signature_dep_encoded_len(&msg);
}
};

template <>
struct MessageTraits<sbp_msg_ecdsa_signature_t> {
static constexpr sbp_msg_type_t id = SbpMsgEcdsaSignature;
Expand Down
8 changes: 7 additions & 1 deletion c/include/libsbp/legacy/cpp/message_traits.h
Original file line number Diff line number Diff line change
Expand Up @@ -1192,11 +1192,17 @@ struct MessageTraits<msg_certificate_chain_t> {


template<>
struct MessageTraits<msg_ecdsa_signature_t> {
struct MessageTraits<msg_ecdsa_signature_dep_t> {
static constexpr u16 id = 3078;
};


template<>
struct MessageTraits<msg_ecdsa_signature_t> {
static constexpr u16 id = 3079;
};


template<>
struct MessageTraits<msg_fileio_config_req_t> {
static constexpr u16 id = 4097;
Expand Down
41 changes: 40 additions & 1 deletion c/include/libsbp/legacy/signing.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,45 @@ typedef struct SBP_ATTR_PACKED {
`http(s)://certs.swiftnav.com/chain`. */
} msg_certificate_chain_t;

/** An ECDSA signature
*
* An ECDSA-256 signature using SHA-256 as the message digest algorithm.
*/

typedef struct SBP_ATTR_PACKED {
u8 flags; /**< Describes the format of the `signed\_messages`
field below. */
u8 stream_counter; /**< 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 on_demand_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 certificate_id[4]; /**< The last 4 bytes of the certificate's SHA-1
fingerprint */
u8 n_signature_bytes; /**< Number of bytes to use of the signature field.
The DER encoded signature has a maximum size
of 72 bytes but can vary between 70 and 72
bytes in length. */
u8 signature[72]; /**< DER encoded ECDSA signature for the messages
using SHA-256 as the digest algorithm. */
u8 signed_messages[0]; /**< 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. */
} msg_ecdsa_signature_t;

/** An ECDSA signature
*
* An ECDSA-256 signature using SHA-256 as the message digest algorithm.
Expand Down Expand Up @@ -114,7 +153,7 @@ typedef struct SBP_ATTR_PACKED {
this will be 16-bit CRCs from the SBP framing.
See the `flags` field to determine the type of
CRCs covered. */
} msg_ecdsa_signature_t;
} msg_ecdsa_signature_dep_t;

typedef struct SBP_ATTR_PACKED {
u8 n_msg; /**< Total number messages that make up the
Expand Down
1 change: 1 addition & 0 deletions c/include/libsbp/sbp_msg_type.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ typedef enum {
SbpMsgDopsDepA = SBP_MSG_DOPS_DEP_A,
SbpMsgDops = SBP_MSG_DOPS,
SbpMsgEcdsaCertificate = SBP_MSG_ECDSA_CERTIFICATE,
SbpMsgEcdsaSignatureDep = SBP_MSG_ECDSA_SIGNATURE_DEP,
SbpMsgEcdsaSignature = SBP_MSG_ECDSA_SIGNATURE,
SbpMsgEd25519CertificateDep = SBP_MSG_ED25519_CERTIFICATE_DEP,
SbpMsgEd25519SignatureDepA = SBP_MSG_ED25519_SIGNATURE_DEP_A,
Expand Down
63 changes: 59 additions & 4 deletions c/include/libsbp/signing_macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@
*/
#define SBP_MSG_CERTIFICATE_CHAIN_ENCODED_LEN 135u

#define SBP_MSG_ECDSA_SIGNATURE 0x0C06
#define SBP_MSG_ECDSA_SIGNATURE 0x0C07
#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 Expand Up @@ -142,15 +142,15 @@
* msg_ecdsa_signature_t::signature (legacy API) before the maximum SBP message
* size is exceeded
*/
#define SBP_MSG_ECDSA_SIGNATURE_SIGNATURE_MAX 64u
#define SBP_MSG_ECDSA_SIGNATURE_SIGNATURE_MAX 72u

/**
* The maximum number of items that can be stored in
* sbp_msg_ecdsa_signature_t::signed_messages (V4 API) or
* msg_ecdsa_signature_t::signed_messages (legacy API) before the maximum SBP
* message size is exceeded
*/
#define SBP_MSG_ECDSA_SIGNATURE_SIGNED_MESSAGES_MAX 184u
#define SBP_MSG_ECDSA_SIGNATURE_SIGNED_MESSAGES_MAX 175u

/**
* Encoded length of sbp_msg_ecdsa_signature_t (V4 API) and
Expand All @@ -165,7 +165,62 @@
* See the documentation for libsbp for more details regarding the message
* structure and its variable length component(s)
*/
#define SBP_MSG_ECDSA_SIGNATURE_ENCODED_OVERHEAD 71u
#define SBP_MSG_ECDSA_SIGNATURE_ENCODED_OVERHEAD 80u

#define SBP_MSG_ECDSA_SIGNATURE_DEP 0x0C06
#define SBP_ECDSA_SIGNATURE_DEP_CRC_TYPE_MASK (0x3u)
#define SBP_ECDSA_SIGNATURE_DEP_CRC_TYPE_SHIFT (0u)
#define SBP_ECDSA_SIGNATURE_DEP_CRC_TYPE_GET(flags) \
((u8)((u8)((flags) >> SBP_ECDSA_SIGNATURE_DEP_CRC_TYPE_SHIFT) & \
SBP_ECDSA_SIGNATURE_DEP_CRC_TYPE_MASK))
#define SBP_ECDSA_SIGNATURE_DEP_CRC_TYPE_SET(flags, val) \
do { \
(flags) = (u8)((flags & (~(SBP_ECDSA_SIGNATURE_DEP_CRC_TYPE_MASK \
<< SBP_ECDSA_SIGNATURE_DEP_CRC_TYPE_SHIFT))) | \
(((val) & (SBP_ECDSA_SIGNATURE_DEP_CRC_TYPE_MASK)) \
<< (SBP_ECDSA_SIGNATURE_DEP_CRC_TYPE_SHIFT))); \
} while (0)

#define SBP_ECDSA_SIGNATURE_DEP_CRC_TYPE_24_BIT_CRCS_FROM_RTCM_FRAMING (0)
#define SBP_ECDSA_SIGNATURE_DEP_CRC_TYPE_16_BIT_CRCS_FROM_SBP_FRAMING (1)
/**
* The maximum number of items that can be stored in
* sbp_msg_ecdsa_signature_dep_t::certificate_id (V4 API) or
* msg_ecdsa_signature_dep_t::certificate_id (legacy API) before the maximum SBP
* message size is exceeded
*/
#define SBP_MSG_ECDSA_SIGNATURE_DEP_CERTIFICATE_ID_MAX 4u

/**
* The maximum number of items that can be stored in
* sbp_msg_ecdsa_signature_dep_t::signature (V4 API) or
* msg_ecdsa_signature_dep_t::signature (legacy API) before the maximum SBP
* message size is exceeded
*/
#define SBP_MSG_ECDSA_SIGNATURE_DEP_SIGNATURE_MAX 64u

/**
* The maximum number of items that can be stored in
* sbp_msg_ecdsa_signature_dep_t::signed_messages (V4 API) or
* msg_ecdsa_signature_dep_t::signed_messages (legacy API) before the maximum
* SBP message size is exceeded
*/
#define SBP_MSG_ECDSA_SIGNATURE_DEP_SIGNED_MESSAGES_MAX 184u

/**
* Encoded length of sbp_msg_ecdsa_signature_dep_t (V4 API) and
* msg_ecdsa_signature_dep_t (legacy API)
*
* This type is not fixed size and an instance of this message may be longer
* than the value indicated by this symbol. Users of the V4 API should call
* #sbp_msg_ecdsa_signature_dep_encoded_len to determine the actual size of an
* instance of this message. Users of the legacy API are required to track the
* encoded message length when interacting with the legacy type.
*
* See the documentation for libsbp for more details regarding the message
* structure and its variable length component(s)
*/
#define SBP_MSG_ECDSA_SIGNATURE_DEP_ENCODED_OVERHEAD 71u

#define SBP_MSG_ED25519_CERTIFICATE_DEP 0x0C02
/**
Expand Down
12 changes: 12 additions & 0 deletions c/include/libsbp/v4/sbp_msg.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ typedef union {
sbp_msg_dops_dep_a_t dops_dep_a;
sbp_msg_dops_t dops;
sbp_msg_ecdsa_certificate_t ecdsa_certificate;
sbp_msg_ecdsa_signature_dep_t ecdsa_signature_dep;
sbp_msg_ecdsa_signature_t ecdsa_signature;
sbp_msg_ed25519_certificate_dep_t ed25519_certificate_dep;
sbp_msg_ed25519_signature_dep_a_t ed25519_signature_dep_a;
Expand Down Expand Up @@ -419,6 +420,9 @@ static inline s8 sbp_message_encode(uint8_t *buf, uint8_t len,
case SbpMsgEcdsaCertificate:
return sbp_msg_ecdsa_certificate_encode(buf, len, n_written,
&msg->ecdsa_certificate);
case SbpMsgEcdsaSignatureDep:
return sbp_msg_ecdsa_signature_dep_encode(buf, len, n_written,
&msg->ecdsa_signature_dep);
case SbpMsgEcdsaSignature:
return sbp_msg_ecdsa_signature_encode(buf, len, n_written,
&msg->ecdsa_signature);
Expand Down Expand Up @@ -1065,6 +1069,9 @@ static inline s8 sbp_message_decode(const uint8_t *buf, uint8_t len,
case SbpMsgEcdsaCertificate:
return sbp_msg_ecdsa_certificate_decode(buf, len, n_read,
&msg->ecdsa_certificate);
case SbpMsgEcdsaSignatureDep:
return sbp_msg_ecdsa_signature_dep_decode(buf, len, n_read,
&msg->ecdsa_signature_dep);
case SbpMsgEcdsaSignature:
return sbp_msg_ecdsa_signature_decode(buf, len, n_read,
&msg->ecdsa_signature);
Expand Down Expand Up @@ -1681,6 +1688,8 @@ static inline size_t sbp_message_encoded_len(sbp_msg_type_t msg_type,
return sbp_msg_dops_encoded_len(&msg->dops);
case SbpMsgEcdsaCertificate:
return sbp_msg_ecdsa_certificate_encoded_len(&msg->ecdsa_certificate);
case SbpMsgEcdsaSignatureDep:
return sbp_msg_ecdsa_signature_dep_encoded_len(&msg->ecdsa_signature_dep);
case SbpMsgEcdsaSignature:
return sbp_msg_ecdsa_signature_encoded_len(&msg->ecdsa_signature);
case SbpMsgEd25519CertificateDep:
Expand Down Expand Up @@ -2226,6 +2235,9 @@ static inline int sbp_message_cmp(sbp_msg_type_t msg_type, const sbp_msg_t *a,
case SbpMsgEcdsaCertificate:
return sbp_msg_ecdsa_certificate_cmp(&a->ecdsa_certificate,
&b->ecdsa_certificate);
case SbpMsgEcdsaSignatureDep:
return sbp_msg_ecdsa_signature_dep_cmp(&a->ecdsa_signature_dep,
&b->ecdsa_signature_dep);
case SbpMsgEcdsaSignature:
return sbp_msg_ecdsa_signature_cmp(&a->ecdsa_signature,
&b->ecdsa_signature);
Expand Down
1 change: 1 addition & 0 deletions c/include/libsbp/v4/signing.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include <libsbp/v4/signing/MSG_CERTIFICATE_CHAIN.h>
#include <libsbp/v4/signing/MSG_ECDSA_CERTIFICATE.h>
#include <libsbp/v4/signing/MSG_ECDSA_SIGNATURE.h>
#include <libsbp/v4/signing/MSG_ECDSA_SIGNATURE_DEP.h>
#include <libsbp/v4/signing/MSG_ED25519_CERTIFICATE_DEP.h>
#include <libsbp/v4/signing/MSG_ED25519_SIGNATURE_DEP_A.h>
#include <libsbp/v4/signing/MSG_ED25519_SIGNATURE_DEP_B.h>
Expand Down
10 changes: 9 additions & 1 deletion c/include/libsbp/v4/signing/MSG_ECDSA_SIGNATURE.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,15 @@ typedef struct {
u8 certificate_id[SBP_MSG_ECDSA_SIGNATURE_CERTIFICATE_ID_MAX];

/**
* ECDSA signature for the messages using SHA-256 as the digest algorithm.
* Number of bytes to use of the signature field. The DER encoded signature
* has a maximum size of 72 bytes but can vary between 70 and 72 bytes in
* length.
*/
u8 n_signature_bytes;

/**
* DER encoded ECDSA signature for the messages using SHA-256 as the digest
* algorithm.
*/
u8 signature[SBP_MSG_ECDSA_SIGNATURE_SIGNATURE_MAX];

Expand Down
Loading