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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [libjava/] [classpath/] [gnu/] [java/] [security/] [jce/] [prng/] [SecureRandomAdapter.java] - Blame information for rev 769

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 769 jeremybenn
/* SecureRandomAdapter.java --
2
   Copyright (C) 2001, 2002, 2003, 2006 Free Software Foundation, Inc.
3
 
4
This file is a 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 of the License, or (at
9
your option) 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; if not, write to the Free Software
18
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
19
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.java.security.jce.prng;
40
 
41
import gnu.java.security.action.GetSecurityPropertyAction;
42
import gnu.classpath.SystemProperties;
43
import gnu.java.security.prng.LimitReachedException;
44
import gnu.java.security.prng.MDGenerator;
45
 
46
import java.security.AccessController;
47
import java.security.SecureRandom;
48
import java.security.SecureRandomSpi;
49
 
50
import java.util.Collections;
51
import java.util.logging.Level;
52
import java.util.logging.Logger;
53
 
54
import java.io.InputStream;
55
import java.io.IOException;
56
 
57
import java.net.MalformedURLException;
58
import java.net.URL;
59
 
60
/**
61
 * <p>The implementation of a generic {@link java.security.SecureRandom} adapter
62
 * class to wrap gnu.crypto prng instances based on Message Digest algorithms.</p>
63
 *
64
 * <p>This class defines the <i>Service Provider Interface</i> (<b>SPI</b>) for
65
 * the {@link java.security.SecureRandom} class, which provides the
66
 * functionality of a cryptographically strong pseudo-random number generator.</p>
67
 *
68
 * <p>All the abstract methods in the {@link SecureRandomSpi} class are
69
 * implemented by this class and all its sub-classes.</p>
70
 */
71
public abstract class SecureRandomAdapter
72
  extends SecureRandomSpi
73
{
74
 
75
  private boolean isSeeded = false;
76
 
77
  /** Our underlying prng instance. */
78
  private MDGenerator adaptee = new MDGenerator();
79
 
80
  /** The name of the message digest algorithm used by the adaptee. */
81
  private String mdName;
82
 
83
  private static final Logger logger =
84
    Logger.getLogger(SecureRandom.class.getName());
85
 
86
  private static final String SECURERANDOM_SOURCE = "securerandom.source";
87
  private static final String JAVA_SECURITY_EGD = "java.security.egd";
88
 
89
  /**
90
   * <p>Trivial protected constructor.</p>
91
   *
92
   * @param mdName the canonical name of the underlying hash algorithm.
93
   */
94
  protected SecureRandomAdapter(String mdName)
95
  {
96
    super();
97
 
98
    this.mdName = mdName;
99
    adaptee.init (Collections.singletonMap (MDGenerator.MD_NAME, mdName));
100
  }
101
 
102
  public static final byte[] getSeed(int numBytes)
103
  {
104
    URL sourceUrl = null;
105
    String urlStr = null;
106
 
107
    byte[] buffer = new byte[numBytes];
108
 
109
    GetSecurityPropertyAction action =
110
      new GetSecurityPropertyAction(SECURERANDOM_SOURCE);
111
    try
112
      {
113
        urlStr = (String) AccessController.doPrivileged(action);
114
        if (urlStr != null)
115
          sourceUrl = new URL(urlStr);
116
      }
117
    catch (MalformedURLException ignored)
118
      {
119
        logger.log(Level.WARNING,
120
                   SECURERANDOM_SOURCE + " property is malformed: {0}",
121
                   urlStr);
122
      }
123
 
124
    if (sourceUrl == null)
125
      {
126
        try
127
          {
128
            urlStr = SystemProperties.getProperty(JAVA_SECURITY_EGD);
129
            if (urlStr != null)
130
              sourceUrl = new URL(urlStr);
131
          }
132
        catch (MalformedURLException mue)
133
          {
134
            logger.log(Level.WARNING,
135
                       JAVA_SECURITY_EGD + " property is malformed: {0}",
136
                       urlStr);
137
          }
138
      }
139
 
140
    if (sourceUrl != null)
141
      {
142
        try
143
          {
144
            InputStream in = sourceUrl.openStream();
145
            in.read(buffer);
146
            return buffer;
147
          }
148
        catch (IOException ioe)
149
          {
150
            logger.log(Level.FINE, "error reading random bytes", ioe);
151
          }
152
      }
153
 
154
    // If we get here, we did not get any seed from a property URL.
155
    VMSecureRandom.generateSeed(buffer, 0, buffer.length);
156
    return buffer;
157
  }
158
 
159
  public byte[] engineGenerateSeed(int numBytes)
160
  {
161
    return getSeed(numBytes);
162
  }
163
 
164
  public void engineNextBytes(byte[] bytes)
165
  {
166
    if (!isSeeded)
167
      {
168
        engineSetSeed(engineGenerateSeed(32));
169
      }
170
    try
171
      {
172
        adaptee.nextBytes(bytes, 0, bytes.length);
173
      }
174
    catch (LimitReachedException ignored)
175
      {
176
      }
177
  }
178
 
179
  public void engineSetSeed(byte[] seed)
180
  {
181
    adaptee.addRandomBytes (seed);
182
    isSeeded = true;
183
  }
184
}

powered by: WebSVN 2.1.0

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