You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

175 lines
4.6 KiB

  1. /**
  2. * @file decaf_crypto.h
  3. * @copyright
  4. * Copyright (c) 2015 Cryptography Research, Inc. \n
  5. * Released under the MIT License. See LICENSE.txt for license information.
  6. * @author Mike Hamburg
  7. * @brief Decaf cyrpto routines.
  8. * @warning Experimental! The names, parameter orders etc are likely to change.
  9. */
  10. #ifndef __DECAF_CRYPTO_H__
  11. #define __DECAF_CRYPTO_H__ 1
  12. #include "decaf.h"
  13. #include "shake.h"
  14. #define DECAF_448_SYMMETRIC_KEY_BYTES 32
  15. /** @cond internal */
  16. #define API_VIS __attribute__((visibility("default"))) __attribute__((noinline)) // TODO: synergize with decaf.h
  17. #define WARN_UNUSED __attribute__((warn_unused_result))
  18. #define NONNULL1 __attribute__((nonnull(1)))
  19. #define NONNULL2 __attribute__((nonnull(1,2)))
  20. #define NONNULL3 __attribute__((nonnull(1,2,3)))
  21. #define NONNULL134 __attribute__((nonnull(1,3,4)))
  22. #define NONNULL5 __attribute__((nonnull(1,2,3,4,5)))
  23. /** @endcond */
  24. /** A symmetric key, the compressed point of a private key. */
  25. typedef unsigned char decaf_448_symmetric_key_t[DECAF_448_SYMMETRIC_KEY_BYTES];
  26. /** An encoded public key. */
  27. typedef unsigned char decaf_448_public_key_t[DECAF_448_SER_BYTES];
  28. /** A signature. */
  29. typedef unsigned char decaf_448_signature_t[DECAF_448_SER_BYTES + DECAF_448_SCALAR_BYTES];
  30. /** A private key. */
  31. typedef struct {
  32. decaf_448_symmetric_key_t sym;
  33. decaf_448_scalar_t secret_scalar;
  34. decaf_448_public_key_t pub;
  35. } decaf_448_private_key_t[1];
  36. #ifdef __cplusplus
  37. extern "C" {
  38. #endif
  39. /**
  40. * @brief Derive a key from its compressed form.
  41. * @param [out] privkey The derived private key.
  42. * @param [in] proto The compressed or proto-key, which must be 32 random bytes.
  43. */
  44. void decaf_448_derive_private_key (
  45. decaf_448_private_key_t priv,
  46. const decaf_448_symmetric_key_t proto
  47. ) NONNULL2 API_VIS;
  48. /**
  49. * @brief Destroy a private key.
  50. */
  51. void decaf_448_destroy_private_key (
  52. decaf_448_private_key_t priv
  53. ) NONNULL1 API_VIS;
  54. /**
  55. * @brief Convert a private key to a public one.
  56. * @param [out] pub The extracted private key.
  57. * @param [in] priv The private key.
  58. */
  59. void decaf_448_private_to_public (
  60. decaf_448_public_key_t pub,
  61. const decaf_448_private_key_t priv
  62. ) NONNULL2 API_VIS;
  63. /**
  64. * @brief Compute a Diffie-Hellman shared secret.
  65. *
  66. * This is an example routine; real protocols would use something
  67. * protocol-specific.
  68. *
  69. * @param [out] shared A buffer to store the shared secret.
  70. * @param [in] shared_bytes The size of the buffer.
  71. * @param [in] my_privkey My private key.
  72. * @param [in] your_pubkey Your public key.
  73. *
  74. * @retval DECAF_SUCCESS Key exchange was successful.
  75. * @retval DECAF_FAILURE Key exchange failed.
  76. *
  77. * @warning This is a pretty silly shared secret computation
  78. * and will almost definitely change in the future.
  79. */
  80. decaf_bool_t
  81. decaf_448_shared_secret (
  82. uint8_t *shared,
  83. size_t shared_bytes,
  84. const decaf_448_private_key_t my_privkey,
  85. const decaf_448_public_key_t your_pubkey
  86. ) NONNULL134 WARN_UNUSED API_VIS;
  87. /**
  88. * @brief Sign a message from its SHAKE context.
  89. *
  90. * @param [out] sig The signature.
  91. * @param [in] priv Your private key.
  92. * @param [in] shake A SHAKE256 context with the message.
  93. */
  94. void
  95. decaf_448_sign_shake (
  96. decaf_448_signature_t sig,
  97. const decaf_448_private_key_t priv,
  98. const keccak_sponge_t shake
  99. ) NONNULL3 API_VIS;
  100. /**
  101. * @brief Sign a message from its SHAKE context.
  102. *
  103. * @param [out] sig The signature.
  104. * @param [in] priv Your private key.
  105. * @param [in] message The message.
  106. * @param [in] message_len The message's length.
  107. */
  108. void
  109. decaf_448_sign (
  110. decaf_448_signature_t sig,
  111. const decaf_448_private_key_t priv,
  112. const unsigned char *message,
  113. size_t message_len
  114. ) NONNULL3 API_VIS;
  115. /**
  116. * @brief Verify a signed message from its SHAKE context.
  117. *
  118. * @param [in] sig The signature.
  119. * @param [in] pub The public key.
  120. * @param [in] shake A SHAKE256 context with the message.
  121. */
  122. decaf_bool_t
  123. decaf_448_verify_shake (
  124. const decaf_448_signature_t sig,
  125. const decaf_448_public_key_t pub,
  126. const keccak_sponge_t shake
  127. ) NONNULL3 API_VIS WARN_UNUSED;
  128. /**
  129. * @brief Verify a signed message.
  130. *
  131. * @param [in] sig The signature.
  132. * @param [in] pub The public key.
  133. * @param [in] message The message.
  134. * @param [in] message_len The message's length.
  135. */
  136. decaf_bool_t
  137. decaf_448_verify (
  138. const decaf_448_signature_t sig,
  139. const decaf_448_public_key_t pub,
  140. const unsigned char *message,
  141. size_t message_len
  142. ) NONNULL3 API_VIS WARN_UNUSED;
  143. #undef API_VIS
  144. #undef WARN_UNUSED
  145. #undef NONNULL1
  146. #undef NONNULL2
  147. #undef NONNULL3
  148. #undef NONNULL134
  149. #undef NONNULL5
  150. #ifdef __cplusplus
  151. }; /* extern "C" */
  152. #endif
  153. #endif /* __DECAF_CRYPTO_H__ */