equal
deleted
inserted
replaced
1 /* |
1 /* |
2 * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved. |
2 * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 * |
4 * |
5 * This code is free software; you can redistribute it and/or modify it |
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 |
6 * under the terms of the GNU General Public License version 2 only, as |
7 * published by the Free Software Foundation. Oracle designates this |
7 * published by the Free Software Foundation. Oracle designates this |
165 public class Cipher { |
165 public class Cipher { |
166 |
166 |
167 private static final Debug debug = |
167 private static final Debug debug = |
168 Debug.getInstance("jca", "Cipher"); |
168 Debug.getInstance("jca", "Cipher"); |
169 |
169 |
|
170 private static final Debug pdebug = |
|
171 Debug.getInstance("provider", "Provider"); |
|
172 private static final boolean skipDebug = |
|
173 Debug.isOn("engine=") && !Debug.isOn("cipher"); |
|
174 |
170 /** |
175 /** |
171 * Constant used to initialize cipher to encryption mode. |
176 * Constant used to initialize cipher to encryption mode. |
172 */ |
177 */ |
173 public static final int ENCRYPT_MODE = 1; |
178 public static final int ENCRYPT_MODE = 1; |
174 |
179 |
1108 if ((opmode < ENCRYPT_MODE) || (opmode > UNWRAP_MODE)) { |
1113 if ((opmode < ENCRYPT_MODE) || (opmode > UNWRAP_MODE)) { |
1109 throw new InvalidParameterException("Invalid operation mode"); |
1114 throw new InvalidParameterException("Invalid operation mode"); |
1110 } |
1115 } |
1111 } |
1116 } |
1112 |
1117 |
|
1118 private static String getOpmodeString(int opmode) { |
|
1119 switch (opmode) { |
|
1120 case ENCRYPT_MODE: |
|
1121 return "encryption"; |
|
1122 case DECRYPT_MODE: |
|
1123 return "decryption"; |
|
1124 case WRAP_MODE: |
|
1125 return "key wrapping"; |
|
1126 case UNWRAP_MODE: |
|
1127 return "key unwrapping"; |
|
1128 default: |
|
1129 return ""; |
|
1130 } |
|
1131 } |
|
1132 |
1113 /** |
1133 /** |
1114 * Initializes this cipher with a key. |
1134 * Initializes this cipher with a key. |
1115 * |
1135 * |
1116 * <p>The cipher is initialized for one of the following four operations: |
1136 * <p>The cipher is initialized for one of the following four operations: |
1117 * encryption, decryption, key wrapping or key unwrapping, depending |
1137 * encryption, decryption, key wrapping or key unwrapping, depending |
1233 } |
1253 } |
1234 } |
1254 } |
1235 |
1255 |
1236 initialized = true; |
1256 initialized = true; |
1237 this.opmode = opmode; |
1257 this.opmode = opmode; |
|
1258 |
|
1259 if (!skipDebug && pdebug != null) { |
|
1260 pdebug.println("Cipher." + transformation + " " + |
|
1261 getOpmodeString(opmode) + " algorithm from: " + |
|
1262 this.provider.getName()); |
|
1263 } |
1238 } |
1264 } |
1239 |
1265 |
1240 /** |
1266 /** |
1241 * Initializes this cipher with a key and a set of algorithm |
1267 * Initializes this cipher with a key and a set of algorithm |
1242 * parameters. |
1268 * parameters. |
1370 chooseProvider(I_PARAMSPEC, opmode, key, params, null, random); |
1396 chooseProvider(I_PARAMSPEC, opmode, key, params, null, random); |
1371 } |
1397 } |
1372 |
1398 |
1373 initialized = true; |
1399 initialized = true; |
1374 this.opmode = opmode; |
1400 this.opmode = opmode; |
|
1401 |
|
1402 if (!skipDebug && pdebug != null) { |
|
1403 pdebug.println("Cipher." + transformation + " " + |
|
1404 getOpmodeString(opmode) + " algorithm from: " + |
|
1405 this.provider.getName()); |
|
1406 } |
1375 } |
1407 } |
1376 |
1408 |
1377 /** |
1409 /** |
1378 * Initializes this cipher with a key and a set of algorithm |
1410 * Initializes this cipher with a key and a set of algorithm |
1379 * parameters. |
1411 * parameters. |
1507 chooseProvider(I_PARAMS, opmode, key, null, params, random); |
1539 chooseProvider(I_PARAMS, opmode, key, null, params, random); |
1508 } |
1540 } |
1509 |
1541 |
1510 initialized = true; |
1542 initialized = true; |
1511 this.opmode = opmode; |
1543 this.opmode = opmode; |
|
1544 |
|
1545 if (!skipDebug && pdebug != null) { |
|
1546 pdebug.println("Cipher." + transformation + " " + |
|
1547 getOpmodeString(opmode) + " algorithm from: " + |
|
1548 this.provider.getName()); |
|
1549 } |
1512 } |
1550 } |
1513 |
1551 |
1514 /** |
1552 /** |
1515 * Initializes this cipher with the public key from the given certificate. |
1553 * Initializes this cipher with the public key from the given certificate. |
1516 * <p> The cipher is initialized for one of the following four operations: |
1554 * <p> The cipher is initialized for one of the following four operations: |
1691 } |
1729 } |
1692 } |
1730 } |
1693 |
1731 |
1694 initialized = true; |
1732 initialized = true; |
1695 this.opmode = opmode; |
1733 this.opmode = opmode; |
|
1734 |
|
1735 if (!skipDebug && pdebug != null) { |
|
1736 pdebug.println("Cipher." + transformation + " " + |
|
1737 getOpmodeString(opmode) + " algorithm from: " + |
|
1738 this.provider.getName()); |
|
1739 } |
1696 } |
1740 } |
1697 |
1741 |
1698 /** |
1742 /** |
1699 * Ensures that Cipher is in a valid state for update() and doFinal() |
1743 * Ensures that Cipher is in a valid state for update() and doFinal() |
1700 * calls - should be initialized and in ENCRYPT_MODE or DECRYPT_MODE. |
1744 * calls - should be initialized and in ENCRYPT_MODE or DECRYPT_MODE. |