Skip to content

Commit

Permalink
Added crypto_sign_detached to libsodium_wrapper api
Browse files Browse the repository at this point in the history
  • Loading branch information
Gustav Carlstedt authored and HRio committed Sep 13, 2024
1 parent e9d50c2 commit 6d8c9f1
Show file tree
Hide file tree
Showing 4 changed files with 145 additions and 0 deletions.
42 changes: 42 additions & 0 deletions src/external/libsodium/libsodium_wrapper.c
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,48 @@ int api_crypto_sign(uint8_t *signed_message,
return ret;
}

/**
* @brief Creates a signature using a secret signing key.
*
* Example usage:
* const uint8_t signature[api_crypto_sign_BYTES];
* const uint8_t signature_length;
* const uint8_t message[4] = { 'a', 'b', 'c', 'd' };
* uint64_t mlen = 4;
* const uint8_t secret_key[crypto_sign_SECRETKEYBYTES];
* api_crypto_sign_detached(signature, &signature_length, message, mlen, secret_key);
*
* @param signature Pointer where to store signature.
* @param signature_length Signature length will be returned if pointed value
* is not NULL. I.e, the pointer may be NULL. The length of
* the signature is always api_crypto_sign_BYTES.
*
* @param message Pointer to message to sign.
* @param message_length Length of message to sign.
* @param secret_key Pointer to signer's secret key, api_crypto_sign_SECRETKEYBYTES
* bytes long.
*
* @return 0 The signature was successfully created.
* @return != 0 The signature could not be created.
*/
int api_crypto_sign_detached(uint8_t *signature,
uint64_t *signature_length,
const uint8_t *message,
uint64_t message_length,
const uint8_t *secret_key)
{
unsigned long long smlen;
int ret = crypto_sign_detached(signature,
&smlen,
message,
message_length,
secret_key);
if (signature_length != NULL) {
*signature_length = smlen;
}
return ret;
}

/**
* @brief Verifies a signed message using the signer's public key.
*
Expand Down
30 changes: 30 additions & 0 deletions src/salt_crypto_wrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,36 @@ int api_crypto_sign(uint8_t *signed_message,
uint64_t message_length,
const uint8_t *secret_key);

/**
* @brief Creates a signature using a secret signing key.
*
* Example usage:
* const uint8_t signature[api_crypto_sign_BYTES];
* const uint8_t signature_length;
* const uint8_t message[4] = { 'a', 'b', 'c', 'd' };
* uint64_t mlen = 4;
* const uint8_t secret_key[crypto_sign_SECRETKEYBYTES];
* api_crypto_sign_detached(signature, &signature_length, message, mlen, secret_key);
*
* @param signature Pointer where to store signature.
* @param signature_length Signature length will be returned if pointed value
* is not NULL. I.e, the pointer may be NULL. The length of
* the signature is always api_crypto_sign_BYTES.
*
* @param message Pointer to message to sign.
* @param message_length Length of message to sign.
* @param secret_key Pointer to signer's secret key, api_crypto_sign_SECRETKEYBYTES
* bytes long.
*
* @return 0 The signature was successfully created.
* @return != 0 The signature could not be created.
*/
int api_crypto_sign_detached(uint8_t *signature,
uint64_t *signature_length,
const uint8_t *message,
uint64_t message_length,
const uint8_t *secret_key);

/**
* @brief Verifies a signed message using the signer's public key.
*
Expand Down
72 changes: 72 additions & 0 deletions src/salt_crypto_wrapper_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ int salt_crypto_wrapper_test(void)
VERIFY(test_api_crypto_box_beforenm() == 0);
VERIFY(test_api_crypto_box_afternm() == 0);
VERIFY(test_api_crypto_sign() == 0);
VERIFY(test_api_crypto_sign_detached() == 0);
VERIFY(test_api_crypto_hash() == 0);

return 0;
Expand Down Expand Up @@ -379,6 +380,77 @@ int test_api_crypto_sign(void)
return 0;
}

int test_api_crypto_sign_detached(void)
{
int ret;
const uint8_t message[26] = {
/* Ascii string: "Signed message from Alice" */
0x53, 0x69, 0x67, 0x6e, 0x65, 0x64, 0x20, 0x6d,
0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x20, 0x66,
0x72, 0x6f, 0x6d, 0x20, 0x41, 0x6c, 0x69, 0x63,
0x65, 0x00
};
const uint8_t expected_signature[api_crypto_sign_BYTES] = {
/* Signature of message[] signed by alice. */
0xf9, 0x01, 0xda, 0xe6, 0x52, 0x8a, 0x86, 0xb7,
0xa0, 0xb4, 0x2d, 0xef, 0xe8, 0xdf, 0x88, 0x3a,
0xa5, 0x52, 0x60, 0xd5, 0x3b, 0x0e, 0xed, 0xf3,
0x80, 0x58, 0xa5, 0x3d, 0xa3, 0xf3, 0xf5, 0x67,
0xce, 0x56, 0xa6, 0x09, 0x45, 0xbe, 0x71, 0x8f,
0x68, 0x3b, 0x39, 0x5c, 0xc1, 0x1e, 0xee, 0xab,
0x82, 0xaa, 0x69, 0x24, 0xc8, 0xa8, 0x35, 0x3a,
0x1a, 0x84, 0x8c, 0xf0, 0xa4, 0xab, 0x51, 0x06,
};

uint8_t calculated_signature[api_crypto_sign_BYTES];
memset(calculated_signature, 0x00, api_crypto_sign_BYTES);

uint64_t signature_length;

ret = api_crypto_sign_detached(calculated_signature,
&signature_length,
message,
sizeof(message),
alice_sk_sec);
VERIFY(0 == ret);
VERIFY(memcmp(calculated_signature, expected_signature, api_crypto_sign_BYTES) == 0);
VERIFY(signature_length == api_crypto_sign_BYTES);
memset(calculated_signature, 0x00, api_crypto_sign_BYTES);

/* api_crypto_sign allows NULL ptr on length arg. */
ret = api_crypto_sign_detached(calculated_signature,
NULL,
message,
sizeof(message),
alice_sk_sec);
VERIFY(0 == ret);
VERIFY(memcmp(calculated_signature, expected_signature, api_crypto_sign_BYTES) == 0);


/* Different message should give different signature. */
uint8_t message2[sizeof(message)];
memcpy(message2, message, sizeof(message));
message2[0] = ~message2[0];
ret = api_crypto_sign_detached(calculated_signature,
NULL,
message2,
sizeof(message2),
alice_sk_sec);
VERIFY(0 == ret);
VERIFY(memcmp(calculated_signature, expected_signature, api_crypto_sign_BYTES) != 0);

/* Different public key should give different signature. */
ret = api_crypto_sign_detached(calculated_signature,
NULL,
message,
sizeof(message),
bob_sk_sec);
VERIFY(0 == ret);
VERIFY(memcmp(calculated_signature, expected_signature, api_crypto_sign_BYTES) != 0);

return 0;
}

int test_api_crypto_hash(void)
{

Expand Down
1 change: 1 addition & 0 deletions src/salt_crypto_wrapper_test.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ int salt_crypto_wrapper_test(void);
int test_api_crypto_box_beforenm(void);
int test_api_crypto_box_afternm(void);
int test_api_crypto_sign(void);
int test_api_crypto_sign_detached(void);
int test_api_crypto_hash(void);

#ifdef __cplusplus
Expand Down

0 comments on commit 6d8c9f1

Please sign in to comment.