KeyAgreement.hpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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_HPP
  26. #define CAT_KEY_AGREEMENT_HPP
  27. #include <cat/math/BigTwistedEdwards.hpp>
  28. #include <cat/crypt/rand/Fortuna.hpp>
  29. namespace cat {
  30. /*
  31. Tunnel Key Agreement "Tabby" protocol:
  32. An unauthenticated Diffie-Hellman key agreement protocol with forward secrecy
  33. Immune to active attacks (man-in-the-middle) if server key is known ahead of time
  34. Using Elliptic Curve Cryptography over finite field Fp, p = 2^n - c, c small
  35. Shape of curve: a' * x^2 + y^2 = 1 + d' * x^2 * y^2, a' = -1 (square in Fp)
  36. d' (non square in Fp) -> order of curve = q * cofactor h, order of generator point = q
  37. Curves satisfy MOV conditions and are not anomalous
  38. Point operations performed with Extended Twisted Edwards group laws
  39. See BigTwistedEdwards.hpp for more information
  40. H: Skein-Key, either 256-bit or 512-bit based on security level
  41. MAC: Skein-MAC, keyed from output of H()
  42. Here the protocol initiator is the (c)lient, and the responder is the (s)erver:
  43. s: long-term private key 1 < b < q, long-term public key B = b * G
  44. 256-bit security: B = 64 bytes for public key, b = 32 bytes for private key
  45. 384-bit security: B = 96 bytes for public key, b = 48 bytes for private key
  46. 512-bit security: B = 128 bytes for public key, b = 64 bytes for private key
  47. c: Client already knows the server's public key B before Key Agreement
  48. c: ephemeral private key 1 < a < q, ephemeral public key A = a * G
  49. Initiator Challenge: c2s A
  50. 256-bit security: A = 64 bytes
  51. 384-bit security: A = 96 bytes
  52. 512-bit security: A = 128 bytes
  53. s: validate A, ignore invalid
  54. Invalid A(x,y) would be the additive identity x=0 or any point not on the curve
  55. s: ephemeral private key 1 < y < q, ephemeral public key Y = y * G
  56. Ephemeral key is re-used for several connections before being regenerated
  57. s: hA = h * A
  58. s: random n-bit number r
  59. s: d = H(A,B,Y,r)
  60. Repeat the previous two steps until d >= 1000
  61. s: e = b + d*y (mod q)
  62. s: T = AffineX(e * hA)
  63. s: k = H(d,T)
  64. Responder Answer: s2c Y || r || MAC(k) {"responder proof"}
  65. 256-bit security: Y(64by) r(32by) MAC(32by) = 128 bytes
  66. 384-bit security: Y(96by) r(48by) MAC(48by) = 192 bytes
  67. 512-bit security: Y(128by) r(64by) MAC(64by) = 256 bytes
  68. c: validate Y, ignore invalid
  69. Invalid Y(x,y) would be the additive identity x=0 or any point not on the curve
  70. c: hY = h * Y
  71. c: d = H(A,B,Y,r)
  72. c: Verify d >= 1000
  73. c: T = AffineX(a * hB + d*a * hY)
  74. c: k = H(d,T)
  75. c: validate MAC, ignore invalid
  76. Initiator Proof: c2s MAC(k) {"initiator proof"}
  77. This packet can also include the client's first encrypted message
  78. 256-bit security: MAC(32by) = 32 bytes
  79. 384-bit security: MAC(48by) = 48 bytes
  80. 512-bit security: MAC(64by) = 64 bytes
  81. s: validate MAC, ignore invalid
  82. Notes:
  83. The strategy of this protocol is to perform two EC Diffie-Hellman exchanges,
  84. one with the long-term server key and the second with an ephemeral key that
  85. should be much harder to obtain by an attacker. The resulting two shared
  86. secret points are added together into one point that is used for the key.
  87. It is perfectly acceptable to re-use an ephemeral key for several runs of
  88. the protocol. This means that most of the processing done by the server is
  89. just one point multiplication.
  90. */
  91. /*
  92. Schnorr signatures:
  93. For signing, the signer reuses its Key Agreement key pair (b,B)
  94. H: Skein-Key, either 256-bit or 512-bit based on security level
  95. To sign a message M, signer computes:
  96. ephemeral secret random 1 < k < q, ephemeral point K = k * G
  97. e = H(M || K)
  98. s = k - b*e (mod q)
  99. This process is repeated until e and s are non-zero
  100. Signature: s2c e || s
  101. 256-bit security: e(32by) s(32by) = 64 bytes
  102. 384-bit security: e(48by) s(48by) = 96 bytes
  103. 512-bit security: e(64by) s(64by) = 128 bytes
  104. To verify a signature:
  105. Check e, s are in the range [1,q-1]
  106. K' = s*G + e*B
  107. e' = H(M || K')
  108. The signature is verified if e == e'
  109. Notes:
  110. K ?= K'
  111. = s*G + e*B
  112. = (k - b*e)*G + e*(b*G)
  113. = k*G - b*e*G + e*b*G
  114. = K
  115. */
  116. // If CAT_DETERMINISTIC_KEY_GENERATION is undefined, the time to generate a
  117. // key is unbounded, but tends to be 1 try. I think this is a good thing
  118. // because it randomizes the runtime and helps avoid timing attacks
  119. //#define CAT_DETERMINISTIC_KEY_GENERATION
  120. // If CAT_USER_ERROR_CHECKING is defined, the key agreement objects will
  121. // check to make sure that the input parameters are all the right length
  122. // and that the math and prng objects are not null
  123. #define CAT_USER_ERROR_CHECKING
  124. class CAT_EXPORT KeyAgreementCommon
  125. {
  126. public:
  127. static BigTwistedEdwards *InstantiateMath(int bits);
  128. // Math library register usage
  129. static const int ECC_REG_OVERHEAD = 21;
  130. // c: field prime modulus p = 2^bits - C, p = 5 mod 8 s.t. a=-1 is a square in Fp
  131. // d: curve coefficient (yy-xx=1+Dxxyy), not a square in Fp
  132. static const int EDWARD_C_256 = 435;
  133. static const int EDWARD_D_256 = 31720;
  134. static const int EDWARD_C_384 = 2147;
  135. static const int EDWARD_D_384 = 13036;
  136. static const int EDWARD_C_512 = 875;
  137. static const int EDWARD_D_512 = 32;
  138. // Limits on field prime
  139. static const int MAX_BITS = 512;
  140. static const int MAX_BYTES = MAX_BITS / 8;
  141. static const int MAX_LEGS = MAX_BYTES / sizeof(Leg);
  142. protected:
  143. int KeyBits, KeyBytes, KeyLegs;
  144. bool Initialize(int bits);
  145. public:
  146. // Generates an unbiased random key in the range 1 < key < q
  147. void GenerateKey(BigTwistedEdwards *math, IRandom *prng, Leg *key);
  148. };
  149. } // namespace cat
  150. #endif // CAT_KEY_AGREEMENT_HPP
粤ICP备19079148号