RSA.PrivateKey
[org.ibex.crypto.git] / src / org / ibex / crypto / RSA.java
1 // Copyright 2000-2005 the Contributors, as shown in the revision logs.
2 // Licensed under the Apache Public Source License 2.0 ("the License").
3 // You may not use this file except in compliance with the License.
4
5 package org.ibex.crypto;
6 import java.math.BigInteger;
7 import java.util.*;
8
9 public class RSA {
10     private final BigInteger pq;
11     private final BigInteger e;
12     private final boolean reverse;
13     
14     public RSA(BigInteger pq, BigInteger e, boolean reverse) {
15         this.pq = pq;
16         this.e = e;
17         this.reverse = reverse;
18     }
19     
20     public int getInputBlockSize() { return (pq.bitLength()+7) / 8 - (reverse ? 0 : 1); }
21     public int getOutputBlockSize() { return (pq.bitLength()+7) / 8 - (reverse ? 1 : 0); }
22     
23     public byte[] process(byte[] in) {
24         // output block is the same size as the modulus (rounded up)
25         int outSize = getOutputBlockSize();
26         BigInteger t = new BigInteger(1,in);
27         BigInteger c = t.modPow(e,pq);
28         byte[] cbytes = c.toByteArray();
29         if(cbytes.length > outSize || (reverse && cbytes[0] == 0)) {
30             if(cbytes[0] != 0) throw new RuntimeException("should never happen");
31             byte[] buf = new byte[outSize];
32             System.arraycopy(cbytes,1,buf,0,outSize);
33             return buf;
34         } else if(!reverse && cbytes.length < outSize) {
35             // output needs to be exactly outSize in length
36             byte[] buf = new byte[outSize];
37             System.arraycopy(cbytes,0,buf,outSize-cbytes.length,cbytes.length);
38             return buf;
39         } else {
40             return cbytes;
41         }
42     }
43
44     public static class PublicKey {
45         public final BigInteger modulus;
46         public final BigInteger exponent;
47         
48         public PublicKey(Object o) {
49             Vector seq = (Vector) o;
50             modulus = (BigInteger) seq.elementAt(0);
51             exponent = (BigInteger) seq.elementAt(1);
52         }
53     }
54     
55     public static class PrivateKey {
56         public final int version;
57         public final BigInteger modulus;
58         public final BigInteger publicExponent;
59         public final BigInteger privateExponent;
60         public final BigInteger prime1;
61         public final BigInteger prime2;
62         public final BigInteger exponent1;
63         public final BigInteger exponent2;
64         public final BigInteger coefficient;
65         
66         public PrivateKey(Object o) throws DER.Exception {
67             Vector seq = (Vector) o;
68             version = ((Number)seq.elementAt(0)).intValue();
69             if(version != 0) throw new DER.Exception("Only private key version 0 is supported");
70             modulus = (BigInteger) seq.elementAt(1);
71             publicExponent = (BigInteger) seq.elementAt(2);
72             privateExponent = (BigInteger) seq.elementAt(3);
73             prime1 = (BigInteger) seq.elementAt(4);
74             prime2 = (BigInteger) seq.elementAt(5);
75             exponent1 = (BigInteger) seq.elementAt(6);
76             exponent2 = (BigInteger) seq.elementAt(7);
77             coefficient = (BigInteger) seq.elementAt(8);
78         }
79     }
80 }