The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package org.apache.harmony.xnet.provider.jsse; |
| 18 | |
| 19 | import org.bouncycastle.crypto.ExtendedDigest; |
| 20 | |
Urs Grob | 87eb4de | 2009-07-22 20:06:03 +0200 | [diff] [blame] | 21 | import java.security.NoSuchAlgorithmException; |
| 22 | |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 23 | /** |
| 24 | * Implements the BouncyCastle Digest interface using OpenSSL's EVP API. |
| 25 | */ |
| 26 | public 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 Hughes | f33eae7 | 2010-05-13 12:36:25 -0700 | [diff] [blame] | 46 | * |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 47 | * @param algorithm The name of the algorithm, e.g. "SHA1". |
Elliott Hughes | f33eae7 | 2010-05-13 12:36:25 -0700 | [diff] [blame] | 48 | * |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 49 | * @return The new OpenSSLMessageDigest instance. |
Elliott Hughes | f33eae7 | 2010-05-13 12:36:25 -0700 | [diff] [blame] | 50 | * |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 51 | * @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 Hughes | f33eae7 | 2010-05-13 12:36:25 -0700 | [diff] [blame] | 60 | * |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 61 | * @param algorithm The name of the algorithm, e.g. "SHA1". |
| 62 | */ |
| 63 | private OpenSSLMessageDigest(String algorithm) { |
| 64 | this.algorithm = algorithm; |
Urs Grob | 87eb4de | 2009-07-22 20:06:03 +0200 | [diff] [blame] | 65 | |
| 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 Hughes | 79f07cc | 2010-06-11 15:58:28 -0700 | [diff] [blame] | 69 | .equals(algorithm)) { |
Urs Grob | 87eb4de | 2009-07-22 20:06:03 +0200 | [diff] [blame] | 70 | throw new RuntimeException(algorithm + " not supported"); |
| 71 | } |
| 72 | |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 73 | 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 Hughes | f33eae7 | 2010-05-13 12:36:25 -0700 | [diff] [blame] | 80 | |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 81 | 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 Hughes | f33eae7 | 2010-05-13 12:36:25 -0700 | [diff] [blame] | 117 | |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 118 | } |