2 Copyright (C) 2003, 2006 Free Software Foundation, Inc.
4 This file is a part of GNU Classpath.
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.
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.
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
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
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. */
39 package gnu
.javax
.crypto
.pad
;
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
;
46 import java
.io
.PrintWriter
;
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>
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>
64 public class PKCS1_V1_5
extends BasePad
67 // Debugging methods and variables
68 // -------------------------------------------------------------------------
70 private static final String NAME
= Registry
.EME_PKCS1_V1_5_PAD
;
72 private static final boolean DEBUG
= false;
74 private static final int debuglevel
= 9;
76 private static final PrintWriter err
= new PrintWriter(System
.out
, true);
78 private static void debug(final String s
)
80 err
.println(">>> " + NAME
+ ": " + s
);
83 // Constants and variables
84 // -------------------------------------------------------------------------
86 private EME_PKCS1_V1_5 codec
;
89 // -------------------------------------------------------------------------
92 * <p>Trivial package-private constructor for use by the <i>Factory</i> class.
95 * @see gnu.crypto.pad.PadFactory
99 super(Registry
.EME_PKCS1_V1_5_PAD
);
103 // -------------------------------------------------------------------------
105 // Implementation of abstract methods in BasePad
106 // -------------------------------------------------------------------------
110 codec
= EME_PKCS1_V1_5
.getInstance(blockSize
);
113 public byte[] pad(final byte[] in
, final int offset
, final int length
)
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)
122 debug("padding: 0x" + Util
.toString(result
));
127 public int unpad(final byte[] in
, final int offset
, final int length
)
128 throws WrongPaddingException
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)
135 debug("padding length: " + String
.valueOf(result
));
140 // overloaded methods ------------------------------------------------------
142 public boolean selfTest()
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];
150 for (bs
= 256; bs
< 1025; bs
+= 256)
153 for (i
= 0; i
< mLen
.length
; i
++)
157 if (j
+ p
.length
!= blockSize
)
159 new RuntimeException(name()).printStackTrace(System
.err
);
163 System
.arraycopy(p
, 0, EM
, 0, p
.length
);
164 System
.arraycopy(M
, 0, EM
, p
.length
, j
);
167 if (p
.length
!= unpad(EM
, 0, blockSize
))
169 new RuntimeException(name()).printStackTrace(System
.err
);
173 catch (WrongPaddingException x
)
175 x
.printStackTrace(System
.err
);