/jdk/test/sun/security/pkcs11/Cipher/TestRSACipherWrap.java

https://github.com/ibmruntimes/openj9-openjdk-jdk9 · Java · 118 lines · 74 code · 12 blank · 32 comment · 4 complexity · a8e02370005513b19c22c51bbfbd225f MD5 · raw file

  1. /*
  2. * Copyright (c) 2008, 2017, Oracle and/or its affiliates. All rights reserved.
  3. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4. *
  5. * This code is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License version 2 only, as
  7. * published by the Free Software Foundation.
  8. *
  9. * This code is distributed in the hope that it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  12. * version 2 for more details (a copy is included in the LICENSE file that
  13. * accompanied this code).
  14. *
  15. * You should have received a copy of the GNU General Public License version
  16. * 2 along with this work; if not, write to the Free Software Foundation,
  17. * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18. *
  19. * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20. * or visit www.oracle.com if you need additional information or have any
  21. * questions.
  22. */
  23. /*
  24. * @test
  25. * @bug 6572331 6994008
  26. * @summary basic test for RSA cipher key wrapping functionality
  27. * @author Valerie Peng
  28. * @library ..
  29. * @modules jdk.crypto.cryptoki
  30. * @run main/othervm TestRSACipherWrap
  31. * @run main/othervm TestRSACipherWrap sm
  32. */
  33. import java.security.GeneralSecurityException;
  34. import java.security.InvalidParameterException;
  35. import java.security.Key;
  36. import java.security.KeyPair;
  37. import java.security.KeyPairGenerator;
  38. import java.security.Provider;
  39. import java.util.Arrays;
  40. import javax.crypto.Cipher;
  41. import javax.crypto.KeyGenerator;
  42. import javax.crypto.SecretKey;
  43. import javax.crypto.spec.SecretKeySpec;
  44. public class TestRSACipherWrap extends PKCS11Test {
  45. private static final String[] RSA_ALGOS =
  46. { "RSA/ECB/PKCS1Padding", "RSA" };
  47. @Override
  48. public void main(Provider p) throws Exception {
  49. try {
  50. Cipher.getInstance(RSA_ALGOS[0], p);
  51. } catch (GeneralSecurityException e) {
  52. System.out.println(RSA_ALGOS[0] + " unsupported, skipping");
  53. return;
  54. }
  55. KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA", p);
  56. kpg.initialize(1024);
  57. KeyPair kp = kpg.generateKeyPair();
  58. for (String rsaAlgo: RSA_ALGOS) {
  59. Cipher cipherPKCS11 = Cipher.getInstance(rsaAlgo, p);
  60. Cipher cipherJce = Cipher.getInstance(rsaAlgo, "SunJCE");
  61. String algos[] = {"AES", "RC2", "Blowfish"};
  62. int keySizes[] = {128, 256};
  63. for (int j = 0; j < algos.length; j++) {
  64. String algorithm = algos[j];
  65. KeyGenerator keygen =
  66. KeyGenerator.getInstance(algorithm);
  67. for (int i = 0; i < keySizes.length; i++) {
  68. SecretKey secretKey = null;
  69. System.out.print("Generate " + keySizes[i] + "-bit " +
  70. algorithm + " key using ");
  71. try {
  72. keygen.init(keySizes[i]);
  73. secretKey = keygen.generateKey();
  74. System.out.println(keygen.getProvider().getName());
  75. } catch (InvalidParameterException ipe) {
  76. secretKey = new SecretKeySpec(new byte[32], algorithm);
  77. System.out.println("SecretKeySpec class");
  78. }
  79. test(kp, secretKey, cipherPKCS11, cipherJce);
  80. test(kp, secretKey, cipherPKCS11, cipherPKCS11);
  81. test(kp, secretKey, cipherJce, cipherPKCS11);
  82. }
  83. }
  84. }
  85. }
  86. private static void test(KeyPair kp, SecretKey secretKey,
  87. Cipher wrapCipher, Cipher unwrapCipher)
  88. throws Exception {
  89. String algo = secretKey.getAlgorithm();
  90. wrapCipher.init(Cipher.WRAP_MODE, kp.getPublic());
  91. byte[] wrappedKey = wrapCipher.wrap(secretKey);
  92. unwrapCipher.init(Cipher.UNWRAP_MODE, kp.getPrivate());
  93. Key unwrappedKey =
  94. unwrapCipher.unwrap(wrappedKey, algo, Cipher.SECRET_KEY);
  95. System.out.println("Test " + wrapCipher.getProvider().getName() +
  96. "/" + unwrapCipher.getProvider().getName() + ": ");
  97. if (!Arrays.equals(secretKey.getEncoded(),
  98. unwrappedKey.getEncoded())) {
  99. throw new Exception("Test Failed!");
  100. }
  101. System.out.println("Passed");
  102. }
  103. public static void main(String[] args) throws Exception {
  104. main(new TestRSACipherWrap(), args);
  105. }
  106. }