jdk/src/share/classes/sun/security/ssl/MAC.java
author xuelei
Mon, 01 Nov 2010 22:02:35 -0700
changeset 7043 5e2d1edeb2c7
parent 7039 6464c8e62a18
child 10892 9af809b604d9
permissions -rw-r--r--
6916074: Add support for TLS 1.2 6985179: To support Server Name Indication extension for JSSE client Summary: Introduces the algorithm constraints to support signature and hash algorithm selection. Includes contributions from wetmore and weijung. Reviewed-by: wetmore, weijun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
     2
 * Copyright (c) 1996, 2010, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
package sun.security.ssl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.security.InvalidKeyException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.security.NoSuchAlgorithmException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.nio.ByteBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import javax.crypto.Mac;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import javax.crypto.SecretKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import sun.security.ssl.CipherSuite.MacAlg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import static sun.security.ssl.CipherSuite.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * This class computes the "Message Authentication Code" (MAC) for each
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * SSL message.  This is essentially a shared-secret signature, used to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * provide integrity protection for SSL messages.  The MAC is actually
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * one of several keyed hashes, as associated with the cipher suite and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * protocol version.  (SSL v3.0 uses one construct, TLS uses another.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * <P>NOTE: MAC computation is the only place in the SSL protocol that the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * sequence number is used.  It's also reset to zero with each change of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * a cipher spec, so this is the only place this state is needed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * @author David Brownell
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * @author Andreas Sterbenz
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
final class MAC {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    final static MAC NULL = new MAC();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    // Value of the null MAC is fixed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    private static final byte nullMAC[] = new byte[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    // internal identifier for the MAC algorithm
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    private final MacAlg        macAlg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    // stuff defined by the kind of MAC algorithm
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    private final int           macSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    // JCE Mac object
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    private final Mac mac;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    // byte array containing the additional information we MAC in each record
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    // (see below)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    private final byte[] block;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    // sequence number + record type + + record length
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    private static final int BLOCK_SIZE_SSL = 8 + 1 + 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    // sequence number + record type + protocol version + record length
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    private static final int BLOCK_SIZE_TLS = 8 + 1 + 2 + 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    // offset of record type in block
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    private static final int BLOCK_OFFSET_TYPE    = 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    // offset of protocol version number in block (TLS only)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    private static final int BLOCK_OFFSET_VERSION = 8 + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    private MAC() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        macSize = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        macAlg = M_NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        mac = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        block = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * Set up, configured for the given SSL/TLS MAC type and version.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    MAC(MacAlg macAlg, ProtocolVersion protocolVersion, SecretKey key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            throws NoSuchAlgorithmException, InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        this.macAlg = macAlg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        this.macSize = macAlg.size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        String algorithm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        boolean tls = (protocolVersion.v >= ProtocolVersion.TLS10.v);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        if (macAlg == M_MD5) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            algorithm = tls ? "HmacMD5" : "SslMacMD5";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        } else if (macAlg == M_SHA) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            algorithm = tls ? "HmacSHA1" : "SslMacSHA1";
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   108
        } else if (macAlg == M_SHA256) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   109
            algorithm = "HmacSHA256";    // TLS 1.2+
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   110
        } else if (macAlg == M_SHA384) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   111
            algorithm = "HmacSHA384";    // TLS 1.2+
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            throw new RuntimeException("Unknown Mac " + macAlg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        mac = JsseJce.getMac(algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        mac.init(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        if (tls) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            block = new byte[BLOCK_SIZE_TLS];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            block[BLOCK_OFFSET_VERSION]   = protocolVersion.major;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            block[BLOCK_OFFSET_VERSION+1] = protocolVersion.minor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            block = new byte[BLOCK_SIZE_SSL];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * Returns the length of the MAC.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    int MAClen() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        return macSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * Computes and returns the MAC for the data in this byte array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * @param type record type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * @param buf compressed record on which the MAC is computed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * @param offset start of compressed record data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * @param len the size of the compressed record
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    final byte[] compute(byte type, byte buf[], int offset, int len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        return compute(type, null, buf, offset, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * Compute and returns the MAC for the remaining data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * in this ByteBuffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * On return, the bb position == limit, and limit will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * have not changed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * @param type record type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * @param bb a ByteBuffer in which the position and limit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     *          demarcate the data to be MAC'd.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    final byte[] compute(byte type, ByteBuffer bb) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        return compute(type, bb, null, 0, bb.remaining());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   162
    /**
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   163
     * Check whether the sequence number is close to wrap
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   164
     *
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   165
     * Sequence numbers are of type uint64 and may not exceed 2^64-1.
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   166
     * Sequence numbers do not wrap. When the sequence number is near
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   167
     * to wrap, we need to close the connection immediately.
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   168
     */
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   169
    final boolean seqNumOverflow() {
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   170
        /*
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   171
         * Conservatively, we don't allow more records to be generated
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   172
         * when there are only 2^8 sequence numbers left.
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   173
         */
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   174
        return (block != null && mac != null &&
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   175
                block[0] == 0xFF && block[1] == 0xFF &&
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   176
                block[2] == 0xFF && block[3] == 0xFF &&
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   177
                block[4] == 0xFF && block[5] == 0xFF &&
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   178
                block[6] == 0xFF);
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   179
    }
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   180
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   181
    /*
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   182
     * Check whether to renew the sequence number
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   183
     *
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   184
     * Sequence numbers are of type uint64 and may not exceed 2^64-1.
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   185
     * Sequence numbers do not wrap.  If a TLS
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   186
     * implementation would need to wrap a sequence number, it must
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   187
     * renegotiate instead.
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   188
     */
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   189
    final boolean seqNumIsHuge() {
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   190
        /*
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   191
         * Conservatively, we should ask for renegotiation when there are
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   192
         * only 2^48 sequence numbers left.
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   193
         */
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   194
        return (block != null && mac != null &&
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   195
                block[0] == 0xFF && block[1] == 0xFF);
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   196
    }
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 5506
diff changeset
   197
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    // increment the sequence number in the block array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    // it is a 64-bit number stored in big-endian format
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    private void incrementSequenceNumber() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        int k = 7;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        while ((k >= 0) && (++block[k] == 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            k--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * Compute based on either buffer type, either bb.position/limit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * or buf/offset/len.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     */
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   211
    private byte[] compute(byte type, ByteBuffer bb, byte[] buf,
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   212
            int offset, int len) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        if (macSize == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            return nullMAC;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        block[BLOCK_OFFSET_TYPE] = type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        block[block.length - 2]  = (byte)(len >> 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        block[block.length - 1]  = (byte)(len     );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        mac.update(block);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        incrementSequenceNumber();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        // content
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        if (bb != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            mac.update(bb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            mac.update(buf, offset, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        return mac.doFinal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
}