SRP6User.java 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /* SRP6User.java --
  2. Copyright (C) 2003, 2006 Free Software Foundation, Inc.
  3. This file is a 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 of the License, or (at
  7. your option) 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; if not, write to the Free Software
  14. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
  15. 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.key.srp6;
  32. import gnu.java.security.hash.IMessageDigest;
  33. import gnu.java.security.util.Util;
  34. import gnu.javax.crypto.key.KeyAgreementException;
  35. import gnu.javax.crypto.key.IncomingMessage;
  36. import gnu.javax.crypto.key.OutgoingMessage;
  37. import gnu.javax.crypto.sasl.srp.SRP;
  38. import java.math.BigInteger;
  39. import java.security.KeyPair;
  40. import java.security.SecureRandom;
  41. import java.util.HashMap;
  42. import java.util.Map;
  43. /**
  44. * The implementation of the User in the SRP-6 protocol.
  45. * <p>
  46. * Reference:
  47. * <ol>
  48. * <li><a href="http://srp.stanford.edu/design.html">SRP Protocol Design</a><br>
  49. * Thomas J. Wu.</li>
  50. * </ol>
  51. */
  52. public class SRP6User
  53. extends SRP6KeyAgreement
  54. {
  55. /** The user's identity. */
  56. private String I;
  57. /** The user's cleartext password. */
  58. private byte[] p;
  59. /** The user's ephemeral key pair. */
  60. private KeyPair userKeyPair;
  61. // default 0-arguments constructor
  62. protected void engineInit(final Map attributes) throws KeyAgreementException
  63. {
  64. rnd = (SecureRandom) attributes.get(SOURCE_OF_RANDOMNESS);
  65. N = (BigInteger) attributes.get(SHARED_MODULUS);
  66. if (N == null)
  67. throw new KeyAgreementException("missing shared modulus");
  68. g = (BigInteger) attributes.get(GENERATOR);
  69. if (g == null)
  70. throw new KeyAgreementException("missing generator");
  71. final String md = (String) attributes.get(HASH_FUNCTION);
  72. if (md == null || md.trim().length() == 0)
  73. throw new KeyAgreementException("missing hash function");
  74. srp = SRP.instance(md);
  75. I = (String) attributes.get(USER_IDENTITY);
  76. if (I == null)
  77. throw new KeyAgreementException("missing user identity");
  78. p = (byte[]) attributes.get(USER_PASSWORD);
  79. if (p == null)
  80. throw new KeyAgreementException("missing user password");
  81. }
  82. protected OutgoingMessage engineProcessMessage(final IncomingMessage in)
  83. throws KeyAgreementException
  84. {
  85. switch (step)
  86. {
  87. case 0:
  88. return sendIdentity(in);
  89. case 1:
  90. return computeSharedSecret(in);
  91. default:
  92. throw new IllegalStateException("unexpected state");
  93. }
  94. }
  95. protected void engineReset()
  96. {
  97. I = null;
  98. p = null;
  99. userKeyPair = null;
  100. super.engineReset();
  101. }
  102. private OutgoingMessage sendIdentity(final IncomingMessage in)
  103. throws KeyAgreementException
  104. {
  105. // generate an ephemeral keypair
  106. final SRPKeyPairGenerator kpg = new SRPKeyPairGenerator();
  107. final Map attributes = new HashMap();
  108. if (rnd != null)
  109. attributes.put(SRPKeyPairGenerator.SOURCE_OF_RANDOMNESS, rnd);
  110. attributes.put(SRPKeyPairGenerator.SHARED_MODULUS, N);
  111. attributes.put(SRPKeyPairGenerator.GENERATOR, g);
  112. kpg.setup(attributes);
  113. userKeyPair = kpg.generate();
  114. final OutgoingMessage result = new OutgoingMessage();
  115. result.writeString(I);
  116. result.writeMPI(((SRPPublicKey) userKeyPair.getPublic()).getY());
  117. return result;
  118. }
  119. private OutgoingMessage computeSharedSecret(final IncomingMessage in)
  120. throws KeyAgreementException
  121. {
  122. final BigInteger s = in.readMPI();
  123. final BigInteger B = in.readMPI();
  124. final BigInteger A = ((SRPPublicKey) userKeyPair.getPublic()).getY();
  125. final BigInteger u = uValue(A, B); // u = H(A | B)
  126. final BigInteger x;
  127. try
  128. {
  129. x = new BigInteger(1, srp.computeX(Util.trim(s), I, p));
  130. }
  131. catch (Exception e)
  132. {
  133. throw new KeyAgreementException("computeSharedSecret()", e);
  134. }
  135. // compute S = (B - 3g^x) ^ (a + ux)
  136. final BigInteger a = ((SRPPrivateKey) userKeyPair.getPrivate()).getX();
  137. final BigInteger S = B.subtract(THREE.multiply(g.modPow(x, N)))
  138. .modPow(a.add(u.multiply(x)), N);
  139. final byte[] sBytes = Util.trim(S);
  140. final IMessageDigest hash = srp.newDigest();
  141. hash.update(sBytes, 0, sBytes.length);
  142. K = new BigInteger(1, hash.digest());
  143. complete = true;
  144. return null;
  145. }
  146. }