SignatureDSA.java

Go to the documentation of this file.
00001 /* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */
00002 /*
00003 Copyright (c) 2002-2011 ymnk, JCraft,Inc. All rights reserved.
00004 
00005 Redistribution and use in source and binary forms, with or without
00006 modification, are permitted provided that the following conditions are met:
00007 
00008   1. Redistributions of source code must retain the above copyright notice,
00009      this list of conditions and the following disclaimer.
00010 
00011   2. Redistributions in binary form must reproduce the above copyright 
00012      notice, this list of conditions and the following disclaimer in 
00013      the documentation and/or other materials provided with the distribution.
00014 
00015   3. The names of the authors may not be used to endorse or promote products
00016      derived from this software without specific prior written permission.
00017 
00018 THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
00019 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
00020 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
00021 INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
00022 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00023 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
00024 OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00025 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00026 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
00027 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00028 */
00029 
00030 package com.jcraft.jsch.jce;
00031 
00032 import java.math.BigInteger;
00033 import java.security.*;
00034 import java.security.spec.*;
00035 
00036 public class SignatureDSA implements com.jcraft.jsch.SignatureDSA{
00037 
00038   java.security.Signature signature;
00039   KeyFactory keyFactory;
00040 
00041   public void init() throws Exception{
00042     signature=java.security.Signature.getInstance("SHA1withDSA");
00043     keyFactory=KeyFactory.getInstance("DSA");
00044   }     
00045   public void setPubKey(byte[] y, byte[] p, byte[] q, byte[] g) throws Exception{
00046     DSAPublicKeySpec dsaPubKeySpec = 
00047     new DSAPublicKeySpec(new BigInteger(y),
00048                  new BigInteger(p),
00049                  new BigInteger(q),
00050                  new BigInteger(g));
00051     PublicKey pubKey=keyFactory.generatePublic(dsaPubKeySpec);
00052     signature.initVerify(pubKey);
00053   }
00054   public void setPrvKey(byte[] x, byte[] p, byte[] q, byte[] g) throws Exception{
00055     DSAPrivateKeySpec dsaPrivKeySpec = 
00056     new DSAPrivateKeySpec(new BigInteger(x),
00057                   new BigInteger(p),
00058                   new BigInteger(q),
00059                   new BigInteger(g));
00060     PrivateKey prvKey = keyFactory.generatePrivate(dsaPrivKeySpec);
00061     signature.initSign(prvKey);
00062   }
00063   public byte[] sign() throws Exception{
00064     byte[] sig=signature.sign();      
00065 /*
00066 System.err.print("sign["+sig.length+"] ");
00067 for(int i=0; i<sig.length;i++){
00068 System.err.print(Integer.toHexString(sig[i]&0xff)+":");
00069 }
00070 System.err.println("");
00071 */
00072     // sig is in ASN.1
00073     // SEQUENCE::={ r INTEGER, s INTEGER }
00074     int len=0;  
00075     int index=3;
00076     len=sig[index++]&0xff;
00077 //System.err.println("! len="+len);
00078     byte[] r=new byte[len];
00079     System.arraycopy(sig, index, r, 0, r.length);
00080     index=index+len+1;
00081     len=sig[index++]&0xff;
00082 //System.err.println("!! len="+len);
00083     byte[] s=new byte[len];
00084     System.arraycopy(sig, index, s, 0, s.length);
00085 
00086     byte[] result=new byte[40];
00087 
00088     // result must be 40 bytes, but length of r and s may not be 20 bytes  
00089 
00090     System.arraycopy(r, (r.length>20)?1:0,
00091              result, (r.length>20)?0:20-r.length,
00092              (r.length>20)?20:r.length);
00093     System.arraycopy(s, (s.length>20)?1:0,
00094              result, (s.length>20)?20:40-s.length,
00095              (s.length>20)?20:s.length);
00096  
00097 //  System.arraycopy(sig, (sig[3]==20?4:5), result, 0, 20);
00098 //  System.arraycopy(sig, sig.length-20, result, 20, 20);
00099 
00100     return result;
00101   }
00102   public void update(byte[] foo) throws Exception{
00103    signature.update(foo);
00104   }
00105   public boolean verify(byte[] sig) throws Exception{
00106     int i=0;
00107     int j=0;
00108     byte[] tmp;
00109 
00110     if(sig[0]==0 && sig[1]==0 && sig[2]==0){
00111     j=((sig[i++]<<24)&0xff000000)|((sig[i++]<<16)&0x00ff0000)|
00112     ((sig[i++]<<8)&0x0000ff00)|((sig[i++])&0x000000ff);
00113     i+=j;
00114     j=((sig[i++]<<24)&0xff000000)|((sig[i++]<<16)&0x00ff0000)|
00115     ((sig[i++]<<8)&0x0000ff00)|((sig[i++])&0x000000ff);
00116     tmp=new byte[j]; 
00117     System.arraycopy(sig, i, tmp, 0, j); sig=tmp;
00118     }
00119 
00120     // ASN.1
00121     int frst=((sig[0]&0x80)!=0?1:0);
00122     int scnd=((sig[20]&0x80)!=0?1:0);
00123     //System.err.println("frst: "+frst+", scnd: "+scnd);
00124 
00125     int length=sig.length+6+frst+scnd;
00126     tmp=new byte[length];
00127     tmp[0]=(byte)0x30; tmp[1]=(byte)0x2c; 
00128     tmp[1]+=frst; tmp[1]+=scnd;
00129     tmp[2]=(byte)0x02; tmp[3]=(byte)0x14;
00130     tmp[3]+=frst;
00131     System.arraycopy(sig, 0, tmp, 4+frst, 20);
00132     tmp[4+tmp[3]]=(byte)0x02; tmp[5+tmp[3]]=(byte)0x14;
00133     tmp[5+tmp[3]]+=scnd;
00134     System.arraycopy(sig, 20, tmp, 6+tmp[3]+scnd, 20);
00135     sig=tmp;
00136 
00137 /*
00138     tmp=new byte[sig.length+6];
00139     tmp[0]=(byte)0x30; tmp[1]=(byte)0x2c; 
00140     tmp[2]=(byte)0x02; tmp[3]=(byte)0x14;
00141     System.arraycopy(sig, 0, tmp, 4, 20);
00142     tmp[24]=(byte)0x02; tmp[25]=(byte)0x14;
00143     System.arraycopy(sig, 20, tmp, 26, 20); sig=tmp;
00144 */  
00145     return signature.verify(sig);
00146   }
00147 }

Generated on 5 May 2015 for HPCVIEWER by  doxygen 1.6.1