jdk/src/jdk.crypto.token/share/classes/sun/security/pkcs11/P11Mac.java
author mchung
Wed, 14 Dec 2016 10:51:13 -0800
changeset 42693 6645de32a866
parent 25859 jdk/src/jdk.crypto.pkcs11/share/classes/sun/security/pkcs11/P11Mac.java@3317bb8137f4
permissions -rw-r--r--
8171201: Drop java.compact$N aggregator modules 8171202: Rename jdk.crypto.pkcs11 and jdk.pack200 to end with Java letters Reviewed-by: alanb, erikj
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
12685
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 5506
diff changeset
     2
 * Copyright (c) 2003, 2012, 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: 5291
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: 5291
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: 5291
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5291
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5291
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 sun.security.pkcs11;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.nio.ByteBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.security.spec.AlgorithmParameterSpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import javax.crypto.MacSpi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import sun.nio.ch.DirectBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import sun.security.pkcs11.wrapper.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import static sun.security.pkcs11.wrapper.PKCS11Constants.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * MAC implementation class. This class currently supports HMAC using
12685
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 5506
diff changeset
    43
 * MD5, SHA-1, SHA-224, SHA-256, SHA-384, and SHA-512 and the SSL3 MAC
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 5506
diff changeset
    44
 * using MD5 and SHA-1.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * Note that unlike other classes (e.g. Signature), this does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * composite various operations if the token only supports part of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * required functionality. The MAC implementations in SunJCE already
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * do exactly that by implementing an MAC on top of MessageDigests. We
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * could not do any better than they.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * @author  Andreas Sterbenz
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * @since   1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
final class P11Mac extends MacSpi {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    /* unitialized, all fields except session have arbitrary values */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    private final static int S_UNINIT   = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    /* session initialized, no data processed yet */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    private final static int S_RESET    = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    /* session initialized, data processed */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    private final static int S_UPDATE   = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    /* transitional state after doFinal() before we go to S_UNINIT */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    private final static int S_DOFINAL  = 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    // token instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    private final Token token;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    // algorithm name
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    private final String algorithm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    // mechanism id
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    private final long mechanism;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    // mechanism object
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    private final CK_MECHANISM ckMechanism;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    // length of the MAC in bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    private final int macLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    // key instance used, if operation active
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    private P11Key p11Key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    // associated session, if any
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    private Session session;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    // state, one of S_* above
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    private int state;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    // one byte buffer for the update(byte) method, initialized on demand
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    private byte[] oneByte;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    P11Mac(Token token, String algorithm, long mechanism)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            throws PKCS11Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        super();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        this.token = token;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        this.algorithm = algorithm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        this.mechanism = mechanism;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        Long params = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        switch ((int)mechanism) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        case (int)CKM_MD5_HMAC:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            macLength = 16;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        case (int)CKM_SHA_1_HMAC:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            macLength = 20;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            break;
12685
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 5506
diff changeset
   110
        case (int)CKM_SHA224_HMAC:
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 5506
diff changeset
   111
            macLength = 28;
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 5506
diff changeset
   112
            break;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        case (int)CKM_SHA256_HMAC:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            macLength = 32;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        case (int)CKM_SHA384_HMAC:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            macLength = 48;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        case (int)CKM_SHA512_HMAC:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            macLength = 64;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        case (int)CKM_SSL3_MD5_MAC:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            macLength = 16;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            params = Long.valueOf(16);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        case (int)CKM_SSL3_SHA1_MAC:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            macLength = 20;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            params = Long.valueOf(20);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            throw new ProviderException("Unknown mechanism: " + mechanism);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        ckMechanism = new CK_MECHANISM(mechanism, params);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        state = S_UNINIT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        initialize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    private void ensureInitialized() throws PKCS11Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        token.ensureValid();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        if (state == S_UNINIT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            initialize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    private void cancelOperation() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        token.ensureValid();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        if (state == S_UNINIT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        state = S_UNINIT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        if ((session == null) || (token.explicitCancel == false)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            token.p11.C_SignFinal(session.id(), 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        } catch (PKCS11Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            throw new ProviderException("Cancel failed", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    private void initialize() throws PKCS11Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        if (state == S_RESET) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        if (session == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            session = token.getOpSession();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        if (p11Key != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            token.p11.C_SignInit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                (session.id(), ckMechanism, p11Key.keyID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            state = S_RESET;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            state = S_UNINIT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    // see JCE spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    protected int engineGetMacLength() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        return macLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    // see JCE spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    protected void engineReset() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        // the framework insists on calling reset() after doFinal(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        // but we prefer to take care of reinitialization ourselves
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        if (state == S_DOFINAL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            state = S_UNINIT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        cancelOperation();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            initialize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        } catch (PKCS11Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            throw new ProviderException("reset() failed, ", e);
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
    // see JCE spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    protected void engineInit(Key key, AlgorithmParameterSpec params)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            throws InvalidKeyException, InvalidAlgorithmParameterException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        if (params != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            throw new InvalidAlgorithmParameterException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                ("Parameters not supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        cancelOperation();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        p11Key = P11SecretKeyFactory.convertKey(token, key, algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            initialize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        } catch (PKCS11Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            throw new InvalidKeyException("init() failed", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    // see JCE spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    protected byte[] engineDoFinal() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            ensureInitialized();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            byte[] mac = token.p11.C_SignFinal(session.id(), 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            state = S_DOFINAL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            return mac;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        } catch (PKCS11Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            throw new ProviderException("doFinal() failed", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            session = token.releaseSession(session);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    // see JCE spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    protected void engineUpdate(byte input) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        if (oneByte == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
           oneByte = new byte[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        oneByte[0] = input;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        engineUpdate(oneByte, 0, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    // see JCE spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    protected void engineUpdate(byte[] b, int ofs, int len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            ensureInitialized();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            token.p11.C_SignUpdate(session.id(), 0, b, ofs, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            state = S_UPDATE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        } catch (PKCS11Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            throw new ProviderException("update() failed", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    // see JCE spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    protected void engineUpdate(ByteBuffer byteBuffer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            ensureInitialized();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            int len = byteBuffer.remaining();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            if (len <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            if (byteBuffer instanceof DirectBuffer == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                super.engineUpdate(byteBuffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            long addr = ((DirectBuffer)byteBuffer).address();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            int ofs = byteBuffer.position();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            token.p11.C_SignUpdate(session.id(), addr + ofs, null, 0, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            byteBuffer.position(ofs + len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            state = S_UPDATE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        } catch (PKCS11Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            throw new ProviderException("update() failed", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
}