Showing the changes to java/pkg/sun/security/ec/ECDHKeyAgreement.java (1 of 18352 files). Show all
Unified · Split
| @@ -0,0 +1,175 @@ | |||
| 1 | /* | ||
| 2 | * Copyright (c) 2009, 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. Oracle designates this | ||
| 8 | * particular file as subject to the "Classpath" exception as provided | ||
| 9 | * by Oracle in the LICENSE file that accompanied this code. | ||
| 10 | * | ||
| 11 | * This code is distributed in the hope that it will be useful, but WITHOUT | ||
| 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
| 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | ||
| 14 | * version 2 for more details (a copy is included in the LICENSE file that | ||
| 15 | * accompanied this code). | ||
| 16 | * | ||
| 17 | * You should have received a copy of the GNU General Public License version | ||
| 18 | * 2 along with this work; if not, write to the Free Software Foundation, | ||
| 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. | ||
| 20 | * | ||
| 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA | ||
| 22 | * or visit www.oracle.com if you need additional information or have any | ||
| 23 | * questions. | ||
| 24 | */ | ||
| 25 | |||
| 26 | package sun.security.ec; | ||
| 27 | |||
| 28 | import java.security.*; | ||
| 29 | import java.security.interfaces.*; | ||
| 30 | import java.security.spec.*; | ||
| 31 | |||
| 32 | import javax.crypto.*; | ||
| 33 | import javax.crypto.spec.*; | ||
| 34 | |||
| 35 | /** | ||
| 36 | * KeyAgreement implementation for ECDH. | ||
| 37 | * | ||
| 38 | * @since 1.7 | ||
| 39 | */ | ||
| 40 | public final class ECDHKeyAgreement extends KeyAgreementSpi { | ||
| 41 | |||
| 42 | // private key, if initialized | ||
| 43 | private ECPrivateKey privateKey; | ||
| 44 | |||
| 45 | // encoded public point, non-null between doPhase() & generateSecret() only | ||
| 46 | private byte[] publicValue; | ||
| 47 | |||
| 48 | // length of the secret to be derived | ||
| 49 | private int secretLen; | ||
| 50 | |||
| 51 | /** | ||
| 52 | * Constructs a new ECDHKeyAgreement. | ||
| 53 | */ | ||
| 54 | public ECDHKeyAgreement() { | ||
| 55 | } | ||
| 56 | |||
| 57 | // see JCE spec | ||
| 58 | @Override | ||
| 59 | protected void engineInit(Key key, SecureRandom random) | ||
| 60 | throws InvalidKeyException { | ||
| 61 | if (!(key instanceof PrivateKey)) { | ||
| 62 | throw new InvalidKeyException | ||
| 63 | ("Key must be instance of PrivateKey"); | ||
| 64 | } | ||
| 65 | privateKey = (ECPrivateKey) ECKeyFactory.toECKey(key); | ||
| 66 | publicValue = null; | ||
| 67 | } | ||
| 68 | |||
| 69 | // see JCE spec | ||
| 70 | @Override | ||
| 71 | protected void engineInit(Key key, AlgorithmParameterSpec params, | ||
| 72 | SecureRandom random) throws InvalidKeyException, | ||
| 73 | InvalidAlgorithmParameterException { | ||
| 74 | if (params != null) { | ||
| 75 | throw new InvalidAlgorithmParameterException | ||
| 76 | ("Parameters not supported"); | ||
| 77 | } | ||
| 78 | engineInit(key, random); | ||
| 79 | } | ||
| 80 | |||
| 81 | // see JCE spec | ||
| 82 | @Override | ||
| 83 | protected Key engineDoPhase(Key key, boolean lastPhase) | ||
| 84 | throws InvalidKeyException, IllegalStateException { | ||
| 85 | if (privateKey == null) { | ||
| 86 | throw new IllegalStateException("Not initialized"); | ||
| 87 | } | ||
| 88 | if (publicValue != null) { | ||
| 89 | throw new IllegalStateException("Phase already executed"); | ||
| 90 | } | ||
| 91 | if (!lastPhase) { | ||
| 92 | throw new IllegalStateException | ||
| 93 | ("Only two party agreement supported, lastPhase must be true"); | ||
| 94 | } | ||
| 95 | if (!(key instanceof ECPublicKey)) { | ||
| 96 | throw new InvalidKeyException | ||
| 97 | ("Key must be a PublicKey with algorithm EC"); | ||
| 98 | } | ||
| 99 | |||
| 100 | ECPublicKey ecKey = (ECPublicKey)key; | ||
| 101 | ECParameterSpec params = ecKey.getParams(); | ||
| 102 | |||
| 103 | if (ecKey instanceof ECPublicKeyImpl) { | ||
| 104 | publicValue = ((ECPublicKeyImpl)ecKey).getEncodedPublicValue(); | ||
| 105 | } else { // instanceof ECPublicKey | ||
| 106 | publicValue = | ||
| 107 | ECParameters.encodePoint(ecKey.getW(), params.getCurve()); | ||
| 108 | } | ||
| 109 | int keyLenBits = params.getCurve().getField().getFieldSize(); | ||
| 110 | secretLen = (keyLenBits + 7) >> 3; | ||
| 111 | |||
| 112 | return null; | ||
| 113 | } | ||
| 114 | |||
| 115 | // see JCE spec | ||
| 116 | @Override | ||
| 117 | protected byte[] engineGenerateSecret() throws IllegalStateException { | ||
| 118 | if ((privateKey == null) || (publicValue == null)) { | ||
| 119 | throw new IllegalStateException("Not initialized correctly"); | ||
| 120 | } | ||
| 121 | |||
| 122 | byte[] s = privateKey.getS().toByteArray(); | ||
| 123 | byte[] encodedParams = | ||
| 124 | ECParameters.encodeParameters(privateKey.getParams()); // DER OID | ||
| 125 | |||
| 126 | try { | ||
| 127 | |||
| 128 | return deriveKey(s, publicValue, encodedParams); | ||
| 129 | |||
| 130 | } catch (GeneralSecurityException e) { | ||
| 131 | throw new ProviderException("Could not derive key", e); | ||
| 132 | } | ||
| 133 | |||
| 134 | } | ||
| 135 | |||
| 136 | // see JCE spec | ||
| 137 | @Override | ||
| 138 | protected int engineGenerateSecret(byte[] sharedSecret, int | ||
| 139 | offset) throws IllegalStateException, ShortBufferException { | ||
| 140 | if (offset + secretLen > sharedSecret.length) { | ||
| 141 | throw new ShortBufferException("Need " + secretLen | ||
| 142 | + " bytes, only " + (sharedSecret.length - offset) + " available"); | ||
| 143 | } | ||
| 144 | byte[] secret = engineGenerateSecret(); | ||
| 145 | System.arraycopy(secret, 0, sharedSecret, offset, secret.length); | ||
| 146 | return secret.length; | ||
| 147 | } | ||
| 148 | |||
| 149 | // see JCE spec | ||
| 150 | @Override | ||
| 151 | protected SecretKey engineGenerateSecret(String algorithm) | ||
| 152 | throws IllegalStateException, NoSuchAlgorithmException, | ||
| 153 | InvalidKeyException { | ||
| 154 | if (algorithm == null) { | ||
| 155 | throw new NoSuchAlgorithmException("Algorithm must not be null"); | ||
| 156 | } | ||
| 157 | if (!(algorithm.equals("TlsPremasterSecret"))) { | ||
| 158 | throw new NoSuchAlgorithmException | ||
| 159 | ("Only supported for algorithm TlsPremasterSecret"); | ||
| 160 | } | ||
| 161 | return new SecretKeySpec(engineGenerateSecret(), "TlsPremasterSecret"); | ||
| 162 | } | ||
| 163 | |||
| 164 | /** | ||
| 165 | * Generates a secret key using the public and private keys. | ||
| 166 | * | ||
| 167 | * @param s the private key's S value. | ||
| 168 | * @param w the public key's W point (in uncompressed form). | ||
| 169 | * @param encodedParams the curve's DER encoded object identifier. | ||
| 170 | * | ||
| 171 | * @return byte[] the secret key. | ||
| 172 | */ | ||
| 173 | private static native byte[] deriveKey(byte[] s, byte[] w, | ||
| 174 | byte[] encodedParams) throws GeneralSecurityException; | ||
| 175 | } | ||