Imported GNU Classpath 0.90
[official-gcc.git] / libjava / classpath / gnu / java / security / sig / rsa / EME_PKCS1_V1_5.java
blobefe580d51675befb11b40b6e2c717e893d53454f
1 /* EME_PKCS1_V1_5.java --
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
19 USA
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.
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.java.security.sig.rsa;
41 import gnu.java.security.prng.IRandom;
42 import gnu.java.security.prng.LimitReachedException;
43 import gnu.java.security.util.PRNG;
45 import java.io.ByteArrayOutputStream;
46 import java.security.interfaces.RSAKey;
47 import java.util.Random;
49 /**
50 * <p>An implementation of the EME-PKCS1-V1.5 encoding and decoding methods.</p>
52 * <p>EME-PKCS1-V1.5 is parameterised by the entity <code>k</code> which is the
53 * byte count of an RSA public shared modulus.</p>
55 * <p>References:</p>
56 * <ol>
57 * <li><a href="http://www.ietf.org/rfc/rfc3447.txt">Public-Key Cryptography
58 * Standards (PKCS) #1:</a><br>
59 * RSA Cryptography Specifications Version 2.1.<br>
60 * Jakob Jonsson and Burt Kaliski.</li>
61 * </ol>
63 public class EME_PKCS1_V1_5
66 // Constants and variables
67 // -------------------------------------------------------------------------
69 private int k;
71 private ByteArrayOutputStream baos = new ByteArrayOutputStream();
73 /** Our default source of randomness. */
74 private PRNG prng = PRNG.getInstance();
76 // Constructor(s)
77 // -------------------------------------------------------------------------
79 private EME_PKCS1_V1_5(final int k)
81 super();
83 this.k = k;
86 // Class methods
87 // -------------------------------------------------------------------------
89 public static final EME_PKCS1_V1_5 getInstance(final int k)
91 if (k < 0)
93 throw new IllegalArgumentException("k must be a positive integer");
95 return new EME_PKCS1_V1_5(k);
98 public static final EME_PKCS1_V1_5 getInstance(final RSAKey key)
100 final int modBits = key.getModulus().bitLength();
101 final int k = (modBits + 7) / 8;
102 return EME_PKCS1_V1_5.getInstance(k);
105 // Instance methods
106 // -------------------------------------------------------------------------
109 * <p>Generates an octet string <code>PS</code> of length <code>k - mLen -
110 * 3</code> consisting of pseudo-randomly generated nonzero octets. The
111 * length of <code>PS</code> will be at least eight octets.</p>
113 * <p>The method then concatenates <code>PS</code>, the message <code>M</code>,
114 * and other padding to form an encoded message <code>EM</code> of length
115 * <code>k</code> octets as:</p>
117 * <pre>
118 * EM = 0x00 || 0x02 || PS || 0x00 || M.
119 * </pre>
121 * <p>This method uses a default PRNG to obtain the padding bytes.</p>
123 * @param M the message to encode.
124 * @return the encoded message <code>EM</code>.
126 public byte[] encode(final byte[] M)
128 // a. Generate an octet string PS of length k - mLen - 3 consisting
129 // of pseudo-randomly generated nonzero octets. The length of PS
130 // will be at least eight octets.
131 final byte[] PS = new byte[k - M.length - 3];
133 // FIXME. This should be configurable, somehow.
134 prng.nextBytes(PS);
135 int i = 0;
136 for (; i < PS.length; i++)
138 if (PS[i] == 0)
139 PS[i] = 1;
141 // b. Concatenate PS, the message M, and other padding to form an
142 // encoded message EM of length k octets as
144 // EM = 0x00 || 0x02 || PS || 0x00 || M.
145 return assembleEM(PS, M);
149 * <p>Similar to {@link #encode(byte[])} method, except that the source of
150 * randomness to use for obtaining the padding bytes (an instance of
151 * {@link IRandom}) is given as a parameter.</p>
153 * @param M the message to encode.
154 * @param irnd the {@link IRandom} instance to use as a source of randomness.
155 * @return the encoded message <code>EM</code>.
157 public byte[] encode(final byte[] M, final IRandom irnd)
159 final byte[] PS = new byte[k - M.length - 3];
162 irnd.nextBytes(PS, 0, PS.length);
163 int i = 0;
164 outer: while (true)
166 for (; i < PS.length; i++)
168 if (PS[i] == 0x00)
170 System.arraycopy(PS, i + 1, PS, i, PS.length - i - 1);
171 irnd.nextBytes(PS, PS.length - 1, 1);
172 continue outer;
175 break;
178 catch (IllegalStateException x)
180 throw new RuntimeException("encode(): " + String.valueOf(x));
182 catch (LimitReachedException x)
184 throw new RuntimeException("encode(): " + String.valueOf(x));
187 return assembleEM(PS, M);
191 * <p>Similar to the {@link #encode(byte[], IRandom)} method, except that
192 * the source of randmoness is an instance of {@link Random}.
194 * @param M the message to encode.
195 * @param rnd the {@link Random} instance to use as a source of randomness.
196 * @return the encoded message <code>EM</code>.
198 public byte[] encode(final byte[] M, final Random rnd)
200 final byte[] PS = new byte[k - M.length - 3];
201 rnd.nextBytes(PS);
202 int i = 0;
203 outer: while (true)
205 for (; i < PS.length; i++)
207 if (PS[i] == 0x00)
209 System.arraycopy(PS, i + 1, PS, i, PS.length - i - 1);
210 PS[PS.length - 1] = (byte) rnd.nextInt();
211 continue outer;
214 break;
217 return assembleEM(PS, M);
221 * <p>Separate the encoded message <code>EM</code> into an octet string
222 * <code>PS</code> consisting of nonzero octets and a message <code>M</code>
223 * as:</p>
225 * <pre>
226 * EM = 0x00 || 0x02 || PS || 0x00 || M.
227 * </pre>
229 * <p>If the first octet of <code>EM</code> does not have hexadecimal value
230 * <code>0x00</code>, if the second octet of <code>EM</code> does not have
231 * hexadecimal value <code>0x02</code>, if there is no octet with hexadecimal
232 * value <code>0x00</code> to separate <code>PS</code> from <code>M</code>,
233 * or if the length of <code>PS</code> is less than <code>8</code> octets,
234 * output "decryption error" and stop.</p>
236 * @param EM the designated encoded message.
237 * @return the decoded message <code>M</code> framed in the designated
238 * <code>EM</code> value.
239 * @throws IllegalArgumentException if the length of the designated entity
240 * <code>EM</code> is different than <code>k</code> (the length in bytes of
241 * the public shared modulus), or if any of the conditions described above
242 * is detected.
244 public byte[] decode(final byte[] EM)
246 // Separate the encoded message EM into an
247 // octet string PS consisting of nonzero octets and a message M as
249 // EM = 0x00 || 0x02 || PS || 0x00 || M.
251 // If the first octet of EM does not have hexadecimal value 0x00, if
252 // the second octet of EM does not have hexadecimal value 0x02, if
253 // there is no octet with hexadecimal value 0x00 to separate PS from
254 // M, or if the length of PS is less than 8 octets, output
255 // "decryption error" and stop. (See the note below.)
256 final int emLen = EM.length;
257 if (emLen != k)
259 throw new IllegalArgumentException("decryption error");
261 if (EM[0] != 0x00)
263 throw new IllegalArgumentException("decryption error");
265 if (EM[1] != 0x02)
267 throw new IllegalArgumentException("decryption error");
269 int i = 2;
270 for (; i < emLen; i++)
272 if (EM[i] == 0x00)
274 break;
277 if (i >= emLen || i < 11)
279 throw new IllegalArgumentException("decryption error");
281 i++;
282 final byte[] result = new byte[emLen - i];
283 System.arraycopy(EM, i, result, 0, result.length);
284 return result;
287 // helper methods ----------------------------------------------------------
289 private byte[] assembleEM(final byte[] PS, final byte[] M)
291 // b. Concatenate PS, the message M, and other padding to form an
292 // encoded message EM of length k octets as
294 // EM = 0x00 || 0x02 || PS || 0x00 || M.
295 baos.reset();
296 baos.write(0x00);
297 baos.write(0x02);
298 baos.write(PS, 0, PS.length);
299 baos.write(0x00);
300 baos.write(M, 0, M.length);
301 final byte[] result = baos.toByteArray();
302 baos.reset();
304 return result;