src/java.security.jgss/share/classes/sun/security/krb5/internal/crypto/Aes128Sha2.java
author mbalao
Wed, 05 Jun 2019 01:42:11 -0300
changeset 55258 d65d3c37232c
parent 48651 67abfee27e69
permissions -rw-r--r--
8215032: Support Kerberos cross-realm referrals (RFC 6806) Reviewed-by: weijun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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.internal.crypto.dk.AesSha2DkCrypto;
67abfee27e69 8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents:
diff changeset
    29
import sun.security.krb5.KrbCryptoException;
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
67abfee27e69 8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents:
diff changeset
    32
/**
67abfee27e69 8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents:
diff changeset
    33
 * Class with static methods for doing aes128-cts-hmac-sha256-128 operations.
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
67abfee27e69 8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents:
diff changeset
    36
public class Aes128Sha2 {
67abfee27e69 8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents:
diff changeset
    37
    private static final AesSha2DkCrypto CRYPTO = new AesSha2DkCrypto(128);
67abfee27e69 8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents:
diff changeset
    38
67abfee27e69 8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents:
diff changeset
    39
    private Aes128Sha2() {
67abfee27e69 8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents:
diff changeset
    40
    }
67abfee27e69 8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents:
diff changeset
    41
67abfee27e69 8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents:
diff changeset
    42
    public static byte[] stringToKey(char[] password, String salt, byte[] params)
67abfee27e69 8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents:
diff changeset
    43
        throws GeneralSecurityException {
67abfee27e69 8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents:
diff changeset
    44
        return CRYPTO.stringToKey(password, salt, params);
67abfee27e69 8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents:
diff changeset
    45
    }
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
    // in bytes
67abfee27e69 8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents:
diff changeset
    48
    public static int getChecksumLength() {
67abfee27e69 8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents:
diff changeset
    49
        return CRYPTO.getChecksumLength();
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 static byte[] calculateChecksum(byte[] baseKey, int usage,
67abfee27e69 8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents:
diff changeset
    53
        byte[] input, int start, int len) throws GeneralSecurityException {
67abfee27e69 8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents:
diff changeset
    54
            return CRYPTO.calculateChecksum(baseKey, usage, input, start, len);
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
67abfee27e69 8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents:
diff changeset
    57
    public static byte[] encrypt(byte[] baseKey, int usage,
67abfee27e69 8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents:
diff changeset
    58
        byte[] ivec, byte[] plaintext, int start, int len)
67abfee27e69 8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents:
diff changeset
    59
        throws GeneralSecurityException, KrbCryptoException {
67abfee27e69 8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents:
diff changeset
    60
            return CRYPTO.encrypt(baseKey, usage, ivec, null /* new_ivec */,
67abfee27e69 8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents:
diff changeset
    61
                plaintext, start, len);
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
    /* Encrypt plaintext; do not add confounder, or checksum */
67abfee27e69 8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents:
diff changeset
    65
    public static byte[] encryptRaw(byte[] baseKey, int usage,
67abfee27e69 8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents:
diff changeset
    66
        byte[] ivec, byte[] plaintext, int start, int len)
67abfee27e69 8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents:
diff changeset
    67
        throws GeneralSecurityException, KrbCryptoException {
67abfee27e69 8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents:
diff changeset
    68
        return CRYPTO.encryptRaw(baseKey, usage, ivec, plaintext, start, len);
67abfee27e69 8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents:
diff changeset
    69
    }
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
    public static byte[] decrypt(byte[] baseKey, int usage, byte[] ivec,
67abfee27e69 8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents:
diff changeset
    72
        byte[] ciphertext, int start, int len)
67abfee27e69 8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents:
diff changeset
    73
        throws GeneralSecurityException {
67abfee27e69 8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents:
diff changeset
    74
        return CRYPTO.decrypt(baseKey, usage, ivec, ciphertext, start, len);
67abfee27e69 8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents:
diff changeset
    75
    }
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
    /* Decrypt ciphertext; do not remove confounder, or check checksum */
67abfee27e69 8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents:
diff changeset
    78
    public static byte[] decryptRaw(byte[] baseKey, int usage, byte[] ivec,
67abfee27e69 8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents:
diff changeset
    79
        byte[] ciphertext, int start, int len)
67abfee27e69 8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents:
diff changeset
    80
        throws GeneralSecurityException {
67abfee27e69 8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents:
diff changeset
    81
        return CRYPTO.decryptRaw(baseKey, usage, ivec, ciphertext, start, len);
67abfee27e69 8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents:
diff changeset
    82
    }
67abfee27e69 8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents:
diff changeset
    83
};