Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 24 additions & 7 deletions test/jdk/sun/security/pkcs11/Cipher/KeyWrap/NISTWrapKAT.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021, 2024, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2021, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -29,17 +29,25 @@
* @summary Verify that the AES-Key-Wrap and AES-Key-Wrap-Pad ciphers
* work as expected using NIST test vectors.
*/
import jtreg.SkippedException;

import java.security.Key;
import java.security.AlgorithmParameters;
import java.security.Provider;
import javax.crypto.*;
import javax.crypto.spec.*;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.util.ArrayList;
import java.util.Arrays;
import java.math.BigInteger;
import java.util.List;

// adapted from com/sun/crypto/provider/Cipher/KeyWrap/NISTWrapKAT.java
public class NISTWrapKAT extends PKCS11Test {

private static final List<String> skippedAlgoList = new ArrayList<>();

private static final String KEK =
"000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f";
private static final String DATA =
Expand Down Expand Up @@ -256,7 +264,8 @@ public void testKeyWrap(String algo, String key, int keyLen,
dataLen + "-byte key with " + 8*keyLen + "-bit KEK");
int allowed = Cipher.getMaxAllowedKeyLength("AES");
if (keyLen > allowed) {
System.out.println("=> skip, exceeds max allowed size " + allowed);
System.err.println("Skip, exceeds max allowed size " + allowed);
skippedAlgoList.add(algo);
return;
}
Cipher c1 = Cipher.getInstance(algo,
Expand Down Expand Up @@ -319,7 +328,8 @@ public void testEnc(String algo, String key, int keyLen, String data,
dataLen + "-byte data with " + 8*keyLen + "-bit KEK");
int allowed = Cipher.getMaxAllowedKeyLength("AES");
if (keyLen > allowed) {
System.out.println("=> skip, exceeds max allowed size " + allowed);
System.err.println("Skip, exceeds max allowed size " + allowed);
skippedAlgoList.add(algo);
return;
}
Cipher c1 = Cipher.getInstance(algo,
Expand Down Expand Up @@ -388,14 +398,21 @@ public void main(Provider p) throws Exception {
Object[] td = testDatum[i];
String algo = (String) td[0];
if (p.getService("Cipher", algo) == null) {
System.out.println("Skip, due to no support: " + algo);
System.err.println("Skip, due to no support: " + algo);
skippedAlgoList.add(algo);
continue;
}
testKeyWrap(algo, (String)td[1], (int)td[2], (String)td[3],
(int)td[4], (String)td[5], p);
testEnc(algo, (String)td[1], (int)td[2], (String)td[3],
(int)td[4], (String)td[5], p);
}
System.out.println("Test Passed");

//Check if tests were skipped.
if (skippedAlgoList.isEmpty()) {
System.out.println("All Tests Passed");
} else {
throw new SkippedException("Some tests were skipped : " + skippedAlgoList);
}
}
}
34 changes: 26 additions & 8 deletions test/jdk/sun/security/pkcs11/Cipher/KeyWrap/TestGeneral.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021, 2024, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2021, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -29,12 +29,23 @@
* @library /test/lib ../..
* @run main/othervm TestGeneral
*/
import jtreg.SkippedException;

import java.nio.ByteBuffer;
import java.security.AlgorithmParameters;
import java.security.InvalidAlgorithmParameterException;
import java.security.Key;
import java.security.PrivateKey;
import java.security.KeyPairGenerator;
import java.security.Provider;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HexFormat;
import java.security.*;
import javax.crypto.*;
import javax.crypto.spec.*;
import java.util.List;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

// adapted from com/sun/crypto/provider/Cipher/KeyWrap/TestGeneral.java
public class TestGeneral extends PKCS11Test {
Expand All @@ -49,7 +60,7 @@ public class TestGeneral extends PKCS11Test {
private static final int MAX_KWP_PAD_LEN = 7; // 0-7

public static void testEnc(Cipher c, byte[] in, int startLen, int inc,
IvParameterSpec[] ivs, int maxPadLen) throws Exception {
IvParameterSpec[] ivs, int maxPadLen) throws Exception {

System.out.println("testEnc, input len=" + startLen + " w/ inc=" +
inc);
Expand Down Expand Up @@ -118,7 +129,7 @@ public static void testKAT(Cipher c, String keyStr, String inStr,
}

public static void testWrap(Cipher c, Key[] inKeys, IvParameterSpec[] ivs,
int maxPadLen) throws Exception {
int maxPadLen) throws Exception {

for (Key inKey : inKeys) {
System.out.println("testWrap, key: " + inKey);
Expand Down Expand Up @@ -251,9 +262,10 @@ public void main(Provider p) throws Exception {
String[] algos = {
"AES/KW/PKCS5Padding", "AES/KW/NoPadding", "AES/KWP/NoPadding"
};
List<String> skippedAlgoList = new ArrayList<>();
for (String a : algos) {
if (p.getService("Cipher", a) == null) {
System.out.println("Skip, due to no support: " + a);
skippedAlgoList.add(a);
continue;
}

Expand Down Expand Up @@ -329,6 +341,12 @@ public void main(Provider p) throws Exception {
testWrap(c, keys, ivs, padLen);
testIv(c, ivLen, allowCustomIv);
}
System.out.println("All Tests Passed");

//Check if tests were skipped.
if (skippedAlgoList.isEmpty()) {
System.out.println("All Tests Passed");
} else {
throw new SkippedException("Some tests were skipped due to no support : " + skippedAlgoList);
}
}
}
17 changes: 8 additions & 9 deletions test/jdk/sun/security/pkcs11/Cipher/KeyWrap/XMLEncKAT.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2021, 2025 Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -28,14 +28,16 @@
* @library /test/lib ../..
* @run main/othervm XMLEncKAT
*/
import jtreg.SkippedException;

import java.util.Base64;
import java.security.Key;
import java.security.AlgorithmParameters;
import java.security.Provider;
import javax.crypto.*;
import javax.crypto.spec.*;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
import java.io.IOException;

// adapted from com/sun/crypto/provider/Cipher/KeyWrap/XMLEncKAT.java
public class XMLEncKAT extends PKCS11Test {
Expand Down Expand Up @@ -129,12 +131,9 @@ public static void main(String[] argv) throws Exception {
@Override
public void main(Provider p) throws Exception {
String wrapAlg = "AESWrap";

if (p.getService("Cipher", wrapAlg) == null) {
System.out.println("Skip, due to no support: " + wrapAlg);
return;
throw new SkippedException("No support : " + wrapAlg);
}

String keyAlg = "AES";
testKeyWrap(p, wrapAlg, aes128Key_1, keyAlg, aes128WrappedKey_1);
testKeyWrap(p, wrapAlg, aes128Key_2, keyAlg, aes128WrappedKey_2);
Expand Down