1 /**
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
20 package org.apache.wss4j.common.derivedKey;
21
22 /**
23 *
24 <pre>
25 P_SHA-1 DEFINITION
26 ==================
27 <b>P_SHA-1(secret, seed)</b> =
28 HMAC_SHA-1(secret, A(1) + seed) +
29 HMAC_SHA-1(secret, A(2) + seed) +
30 HMAC_SHA-1(secret, A(3) + seed) + ...
31 <i>Where + indicates concatenation.</i>
32 <br>
33 A() is defined as:
34 A(0) = seed
35 A(i) = HMAC_SHA-1(secret, A(i-1))
36 <br>
37 <i>Source : RFC 2246 - The TLS Protocol Version 1.0
38 Section 5. HMAC and the pseudorandom function</i>
39 </pre>
40 */
41
42 import javax.crypto.Mac;
43 import javax.crypto.spec.SecretKeySpec;
44 import javax.security.auth.DestroyFailedException;
45
46 import org.apache.wss4j.common.ext.WSSecurityException;
47
48 import java.security.InvalidKeyException;
49 import java.security.NoSuchAlgorithmException;
50
51 public class P_SHA1 implements DerivationAlgorithm {
52
53 private static final org.slf4j.Logger LOG =
54 org.slf4j.LoggerFactory.getLogger(P_SHA1.class);
55
56 @Override
57 public byte[] createKey(byte[] secret, byte[] seed, int offset, long length)
58 throws WSSecurityException {
59
60 try {
61 Mac mac = Mac.getInstance("HmacSHA1");
62
63 byte[] tempBytes = pHash(secret, seed, mac, offset + (int) length);
64
65 byte[] key = new byte[(int) length];
66
67 System.arraycopy(tempBytes, offset, key, 0, key.length);
68
69 return key;
70 } catch (NoSuchAlgorithmException | InvalidKeyException e) {
71 throw new WSSecurityException(WSSecurityException.ErrorCode.FAILURE, e, "errorInKeyDerivation");
72 }
73 }
74
75 /**
76 * P_hash as defined in RFC 2246 for TLS.
77 *
78 * @param secret is the key for the HMAC
79 * @param seed the seed value to start the generation - A(0)
80 * @param mac the HMAC algorithm
81 * @param required number of bytes to generate
82 * @return a byte array that contains a secret key
83 * @throws InvalidKeyException
84 */
85 private static byte[] pHash(byte[] secret, byte[] seed, Mac mac, int required)
86 throws InvalidKeyException {
87
88 byte[] out = new byte[required];
89 int offset = 0, tocpy;
90 byte[] a = seed; // a(0) is the seed
91 byte[] tmp;
92
93 SecretKeySpec key = new SecretKeySpec(secret, "HMACSHA1");
94 mac.init(key);
95
96 int bytesRequired = required;
97 while (bytesRequired > 0) {
98 mac.update(a);
99 a = mac.doFinal();
100 mac.update(a);
101 mac.update(seed);
102 tmp = mac.doFinal();
103 tocpy = Math.min(bytesRequired, tmp.length);
104 System.arraycopy(tmp, 0, out, offset, tocpy);
105 offset += tocpy;
106 bytesRequired -= tocpy;
107 }
108
109 try {
110 key.destroy();
111 } catch (DestroyFailedException e) {
112 LOG.debug("Error destroying key: {}", e.getMessage());
113 }
114 return out;
115 }
116 }