1/*
2 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24package sun.security.provider;
25
26import java.security.SecureRandomParameters;
27import java.security.SecureRandomSpi;
28
29/**
30 * Read ../../../../SpecTest.java for details.
31 */
32public class S extends SecureRandomSpi {
33
34    protected AbstractDrbg impl;
35
36    // This is not a DRBG.
37    public static class S1 extends SecureRandomSpi {
38        @Override
39        protected void engineSetSeed(byte[] seed) {
40        }
41
42        @Override
43        protected void engineNextBytes(byte[] bytes) {
44        }
45
46        @Override
47        protected byte[] engineGenerateSeed(int numBytes) {
48            return new byte[numBytes];
49        }
50    }
51
52    // This is a weak DRBG. maximum strength is 128 and does
53    // not support prediction resistance or reseed.
54    public static class S2 extends S {
55        public S2(SecureRandomParameters params) {
56            impl = new Impl2(params);
57        }
58    }
59
60    // This is a strong DRBG.
61    public static class S3 extends S {
62        public S3(SecureRandomParameters params) {
63            impl = new Impl3(params);
64        }
65    }
66
67    // AbstractDrbg Implementations
68
69    static class Impl3 extends AbstractDrbg {
70
71        public Impl3(SecureRandomParameters params) {
72            supportPredictionResistance = true;
73            supportReseeding = true;
74            highestSupportedSecurityStrength = 192;
75            mechName = "S3";
76            algorithm = "SQUEEZE";
77            configure(params);
78        }
79
80        protected void chooseAlgorithmAndStrength() {
81            if (requestedInstantiationSecurityStrength < 0) {
82                securityStrength = DEFAULT_STRENGTH;
83            } else {
84                securityStrength = requestedInstantiationSecurityStrength;
85            }
86            minLength = securityStrength / 8;
87            maxAdditionalInputLength = maxPersonalizationStringLength = 100;
88        }
89
90        @Override
91        protected void initEngine() {
92        }
93
94        @Override
95        protected void instantiateAlgorithm(byte[] ei) {
96        }
97
98        @Override
99        protected void generateAlgorithm(byte[] result, byte[] additionalInput) {
100        }
101
102        @Override
103        protected void reseedAlgorithm(byte[] ei, byte[] additionalInput) {
104        }
105    }
106
107    static class Impl2 extends Impl3 {
108        public Impl2(SecureRandomParameters params) {
109            super(null);
110            mechName = "S2";
111            highestSupportedSecurityStrength = 128;
112            supportPredictionResistance = false;
113            supportReseeding = false;
114            configure(params);
115        }
116    }
117
118    // Overridden SecureRandomSpi methods
119
120    @Override
121    protected void engineSetSeed(byte[] seed) {
122        impl.engineSetSeed(seed);
123    }
124
125    @Override
126    protected void engineNextBytes(byte[] bytes) {
127        impl.engineNextBytes(bytes);
128    }
129
130    @Override
131    protected byte[] engineGenerateSeed(int numBytes) {
132        return impl.engineGenerateSeed(numBytes);
133    }
134
135    @Override
136    protected void engineNextBytes(
137            byte[] bytes, SecureRandomParameters params) {
138        impl.engineNextBytes(bytes, params);
139    }
140
141    @Override
142    protected void engineReseed(SecureRandomParameters params) {
143        impl.engineReseed(params);
144    }
145
146    @Override
147    protected SecureRandomParameters engineGetParameters() {
148        return impl.engineGetParameters();
149    }
150
151    @Override
152    public String toString() {
153        return impl.toString();
154    }
155}
156