Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: gnu/javax/crypto/pad/PKCS1_V1_5.java


1   /* PKCS1_V1_5.java -- 
2      Copyright (C) 2003, 2006 Free Software Foundation, Inc.
3   
4   This file is a part of GNU Classpath.
5   
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
8   the Free Software Foundation; either version 2 of the License, or (at
9   your option) any later version.
10  
11  GNU Classpath is distributed in the hope that it will be useful, but
12  WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  General Public License for more details.
15  
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
18  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
19  USA
20  
21  Linking this library statically or dynamically with other modules is
22  making a combined work based on this library.  Thus, the terms and
23  conditions of the GNU General Public License cover the whole
24  combination.
25  
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
28  executable, regardless of the license terms of these independent
29  modules, and to copy and distribute the resulting executable under
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
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
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
36  exception statement from your version.  */
37  
38  
39  package gnu.javax.crypto.pad;
40  
41  import gnu.java.security.Registry;
42  import gnu.java.security.sig.rsa.EME_PKCS1_V1_5;
43  import gnu.java.security.util.PRNG;
44  import gnu.java.security.util.Util;
45  
46  import java.io.PrintWriter;
47  
48  /**
49   * <p>A padding algorithm implementation of the EME-PKCS1-V1.5 encoding/decoding
50   * algorithm as described in section 7.2 of RFC-3447. This is effectively an
51   * <i>Adapter</i> over an instance of {@link EME_PKCS1_V1_5} initialised with
52   * the RSA public shared modulus length (in bytes).</p>
53   *
54   * <p>References:</p>
55   * <ol>
56   *    <li><a href="http://www.ietf.org/rfc/rfc3447.txt">Public-Key Cryptography
57   *    Standards (PKCS) #1:</a><br>
58   *    RSA Cryptography Specifications Version 2.1.<br>
59   *    Jakob Jonsson and Burt Kaliski.</li>
60   * </ol>
61   *
62   * @see EME_PKCS1_V1_5
63   */
64  public class PKCS1_V1_5 extends BasePad
65  {
66  
67    // Debugging methods and variables
68    // -------------------------------------------------------------------------
69  
70    private static final String NAME = Registry.EME_PKCS1_V1_5_PAD;
71  
72    private static final boolean DEBUG = false;
73  
74    private static final int debuglevel = 9;
75  
76    private static final PrintWriter err = new PrintWriter(System.out, true);
77  
78    private static void debug(final String s)
79    {
80      err.println(">>> " + NAME + ": " + s);
81    }
82  
83    // Constants and variables
84    // -------------------------------------------------------------------------
85  
86    private EME_PKCS1_V1_5 codec;
87  
88    // Constructor(s)
89    // -------------------------------------------------------------------------
90  
91    /**
92     * <p>Trivial package-private constructor for use by the <i>Factory</i> class.
93     * </p>
94     *
95     * @see gnu.crypto.pad.PadFactory
96     */
97    PKCS1_V1_5()
98    {
99      super(Registry.EME_PKCS1_V1_5_PAD);
100   }
101 
102   // Class methods
103   // -------------------------------------------------------------------------
104 
105   // Implementation of abstract methods in BasePad
106   // -------------------------------------------------------------------------
107 
108   public void setup()
109   {
110     codec = EME_PKCS1_V1_5.getInstance(blockSize);
111   }
112 
113   public byte[] pad(final byte[] in, final int offset, final int length)
114   {
115     final byte[] M = new byte[length];
116     System.arraycopy(in, offset, M, 0, length);
117     final byte[] EM = codec.encode(M);
118     final byte[] result = new byte[blockSize - length];
119     System.arraycopy(EM, 0, result, 0, result.length);
120     if (DEBUG && debuglevel > 8)
121       {
122         debug("padding: 0x" + Util.toString(result));
123       }
124     return result;
125   }
126 
127   public int unpad(final byte[] in, final int offset, final int length)
128       throws WrongPaddingException
129   {
130     final byte[] EM = new byte[length];
131     System.arraycopy(in, offset, EM, 0, length);
132     final int result = length - codec.decode(EM).length;
133     if (DEBUG && debuglevel > 8)
134       {
135         debug("padding length: " + String.valueOf(result));
136       }
137     return result;
138   }
139 
140   // overloaded methods ------------------------------------------------------
141 
142   public boolean selfTest()
143   {
144     final int[] mLen = new int[] { 16, 20, 32, 48, 64 };
145     final byte[] M = new byte[mLen[mLen.length - 1]];
146     PRNG.getInstance().nextBytes(M);
147     final byte[] EM = new byte[1024];
148     byte[] p;
149     int bs, i, j;
150     for (bs = 256; bs < 1025; bs += 256)
151       {
152         init(bs);
153         for (i = 0; i < mLen.length; i++)
154           {
155             j = mLen[i];
156             p = pad(M, 0, j);
157             if (j + p.length != blockSize)
158               {
159                 new RuntimeException(name()).printStackTrace(System.err);
160                 return false;
161               }
162 
163             System.arraycopy(p, 0, EM, 0, p.length);
164             System.arraycopy(M, 0, EM, p.length, j);
165             try
166               {
167                 if (p.length != unpad(EM, 0, blockSize))
168                   {
169                     new RuntimeException(name()).printStackTrace(System.err);
170                     return false;
171                   }
172               }
173             catch (WrongPaddingException x)
174               {
175                 x.printStackTrace(System.err);
176                 return false;
177               }
178           }
179         reset();
180       }
181 
182     return true;
183   }
184 }