1 /*
2  * Created on Jul 12, 2008
3  * Created by Paul Gardner
4  *
5  * Copyright (C) Azureus Software, Inc, All Rights Reserved.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18  */
19 
20 
21 package com.aelitis.azureus.core.security;
22 
23 import java.math.BigInteger;
24 import java.security.Key;
25 import java.security.KeyFactory;
26 import java.security.KeyPair;
27 import java.security.KeyPairGenerator;
28 import java.security.PrivateKey;
29 import java.security.PublicKey;
30 import java.security.Signature;
31 import java.security.spec.KeySpec;
32 
33 import org.gudy.bouncycastle.jce.ECNamedCurveTable;
34 import org.gudy.bouncycastle.jce.interfaces.ECPrivateKey;
35 import org.gudy.bouncycastle.jce.interfaces.ECPublicKey;
36 import org.gudy.bouncycastle.jce.provider.BouncyCastleProvider;
37 import org.gudy.bouncycastle.jce.spec.ECNamedCurveParameterSpec;
38 import org.gudy.bouncycastle.jce.spec.ECParameterSpec;
39 import org.gudy.bouncycastle.jce.spec.ECPrivateKeySpec;
40 import org.gudy.bouncycastle.jce.spec.ECPublicKeySpec;
41 import org.gudy.bouncycastle.math.ec.ECPoint;
42 
43 public class
44 CryptoECCUtils
45 {
46 	private static final ECNamedCurveParameterSpec ECCparam = ECNamedCurveTable.getParameterSpec("prime192v2");
47 
48 	public static KeyPair
createKeys()49 	createKeys()
50 
51 		throws CryptoManagerException
52 	{
53 		try
54 		{
55 			KeyPairGenerator keyGen = KeyPairGenerator.getInstance("ECDSA", BouncyCastleProvider.PROVIDER_NAME);
56 
57 			keyGen.initialize(ECCparam);
58 
59 			return keyGen.genKeyPair();
60 
61 		}catch(Throwable e){
62 
63 			throw( new CryptoManagerException( "Failed to create keys", e ));
64 		}
65 	}
66 
67 	public static Signature
getSignature( Key key )68 	getSignature(
69 		Key key )
70 
71 		throws CryptoManagerException
72 	{
73 		try
74 		{
75 			Signature ECCsig = Signature.getInstance("SHA1withECDSA", BouncyCastleProvider.PROVIDER_NAME);
76 
77 			if( key instanceof ECPrivateKey ){
78 
79 				ECCsig.initSign((ECPrivateKey)key);
80 
81 			}else if( key instanceof ECPublicKey ){
82 
83 				ECCsig.initVerify((ECPublicKey)key);
84 
85 			}else{
86 
87 				throw new CryptoManagerException("Invalid Key Type, ECC keys required");
88 			}
89 
90 			return ECCsig;
91 
92 		}catch( CryptoManagerException e ){
93 
94 			throw( e );
95 
96 		}catch( Throwable e ){
97 
98 			throw( new CryptoManagerException( "Failed to create Signature", e ));
99 		}
100 	}
101 
102 	public static byte[]
keyToRawdata( PrivateKey privkey )103    	keyToRawdata(
104    		PrivateKey privkey )
105 
106    		throws CryptoManagerException
107    	{
108    		if(!(privkey instanceof ECPrivateKey)){
109 
110    			throw( new CryptoManagerException( "Invalid private key" ));
111    		}
112 
113    		return ((ECPrivateKey)privkey).getD().toByteArray();
114    	}
115 
116 	public static PrivateKey
rawdataToPrivkey( byte[] input )117    	rawdataToPrivkey(
118    		byte[] input )
119 
120    		throws CryptoManagerException
121    	{
122    		BigInteger D = new BigInteger(input);
123 
124    		KeySpec keyspec = new ECPrivateKeySpec(D,(ECParameterSpec)ECCparam);
125 
126    		PrivateKey privkey = null;
127 
128    		try{
129    			privkey = KeyFactory.getInstance("ECDSA",BouncyCastleProvider.PROVIDER_NAME).generatePrivate(keyspec);
130 
131    			return privkey;
132 
133    		}catch( Throwable e ){
134 
135    			throw( new CryptoManagerException( "Failed to decode private key" ));
136    		}
137    	}
138 
139 	public static byte[]
keyToRawdata( PublicKey pubkey )140    	keyToRawdata(
141    		PublicKey pubkey )
142 
143    		throws CryptoManagerException
144    	{
145    		if(!(pubkey instanceof ECPublicKey)){
146 
147    			throw( new CryptoManagerException( "Invalid public key" ));
148    		}
149 
150    		return ((ECPublicKey)pubkey).getQ().getEncoded();
151    	}
152 
153 
154 	public static  PublicKey
rawdataToPubkey( byte[] input )155    	rawdataToPubkey(
156    		byte[] input )
157 
158    		throws CryptoManagerException
159    	{
160    		ECPoint W = ECCparam.getCurve().decodePoint(input);
161 
162    		KeySpec keyspec = new ECPublicKeySpec(W,(ECParameterSpec)ECCparam);
163 
164    		try{
165 
166    			return KeyFactory.getInstance("ECDSA", BouncyCastleProvider.PROVIDER_NAME).generatePublic(keyspec);
167 
168    		}catch (Throwable e){
169 
170    			throw( new CryptoManagerException( "Failed to decode public key", e ));
171    		}
172    	}
173 }
174