You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CipherAlgorithm.java 3.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.poifs.crypt;
  16. import org.apache.poi.EncryptedDocumentException;
  17. public enum CipherAlgorithm {
  18. // key size for rc4: 0x00000028 - 0x00000080 (inclusive) with 8-bit increments
  19. // no block size, because its a streaming cipher
  20. rc4(CipherProvider.rc4, "RC4", 0x6801, 0x40, new int[]{0x28,0x30,0x38,0x40,0x48,0x50,0x58,0x60,0x68,0x70,0x78,0x80}, -1, 20, "RC4", false),
  21. // aes has always a block size of 128 - only its keysize may vary
  22. aes128(CipherProvider.aes, "AES", 0x660E, 128, new int[]{128}, 16, 32, "AES", false),
  23. aes192(CipherProvider.aes, "AES", 0x660F, 192, new int[]{192}, 16, 32, "AES", false),
  24. aes256(CipherProvider.aes, "AES", 0x6610, 256, new int[]{256}, 16, 32, "AES", false),
  25. rc2(null, "RC2", -1, 0x80, new int[]{0x28,0x30,0x38,0x40,0x48,0x50,0x58,0x60,0x68,0x70,0x78,0x80}, 8, 20, "RC2", false),
  26. des(null, "DES", -1, 64, new int[]{64}, 8/*for 56-bit*/, 32, "DES", false),
  27. // desx is not supported. Not sure, if it can be simulated by des3 somehow
  28. des3(null, "DESede", -1, 192, new int[]{192}, 8, 32, "3DES", false),
  29. // need bouncycastle provider for this one ...
  30. des3_112(null, "DESede", -1, 128, new int[]{128}, 8, 32, "3DES_112", true),
  31. // only for digital signatures
  32. rsa(null, "RSA", -1, 1024, new int[]{1024, 2048, 3072, 4096}, -1, -1, "", false);
  33. public final CipherProvider provider;
  34. public final String jceId;
  35. public final int ecmaId;
  36. public final int defaultKeySize;
  37. public final int[] allowedKeySize;
  38. public final int blockSize;
  39. public final int encryptedVerifierHashLength;
  40. public final String xmlId;
  41. public final boolean needsBouncyCastle;
  42. CipherAlgorithm(CipherProvider provider, String jceId, int ecmaId, int defaultKeySize, int[] allowedKeySize, int blockSize, int encryptedVerifierHashLength, String xmlId, boolean needsBouncyCastle) {
  43. this.provider = provider;
  44. this.jceId = jceId;
  45. this.ecmaId = ecmaId;
  46. this.defaultKeySize = defaultKeySize;
  47. this.allowedKeySize = allowedKeySize.clone();
  48. this.blockSize = blockSize;
  49. this.encryptedVerifierHashLength = encryptedVerifierHashLength;
  50. this.xmlId = xmlId;
  51. this.needsBouncyCastle = needsBouncyCastle;
  52. }
  53. public static CipherAlgorithm fromEcmaId(int ecmaId) {
  54. for (CipherAlgorithm ca : CipherAlgorithm.values()) {
  55. if (ca.ecmaId == ecmaId) return ca;
  56. }
  57. throw new EncryptedDocumentException("cipher algorithm " + ecmaId + " not found");
  58. }
  59. public static CipherAlgorithm fromXmlId(String xmlId, int keySize) {
  60. for (CipherAlgorithm ca : CipherAlgorithm.values()) {
  61. if (!ca.xmlId.equals(xmlId)) continue;
  62. for (int ks : ca.allowedKeySize) {
  63. if (ks == keySize) return ca;
  64. }
  65. }
  66. throw new EncryptedDocumentException("cipher algorithm " + xmlId + "/" + keySize + " not found");
  67. }
  68. }