SSLHMac.java 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /* SSLHMac.java -- SSLv3's MAC algorithm.
  2. Copyright (C) 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.net.ssl.provider;
  32. import java.util.Arrays;
  33. import java.util.Map;
  34. import gnu.java.security.hash.HashFactory;
  35. import gnu.java.security.hash.IMessageDigest;
  36. import gnu.javax.crypto.mac.IMac;
  37. /**
  38. * The MAC function in SSLv3. This mac is defined as:
  39. *
  40. * <pre>
  41. * hash(MAC_write_secret, pad_2 +
  42. * hash(MAC_write_secret + pad_1 + data));</pre>
  43. *
  44. * <p><tt>hash</tt> is e.g. MD5 or SHA-1, <tt>pad_1</tt> is the value
  45. * 0x36 48 times for MD5 and 40 times for SHA-1, and <tt>pad_2</tt> is
  46. * the value 0x5c repeated similarly.
  47. */
  48. class SSLHMac implements IMac, Cloneable
  49. {
  50. // Fields.
  51. // -------------------------------------------------------------------------
  52. static final byte PAD1 = 0x36;
  53. static final byte PAD2 = 0x5c;
  54. protected IMessageDigest md;
  55. protected byte[] key;
  56. protected final byte[] pad1, pad2;
  57. // Constructors.
  58. // -------------------------------------------------------------------------
  59. SSLHMac(String mdName)
  60. {
  61. super();
  62. this.md = HashFactory.getInstance(mdName);
  63. if (mdName.equalsIgnoreCase("MD5"))
  64. {
  65. pad1 = new byte[48];
  66. pad2 = new byte[48];
  67. }
  68. else
  69. {
  70. pad1 = new byte[40];
  71. pad2 = new byte[40];
  72. }
  73. Arrays.fill(pad1, PAD1);
  74. Arrays.fill(pad2, PAD2);
  75. }
  76. // Instance methods.
  77. // -------------------------------------------------------------------------
  78. public Object clone()
  79. {
  80. try
  81. {
  82. return super.clone();
  83. }
  84. catch (CloneNotSupportedException cnse)
  85. {
  86. throw new Error();
  87. }
  88. }
  89. public String name()
  90. {
  91. return "SSLHMac-" + md.name();
  92. }
  93. public int macSize()
  94. {
  95. return md.hashSize();
  96. }
  97. public void init(Map attributes)
  98. {
  99. key = (byte[]) attributes.get(MAC_KEY_MATERIAL);
  100. if (key == null)
  101. throw new NullPointerException();
  102. reset();
  103. }
  104. public void reset()
  105. {
  106. md.reset();
  107. md.update(key, 0, key.length);
  108. md.update(pad1, 0, pad1.length);
  109. }
  110. public byte[] digest()
  111. {
  112. byte[] h1 = md.digest();
  113. md.update(key, 0, key.length);
  114. md.update(pad2, 0, pad2.length);
  115. md.update(h1, 0, h1.length);
  116. byte[] result = md.digest();
  117. reset();
  118. return result;
  119. }
  120. public void update(byte b)
  121. {
  122. md.update(b);
  123. }
  124. public void update(byte[] buf, int off, int len)
  125. {
  126. md.update(buf, off, len);
  127. }
  128. public boolean selfTest()
  129. {
  130. return true; // XXX
  131. }
  132. }