SRPKey.java 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /* SRPKey.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.Registry;
  33. import gnu.java.security.key.IKeyPairCodec;
  34. import java.io.Serializable;
  35. import java.math.BigInteger;
  36. import java.security.Key;
  37. /**
  38. * An abstract representation of a base SRP ephemeral key.
  39. * <p>
  40. * This object encapsulates the two numbers:
  41. * <ul>
  42. * <li><b>N</b>: A large safe prime (N = 2q+1, where q is prime).</li>
  43. * <li><b>g</b>: A generator modulo N.</li>
  44. * </ul>
  45. * <p>
  46. * Note that in SRP, all arithmetic is done modulo N.
  47. * <p>
  48. * Reference:
  49. * <ol>
  50. * <li><a href="http://srp.stanford.edu/design.html">SRP Protocol Design</a><br>
  51. * Thomas J. Wu.</li>
  52. * </ol>
  53. */
  54. public abstract class SRPKey
  55. implements Key, Serializable
  56. {
  57. /** The public, Germaine prime, shared modulus. */
  58. protected final BigInteger N;
  59. /** The generator. */
  60. protected final BigInteger g;
  61. protected SRPKey(BigInteger N, BigInteger g)
  62. {
  63. super();
  64. this.N = N;
  65. this.g = g;
  66. }
  67. /**
  68. * Returns the standard algorithm name for this key.
  69. *
  70. * @return the standard algorithm name for this key.
  71. */
  72. public String getAlgorithm()
  73. {
  74. return Registry.SRP_KPG;
  75. }
  76. /** @deprecated see getEncoded(int). */
  77. public byte[] getEncoded()
  78. {
  79. return getEncoded(IKeyPairCodec.RAW_FORMAT);
  80. }
  81. /**
  82. * Returns {@link Registry#RAW_ENCODING_SHORT_NAME} which is the sole format
  83. * supported for this type of keys.
  84. *
  85. * @return {@link Registry#RAW_ENCODING_SHORT_NAME} ALWAYS.
  86. */
  87. public String getFormat()
  88. {
  89. return Registry.RAW_ENCODING_SHORT_NAME;
  90. }
  91. /**
  92. * Returns the public shared modulus.
  93. *
  94. * @return <code>N</code>.
  95. */
  96. public BigInteger getN()
  97. {
  98. return N;
  99. }
  100. /**
  101. * Returns the generator.
  102. *
  103. * @return <code>g</code>.
  104. */
  105. public BigInteger getG()
  106. {
  107. return g;
  108. }
  109. /**
  110. * Returns <code>true</code> if the designated object is an instance of
  111. * <code>SRPKey</code> and has the same SRP parameter values as this one.
  112. *
  113. * @param obj the other non-null SRP key to compare to.
  114. * @return <code>true</code> if the designated object is of the same type
  115. * and value as this one.
  116. */
  117. public boolean equals(Object obj)
  118. {
  119. if (obj == null)
  120. return false;
  121. if (! (obj instanceof SRPKey))
  122. return false;
  123. SRPKey that = (SRPKey) obj;
  124. return N.equals(that.getN()) && g.equals(that.getG());
  125. }
  126. public abstract byte[] getEncoded(int format);
  127. }