DiffieHellmanImpl.java 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /* DiffieHellmanImpl.java -- implementation of the Diffie-Hellman key agreement.
  2. Copyright (C) 2005, 2006 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package gnu.javax.crypto.jce;
  32. import java.math.BigInteger;
  33. import java.security.InvalidKeyException;
  34. import java.security.Key;
  35. import java.security.SecureRandom;
  36. import java.security.spec.AlgorithmParameterSpec;
  37. import javax.crypto.KeyAgreementSpi;
  38. import javax.crypto.SecretKey;
  39. import javax.crypto.ShortBufferException;
  40. import javax.crypto.interfaces.DHPrivateKey;
  41. import javax.crypto.interfaces.DHPublicKey;
  42. import javax.crypto.spec.DHParameterSpec;
  43. import javax.crypto.spec.SecretKeySpec;
  44. /**
  45. * The JCE implementation of a 2-party Diffie-Hellman key agreement.
  46. *
  47. * @author Casey Marshall (csm@gnu.org)
  48. */
  49. public final class DiffieHellmanImpl
  50. extends KeyAgreementSpi
  51. {
  52. /** The private key being used for this agreement. */
  53. private DHPrivateKey key;
  54. /** The current result. */
  55. private byte[] result;
  56. /** True if the caller told us we are done. */
  57. private boolean last_phase_done;
  58. /** Trivial default constructor. */
  59. public DiffieHellmanImpl()
  60. {
  61. super();
  62. key = null;
  63. result = null;
  64. last_phase_done = false;
  65. }
  66. protected Key engineDoPhase(Key incoming, boolean lastPhase)
  67. throws InvalidKeyException
  68. {
  69. if (key == null)
  70. throw new IllegalStateException("Not initialized");
  71. if (last_phase_done)
  72. throw new IllegalStateException("Last phase already done");
  73. if (! (incoming instanceof DHPublicKey))
  74. throw new InvalidKeyException("Key MUST be a DHPublicKey");
  75. DHPublicKey pub = (DHPublicKey) incoming;
  76. DHParameterSpec s1 = key.getParams();
  77. DHParameterSpec s2 = pub.getParams();
  78. if (! s1.getG().equals(s2.getG()) || ! s1.getP().equals(s2.getP()))
  79. throw new InvalidKeyException("Incompatible key");
  80. if (! lastPhase)
  81. throw new IllegalArgumentException(
  82. "This key-agreement MUST be concluded in one step only");
  83. BigInteger resultBI = pub.getY().modPow(key.getX(), s1.getP());
  84. result = resultBI.toByteArray();
  85. if (result[0] == 0x00)
  86. {
  87. byte[] buf = new byte[result.length - 1];
  88. System.arraycopy(result, 1, buf, 0, buf.length);
  89. result = buf;
  90. }
  91. last_phase_done = true;
  92. return null;
  93. }
  94. protected byte[] engineGenerateSecret()
  95. {
  96. checkState();
  97. byte[] res = (byte[]) result.clone();
  98. reset();
  99. return res;
  100. }
  101. protected int engineGenerateSecret(byte[] secret, int offset)
  102. throws ShortBufferException
  103. {
  104. checkState();
  105. if (result.length > secret.length - offset)
  106. throw new ShortBufferException();
  107. System.arraycopy(result, 0, secret, offset, result.length);
  108. int res = result.length;
  109. reset();
  110. return res;
  111. }
  112. protected SecretKey engineGenerateSecret(String algorithm)
  113. throws InvalidKeyException
  114. {
  115. checkState();
  116. byte[] s = (byte[]) result.clone();
  117. SecretKey res = new SecretKeySpec(s, algorithm);
  118. reset();
  119. return res;
  120. }
  121. protected void engineInit(Key key, SecureRandom random)
  122. throws InvalidKeyException
  123. {
  124. if (! (key instanceof DHPrivateKey))
  125. throw new InvalidKeyException("Key MUST be a DHPrivateKey");
  126. this.key = (DHPrivateKey) key;
  127. reset();
  128. }
  129. protected void engineInit(Key key, AlgorithmParameterSpec params,
  130. SecureRandom random)
  131. throws InvalidKeyException
  132. {
  133. engineInit(key, random);
  134. }
  135. private void reset()
  136. {
  137. result = null;
  138. last_phase_done = false;
  139. }
  140. private void checkState()
  141. {
  142. if (result == null || ! last_phase_done)
  143. throw new IllegalStateException("Not finished");
  144. }
  145. }