src/jdk.crypto.ucrypto/solaris/classes/com/oracle/security/ucrypto/NativeDigestMD.java
author eosterlund
Mon, 29 Jul 2019 13:57:54 +0200
changeset 57582 a79a819a8218
parent 47216 71c04702a3d5
permissions -rw-r--r--
8227407: ZGC: C2 loads and load barriers can get separated by safepoints Reviewed-by: neliasso, smonteith, roland
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
37909
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
     1
/*
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
     2
 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
     4
 *
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
    10
 *
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
    15
 * accompanied this code).
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
    16
 *
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
    20
 *
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
    23
 * questions.
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
    24
 */
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
    25
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
    26
package com.oracle.security.ucrypto;
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
    27
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
    28
import java.lang.ref.*;
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
    29
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
    30
import java.io.ByteArrayOutputStream;
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
    31
import java.util.*;
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
    32
import java.util.concurrent.ConcurrentSkipListSet;
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
    33
import java.security.*;
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
    34
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
    35
/**
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
    36
 * MessageDigest implementation class for libMD API. This class currently supports
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
    37
 * MD5, SHA1, SHA256, SHA384, and SHA512
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
    38
 *
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
    39
 * @since 9
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
    40
 */
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
    41
public abstract class NativeDigestMD extends MessageDigestSpi
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
    42
        implements Cloneable {
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
    43
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
    44
    private static final int MECH_MD5 = 1;
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
    45
    private static final int MECH_SHA1 = 2;
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
    46
    private static final int MECH_SHA256 = 3;
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
    47
    private static final int MECH_SHA224 = 4;
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
    48
    private static final int MECH_SHA384 = 5;
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
    49
    private static final int MECH_SHA512 = 6;
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
    50
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
    51
    private final int digestLen;
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
    52
    private final int mech;
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
    53
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
    54
    // field for ensuring native memory is freed
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
    55
    private DigestContextRef pCtxt = null;
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
    56
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
    57
    private static class DigestContextRef extends PhantomReference<NativeDigestMD>
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
    58
        implements Comparable<DigestContextRef> {
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
    59
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
    60
        private static ReferenceQueue<NativeDigestMD> refQueue =
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
    61
            new ReferenceQueue<NativeDigestMD>();
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
    62
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
    63
        // Needed to keep these references from being GC'ed until when their
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
    64
        // referents are GC'ed so we can do post-mortem processing
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
    65
        private static Set<DigestContextRef> refList =
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
    66
            new ConcurrentSkipListSet<DigestContextRef>();
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
    67
            //            Collections.synchronizedSortedSet(new TreeSet<DigestContextRef>());
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
    68
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
    69
        private final long id;
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
    70
        private final int mech;
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
    71
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
    72
        private static void drainRefQueueBounded() {
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
    73
            while (true) {
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
    74
                DigestContextRef next = (DigestContextRef) refQueue.poll();
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
    75
                if (next == null) break;
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
    76
                next.dispose(true);
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
    77
            }
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
    78
        }
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
    79
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
    80
        DigestContextRef(NativeDigestMD nc, long id, int mech) {
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
    81
            super(nc, refQueue);
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
    82
            this.id = id;
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
    83
            this.mech = mech;
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
    84
            refList.add(this);
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
    85
            UcryptoProvider.debug("Resource: track Digest Ctxt " + this.id);
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
    86
            drainRefQueueBounded();
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
    87
        }
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
    88
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
    89
        public int compareTo(DigestContextRef other) {
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
    90
            if (this.id == other.id) {
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
    91
                return 0;
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
    92
            } else {
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
    93
                return (this.id < other.id) ? -1 : 1;
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
    94
            }
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
    95
        }
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
    96
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
    97
        void dispose(boolean needFree) {
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
    98
            refList.remove(this);
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
    99
            try {
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
   100
                if (needFree) {
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
   101
                    UcryptoProvider.debug("Resource: free Digest Ctxt " + this.id);
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
   102
                    NativeDigestMD.nativeFree(mech, id);
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
   103
                } else UcryptoProvider.debug("Resource: stop tracking Digest Ctxt " + this.id);
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
   104
            } finally {
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
   105
                this.clear();
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
   106
            }
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
   107
        }
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
   108
    }
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
   109
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
   110
    NativeDigestMD(int mech, int digestLen) {
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
   111
        this.digestLen = digestLen;
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
   112
        this.mech = mech;
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
   113
    }
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
   114
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
   115
    // see JCA spec
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
   116
    protected int engineGetDigestLength() {
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
   117
        return digestLen;
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
   118
    }
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
   119
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
   120
    // see JCA spec
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
   121
    protected synchronized void engineReset() {
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
   122
        if (pCtxt != null) {
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
   123
            pCtxt.dispose(true);
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
   124
            pCtxt = null;
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
   125
        }
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
   126
    }
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
   127
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
   128
    // see JCA spec
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
   129
    protected synchronized byte[] engineDigest() {
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
   130
        byte[] digest = new byte[digestLen];
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
   131
        try {
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
   132
            int len = engineDigest(digest, 0, digestLen);
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
   133
            if (len != digestLen) {
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
   134
                throw new UcryptoException("Digest length mismatch." +
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
   135
                    " Len: " + len + ". digestLen: " + digestLen);
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
   136
            }
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
   137
            return digest;
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
   138
        } catch (DigestException de) {
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
   139
            throw new UcryptoException("Internal error", de);
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
   140
        }
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
   141
    }
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
   142
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
   143
    // see JCA spec
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
   144
    protected synchronized int engineDigest(byte[] out, int ofs, int len)
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
   145
            throws DigestException {
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
   146
        if (len < digestLen) {
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
   147
            throw new DigestException("Output buffer must be at least " +
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
   148
                          digestLen + " bytes long. Got: " + len);
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
   149
        }
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
   150
        if ((ofs < 0) || (len < 0) || (ofs > out.length - len)) {
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
   151
            throw new DigestException("Buffer too short to store digest. " +
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
   152
                "ofs: " + ofs + ". len: " + len + ". out.length: " + out.length);
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
   153
        }
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
   154
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
   155
        if (pCtxt == null) {
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
   156
            pCtxt = new DigestContextRef(this, nativeInit(mech), mech);
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
   157
        }
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
   158
        try {
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
   159
            int status = nativeDigest(mech, pCtxt.id, out, ofs, digestLen);
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
   160
            if (status != 0) {
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
   161
                throw new DigestException("Internal error: " + status);
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
   162
            }
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
   163
        } finally {
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
   164
            pCtxt.dispose(false);
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
   165
            pCtxt = null;
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
   166
        }
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
   167
        return digestLen;
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
   168
    }
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
   169
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
   170
    // see JCA spec
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
   171
    protected synchronized void engineUpdate(byte in) {
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
   172
        byte[] temp = { in };
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
   173
        engineUpdate(temp, 0, 1);
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
   174
    }
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
   175
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
   176
    // see JCA spec
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
   177
    protected synchronized void engineUpdate(byte[] in, int ofs, int len) {
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
   178
        if (len == 0) {
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
   179
            return;
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
   180
        }
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
   181
        if ((ofs < 0) || (len < 0) || (ofs > in.length - len)) {
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
   182
            throw new ArrayIndexOutOfBoundsException("ofs: " + ofs + ". len: "
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
   183
                + len + ". in.length: " + in.length);
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
   184
        }
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
   185
        if (pCtxt == null) {
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
   186
            pCtxt = new DigestContextRef(this, nativeInit(mech), mech);
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
   187
        }
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
   188
        nativeUpdate(mech, pCtxt.id, in, ofs, len);
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
   189
    }
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
   190
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
   191
    /**
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
   192
     * Clone this digest.
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
   193
     */
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
   194
    public synchronized Object clone() throws CloneNotSupportedException {
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
   195
        NativeDigestMD copy = (NativeDigestMD) super.clone();
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
   196
        // re-work the fields that cannot be copied over
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
   197
        if (pCtxt != null) {
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
   198
            copy.pCtxt = new DigestContextRef(this, nativeClone(mech, pCtxt.id), mech);
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
   199
        }
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
   200
        return copy;
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
   201
    }
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
   202
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
   203
    // return pointer to the context
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
   204
    protected static final native long nativeInit(int mech);
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
   205
    // return status code; always 0
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
   206
    protected static final native int nativeUpdate(int mech, long pCtxt, byte[] in, int ofs, int inLen);
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
   207
    // return status code; always 0
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
   208
    protected static final native int nativeDigest(int mech, long pCtxt, byte[] out, int ofs, int digestLen);
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
   209
    // return pointer to the duplicated context
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
   210
    protected static final native long nativeClone(int mech, long pCtxt);
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
   211
    // free the specified context
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
   212
    private static final native void nativeFree(int mech, long id);
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
   213
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
   214
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
   215
    public static final class MD5 extends NativeDigestMD {
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
   216
        public MD5() {
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
   217
            super(MECH_MD5, 16);
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
   218
        }
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
   219
    }
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
   220
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
   221
    public static final class SHA1 extends NativeDigestMD {
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
   222
        public SHA1() {
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
   223
            super(MECH_SHA1, 20);
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
   224
        }
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
   225
    }
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
   226
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
   227
    public static final class SHA256 extends NativeDigestMD {
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
   228
        public SHA256() {
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
   229
            super(MECH_SHA256, 32);
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
   230
        }
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
   231
    }
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
   232
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
   233
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
   234
    public static final class SHA384 extends NativeDigestMD {
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
   235
        public SHA384() {
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
   236
            super(MECH_SHA384, 48);
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
   237
        }
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
   238
    }
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
   239
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
   240
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
   241
    public static final class SHA512 extends NativeDigestMD {
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
   242
        public SHA512() {
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
   243
            super(MECH_SHA512, 64);
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
   244
        }
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
   245
    }
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents:
diff changeset
   246
}