1 /* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */
3 Copyright (c) 2002-2010 ymnk, JCraft,Inc. All rights reserved.
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
8 1. Redistributions of source code must retain the above copyright notice,
9 this list of conditions and the following disclaimer.
11 2. Redistributions in binary form must reproduce the above copyright
12 notice, this list of conditions and the following disclaimer in
13 the documentation and/or other materials provided with the distribution.
15 3. The names of the authors may not be used to endorse or promote products
16 derived from this software without specific prior written permission.
18 THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
19 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
21 INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
22 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
24 OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
27 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 package com.jcraft.jsch.jce;
32 import java.math.BigInteger;
33 import java.security.*;
34 import java.security.spec.*;
36 public class SignatureDSA implements com.jcraft.jsch.SignatureDSA{
38 java.security.Signature signature;
39 KeyFactory keyFactory;
41 public void init() throws Exception{
42 signature=java.security.Signature.getInstance("SHA1withDSA");
43 keyFactory=KeyFactory.getInstance("DSA");
45 public void setPubKey(byte[] y, byte[] p, byte[] q, byte[] g) throws Exception{
46 DSAPublicKeySpec dsaPubKeySpec =
47 new DSAPublicKeySpec(new BigInteger(y),
51 PublicKey pubKey=keyFactory.generatePublic(dsaPubKeySpec);
52 signature.initVerify(pubKey);
54 public void setPrvKey(byte[] x, byte[] p, byte[] q, byte[] g) throws Exception{
55 DSAPrivateKeySpec dsaPrivKeySpec =
56 new DSAPrivateKeySpec(new BigInteger(x),
60 PrivateKey prvKey = keyFactory.generatePrivate(dsaPrivKeySpec);
61 signature.initSign(prvKey);
63 public byte[] sign() throws Exception{
64 byte[] sig=signature.sign();
66 System.err.print("sign["+sig.length+"] ");
67 for(int i=0; i<sig.length;i++){
68 System.err.print(Integer.toHexString(sig[i]&0xff)+":");
70 System.err.println("");
73 // SEQUENCE::={ r INTEGER, s INTEGER }
76 len=sig[index++]&0xff;
77 //System.err.println("! len="+len);
78 byte[] r=new byte[len];
79 System.arraycopy(sig, index, r, 0, r.length);
81 len=sig[index++]&0xff;
82 //System.err.println("!! len="+len);
83 byte[] s=new byte[len];
84 System.arraycopy(sig, index, s, 0, s.length);
86 byte[] result=new byte[40];
88 // result must be 40 bytes, but length of r and s may not be 20 bytes
90 System.arraycopy(r, (r.length>20)?1:0,
91 result, (r.length>20)?0:20-r.length,
92 (r.length>20)?20:r.length);
93 System.arraycopy(s, (s.length>20)?1:0,
94 result, (s.length>20)?20:40-s.length,
95 (s.length>20)?20:s.length);
97 // System.arraycopy(sig, (sig[3]==20?4:5), result, 0, 20);
98 // System.arraycopy(sig, sig.length-20, result, 20, 20);
102 public void update(byte[] foo) throws Exception{
103 signature.update(foo);
105 public boolean verify(byte[] sig) throws Exception{
110 if(sig[0]==0 && sig[1]==0 && sig[2]==0){
111 j=((sig[i++]<<24)&0xff000000)|((sig[i++]<<16)&0x00ff0000)|
112 ((sig[i++]<<8)&0x0000ff00)|((sig[i++])&0x000000ff);
114 j=((sig[i++]<<24)&0xff000000)|((sig[i++]<<16)&0x00ff0000)|
115 ((sig[i++]<<8)&0x0000ff00)|((sig[i++])&0x000000ff);
117 System.arraycopy(sig, i, tmp, 0, j); sig=tmp;
121 int frst=((sig[0]&0x80)!=0?1:0);
122 int scnd=((sig[20]&0x80)!=0?1:0);
123 //System.err.println("frst: "+frst+", scnd: "+scnd);
125 int length=sig.length+6+frst+scnd;
126 tmp=new byte[length];
127 tmp[0]=(byte)0x30; tmp[1]=(byte)0x2c;
128 tmp[1]+=frst; tmp[1]+=scnd;
129 tmp[2]=(byte)0x02; tmp[3]=(byte)0x14;
131 System.arraycopy(sig, 0, tmp, 4+frst, 20);
132 tmp[4+tmp[3]]=(byte)0x02; tmp[5+tmp[3]]=(byte)0x14;
134 System.arraycopy(sig, 20, tmp, 6+tmp[3]+scnd, 20);
138 tmp=new byte[sig.length+6];
139 tmp[0]=(byte)0x30; tmp[1]=(byte)0x2c;
140 tmp[2]=(byte)0x02; tmp[3]=(byte)0x14;
141 System.arraycopy(sig, 0, tmp, 4, 20);
142 tmp[24]=(byte)0x02; tmp[25]=(byte)0x14;
143 System.arraycopy(sig, 20, tmp, 26, 20); sig=tmp;
145 return signature.verify(sig);