30 throw new InvalidParameterException(
"The content master key (CMK) is null");
32 if (blockEncryptionAlgorithm == null) {
33 throw new InvalidParameterException(
"The block encryption algorithm is null");
35 if (blockEncryptionAlgorithm != BlockEncryptionAlgorithm.A128CBC_PLUS_HS256
36 && blockEncryptionAlgorithm != BlockEncryptionAlgorithm.A256CBC_PLUS_HS512) {
37 throw new InvalidParameterException(
"The block encryption algorithm is not supported");
40 byte[] round1 = Base64Util.unsignedToBytes(
new int[]{0, 0, 0, 1});
41 byte[] outputBitSize = null;
42 if (blockEncryptionAlgorithm != BlockEncryptionAlgorithm.A128CBC_PLUS_HS256) {
43 outputBitSize = Base64Util.unsignedToBytes(
new int[]{0, 0, 0, 128});
45 outputBitSize = Base64Util.unsignedToBytes(
new int[]{0, 0, 1, 0});
47 byte[] encValue = blockEncryptionAlgorithm.getName().getBytes(Util.UTF8_STRING_ENCODING);
48 byte[] epu = Base64Util.unsignedToBytes(
new int[]{0, 0, 0, 0});
49 byte[] epv = Base64Util.unsignedToBytes(
new int[]{0, 0, 0, 0});
50 byte[] label =
"Encryption".getBytes(Util.UTF8_STRING_ENCODING);
51 byte[] round1Input = ArrayUtils.addAll(round1, cmk);
52 round1Input = ArrayUtils.addAll(round1Input, outputBitSize);
53 round1Input = ArrayUtils.addAll(round1Input, encValue);
54 round1Input = ArrayUtils.addAll(round1Input, epu);
55 round1Input = ArrayUtils.addAll(round1Input, epv);
56 round1Input = ArrayUtils.addAll(round1Input, label);
58 MessageDigest mda = MessageDigest.getInstance(blockEncryptionAlgorithm.getMessageDiggestAlgorithm(),
"BC");
59 byte[] round1Hash = mda.digest(round1Input);
60 byte[] cek = Arrays.copyOf(round1Hash, blockEncryptionAlgorithm.getCekLength() / 8);