summaryrefslogtreecommitdiff
path: root/src/main/java/org/bouncycastle/crypto/digests/OpenSSLDigest.java
blob: 14e691223aea0f7b5372efe1d88b346023e23b4d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*
 * Copyright (C) 2008 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.bouncycastle.crypto.digests;

import org.apache.harmony.xnet.provider.jsse.NativeCrypto;
import org.bouncycastle.crypto.ExtendedDigest;

/**
 * Implements the BouncyCastle Digest interface using OpenSSL's EVP API.
 */
public class OpenSSLDigest implements ExtendedDigest {

    /**
     * Holds the standard name of the hashing algorithm, e.g. "SHA-1";
     */
    private String algorithm;

    /**
     * Holds the OpenSSL name of the hashing algorithm, e.g. "sha1";
     */
    private String openssl;

    /**
     * Holds a pointer to the native message digest context.
     */
    private int ctx;

    /**
     * Holds a dummy buffer for writing single bytes to the digest.
     */
    private byte[] singleByte = new byte[1];

    /**
     * Creates a new OpenSSLMessageDigest instance for the given algorithm
     * name.
     *
     * @param algorithm The standard name of the algorithm, e.g. "SHA-1".
     * @param algorithm The name of the openssl algorithm, e.g. "sha1".
     */
    private OpenSSLDigest(String algorithm, String openssl) {
        this.algorithm = algorithm;
        ctx = NativeCrypto.EVP_MD_CTX_create();
        try {
            NativeCrypto.EVP_DigestInit(ctx, openssl);
        } catch (Exception ex) {
            throw new RuntimeException(ex.getMessage() + " (" + algorithm + ")");
        }
    }

    public int doFinal(byte[] out, int outOff) {
        int i = NativeCrypto.EVP_DigestFinal(ctx, out, outOff);
        reset();
        return i;
    }

    public String getAlgorithmName() {
        return algorithm;
    }

    public int getDigestSize() {
        return NativeCrypto.EVP_MD_CTX_size(ctx);
    }

    public int getByteLength() {
        return NativeCrypto.EVP_MD_CTX_block_size(ctx);
    }

    public void reset() {
        NativeCrypto.EVP_DigestInit(ctx, openssl);
    }

    public void update(byte in) {
        singleByte[0] = in;
        NativeCrypto.EVP_DigestUpdate(ctx, singleByte, 0, 1);
    }

    public void update(byte[] in, int inOff, int len) {
        NativeCrypto.EVP_DigestUpdate(ctx, in, inOff, len);
    }

    @Override
    protected void finalize() throws Throwable {
        super.finalize();
        NativeCrypto.EVP_MD_CTX_destroy(ctx);
        ctx = 0;
    }

    public static class MD5 extends OpenSSLDigest {
        public MD5() { super("MD5", "md5"); }
    }

    public static class SHA1 extends OpenSSLDigest {
        public SHA1() { super("SHA-1", "sha1"); }
    }

    public static class SHA256 extends OpenSSLDigest {
        public SHA256() { super("SHA-256", "sha256"); }
    }

    public static class SHA384 extends OpenSSLDigest {
        public SHA384() { super("SHA-384", "sha384"); }
    }

    public static class SHA512 extends OpenSSLDigest {
        public SHA512() { super("SHA-512", "sha512"); }
    }
}