src/java.security.jgss/share/classes/sun/security/krb5/internal/crypto/HmacSha2Aes128CksumType.java
changeset 48651 67abfee27e69
equal deleted inserted replaced
48650:e7164f73c4d3 48651:67abfee27e69
       
     1 /*
       
     2  * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     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
       
     7  * published by the Free Software Foundation.  Oracle designates this
       
     8  * particular file as subject to the "Classpath" exception as provided
       
     9  * by Oracle in the LICENSE file that accompanied this code.
       
    10  *
       
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    14  * version 2 for more details (a copy is included in the LICENSE file that
       
    15  * accompanied this code).
       
    16  *
       
    17  * You should have received a copy of the GNU General Public License version
       
    18  * 2 along with this work; if not, write to the Free Software Foundation,
       
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    20  *
       
    21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    22  * or visit www.oracle.com if you need additional information or have any
       
    23  * questions.
       
    24  */
       
    25 
       
    26 package sun.security.krb5.internal.crypto;
       
    27 
       
    28 import sun.security.krb5.Checksum;
       
    29 import sun.security.krb5.KrbCryptoException;
       
    30 import sun.security.krb5.internal.*;
       
    31 import java.security.GeneralSecurityException;
       
    32 
       
    33 /*
       
    34  * This class encapsulates the checksum type for aes128-cts-sha256
       
    35  */
       
    36 
       
    37 public class HmacSha2Aes128CksumType extends CksumType {
       
    38 
       
    39     public HmacSha2Aes128CksumType() {
       
    40     }
       
    41 
       
    42     public int confounderSize() {
       
    43         return 16;
       
    44     }
       
    45 
       
    46     public int cksumType() {
       
    47         return Checksum.CKSUMTYPE_HMAC_SHA256_128_AES128;
       
    48     }
       
    49 
       
    50     public boolean isSafe() {
       
    51         return true;
       
    52     }
       
    53 
       
    54     public int cksumSize() {
       
    55         return 16;  // bytes
       
    56     }
       
    57 
       
    58     public int keyType() {
       
    59         return Krb5.KEYTYPE_AES;
       
    60     }
       
    61 
       
    62     public int keySize() {
       
    63         return 16;   // bytes
       
    64     }
       
    65 
       
    66     public byte[] calculateChecksum(byte[] data, int size) {
       
    67         return null;
       
    68     }
       
    69 
       
    70     /**
       
    71      * Calculates keyed checksum.
       
    72      * @param data the data used to generate the checksum.
       
    73      * @param size length of the data.
       
    74      * @param key the key used to encrypt the checksum.
       
    75      * @return keyed checksum.
       
    76      */
       
    77     public byte[] calculateKeyedChecksum(byte[] data, int size, byte[] key,
       
    78         int usage) throws KrbCryptoException {
       
    79 
       
    80          try {
       
    81             return Aes128Sha2.calculateChecksum(key, usage, data, 0, size);
       
    82          } catch (GeneralSecurityException e) {
       
    83             KrbCryptoException ke = new KrbCryptoException(e.getMessage());
       
    84             ke.initCause(e);
       
    85             throw ke;
       
    86          }
       
    87     }
       
    88 
       
    89     /**
       
    90      * Verifies keyed checksum.
       
    91      * @param data the data.
       
    92      * @param size the length of data.
       
    93      * @param key the key used to encrypt the checksum.
       
    94      * @param checksum the checksum.
       
    95      * @return true if verification is successful.
       
    96      */
       
    97     public boolean verifyKeyedChecksum(byte[] data, int size,
       
    98         byte[] key, byte[] checksum, int usage) throws KrbCryptoException {
       
    99 
       
   100          try {
       
   101             byte[] newCksum = Aes128Sha2.calculateChecksum(key, usage,
       
   102                                                         data, 0, size);
       
   103             return isChecksumEqual(checksum, newCksum);
       
   104          } catch (GeneralSecurityException e) {
       
   105             KrbCryptoException ke = new KrbCryptoException(e.getMessage());
       
   106             ke.initCause(e);
       
   107             throw ke;
       
   108          }
       
   109     }
       
   110 }