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.
 
 
 
 
 

164 lines
4.5 KiB

  1. /**
  2. * Example Decaf crypto routines.
  3. * @warning These are merely examples, though they ought to be secure. But real
  4. * protocols will decide differently on magic numbers, formats, which items to
  5. * hash, etc.
  6. * @warning Experimental! The names, parameter orders etc are likely to change.
  7. */
  8. #include <decaf/point_$(gf_bits).h>
  9. #include <decaf/strobe.h>
  10. #ifdef __cplusplus
  11. extern "C" {
  12. #endif
  13. /** Number of bytes for a symmetric key (expanded to full key) */
  14. #define $(C_NS)_SYMMETRIC_KEY_BYTES 32
  15. /** A symmetric key, the compressed point of a private key. */
  16. typedef unsigned char $(c_ns)_TOY_symmetric_key_t[$(C_NS)_SYMMETRIC_KEY_BYTES];
  17. /** An encoded public key. */
  18. typedef unsigned char $(c_ns)_TOY_public_key_t[$(C_NS)_SER_BYTES];
  19. /** A signature. */
  20. typedef unsigned char $(c_ns)_TOY_signature_t[$(C_NS)_SER_BYTES + $(C_NS)_SCALAR_BYTES];
  21. typedef struct {
  22. /** @cond internal */
  23. /** The symmetric key from which everything is expanded */
  24. $(c_ns)_TOY_symmetric_key_t sym;
  25. /** The scalar x */
  26. $(c_ns)_scalar_t secret_scalar;
  27. /** x*Base */
  28. $(c_ns)_TOY_public_key_t pub;
  29. /** @endcond */
  30. } /** Private key structure for pointers. */
  31. $(c_ns)_TOY_private_key_s,
  32. /** A private key (gmp array[1] style). */
  33. $(c_ns)_TOY_private_key_t[1];
  34. /**
  35. * Derive a key from its compressed form.
  36. * @param [out] priv The derived private key.
  37. * @param [in] proto The compressed or proto-key, which must be 32 random bytes.
  38. */
  39. void $(c_ns)_TOY_derive_private_key (
  40. $(c_ns)_TOY_private_key_t priv,
  41. const $(c_ns)_TOY_symmetric_key_t proto
  42. ) NONNULL API_VIS;
  43. /**
  44. * Destroy a private key.
  45. */
  46. void $(c_ns)_TOY_destroy_private_key (
  47. $(c_ns)_TOY_private_key_t priv
  48. ) NONNULL API_VIS;
  49. /**
  50. * Convert a private key to a public one.
  51. * @param [out] pub The extracted private key.
  52. * @param [in] priv The private key.
  53. */
  54. void $(c_ns)_TOY_private_to_public (
  55. $(c_ns)_TOY_public_key_t pub,
  56. const $(c_ns)_TOY_private_key_t priv
  57. ) NONNULL API_VIS;
  58. /**
  59. * Compute a Diffie-Hellman shared secret.
  60. *
  61. * This is an example routine; real protocols would use something
  62. * protocol-specific.
  63. *
  64. * @param [out] shared A buffer to store the shared secret.
  65. * @param [in] shared_bytes The size of the buffer.
  66. * @param [in] my_privkey My private key.
  67. * @param [in] your_pubkey Your public key.
  68. * @param [in] me_first Direction flag to break symmetry.
  69. *
  70. * @retval DECAF_SUCCESS Key exchange was successful.
  71. * @retval DECAF_FAILURE Key exchange failed.
  72. */
  73. decaf_error_t
  74. $(c_ns)_TOY_shared_secret (
  75. uint8_t *shared,
  76. size_t shared_bytes,
  77. const $(c_ns)_TOY_private_key_t my_privkey,
  78. const $(c_ns)_TOY_public_key_t your_pubkey,
  79. int me_first
  80. ) NONNULL WARN_UNUSED API_VIS;
  81. /**
  82. * Sign a message from a STROBE context.
  83. *
  84. * @param [out] sig The signature.
  85. * @param [in] priv Your private key.
  86. * @param [in] strobe A STROBE context with the message.
  87. */
  88. void
  89. $(c_ns)_TOY_sign_strobe (
  90. keccak_decaf_TOY_strobe_t strobe,
  91. $(c_ns)_TOY_signature_t sig,
  92. const $(c_ns)_TOY_private_key_t priv
  93. ) NONNULL API_VIS;
  94. /**
  95. * Sign a message.
  96. *
  97. * @param [out] sig The signature.
  98. * @param [in] priv Your private key.
  99. * @param [in] message The message.
  100. * @param [in] message_len The message's length.
  101. */
  102. void
  103. $(c_ns)_TOY_sign (
  104. $(c_ns)_TOY_signature_t sig,
  105. const $(c_ns)_TOY_private_key_t priv,
  106. const unsigned char *message,
  107. size_t message_len
  108. ) NONNULL API_VIS;
  109. /**
  110. * Verify a signed message from its STROBE context.
  111. *
  112. * @param [in] sig The signature.
  113. * @param [in] pub The public key.
  114. * @param [in] strobe A STROBE context with the message.
  115. *
  116. * @return DECAF_SUCCESS The signature verified successfully.
  117. * @return DECAF_FAILURE The signature did not verify successfully.
  118. */
  119. decaf_error_t
  120. $(c_ns)_TOY_verify_strobe (
  121. keccak_decaf_TOY_strobe_t strobe,
  122. const $(c_ns)_TOY_signature_t sig,
  123. const $(c_ns)_TOY_public_key_t pub
  124. ) NONNULL API_VIS WARN_UNUSED;
  125. /**
  126. * Verify a signed message.
  127. *
  128. * @param [in] sig The signature.
  129. * @param [in] pub The public key.
  130. * @param [in] message The message.
  131. * @param [in] message_len The message's length.
  132. *
  133. * @return DECAF_SUCCESS The signature verified successfully.
  134. * @return DECAF_FAILURE The signature did not verify successfully.
  135. */
  136. decaf_error_t
  137. $(c_ns)_TOY_verify (
  138. const $(c_ns)_TOY_signature_t sig,
  139. const $(c_ns)_TOY_public_key_t pub,
  140. const unsigned char *message,
  141. size_t message_len
  142. ) NONNULL API_VIS WARN_UNUSED;
  143. #ifdef __cplusplus
  144. } /* extern "C" */
  145. #endif