TBC.java 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /* TBC.java --
  2. Copyright (C) 2001, 2002, 2006, 2010 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.pad;
  32. import gnu.java.security.Configuration;
  33. import gnu.java.security.Registry;
  34. import gnu.java.security.util.Util;
  35. import java.util.logging.Logger;
  36. /**
  37. * The implementation of the Trailing Bit Complement (TBC) padding algorithm.
  38. * <p>
  39. * In this mode, "...the data string is padded at the trailing end with the
  40. * complement of the trailing bit of the unpadded message: if the trailing bit
  41. * is <tt>1</tt>, then <tt>0</tt> bits are appended, and if the trailing
  42. * bit is <tt>0</tt>, then <tt>1</tt> bits are appended. As few bits are
  43. * added as are necessary to meet the formatting size requirement."
  44. * <p>
  45. * References:
  46. * <ol>
  47. * <li><a
  48. * href="http://csrc.nist.gov/encryption/modes/Recommendation/Modes01.pdf">
  49. * Recommendation for Block Cipher Modes of Operation Methods and
  50. * Techniques</a>, Morris Dworkin.</li>
  51. * </ol>
  52. */
  53. public final class TBC
  54. extends BasePad
  55. {
  56. private static final Logger log = Configuration.DEBUG ?
  57. Logger.getLogger(TBC.class.getName()) : null;
  58. /**
  59. * Trivial package-private constructor for use by the <i>Factory</i> class.
  60. *
  61. * @see PadFactory
  62. */
  63. TBC()
  64. {
  65. super(Registry.TBC_PAD);
  66. }
  67. public void setup()
  68. {
  69. if (blockSize < 1 || blockSize > 256)
  70. throw new IllegalArgumentException();
  71. }
  72. public byte[] pad(byte[] in, int offset, int length)
  73. {
  74. int padLength = blockSize;
  75. if (length % blockSize != 0)
  76. padLength = blockSize - length % blockSize;
  77. byte[] result = new byte[padLength];
  78. int lastBit = in[offset + length - 1] & 0x01;
  79. if (lastBit == 0)
  80. for (int i = 0; i < padLength;)
  81. result[i++] = 0x01;
  82. // else it's already set to zeroes by virtue of initialisation
  83. if (Configuration.DEBUG)
  84. log.fine("padding: 0x" + Util.toString(result));
  85. return result;
  86. }
  87. public int unpad(byte[] in, int offset, int length)
  88. throws WrongPaddingException
  89. {
  90. int limit = offset + length - 1;
  91. int lastBit = in[limit] & 0xFF;
  92. int result = 0;
  93. while (lastBit == (in[limit] & 0xFF))
  94. {
  95. result++;
  96. limit--;
  97. }
  98. if (result > length)
  99. throw new WrongPaddingException();
  100. if (Configuration.DEBUG)
  101. log.fine("padding length: " + result);
  102. return result;
  103. }
  104. }