View | Details | Return to bug 26067 | Differences between
and this patch

Collapse All | Expand All

(-)BasePad.java (-27 / +10 lines)
Lines 46-52 Link Here
46
/**
46
/**
47
 * An abstract class to facilitate implementing padding algorithms.
47
 * An abstract class to facilitate implementing padding algorithms.
48
 */
48
 */
49
public abstract class BasePad implements IPad
49
public abstract class BasePad
50
    implements IPad
50
{
51
{
51
  private static final Logger log = Logger.getLogger(BasePad.class.getName());
52
  private static final Logger log = Logger.getLogger(BasePad.class.getName());
52
  /** The canonical name prefix of the padding algorithm. */
53
  /** The canonical name prefix of the padding algorithm. */
Lines 54-62 Link Here
54
  /** The block size, in bytes, for this instance. */
55
  /** The block size, in bytes, for this instance. */
55
  protected int blockSize;
56
  protected int blockSize;
56
57
57
  // Constructor(s)
58
  // -------------------------------------------------------------------------
59
60
  /** Trivial constructor for use by concrete subclasses. */
58
  /** Trivial constructor for use by concrete subclasses. */
61
  protected BasePad(final String name)
59
  protected BasePad(final String name)
62
  {
60
  {
Lines 66-95 Link Here
66
    blockSize = -1;
64
    blockSize = -1;
67
  }
65
  }
68
66
69
  // Class methods
70
  // -------------------------------------------------------------------------
71
72
  // Instance methods
73
  // -------------------------------------------------------------------------
74
75
  // IPad interface implementation -------------------------------------------
76
77
  public String name()
67
  public String name()
78
  {
68
  {
79
    final StringBuffer sb = new StringBuffer(name);
69
    final StringBuffer sb = new StringBuffer(name);
80
    if (blockSize != -1)
70
    if (blockSize != -1)
81
      {
71
      sb.append('-').append(String.valueOf(8 * blockSize));
82
        sb.append('-').append(String.valueOf(8 * blockSize));
83
      }
84
    return sb.toString();
72
    return sb.toString();
85
  }
73
  }
86
74
87
  public void init(final int bs) throws IllegalStateException
75
  public void init(final int bs) throws IllegalStateException
88
  {
76
  {
89
    if (blockSize != -1)
77
    if (blockSize != -1)
90
      {
78
      throw new IllegalStateException();
91
        throw new IllegalStateException();
92
      }
93
    blockSize = bs;
79
    blockSize = bs;
94
    setup();
80
    setup();
95
  }
81
  }
Lines 133-139 Link Here
133
    byte[] padBytes;
119
    byte[] padBytes;
134
    final int offset = 5;
120
    final int offset = 5;
135
    final int limit = buffer.length;
121
    final int limit = buffer.length;
136
137
    this.init(size);
122
    this.init(size);
138
    for (int i = 0; i < limit - offset - blockSize; i++)
123
    for (int i = 0; i < limit - offset - blockSize; i++)
139
      {
124
      {
Lines 142-149 Link Here
142
          {
127
          {
143
            if (Configuration.DEBUG)
128
            if (Configuration.DEBUG)
144
              log.log(Level.SEVERE,
129
              log.log(Level.SEVERE,
145
                      "Length of padded text MUST be a multiple of " + blockSize,
130
                      "Length of padded text MUST be a multiple of "
146
                      new RuntimeException(name()));
131
                      + blockSize, new RuntimeException(name()));
147
            return false;
132
            return false;
148
          }
133
          }
149
        System.arraycopy(padBytes, 0, buffer, offset + i, padBytes.length);
134
        System.arraycopy(padBytes, 0, buffer, offset + i, padBytes.length);
Lines 169-180 Link Here
169
    return true;
154
    return true;
170
  }
155
  }
171
156
172
  // abstract methods to implement by subclasses -----------------------------
173
174
  /**
157
  /**
175
   * <p>If any additional checks or resource setup must be done by the
158
   * If any additional checks or resource setup must be done by the subclass,
176
   * subclass, then this is the hook for it. This method will be called before
159
   * then this is the hook for it. This method will be called before the
177
   * the {@link #init(int)} method returns.</p>
160
   * {@link #init(int)} method returns.
178
   */
161
   */
179
  public abstract void setup();
162
  public abstract void setup();
180
163
Lines 182-185 Link Here
182
165
183
  public abstract int unpad(byte[] in, int off, int len)
166
  public abstract int unpad(byte[] in, int off, int len)
184
      throws WrongPaddingException;
167
      throws WrongPaddingException;
185
}
168
}
(-)IPad.java (-24 / +19 lines)
Lines 39-66 Link Here
39
package gnu.javax.crypto.pad;
39
package gnu.javax.crypto.pad;
40
40
41
/**
41
/**
42
 * <p>The basic visible methods of any padding algorithm.</p>
42
 * The basic visible methods of any padding algorithm.
43
 *
43
 * <p>
44
 * <p>Padding algorithms serve to <i>pad</i> and <i>unpad</i> byte arrays usually
44
 * Padding algorithms serve to <i>pad</i> and <i>unpad</i> byte arrays usually
45
 * as the last step in an <i>encryption</i> or respectively a <i>decryption</i>
45
 * as the last step in an <i>encryption</i> or respectively a <i>decryption</i>
46
 * operation. Their input buffers are usually those processed by instances of
46
 * operation. Their input buffers are usually those processed by instances of
47
 * {@link gnu.javax.crypto.mode.IMode} and/or {@link gnu.javax.crypto.cipher.IBlockCipher}.</p>
47
 * {@link gnu.javax.crypto.mode.IMode} and/or
48
 * {@link gnu.javax.crypto.cipher.IBlockCipher}.
48
 */
49
 */
49
public interface IPad
50
public interface IPad
50
{
51
{
51
52
  // Constants
53
  // -------------------------------------------------------------------------
54
55
  // Methods
56
  // -------------------------------------------------------------------------
57
58
  /** @return the canonical name of this instance. */
52
  /** @return the canonical name of this instance. */
59
  String name();
53
  String name();
60
54
61
  /**
55
  /**
62
   * Initialises the padding scheme with a designated block size.
56
   * Initialises the padding scheme with a designated block size.
63
   *
57
   * 
64
   * @param bs the designated block size.
58
   * @param bs the designated block size.
65
   * @exception IllegalStateException if the instance is already initialised.
59
   * @exception IllegalStateException if the instance is already initialised.
66
   * @exception IllegalArgumentException if the block size value is invalid.
60
   * @exception IllegalArgumentException if the block size value is invalid.
Lines 69-96 Link Here
69
63
70
  /**
64
  /**
71
   * Returns the byte sequence that should be appended to the designated input.
65
   * Returns the byte sequence that should be appended to the designated input.
72
   *
66
   * 
73
   * @param in the input buffer containing the bytes to pad.
67
   * @param in the input buffer containing the bytes to pad.
74
   * @param offset the starting index of meaningful data in <i>in</i>.
68
   * @param offset the starting index of meaningful data in <i>in</i>.
75
   * @param length the number of meaningful bytes in <i>in</i>.
69
   * @param length the number of meaningful bytes in <i>in</i>.
76
   * @return the possibly 0-byte long sequence to be appended to the designated
70
   * @return the possibly 0-byte long sequence to be appended to the designated
77
   * input.
71
   *         input.
78
   */
72
   */
79
  byte[] pad(byte[] in, int offset, int length);
73
  byte[] pad(byte[] in, int offset, int length);
80
74
81
  /**
75
  /**
82
   * Returns the number of bytes to discard from a designated input buffer.
76
   * Returns the number of bytes to discard from a designated input buffer.
83
   *
77
   * 
84
   * @param in the input buffer containing the bytes to unpad.
78
   * @param in the input buffer containing the bytes to unpad.
85
   * @param offset the starting index of meaningful data in <i>in</i>.
79
   * @param offset the starting index of meaningful data in <i>in</i>.
86
   * @param length the number of meaningful bytes in <i>in</i>.
80
   * @param length the number of meaningful bytes in <i>in</i>.
87
   * @return the number of bytes to discard, to the left of index position
81
   * @return the number of bytes to discard, to the left of index position
88
   * <tt>offset + length</tt> in <i>in</i>. In other words, if the return
82
   *         <code>offset + length</code> in <i>in</i>. In other words, if
89
   * value of a successful invocation of this method is <tt>result</tt>, then
83
   *         the return value of a successful invocation of this method is
90
   * the unpadded byte sequence will be <tt>offset + length - result</tt> bytes
84
   *         <code>result</code>, then the unpadded byte sequence will be
91
   * in <i>in</i>, starting from index position <tt>offset</tt>.
85
   *         <code>offset + length - result</code> bytes in <i>in</i>,
86
   *         starting from index position <code>offset</code>.
92
   * @exception WrongPaddingException if the data is not terminated with the
87
   * @exception WrongPaddingException if the data is not terminated with the
93
   * expected padding bytes.
88
   *              expected padding bytes.
94
   */
89
   */
95
  int unpad(byte[] in, int offset, int length) throws WrongPaddingException;
90
  int unpad(byte[] in, int offset, int length) throws WrongPaddingException;
96
91
Lines 102-110 Link Here
102
97
103
  /**
98
  /**
104
   * A basic symmetric pad/unpad test.
99
   * A basic symmetric pad/unpad test.
105
   *
100
   * 
106
   * @return <tt>true</tt> if the implementation passes a basic symmetric
101
   * @return <code>true</code> if the implementation passes a basic symmetric
107
   * self-test. Returns <tt>false</tt> otherwise.
102
   *         self-test. Returns <code>false</code> otherwise.
108
   */
103
   */
109
  boolean selfTest();
104
  boolean selfTest();
110
}
105
}
(-)PKCS1_V1_5.java (-30 / +16 lines)
Lines 48-79 Link Here
48
import java.util.logging.Logger;
48
import java.util.logging.Logger;
49
49
50
/**
50
/**
51
 * <p>A padding algorithm implementation of the EME-PKCS1-V1.5 encoding/decoding
51
 * A padding algorithm implementation of the EME-PKCS1-V1.5 encoding/decoding
52
 * algorithm as described in section 7.2 of RFC-3447. This is effectively an
52
 * algorithm as described in section 7.2 of RFC-3447. This is effectively an
53
 * <i>Adapter</i> over an instance of {@link EME_PKCS1_V1_5} initialised with
53
 * <i>Adapter</i> over an instance of {@link EME_PKCS1_V1_5} initialised with
54
 * the RSA public shared modulus length (in bytes).</p>
54
 * the RSA public shared modulus length (in bytes).
55
 *
55
 * <p>
56
 * <p>References:</p>
56
 * References:
57
 * <ol>
57
 * <ol>
58
 *    <li><a href="http://www.ietf.org/rfc/rfc3447.txt">Public-Key Cryptography
58
 * <li><a href="http://www.ietf.org/rfc/rfc3447.txt">Public-Key Cryptography
59
 *    Standards (PKCS) #1:</a><br>
59
 * Standards (PKCS) #1:</a><br>
60
 *    RSA Cryptography Specifications Version 2.1.<br>
60
 * RSA Cryptography Specifications Version 2.1.<br>
61
 *    Jakob Jonsson and Burt Kaliski.</li>
61
 * Jakob Jonsson and Burt Kaliski.</li>
62
 * </ol>
62
 * </ol>
63
 *
63
 * 
64
 * @see EME_PKCS1_V1_5
64
 * @see EME_PKCS1_V1_5
65
 */
65
 */
66
public class PKCS1_V1_5 extends BasePad
66
public class PKCS1_V1_5
67
    extends BasePad
67
{
68
{
68
  private static final Logger log = Logger.getLogger(PKCS1_V1_5.class.getName());
69
  private static final Logger log = Logger.getLogger(PKCS1_V1_5.class.getName());
69
  private EME_PKCS1_V1_5 codec;
70
  private EME_PKCS1_V1_5 codec;
70
71
71
  // Constructor(s)
72
  // -------------------------------------------------------------------------
73
74
  /**
72
  /**
75
   * <p>Trivial package-private constructor for use by the <i>Factory</i> class.
73
   * Trivial package-private constructor for use by the <i>Factory</i> class.
76
   * </p>
77
   *
74
   *
78
   * @see PadFactory
75
   * @see PadFactory
79
   */
76
   */
Lines 82-93 Link Here
82
    super(Registry.EME_PKCS1_V1_5_PAD);
79
    super(Registry.EME_PKCS1_V1_5_PAD);
83
  }
80
  }
84
81
85
  // Class methods
86
  // -------------------------------------------------------------------------
87
88
  // Implementation of abstract methods in BasePad
89
  // -------------------------------------------------------------------------
90
91
  public void setup()
82
  public void setup()
92
  {
83
  {
93
    codec = EME_PKCS1_V1_5.getInstance(blockSize);
84
    codec = EME_PKCS1_V1_5.getInstance(blockSize);
Lines 116-123 Link Here
116
    return result;
107
    return result;
117
  }
108
  }
118
109
119
  // overloaded methods ------------------------------------------------------
120
121
  public boolean selfTest()
110
  public boolean selfTest()
122
  {
111
  {
123
    final int[] mLen = new int[] { 16, 20, 32, 48, 64 };
112
    final int[] mLen = new int[] { 16, 20, 32, 48, 64 };
Lines 137-147 Link Here
137
              {
126
              {
138
                if (Configuration.DEBUG)
127
                if (Configuration.DEBUG)
139
                  log.log(Level.SEVERE,
128
                  log.log(Level.SEVERE,
140
                          "Length of padded text MUST be a multiple of " + blockSize,
129
                          "Length of padded text MUST be a multiple of "
141
                          new RuntimeException(name()));
130
                          + blockSize, new RuntimeException(name()));
142
                return false;
131
                return false;
143
              }
132
              }
144
145
            System.arraycopy(p, 0, EM, 0, p.length);
133
            System.arraycopy(p, 0, EM, 0, p.length);
146
            System.arraycopy(M, 0, EM, p.length, j);
134
            System.arraycopy(M, 0, EM, p.length, j);
147
            try
135
            try
Lines 149-156 Link Here
149
                if (p.length != unpad(EM, 0, blockSize))
137
                if (p.length != unpad(EM, 0, blockSize))
150
                  {
138
                  {
151
                    if (Configuration.DEBUG)
139
                    if (Configuration.DEBUG)
152
                      log.log(Level.SEVERE,
140
                      log.log(Level.SEVERE, "Failed symmetric operation",
153
                              "Failed symmetric operation",
154
                              new RuntimeException(name()));
141
                              new RuntimeException(name()));
155
                    return false;
142
                    return false;
156
                  }
143
                  }
Lines 164-170 Link Here
164
          }
151
          }
165
        reset();
152
        reset();
166
      }
153
      }
167
168
    return true;
154
    return true;
169
  }
155
  }
170
}
156
}
(-)PKCS7.java (-70 / +58 lines)
Lines 1-39 Link Here
1
/* PKCS7.java -- 
1
/* PKCS7.java -- 
2
   Copyright (C) 2001, 2002, 2006 Free Software Foundation, Inc.
2
 Copyright (C) 2001, 2002, 2006 Free Software Foundation, Inc.
3
3
4
This file is a part of GNU Classpath.
4
 This file is a part of GNU Classpath.
5
5
6
GNU Classpath is free software; you can redistribute it and/or modify
6
 GNU Classpath is free software; you can redistribute it and/or modify
7
it under the terms of the GNU General Public License as published by
7
 it under the terms of the GNU General Public License as published by
8
the Free Software Foundation; either version 2 of the License, or (at
8
 the Free Software Foundation; either version 2 of the License, or (at
9
your option) any later version.
9
 your option) any later version.
10
10
11
GNU Classpath is distributed in the hope that it will be useful, but
11
 GNU Classpath is distributed in the hope that it will be useful, but
12
WITHOUT ANY WARRANTY; without even the implied warranty of
12
 WITHOUT ANY WARRANTY; without even the implied warranty of
13
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
General Public License for more details.
14
 General Public License for more details.
15
15
16
You should have received a copy of the GNU General Public License
16
 You should have received a copy of the GNU General Public License
17
along with GNU Classpath; if not, write to the Free Software
17
 along with GNU Classpath; if not, write to the Free Software
18
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
18
 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
19
USA
19
 USA
20
20
21
Linking this library statically or dynamically with other modules is
21
 Linking this library statically or dynamically with other modules is
22
making a combined work based on this library.  Thus, the terms and
22
 making a combined work based on this library.  Thus, the terms and
23
conditions of the GNU General Public License cover the whole
23
 conditions of the GNU General Public License cover the whole
24
combination.
24
 combination.
25
25
26
As a special exception, the copyright holders of this library give you
26
 As a special exception, the copyright holders of this library give you
27
permission to link this library with independent modules to produce an
27
 permission to link this library with independent modules to produce an
28
executable, regardless of the license terms of these independent
28
 executable, regardless of the license terms of these independent
29
modules, and to copy and distribute the resulting executable under
29
 modules, and to copy and distribute the resulting executable under
30
terms of your choice, provided that you also meet, for each linked
30
 terms of your choice, provided that you also meet, for each linked
31
independent module, the terms and conditions of the license of that
31
 independent module, the terms and conditions of the license of that
32
module.  An independent module is a module which is not derived from
32
 module.  An independent module is a module which is not derived from
33
or based on this library.  If you modify this library, you may extend
33
 or based on this library.  If you modify this library, you may extend
34
this exception to your version of the library, but you are not
34
 this exception to your version of the library, but you are not
35
obligated to do so.  If you do not wish to do so, delete this
35
 obligated to do so.  If you do not wish to do so, delete this
36
exception statement from your version.  */
36
 exception statement from your version.  */
37
37
38
38
39
package gnu.javax.crypto.pad;
39
package gnu.javax.crypto.pad;
Lines 45-71 Link Here
45
import java.util.logging.Logger;
45
import java.util.logging.Logger;
46
46
47
/**
47
/**
48
 * <p>The implementation of the PKCS7 padding algorithm.</p>
48
 * The implementation of the PKCS7 padding algorithm.
49
 *
49
 * <p>
50
 * <p>This algorithm is described for 8-byte blocks in [RFC-1423] and extended to
50
 * This algorithm is described for 8-byte blocks in [RFC-1423] and extended to
51
 * block sizes of up to 256 bytes in [PKCS-7].</p>
51
 * block sizes of up to 256 bytes in [PKCS-7].
52
 *
52
 * <p>
53
 * References:<br>
53
 * References:
54
 * <a href="http://www.ietf.org/rfc/rfc1423.txt">RFC-1423</a>: Privacy
54
 * <ol>
55
 * <li><a href="http://www.ietf.org/rfc/rfc1423.txt">RFC-1423</a>: Privacy
55
 * Enhancement for Internet Electronic Mail: Part III: Algorithms, Modes, and
56
 * Enhancement for Internet Electronic Mail: Part III: Algorithms, Modes, and
56
 * Identifiers.<br>
57
 * Identifiers.</li>
57
 * <a href="http://www.ietf.org/">IETF</a>.
58
 * <li><a href="http://www.ietf.org/">IETF</a>.</li>
58
 * <a href="http://www.rsasecurity.com/rsalabs/pkcs/pkcs-7/">[PKCS-7]</a>PKCS #7:
59
 * <li><a href="http://www.rsasecurity.com/rsalabs/pkcs/pkcs-7/">[PKCS-7]</a>
59
 * Cryptographic Message Syntax Standard - An RSA Laboratories Technical Note.<br>
60
 * PKCS #7: Cryptographic Message Syntax Standard - An RSA Laboratories
60
 * <a href="http://www.rsasecurity.com/">RSA Security</a>.<p>
61
 * Technical Note.</li>
62
 * <li><a href="http://www.rsasecurity.com/">RSA Security</a>.</li>
63
 * </ol>
61
 */
64
 */
62
public final class PKCS7 extends BasePad
65
public final class PKCS7
66
    extends BasePad
63
{
67
{
64
  private static final Logger log = Logger.getLogger(PKCS7.class.getName());
68
  private static final Logger log = Logger.getLogger(PKCS7.class.getName());
65
69
66
  /**
70
  /**
67
   * Trivial package-private constructor for use by the <i>Factory</i> class.<p>
71
   * Trivial package-private constructor for use by the <i>Factory</i> class.
68
   *
72
   * 
69
   * @see PadFactory
73
   * @see PadFactory
70
   */
74
   */
71
  PKCS7()
75
  PKCS7()
Lines 73-104 Link Here
73
    super(Registry.PKCS7_PAD);
77
    super(Registry.PKCS7_PAD);
74
  }
78
  }
75
79
76
  // Class methods
77
  // -------------------------------------------------------------------------
78
79
  // Implementation of abstract methods in BasePad
80
  // -------------------------------------------------------------------------
81
82
  public void setup()
80
  public void setup()
83
  {
81
  {
84
    if (blockSize < 2 || blockSize > 256)
82
    if (blockSize < 2 || blockSize > 256)
85
      {
83
      throw new IllegalArgumentException();
86
        throw new IllegalArgumentException();
87
      }
88
  }
84
  }
89
85
90
  public byte[] pad(byte[] in, int offset, int length)
86
  public byte[] pad(byte[] in, int offset, int length)
91
  {
87
  {
92
    int padLength = blockSize;
88
    int padLength = blockSize;
93
    if (length % blockSize != 0)
89
    if (length % blockSize != 0)
94
      {
90
      padLength = blockSize - length % blockSize;
95
        padLength = blockSize - length % blockSize;
96
      }
97
    byte[] result = new byte[padLength];
91
    byte[] result = new byte[padLength];
98
    for (int i = 0; i < padLength;)
92
    for (int i = 0; i < padLength;)
99
      {
93
      result[i++] = (byte) padLength;
100
        result[i++] = (byte) padLength;
101
      }
102
    if (Configuration.DEBUG)
94
    if (Configuration.DEBUG)
103
      log.fine("padding: 0x" + Util.toString(result));
95
      log.fine("padding: 0x" + Util.toString(result));
104
    return result;
96
    return result;
Lines 110-123 Link Here
110
    int limit = offset + length;
102
    int limit = offset + length;
111
    int result = in[limit - 1] & 0xFF;
103
    int result = in[limit - 1] & 0xFF;
112
    for (int i = 0; i < result; i++)
104
    for (int i = 0; i < result; i++)
113
      {
105
      if (result != (in[--limit] & 0xFF))
114
        if (result != (in[--limit] & 0xFF))
106
        throw new WrongPaddingException();
115
          {
116
            throw new WrongPaddingException();
117
          }
118
      }
119
    if (Configuration.DEBUG)
107
    if (Configuration.DEBUG)
120
      log.fine("padding length: " + result);
108
      log.fine("padding length: " + result);
121
    return result;
109
    return result;
122
  }
110
  }
123
}
111
}
(-)PadFactory.java (-37 / +19 lines)
Lines 45-53 Link Here
45
import java.util.Set;
45
import java.util.Set;
46
46
47
/**
47
/**
48
 * <p>A Factory to instantiate padding schemes.</p>
48
 * A Factory to instantiate padding schemes.
49
 */
49
 */
50
public class PadFactory implements Registry
50
public class PadFactory
51
    implements Registry
51
{
52
{
52
  /** Collection of padding algorithm names --cached for speed. */
53
  /** Collection of padding algorithm names --cached for speed. */
53
  private static Set names;
54
  private static Set names;
Lines 58-122 Link Here
58
    super();
59
    super();
59
  }
60
  }
60
61
61
  // Class methods
62
  // -------------------------------------------------------------------------
63
64
  /**
62
  /**
65
   * <p>Returns an instance of a padding algorithm given its name.</p>
63
   * Returns an instance of a padding algorithm given its name.
66
   *
64
   * 
67
   * @param pad the case-insensitive name of the padding algorithm.
65
   * @param pad the case-insensitive name of the padding algorithm.
68
   * @return an instance of the padding algorithm, operating with a given
66
   * @return an instance of the padding algorithm, operating with a given block
69
   * block size, or <code>null</code> if none found.
67
   *         size, or <code>null</code> if none found.
70
   * @throws InternalError if the implementation does not pass its self-test.
68
   * @throws InternalError if the implementation does not pass its self-test.
71
   */
69
   */
72
  public static final IPad getInstance(String pad)
70
  public static final IPad getInstance(String pad)
73
  {
71
  {
74
    if (pad == null)
72
    if (pad == null)
75
      {
73
      return null;
76
        return null;
77
      }
78
74
79
    pad = pad.trim().toLowerCase();
75
    pad = pad.trim().toLowerCase();
80
    if (pad.endsWith("padding"))
76
    if (pad.endsWith("padding"))
81
      pad = pad.substring(0, pad.length() - "padding".length());
77
      pad = pad.substring(0, pad.length() - "padding".length());
82
    IPad result = null;
78
    IPad result = null;
83
    if (pad.equals(PKCS7_PAD) || pad.equals(PKCS5_PAD))
79
    if (pad.equals(PKCS7_PAD) || pad.equals(PKCS5_PAD))
84
      {
80
      result = new PKCS7();
85
        result = new PKCS7();
86
      }
87
    else if (pad.equals(TBC_PAD))
81
    else if (pad.equals(TBC_PAD))
88
      {
82
      result = new TBC();
89
        result = new TBC();
90
      }
91
    else if (pad.equals(EME_PKCS1_V1_5_PAD))
83
    else if (pad.equals(EME_PKCS1_V1_5_PAD))
92
      {
84
      result = new PKCS1_V1_5();
93
        result = new PKCS1_V1_5();
94
      }
95
    else if (pad.equals(SSL3_PAD))
85
    else if (pad.equals(SSL3_PAD))
96
      {
86
      result = new SSL3();
97
        result = new SSL3();
98
      }
99
    else if (pad.equals(TLS1_PAD))
87
    else if (pad.equals(TLS1_PAD))
100
      {
88
      result = new TLS1();
101
        result = new TLS1();
102
      }
103
    else if (pad.equals(ISO10126_PAD))
89
    else if (pad.equals(ISO10126_PAD))
104
      {
90
      result = new ISO10126();
105
        result = new ISO10126();
106
      }
107
91
108
    if (result != null && !result.selfTest())
92
    if (result != null && ! result.selfTest())
109
      {
93
      throw new InternalError(result.name());
110
        throw new InternalError(result.name());
111
      }
112
94
113
    return result;
95
    return result;
114
  }
96
  }
115
97
116
  /**
98
  /**
117
   * Returns a {@link java.util.Set} of names of padding algorithms
99
   * Returns a {@link Set} of names of padding algorithms supported by this
118
   * supported by this <i>Factory</i>.
100
   * <i>Factory</i>.
119
   *
101
   * 
120
   * @return a {@link Set} of padding algorithm names (Strings).
102
   * @return a {@link Set} of padding algorithm names (Strings).
121
   */
103
   */
122
  public static final Set getNames()
104
  public static final Set getNames()
(-)SSL3.java (-26 / +18 lines)
Lines 39-77 Link Here
39
package gnu.javax.crypto.pad;
39
package gnu.javax.crypto.pad;
40
40
41
/**
41
/**
42
 * The padding scheme used by the Secure Sockets Layer, version 3. This
42
 * The padding scheme used by the Secure Sockets Layer, version 3. This padding
43
 * padding scheme is used in the block-ciphered struct, e.g.:
43
 * scheme is used in the block-ciphered struct, e.g.:
44
 *
45
 * <pre>
44
 * <pre>
46
 * block-ciphered struct {
45
 *  block-ciphered struct {
47
 *   opaque content[SSLCompressed.length];
46
 *    opaque content[SSLCompressed.length];
48
 *   opaque MAC[CipherSpec.hash_size];
47
 *    opaque MAC[CipherSpec.hash_size];
49
 *   uint8 padding[GenericBlockCipher.padding_length];
48
 *    uint8 padding[GenericBlockCipher.padding_length];
50
 *   uint8 padding_length;
49
 *    uint8 padding_length;
51
 * } GenericBlockCipher;
50
 *  } GenericBlockCipher;
52
 * </pre>
51
 * </pre>
53
 *
52
 * <p>
54
 * <p>Where <i>padding_length</i> is <i>cipher_block_size</i> -
53
 * Where <i>padding_length</i> is <i>cipher_block_size</i> -
55
 * ((<i>SSLCompressed.length</i> + <i>CipherSpec.hash_size</i>)
54
 * ((<i>SSLCompressed.length</i> + <i>CipherSpec.hash_size</i>) %
56
 * % <i>cipher_block_size</i>) - 1. That is, the padding is enough bytes
55
 * <i>cipher_block_size</i>) - 1. That is, the padding is enough bytes to make
57
 * to make the plaintext a multiple of the block size minus one, plus one
56
 * the plaintext a multiple of the block size minus one, plus one additional
58
 * additional byte for the padding length. The padding can be any arbitrary
57
 * byte for the padding length. The padding can be any arbitrary data.
59
 * data.</p>
60
 */
58
 */
61
public class SSL3 extends BasePad
59
public class SSL3
60
    extends BasePad
62
{
61
{
63
64
  // Constructors.
65
  // -------------------------------------------------------------------------
66
67
  public SSL3()
62
  public SSL3()
68
  {
63
  {
69
    super("ssl3");
64
    super("ssl3");
70
  }
65
  }
71
66
72
  // Instance methods.
73
  // -------------------------------------------------------------------------
74
75
  public void setup()
67
  public void setup()
76
  {
68
  {
77
    if (blockSize <= 0 || blockSize > 255)
69
    if (blockSize <= 0 || blockSize > 255)
Lines 83-89 Link Here
83
    int padlen = blockSize - (len % blockSize);
75
    int padlen = blockSize - (len % blockSize);
84
    byte[] pad = new byte[padlen];
76
    byte[] pad = new byte[padlen];
85
    for (int i = 0; i < padlen; i++)
77
    for (int i = 0; i < padlen; i++)
86
      pad[i] = (byte) (padlen - 1);
78
      pad[i] = (byte)(padlen - 1);
87
    return pad;
79
    return pad;
88
  }
80
  }
89
81
Lines 95-98 Link Here
95
      throw new WrongPaddingException();
87
      throw new WrongPaddingException();
96
    return padlen + 1;
88
    return padlen + 1;
97
  }
89
  }
98
}
90
}
(-)TBC.java (-38 / +25 lines)
Lines 45-70 Link Here
45
import java.util.logging.Logger;
45
import java.util.logging.Logger;
46
46
47
/**
47
/**
48
 * <p>The implementation of the Trailing Bit Complement (TBC) padding algorithm.</p>
48
 * The implementation of the Trailing Bit Complement (TBC) padding algorithm.
49
 *
49
 * <p>
50
 * <p>In this mode, "...the data string is padded at the trailing end with the
50
 * In this mode, "...the data string is padded at the trailing end with the
51
 * complement of the trailing bit of the unpadded message: if the trailing bit
51
 * complement of the trailing bit of the unpadded message: if the trailing bit
52
 * is <tt>1</tt>, then <tt>0</tt> bits are appended, and if the trailing bit is
52
 * is <tt>1</tt>, then <tt>0</tt> bits are appended, and if the trailing
53
 * <tt>0</tt>, then <tt>1</tt> bits are appended. As few bits are added as are
53
 * bit is <tt>0</tt>, then <tt>1</tt> bits are appended. As few bits are
54
 * necessary to meet the formatting size requirement."</p>
54
 * added as are necessary to meet the formatting size requirement."
55
 *
55
 * <p>
56
 * References:<br>
56
 * References:
57
 * <a href="http://csrc.nist.gov/encryption/modes/Recommendation/Modes01.pdf">
57
 * <ol>
58
 * Recommendation for Block Cipher Modes of Operation Methods and Techniques</a>,
58
 * <li><a
59
 * Morris Dworkin.<p>
59
 * href="http://csrc.nist.gov/encryption/modes/Recommendation/Modes01.pdf">
60
 * Recommendation for Block Cipher Modes of Operation Methods and
61
 * Techniques</a>, Morris Dworkin.</li>
62
 * </ol>
60
 */
63
 */
61
public final class TBC extends BasePad
64
public final class TBC
65
    extends BasePad
62
{
66
{
63
  private static final Logger log = Logger.getLogger(TBC.class.getName());
67
  private static final Logger log = Logger.getLogger(TBC.class.getName());
64
68
65
  /**
69
  /**
66
   * Trivial package-private constructor for use by the <i>Factory</i> class.<p>
70
   * Trivial package-private constructor for use by the <i>Factory</i> class.
67
   *
71
   * 
68
   * @see PadFactory
72
   * @see PadFactory
69
   */
73
   */
70
  TBC()
74
  TBC()
Lines 72-108 Link Here
72
    super(Registry.TBC_PAD);
76
    super(Registry.TBC_PAD);
73
  }
77
  }
74
78
75
  // Class methods
76
  // -------------------------------------------------------------------------
77
78
  // Implementation of abstract methods in BasePad
79
  // -------------------------------------------------------------------------
80
81
  public void setup()
79
  public void setup()
82
  {
80
  {
83
    if (blockSize < 1 || blockSize > 256)
81
    if (blockSize < 1 || blockSize > 256)
84
      {
82
      throw new IllegalArgumentException();
85
        throw new IllegalArgumentException();
86
      }
87
  }
83
  }
88
84
89
  public byte[] pad(byte[] in, int offset, int length)
85
  public byte[] pad(byte[] in, int offset, int length)
90
  {
86
  {
91
    int padLength = blockSize;
87
    int padLength = blockSize;
92
    if (length % blockSize != 0)
88
    if (length % blockSize != 0)
93
      {
89
      padLength = blockSize - length % blockSize;
94
        padLength = blockSize - length % blockSize;
95
      }
96
    byte[] result = new byte[padLength];
90
    byte[] result = new byte[padLength];
97
    int lastBit = in[offset + length - 1] & 0x01;
91
    int lastBit = in[offset + length - 1] & 0x01;
98
    if (lastBit == 0)
92
    if (lastBit == 0)
99
      {
93
      for (int i = 0; i < padLength;)
100
        for (int i = 0; i < padLength;)
94
        result[i++] = 0x01;
101
          {
95
    // else it's already set to zeroes by virtue of initialisation
102
            result[i++] = 0x01;
103
          }
104
      } // else it's already set to zeroes by virtue of initialisation
105
106
    if (Configuration.DEBUG)
96
    if (Configuration.DEBUG)
107
      log.fine("padding: 0x" + Util.toString(result));
97
      log.fine("padding: 0x" + Util.toString(result));
108
    return result;
98
    return result;
Lines 119-131 Link Here
119
        result++;
109
        result++;
120
        limit--;
110
        limit--;
121
      }
111
      }
122
123
    if (result > length)
112
    if (result > length)
124
      {
113
      throw new WrongPaddingException();
125
        throw new WrongPaddingException();
126
      }
127
    if (Configuration.DEBUG)
114
    if (Configuration.DEBUG)
128
      log.fine("padding length: " + result);
115
      log.fine("padding length: " + result);
129
    return result;
116
    return result;
130
  }
117
  }
131
}
118
}
(-)TLS1.java (-31 / +19 lines)
Lines 39-77 Link Here
39
package gnu.javax.crypto.pad;
39
package gnu.javax.crypto.pad;
40
40
41
/**
41
/**
42
 * The padding scheme used by the Transport Layer Security protocol,
42
 * The padding scheme used by the Transport Layer Security protocol, version 1.
43
 * version 1. This padding scheme is used in the block-ciphered struct,
43
 * This padding scheme is used in the block-ciphered struct, e.g.:
44
 * e.g.:
45
 *
46
 * <pre>
44
 * <pre>
47
 * block-ciphered struct {
45
 *  block-ciphered struct {
48
 *   opaque content[TLSCompressed.length];
46
 *    opaque content[TLSCompressed.length];
49
 *   opaque MAC[CipherSpec.hash_size];
47
 *    opaque MAC[CipherSpec.hash_size];
50
 *   uint8 padding[GenericBlockCipher.padding_length];
48
 *    uint8 padding[GenericBlockCipher.padding_length];
51
 *   uint8 padding_length;
49
 *    uint8 padding_length;
52
 * } GenericBlockCipher;
50
 *  } GenericBlockCipher;
53
 * </pre>
51
 * </pre>
54
 *
52
 * <p>
55
 * <p>Where <i>padding_length</i> is any multiple of <i>cipher_block_size</i> -
53
 * Where <i>padding_length</i> is any multiple of <i>cipher_block_size</i> -
56
 * ((<i>SSLCompressed.length</i> + <i>CipherSpec.hash_size</i>)
54
 * ((<i>SSLCompressed.length</i> + <i>CipherSpec.hash_size</i>) %
57
 * % <i>cipher_block_size</i>) - 1 that is less than 255. Every byte of the
55
 * <i>cipher_block_size</i>) - 1 that is less than 255. Every byte of the
58
 * padding must be equal to <i>padding_length</i>. That is, the end of the
56
 * padding must be equal to <i>padding_length</i>. That is, the end of the
59
 * plaintext is <i>n</i> + 1 copies of the unsigned byte <i>n</i>.</p>
57
 * plaintext is <i>n</i> + 1 copies of the unsigned byte <i>n</i>.
60
 */
58
 */
61
public class TLS1 extends BasePad
59
public class TLS1
60
    extends BasePad
62
{
61
{
63
64
  // Constructors.
65
  // -------------------------------------------------------------------------
66
67
  public TLS1()
62
  public TLS1()
68
  {
63
  {
69
    super("tls1");
64
    super("tls1");
70
  }
65
  }
71
66
72
  // Instance methods.
73
  // -------------------------------------------------------------------------
74
75
  public void setup()
67
  public void setup()
76
  {
68
  {
77
    if (blockSize <= 0 || blockSize > 255)
69
    if (blockSize <= 0 || blockSize > 255)
Lines 83-91 Link Here
83
    int padlen = blockSize - (len % blockSize);
75
    int padlen = blockSize - (len % blockSize);
84
    byte[] pad = new byte[padlen];
76
    byte[] pad = new byte[padlen];
85
    for (int i = 0; i < padlen; i++)
77
    for (int i = 0; i < padlen; i++)
86
      {
78
      pad[i] = (byte)(padlen - 1);
87
        pad[i] = (byte) (padlen - 1);
88
      }
89
    return pad;
79
    return pad;
90
  }
80
  }
91
81
Lines 94-103 Link Here
94
  {
84
  {
95
    int padlen = in[off + len - 1] & 0xFF;
85
    int padlen = in[off + len - 1] & 0xFF;
96
    for (int i = off + (len - padlen - 1); i < off + len - 1; i++)
86
    for (int i = off + (len - padlen - 1); i < off + len - 1; i++)
97
      {
87
      if ((in[i] & 0xFF) != padlen)
98
        if ((in[i] & 0xFF) != padlen)
88
        throw new WrongPaddingException();
99
          throw new WrongPaddingException();
100
      }
101
    return padlen + 1;
89
    return padlen + 1;
102
  }
90
  }
103
}
91
}
(-)WrongPaddingException.java (-17 / +4 lines)
Lines 39-61 Link Here
39
package gnu.javax.crypto.pad;
39
package gnu.javax.crypto.pad;
40
40
41
/**
41
/**
42
 * <p>A checked exception that indicates that a padding algorithm did not find the
42
 * A checked exception that indicates that a padding algorithm did not find the
43
 * expected padding bytes when unpadding some data.</p>
43
 * expected padding bytes when unpadding some data.
44
 */
44
 */
45
public class WrongPaddingException extends Exception
45
public class WrongPaddingException
46
    extends Exception
46
{
47
{
47
48
  // Constants and variables
49
  // -------------------------------------------------------------------------
50
51
  // Constructor(s)
52
  // -------------------------------------------------------------------------
53
54
  // implicit 0-arguments constructor
55
56
  // Class methods
57
  // -------------------------------------------------------------------------
58
59
  // Instant methods
60
  // -------------------------------------------------------------------------
61
}
48
}

Return to bug 26067