OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [libjava/] [classpath/] [gnu/] [javax/] [crypto/] [jce/] [sig/] [DHParameters.java] - Blame information for rev 769

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 769 jeremybenn
/* DHParameters.java -- DH parameters DAO
2
   Copyright (C) 2006 Free Software Foundation, Inc.
3
 
4
This file is part of GNU Classpath.
5
 
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, or (at your option)
9
any later version.
10
 
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.
15
 
16
You should have received a copy of the GNU General Public License
17
along with GNU Classpath; see the file COPYING.  If not, write to the
18
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19
02110-1301 USA.
20
 
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.
25
 
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. */
37
 
38
 
39
package gnu.javax.crypto.jce.sig;
40
 
41
import gnu.java.lang.CPStringBuilder;
42
 
43
import gnu.java.security.Registry;
44
import gnu.java.security.der.DER;
45
import gnu.java.security.der.DERReader;
46
import gnu.java.security.der.DERValue;
47
import gnu.java.security.der.DERWriter;
48
import gnu.java.security.util.DerUtil;
49
 
50
import java.io.ByteArrayOutputStream;
51
import java.io.IOException;
52
import java.math.BigInteger;
53
import java.security.AlgorithmParametersSpi;
54
import java.security.spec.AlgorithmParameterSpec;
55
import java.security.spec.InvalidParameterSpecException;
56
import java.util.ArrayList;
57
 
58
import javax.crypto.spec.DHGenParameterSpec;
59
import javax.crypto.spec.DHParameterSpec;
60
 
61
/**
62
 * A JCE-specific Data Access Object (DAO) for DH parameters.
63
 */
64
public class DHParameters
65
    extends AlgorithmParametersSpi
66
{
67
  /** The prime public modulus. */
68
  private BigInteger p;
69
 
70
  /** The generator. */
71
  private BigInteger g;
72
 
73
  /** A prime factor of p-1. */
74
  private BigInteger q;
75
 
76
  /** The (private) random exponent's size (in bits). */
77
  private int l;
78
 
79
  // default 0-arguments constructor
80
 
81
  protected void engineInit(AlgorithmParameterSpec spec)
82
      throws InvalidParameterSpecException
83
  {
84
    if (! (spec instanceof DHParameterSpec))
85
      throw new InvalidParameterSpecException("Wrong AlgorithmParameterSpec type: "
86
                                              + spec.getClass().getName());
87
    DHParameterSpec dhSpec = (DHParameterSpec) spec;
88
    p = dhSpec.getP();
89
    g = dhSpec.getG();
90
    l = dhSpec.getL();
91
  }
92
 
93
  /**
94
   * Decodes the set of DH parameters as per RFC-2459; i.e. the DER-encoded
95
   * form of the following ASN.1 construct:
96
   *
97
   * <pre>
98
   *   DhParams ::= SEQUENCE {
99
   *     p  INTEGER, -- odd prime, p=jq +1
100
   *     g  INTEGER, -- generator, g
101
   *     q  INTEGER  -- factor of p-1
102
   *   }
103
   * </pre>
104
   */
105
  protected void engineInit(byte[] params) throws IOException
106
  {
107
    DERReader der = new DERReader(params);
108
 
109
    DERValue derParams = der.read();
110
    DerUtil.checkIsConstructed(derParams, "Wrong DH Parameters field");
111
 
112
    DERValue val = der.read();
113
    DerUtil.checkIsBigInteger(val, "Wrong P field");
114
    p = (BigInteger) val.getValue();
115
    val = der.read();
116
    DerUtil.checkIsBigInteger(val, "Wrong G field");
117
    g = (BigInteger) val.getValue();
118
    val = der.read();
119
    DerUtil.checkIsBigInteger(val, "Wrong Q field");
120
    q = (BigInteger) val.getValue();
121
    l = q.bitLength();
122
  }
123
 
124
  protected void engineInit(byte[] params, String format) throws IOException
125
  {
126
    if (format != null)
127
      {
128
        format = format.trim();
129
        if (format.length() == 0)
130
          throw new IOException("Format MUST NOT be an empty string");
131
 
132
        if (! format.equalsIgnoreCase(Registry.ASN1_ENCODING_SHORT_NAME))
133
          throw new IOException("Unknown or unsupported format: " + format);
134
      }
135
 
136
    engineInit(params);
137
  }
138
 
139
  protected AlgorithmParameterSpec engineGetParameterSpec(Class paramSpec)
140
      throws InvalidParameterSpecException
141
  {
142
    if (paramSpec.isAssignableFrom(DHParameterSpec.class))
143
      return new DHParameterSpec(p, g, l);
144
 
145
    if (paramSpec.isAssignableFrom(DHGenParameterSpec.class))
146
      return new DHGenParameterSpec(p.bitLength(), l);
147
 
148
    throw new InvalidParameterSpecException("Wrong AlgorithmParameterSpec type: "
149
                                            + paramSpec.getName());
150
  }
151
 
152
  /**
153
   * Encodes the set of DH parameters as per RFC-2459; i.e. as the DER-encoded
154
   * form of the following ASN.1 construct:
155
   *
156
   * <pre>
157
   *   DhParams ::= SEQUENCE {
158
   *     p  INTEGER, -- odd prime, p=jq +1
159
   *     g  INTEGER, -- generator, g
160
   *     q  INTEGER  -- factor of p-1
161
   *   }
162
   * </pre>
163
   */
164
  protected byte[] engineGetEncoded() throws IOException
165
  {
166
    DERValue derP = new DERValue(DER.INTEGER, p);
167
    DERValue derG = new DERValue(DER.INTEGER, g);
168
    DERValue derQ = new DERValue(DER.INTEGER, q);
169
 
170
    ArrayList params = new ArrayList(3);
171
    params.add(derP);
172
    params.add(derG);
173
    params.add(derQ);
174
    DERValue derParams = new DERValue(DER.CONSTRUCTED | DER.SEQUENCE, params);
175
 
176
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
177
    DERWriter.write(baos, derParams);
178
    byte[] result = baos.toByteArray();
179
 
180
    return result;
181
  }
182
 
183
  protected byte[] engineGetEncoded(String format) throws IOException
184
  {
185
    if (format != null)
186
      {
187
        format = format.trim();
188
        if (format.length() == 0)
189
          throw new IOException("Format MUST NOT be an empty string");
190
 
191
        if (! format.equalsIgnoreCase(Registry.ASN1_ENCODING_SHORT_NAME))
192
          throw new IOException("Unknown or unsupported format: " + format);
193
      }
194
 
195
    return engineGetEncoded();
196
  }
197
 
198
  protected String engineToString()
199
  {
200
    CPStringBuilder sb = new CPStringBuilder("p=");
201
    if (p == null)
202
      sb.append("???");
203
    else
204
      sb.append("0x").append(p.toString(16));
205
 
206
    sb.append(", g=");
207
    if (g == null)
208
      sb.append("???");
209
    else
210
      sb.append("0x").append(g.toString(16));
211
 
212
    sb.append(", q=");
213
    if (q == null)
214
      sb.append("???");
215
    else
216
      sb.append("0x").append(q.toString(16));
217
 
218
    sb.append(", l=").append(l);
219
 
220
    return sb.toString();
221
  }
222
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.