author | weijun |
Mon, 22 Jan 2018 12:00:41 +0800 | |
changeset 48651 | 67abfee27e69 |
permissions | -rw-r--r-- |
48651
67abfee27e69
8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents:
diff
changeset
|
1 |
/* |
67abfee27e69
8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents:
diff
changeset
|
2 |
* Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved. |
67abfee27e69
8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
67abfee27e69
8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents:
diff
changeset
|
4 |
* |
67abfee27e69
8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
67abfee27e69
8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
67abfee27e69
8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation. Oracle designates this |
67abfee27e69
8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents:
diff
changeset
|
8 |
* particular file as subject to the "Classpath" exception as provided |
67abfee27e69
8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents:
diff
changeset
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
67abfee27e69
8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents:
diff
changeset
|
10 |
* |
67abfee27e69
8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents:
diff
changeset
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
67abfee27e69
8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents:
diff
changeset
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
67abfee27e69
8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents:
diff
changeset
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
67abfee27e69
8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents:
diff
changeset
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
67abfee27e69
8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents:
diff
changeset
|
15 |
* accompanied this code). |
67abfee27e69
8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents:
diff
changeset
|
16 |
* |
67abfee27e69
8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents:
diff
changeset
|
17 |
* You should have received a copy of the GNU General Public License version |
67abfee27e69
8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents:
diff
changeset
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
67abfee27e69
8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents:
diff
changeset
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
67abfee27e69
8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents:
diff
changeset
|
20 |
* |
67abfee27e69
8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents:
diff
changeset
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
67abfee27e69
8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents:
diff
changeset
|
22 |
* or visit www.oracle.com if you need additional information or have any |
67abfee27e69
8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents:
diff
changeset
|
23 |
* questions. |
67abfee27e69
8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents:
diff
changeset
|
24 |
*/ |
67abfee27e69
8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents:
diff
changeset
|
25 |
|
67abfee27e69
8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents:
diff
changeset
|
26 |
package sun.security.krb5.internal.crypto; |
67abfee27e69
8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents:
diff
changeset
|
27 |
|
67abfee27e69
8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents:
diff
changeset
|
28 |
import sun.security.krb5.KrbCryptoException; |
67abfee27e69
8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents:
diff
changeset
|
29 |
import sun.security.krb5.internal.*; |
67abfee27e69
8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents:
diff
changeset
|
30 |
import java.security.GeneralSecurityException; |
67abfee27e69
8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents:
diff
changeset
|
31 |
import sun.security.krb5.EncryptedData; |
67abfee27e69
8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents:
diff
changeset
|
32 |
import sun.security.krb5.Checksum; |
67abfee27e69
8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents:
diff
changeset
|
33 |
|
67abfee27e69
8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents:
diff
changeset
|
34 |
/* |
67abfee27e69
8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents:
diff
changeset
|
35 |
* This class encapsulates the encryption type for aes128-cts-hmac-sha256-128 |
67abfee27e69
8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents:
diff
changeset
|
36 |
*/ |
67abfee27e69
8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents:
diff
changeset
|
37 |
|
67abfee27e69
8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents:
diff
changeset
|
38 |
public final class Aes128CtsHmacSha2EType extends EType { |
67abfee27e69
8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents:
diff
changeset
|
39 |
|
67abfee27e69
8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents:
diff
changeset
|
40 |
public int eType() { |
67abfee27e69
8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents:
diff
changeset
|
41 |
return EncryptedData.ETYPE_AES128_CTS_HMAC_SHA256_128; |
67abfee27e69
8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents:
diff
changeset
|
42 |
} |
67abfee27e69
8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents:
diff
changeset
|
43 |
|
67abfee27e69
8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents:
diff
changeset
|
44 |
public int minimumPadSize() { |
67abfee27e69
8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents:
diff
changeset
|
45 |
return 0; |
67abfee27e69
8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents:
diff
changeset
|
46 |
} |
67abfee27e69
8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents:
diff
changeset
|
47 |
|
67abfee27e69
8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents:
diff
changeset
|
48 |
public int confounderSize() { |
67abfee27e69
8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents:
diff
changeset
|
49 |
return blockSize(); |
67abfee27e69
8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents:
diff
changeset
|
50 |
} |
67abfee27e69
8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents:
diff
changeset
|
51 |
|
67abfee27e69
8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents:
diff
changeset
|
52 |
public int checksumType() { |
67abfee27e69
8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents:
diff
changeset
|
53 |
return Checksum.CKSUMTYPE_HMAC_SHA256_128_AES128; |
67abfee27e69
8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents:
diff
changeset
|
54 |
} |
67abfee27e69
8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents:
diff
changeset
|
55 |
|
67abfee27e69
8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents:
diff
changeset
|
56 |
public int checksumSize() { |
67abfee27e69
8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents:
diff
changeset
|
57 |
return Aes128Sha2.getChecksumLength(); |
67abfee27e69
8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents:
diff
changeset
|
58 |
} |
67abfee27e69
8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents:
diff
changeset
|
59 |
|
67abfee27e69
8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents:
diff
changeset
|
60 |
public int blockSize() { |
67abfee27e69
8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents:
diff
changeset
|
61 |
return 16; |
67abfee27e69
8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents:
diff
changeset
|
62 |
} |
67abfee27e69
8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents:
diff
changeset
|
63 |
|
67abfee27e69
8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents:
diff
changeset
|
64 |
public int keyType() { |
67abfee27e69
8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents:
diff
changeset
|
65 |
return Krb5.KEYTYPE_AES; |
67abfee27e69
8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents:
diff
changeset
|
66 |
} |
67abfee27e69
8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents:
diff
changeset
|
67 |
|
67abfee27e69
8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents:
diff
changeset
|
68 |
public int keySize() { |
67abfee27e69
8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents:
diff
changeset
|
69 |
return 16; // bytes |
67abfee27e69
8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents:
diff
changeset
|
70 |
} |
67abfee27e69
8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents:
diff
changeset
|
71 |
|
67abfee27e69
8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents:
diff
changeset
|
72 |
public byte[] encrypt(byte[] data, byte[] key, int usage) |
67abfee27e69
8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents:
diff
changeset
|
73 |
throws KrbCryptoException { |
67abfee27e69
8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents:
diff
changeset
|
74 |
byte[] ivec = new byte[blockSize()]; |
67abfee27e69
8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents:
diff
changeset
|
75 |
return encrypt(data, key, ivec, usage); |
67abfee27e69
8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents:
diff
changeset
|
76 |
} |
67abfee27e69
8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents:
diff
changeset
|
77 |
|
67abfee27e69
8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents:
diff
changeset
|
78 |
public byte[] encrypt(byte[] data, byte[] key, byte[] ivec, int usage) |
67abfee27e69
8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents:
diff
changeset
|
79 |
throws KrbCryptoException { |
67abfee27e69
8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents:
diff
changeset
|
80 |
try { |
67abfee27e69
8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents:
diff
changeset
|
81 |
return Aes128Sha2.encrypt(key, usage, ivec, data, 0, data.length); |
67abfee27e69
8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents:
diff
changeset
|
82 |
} catch (GeneralSecurityException e) { |
67abfee27e69
8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents:
diff
changeset
|
83 |
KrbCryptoException ke = new KrbCryptoException(e.getMessage()); |
67abfee27e69
8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents:
diff
changeset
|
84 |
ke.initCause(e); |
67abfee27e69
8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents:
diff
changeset
|
85 |
throw ke; |
67abfee27e69
8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents:
diff
changeset
|
86 |
} |
67abfee27e69
8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents:
diff
changeset
|
87 |
} |
67abfee27e69
8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents:
diff
changeset
|
88 |
|
67abfee27e69
8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents:
diff
changeset
|
89 |
public byte[] decrypt(byte[] cipher, byte[] key, int usage) |
67abfee27e69
8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents:
diff
changeset
|
90 |
throws KrbApErrException, KrbCryptoException { |
67abfee27e69
8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents:
diff
changeset
|
91 |
byte[] ivec = new byte[blockSize()]; |
67abfee27e69
8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents:
diff
changeset
|
92 |
return decrypt(cipher, key, ivec, usage); |
67abfee27e69
8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents:
diff
changeset
|
93 |
} |
67abfee27e69
8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents:
diff
changeset
|
94 |
|
67abfee27e69
8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents:
diff
changeset
|
95 |
public byte[] decrypt(byte[] cipher, byte[] key, byte[] ivec, int usage) |
67abfee27e69
8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents:
diff
changeset
|
96 |
throws KrbApErrException, KrbCryptoException { |
67abfee27e69
8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents:
diff
changeset
|
97 |
try { |
67abfee27e69
8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents:
diff
changeset
|
98 |
return Aes128Sha2.decrypt(key, usage, ivec, cipher, 0, cipher.length); |
67abfee27e69
8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents:
diff
changeset
|
99 |
} catch (GeneralSecurityException e) { |
67abfee27e69
8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents:
diff
changeset
|
100 |
KrbCryptoException ke = new KrbCryptoException(e.getMessage()); |
67abfee27e69
8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents:
diff
changeset
|
101 |
ke.initCause(e); |
67abfee27e69
8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents:
diff
changeset
|
102 |
throw ke; |
67abfee27e69
8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents:
diff
changeset
|
103 |
} |
67abfee27e69
8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents:
diff
changeset
|
104 |
} |
67abfee27e69
8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents:
diff
changeset
|
105 |
|
67abfee27e69
8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents:
diff
changeset
|
106 |
// Override default, because our decrypted data does not return confounder |
67abfee27e69
8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents:
diff
changeset
|
107 |
// Should eventually get rid of EType.decryptedData and |
67abfee27e69
8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents:
diff
changeset
|
108 |
// EncryptedData.decryptedData altogether |
67abfee27e69
8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents:
diff
changeset
|
109 |
public byte[] decryptedData(byte[] data) { |
67abfee27e69
8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents:
diff
changeset
|
110 |
return data; |
67abfee27e69
8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents:
diff
changeset
|
111 |
} |
67abfee27e69
8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents:
diff
changeset
|
112 |
} |