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.
 
 
 
 
 

217 lines
6.6 KiB

  1. /**
  2. * @file src/GENERATED/c/decaf/crypto_448.hxx
  3. * @author Mike Hamburg
  4. *
  5. * @copyright
  6. * Copyright (c) 2015-2016 Cryptography Research, Inc. \n
  7. * Released under the MIT License. See LICENSE.txt for license information.
  8. *
  9. *
  10. *
  11. * @warning This file was automatically generated in Python.
  12. * Please do not edit it.
  13. */
  14. #ifndef __SRC_GENERATED_C_DECAF_CRYPTO_448_HXX__
  15. #define __SRC_GENERATED_C_DECAF_CRYPTO_448_HXX__ 1
  16. /*
  17. * Example Decaf cyrpto routines, C++ wrapper.
  18. * @warning These are merely examples, though they ought to be secure. But real
  19. * protocols will decide differently on magic numbers, formats, which items to
  20. * hash, etc.
  21. * @warning Experimental! The names, parameter orders etc are likely to change.
  22. */
  23. #include <decaf/decaf_448.hxx>
  24. #include <decaf/shake.hxx>
  25. #include <decaf/strobe.hxx>
  26. /** @cond internal */
  27. #if __cplusplus >= 201103L
  28. #define NOEXCEPT noexcept
  29. #else
  30. #define NOEXCEPT throw()
  31. #endif
  32. /** @endcond */
  33. namespace decaf { namespace TOY {
  34. /** A public key for crypto over some Group */
  35. template <typename Group> class PublicKey;
  36. /** A private key for crypto over some Group */
  37. template <typename Group> class PrivateKey;
  38. /** A public key for crypto over Ed448-Goldilocks */
  39. template<> class PublicKey<Ed448Goldilocks>
  40. : public Serializable< PublicKey<Ed448Goldilocks> > {
  41. private:
  42. /** @cond internal */
  43. typedef decaf_448_TOY_public_key_t Wrapped;
  44. Wrapped wrapped;
  45. template<class Group> friend class PrivateKey;
  46. /** @endcond */
  47. public:
  48. /** Underlying group */
  49. typedef Ed448Goldilocks Group;
  50. /** Signature size. */
  51. static const size_t SIG_BYTES = sizeof(decaf_448_TOY_signature_t);
  52. /** Serialization size. */
  53. static const size_t SER_BYTES = sizeof(Wrapped);
  54. /** Read a private key from a string*/
  55. inline explicit PublicKey(const FixedBlock<SER_BYTES> &b) NOEXCEPT {
  56. memcpy(wrapped,b.data(),sizeof(wrapped));
  57. }
  58. /** Read a private key from a string*/
  59. inline explicit PublicKey(const PrivateKey<Ed448Goldilocks> &b) NOEXCEPT;
  60. /** Create but don't initialize */
  61. inline explicit PublicKey(const NOINIT&) NOEXCEPT { }
  62. /** Serialize into a buffer. */
  63. inline void serialize_into(unsigned char *x) const NOEXCEPT {
  64. memcpy(x,wrapped,sizeof(wrapped));
  65. }
  66. /** Serialization size. */
  67. inline size_t ser_size() const NOEXCEPT { return SER_BYTES; }
  68. /** Verify a message */
  69. inline void verify(
  70. const Block &message,
  71. const FixedBlock<SIG_BYTES> &sig
  72. ) const throw(CryptoException) {
  73. if (DECAF_SUCCESS != decaf_448_TOY_verify(sig.data(),wrapped,message.data(),message.size())) {
  74. throw(CryptoException());
  75. }
  76. }
  77. /** Verify a message */
  78. inline void verify(
  79. Strobe &context,
  80. const FixedBlock<SIG_BYTES> &sig
  81. ) const throw(CryptoException) {
  82. if (DECAF_SUCCESS != decaf_448_TOY_verify_strobe(context.wrapped,sig.data(),wrapped)) {
  83. throw(CryptoException());
  84. }
  85. }
  86. };
  87. /** A private key for crypto over Ed448-Goldilocks */
  88. template<> class PrivateKey<Ed448Goldilocks>
  89. : public Serializable< PrivateKey<Ed448Goldilocks> > {
  90. private:
  91. /** @cond internal */
  92. typedef decaf_448_TOY_private_key_t Wrapped;
  93. Wrapped wrapped;
  94. template<class Group> friend class PublicKey;
  95. /** @endcond */
  96. public:
  97. /** Underlying group */
  98. typedef Ed448Goldilocks Group;
  99. /** Signature size. */
  100. static const size_t SIG_BYTES = sizeof(decaf_448_TOY_signature_t);
  101. /** Serialization size. */
  102. static const size_t SER_BYTES = sizeof(Wrapped);
  103. /** Compressed size. */
  104. static const size_t SYM_BYTES = DECAF_448_SYMMETRIC_KEY_BYTES;
  105. /** Create but don't initialize */
  106. inline explicit PrivateKey(const NOINIT&) NOEXCEPT { }
  107. /** Read a private key from a string*/
  108. inline explicit PrivateKey(const FixedBlock<SER_BYTES> &b) NOEXCEPT {
  109. memcpy(wrapped,b.data(),sizeof(wrapped));
  110. }
  111. /** Read a private key from a string*/
  112. inline explicit PrivateKey(const FixedBlock<SYM_BYTES> &b) NOEXCEPT {
  113. decaf_448_TOY_derive_private_key(wrapped, b.data());
  114. }
  115. /** Create at random */
  116. inline explicit PrivateKey(Rng &r) NOEXCEPT {
  117. FixedArrayBuffer<SYM_BYTES> tmp(r);
  118. decaf_448_TOY_derive_private_key(wrapped, tmp.data());
  119. }
  120. /** Secure destructor */
  121. inline ~PrivateKey() NOEXCEPT {
  122. decaf_448_TOY_destroy_private_key(wrapped);
  123. }
  124. /** Serialization size. */
  125. inline size_t ser_size() const NOEXCEPT { return SER_BYTES; }
  126. /** Serialize into a buffer. */
  127. inline void serialize_into(unsigned char *x) const NOEXCEPT {
  128. memcpy(x,wrapped,sizeof(wrapped));
  129. }
  130. /** Compressed serialize. */
  131. inline SecureBuffer compress() const throw(std::bad_alloc) {
  132. SecureBuffer ret(sizeof(wrapped->sym));
  133. memcpy(ret.data(),wrapped->sym,sizeof(wrapped->sym));
  134. return ret;
  135. }
  136. /** Get the public key */
  137. inline PublicKey<Ed448Goldilocks> pub() const NOEXCEPT {
  138. PublicKey<Ed448Goldilocks> ret(*this); return ret;
  139. }
  140. /** Derive a shared secret */
  141. inline SecureBuffer shared_secret(
  142. const PublicKey<Ed448Goldilocks> &pub,
  143. size_t bytes,
  144. bool me_first
  145. ) const throw(CryptoException,std::bad_alloc) {
  146. SecureBuffer ret(bytes);
  147. if (DECAF_SUCCESS != decaf_448_TOY_shared_secret(ret.data(),bytes,wrapped,pub.wrapped,me_first)) {
  148. throw(CryptoException());
  149. }
  150. return ret;
  151. }
  152. /** Derive a shared secret */
  153. inline decaf_error_t __attribute__((warn_unused_result))
  154. shared_secret_noexcept(
  155. Buffer ret,
  156. const PublicKey<Ed448Goldilocks> &pub,
  157. bool me_first
  158. ) const NOEXCEPT {
  159. return decaf_448_TOY_shared_secret(ret.data(),ret.size(),wrapped,pub.wrapped,me_first);
  160. }
  161. /** Sign a message. */
  162. inline SecureBuffer sign(const Block &message) const {
  163. SecureBuffer sig(SIG_BYTES);
  164. decaf_448_TOY_sign(sig.data(), wrapped, message.data(), message.size());
  165. return sig;
  166. }
  167. /** Sign a message. */
  168. inline SecureBuffer verify(Strobe &context) const {
  169. SecureBuffer sig(SIG_BYTES);
  170. decaf_448_TOY_sign_strobe(context.wrapped, sig.data(), wrapped);
  171. return sig;
  172. }
  173. };
  174. /** @cond internal */
  175. PublicKey<Ed448Goldilocks>::PublicKey(const PrivateKey<Ed448Goldilocks> &b) NOEXCEPT {
  176. decaf_448_TOY_private_to_public(wrapped,b.wrapped);
  177. }
  178. /** @endcond */
  179. #undef NOEXCEPT
  180. }} /* namespace decaf::TOY */
  181. #endif /* __SRC_GENERATED_C_DECAF_CRYPTO_448_HXX__ */