All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.wss4j.common.derivedKey.P_SHA1 Maven / Gradle / Ivy

There is a newer version: 3.0.3
Show newest version
/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package org.apache.wss4j.common.derivedKey;

/**
 *
 
 P_SHA-1 DEFINITION
 ==================
 P_SHA-1(secret, seed) =
 HMAC_SHA-1(secret, A(1) + seed) +
 HMAC_SHA-1(secret, A(2) + seed) +
 HMAC_SHA-1(secret, A(3) + seed) + ...
 Where + indicates concatenation.
 
A() is defined as: A(0) = seed A(i) = HMAC_SHA-1(secret, A(i-1))
Source : RFC 2246 - The TLS Protocol Version 1.0 Section 5. HMAC and the pseudorandom function
*/ import javax.crypto.Mac; import javax.crypto.spec.SecretKeySpec; import java.security.InvalidKeyException; import java.security.NoSuchAlgorithmException; public class P_SHA1 implements DerivationAlgorithm { @Override public byte[] createKey(byte[] secret, byte[] seed, int offset, long length) throws ConversationException { try { Mac mac = Mac.getInstance("HmacSHA1"); byte[] tempBytes = P_hash(secret, seed, mac, offset + (int) length); byte[] key = new byte[(int) length]; System.arraycopy(tempBytes, offset, key, 0, key.length); return key; } catch (NoSuchAlgorithmException e) { throw new ConversationException("errorInKeyDerivation", null, e); } catch (InvalidKeyException e) { throw new ConversationException("errorInKeyDerivation", null, e); } } /** * P_hash as defined in RFC 2246 for TLS. * * @param secret is the key for the HMAC * @param seed the seed value to start the generation - A(0) * @param mac the HMAC algorithm * @param required number of bytes to generate * @return a byte array that contains a secret key * @throws InvalidKeyException */ private static byte[] P_hash(byte[] secret, byte[] seed, Mac mac, int required) throws InvalidKeyException { byte[] out = new byte[required]; int offset = 0, tocpy; byte[] a = seed; // a(0) is the seed byte[] tmp; SecretKeySpec key = new SecretKeySpec(secret, "HMACSHA1"); mac.init(key); while (required > 0) { mac.update(a); a = mac.doFinal(); mac.update(a); mac.update(seed); tmp = mac.doFinal(); tocpy = Math.min(required, tmp.length); System.arraycopy(tmp, 0, out, offset, tocpy); offset += tocpy; required -= tocpy; } return out; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy