PKCS7.java 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /* PKCS7.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 PKCS7 padding algorithm.
  38. * <p>
  39. * This algorithm is described for 8-byte blocks in [RFC-1423] and extended to
  40. * block sizes of up to 256 bytes in [PKCS-7].
  41. * <p>
  42. * References:
  43. * <ol>
  44. * <li><a href="http://www.ietf.org/rfc/rfc1423.txt">RFC-1423</a>: Privacy
  45. * Enhancement for Internet Electronic Mail: Part III: Algorithms, Modes, and
  46. * Identifiers.</li>
  47. * <li><a href="http://www.ietf.org/">IETF</a>.</li>
  48. * <li><a href="http://www.rsasecurity.com/rsalabs/pkcs/pkcs-7/">[PKCS-7]</a>
  49. * PKCS #7: Cryptographic Message Syntax Standard - An RSA Laboratories
  50. * Technical Note.</li>
  51. * <li><a href="http://www.rsasecurity.com/">RSA Security</a>.</li>
  52. * </ol>
  53. */
  54. public final class PKCS7
  55. extends BasePad
  56. {
  57. private static final Logger log = Configuration.DEBUG ?
  58. Logger.getLogger(PKCS7.class.getName()) : null;
  59. /**
  60. * Trivial package-private constructor for use by the <i>Factory</i> class.
  61. *
  62. * @see PadFactory
  63. */
  64. PKCS7()
  65. {
  66. super(Registry.PKCS7_PAD);
  67. }
  68. public void setup()
  69. {
  70. if (blockSize < 2 || blockSize > 256)
  71. throw new IllegalArgumentException();
  72. }
  73. public byte[] pad(byte[] in, int offset, int length)
  74. {
  75. int padLength = blockSize;
  76. if (length % blockSize != 0)
  77. padLength = blockSize - length % blockSize;
  78. byte[] result = new byte[padLength];
  79. for (int i = 0; i < padLength;)
  80. result[i++] = (byte) padLength;
  81. if (Configuration.DEBUG)
  82. log.fine("padding: 0x" + Util.toString(result));
  83. return result;
  84. }
  85. public int unpad(byte[] in, int offset, int length)
  86. throws WrongPaddingException
  87. {
  88. int limit = offset + length;
  89. int result = in[--limit] & 0xFF;
  90. for (int i = 0; i < result - 1; i++)
  91. if (result != (in[--limit] & 0xFF))
  92. throw new WrongPaddingException();
  93. if (Configuration.DEBUG)
  94. log.fine("padding length: " + result);
  95. return result;
  96. }
  97. }