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 25, 2024
1 parent 77bb730 commit 02cf30e
Show file tree
Hide file tree
Showing 2 changed files with 35 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
20 changes: 10 additions & 10 deletions sw/device/lib/crypto/include/datatypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,10 @@ typedef enum otcrypto_status_value {
* crypto library will throw an error if `len` doesn't match expectations.
*/
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 @@ -96,10 +96,10 @@ typedef struct otcrypto_byte_buf {
* otcrypto_byte_buf_t` would still allow data to change.
*/
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 @@ -110,10 +110,10 @@ typedef struct otcrypto_const_byte_buf {
* crypto library will throw an error if `len` doesn't match expectations.
*/
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 @@ -125,10 +125,10 @@ typedef struct otcrypto_word32_buf {
* otcrypto_word32_buf_t` would still allow data to change.
*/
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 +445,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 02cf30e

Please sign in to comment.