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.
 
 
 
 
 

188 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. }
  41. /** Private key structure for pointers. */
  42. decaf_448_private_key_s,
  43. /** A private key (gmp array[1] style). */
  44. decaf_448_private_key_t[1];
  45. #ifdef __cplusplus
  46. extern "C" {
  47. #endif
  48. /**
  49. * @brief Derive a key from its compressed form.
  50. * @param [out] priv The derived private key.
  51. * @param [in] proto The compressed or proto-key, which must be 32 random bytes.
  52. */
  53. void decaf_448_derive_private_key (
  54. decaf_448_private_key_t priv,
  55. const decaf_448_symmetric_key_t proto
  56. ) NONNULL2 API_VIS;
  57. /**
  58. * @brief Destroy a private key.
  59. */
  60. void decaf_448_destroy_private_key (
  61. decaf_448_private_key_t priv
  62. ) NONNULL1 API_VIS;
  63. /**
  64. * @brief Convert a private key to a public one.
  65. * @param [out] pub The extracted private key.
  66. * @param [in] priv The private key.
  67. */
  68. void decaf_448_private_to_public (
  69. decaf_448_public_key_t pub,
  70. const decaf_448_private_key_t priv
  71. ) NONNULL2 API_VIS;
  72. /**
  73. * @brief Compute a Diffie-Hellman shared secret.
  74. *
  75. * This is an example routine; real protocols would use something
  76. * protocol-specific.
  77. *
  78. * @param [out] shared A buffer to store the shared secret.
  79. * @param [in] shared_bytes The size of the buffer.
  80. * @param [in] my_privkey My private key.
  81. * @param [in] your_pubkey Your public key.
  82. *
  83. * @retval DECAF_SUCCESS Key exchange was successful.
  84. * @retval DECAF_FAILURE Key exchange failed.
  85. *
  86. * @warning This is a pretty silly shared secret computation
  87. * and will almost definitely change in the future.
  88. */
  89. decaf_bool_t
  90. decaf_448_shared_secret (
  91. uint8_t *shared,
  92. size_t shared_bytes,
  93. const decaf_448_private_key_t my_privkey,
  94. const decaf_448_public_key_t your_pubkey
  95. ) NONNULL134 WARN_UNUSED API_VIS;
  96. /**
  97. * @brief Sign a message from its SHAKE context.
  98. *
  99. * @param [out] sig The signature.
  100. * @param [in] priv Your private key.
  101. * @param [in] shake A SHAKE256 context with the message.
  102. */
  103. void
  104. decaf_448_sign_shake (
  105. decaf_448_signature_t sig,
  106. const decaf_448_private_key_t priv,
  107. const keccak_sponge_t shake
  108. ) NONNULL3 API_VIS;
  109. /**
  110. * @brief Sign a message from its SHAKE context.
  111. *
  112. * @param [out] sig The signature.
  113. * @param [in] priv Your private key.
  114. * @param [in] message The message.
  115. * @param [in] message_len The message's length.
  116. */
  117. void
  118. decaf_448_sign (
  119. decaf_448_signature_t sig,
  120. const decaf_448_private_key_t priv,
  121. const unsigned char *message,
  122. size_t message_len
  123. ) NONNULL3 API_VIS;
  124. /**
  125. * @brief Verify a signed message from its SHAKE context.
  126. *
  127. * @param [in] sig The signature.
  128. * @param [in] pub The public key.
  129. * @param [in] shake A SHAKE256 context with the message.
  130. */
  131. decaf_bool_t
  132. decaf_448_verify_shake (
  133. const decaf_448_signature_t sig,
  134. const decaf_448_public_key_t pub,
  135. const keccak_sponge_t shake
  136. ) NONNULL3 API_VIS WARN_UNUSED;
  137. /**
  138. * @brief Verify a signed message.
  139. *
  140. * @param [in] sig The signature.
  141. * @param [in] pub The public key.
  142. * @param [in] message The message.
  143. * @param [in] message_len The message's length.
  144. */
  145. decaf_bool_t
  146. decaf_448_verify (
  147. const decaf_448_signature_t sig,
  148. const decaf_448_public_key_t pub,
  149. const unsigned char *message,
  150. size_t message_len
  151. ) NONNULL3 API_VIS WARN_UNUSED;
  152. #undef API_VIS
  153. #undef WARN_UNUSED
  154. #undef NONNULL1
  155. #undef NONNULL2
  156. #undef NONNULL3
  157. #undef NONNULL134
  158. #undef NONNULL5
  159. #ifdef __cplusplus
  160. }; /* extern "C" */
  161. #endif
  162. #endif /* __DECAF_CRYPTO_H__ */