jdk/src/java.base/share/classes/com/sun/crypto/provider/AESCipher.java
author chegar
Sun, 17 Aug 2014 15:54:13 +0100
changeset 25859 3317bb8137f4
parent 15008 jdk/src/share/classes/com/sun/crypto/provider/AESCipher.java@6a494f8ba5b5
child 39750 982b75e31495
permissions -rw-r--r--
8054834: Modular Source Code Reviewed-by: alanb, chegar, ihse, mduigou Contributed-by: alan.bateman@oracle.com, alex.buckley@oracle.com, chris.hegarty@oracle.com, erik.joelsson@oracle.com, jonathan.gibbons@oracle.com, karen.kinnear@oracle.com, magnus.ihse.bursie@oracle.com, mandy.chung@oracle.com, mark.reinhold@oracle.com, paul.sandoz@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
15008
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents: 13672
diff changeset
     2
 * Copyright (c) 2002, 2013, 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: 3353
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: 3353
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: 3353
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3353
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3353
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
package com.sun.crypto.provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.security.spec.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import javax.crypto.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import javax.crypto.spec.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import javax.crypto.BadPaddingException;
15008
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents: 13672
diff changeset
    33
import java.nio.ByteBuffer;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * This class implements the AES algorithm in its various modes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * (<code>ECB</code>, <code>CFB</code>, <code>OFB</code>, <code>CBC</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * <code>PCBC</code>) and padding schemes (<code>PKCS5Padding</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * <code>NoPadding</code>, <code>ISO10126Padding</code>).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * @author Valerie Peng
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * @see AESCrypt
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * @see CipherBlockChaining
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * @see ElectronicCodeBook
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * @see CipherFeedback
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * @see OutputFeedback
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
13672
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
    51
abstract class AESCipher extends CipherSpi {
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
    52
    public static final class General extends AESCipher {
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
    53
        public General() {
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
    54
            super(-1);
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
    55
        }
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
    56
    }
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
    57
    abstract static class OidImpl extends AESCipher {
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
    58
        protected OidImpl(int keySize, String mode, String padding) {
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
    59
            super(keySize);
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
    60
            try {
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
    61
                engineSetMode(mode);
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
    62
                engineSetPadding(padding);
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
    63
            } catch (GeneralSecurityException gse) {
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
    64
                // internal error; re-throw as provider exception
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
    65
                ProviderException pe =new ProviderException("Internal Error");
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
    66
                pe.initCause(gse);
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
    67
                throw pe;
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
    68
            }
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
    69
        }
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
    70
    }
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
    71
    public static final class AES128_ECB_NoPadding extends OidImpl {
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
    72
        public AES128_ECB_NoPadding() {
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
    73
            super(16, "ECB", "NOPADDING");
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
    74
        }
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
    75
    }
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
    76
    public static final class AES192_ECB_NoPadding extends OidImpl {
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
    77
        public AES192_ECB_NoPadding() {
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
    78
            super(24, "ECB", "NOPADDING");
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
    79
        }
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
    80
    }
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
    81
    public static final class AES256_ECB_NoPadding extends OidImpl {
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
    82
        public AES256_ECB_NoPadding() {
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
    83
            super(32, "ECB", "NOPADDING");
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
    84
        }
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
    85
    }
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
    86
    public static final class AES128_CBC_NoPadding extends OidImpl {
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
    87
        public AES128_CBC_NoPadding() {
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
    88
            super(16, "CBC", "NOPADDING");
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
    89
        }
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
    90
    }
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
    91
    public static final class AES192_CBC_NoPadding extends OidImpl {
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
    92
        public AES192_CBC_NoPadding() {
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
    93
            super(24, "CBC", "NOPADDING");
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
    94
        }
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
    95
    }
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
    96
    public static final class AES256_CBC_NoPadding extends OidImpl {
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
    97
        public AES256_CBC_NoPadding() {
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
    98
            super(32, "CBC", "NOPADDING");
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
    99
        }
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
   100
    }
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
   101
    public static final class AES128_OFB_NoPadding extends OidImpl {
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
   102
        public AES128_OFB_NoPadding() {
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
   103
            super(16, "OFB", "NOPADDING");
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
   104
        }
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
   105
    }
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
   106
    public static final class AES192_OFB_NoPadding extends OidImpl {
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
   107
        public AES192_OFB_NoPadding() {
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
   108
            super(24, "OFB", "NOPADDING");
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
   109
        }
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
   110
    }
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
   111
    public static final class AES256_OFB_NoPadding extends OidImpl {
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
   112
        public AES256_OFB_NoPadding() {
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
   113
            super(32, "OFB", "NOPADDING");
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
   114
        }
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
   115
    }
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
   116
    public static final class AES128_CFB_NoPadding extends OidImpl {
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
   117
        public AES128_CFB_NoPadding() {
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
   118
            super(16, "CFB", "NOPADDING");
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
   119
        }
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
   120
    }
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
   121
    public static final class AES192_CFB_NoPadding extends OidImpl {
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
   122
        public AES192_CFB_NoPadding() {
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
   123
            super(24, "CFB", "NOPADDING");
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
   124
        }
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
   125
    }
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
   126
    public static final class AES256_CFB_NoPadding extends OidImpl {
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
   127
        public AES256_CFB_NoPadding() {
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
   128
            super(32, "CFB", "NOPADDING");
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
   129
        }
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
   130
    }
15008
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents: 13672
diff changeset
   131
    public static final class AES128_GCM_NoPadding extends OidImpl {
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents: 13672
diff changeset
   132
        public AES128_GCM_NoPadding() {
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents: 13672
diff changeset
   133
            super(16, "GCM", "NOPADDING");
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents: 13672
diff changeset
   134
        }
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents: 13672
diff changeset
   135
    }
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents: 13672
diff changeset
   136
    public static final class AES192_GCM_NoPadding extends OidImpl {
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents: 13672
diff changeset
   137
        public AES192_GCM_NoPadding() {
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents: 13672
diff changeset
   138
            super(24, "GCM", "NOPADDING");
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents: 13672
diff changeset
   139
        }
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents: 13672
diff changeset
   140
    }
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents: 13672
diff changeset
   141
    public static final class AES256_GCM_NoPadding extends OidImpl {
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents: 13672
diff changeset
   142
        public AES256_GCM_NoPadding() {
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents: 13672
diff changeset
   143
            super(32, "GCM", "NOPADDING");
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents: 13672
diff changeset
   144
        }
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents: 13672
diff changeset
   145
    }
13672
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
   146
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
   147
    // utility method used by AESCipher and AESWrapCipher
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
   148
    static final void checkKeySize(Key key, int fixedKeySize)
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
   149
        throws InvalidKeyException {
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
   150
        if (fixedKeySize != -1) {
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
   151
            if (key == null) {
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
   152
                throw new InvalidKeyException("The key must not be null");
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
   153
            }
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
   154
            byte[] value = key.getEncoded();
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
   155
            if (value == null) {
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
   156
                throw new InvalidKeyException("Key encoding must not be null");
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
   157
            } else if (value.length != fixedKeySize) {
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
   158
                throw new InvalidKeyException("The key must be " +
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
   159
                    fixedKeySize*8 + " bits");
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
   160
            }
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
   161
        }
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
   162
    }
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
   163
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * internal CipherCore object which does the real work.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    private CipherCore core = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
13672
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
   169
    /*
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
   170
     * needed to support AES oids which associates a fixed key size
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
   171
     * to the cipher object.
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
   172
     */
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
   173
    private final int fixedKeySize; // in bytes, -1 if no restriction
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
   174
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * Creates an instance of AES cipher with default ECB mode and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * PKCS5Padding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     */
13672
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
   179
    protected AESCipher(int keySize) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        core = new CipherCore(new AESCrypt(), AESConstants.AES_BLOCK_SIZE);
13672
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
   181
        fixedKeySize = keySize;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * Sets the mode of this cipher.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * @param mode the cipher mode
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * @exception NoSuchAlgorithmException if the requested cipher mode does
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * not exist
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    protected void engineSetMode(String mode)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        throws NoSuchAlgorithmException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        core.setMode(mode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * Sets the padding mechanism of this cipher.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * @param padding the padding mechanism
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * @exception NoSuchPaddingException if the requested padding mechanism
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * does not exist
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    protected void engineSetPadding(String paddingScheme)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        throws NoSuchPaddingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        core.setPadding(paddingScheme);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * Returns the block size (in bytes).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * @return the block size (in bytes), or 0 if the underlying algorithm is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * not a block cipher
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    protected int engineGetBlockSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        return AESConstants.AES_BLOCK_SIZE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * Returns the length in bytes that an output buffer would need to be in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * order to hold the result of the next <code>update</code> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * <code>doFinal</code> operation, given the input length
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * <code>inputLen</code> (in bytes).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * <p>This call takes into account any unprocessed (buffered) data from a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * previous <code>update</code> call, and padding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * <p>The actual output length of the next <code>update</code> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * <code>doFinal</code> call may be smaller than the length returned by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * @param inputLen the input length (in bytes)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * @return the required output buffer size (in bytes)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    protected int engineGetOutputSize(int inputLen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        return core.getOutputSize(inputLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * Returns the initialization vector (IV) in a new buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * <p>This is useful in the case where a random IV has been created
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * (see <a href = "#init">init</a>),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * or in the context of password-based encryption or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * decryption, where the IV is derived from a user-provided password.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * @return the initialization vector in a new buffer, or null if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * underlying algorithm does not use an IV, or if the IV has not yet
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * been set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    protected byte[] engineGetIV() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        return core.getIV();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * Returns the parameters used with this cipher.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * <p>The returned parameters may be the same that were used to initialize
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * this cipher, or may contain the default set of parameters or a set of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * randomly generated parameters used by the underlying cipher
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * implementation (provided that the underlying cipher implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * uses a default set of parameters or creates new parameters if it needs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * parameters but was not initialized with any).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * @return the parameters used with this cipher, or null if this cipher
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * does not use any parameters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    protected AlgorithmParameters engineGetParameters() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        return core.getParameters("AES");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * Initializes this cipher with a key and a source of randomness.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     * <p>The cipher is initialized for one of the following four operations:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * encryption, decryption, key wrapping or key unwrapping, depending on
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * the value of <code>opmode</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     * <p>If this cipher requires an initialization vector (IV), it will get
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * it from <code>random</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     * This behaviour should only be used in encryption or key wrapping
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     * mode, however.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * When initializing a cipher that requires an IV for decryption or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * key unwrapping, the IV
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * (same IV that was used for encryption or key wrapping) must be provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * explicitly as a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * parameter, in order to get the correct result.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * <p>This method also cleans existing buffer and other related state
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * information.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * @param opmode the operation mode of this cipher (this is one of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * the following:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     * <code>ENCRYPT_MODE</code>, <code>DECRYPT_MODE</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * <code>WRAP_MODE</code> or <code>UNWRAP_MODE</code>)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * @param key the secret key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * @param random the source of randomness
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     * @exception InvalidKeyException if the given key is inappropriate for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     * initializing this cipher
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
    protected void engineInit(int opmode, Key key, SecureRandom random)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        throws InvalidKeyException {
13672
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
   306
        checkKeySize(key, fixedKeySize);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        core.init(opmode, key, random);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * Initializes this cipher with a key, a set of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     * algorithm parameters, and a source of randomness.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     * <p>The cipher is initialized for one of the following four operations:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     * encryption, decryption, key wrapping or key unwrapping, depending on
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * the value of <code>opmode</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     * <p>If this cipher (including its underlying feedback or padding scheme)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * requires any random bytes, it will get them from <code>random</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     * @param opmode the operation mode of this cipher (this is one of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     * the following:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     * <code>ENCRYPT_MODE</code>, <code>DECRYPT_MODE</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     * <code>WRAP_MODE</code> or <code>UNWRAP_MODE</code>)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     * @param key the encryption key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     * @param params the algorithm parameters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     * @param random the source of randomness
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     * @exception InvalidKeyException if the given key is inappropriate for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     * initializing this cipher
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     * @exception InvalidAlgorithmParameterException if the given algorithm
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     * parameters are inappropriate for this cipher
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
    protected void engineInit(int opmode, Key key,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                              AlgorithmParameterSpec params,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                              SecureRandom random)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        throws InvalidKeyException, InvalidAlgorithmParameterException {
13672
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
   338
        checkKeySize(key, fixedKeySize);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        core.init(opmode, key, params, random);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
    protected void engineInit(int opmode, Key key,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                              AlgorithmParameters params,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                              SecureRandom random)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        throws InvalidKeyException, InvalidAlgorithmParameterException {
13672
604588823b5a 7044060: Need to support NSA Suite B Cryptography algorithms
valeriep
parents: 5506
diff changeset
   346
        checkKeySize(key, fixedKeySize);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        core.init(opmode, key, params, random);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     * Continues a multiple-part encryption or decryption operation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     * (depending on how this cipher was initialized), processing another data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     * part.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     * <p>The first <code>inputLen</code> bytes in the <code>input</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     * buffer, starting at <code>inputOffset</code>, are processed, and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     * result is stored in a new buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     * @param input the input buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     * @param inputOffset the offset in <code>input</code> where the input
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     * starts
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     * @param inputLen the input length
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     * @return the new buffer with the result
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     * @exception IllegalStateException if this cipher is in a wrong state
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     * (e.g., has not been initialized)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
    protected byte[] engineUpdate(byte[] input, int inputOffset,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
                                  int inputLen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        return core.update(input, inputOffset, inputLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     * Continues a multiple-part encryption or decryption operation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     * (depending on how this cipher was initialized), processing another data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     * part.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     * <p>The first <code>inputLen</code> bytes in the <code>input</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     * buffer, starting at <code>inputOffset</code>, are processed, and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     * result is stored in the <code>output</code> buffer, starting at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     * <code>outputOffset</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     * @param input the input buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     * @param inputOffset the offset in <code>input</code> where the input
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     * starts
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     * @param inputLen the input length
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     * @param output the buffer for the result
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     * @param outputOffset the offset in <code>output</code> where the result
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     * is stored
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     * @return the number of bytes stored in <code>output</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     * @exception ShortBufferException if the given output buffer is too small
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     * to hold the result
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
    protected int engineUpdate(byte[] input, int inputOffset, int inputLen,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
                               byte[] output, int outputOffset)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        throws ShortBufferException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        return core.update(input, inputOffset, inputLen, output,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
                           outputOffset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     * Encrypts or decrypts data in a single-part operation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     * or finishes a multiple-part operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     * The data is encrypted or decrypted, depending on how this cipher was
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     * initialized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     * <p>The first <code>inputLen</code> bytes in the <code>input</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
     * buffer, starting at <code>inputOffset</code>, and any input bytes that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
     * may have been buffered during a previous <code>update</code> operation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
     * are processed, with padding (if requested) being applied.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
     * The result is stored in a new buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     * <p>The cipher is reset to its initial state (uninitialized) after this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     * call.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     * @param input the input buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     * @param inputOffset the offset in <code>input</code> where the input
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     * starts
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     * @param inputLen the input length
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
     * @return the new buffer with the result
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     * @exception IllegalBlockSizeException if this cipher is a block cipher,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
     * no padding has been requested (only in encryption mode), and the total
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     * input length of the data processed by this cipher is not a multiple of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
     * block size
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
     * @exception BadPaddingException if this cipher is in decryption mode,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
     * and (un)padding has been requested, but the decrypted data is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     * bounded by the appropriate padding bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
    protected byte[] engineDoFinal(byte[] input, int inputOffset, int inputLen)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
        throws IllegalBlockSizeException, BadPaddingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        return core.doFinal(input, inputOffset, inputLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
     * Encrypts or decrypts data in a single-part operation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
     * or finishes a multiple-part operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
     * The data is encrypted or decrypted, depending on how this cipher was
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
     * initialized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
     * <p>The first <code>inputLen</code> bytes in the <code>input</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
     * buffer, starting at <code>inputOffset</code>, and any input bytes that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
     * may have been buffered during a previous <code>update</code> operation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
     * are processed, with padding (if requested) being applied.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
     * The result is stored in the <code>output</code> buffer, starting at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
     * <code>outputOffset</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
     * <p>The cipher is reset to its initial state (uninitialized) after this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
     * call.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
     * @param input the input buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
     * @param inputOffset the offset in <code>input</code> where the input
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
     * starts
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
     * @param inputLen the input length
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
     * @param output the buffer for the result
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
     * @param outputOffset the offset in <code>output</code> where the result
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
     * is stored
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
     * @return the number of bytes stored in <code>output</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
     * @exception IllegalBlockSizeException if this cipher is a block cipher,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
     * no padding has been requested (only in encryption mode), and the total
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
     * input length of the data processed by this cipher is not a multiple of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
     * block size
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
     * @exception ShortBufferException if the given output buffer is too small
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
     * to hold the result
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
     * @exception BadPaddingException if this cipher is in decryption mode,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
     * and (un)padding has been requested, but the decrypted data is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
     * bounded by the appropriate padding bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
    protected int engineDoFinal(byte[] input, int inputOffset, int inputLen,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
                                byte[] output, int outputOffset)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
        throws IllegalBlockSizeException, ShortBufferException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
               BadPaddingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
        return core.doFinal(input, inputOffset, inputLen, output,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
                            outputOffset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
     *  Returns the key size of the given key object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
     * @param key the key object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
     * @return the key size of the given key object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
     * @exception InvalidKeyException if <code>key</code> is invalid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
    protected int engineGetKeySize(Key key) throws InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
        byte[] encoded = key.getEncoded();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
        if (!AESCrypt.isKeySizeValid(encoded.length)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
            throw new InvalidKeyException("Invalid AES key length: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
                                          encoded.length + " bytes");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
        return encoded.length * 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
     * Wrap a key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
     * @param key the key to be wrapped.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
     * @return the wrapped key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
     * @exception IllegalBlockSizeException if this cipher is a block
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
     * cipher, no padding has been requested, and the length of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
     * encoding of the key to be wrapped is not a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
     * multiple of the block size.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
     * @exception InvalidKeyException if it is impossible or unsafe to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
     * wrap the key with this cipher (e.g., a hardware protected key is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
     * being passed to a software only cipher).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
    protected byte[] engineWrap(Key key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
        throws IllegalBlockSizeException, InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
        return core.wrap(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
     * Unwrap a previously wrapped key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
     * @param wrappedKey the key to be unwrapped.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
     * @param wrappedKeyAlgorithm the algorithm the wrapped key is for.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
     * @param wrappedKeyType the type of the wrapped key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
     * This is one of <code>Cipher.SECRET_KEY</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
     * <code>Cipher.PRIVATE_KEY</code>, or <code>Cipher.PUBLIC_KEY</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
     * @return the unwrapped key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
     * @exception NoSuchAlgorithmException if no installed providers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
     * can create keys of type <code>wrappedKeyType</code> for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
     * <code>wrappedKeyAlgorithm</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
     * @exception InvalidKeyException if <code>wrappedKey</code> does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
     * represent a wrapped key of type <code>wrappedKeyType</code> for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
     * the <code>wrappedKeyAlgorithm</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
    protected Key engineUnwrap(byte[] wrappedKey,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
                                     String wrappedKeyAlgorithm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
                                     int wrappedKeyType)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
        throws InvalidKeyException, NoSuchAlgorithmException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
        return core.unwrap(wrappedKey, wrappedKeyAlgorithm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
                           wrappedKeyType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
    }
15008
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents: 13672
diff changeset
   550
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents: 13672
diff changeset
   551
    /**
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents: 13672
diff changeset
   552
     * Continues a multi-part update of the Additional Authentication
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents: 13672
diff changeset
   553
     * Data (AAD), using a subset of the provided buffer.
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents: 13672
diff changeset
   554
     * <p>
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents: 13672
diff changeset
   555
     * Calls to this method provide AAD to the cipher when operating in
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents: 13672
diff changeset
   556
     * modes such as AEAD (GCM/CCM).  If this cipher is operating in
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents: 13672
diff changeset
   557
     * either GCM or CCM mode, all AAD must be supplied before beginning
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents: 13672
diff changeset
   558
     * operations on the ciphertext (via the {@code update} and {@code
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents: 13672
diff changeset
   559
     * doFinal} methods).
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents: 13672
diff changeset
   560
     *
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents: 13672
diff changeset
   561
     * @param src the buffer containing the AAD
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents: 13672
diff changeset
   562
     * @param offset the offset in {@code src} where the AAD input starts
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents: 13672
diff changeset
   563
     * @param len the number of AAD bytes
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents: 13672
diff changeset
   564
     *
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents: 13672
diff changeset
   565
     * @throws IllegalStateException if this cipher is in a wrong state
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents: 13672
diff changeset
   566
     * (e.g., has not been initialized), does not accept AAD, or if
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents: 13672
diff changeset
   567
     * operating in either GCM or CCM mode and one of the {@code update}
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents: 13672
diff changeset
   568
     * methods has already been called for the active
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents: 13672
diff changeset
   569
     * encryption/decryption operation
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents: 13672
diff changeset
   570
     * @throws UnsupportedOperationException if this method
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents: 13672
diff changeset
   571
     * has not been overridden by an implementation
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents: 13672
diff changeset
   572
     *
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents: 13672
diff changeset
   573
     * @since 1.8
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents: 13672
diff changeset
   574
     */
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents: 13672
diff changeset
   575
    @Override
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents: 13672
diff changeset
   576
    protected void engineUpdateAAD(byte[] src, int offset, int len) {
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents: 13672
diff changeset
   577
        core.updateAAD(src, offset, len);
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents: 13672
diff changeset
   578
    }
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents: 13672
diff changeset
   579
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents: 13672
diff changeset
   580
    /**
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents: 13672
diff changeset
   581
     * Continues a multi-part update of the Additional Authentication
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents: 13672
diff changeset
   582
     * Data (AAD).
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents: 13672
diff changeset
   583
     * <p>
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents: 13672
diff changeset
   584
     * Calls to this method provide AAD to the cipher when operating in
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents: 13672
diff changeset
   585
     * modes such as AEAD (GCM/CCM).  If this cipher is operating in
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents: 13672
diff changeset
   586
     * either GCM or CCM mode, all AAD must be supplied before beginning
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents: 13672
diff changeset
   587
     * operations on the ciphertext (via the {@code update} and {@code
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents: 13672
diff changeset
   588
     * doFinal} methods).
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents: 13672
diff changeset
   589
     * <p>
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents: 13672
diff changeset
   590
     * All {@code src.remaining()} bytes starting at
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents: 13672
diff changeset
   591
     * {@code src.position()} are processed.
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents: 13672
diff changeset
   592
     * Upon return, the input buffer's position will be equal
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents: 13672
diff changeset
   593
     * to its limit; its limit will not have changed.
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents: 13672
diff changeset
   594
     *
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents: 13672
diff changeset
   595
     * @param src the buffer containing the AAD
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents: 13672
diff changeset
   596
     *
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents: 13672
diff changeset
   597
     * @throws IllegalStateException if this cipher is in a wrong state
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents: 13672
diff changeset
   598
     * (e.g., has not been initialized), does not accept AAD, or if
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents: 13672
diff changeset
   599
     * operating in either GCM or CCM mode and one of the {@code update}
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents: 13672
diff changeset
   600
     * methods has already been called for the active
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents: 13672
diff changeset
   601
     * encryption/decryption operation
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents: 13672
diff changeset
   602
     * @throws UnsupportedOperationException if this method
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents: 13672
diff changeset
   603
     * has not been overridden by an implementation
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents: 13672
diff changeset
   604
     *
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents: 13672
diff changeset
   605
     * @since 1.8
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents: 13672
diff changeset
   606
     */
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents: 13672
diff changeset
   607
    @Override
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents: 13672
diff changeset
   608
    protected void engineUpdateAAD(ByteBuffer src) {
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents: 13672
diff changeset
   609
        if (src != null) {
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents: 13672
diff changeset
   610
            int aadLen = src.limit() - src.position();
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents: 13672
diff changeset
   611
            if (aadLen != 0) {
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents: 13672
diff changeset
   612
                if (src.hasArray()) {
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents: 13672
diff changeset
   613
                    int aadOfs = src.arrayOffset() + src.position();
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents: 13672
diff changeset
   614
                    core.updateAAD(src.array(), aadOfs, aadLen);
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents: 13672
diff changeset
   615
                    src.position(src.limit());
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents: 13672
diff changeset
   616
                } else {
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents: 13672
diff changeset
   617
                    byte[] aad = new byte[aadLen];
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents: 13672
diff changeset
   618
                    src.get(aad);
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents: 13672
diff changeset
   619
                    core.updateAAD(aad, 0, aadLen);
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents: 13672
diff changeset
   620
                }
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents: 13672
diff changeset
   621
            }
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents: 13672
diff changeset
   622
        }
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents: 13672
diff changeset
   623
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
}
15008
6a494f8ba5b5 6996769: support AEAD cipher
valeriep
parents: 13672
diff changeset
   625