PKCS1_V1_5.java 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /* PKCS1_V1_5.java --
  2. Copyright (C) 2003, 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.sig.rsa.EME_PKCS1_V1_5;
  35. import gnu.java.security.util.PRNG;
  36. import gnu.java.security.util.Util;
  37. import java.util.logging.Level;
  38. import java.util.logging.Logger;
  39. /**
  40. * A padding algorithm implementation of the EME-PKCS1-V1.5 encoding/decoding
  41. * algorithm as described in section 7.2 of RFC-3447. This is effectively an
  42. * <i>Adapter</i> over an instance of {@link EME_PKCS1_V1_5} initialised with
  43. * the RSA public shared modulus length (in bytes).
  44. * <p>
  45. * References:
  46. * <ol>
  47. * <li><a href="http://www.ietf.org/rfc/rfc3447.txt">Public-Key Cryptography
  48. * Standards (PKCS) #1:</a><br>
  49. * RSA Cryptography Specifications Version 2.1.<br>
  50. * Jakob Jonsson and Burt Kaliski.</li>
  51. * </ol>
  52. *
  53. * @see EME_PKCS1_V1_5
  54. */
  55. public class PKCS1_V1_5
  56. extends BasePad
  57. {
  58. private static final Logger log = Configuration.DEBUG ?
  59. Logger.getLogger(PKCS1_V1_5.class.getName()) : null;
  60. private EME_PKCS1_V1_5 codec;
  61. /**
  62. * Trivial package-private constructor for use by the <i>Factory</i> class.
  63. *
  64. * @see PadFactory
  65. */
  66. PKCS1_V1_5()
  67. {
  68. super(Registry.EME_PKCS1_V1_5_PAD);
  69. }
  70. public void setup()
  71. {
  72. codec = EME_PKCS1_V1_5.getInstance(blockSize);
  73. }
  74. public byte[] pad(final byte[] in, final int offset, final int length)
  75. {
  76. final byte[] M = new byte[length];
  77. System.arraycopy(in, offset, M, 0, length);
  78. final byte[] EM = codec.encode(M);
  79. final byte[] result = new byte[blockSize - length];
  80. System.arraycopy(EM, 0, result, 0, result.length);
  81. if (Configuration.DEBUG)
  82. log.fine("padding: 0x" + Util.toString(result));
  83. return result;
  84. }
  85. public int unpad(final byte[] in, final int offset, final int length)
  86. throws WrongPaddingException
  87. {
  88. final byte[] EM = new byte[length];
  89. System.arraycopy(in, offset, EM, 0, length);
  90. final int result = length - codec.decode(EM).length;
  91. if (Configuration.DEBUG)
  92. log.fine("padding length: " + String.valueOf(result));
  93. return result;
  94. }
  95. public boolean selfTest()
  96. {
  97. final int[] mLen = new int[] { 16, 20, 32, 48, 64 };
  98. final byte[] M = new byte[mLen[mLen.length - 1]];
  99. PRNG.getInstance().nextBytes(M);
  100. final byte[] EM = new byte[1024];
  101. byte[] p;
  102. int bs, i, j;
  103. for (bs = 256; bs < 1025; bs += 256)
  104. {
  105. init(bs);
  106. for (i = 0; i < mLen.length; i++)
  107. {
  108. j = mLen[i];
  109. p = pad(M, 0, j);
  110. if (j + p.length != blockSize)
  111. {
  112. if (Configuration.DEBUG)
  113. log.log(Level.SEVERE,
  114. "Length of padded text MUST be a multiple of "
  115. + blockSize, new RuntimeException(name()));
  116. return false;
  117. }
  118. System.arraycopy(p, 0, EM, 0, p.length);
  119. System.arraycopy(M, 0, EM, p.length, j);
  120. try
  121. {
  122. if (p.length != unpad(EM, 0, blockSize))
  123. {
  124. if (Configuration.DEBUG)
  125. log.log(Level.SEVERE, "Failed symmetric operation",
  126. new RuntimeException(name()));
  127. return false;
  128. }
  129. }
  130. catch (WrongPaddingException x)
  131. {
  132. if (Configuration.DEBUG)
  133. log.throwing(this.getClass().getName(), "selfTest", x);
  134. return false;
  135. }
  136. }
  137. reset();
  138. }
  139. return true;
  140. }
  141. }