PageRenderTime 674ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/java/crypto/test/src/org/bouncycastle/jce/provider/test/BlockCipherTest.java

https://github.com/jsiwek/BouncyCastleSSLv3
Java | 932 lines | 766 code | 105 blank | 61 comment | 30 complexity | b8ff6d85e17b9d54ebc351cf9f3e160e MD5 | raw file
  1. package org.bouncycastle.jce.provider.test;
  2. import org.bouncycastle.jce.provider.BouncyCastleProvider;
  3. import org.bouncycastle.util.encoders.Hex;
  4. import org.bouncycastle.util.test.SimpleTest;
  5. import javax.crypto.Cipher;
  6. import javax.crypto.CipherInputStream;
  7. import javax.crypto.CipherOutputStream;
  8. import javax.crypto.IllegalBlockSizeException;
  9. import javax.crypto.KeyGenerator;
  10. import javax.crypto.SecretKey;
  11. import javax.crypto.SecretKeyFactory;
  12. import javax.crypto.ShortBufferException;
  13. import javax.crypto.spec.DESedeKeySpec;
  14. import javax.crypto.spec.IvParameterSpec;
  15. import javax.crypto.spec.RC2ParameterSpec;
  16. import javax.crypto.spec.RC5ParameterSpec;
  17. import javax.crypto.spec.SecretKeySpec;
  18. import java.io.ByteArrayInputStream;
  19. import java.io.ByteArrayOutputStream;
  20. import java.io.DataInputStream;
  21. import java.io.IOException;
  22. import java.security.AlgorithmParameters;
  23. import java.security.InvalidAlgorithmParameterException;
  24. import java.security.InvalidKeyException;
  25. import java.security.InvalidParameterException;
  26. import java.security.Key;
  27. import java.security.PrivateKey;
  28. import java.security.PublicKey;
  29. import java.security.SecureRandom;
  30. import java.security.Security;
  31. import java.security.spec.InvalidKeySpecException;
  32. import java.security.spec.KeySpec;
  33. /**
  34. * basic test class for a block cipher, basically this just exercises the provider, and makes sure we
  35. * are behaving sensibly, correctness of the implementation is shown in the lightweight test classes.
  36. */
  37. public class BlockCipherTest
  38. extends SimpleTest
  39. {
  40. static String[] cipherTests1 =
  41. {
  42. "DES",
  43. "466da00648ef0e1f9617b1f002e225251a3248d09172f46b9617b1f002e225250112ecb3da61bc99",
  44. "DESede",
  45. "2f4bc6b30c893fa549d82c560d61cf3eb088aed020603de249d82c560d61cf3e529e95ecd8e05394",
  46. "SKIPJACK",
  47. "d4de46d52274dbb029f33b076043f8c40089f906751623de29f33b076043f8c4ac99b90f9396cb04",
  48. "Blowfish",
  49. "7870ebe7f6a52803eb9396ba6c5198216ce81d76d8d4c74beb9396ba6c5198211212473b05214e9f",
  50. "Twofish",
  51. "70336d9c9718a8a2ced1b19deed973a3c58af7ea71a69e7efc4df082dca581c0839e31468661bcfc57a14899ceeb0253",
  52. "RC2",
  53. "eb5b889bbcced12eb6b1a3da6a3d965bba66a5edfdd4c8a6b6b1a3da6a3d965b994a5b859e765797",
  54. "RC5",
  55. "220053543e3eca3bc9503a091ca67b08372560d8a4fdbee8c9503a091ca67b08a796d53bb8a4b7e0",
  56. "RC5-64",
  57. "e0b4a526ba3bc5f09199c3b1fe3737fe6d248cde70e565b0feea59ebfda375ae1946c386a48d8d8a74d7b1947ff6a788",
  58. "RC6",
  59. "44c97b67ca8486067f8b6c5b97632f3049e5e52c1d61fdd527dc3da39616540f19a3db39aac1ffd713795cd886cce0c0",
  60. "IDEA",
  61. "8c9fd56823ffdc523f6ccf7f614aa6173553e594fc7a21b53f6ccf7f614aa61740c54f7a66e95108",
  62. "TEA",
  63. "fcf45062104fda7c35712368b56dd4216a6ca998dc297b5435712368b56dd421208027ed2923cd0c",
  64. "XTEA",
  65. "4b427893d3d6aaded2afafabe25f7b233fb5589faa2b6389d2afafabe25f7b239d12979ac67e1c07",
  66. "Camellia",
  67. "3a68b4ad145bc2c76010669d68f2826359887afce763a78d9994143266adfaec8ba7ee562a1688ef9dfd7f897e5c44dc",
  68. "SEED",
  69. "d53d4ce1f48b9879420949467bfcbfbe2c6a7d4a8770bee0c71211def898d7c5024ce2007dd85accb3f69d906ae2164d",
  70. "Noekeon",
  71. "7e68ceb33aad9db04af6b878a16dd6c6b4f880d6c89027ba581884c10690bb6b3dbfd6ed5513e2c4f5670c3528023121",
  72. "DES/CBC/NoPadding",
  73. "60fa2f8fae5aa2a38e9ac77d0246726beb7511e4515feb12cf99f75cc6e0122a",
  74. "DESede/CBC/NoPadding",
  75. "4d3d7931875cf25593dc402298add8b914761e4936c9585ae22b2c1441169231",
  76. "SKIPJACK/CBC/NoPadding",
  77. "ceebcc2e5e2b847f9ed797b4930b95f115b9e6cf49c457fc2ea0df79ad5c8334",
  78. "Blowfish/CBC/NoPadding",
  79. "80823abbabc109733e7ebf3ce3344d67fc387c306b782086b452f7fbe8e844ce",
  80. "Twofish/CBC/NoPadding",
  81. "f819694251a00bdd403928745cd1d8a094de61f49ddf8e7692e9d81a83812943",
  82. "RC2/CBC/NoPadding",
  83. "a51facdb3933c9676795cd38cc3146fd4694722b468b1a979a399c77606abf99",
  84. "RC5/CBC/NoPadding",
  85. "9ee7517eab0280445f3a7c60c90c0f75029d65bca8b1af83ace5399d388c83c3",
  86. "RC6/CBC/NoPadding",
  87. "c44695633c07010f3a0d8f7ea046a642d4a96bf4e44f89fd91b46830bc95b130",
  88. "IDEA/CBC/NoPadding",
  89. "30cd990ebdae80fe12b6c6e4fcd1c064a27d985c276b3d7097351c8684e4c4d9",
  90. "DES/CBC/PKCS5Padding",
  91. "60fa2f8fae5aa2a38e9ac77d0246726beb7511e4515feb12cf99f75cc6e0122afdc70484fb9c0232",
  92. "DES/CBC/ISO10126Padding",
  93. "60fa2f8fae5aa2a38e9ac77d0246726beb7511e4515feb12cf99f75cc6e0122a980639850a2cc3e8",
  94. "DES/CBC/ISO7816-4Padding",
  95. "60fa2f8fae5aa2a38e9ac77d0246726beb7511e4515feb12cf99f75cc6e0122a1f80b9b0f1be49ac",
  96. "DES/CBC/X9.23Padding",
  97. "60fa2f8fae5aa2a38e9ac77d0246726beb7511e4515feb12cf99f75cc6e0122a980639850a2cc3e8",
  98. "DESede/CBC/PKCS7Padding",
  99. "4d3d7931875cf25593dc402298add8b914761e4936c9585ae22b2c1441169231a41e40695f1cff84",
  100. "SKIPJACK/CBC/PKCS7Padding",
  101. "ceebcc2e5e2b847f9ed797b4930b95f115b9e6cf49c457fc2ea0df79ad5c8334df7042de5db89c96",
  102. "Blowfish/CBC/PKCS7Padding",
  103. "80823abbabc109733e7ebf3ce3344d67fc387c306b782086b452f7fbe8e844cef986562ab1a675e8",
  104. "Twofish/CBC/PKCS7Padding",
  105. "f819694251a00bdd403928745cd1d8a094de61f49ddf8e7692e9d81a838129433e5f1343d6cdb0b41838619da1541f04",
  106. "RC2/CBC/PKCS7Padding",
  107. "a51facdb3933c9676795cd38cc3146fd4694722b468b1a979a399c77606abf9958435525f770f137",
  108. "RC5/CBC/PKCS7Padding",
  109. "9ee7517eab0280445f3a7c60c90c0f75029d65bca8b1af83ace5399d388c83c3edd95ff49be76651",
  110. "RC5-64/CBC/PKCS7Padding",
  111. "e479fd11f89dab22d2f3dd062b1d2abd5b5962553421a5c562dc7214c3b23b8e21949fda87f2f820e5f032c552c6ec78",
  112. "RC6/CBC/PKCS7Padding",
  113. "c44695633c07010f3a0d8f7ea046a642d4a96bf4e44f89fd91b46830bc95b130824b972c9019a69d2dd05ef2d36b37ac",
  114. "IDEA/CBC/PKCS7Padding",
  115. "30cd990ebdae80fe12b6c6e4fcd1c064a27d985c276b3d7097351c8684e4c4d9e584751325ef7c32",
  116. "IDEA/CBC/ISO10126Padding",
  117. "30cd990ebdae80fe12b6c6e4fcd1c064a27d985c276b3d7097351c8684e4c4d978b3fd73135f033b",
  118. "IDEA/CBC/X9.23Padding",
  119. "30cd990ebdae80fe12b6c6e4fcd1c064a27d985c276b3d7097351c8684e4c4d978b3fd73135f033b",
  120. "AES/CBC/PKCS7Padding",
  121. "cf87f4d8bb9d1abb36cdd9f44ead7d046db2f802d99e1ef0a5940f306079e08389a44c4a8cc1a47cbaee1128da55bbb7",
  122. "AES/CBC/ISO7816-4Padding",
  123. "cf87f4d8bb9d1abb36cdd9f44ead7d046db2f802d99e1ef0a5940f306079e08306d84876508a33efec701118d8eeaf6d",
  124. "Rijndael/CBC/PKCS7Padding",
  125. "cf87f4d8bb9d1abb36cdd9f44ead7d046db2f802d99e1ef0a5940f306079e08389a44c4a8cc1a47cbaee1128da55bbb7",
  126. "Serpent/CBC/PKCS7Padding",
  127. "f8940ca31aba8ce1e0693b1ae0b1e08daef6de03c80f019774280052f824ac44540bb8dd74dfad47f83f9c7ec268ca68",
  128. "CAST5/CBC/PKCS7Padding",
  129. "87b6dc0c5a1d23d42fa740b0548be0b298112000544610d889d6361994cf8e670a19d6af72d7289f",
  130. "CAST6/CBC/PKCS7Padding",
  131. "943445569cfdda174118e433828f84e137faee38cac5c827d87a3c9a5a46a07dd64e7ad8accd921f248eea627cd6826f",
  132. "DES/CBC/WithCTS",
  133. "60fa2f8fae5aa2a38e9ac77d0246726bcf99f75cc6e0122aeb7511e4515feb12",
  134. "IDEA/CBC/PKCS7Padding",
  135. "30cd990ebdae80fe12b6c6e4fcd1c064a27d985c276b3d7097351c8684e4c4d9e584751325ef7c32",
  136. "DES/CBC/ZeroBytePadding",
  137. "60fa2f8fae5aa2a38e9ac77d0246726beb7511e4515feb12cf99f75cc6e0122ad3b3f002c927f1fd",
  138. "DES/CTS/NoPadding", // official style
  139. "60fa2f8fae5aa2a38e9ac77d0246726bcf99f75cc6e0122aeb7511e4515feb12",
  140. "DESede/CTS/NoPadding",
  141. "4d3d7931875cf25593dc402298add8b9e22b2c144116923114761e4936c9585a",
  142. "SKIPJACK/CTS/NoPadding",
  143. "ceebcc2e5e2b847f9ed797b4930b95f12ea0df79ad5c833415b9e6cf49c457fc",
  144. "Blowfish/CTS/NoPadding",
  145. "80823abbabc109733e7ebf3ce3344d67b452f7fbe8e844cefc387c306b782086",
  146. "Twofish/CTS/NoPadding",
  147. "94de61f49ddf8e7692e9d81a83812943f819694251a00bdd403928745cd1d8a0",
  148. "AES/CTS/NoPadding",
  149. "6db2f802d99e1ef0a5940f306079e083cf87f4d8bb9d1abb36cdd9f44ead7d04",
  150. "Rijndael/CTS/NoPadding",
  151. "6db2f802d99e1ef0a5940f306079e083cf87f4d8bb9d1abb36cdd9f44ead7d04",
  152. "Serpent/CTS/NoPadding",
  153. "aef6de03c80f019774280052f824ac44f8940ca31aba8ce1e0693b1ae0b1e08d",
  154. "CAST5/CTS/NoPadding",
  155. "87b6dc0c5a1d23d42fa740b0548be0b289d6361994cf8e6798112000544610d8",
  156. "CAST6/CTS/NoPadding",
  157. "37faee38cac5c827d87a3c9a5a46a07d943445569cfdda174118e433828f84e1",
  158. "RC2/CTS/NoPadding",
  159. "a51facdb3933c9676795cd38cc3146fd9a399c77606abf994694722b468b1a97",
  160. "RC5/CTS/NoPadding",
  161. "9ee7517eab0280445f3a7c60c90c0f75ace5399d388c83c3029d65bca8b1af83",
  162. "RC6/CTS/NoPadding",
  163. "d4a96bf4e44f89fd91b46830bc95b130c44695633c07010f3a0d8f7ea046a642",
  164. "IDEA/CTS/NoPadding",
  165. "30cd990ebdae80fe12b6c6e4fcd1c06497351c8684e4c4d9a27d985c276b3d70",
  166. "DES/CBC/WithCTS", // older style
  167. "60fa2f8fae5aa2a38e9ac77d0246726bcf99f75cc6e0122aeb7511e4515feb12",
  168. "DESede/CBC/WithCTS",
  169. "4d3d7931875cf25593dc402298add8b9e22b2c144116923114761e4936c9585a",
  170. "SKIPJACK/CBC/WithCTS",
  171. "ceebcc2e5e2b847f9ed797b4930b95f12ea0df79ad5c833415b9e6cf49c457fc",
  172. "Blowfish/CBC/WithCTS",
  173. "80823abbabc109733e7ebf3ce3344d67b452f7fbe8e844cefc387c306b782086",
  174. "Twofish/CBC/WithCTS",
  175. "94de61f49ddf8e7692e9d81a83812943f819694251a00bdd403928745cd1d8a0",
  176. "AES/CBC/WithCTS",
  177. "6db2f802d99e1ef0a5940f306079e083cf87f4d8bb9d1abb36cdd9f44ead7d04",
  178. "Rijndael/CBC/WithCTS",
  179. "6db2f802d99e1ef0a5940f306079e083cf87f4d8bb9d1abb36cdd9f44ead7d04",
  180. "Serpent/CBC/WithCTS",
  181. "aef6de03c80f019774280052f824ac44f8940ca31aba8ce1e0693b1ae0b1e08d",
  182. "CAST5/CBC/WithCTS",
  183. "87b6dc0c5a1d23d42fa740b0548be0b289d6361994cf8e6798112000544610d8",
  184. "CAST6/CBC/WithCTS",
  185. "37faee38cac5c827d87a3c9a5a46a07d943445569cfdda174118e433828f84e1",
  186. "RC2/CBC/WithCTS",
  187. "a51facdb3933c9676795cd38cc3146fd9a399c77606abf994694722b468b1a97",
  188. "RC5/CBC/WithCTS",
  189. "9ee7517eab0280445f3a7c60c90c0f75ace5399d388c83c3029d65bca8b1af83",
  190. "RC6/CBC/WithCTS",
  191. "d4a96bf4e44f89fd91b46830bc95b130c44695633c07010f3a0d8f7ea046a642",
  192. "IDEA/CBC/WithCTS",
  193. "30cd990ebdae80fe12b6c6e4fcd1c06497351c8684e4c4d9a27d985c276b3d70",
  194. "DES/OFB/NoPadding",
  195. "537572e480c1714f5c9a4f3b874df824dc6681b1fd6c11982debcad91e3f78b7",
  196. "DESede/OFB/NoPadding",
  197. "481e9872acea7fcf8e29a453242da774e5f6a28f15f7723659a73e4ff4939f80",
  198. "SKIPJACK/OFB/NoPadding",
  199. "71143a124e3a0cde753b60fe9b200e559018b6a0fe0682659f7c13feb9df995c",
  200. "Blowfish/OFB/NoPadding",
  201. "6cd6f7c5d2c655556d7a9e98a1696d1875e9f1b2fc991e28a2d55b56861e80bd",
  202. "Twofish/OFB/NoPadding",
  203. "821c54b1b54ae113cf74595eefe10c83b61c9682fc81f92c52f39a3a693f88b8",
  204. "RC2/OFB/NoPadding",
  205. "0a07cb78537cb04c0c74e28a7b86b80f80acadf87d6ef32792f1a8cf74b39f74",
  206. "RC5/OFB/NoPadding",
  207. "c62b233df296283b918a2b4cc53a54fbf061850e781b97332ed1bd78b88d9670",
  208. "IDEA/OFB/NoPadding",
  209. "dd447da3cbdcf81f4053fb446596261cb00a3c49a66085485af5f7c10ba20dad",
  210. "DES/OFB8/NoPadding",
  211. "53cb5010d189f94cf584e5ff1c4a9d86443c45ddb6fa3c2d1a5dadfcdf01db8a",
  212. "DESede/OFB8/NoPadding",
  213. "482c0c1ccd0e6d218e1cffb0a295352c2357ffaa673f2257ef5c77b6c04f03b5",
  214. "SKIPJACK/OFB8/NoPadding",
  215. "719ea1b432b3d2c8011e5aa873f95978420022b5e2c9c1a1c1082cd1f4999da2",
  216. "Blowfish/OFB8/NoPadding",
  217. "6ca6078755b263f09787d830b6fda7b7748494634bdc73ab68540cf9f6b7eccf",
  218. "Twofish/OFB8/NoPadding",
  219. "825dcec234ad52253d6e064b0d769bc04b1142435933f4a510ffc20d70095a88",
  220. "RC2/OFB8/NoPadding",
  221. "0aa26c6f6a820fe7d38da97085995ad62e2e293323a76300fcd4eb572810f7c6",
  222. "RC5/OFB8/NoPadding",
  223. "c601a9074dbd874f4d3293f6a32d93d9f0a4f5685d8597f0102fcc96d444f976",
  224. "IDEA/OFB8/NoPadding",
  225. "dd7897b6ced43d060a518bb38d570308b83b4de577eb208130daabf619e9b1fb",
  226. "DES/CFB/NoPadding",
  227. "537572e480c1714fec3c7424f88d4202219244c5ca8f5e4361d64f08fe747bb2",
  228. "DESede/CFB/NoPadding",
  229. "481e9872acea7fcfb75bb58670fe64c59123265139e357d161cd4ddb5eba042a",
  230. "SKIPJACK/CFB/NoPadding",
  231. "71143a124e3a0cde70a69ede4ceb14376b1e6a80bafde0a6330508dfa86a7c41",
  232. "Blowfish/CFB/NoPadding",
  233. "6cd6f7c5d2c6555561167fe9b10665102206869339122f1ed89efa4a985397f6",
  234. "Twofish/CFB/NoPadding",
  235. "821c54b1b54ae113cf74595eefe10c8308b7a438277de4f40948ac2d172d53d2",
  236. "RC2/CFB/NoPadding",
  237. "0a07cb78537cb04ca1401450d5cd411c7da7fa5b6baaa17bb2137bd95c9f26a5",
  238. "RC5/CFB/NoPadding",
  239. "c62b233df296283b989352bbebf616a19e11503ac737f9e0eaf19049cde05d34",
  240. "IDEA/CFB/NoPadding",
  241. "dd447da3cbdcf81fcbe4661dcbed88aed899f87585118384bd0565067fa6c13a",
  242. "DES/CFB8/NoPadding",
  243. "53cb0cdff712a825eb283b23c31e7323aa12495e7e751428b5c4eb89b28a25d4",
  244. "DESede/CFB8/NoPadding",
  245. "482cd5bf87ca4cee0b573d66a077231bfea93843ce2d1f948550a1d208e18279",
  246. "SKIPJACK/CFB8/NoPadding",
  247. "719eef3906bef23f7b63599285437d8e34183b165acf3e855b4e160d4f036508",
  248. "Blowfish/CFB8/NoPadding",
  249. "6ca63aaada9188d2410c07513cc0736b9888770768c25a5befc776beea5bdc4c",
  250. "Twofish/CFB8/NoPadding",
  251. "825d12af040721cf5ed4a4798647837ac5eb14d752aace28728aeb37b2010abd",
  252. "RC2/CFB8/NoPadding",
  253. "0aa227f94be3a32ff927c5d25647ea41d7c2a1e94012fc7f2ad6767b9664bce5",
  254. "RC5/CFB8/NoPadding",
  255. "c601cf88725411f119965b9cd38d6c313b91128ed7c98c7604cc62d9b210be79",
  256. "IDEA/CFB8/NoPadding",
  257. "dd7839d2525420d10f95eec23dbaf3463302c445972a28c563c2635191bc19af",
  258. "IDEA/PGPCFB/NoPadding",
  259. "dd447da3cbdcf81fcbe4661dcbed88aed899f87585118384bd0565067fa6c13a",
  260. "IDEA/PGPCFBwithIv/NoPadding",
  261. "ed5adbac0e730cc0f00df7e4f6fef672ab042673106435faf3ecf3996a72a0e127b440ba9e5313501de3",
  262. "Twofish/ECB/TBCPadding",
  263. "70336d9c9718a8a2ced1b19deed973a3c58af7ea71a69e7efc4df082dca581c019d7daa58d02b89aab6e8c0d17202439",
  264. "RC2/ECB/TBCPadding",
  265. "eb5b889bbcced12eb6b1a3da6a3d965bba66a5edfdd4c8a6b6b1a3da6a3d965b6b5359ba5e69b179"
  266. };
  267. static String[] cipherTests2 =
  268. {
  269. "DES/OFB64/NoPadding",
  270. "537572e480c1714f5c9a4f3b874df824dc6681b1fd6c11982debcad91e",
  271. "DES/CFB64/NoPadding",
  272. "537572e480c1714fec3c7424f88d4202219244c5ca8f5e4361d64f08fe",
  273. "DES/CTR/NoPadding",
  274. "537572e480c1714fb47081d35eb18eaca9e0a5aee982f105438a0db6ce",
  275. "DES/CTS/NoPadding",
  276. "60fa2f8fae5aa2a38e9ac77d0246726b32df660db51a710ceb7511e451"
  277. };
  278. static byte[] input1 = Hex.decode("000102030405060708090a0b0c0d0e0fff0102030405060708090a0b0c0d0e0f");
  279. static byte[] input2 = Hex.decode("000102030405060708090a0b0c0d0e0fff0102030405060708090a0b0c");
  280. static RC2ParameterSpec rc2Spec = new RC2ParameterSpec(128, Hex.decode("0123456789abcdef"));
  281. static RC5ParameterSpec rc5Spec = new RC5ParameterSpec(16, 16, 32, Hex.decode("0123456789abcdef"));
  282. static RC5ParameterSpec rc564Spec = new RC5ParameterSpec(16, 16, 64, Hex.decode("0123456789abcdef0123456789abcdef"));
  283. /**
  284. * a fake random number generator - we just want to make sure the random numbers
  285. * aren't random so that we get the same output, while still getting to test the
  286. * key generation facilities.
  287. */
  288. private class FixedSecureRandom
  289. extends SecureRandom
  290. {
  291. byte[] seed = {
  292. (byte)0xaa, (byte)0xfd, (byte)0x12, (byte)0xf6, (byte)0x59,
  293. (byte)0xca, (byte)0xe6, (byte)0x34, (byte)0x89, (byte)0xb4,
  294. (byte)0x79, (byte)0xe5, (byte)0x07, (byte)0x6d, (byte)0xde,
  295. (byte)0xc2, (byte)0xf0, (byte)0x6c, (byte)0xb5, (byte)0x8f
  296. };
  297. public void nextBytes(
  298. byte[] bytes)
  299. {
  300. int offset = 0;
  301. while ((offset + seed.length) < bytes.length)
  302. {
  303. System.arraycopy(seed, 0, bytes, offset, seed.length);
  304. offset += seed.length;
  305. }
  306. System.arraycopy(seed, 0, bytes, offset, bytes.length - offset);
  307. }
  308. }
  309. public String getName()
  310. {
  311. return "BlockCipher";
  312. }
  313. public void test(
  314. String algorithm,
  315. byte[] input,
  316. byte[] output)
  317. {
  318. Key key = null;
  319. KeyGenerator keyGen;
  320. SecureRandom rand;
  321. Cipher in = null;
  322. Cipher out = null;
  323. CipherInputStream cIn;
  324. CipherOutputStream cOut;
  325. ByteArrayInputStream bIn;
  326. ByteArrayOutputStream bOut;
  327. rand = new FixedSecureRandom();
  328. try
  329. {
  330. String baseAlgorithm;
  331. int index = algorithm.indexOf('/');
  332. if (index > 0)
  333. {
  334. baseAlgorithm = algorithm.substring(0, index);
  335. }
  336. else
  337. {
  338. baseAlgorithm = algorithm;
  339. }
  340. if (baseAlgorithm.equals("IDEA") & noIDEA())
  341. {
  342. return;
  343. }
  344. keyGen = KeyGenerator.getInstance(baseAlgorithm, "BC");
  345. if (!keyGen.getAlgorithm().equals(baseAlgorithm))
  346. {
  347. fail("wrong key generator returned!");
  348. }
  349. keyGen.init(rand);
  350. key = keyGen.generateKey();
  351. in = Cipher.getInstance(algorithm, "BC");
  352. out = Cipher.getInstance(algorithm, "BC");
  353. if (!in.getAlgorithm().startsWith(baseAlgorithm))
  354. {
  355. fail("wrong cipher returned!");
  356. }
  357. if (algorithm.startsWith("RC2"))
  358. {
  359. out.init(Cipher.ENCRYPT_MODE, key, rc2Spec, rand);
  360. }
  361. else if (algorithm.startsWith("RC5"))
  362. {
  363. if (algorithm.startsWith("RC5-64"))
  364. {
  365. out.init(Cipher.ENCRYPT_MODE, key, rc564Spec, rand);
  366. }
  367. else
  368. {
  369. out.init(Cipher.ENCRYPT_MODE, key, rc5Spec, rand);
  370. }
  371. }
  372. else
  373. {
  374. out.init(Cipher.ENCRYPT_MODE, key, rand);
  375. }
  376. }
  377. catch (Exception e)
  378. {
  379. fail("" + algorithm + " failed initialisation - " + e.toString(), e);
  380. }
  381. //
  382. // grab the iv if there is one
  383. //
  384. try
  385. {
  386. if (algorithm.startsWith("RC2"))
  387. {
  388. in.init(Cipher.DECRYPT_MODE, key, rc2Spec);
  389. }
  390. else if (algorithm.startsWith("RC5"))
  391. {
  392. if (algorithm.startsWith("RC5-64"))
  393. {
  394. in.init(Cipher.DECRYPT_MODE, key, rc564Spec, rand);
  395. }
  396. else
  397. {
  398. in.init(Cipher.DECRYPT_MODE, key, rc5Spec, rand);
  399. }
  400. }
  401. else
  402. {
  403. byte[] iv;
  404. iv = out.getIV();
  405. if (iv != null)
  406. {
  407. try
  408. {
  409. byte[] nIv = new byte[iv.length - 1];
  410. in.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(nIv));
  411. fail("failed to pick up short IV");
  412. }
  413. catch (InvalidAlgorithmParameterException e)
  414. {
  415. // ignore - this is what we want...
  416. }
  417. IvParameterSpec spec;
  418. spec = new IvParameterSpec(iv);
  419. in.init(Cipher.DECRYPT_MODE, key, spec);
  420. }
  421. else
  422. {
  423. in.init(Cipher.DECRYPT_MODE, key);
  424. }
  425. }
  426. }
  427. catch (Exception e)
  428. {
  429. fail("" + algorithm + " failed initialisation - " + e.toString());
  430. }
  431. //
  432. // encryption pass
  433. //
  434. bOut = new ByteArrayOutputStream();
  435. cOut = new CipherOutputStream(bOut, out);
  436. try
  437. {
  438. for (int i = 0; i != input.length / 2; i++)
  439. {
  440. cOut.write(input[i]);
  441. }
  442. cOut.write(input, input.length / 2, input.length - input.length / 2);
  443. cOut.close();
  444. }
  445. catch (IOException e)
  446. {
  447. fail("" + algorithm + " failed encryption - " + e.toString());
  448. }
  449. byte[] bytes;
  450. bytes = bOut.toByteArray();
  451. if (!areEqual(bytes, output))
  452. {
  453. fail("" + algorithm + " failed encryption - expected " + new String(Hex.encode(output)) + " got " + new String(Hex.encode(bytes)));
  454. }
  455. //
  456. // decryption pass
  457. //
  458. bIn = new ByteArrayInputStream(bytes);
  459. cIn = new CipherInputStream(bIn, in);
  460. try
  461. {
  462. DataInputStream dIn = new DataInputStream(cIn);
  463. bytes = new byte[input.length];
  464. for (int i = 0; i != input.length / 2; i++)
  465. {
  466. bytes[i] = (byte)dIn.read();
  467. }
  468. dIn.readFully(bytes, input.length / 2, bytes.length - input.length / 2);
  469. }
  470. catch (Exception e)
  471. {
  472. fail("" + algorithm + " failed decryption - " + e.toString());
  473. }
  474. if (!areEqual(bytes, input))
  475. {
  476. fail("" + algorithm + " failed decryption - expected " + new String(Hex.encode(input)) + " got " + new String(Hex.encode(bytes)));
  477. }
  478. }
  479. private boolean noIDEA()
  480. {
  481. try
  482. {
  483. Cipher.getInstance("IDEA", "BC");
  484. return false;
  485. }
  486. catch (Exception e)
  487. {
  488. return true;
  489. }
  490. }
  491. private void testExceptions()
  492. {
  493. SecretKeyFactory skF = null;
  494. try
  495. {
  496. skF = SecretKeyFactory.getInstance("DESede", "BC");
  497. }
  498. catch (Exception e)
  499. {
  500. fail("unexpected exception.", e);
  501. }
  502. KeySpec ks = null;
  503. SecretKey secKey = null;
  504. byte[] bb = new byte[24];
  505. try
  506. {
  507. skF.getKeySpec(null, null);
  508. fail("failed exception test - no exception thrown");
  509. }
  510. catch (InvalidKeySpecException e)
  511. {
  512. // ignore okay
  513. }
  514. catch (Exception e)
  515. {
  516. fail("failed exception test.", e);
  517. }
  518. try
  519. {
  520. ks = (KeySpec)new DESedeKeySpec(bb);
  521. skF.getKeySpec(null, ks.getClass());
  522. fail("failed exception test - no exception thrown");
  523. }
  524. catch (InvalidKeySpecException e)
  525. {
  526. // ignore okay;
  527. }
  528. catch (Exception e)
  529. {
  530. fail("failed exception test.", e);
  531. }
  532. try
  533. {
  534. skF.getKeySpec(secKey, null);
  535. }
  536. catch (InvalidKeySpecException e)
  537. {
  538. // ignore okay
  539. }
  540. catch (Exception e)
  541. {
  542. fail("failed exception test.", e);
  543. }
  544. try
  545. {
  546. KeyGenerator kg = KeyGenerator.getInstance("DESede", "BC");
  547. try
  548. {
  549. kg.init(Integer.MIN_VALUE, new SecureRandom());
  550. fail("failed exception test - no exception thrown");
  551. }
  552. catch (InvalidParameterException e)
  553. {
  554. // ignore okay
  555. }
  556. catch (Exception e)
  557. {
  558. fail("failed exception test.", e);
  559. }
  560. }
  561. catch (Exception e)
  562. {
  563. fail("unexpected exception.", e);
  564. }
  565. try
  566. {
  567. skF = SecretKeyFactory.getInstance("DESede", "BC");
  568. try
  569. {
  570. skF.translateKey(null);
  571. fail("failed exception test - no exception thrown");
  572. }
  573. catch (InvalidKeyException e)
  574. {
  575. // ignore okay
  576. }
  577. catch (Exception e)
  578. {
  579. fail("failed exception test.", e);
  580. }
  581. }
  582. catch (Exception e)
  583. {
  584. fail("unexpected exception.", e);
  585. }
  586. try
  587. {
  588. byte[] rawDESKey = { (byte)128, (byte)131, (byte)133, (byte)134,
  589. (byte)137, (byte)138, (byte)140, (byte)143 };
  590. SecretKeySpec cipherKey = new SecretKeySpec(rawDESKey, "DES");
  591. Cipher cipher = Cipher.getInstance("DES/CBC/NoPadding", "BC");
  592. try
  593. {
  594. // According specification engineInit(int opmode, Key key,
  595. // SecureRandom random) throws InvalidKeyException if this
  596. // cipher is being
  597. // initialized for decryption and requires algorithm parameters
  598. // that cannot be determined from the given key
  599. cipher.init(Cipher.DECRYPT_MODE, cipherKey, (SecureRandom)null);
  600. fail("failed exception test - no InvalidKeyException thrown");
  601. }
  602. catch (InvalidKeyException e)
  603. {
  604. // ignore
  605. }
  606. }
  607. catch (Exception e)
  608. {
  609. fail("unexpected exception.", e);
  610. }
  611. try
  612. {
  613. byte[] rawDESKey = { -128, -125, -123, -122, -119, -118 };
  614. SecretKeySpec cipherKey = new SecretKeySpec(rawDESKey, "DES");
  615. Cipher cipher = Cipher.getInstance("DES/ECB/NoPadding", "BC");
  616. try
  617. {
  618. // According specification engineInit(int opmode, Key key,
  619. // SecureRandom random) throws InvalidKeyException if the given
  620. // key is inappropriate for initializing this cipher
  621. cipher.init(Cipher.ENCRYPT_MODE, cipherKey);
  622. fail("failed exception test - no InvalidKeyException thrown");
  623. }
  624. catch (InvalidKeyException e)
  625. {
  626. // ignore
  627. }
  628. }
  629. catch (Exception e)
  630. {
  631. fail("unexpected exception.", e);
  632. }
  633. try
  634. {
  635. byte[] rawDESKey = { -128, -125, -123, -122, -119, -118, -117, -115, -114 };
  636. SecretKeySpec cipherKey = new SecretKeySpec(rawDESKey, "DES");
  637. Cipher cipher = Cipher.getInstance("DES/ECB/NoPadding", "BC");
  638. try
  639. {
  640. // According specification engineInit(int opmode, Key key,
  641. // SecureRandom random) throws InvalidKeyException if the given
  642. // key is inappropriate for initializing this cipher
  643. cipher.init(Cipher.ENCRYPT_MODE, cipherKey);
  644. fail("failed exception test - no InvalidKeyException thrown");
  645. }
  646. catch (InvalidKeyException e)
  647. {
  648. // ignore
  649. }
  650. }
  651. catch (Exception e)
  652. {
  653. fail("unexpected exception.", e);
  654. }
  655. try
  656. {
  657. byte[] rawDESKey = { (byte)128, (byte)131, (byte)133, (byte)134,
  658. (byte)137, (byte)138, (byte)140, (byte)143 };
  659. SecretKeySpec cipherKey = new SecretKeySpec(rawDESKey, "DES");
  660. Cipher ecipher = Cipher.getInstance("DES/ECB/PKCS5Padding", "BC");
  661. ecipher.init(Cipher.ENCRYPT_MODE, cipherKey);
  662. byte[] cipherText = new byte[0];
  663. try
  664. {
  665. // According specification Method engineUpdate(byte[] input,
  666. // int inputOffset, int inputLen, byte[] output, int
  667. // outputOffset)
  668. // throws ShortBufferException - if the given output buffer is
  669. // too
  670. // small to hold the result
  671. ecipher.update(new byte[20], 0, 20, cipherText);
  672. fail("failed exception test - no ShortBufferException thrown");
  673. }
  674. catch (ShortBufferException e)
  675. {
  676. // ignore
  677. }
  678. }
  679. catch (Exception e)
  680. {
  681. fail("unexpected exception.", e);
  682. }
  683. try
  684. {
  685. KeyGenerator keyGen = KeyGenerator.getInstance("DES", "BC");
  686. keyGen.init((SecureRandom)null);
  687. // According specification engineGenerateKey() doesn't throw any exceptions.
  688. SecretKey key = keyGen.generateKey();
  689. if (key == null)
  690. {
  691. fail("key is null!");
  692. }
  693. }
  694. catch (Exception e)
  695. {
  696. fail("unexpected exception.", e);
  697. }
  698. try
  699. {
  700. AlgorithmParameters algParams = AlgorithmParameters.getInstance("DES", "BC");
  701. algParams.init(new IvParameterSpec(new byte[8]));
  702. // According specification engineGetEncoded() returns
  703. // the parameters in their primary encoding format. The primary
  704. // encoding
  705. // format for parameters is ASN.1, if an ASN.1 specification for
  706. // this type
  707. // of parameters exists.
  708. byte[] iv = algParams.getEncoded();
  709. if (iv.length != 10)
  710. {
  711. fail("parameters encoding wrong length - " + iv.length);
  712. }
  713. }
  714. catch (Exception e)
  715. {
  716. fail("unexpected exception.", e);
  717. }
  718. try
  719. {
  720. try
  721. {
  722. AlgorithmParameters algParams = AlgorithmParameters.getInstance("DES", "BC");
  723. byte[] encoding = new byte[10];
  724. encoding[0] = 3;
  725. encoding[1] = 8;
  726. // According specification engineInit(byte[] params, String format)
  727. // throws
  728. // IOException on decoding errors, but BC throws ClassCastException.
  729. algParams.init(encoding, "ASN.1");
  730. fail("failed exception test - no IOException thrown");
  731. }
  732. catch (IOException e)
  733. {
  734. // okay
  735. }
  736. try
  737. {
  738. Cipher c = Cipher.getInstance("DES", "BC");
  739. Key k = new PublicKey()
  740. {
  741. public String getAlgorithm()
  742. {
  743. return "STUB";
  744. }
  745. public String getFormat()
  746. {
  747. return null;
  748. }
  749. public byte[] getEncoded()
  750. {
  751. return null;
  752. }
  753. };
  754. c.init(Cipher.ENCRYPT_MODE, k);
  755. fail("failed exception test - no InvalidKeyException thrown for public key");
  756. }
  757. catch (InvalidKeyException e)
  758. {
  759. // okay
  760. }
  761. try
  762. {
  763. Cipher c = Cipher.getInstance("DES", "BC");
  764. Key k = new PrivateKey()
  765. {
  766. public String getAlgorithm()
  767. {
  768. return "STUB";
  769. }
  770. public String getFormat()
  771. {
  772. return null;
  773. }
  774. public byte[] getEncoded()
  775. {
  776. return null;
  777. }
  778. };
  779. c.init(Cipher.DECRYPT_MODE, k);
  780. fail("failed exception test - no InvalidKeyException thrown for private key");
  781. }
  782. catch (InvalidKeyException e)
  783. {
  784. // okay
  785. }
  786. }
  787. catch (Exception e)
  788. {
  789. fail("unexpected exception.", e);
  790. }
  791. }
  792. public void performTest()
  793. {
  794. for (int i = 0; i != cipherTests1.length; i += 2)
  795. {
  796. test(cipherTests1[i], input1, Hex.decode(cipherTests1[i + 1]));
  797. }
  798. for (int i = 0; i != cipherTests2.length; i += 2)
  799. {
  800. test(cipherTests2[i], input2, Hex.decode(cipherTests2[i + 1]));
  801. }
  802. //
  803. // check for less than a block
  804. //
  805. try
  806. {
  807. Cipher c = Cipher.getInstance("AES/CTS/NoPadding", "BC");
  808. c.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(new byte[16], "AES"));
  809. c.doFinal(new byte[4]);
  810. fail("CTS failed to throw exception");
  811. }
  812. catch (Exception e)
  813. {
  814. if (!(e instanceof IllegalBlockSizeException))
  815. {
  816. fail("CTS exception test - " + e, e);
  817. }
  818. }
  819. testExceptions();
  820. }
  821. public static void main(
  822. String[] args)
  823. {
  824. Security.addProvider(new BouncyCastleProvider());
  825. runTest(new BlockCipherTest());
  826. }
  827. }