blob: 0fd5879d8b621ae94d1062be36e380c76bbe78c2 [file] [log] [blame]
The Android Open Source Projectadc854b2009-03-03 19:28:47 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package org.apache.harmony.xnet.provider.jsse;
18
19import org.bouncycastle.crypto.ExtendedDigest;
20
Urs Grob87eb4de2009-07-22 20:06:03 +020021import java.security.NoSuchAlgorithmException;
22
The Android Open Source Projectadc854b2009-03-03 19:28:47 -080023/**
24 * Implements the BouncyCastle Digest interface using OpenSSL's EVP API.
25 */
26public class OpenSSLMessageDigest implements ExtendedDigest {
27
28 /**
29 * Holds the name of the hashing algorithm, e.g. "SHA-1";
30 */
31 private String algorithm;
32
33 /**
34 * Holds a pointer to the native message digest context.
35 */
36 private int ctx;
37
38 /**
39 * Holds a dummy buffer for writing single bytes to the digest.
40 */
41 private byte[] singleByte = new byte[1];
42
43 /**
44 * Creates a new OpenSSLMessageDigest instance for the given algorithm
45 * name.
Elliott Hughesf33eae72010-05-13 12:36:25 -070046 *
The Android Open Source Projectadc854b2009-03-03 19:28:47 -080047 * @param algorithm The name of the algorithm, e.g. "SHA1".
Elliott Hughesf33eae72010-05-13 12:36:25 -070048 *
The Android Open Source Projectadc854b2009-03-03 19:28:47 -080049 * @return The new OpenSSLMessageDigest instance.
Elliott Hughesf33eae72010-05-13 12:36:25 -070050 *
The Android Open Source Projectadc854b2009-03-03 19:28:47 -080051 * @throws RuntimeException In case of problems.
52 */
53 public static OpenSSLMessageDigest getInstance(String algorithm) {
54 return new OpenSSLMessageDigest(algorithm);
55 }
56
57 /**
58 * Creates a new OpenSSLMessageDigest instance for the given algorithm
59 * name.
Elliott Hughesf33eae72010-05-13 12:36:25 -070060 *
The Android Open Source Projectadc854b2009-03-03 19:28:47 -080061 * @param algorithm The name of the algorithm, e.g. "SHA1".
62 */
63 private OpenSSLMessageDigest(String algorithm) {
64 this.algorithm = algorithm;
Urs Grob87eb4de2009-07-22 20:06:03 +020065
66 // We don't support MD2 anymore. This needs to also check for aliases
67 // and OIDs.
68 if ("MD2".equalsIgnoreCase(algorithm) || "1.2.840.113549.2.2"
Elliott Hughes79f07cc2010-06-11 15:58:28 -070069 .equals(algorithm)) {
Urs Grob87eb4de2009-07-22 20:06:03 +020070 throw new RuntimeException(algorithm + " not supported");
71 }
72
The Android Open Source Projectadc854b2009-03-03 19:28:47 -080073 ctx = NativeCrypto.EVP_new();
74 try {
75 NativeCrypto.EVP_DigestInit(ctx, algorithm.replace("-", "").toLowerCase());
76 } catch (Exception ex) {
77 throw new RuntimeException(ex.getMessage() + " (" + algorithm + ")");
78 }
79 }
Elliott Hughesf33eae72010-05-13 12:36:25 -070080
The Android Open Source Projectadc854b2009-03-03 19:28:47 -080081 public int doFinal(byte[] out, int outOff) {
82 int i = NativeCrypto.EVP_DigestFinal(ctx, out, outOff);
83 reset();
84 return i;
85 }
86
87 public String getAlgorithmName() {
88 return algorithm;
89 }
90
91 public int getDigestSize() {
92 return NativeCrypto.EVP_DigestSize(ctx);
93 }
94
95 public int getByteLength() {
96 return NativeCrypto.EVP_DigestBlockSize(ctx);
97 }
98
99 public void reset() {
100 NativeCrypto.EVP_DigestInit(ctx, algorithm.replace("-", "").toLowerCase());
101 }
102
103 public void update(byte in) {
104 singleByte[0] = in;
105 NativeCrypto.EVP_DigestUpdate(ctx, singleByte, 0, 1);
106 }
107
108 public void update(byte[] in, int inOff, int len) {
109 NativeCrypto.EVP_DigestUpdate(ctx, in, inOff, len);
110 }
111
112 @Override
113 protected void finalize() throws Throwable {
114 super.finalize();
115 NativeCrypto.EVP_free(ctx);
116 }
Elliott Hughesf33eae72010-05-13 12:36:25 -0700117
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800118}