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.
 
 
 
 
 

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