KeyAgreementInitiator.hpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. Copyright (c) 2009-2010 Christopher A. Taylor. All rights reserved.
  3. Redistribution and use in source and binary forms, with or without
  4. modification, are permitted provided that the following conditions are met:
  5. * Redistributions of source code must retain the above copyright notice,
  6. this list of conditions and the following disclaimer.
  7. * Redistributions in binary form must reproduce the above copyright notice,
  8. this list of conditions and the following disclaimer in the documentation
  9. and/or other materials provided with the distribution.
  10. * Neither the name of LibCat nor the names of its contributors may be used
  11. to endorse or promote products derived from this software without
  12. specific prior written permission.
  13. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  14. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  15. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  16. ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
  17. LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  18. CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  19. SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  20. INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  21. CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  22. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  23. POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #ifndef CAT_KEY_AGREEMENT_INITIATOR_HPP
  26. #define CAT_KEY_AGREEMENT_INITIATOR_HPP
  27. #include <cat/crypt/tunnel/KeyAgreement.hpp>
  28. #include <cat/crypt/tunnel/AuthenticatedEncryption.hpp>
  29. namespace cat {
  30. class CAT_EXPORT KeyAgreementInitiator : public KeyAgreementCommon
  31. {
  32. Leg *B; // Responder's public key (pre-shared with initiator)
  33. Leg *a; // Initiator's private key (kept secret)
  34. Leg *A; // Initiator's public key (shared with responder in Challenge message)
  35. Leg *hB; // h*B
  36. Leg *G_MultPrecomp; // Precomputed table for multiplication
  37. Leg *B_MultPrecomp; // Precomputed table for multiplication
  38. Leg *Y_MultPrecomp; // Precomputed table for multiplication
  39. Leg *A_neutral; // Endian-neutral A
  40. Leg *B_neutral; // Endian-neutral B
  41. // Identity data
  42. Leg *I_private; // Initiator's identity private key
  43. Leg *I_public; // Endian-neutral initiator's identity public key
  44. bool AllocateMemory();
  45. void FreeMemory();
  46. public:
  47. KeyAgreementInitiator();
  48. ~KeyAgreementInitiator();
  49. bool Initialize(BigTwistedEdwards *math,
  50. const u8 *responder_public_key, int public_bytes);
  51. // Call after Initialize()
  52. bool SetIdentity(BigTwistedEdwards *math,
  53. const u8 *initiator_public_key, int public_bytes,
  54. const u8 *initiator_private_key, int private_bytes);
  55. public:
  56. bool GenerateChallenge(BigTwistedEdwards *math, FortunaOutput *csprng,
  57. u8 *initiator_challenge, int challenge_bytes);
  58. bool ProcessAnswer(BigTwistedEdwards *math,
  59. const u8 *responder_answer, int answer_bytes,
  60. Skein *key_hash);
  61. // Will fail if SetIdentity() has not been called
  62. bool ProcessAnswerWithIdentity(BigTwistedEdwards *math, FortunaOutput *csprng,
  63. const u8 *responder_answer, int answer_bytes,
  64. Skein *key_hash,
  65. u8 *identity_proof, int proof_bytes);
  66. CAT_INLINE bool KeyEncryption(Skein *key_hash, AuthenticatedEncryption *auth_enc, const char *key_name)
  67. {
  68. return auth_enc->SetKey(KeyBytes, key_hash, true, key_name);
  69. }
  70. // Erase the private key after handshake completes
  71. // Also done as this object is destroyed
  72. void SecureErasePrivateKey();
  73. public:
  74. bool Verify(BigTwistedEdwards *math,
  75. const u8 *message, int message_bytes,
  76. const u8 *signature, int signature_bytes);
  77. };
  78. } // namespace cat
  79. #endif // CAT_KEY_AGREEMENT_INITIATOR_HPP
粤ICP备19079148号