52 }
53
54 XDHKeyAgreement(AlgorithmParameterSpec paramSpec) {
55 lockedParams = XECParameters.get(ProviderException::new, paramSpec);
56 }
57
58 @Override
59 protected void engineInit(Key key, SecureRandom random)
60 throws InvalidKeyException {
61
62 initImpl(key);
63 }
64
65 @Override
66 protected void engineInit(Key key, final AlgorithmParameterSpec params,
67 SecureRandom random) throws InvalidKeyException,
68 InvalidAlgorithmParameterException {
69
70 initImpl(key);
71
72 // the private key parameters must match params
73 XECParameters xecParams = XECParameters.get(
74 InvalidAlgorithmParameterException::new, params);
75 if (!xecParams.oidEquals(this.ops.getParameters())) {
76 throw new InvalidKeyException(
77 "Incorrect private key parameters"
78 );
79 }
80 }
81
82 private
83 <T extends Throwable>
84 void checkLockedParams(Function<String, T> exception,
85 XECParameters params) throws T {
86
87 if (lockedParams != null && lockedParams != params) {
88 throw exception.apply("Parameters must be " +
89 lockedParams.getName());
90 }
91 }
92
93 private void initImpl(Key key) throws InvalidKeyException {
94
95 if (!(key instanceof XECPrivateKey)) {
96 throw new InvalidKeyException
97 ("Unsupported key type");
98 }
99 XECPrivateKey privateKey = (XECPrivateKey) key;
100 XECParameters xecParams = XECParameters.get(
154 }
155
156 /*
157 * Constant-time check for an all-zero array
158 */
159 private boolean allZero(byte[] arr) {
160 byte orValue = (byte) 0;
161 for (int i = 0; i < arr.length; i++) {
162 orValue |= arr[i];
163 }
164
165 return orValue == (byte) 0;
166 }
167
168 @Override
169 protected byte[] engineGenerateSecret() throws IllegalStateException {
170 if (secret == null) {
171 throw new IllegalStateException("Not initialized correctly");
172 }
173
174 return secret.clone();
175 }
176
177 @Override
178 protected int engineGenerateSecret(byte[] sharedSecret, int offset)
179 throws IllegalStateException, ShortBufferException {
180
181 if (secret == null) {
182 throw new IllegalStateException("Not initialized correctly");
183 }
184 int secretLen = this.secret.length;
185 if (offset + secretLen > sharedSecret.length) {
186 throw new ShortBufferException("Need " + secretLen
187 + " bytes, only " + (sharedSecret.length - offset)
188 + " available");
189 }
190
191 System.arraycopy(this.secret, 0, sharedSecret, offset, secretLen);
192 return secret.length;
193 }
194
195 @Override
196 protected SecretKey engineGenerateSecret(String algorithm)
197 throws IllegalStateException, NoSuchAlgorithmException,
198 InvalidKeyException {
199
200 throw new NoSuchAlgorithmException("Not supported");
201 }
202
203 static class X25519 extends XDHKeyAgreement {
204
205 public X25519() {
206 super(NamedParameterSpec.X25519);
207 }
208 }
209
210 static class X448 extends XDHKeyAgreement {
211
212 public X448() {
|
52 }
53
54 XDHKeyAgreement(AlgorithmParameterSpec paramSpec) {
55 lockedParams = XECParameters.get(ProviderException::new, paramSpec);
56 }
57
58 @Override
59 protected void engineInit(Key key, SecureRandom random)
60 throws InvalidKeyException {
61
62 initImpl(key);
63 }
64
65 @Override
66 protected void engineInit(Key key, final AlgorithmParameterSpec params,
67 SecureRandom random) throws InvalidKeyException,
68 InvalidAlgorithmParameterException {
69
70 initImpl(key);
71
72 // the private key parameters must match params, if present
73 if (params != null) {
74 XECParameters xecParams = XECParameters.get(
75 InvalidAlgorithmParameterException::new, params);
76 if (!xecParams.oidEquals(this.ops.getParameters())) {
77 throw new InvalidKeyException(
78 "Incorrect private key parameters"
79 );
80 }
81 }
82 }
83
84 private
85 <T extends Throwable>
86 void checkLockedParams(Function<String, T> exception,
87 XECParameters params) throws T {
88
89 if (lockedParams != null && lockedParams != params) {
90 throw exception.apply("Parameters must be " +
91 lockedParams.getName());
92 }
93 }
94
95 private void initImpl(Key key) throws InvalidKeyException {
96
97 if (!(key instanceof XECPrivateKey)) {
98 throw new InvalidKeyException
99 ("Unsupported key type");
100 }
101 XECPrivateKey privateKey = (XECPrivateKey) key;
102 XECParameters xecParams = XECParameters.get(
156 }
157
158 /*
159 * Constant-time check for an all-zero array
160 */
161 private boolean allZero(byte[] arr) {
162 byte orValue = (byte) 0;
163 for (int i = 0; i < arr.length; i++) {
164 orValue |= arr[i];
165 }
166
167 return orValue == (byte) 0;
168 }
169
170 @Override
171 protected byte[] engineGenerateSecret() throws IllegalStateException {
172 if (secret == null) {
173 throw new IllegalStateException("Not initialized correctly");
174 }
175
176 byte[] result = secret;
177 secret = null;
178 return result;
179 }
180
181 @Override
182 protected int engineGenerateSecret(byte[] sharedSecret, int offset)
183 throws IllegalStateException, ShortBufferException {
184
185 if (secret == null) {
186 throw new IllegalStateException("Not initialized correctly");
187 }
188 int secretLen = this.secret.length;
189 if (offset + secretLen > sharedSecret.length) {
190 throw new ShortBufferException("Need " + secretLen
191 + " bytes, only " + (sharedSecret.length - offset)
192 + " available");
193 }
194
195 System.arraycopy(this.secret, 0, sharedSecret, offset, secretLen);
196 secret = null;
197 return secretLen;
198 }
199
200 @Override
201 protected SecretKey engineGenerateSecret(String algorithm)
202 throws IllegalStateException, NoSuchAlgorithmException,
203 InvalidKeyException {
204
205 throw new NoSuchAlgorithmException("Not supported");
206 }
207
208 static class X25519 extends XDHKeyAgreement {
209
210 public X25519() {
211 super(NamedParameterSpec.X25519);
212 }
213 }
214
215 static class X448 extends XDHKeyAgreement {
216
217 public X448() {
|