PageRenderTime 32ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/luni/src/test/java/libcore/javax/crypto/CipherTest.java

https://bitbucket.org/aways/android_libcore
Java | 960 lines | 793 code | 94 blank | 73 comment | 89 complexity | 86435a2e14dc19ea5f5c2d6a41fc29b0 MD5 | raw file
  1. /*
  2. * Copyright (C) 2011 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package libcore.javax.crypto;
  17. import com.android.org.bouncycastle.asn1.x509.KeyUsage;
  18. import java.io.ByteArrayOutputStream;
  19. import java.io.PrintStream;
  20. import java.math.BigInteger;
  21. import java.security.InvalidAlgorithmParameterException;
  22. import java.security.InvalidKeyException;
  23. import java.security.Key;
  24. import java.security.KeyFactory;
  25. import java.security.PrivateKey;
  26. import java.security.Provider;
  27. import java.security.PublicKey;
  28. import java.security.SecureRandom;
  29. import java.security.Security;
  30. import java.security.cert.Certificate;
  31. import java.security.interfaces.RSAPrivateKey;
  32. import java.security.interfaces.RSAPublicKey;
  33. import java.security.spec.AlgorithmParameterSpec;
  34. import java.security.spec.RSAPrivateKeySpec;
  35. import java.security.spec.RSAPublicKeySpec;
  36. import java.util.ArrayList;
  37. import java.util.Arrays;
  38. import java.util.Collections;
  39. import java.util.HashMap;
  40. import java.util.HashSet;
  41. import java.util.List;
  42. import java.util.Locale;
  43. import java.util.Map;
  44. import java.util.Set;
  45. import javax.crypto.BadPaddingException;
  46. import javax.crypto.Cipher;
  47. import javax.crypto.IllegalBlockSizeException;
  48. import javax.crypto.KeyGenerator;
  49. import javax.crypto.SecretKey;
  50. import javax.crypto.SecretKeyFactory;
  51. import javax.crypto.ShortBufferException;
  52. import javax.crypto.spec.IvParameterSpec;
  53. import javax.crypto.spec.PBEKeySpec;
  54. import javax.crypto.spec.PBEParameterSpec;
  55. import javax.crypto.spec.SecretKeySpec;
  56. import junit.framework.TestCase;
  57. import libcore.java.security.StandardNames;
  58. import libcore.java.security.TestKeyStore;
  59. public final class CipherTest extends TestCase {
  60. private static final String[] RSA_PROVIDERS = ((StandardNames.IS_RI)
  61. ? new String[] { "SunJCE" }
  62. : new String[] { "BC" , "AndroidOpenSSL" });
  63. private static final String[] AES_PROVIDERS = ((StandardNames.IS_RI)
  64. ? new String[] { "SunJCE" }
  65. : new String[] { "BC" }); // TOOD: , "AndroidOpenSSL"
  66. private static final boolean IS_UNLIMITED;
  67. static {
  68. boolean is_unlimited;
  69. if (StandardNames.IS_RI) {
  70. try {
  71. String algorithm = "PBEWITHMD5ANDTRIPLEDES";
  72. Cipher.getInstance(algorithm).init(getEncryptMode(algorithm),
  73. getEncryptKey(algorithm),
  74. getAlgorithmParameterSpec(algorithm));
  75. is_unlimited = true;
  76. } catch (Exception e) {
  77. is_unlimited = false;
  78. System.out.println("WARNING: Some tests disabled due to lack of "
  79. + "'Java Cryptography Extension (JCE) Unlimited Strength Jurisdiction Policy Files'");
  80. }
  81. } else {
  82. is_unlimited = true;
  83. }
  84. IS_UNLIMITED = is_unlimited;
  85. }
  86. private static boolean isUnsupported(String algorithm) {
  87. if (algorithm.equals("RC2")) {
  88. return true;
  89. }
  90. if (algorithm.equals("PBEWITHMD5ANDRC2")) {
  91. return true;
  92. }
  93. if (algorithm.startsWith("PBEWITHSHA1ANDRC2")) {
  94. return true;
  95. }
  96. if (algorithm.equals("PBEWITHSHAAND40BITRC2-CBC")) {
  97. return true;
  98. }
  99. if (algorithm.equals("PBEWITHSHAAND128BITRC2-CBC")) {
  100. return true;
  101. }
  102. if (algorithm.equals("PBEWITHSHAANDTWOFISH-CBC")) {
  103. return true;
  104. }
  105. if (!IS_UNLIMITED) {
  106. if (algorithm.equals("PBEWITHMD5ANDTRIPLEDES")) {
  107. return true;
  108. }
  109. }
  110. return false;
  111. }
  112. private synchronized static int getEncryptMode(String algorithm) throws Exception {
  113. if (isWrap(algorithm)) {
  114. return Cipher.WRAP_MODE;
  115. }
  116. return Cipher.ENCRYPT_MODE;
  117. }
  118. private synchronized static int getDecryptMode(String algorithm) throws Exception {
  119. if (isWrap(algorithm)) {
  120. return Cipher.UNWRAP_MODE;
  121. }
  122. return Cipher.DECRYPT_MODE;
  123. }
  124. private static String getBaseAlgorithm(String algorithm) {
  125. if (algorithm.equals("AESWRAP")) {
  126. return "AES";
  127. }
  128. if (algorithm.startsWith("AES/")) {
  129. return "AES";
  130. }
  131. if (algorithm.equals("PBEWITHMD5AND128BITAES-CBC-OPENSSL")) {
  132. return "AES";
  133. }
  134. if (algorithm.equals("PBEWITHMD5AND192BITAES-CBC-OPENSSL")) {
  135. return "AES";
  136. }
  137. if (algorithm.equals("PBEWITHMD5AND256BITAES-CBC-OPENSSL")) {
  138. return "AES";
  139. }
  140. if (algorithm.equals("PBEWITHSHA256AND128BITAES-CBC-BC")) {
  141. return "AES";
  142. }
  143. if (algorithm.equals("PBEWITHSHA256AND192BITAES-CBC-BC")) {
  144. return "AES";
  145. }
  146. if (algorithm.equals("PBEWITHSHA256AND256BITAES-CBC-BC")) {
  147. return "AES";
  148. }
  149. if (algorithm.equals("PBEWITHSHAAND128BITAES-CBC-BC")) {
  150. return "AES";
  151. }
  152. if (algorithm.equals("PBEWITHSHAAND192BITAES-CBC-BC")) {
  153. return "AES";
  154. }
  155. if (algorithm.equals("PBEWITHSHAAND256BITAES-CBC-BC")) {
  156. return "AES";
  157. }
  158. if (algorithm.equals("PBEWITHMD5ANDDES")) {
  159. return "DES";
  160. }
  161. if (algorithm.equals("PBEWITHSHA1ANDDES")) {
  162. return "DES";
  163. }
  164. if (algorithm.equals("DESEDEWRAP")) {
  165. return "DESEDE";
  166. }
  167. if (algorithm.equals("PBEWITHSHAAND2-KEYTRIPLEDES-CBC")) {
  168. return "DESEDE";
  169. }
  170. if (algorithm.equals("PBEWITHSHAAND3-KEYTRIPLEDES-CBC")) {
  171. return "DESEDE";
  172. }
  173. if (algorithm.equals("PBEWITHMD5ANDTRIPLEDES")) {
  174. return "DESEDE";
  175. }
  176. if (algorithm.equals("PBEWITHSHA1ANDDESEDE")) {
  177. return "DESEDE";
  178. }
  179. if (algorithm.equals("RSA/ECB/NOPADDING")) {
  180. return "RSA";
  181. }
  182. if (algorithm.equals("RSA/ECB/PKCS1PADDING")) {
  183. return "RSA";
  184. }
  185. if (algorithm.equals("PBEWITHSHAAND40BITRC4")) {
  186. return "ARC4";
  187. }
  188. if (algorithm.equals("PBEWITHSHAAND128BITRC4")) {
  189. return "ARC4";
  190. }
  191. return algorithm;
  192. }
  193. private static boolean isAsymmetric(String algorithm) {
  194. return getBaseAlgorithm(algorithm).equals("RSA");
  195. }
  196. private static boolean isWrap(String algorithm) {
  197. return algorithm.endsWith("WRAP");
  198. }
  199. private static boolean isPBE(String algorithm) {
  200. return algorithm.startsWith("PBE");
  201. }
  202. private static Map<String, Key> ENCRYPT_KEYS = new HashMap<String, Key>();
  203. private synchronized static Key getEncryptKey(String algorithm) throws Exception {
  204. Key key = ENCRYPT_KEYS.get(algorithm);
  205. if (key != null) {
  206. return key;
  207. }
  208. if (algorithm.startsWith("RSA")) {
  209. KeyFactory kf = KeyFactory.getInstance("RSA");
  210. RSAPrivateKeySpec keySpec = new RSAPrivateKeySpec(RSA_2048_modulus,
  211. RSA_2048_privateExponent);
  212. key = kf.generatePrivate(keySpec);
  213. } else if (isPBE(algorithm)) {
  214. SecretKeyFactory skf = SecretKeyFactory.getInstance(algorithm);
  215. key = skf.generateSecret(new PBEKeySpec("secret".toCharArray()));
  216. } else {
  217. KeyGenerator kg = KeyGenerator.getInstance(getBaseAlgorithm(algorithm));
  218. if (StandardNames.IS_RI && algorithm.equals("AES")) {
  219. kg.init(128);
  220. }
  221. key = kg.generateKey();
  222. }
  223. ENCRYPT_KEYS.put(algorithm, key);
  224. return key;
  225. }
  226. private static Map<String, Key> DECRYPT_KEYS = new HashMap<String, Key>();
  227. private synchronized static Key getDecryptKey(String algorithm) throws Exception {
  228. Key key = DECRYPT_KEYS.get(algorithm);
  229. if (key != null) {
  230. return key;
  231. }
  232. if (algorithm.startsWith("RSA")) {
  233. KeyFactory kf = KeyFactory.getInstance("RSA");
  234. RSAPublicKeySpec keySpec = new RSAPublicKeySpec(RSA_2048_modulus,
  235. RSA_2048_publicExponent);
  236. key = kf.generatePublic(keySpec);
  237. } else {
  238. assertFalse(algorithm, isAsymmetric(algorithm));
  239. key = getEncryptKey(algorithm);
  240. }
  241. DECRYPT_KEYS.put(algorithm, key);
  242. return key;
  243. }
  244. private static Map<String, Integer> EXPECTED_BLOCK_SIZE = new HashMap<String, Integer>();
  245. static {
  246. setExpectedBlockSize("AES", 16);
  247. setExpectedBlockSize("PBEWITHMD5AND128BITAES-CBC-OPENSSL", 16);
  248. setExpectedBlockSize("PBEWITHMD5AND192BITAES-CBC-OPENSSL", 16);
  249. setExpectedBlockSize("PBEWITHMD5AND256BITAES-CBC-OPENSSL", 16);
  250. setExpectedBlockSize("PBEWITHSHA256AND128BITAES-CBC-BC", 16);
  251. setExpectedBlockSize("PBEWITHSHA256AND192BITAES-CBC-BC", 16);
  252. setExpectedBlockSize("PBEWITHSHA256AND256BITAES-CBC-BC", 16);
  253. setExpectedBlockSize("PBEWITHSHAAND128BITAES-CBC-BC", 16);
  254. setExpectedBlockSize("PBEWITHSHAAND192BITAES-CBC-BC", 16);
  255. setExpectedBlockSize("PBEWITHSHAAND256BITAES-CBC-BC", 16);
  256. if (StandardNames.IS_RI) {
  257. setExpectedBlockSize("AESWRAP", 16);
  258. } else {
  259. setExpectedBlockSize("AESWRAP", 0);
  260. }
  261. setExpectedBlockSize("ARC4", 0);
  262. setExpectedBlockSize("ARCFOUR", 0);
  263. setExpectedBlockSize("PBEWITHSHAAND40BITRC4", 0);
  264. setExpectedBlockSize("PBEWITHSHAAND128BITRC4", 0);
  265. setExpectedBlockSize("BLOWFISH", 8);
  266. setExpectedBlockSize("DES", 8);
  267. setExpectedBlockSize("PBEWITHMD5ANDDES", 8);
  268. setExpectedBlockSize("PBEWITHSHA1ANDDES", 8);
  269. setExpectedBlockSize("DESEDE", 8);
  270. setExpectedBlockSize("PBEWITHSHAAND2-KEYTRIPLEDES-CBC", 8);
  271. setExpectedBlockSize("PBEWITHSHAAND3-KEYTRIPLEDES-CBC", 8);
  272. setExpectedBlockSize("PBEWITHMD5ANDTRIPLEDES", 8);
  273. setExpectedBlockSize("PBEWITHSHA1ANDDESEDE", 8);
  274. if (StandardNames.IS_RI) {
  275. setExpectedBlockSize("DESEDEWRAP", 8);
  276. } else {
  277. setExpectedBlockSize("DESEDEWRAP", 0);
  278. }
  279. if (StandardNames.IS_RI) {
  280. setExpectedBlockSize("RSA", 0);
  281. setExpectedBlockSize("RSA/ECB/NoPadding", 0);
  282. setExpectedBlockSize("RSA/ECB/PKCS1Padding", 0);
  283. } else {
  284. setExpectedBlockSize("RSA", Cipher.ENCRYPT_MODE, 256);
  285. setExpectedBlockSize("RSA/ECB/NoPadding", Cipher.ENCRYPT_MODE, 256);
  286. setExpectedBlockSize("RSA/ECB/PKCS1Padding", Cipher.ENCRYPT_MODE, 245);
  287. // BC strips the leading 0 for us even when NoPadding is specified
  288. setExpectedBlockSize("RSA", Cipher.ENCRYPT_MODE, "BC", 255);
  289. setExpectedBlockSize("RSA/ECB/NoPadding", Cipher.ENCRYPT_MODE, "BC", 255);
  290. setExpectedBlockSize("RSA", Cipher.DECRYPT_MODE, 256);
  291. setExpectedBlockSize("RSA/ECB/NoPadding", Cipher.DECRYPT_MODE, 256);
  292. setExpectedBlockSize("RSA/ECB/PKCS1Padding", Cipher.DECRYPT_MODE, 256);
  293. }
  294. }
  295. private static String modeKey(String algorithm, int mode) {
  296. return algorithm + ":" + mode;
  297. }
  298. private static String modeProviderKey(String algorithm, int mode, String provider) {
  299. return algorithm + ":" + mode + ":" + provider;
  300. }
  301. private static void setExpectedSize(Map<String, Integer> map,
  302. String algorithm, int value) {
  303. algorithm = algorithm.toUpperCase(Locale.US);
  304. map.put(algorithm, value);
  305. }
  306. private static void setExpectedSize(Map<String, Integer> map,
  307. String algorithm, int mode, int value) {
  308. setExpectedSize(map, modeKey(algorithm, mode), value);
  309. }
  310. private static void setExpectedSize(Map<String, Integer> map,
  311. String algorithm, int mode, String provider, int value) {
  312. setExpectedSize(map, modeProviderKey(algorithm, mode, provider), value);
  313. }
  314. private static int getExpectedSize(Map<String, Integer> map, String algorithm, int mode, String provider) {
  315. Integer expected = map.get(modeProviderKey(algorithm, mode, provider));
  316. if (expected != null) {
  317. return expected;
  318. }
  319. expected = map.get(modeKey(algorithm, mode));
  320. if (expected != null) {
  321. return expected;
  322. }
  323. expected = map.get(algorithm);
  324. assertNotNull("Algorithm " + algorithm + " not found in " + map, expected);
  325. return expected;
  326. }
  327. private static void setExpectedBlockSize(String algorithm, int value) {
  328. setExpectedSize(EXPECTED_BLOCK_SIZE, algorithm, value);
  329. }
  330. private static void setExpectedBlockSize(String algorithm, int mode, int value) {
  331. setExpectedSize(EXPECTED_BLOCK_SIZE, algorithm, mode, value);
  332. }
  333. private static void setExpectedBlockSize(String algorithm, int mode, String provider, int value) {
  334. setExpectedSize(EXPECTED_BLOCK_SIZE, algorithm, mode, provider, value);
  335. }
  336. private static int getExpectedBlockSize(String algorithm, int mode, String provider) {
  337. return getExpectedSize(EXPECTED_BLOCK_SIZE, algorithm, mode, provider);
  338. }
  339. private static Map<String, Integer> EXPECTED_OUTPUT_SIZE = new HashMap<String, Integer>();
  340. static {
  341. setExpectedOutputSize("AES", Cipher.ENCRYPT_MODE, 16);
  342. setExpectedOutputSize("PBEWITHMD5AND128BITAES-CBC-OPENSSL", 16);
  343. setExpectedOutputSize("PBEWITHMD5AND192BITAES-CBC-OPENSSL", 16);
  344. setExpectedOutputSize("PBEWITHMD5AND256BITAES-CBC-OPENSSL", 16);
  345. setExpectedOutputSize("PBEWITHSHA256AND128BITAES-CBC-BC", 16);
  346. setExpectedOutputSize("PBEWITHSHA256AND192BITAES-CBC-BC", 16);
  347. setExpectedOutputSize("PBEWITHSHA256AND256BITAES-CBC-BC", 16);
  348. setExpectedOutputSize("PBEWITHSHAAND128BITAES-CBC-BC", 16);
  349. setExpectedOutputSize("PBEWITHSHAAND192BITAES-CBC-BC", 16);
  350. setExpectedOutputSize("PBEWITHSHAAND256BITAES-CBC-BC", 16);
  351. setExpectedOutputSize("AES", Cipher.DECRYPT_MODE, 0);
  352. setExpectedOutputSize("PBEWITHMD5AND128BITAES-CBC-OPENSSL", Cipher.DECRYPT_MODE, 0);
  353. setExpectedOutputSize("PBEWITHMD5AND192BITAES-CBC-OPENSSL", Cipher.DECRYPT_MODE, 0);
  354. setExpectedOutputSize("PBEWITHMD5AND256BITAES-CBC-OPENSSL", Cipher.DECRYPT_MODE, 0);
  355. setExpectedOutputSize("PBEWITHSHA256AND128BITAES-CBC-BC", Cipher.DECRYPT_MODE, 0);
  356. setExpectedOutputSize("PBEWITHSHA256AND192BITAES-CBC-BC", Cipher.DECRYPT_MODE, 0);
  357. setExpectedOutputSize("PBEWITHSHA256AND256BITAES-CBC-BC", Cipher.DECRYPT_MODE, 0);
  358. setExpectedOutputSize("PBEWITHSHAAND128BITAES-CBC-BC", Cipher.DECRYPT_MODE, 0);
  359. setExpectedOutputSize("PBEWITHSHAAND192BITAES-CBC-BC", Cipher.DECRYPT_MODE, 0);
  360. setExpectedOutputSize("PBEWITHSHAAND256BITAES-CBC-BC", Cipher.DECRYPT_MODE, 0);
  361. if (StandardNames.IS_RI) {
  362. setExpectedOutputSize("AESWRAP", Cipher.WRAP_MODE, 8);
  363. setExpectedOutputSize("AESWRAP", Cipher.UNWRAP_MODE, 0);
  364. } else {
  365. setExpectedOutputSize("AESWRAP", -1);
  366. }
  367. setExpectedOutputSize("ARC4", 0);
  368. setExpectedOutputSize("ARCFOUR", 0);
  369. setExpectedOutputSize("PBEWITHSHAAND40BITRC4", 0);
  370. setExpectedOutputSize("PBEWITHSHAAND128BITRC4", 0);
  371. setExpectedOutputSize("BLOWFISH", Cipher.ENCRYPT_MODE, 8);
  372. setExpectedOutputSize("BLOWFISH", Cipher.DECRYPT_MODE, 0);
  373. setExpectedOutputSize("DES", Cipher.ENCRYPT_MODE, 8);
  374. setExpectedOutputSize("PBEWITHMD5ANDDES", Cipher.ENCRYPT_MODE, 8);
  375. setExpectedOutputSize("PBEWITHSHA1ANDDES", Cipher.ENCRYPT_MODE, 8);
  376. setExpectedOutputSize("DES", Cipher.DECRYPT_MODE, 0);
  377. setExpectedOutputSize("PBEWITHMD5ANDDES", Cipher.DECRYPT_MODE, 0);
  378. setExpectedOutputSize("PBEWITHSHA1ANDDES", Cipher.DECRYPT_MODE, 0);
  379. setExpectedOutputSize("DESEDE", Cipher.ENCRYPT_MODE, 8);
  380. setExpectedOutputSize("PBEWITHSHAAND2-KEYTRIPLEDES-CBC", Cipher.ENCRYPT_MODE, 8);
  381. setExpectedOutputSize("PBEWITHSHAAND3-KEYTRIPLEDES-CBC", Cipher.ENCRYPT_MODE, 8);
  382. setExpectedOutputSize("PBEWITHMD5ANDTRIPLEDES", Cipher.ENCRYPT_MODE, 8);
  383. setExpectedOutputSize("PBEWITHSHA1ANDDESEDE", Cipher.ENCRYPT_MODE, 8);
  384. setExpectedOutputSize("DESEDE", Cipher.DECRYPT_MODE, 0);
  385. setExpectedOutputSize("PBEWITHSHAAND2-KEYTRIPLEDES-CBC", Cipher.DECRYPT_MODE, 0);
  386. setExpectedOutputSize("PBEWITHSHAAND3-KEYTRIPLEDES-CBC", Cipher.DECRYPT_MODE, 0);
  387. setExpectedOutputSize("PBEWITHMD5ANDTRIPLEDES", Cipher.DECRYPT_MODE, 0);
  388. setExpectedOutputSize("PBEWITHSHA1ANDDESEDE", Cipher.DECRYPT_MODE, 0);
  389. if (StandardNames.IS_RI) {
  390. setExpectedOutputSize("DESEDEWRAP", Cipher.WRAP_MODE, 16);
  391. setExpectedOutputSize("DESEDEWRAP", Cipher.UNWRAP_MODE, 0);
  392. } else {
  393. setExpectedOutputSize("DESEDEWRAP", -1);
  394. }
  395. setExpectedOutputSize("RSA", Cipher.ENCRYPT_MODE, 256);
  396. setExpectedOutputSize("RSA/ECB/NoPadding", Cipher.ENCRYPT_MODE, 256);
  397. setExpectedOutputSize("RSA/ECB/PKCS1Padding", Cipher.ENCRYPT_MODE, 256);
  398. setExpectedOutputSize("RSA", Cipher.DECRYPT_MODE, 256);
  399. setExpectedOutputSize("RSA/ECB/NoPadding", Cipher.DECRYPT_MODE, 256);
  400. setExpectedOutputSize("RSA/ECB/PKCS1Padding", Cipher.DECRYPT_MODE, 245);
  401. // BC strips the leading 0 for us even when NoPadding is specified
  402. setExpectedOutputSize("RSA", Cipher.DECRYPT_MODE, "BC", 255);
  403. setExpectedOutputSize("RSA/ECB/NoPadding", Cipher.DECRYPT_MODE, "BC", 255);
  404. }
  405. private static void setExpectedOutputSize(String algorithm, int value) {
  406. setExpectedSize(EXPECTED_OUTPUT_SIZE, algorithm, value);
  407. }
  408. private static void setExpectedOutputSize(String algorithm, int mode, int value) {
  409. setExpectedSize(EXPECTED_OUTPUT_SIZE, algorithm, mode, value);
  410. }
  411. private static void setExpectedOutputSize(String algorithm, int mode, String provider, int value) {
  412. setExpectedSize(EXPECTED_OUTPUT_SIZE, algorithm, mode, provider, value);
  413. }
  414. private static int getExpectedOutputSize(String algorithm, int mode, String provider) {
  415. return getExpectedSize(EXPECTED_OUTPUT_SIZE, algorithm, mode, provider);
  416. }
  417. private static byte[] ORIGINAL_PLAIN_TEXT = new byte[] { 0x0a, 0x0b, 0x0c };
  418. private static byte[] PKCS1_BLOCK_TYPE_00_PADDED_PLAIN_TEXT = new byte[] {
  419. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  420. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  421. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  422. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  423. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  424. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  425. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  426. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  427. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  428. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  429. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  430. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  431. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  432. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  433. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  434. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x0a, 0x0b, 0x0c
  435. };
  436. private static byte[] PKCS1_BLOCK_TYPE_01_PADDED_PLAIN_TEXT = new byte[] {
  437. (byte) 0x00, (byte) 0x01, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
  438. (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
  439. (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
  440. (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
  441. (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
  442. (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
  443. (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
  444. (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
  445. (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
  446. (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
  447. (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
  448. (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
  449. (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
  450. (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
  451. (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
  452. (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
  453. (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
  454. (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
  455. (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
  456. (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
  457. (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
  458. (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
  459. (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
  460. (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
  461. (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
  462. (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
  463. (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
  464. (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
  465. (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
  466. (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
  467. (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
  468. (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0x00, (byte) 0x0a, (byte) 0x0b, (byte) 0x0c
  469. };
  470. private static byte[] PKCS1_BLOCK_TYPE_02_PADDED_PLAIN_TEXT = new byte[] {
  471. (byte) 0x00, (byte) 0x02, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
  472. (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
  473. (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
  474. (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
  475. (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
  476. (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
  477. (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
  478. (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
  479. (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
  480. (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
  481. (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
  482. (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
  483. (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
  484. (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
  485. (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
  486. (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
  487. (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
  488. (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
  489. (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
  490. (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
  491. (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
  492. (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
  493. (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
  494. (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
  495. (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
  496. (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
  497. (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
  498. (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
  499. (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
  500. (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
  501. (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
  502. (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0x00, (byte) 0x0a, (byte) 0x0b, (byte) 0x0c
  503. };
  504. private static byte[] getExpectedPlainText(String algorithm) {
  505. if (algorithm.equals("RSA/ECB/NOPADDING")) {
  506. return PKCS1_BLOCK_TYPE_00_PADDED_PLAIN_TEXT;
  507. }
  508. return ORIGINAL_PLAIN_TEXT;
  509. }
  510. private static AlgorithmParameterSpec getAlgorithmParameterSpec(String algorithm) {
  511. if (!isPBE(algorithm)) {
  512. return null;
  513. }
  514. final byte[] salt = new byte[8];
  515. new SecureRandom().nextBytes(salt);
  516. return new PBEParameterSpec(salt, 1024);
  517. }
  518. public void test_getInstance() throws Exception {
  519. final ByteArrayOutputStream errBuffer = new ByteArrayOutputStream();
  520. PrintStream out = new PrintStream(errBuffer);
  521. Set<String> seenBaseCipherNames = new HashSet<String>();
  522. Set<String> seenCiphersWithModeAndPadding = new HashSet<String>();
  523. Provider[] providers = Security.getProviders();
  524. for (Provider provider : providers) {
  525. Set<Provider.Service> services = provider.getServices();
  526. for (Provider.Service service : services) {
  527. String type = service.getType();
  528. if (!type.equals("Cipher")) {
  529. continue;
  530. }
  531. String algorithm = service.getAlgorithm();
  532. /*
  533. * Any specific modes and paddings aren't tested directly here,
  534. * but we need to make sure we see the bare algorithm from some
  535. * provider. We will test each mode specifically when we get the
  536. * base cipher.
  537. */
  538. final int firstSlash = algorithm.indexOf('/');
  539. if (firstSlash == -1) {
  540. seenBaseCipherNames.add(algorithm);
  541. } else {
  542. final String baseCipherName = algorithm.substring(0, firstSlash);
  543. if (!seenBaseCipherNames.contains(baseCipherName)) {
  544. seenCiphersWithModeAndPadding.add(baseCipherName);
  545. }
  546. continue;
  547. }
  548. try {
  549. test_Cipher_Algorithm(provider, algorithm);
  550. } catch (Throwable e) {
  551. out.append("Error encountered checking " + algorithm
  552. + " with provider " + provider.getName() + "\n");
  553. e.printStackTrace(out);
  554. }
  555. Set<String> modes = StandardNames.getModesForCipher(algorithm);
  556. if (modes != null) {
  557. for (String mode : modes) {
  558. Set<String> paddings = StandardNames.getPaddingsForCipher(algorithm);
  559. if (paddings != null) {
  560. for (String padding : paddings) {
  561. final String algorithmName = algorithm + "/" + mode + "/" + padding;
  562. try {
  563. test_Cipher_Algorithm(provider, algorithmName);
  564. } catch (Throwable e) {
  565. out.append("Error encountered checking " + algorithmName
  566. + " with provider " + provider.getName() + "\n");
  567. e.printStackTrace(out);
  568. }
  569. }
  570. }
  571. }
  572. }
  573. }
  574. }
  575. seenCiphersWithModeAndPadding.removeAll(seenBaseCipherNames);
  576. assertEquals("Ciphers seen with mode and padding but not base cipher",
  577. Collections.EMPTY_SET, seenCiphersWithModeAndPadding);
  578. out.flush();
  579. if (errBuffer.size() > 0) {
  580. throw new Exception("Errors encountered:\n\n" + errBuffer.toString() + "\n\n");
  581. }
  582. }
  583. private void test_Cipher_Algorithm(Provider provider, String algorithm) throws Exception {
  584. // Cipher.getInstance(String)
  585. Cipher c1 = Cipher.getInstance(algorithm);
  586. assertEquals(algorithm, c1.getAlgorithm());
  587. test_Cipher(c1);
  588. // Cipher.getInstance(String, Provider)
  589. Cipher c2 = Cipher.getInstance(algorithm, provider);
  590. assertEquals(algorithm, c2.getAlgorithm());
  591. assertEquals(provider, c2.getProvider());
  592. test_Cipher(c2);
  593. // KeyGenerator.getInstance(String, String)
  594. Cipher c3 = Cipher.getInstance(algorithm, provider.getName());
  595. assertEquals(algorithm, c3.getAlgorithm());
  596. assertEquals(provider, c3.getProvider());
  597. test_Cipher(c3);
  598. }
  599. private void test_Cipher(Cipher c) throws Exception {
  600. String algorithm = c.getAlgorithm().toUpperCase(Locale.US);
  601. if (isUnsupported(algorithm)) {
  602. return;
  603. }
  604. String providerName = c.getProvider().getName();
  605. String cipherID = algorithm + ":" + providerName;
  606. try {
  607. c.getOutputSize(0);
  608. } catch (IllegalStateException expected) {
  609. }
  610. // TODO: test keys from different factories (e.g. OpenSSLRSAPrivateKey vs JCERSAPrivateKey)
  611. Key encryptKey = getEncryptKey(algorithm);
  612. final AlgorithmParameterSpec spec = getAlgorithmParameterSpec(algorithm);
  613. int encryptMode = getEncryptMode(algorithm);
  614. c.init(encryptMode, encryptKey, spec);
  615. assertEquals(cipherID, getExpectedBlockSize(algorithm, encryptMode, providerName), c.getBlockSize());
  616. assertEquals(cipherID, getExpectedOutputSize(algorithm, encryptMode, providerName), c.getOutputSize(0));
  617. int decryptMode = getDecryptMode(algorithm);
  618. c.init(decryptMode, encryptKey, spec);
  619. assertEquals(cipherID, getExpectedBlockSize(algorithm, decryptMode, providerName), c.getBlockSize());
  620. assertEquals(cipherID, getExpectedOutputSize(algorithm, decryptMode, providerName), c.getOutputSize(0));
  621. // TODO: test Cipher.getIV()
  622. // TODO: test Cipher.getParameters()
  623. assertNull(cipherID, c.getExemptionMechanism());
  624. c.init(getEncryptMode(algorithm), encryptKey, spec);
  625. if (isWrap(algorithm)) {
  626. byte[] cipherText = c.wrap(encryptKey);
  627. c.init(getDecryptMode(algorithm), getDecryptKey(algorithm), spec);
  628. int keyType = (isAsymmetric(algorithm)) ? Cipher.PRIVATE_KEY : Cipher.SECRET_KEY;
  629. Key decryptedKey = c.unwrap(cipherText, encryptKey.getAlgorithm(), keyType);
  630. assertEquals("encryptKey.getAlgorithm()=" + encryptKey.getAlgorithm()
  631. + " decryptedKey.getAlgorithm()=" + decryptedKey.getAlgorithm()
  632. + " encryptKey.getEncoded()=" + Arrays.toString(encryptKey.getEncoded())
  633. + " decryptedKey.getEncoded()=" + Arrays.toString(decryptedKey.getEncoded()),
  634. encryptKey, decryptedKey);
  635. } else {
  636. byte[] cipherText = c.doFinal(ORIGINAL_PLAIN_TEXT);
  637. c.init(getDecryptMode(algorithm), getDecryptKey(algorithm), spec);
  638. byte[] decryptedPlainText = c.doFinal(cipherText);
  639. assertEquals(cipherID,
  640. Arrays.toString(getExpectedPlainText(algorithm)),
  641. Arrays.toString(decryptedPlainText));
  642. }
  643. }
  644. public void testInputPKCS1Padding() throws Exception {
  645. for (String provider : RSA_PROVIDERS) {
  646. testInputPKCS1Padding(provider);
  647. }
  648. }
  649. private void testInputPKCS1Padding(String provider) throws Exception {
  650. testInputPKCS1Padding(provider, PKCS1_BLOCK_TYPE_01_PADDED_PLAIN_TEXT, getEncryptKey("RSA"), getDecryptKey("RSA"));
  651. try {
  652. testInputPKCS1Padding(provider, PKCS1_BLOCK_TYPE_02_PADDED_PLAIN_TEXT, getEncryptKey("RSA"), getDecryptKey("RSA"));
  653. fail();
  654. } catch (BadPaddingException expected) {
  655. }
  656. try {
  657. testInputPKCS1Padding(provider, PKCS1_BLOCK_TYPE_01_PADDED_PLAIN_TEXT, getDecryptKey("RSA"), getEncryptKey("RSA"));
  658. fail();
  659. } catch (BadPaddingException expected) {
  660. }
  661. testInputPKCS1Padding(provider, PKCS1_BLOCK_TYPE_02_PADDED_PLAIN_TEXT, getDecryptKey("RSA"), getEncryptKey("RSA"));
  662. }
  663. private void testInputPKCS1Padding(String provider, byte[] prePaddedPlainText, Key encryptKey, Key decryptKey) throws Exception {
  664. Cipher encryptCipher = Cipher.getInstance("RSA/ECB/NoPadding", provider);
  665. encryptCipher.init(Cipher.ENCRYPT_MODE, encryptKey);
  666. byte[] cipherText = encryptCipher.doFinal(prePaddedPlainText);
  667. Cipher decryptCipher = Cipher.getInstance("RSA/ECB/PKCS1Padding", provider);
  668. decryptCipher.init(Cipher.DECRYPT_MODE, decryptKey);
  669. byte[] plainText = decryptCipher.doFinal(cipherText);
  670. assertEquals(Arrays.toString(ORIGINAL_PLAIN_TEXT),
  671. Arrays.toString(plainText));
  672. }
  673. public void testOutputPKCS1Padding() throws Exception {
  674. for (String provider : RSA_PROVIDERS) {
  675. testOutputPKCS1Padding(provider);
  676. }
  677. }
  678. private void testOutputPKCS1Padding(String provider) throws Exception {
  679. testOutputPKCS1Padding(provider, (byte) 1, getEncryptKey("RSA"), getDecryptKey("RSA"));
  680. testOutputPKCS1Padding(provider, (byte) 2, getDecryptKey("RSA"), getEncryptKey("RSA"));
  681. }
  682. private void testOutputPKCS1Padding(String provider, byte expectedBlockType, Key encryptKey, Key decryptKey) throws Exception {
  683. Cipher encryptCipher = Cipher.getInstance("RSA/ECB/PKCS1Padding", provider);
  684. encryptCipher.init(Cipher.ENCRYPT_MODE, encryptKey);
  685. byte[] cipherText = encryptCipher.doFinal(ORIGINAL_PLAIN_TEXT);
  686. Cipher decryptCipher = Cipher.getInstance("RSA/ECB/NoPadding", provider);
  687. decryptCipher.init(Cipher.DECRYPT_MODE, decryptKey);
  688. byte[] plainText = decryptCipher.doFinal(cipherText);
  689. assertPadding(provider, expectedBlockType, ORIGINAL_PLAIN_TEXT, plainText);
  690. }
  691. private void assertPadding(String provider, byte expectedBlockType, byte[] expectedData, byte[] actualDataWithPadding) {
  692. assertNotNull(provider, actualDataWithPadding);
  693. int expectedOutputSize = getExpectedOutputSize("RSA", Cipher.DECRYPT_MODE, provider);
  694. assertEquals(provider, expectedOutputSize, actualDataWithPadding.length);
  695. int expectedBlockTypeOffset;
  696. if (provider.equals("BC")) {
  697. // BC strips the leading 0 for us on decrypt even when NoPadding is specified...
  698. expectedBlockTypeOffset = 0;
  699. } else {
  700. expectedBlockTypeOffset = 1;
  701. assertEquals(provider, 0, actualDataWithPadding[0]);
  702. }
  703. byte actualBlockType = actualDataWithPadding[expectedBlockTypeOffset];
  704. assertEquals(provider, expectedBlockType, actualBlockType);
  705. int actualDataOffset = actualDataWithPadding.length - expectedData.length;
  706. if (actualBlockType == 1) {
  707. int expectedDataOffset = expectedBlockTypeOffset + 1;
  708. for (int i = expectedDataOffset; i < actualDataOffset - 1; i++) {
  709. assertEquals(provider, (byte) 0xFF, actualDataWithPadding[i]);
  710. }
  711. }
  712. assertEquals(provider, 0x00, actualDataWithPadding[actualDataOffset-1]);
  713. byte[] actualData = new byte[expectedData.length];
  714. System.arraycopy(actualDataWithPadding, actualDataOffset, actualData, 0, actualData.length);
  715. assertEquals(provider, Arrays.toString(expectedData), Arrays.toString(actualData));
  716. }
  717. public void testCipherInitWithCertificate () throws Exception {
  718. // no key usage specified, everything is fine
  719. assertCipherInitWithKeyUsage(0, true, true, true, true);
  720. // common case is that encrypt/wrap is prohibited when special usage is specified
  721. assertCipherInitWithKeyUsage(KeyUsage.digitalSignature, false, true, false, true);
  722. assertCipherInitWithKeyUsage(KeyUsage.nonRepudiation, false, true, false, true);
  723. assertCipherInitWithKeyUsage(KeyUsage.keyAgreement, false, true, false, true);
  724. assertCipherInitWithKeyUsage(KeyUsage.keyCertSign, false, true, false, true);
  725. assertCipherInitWithKeyUsage(KeyUsage.cRLSign, false, true, false, true);
  726. // Note they encipherOnly/decipherOnly don't have to do with
  727. // ENCRYPT_MODE or DECRYPT_MODE, but restrict usage relative
  728. // to keyAgreement. There is not a *_MODE option that
  729. // corresponds to this in Cipher, the RI does not enforce
  730. // anything in Cipher.
  731. // http://code.google.com/p/android/issues/detail?id=12955
  732. assertCipherInitWithKeyUsage(KeyUsage.encipherOnly, false, true, false, true);
  733. assertCipherInitWithKeyUsage(KeyUsage.decipherOnly, false, true, false, true);
  734. assertCipherInitWithKeyUsage(KeyUsage.keyAgreement | KeyUsage.encipherOnly,
  735. false, true, false, true);
  736. assertCipherInitWithKeyUsage(KeyUsage.keyAgreement | KeyUsage.decipherOnly,
  737. false, true, false, true);
  738. // except when wrapping a key is specifically allowed or
  739. assertCipherInitWithKeyUsage(KeyUsage.keyEncipherment, false, true, true, true);
  740. // except when wrapping data encryption is specifically allowed
  741. assertCipherInitWithKeyUsage(KeyUsage.dataEncipherment, true, true, false, true);
  742. }
  743. private void assertCipherInitWithKeyUsage (int keyUsage,
  744. boolean allowEncrypt,
  745. boolean allowDecrypt,
  746. boolean allowWrap,
  747. boolean allowUnwrap) throws Exception {
  748. Certificate certificate = certificateWithKeyUsage(keyUsage);
  749. assertCipherInitWithKeyUsage(certificate, allowEncrypt, Cipher.ENCRYPT_MODE);
  750. assertCipherInitWithKeyUsage(certificate, allowDecrypt, Cipher.DECRYPT_MODE);
  751. assertCipherInitWithKeyUsage(certificate, allowWrap, Cipher.WRAP_MODE);
  752. assertCipherInitWithKeyUsage(certificate, allowUnwrap, Cipher.UNWRAP_MODE);
  753. }
  754. private void assertCipherInitWithKeyUsage(Certificate certificate,
  755. boolean allowMode,
  756. int mode) throws Exception {
  757. Cipher cipher = Cipher.getInstance("RSA");
  758. if (allowMode) {
  759. cipher.init(mode, certificate);
  760. } else {
  761. try {
  762. cipher.init(mode, certificate);
  763. String modeString;
  764. switch (mode) {
  765. case Cipher.ENCRYPT_MODE:
  766. modeString = "ENCRYPT_MODE";
  767. break;
  768. case Cipher.DECRYPT_MODE:
  769. modeString = "DECRYPT_MODE";
  770. break;
  771. case Cipher.WRAP_MODE:
  772. modeString = "WRAP_MODE";
  773. break;
  774. case Cipher.UNWRAP_MODE:
  775. modeString = "UNWRAP_MODE";
  776. break;
  777. default:
  778. throw new AssertionError("Unknown Cipher.*_MODE " + mode);
  779. }
  780. fail("Should have had InvalidKeyException for " + modeString
  781. + " for " + certificate);
  782. } catch (InvalidKeyException expected) {
  783. }
  784. }
  785. }
  786. private Certificate certificateWithKeyUsage(int keyUsage) throws Exception {
  787. // note the rare usage of non-zero keyUsage
  788. return new TestKeyStore.Builder()
  789. .aliasPrefix("rsa-dsa-ec")
  790. .keyUsage(keyUsage)
  791. .build()
  792. .getPrivateKey("RSA", "RSA").getCertificate();
  793. }
  794. /*
  795. * Test vectors generated with this private key:
  796. *
  797. * -----BEGIN RSA PRIVATE KEY-----
  798. * MIIEpAIBAAKCAQEA4Ec+irjyKE/rnnQv+XSPoRjtmGM8kvUq63ouvg075gMpvnZq
  799. * 0Q62pRXQ0s/ZvqeTDwwwZTeJn3lYzT6FsB+IGFJNMSWEqUslHjYltUFB7b/uGYgI
  800. * 4buX/Hy0m56qr2jpyY19DtxTu8D6ADQ1bWMF+7zDxwAUBThqu8hzyw8+90JfPTPf
  801. * ezFa4DbSoLZq/UdQOxab8247UWJRW3Ff2oPeryxYrrmr+zCXw8yd2dvl7ylsF2E5
  802. * Ao6KZx5jBW1F9AGI0sQTNJCEXeUsJTTpxrJHjAe9rpKII7YtBmx3cPn2Pz26JH9T
  803. * CER0e+eqqF2FO4vSRKzsPePImrRkU6tNJMOsaQIDAQABAoIBADd4R3al8XaY9ayW
  804. * DfuDobZ1ZOZIvQWXz4q4CHGG8macJ6nsvdSA8Bl6gNBzCebGqW+SUzHlf4tKxvTU
  805. * XtpFojJpwJ/EKMB6Tm7fc4oV3sl/q9Lyu0ehTyDqcvz+TDbgGtp3vRN82NTaELsW
  806. * LpSkZilx8XX5hfoYjwVsuX7igW9Dq503R2Ekhs2owWGWwwgYqZXshdOEZ3kSZ7O/
  807. * IfJzcQppJYYldoQcW2cSwS1L0govMpmtt8E12l6VFavadufK8qO+gFUdBzt4vxFi
  808. * xIrSt/R0OgI47k0lL31efmUzzK5kzLOTYAdaL9HgNOw65c6cQIzL8OJeQRQCFoez
  809. * 3UdUroECgYEA9UGIS8Nzeyki1BGe9F4t7izUy7dfRVBaFXqlAJ+Zxzot8HJKxGAk
  810. * MGMy6omBd2NFRl3G3x4KbxQK/ztzluaomUrF2qloc0cv43dJ0U6z4HXmKdvrNYMz
  811. * im82SdCiZUp6Qv2atr+krE1IHTkLsimwZL3DEcwb4bYxidp8QM3s8rECgYEA6hp0
  812. * LduIHO23KIyH442GjdekCdFaQ/RF1Td6C1cx3b/KLa8oqOE81cCvzsM0fXSjniNa
  813. * PNljPydN4rlPkt9DgzkR2enxz1jyfeLgj/RZZMcg0+whOdx8r8kSlTzeyy81Wi4s
  814. * NaUPrXVMs7IxZkJLo7bjESoriYw4xcFe2yOGkzkCgYBRgo8exv2ZYCmQG68dfjN7
  815. * pfCvJ+mE6tiVrOYr199O5FoiQInyzBUa880XP84EdLywTzhqLNzA4ANrokGfVFeS
  816. * YtRxAL6TGYSj76Bb7PFBV03AebOpXEqD5sQ/MhTW3zLVEt4ZgIXlMeYWuD/X3Z0f
  817. * TiYHwzM9B8VdEH0dOJNYcQKBgQDbT7UPUN6O21P/NMgJMYigUShn2izKBIl3WeWH
  818. * wkQBDa+GZNWegIPRbBZHiTAfZ6nweAYNg0oq29NnV1toqKhCwrAqibPzH8zsiiL+
  819. * OVeVxcbHQitOXXSh6ajzDndZufwtY5wfFWc+hOk6XvFQb0MVODw41Fy9GxQEj0ch
  820. * 3IIyYQKBgQDYEUWTr0FfthLb8ZI3ENVNB0hiBadqO0MZSWjA3/HxHvD2GkozfV/T
  821. * dBu8lkDkR7i2tsR8OsEgQ1fTsMVbqShr2nP2KSlvX6kUbYl2NX08dR51FIaWpAt0
  822. * aFyCzjCQLWOdck/yTV4ulAfuNO3tLjtN9lqpvP623yjQe6aQPxZXaA==
  823. * -----END RSA PRIVATE KEY-----
  824. *
  825. */
  826. private static final BigInteger RSA_2048_modulus = new BigInteger(new byte[] {
  827. (byte) 0x00, (byte) 0xe0, (byte) 0x47, (byte) 0x3e, (byte) 0x8a, (byte) 0xb8, (byte) 0xf2, (byte) 0x28,
  828. (byte) 0x4f, (byte) 0xeb, (byte) 0x9e, (byte) 0x74, (byte) 0x2f, (byte) 0xf9, (byte) 0x74, (byte) 0x8f,
  829. (byte) 0xa1, (byte) 0x18, (byte) 0xed, (byte) 0x98, (byte) 0x63, (byte) 0x3c, (byte) 0x92, (byte) 0xf5,
  830. (byte) 0x2a, (byte) 0xeb, (byte) 0x7a, (byte) 0x2e, (byte) 0xbe, (byte) 0x0d, (byte) 0x3b, (byte) 0xe6,
  831. (byte) 0x03, (byte) 0x29, (byte) 0xbe, (byte) 0x76, (byte) 0x6a, (byte) 0xd1, (byte) 0x0e, (byte) 0xb6,
  832. (byte) 0xa5, (byte) 0x15, (byte) 0xd0, (byte) 0xd2, (byte) 0xcf, (byte) 0xd9, (byte) 0xbe, (byte) 0xa7,
  833. (byte) 0x93, (byte) 0x0f, (byte) 0x0c, (byte) 0x30, (byte) 0x65, (byte) 0x37, (byte) 0x89, (byte) 0x9f,
  834. (byte) 0x79, (byte) 0x58, (byte) 0xcd, (byte) 0x3e, (byte) 0x85, (byte) 0xb0, (byte) 0x1f, (byte) 0x88,
  835. (byte) 0x18, (byte) 0x52, (byte) 0x4d, (byte) 0x31, (byte) 0x25, (byte) 0x84, (byte) 0xa9, (byte) 0x4b,
  836. (byte) 0x25, (byte) 0x1e, (byte) 0x36, (byte) 0x25, (byte) 0xb5, (byte) 0x41, (byte) 0x41, (byte) 0xed,
  837. (byte) 0xbf, (byte) 0xee, (byte) 0x19, (byte) 0x88, (byte) 0x08, (byte) 0xe1, (byte) 0xbb, (byte) 0x97,
  838. (byte) 0xfc, (byte) 0x7c, (byte) 0xb4, (byte) 0x9b, (byte) 0x9e, (byte) 0xaa, (byte) 0xaf, (byte) 0x68,
  839. (byte) 0xe9, (byte) 0xc9, (byte) 0x8d, (byte) 0x7d, (byte) 0x0e, (byte) 0xdc, (byte) 0x53, (byte) 0xbb,
  840. (byte) 0xc0, (byte) 0xfa, (byte) 0x00, (byte) 0x34, (byte) 0x35, (byte) 0x6d, (byte) 0x63, (byte) 0x05,
  841. (byte) 0xfb, (byte) 0xbc, (byte) 0xc3, (byte) 0xc7, (byte) 0x00, (byte) 0x14, (byte) 0x05, (byte) 0x38,
  842. (byte) 0x6a, (byte) 0xbb, (byte) 0xc8, (byte) 0x73, (byte) 0xcb, (byte) 0x0f, (byte) 0x3e, (byte) 0xf7,
  843. (byte) 0x42, (byte) 0x5f, (byte) 0x3d, (byte) 0x33, (byte) 0xdf, (byte) 0x7b, (byte) 0x31, (byte) 0x5a,
  844. (byte) 0xe0, (byte) 0x36, (byte) 0xd2, (byte) 0xa0, (byte) 0xb6, (byte) 0x6a, (byte) 0xfd, (byte) 0x47,
  845. (byte) 0x50, (byte) 0x3b, (byte) 0x16, (byte) 0x9b, (byte) 0xf3, (byte) 0x6e, (byte) 0x3b, (byte) 0x51,
  846. (byte) 0x62, (byte) 0x51, (byte) 0x5b, (byte) 0x71, (byte) 0x5f, (byte) 0xda, (byte) 0x83, (byte) 0xde,
  847. (byte) 0xaf, (byte) 0x2c, (byte) 0x58, (byte) 0xae, (byte) 0xb9, (byte) 0xab, (byte) 0xfb, (byte) 0x30,
  848. (byte) 0x97, (byte) 0xc3, (byte) 0xcc, (byte) 0x9d, (byte) 0xd9, (byte) 0xdb, (byte) 0xe5, (byte) 0xef,
  849. (byte) 0x29, (byte) 0x6c, (byte) 0x17, (byte) 0x61, (byte) 0x39, (byte) 0x02, (byte) 0x8e, (byte) 0x8a,
  850. (byte) 0x67, (byte) 0x1e, (byte) 0x63, (byte) 0x05, (byte) 0x6d, (byte) 0x45, (byte) 0xf4, (byte) 0x01,
  851. (byte) 0x88, (byte) 0xd2, (byte) 0xc4, (byte) 0x13, (byte) 0x34, (byte) 0x90, (byte) 0x84, (byte) 0x5d,
  852. (byte) 0xe5, (byte) 0x2c, (byte) 0x25, (byte) 0x34, (byte) 0xe9, (byte) 0xc6, (byte) 0xb2, (byte) 0x47,
  853. (byte) 0x8c, (byte) 0x07, (byte) 0xbd, (byte) 0xae, (byte) 0x92, (byte) 0x88, (byte) 0x23, (byte) 0xb6,
  854. (byte) 0x2d, (byte) 0x06, (byte) 0x6c, (byte) 0x77, (byte) 0x70, (byte) 0xf9, (byte) 0xf6, (byte) 0x3f,
  855. (byte) 0x3d, (byte) 0xba, (byte) 0x24, (byte) 0x7f, (byte) 0x53, (byte) 0x08, (byte) 0x44, (byte) 0x74,
  856. (byte) 0x7b, (byte) 0xe7, (byte) 0xaa, (byte) 0xa8, (byte) 0x5d, (byte) 0x85, (byte) 0x3b, (byte) 0x8b,
  857. (byte) 0xd2, (byte) 0x44, (byte) 0xac, (byte) 0xec, (byte) 0x3d, (byte) 0xe3, (byte) 0xc8, (byte) 0x9a,
  858. (byte) 0xb4, (byte) 0x64, (byte) 0x53, (byte) 0xab, (byte) 0x4d, (byte) 0x24, (byte) 0xc3, (byte) 0xac,
  859. (byte) 0x69,
  860. });
  861. private static final BigInteger RSA_2048_privateExponent = new BigInteger(new byte[] {
  862. (byte) 0x37, (byte) 0x78, (byte) 0x47, (byte) 0x76, (byte) 0xa5, (byte) 0xf1, (byte) 0x76, (byte) 0x98,
  863. (byte) 0xf5, (byte) 0xac, (byte) 0x96, (byte) 0x0d, (byte) 0xfb, (byte) 0x83, (byte) 0xa1, (byte) 0xb6,
  864. (byte) 0x75, (byte) 0x64, (byte) 0xe6, (byte) 0x48, (byte) 0xbd, (byte) 0x05, (byte) 0x97, (byte) 0xcf,
  865. (byte) 0x8a, (byte) 0xb8, (byte) 0x08, (byte) 0x71, (byte) 0x86, (byte) 0xf2, (byte) 0x66, (byte) 0x9c,
  866. (byte) 0x27, (byte) 0xa9, (byte) 0xec, (byte) 0xbd, (byte) 0xd4, (byte) 0