Skip to content

Commit

Permalink
[crypto] Change ordering of buffer fields to match Rust.
Browse files Browse the repository at this point in the history
This is helpful for compatibility when the cryptolib is called from Rust
code.

Signed-off-by: Jade Philipoom <[email protected]>
  • Loading branch information
jadephilipoom committed Jan 19, 2024
1 parent 7737a07 commit 800fe89
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 35 deletions.
50 changes: 25 additions & 25 deletions sw/device/lib/crypto/impl/key_transport_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -129,12 +129,12 @@ TEST(KeyTransport, BlindedKeyImportExport) {
// Import the key into the blinded key struct.
EXPECT_EQ(status_ok(otcrypto_import_blinded_key(
(otcrypto_const_word32_buf_t){
.len = share0.size(),
.data = share0.data(),
.len = share0.size(),
},
(otcrypto_const_word32_buf_t){
.len = share1.size(),
.data = share1.data(),
.len = share1.size(),
},
&blinded_key)),
true);
Expand All @@ -145,15 +145,15 @@ TEST(KeyTransport, BlindedKeyImportExport) {

// Export the key again.
otcrypto_word32_buf_t share0_buf = {
.len = share0.size(),
.data = share0.data(),
.len = share0.size(),
};
otcrypto_word32_buf_t share1_buf = {
.len = share1.size(),
.data = share1.data(),
.len = share1.size(),
};
EXPECT_EQ(status_ok(otcrypto_export_blinded_key(blinded_key, &share0_buf,
&share1_buf)),
EXPECT_EQ(status_ok(otcrypto_export_blinded_key(blinded_key, share0_buf,
share1_buf)),
true);

// Unmask the result and compare to the unmasked key.
Expand All @@ -179,25 +179,25 @@ TEST(KeyTransport, BlindedKeyImportBadLengths) {
// Set a bad length for share 0 and expect the import to fail.
EXPECT_EQ(status_ok(otcrypto_import_blinded_key(
(otcrypto_const_word32_buf_t){
.len = share0.size() - 1,
.data = share0.data(),
.len = share0.size() - 1,
},
(otcrypto_const_word32_buf_t){
.len = share1.size(),
.data = share1.data(),
.len = share1.size(),
},
&blinded_key)),
false);

// Set a bad length for share 1 and expect the import to fail.
EXPECT_EQ(status_ok(otcrypto_import_blinded_key(
(otcrypto_const_word32_buf_t){
.len = share0.size(),
.data = share0.data(),
.len = share0.size(),
},
(otcrypto_const_word32_buf_t){
.len = share1.size() - 1,
.data = share1.data(),
.len = share1.size() - 1,
},
&blinded_key)),
false);
Expand All @@ -210,12 +210,12 @@ TEST(KeyTransport, BlindedKeyImportBadLengths) {
};
EXPECT_EQ(status_ok(otcrypto_import_blinded_key(
(otcrypto_const_word32_buf_t){
.len = share0.size(),
.data = share0.data(),
.len = share0.size(),
},
(otcrypto_const_word32_buf_t){
.len = share1.size(),
.data = share1.data(),
.len = share1.size(),
},
&bad_blinded_key)),
false);
Expand All @@ -238,33 +238,33 @@ TEST(KeyTransport, BlindedKeyExportBadLengths) {
// Import the key.
EXPECT_EQ(status_ok(otcrypto_import_blinded_key(
(otcrypto_const_word32_buf_t){
.len = share0.size(),
.data = share0.data(),
.len = share0.size(),
},
(otcrypto_const_word32_buf_t){
.len = share1.size(),
.data = share1.data(),
.len = share1.size(),
},
&blinded_key)),
true);

otcrypto_word32_buf_t share_with_good_length = {
.len = share0.size(),
.data = share0.data(),
.len = share0.size(),
};
otcrypto_word32_buf_t share_with_bad_length = {
.len = share1.size() - 1,
.data = share1.data(),
.len = share1.size() - 1,
};

// Set a bad length for share 0 and expect the import to fail.
EXPECT_EQ(status_ok(otcrypto_export_blinded_key(
blinded_key, &share_with_bad_length, &share_with_good_length)),
blinded_key, share_with_bad_length, share_with_good_length)),
false);

// Set a bad length for share 1 and expect the import to fail.
EXPECT_EQ(status_ok(otcrypto_export_blinded_key(
blinded_key, &share_with_good_length, &share_with_bad_length)),
blinded_key, share_with_good_length, share_with_bad_length)),
false);

// Set a bad length for the keyblob and expect the export to fail.
Expand All @@ -275,7 +275,7 @@ TEST(KeyTransport, BlindedKeyExportBadLengths) {
};
EXPECT_EQ(
status_ok(otcrypto_export_blinded_key(
bad_blinded_key, &share_with_good_length, &share_with_good_length)),
bad_blinded_key, share_with_good_length, share_with_good_length)),
false);
}

Expand All @@ -296,27 +296,27 @@ TEST(KeyTransport, BlindedKeyExportNotExportable) {
// Import the key.
EXPECT_EQ(status_ok(otcrypto_import_blinded_key(
(otcrypto_const_word32_buf_t){
.len = share0.size(),
.data = share0.data(),
.len = share0.size(),
},
(otcrypto_const_word32_buf_t){
.len = share1.size(),
.data = share1.data(),
.len = share1.size(),
},
&blinded_key)),
true);

// Expect key export to fail.
otcrypto_word32_buf_t share0_buf = {
.len = share0.size(),
.data = share0.data(),
.len = share0.size(),
};
otcrypto_word32_buf_t share1_buf = {
.len = share1.size(),
.data = share1.data(),
.len = share1.size(),
};
EXPECT_EQ(status_ok(otcrypto_export_blinded_key(blinded_key, &share0_buf,
&share1_buf)),
EXPECT_EQ(status_ok(otcrypto_export_blinded_key(blinded_key, share0_buf,
share1_buf)),
false);
}

Expand Down
36 changes: 26 additions & 10 deletions sw/device/lib/crypto/include/datatypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,16 @@ typedef enum otcrypto_status_value {
* Note: the caller must (1) allocate sufficient space and (2) set the `len`
* field and `data` pointer when `otcrypto_byte_buf_t` is used for output. The
* crypto library will throw an error if `len` doesn't match expectations.
*
* The order of `data` and `len` is important here for Rust compatibility; if
* `data` comes first, the representation exactly matches slices and is easier
* to call from Rust.
*/
typedef struct otcrypto_byte_buf {
// Length of the data in bytes.
size_t len;
// Pointer to the data.
uint8_t *data;
// Length of the data in bytes.
size_t len;
} otcrypto_byte_buf_t;

/**
Expand All @@ -94,12 +98,16 @@ typedef struct otcrypto_byte_buf {
* necessary to have this structure separate from `otcrypto_byte_buf_t` because
* data pointed to by a struct does not inherit `const`, so `const
* otcrypto_byte_buf_t` would still allow data to change.
*
* The order of `data` and `len` is important here for Rust compatibility; if
* `data` comes first, the representation exactly matches slices and is easier
* to call from Rust.
*/
typedef struct otcrypto_const_byte_buf {
// Length of the data in bytes.
const size_t len;
// Pointer to the data.
const uint8_t *const data;
// Length of the data in bytes.
const size_t len;
} otcrypto_const_byte_buf_t;

/**
Expand All @@ -108,12 +116,16 @@ typedef struct otcrypto_const_byte_buf {
* Note: the caller must (1) allocate sufficient space and (2) set the `len`
* field and `data` pointer when `otcrypto_word32_buf_t` is used for output. The
* crypto library will throw an error if `len` doesn't match expectations.
*
* The order of `data` and `len` is important here for Rust compatibility; if
* `data` comes first, the representation exactly matches slices and is easier
* to call from Rust.
*/
typedef struct otcrypto_word32_buf {
// Length of the data in words.
size_t len;
// Pointer to the data.
uint32_t *data;
// Length of the data in words.
size_t len;
} otcrypto_word32_buf_t;

/**
Expand All @@ -123,12 +135,16 @@ typedef struct otcrypto_word32_buf {
* necessary to have this structure separate from `otcrypto_word32_buf_t`
* because data pointed to by a struct does not inherit `const`, so `const
* otcrypto_word32_buf_t` would still allow data to change.
*
* The order of `data` and `len` is important here for Rust compatibility; if
* `data` comes first, the representation exactly matches slices and is easier
* to call from Rust.
*/
typedef struct otcrypto_const_word32_buf {
// Length of the data in words.
const size_t len;
// Pointer to the data.
const uint32_t *const data;
// Length of the data in words.
const size_t len;
} otcrypto_const_word32_buf_t;

/**
Expand Down Expand Up @@ -445,10 +461,10 @@ typedef enum otcrypto_hash_mode {
typedef struct otcrypto_hash_digest {
// Digest type.
otcrypto_hash_mode_t mode;
// Digest length in 32-bit words.
size_t len;
// Digest data.
uint32_t *data;
// Digest length in 32-bit words.
size_t len;
} otcrypto_hash_digest_t;

#ifdef __cplusplus
Expand Down

0 comments on commit 800fe89

Please sign in to comment.