aboutsummaryrefslogtreecommitdiffstats
path: root/security-utils/src/main/java/com/yahoo/security/hpke/HkdfSha256.java
blob: a4511a2b804103f29d3eb7ca94c74e306b5c34f1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.security.hpke;

import com.yahoo.security.HKDF;

/**
 * KDF implementation using HKDF-SHA256
 *
 * @author vekterli
 */
final class HkdfSha256 implements Kdf {

    private static final HkdfSha256 INSTANCE = new HkdfSha256();

    public static HkdfSha256 getInstance() { return INSTANCE; }

    @Override
    public byte[] extract(byte[] salt, byte[] labeledIkm) {
        return ((salt.length != 0) ? HKDF.extractedFrom(salt, labeledIkm)
                                   : HKDF.unsaltedExtractedFrom(labeledIkm))
                .pseudoRandomKey();
    }

    @Override
    public byte[] expand(byte[] prk, byte[] info, int nBytesToExpand) {
        return HKDF.ofPseudoRandomKey(prk).expand(nBytesToExpand, info);
    }

    @Override public short nH()    { return 32; } // HMAC-SHA256 output size
    @Override public short kdfId() { return 0x0001; } // HKDF-SHA256

}