/src/test/java/org/jruby/ext/openssl/CipherTest.java

https://github.com/jruby/jruby-openssl · Java · 207 lines · 167 code · 34 blank · 6 comment · 1 complexity · daf9cd3fd4796f1cf88778ce6fd26eaf MD5 · raw file

  1. package org.jruby.ext.openssl;
  2. import org.junit.*;
  3. import static org.junit.Assert.*;
  4. /**
  5. * @author kares
  6. */
  7. public class CipherTest {
  8. @Test
  9. public void ciphersGetLazyInitialized() {
  10. //assertTrue( Cipher.Algorithm.AllSupportedCiphers.CIPHERS_MAP.isEmpty() );
  11. assertFalse( Cipher.isSupportedCipher("UNKNOWN") );
  12. assertFalse( Cipher.Algorithm.AllSupportedCiphers.CIPHERS_MAP.isEmpty() );
  13. assertTrue( Cipher.Algorithm.AllSupportedCiphers.CIPHERS_MAP.get("DES") != null );
  14. assertTrue( Cipher.isSupportedCipher("DES") );
  15. assertTrue( Cipher.isSupportedCipher("des") );
  16. assertTrue( Cipher.isSupportedCipher("AES") );
  17. assertTrue( Cipher.isSupportedCipher("BF") );
  18. assertTrue( Cipher.isSupportedCipher("des3") );
  19. }
  20. @Test
  21. public void jsseToOssl() {
  22. String alg;
  23. alg = Cipher.Algorithm.javaToOssl("RC2/CBC/PKCS5Padding", 40);
  24. assertEquals("RC2-40-CBC", alg);
  25. alg = Cipher.Algorithm.javaToOssl("RC2/CFB/PKCS5Padding", 40);
  26. assertEquals("RC2-40-CFB", alg);
  27. alg = Cipher.Algorithm.javaToOssl("Blowfish", 60);
  28. assertEquals("BF-60-CBC", alg);
  29. alg = Cipher.Algorithm.javaToOssl("DESede", 24);
  30. assertEquals("DES-EDE3-CBC", alg);
  31. }
  32. @Test
  33. public void osslToJsse() {
  34. doTestOsslToJsse();
  35. }
  36. private void doTestOsslToJsse() {
  37. Cipher.Algorithm alg;
  38. alg = Cipher.Algorithm.osslToJava("RC2-40-CBC");
  39. assertEquals("RC2", alg.base);
  40. assertEquals("40", alg.version);
  41. assertEquals("CBC", alg.mode);
  42. assertEquals("RC2/CBC/PKCS5Padding", alg.getRealName());
  43. System.out.println("running ...");
  44. alg = Cipher.Algorithm.osslToJava("DES-EDE3-CBC");
  45. assertEquals("DES", alg.base);
  46. assertEquals("EDE3", alg.version);
  47. assertEquals("CBC", alg.mode);
  48. assertEquals("DESede/CBC/PKCS5Padding", alg.getRealName());
  49. alg = Cipher.Algorithm.osslToJava("DES3");
  50. assertEquals("DES", alg.base);
  51. assertEquals("EDE3", alg.version);
  52. assertEquals("CBC", alg.mode);
  53. assertEquals("DESede/CBC/PKCS5Padding", alg.getRealName());
  54. alg = Cipher.Algorithm.osslToJava("DES-EDE");
  55. assertEquals("DES", alg.base);
  56. assertEquals("EDE", alg.version);
  57. assertEquals("ECB", alg.mode);
  58. assertEquals("DESede/ECB/PKCS5Padding", alg.getRealName());
  59. alg = Cipher.Algorithm.osslToJava("DES-EDE3");
  60. assertEquals("DES", alg.base);
  61. assertEquals("EDE3", alg.version);
  62. assertEquals("ECB", alg.mode);
  63. assertEquals("DESede/ECB/PKCS5Padding", alg.getRealName());
  64. alg = Cipher.Algorithm.osslToJava("DES-EDE-CBC");
  65. assertEquals("DES", alg.base);
  66. assertEquals("EDE", alg.version);
  67. assertEquals("CBC", alg.mode);
  68. assertEquals("DESede/CBC/PKCS5Padding", alg.getRealName());
  69. alg = Cipher.Algorithm.osslToJava("DES-EDE3-CBC");
  70. assertEquals("DES", alg.base);
  71. assertEquals("EDE3", alg.version);
  72. assertEquals("CBC", alg.mode);
  73. assertEquals("DESede/CBC/PKCS5Padding", alg.getRealName());
  74. alg = Cipher.Algorithm.osslToJava("DES-EDE3-CFB");
  75. assertEquals("DES", alg.base);
  76. assertEquals("EDE3", alg.version);
  77. assertEquals("CFB", alg.mode);
  78. assertEquals("DESede/CFB/NoPadding", alg.getRealName());
  79. alg = Cipher.Algorithm.osslToJava("DES-CFB");
  80. assertEquals("DES", alg.base);
  81. assertEquals(null, alg.version);
  82. assertEquals("CFB", alg.mode);
  83. assertEquals("DES/CFB/NoPadding", alg.getRealName());
  84. alg = Cipher.Algorithm.osslToJava("DES3");
  85. assertEquals("DES", alg.base);
  86. //assertEquals("EDE", alg.version);
  87. assertEquals("CBC", alg.mode);
  88. assertEquals("DESede/CBC/PKCS5Padding", alg.getRealName());
  89. alg = Cipher.Algorithm.osslToJava("BF");
  90. assertEquals("BF", alg.base);
  91. assertEquals("Blowfish/CBC/PKCS5Padding", alg.getRealName());
  92. alg = Cipher.Algorithm.osslToJava("AES-128-XTS");
  93. assertEquals("AES", alg.base);
  94. assertEquals("128", alg.version);
  95. assertEquals("XTS", alg.mode);
  96. assertEquals("PKCS5Padding", alg.getPadding());
  97. assertEquals("AES/XTS/PKCS5Padding", alg.getRealName());
  98. alg = Cipher.Algorithm.osslToJava("AES256");
  99. assertEquals("AES", alg.base);
  100. assertEquals("256", alg.version);
  101. assertEquals("CBC", alg.mode);
  102. assertEquals("PKCS5Padding", alg.getPadding());
  103. assertEquals("AES/CBC/PKCS5Padding", alg.getRealName());
  104. alg = Cipher.Algorithm.osslToJava("AES-256-OFB");
  105. assertEquals("AES", alg.base);
  106. assertEquals("256", alg.version);
  107. assertEquals("OFB", alg.mode);
  108. assertEquals("AES/OFB/NoPadding", alg.getRealName());
  109. alg = Cipher.Algorithm.osslToJava("AES-256-CTR");
  110. assertEquals("AES", alg.base);
  111. assertEquals("256", alg.version);
  112. assertEquals("CTR", alg.mode);
  113. assertEquals("AES/CTR/NoPadding", alg.getRealName());
  114. alg = Cipher.Algorithm.osslToJava("AES-256-CBC-HMAC-SHA1");
  115. assertEquals("AES", alg.base);
  116. assertEquals("CBC", alg.mode);
  117. assertEquals("256", alg.version);
  118. assertEquals("PKCS5Padding", alg.getPadding());
  119. assertEquals("AES/CBC/PKCS5Padding", alg.getRealName());
  120. alg = Cipher.Algorithm.osslToJava("RC4");
  121. assertEquals("RC4", alg.base);
  122. assertEquals(null, alg.version);
  123. assertEquals(null, alg.mode);
  124. assertEquals(null, alg.getPadding());
  125. assertEquals("RC4", alg.getRealName());
  126. alg = Cipher.Algorithm.osslToJava("RC4-40");
  127. assertEquals("RC4", alg.base);
  128. assertEquals("40", alg.version);
  129. assertEquals(null, alg.mode);
  130. assertEquals(null, alg.getPadding());
  131. assertEquals("RC4", alg.getRealName());
  132. // keeps "invalid" modes :
  133. alg = Cipher.Algorithm.osslToJava("DES-3X3");
  134. assertEquals("DES", alg.base);
  135. assertEquals(null, alg.version);
  136. assertEquals("3X3", alg.mode);
  137. assertEquals("DES/3X3/PKCS5Padding", alg.getRealName());
  138. alg = Cipher.Algorithm.osslToJava("MES-123-XXX");
  139. assertEquals("MES", alg.base);
  140. assertEquals("123", alg.version);
  141. assertEquals("XXX", alg.mode);
  142. assertEquals("MES/XXX/PKCS5Padding", alg.getRealName());
  143. }
  144. @Test
  145. public void osslKeyIvLength() {
  146. int[] len;
  147. len = Cipher.Algorithm.osslKeyIvLength("RC2-40-CBC");
  148. assertEquals(5, len[0]);
  149. assertEquals(8, len[1]);
  150. len = Cipher.Algorithm.osslKeyIvLength("DES-EDE3-CBC");
  151. assertEquals(24, len[0]);
  152. assertEquals(8, len[1]);
  153. len = Cipher.Algorithm.osslKeyIvLength("DES");
  154. assertEquals(8, len[0]);
  155. assertEquals(8, len[1]);
  156. len = Cipher.Algorithm.osslKeyIvLength("BF");
  157. assertEquals(16, len[0]);
  158. assertEquals(8, len[1]);
  159. len = Cipher.Algorithm.osslKeyIvLength("CAST");
  160. assertEquals(16, len[0]);
  161. assertEquals(8, len[1]);
  162. }
  163. @Test
  164. public void getAlgorithmBase() throws Exception {
  165. javax.crypto.Cipher cipher; String algBase;
  166. cipher = javax.crypto.Cipher.getInstance("DES/CBC/PKCS5Padding");
  167. algBase = Cipher.Algorithm.getAlgorithmBase(cipher);
  168. assertEquals("DES", algBase);
  169. cipher = javax.crypto.Cipher.getInstance("DES");
  170. algBase = Cipher.Algorithm.getAlgorithmBase(cipher);
  171. assertEquals("DES", algBase);
  172. }
  173. }