1 /* 2 * Copyright 2016 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.conscrypt; 18 19 import java.security.NoSuchAlgorithmException; 20 import java.util.Locale; 21 22 /** 23 * Utility class to convert between BoringSSL- and JCE-style message digest identifiers. 24 * 25 * @hide 26 */ 27 @Internal 28 public final class EvpMdRef { 29 public static final String MGF1_ALGORITHM_NAME = "MGF1"; 30 public static final String MGF1_OID = "1.2.840.113549.1.1.8"; 31 32 /** 33 * Returns the canonical JCA digest algorithm name for the provided digest 34 * algorithm name or {@code null} if the digest algorithm is not known. 35 */ getJcaDigestAlgorithmStandardName(String algorithm)36 public static String getJcaDigestAlgorithmStandardName(String algorithm) { 37 String algorithmUpper = algorithm.toUpperCase(Locale.US); 38 if ((SHA256.JCA_NAME.equals(algorithmUpper)) || (SHA256.OID.equals(algorithmUpper))) { 39 return SHA256.JCA_NAME; 40 } else if ((SHA512.JCA_NAME.equals(algorithmUpper)) 41 || (SHA512.OID.equals(algorithmUpper))) { 42 return SHA512.JCA_NAME; 43 } else if ((SHA1.JCA_NAME.equals(algorithmUpper)) || (SHA1.OID.equals(algorithmUpper))) { 44 return SHA1.JCA_NAME; 45 } else if ((SHA384.JCA_NAME.equals(algorithmUpper)) 46 || (SHA384.OID.equals(algorithmUpper))) { 47 return SHA384.JCA_NAME; 48 } else if ((SHA224.JCA_NAME.equals(algorithmUpper)) 49 || (SHA224.OID.equals(algorithmUpper))) { 50 return SHA224.JCA_NAME; 51 } else { 52 return null; 53 } 54 } 55 getEVP_MDByJcaDigestAlgorithmStandardName(String algorithm)56 public static long getEVP_MDByJcaDigestAlgorithmStandardName(String algorithm) 57 throws NoSuchAlgorithmException { 58 String algorithmUpper = algorithm.toUpperCase(Locale.US); 59 if (SHA256.JCA_NAME.equals(algorithmUpper)) { 60 return EvpMdRef.SHA256.EVP_MD; 61 } else if (SHA512.JCA_NAME.equals(algorithmUpper)) { 62 return EvpMdRef.SHA512.EVP_MD; 63 } else if (SHA1.JCA_NAME.equals(algorithmUpper)) { 64 return EvpMdRef.SHA1.EVP_MD; 65 } else if (SHA384.JCA_NAME.equals(algorithmUpper)) { 66 return EvpMdRef.SHA384.EVP_MD; 67 } else if (SHA224.JCA_NAME.equals(algorithmUpper)) { 68 return EvpMdRef.SHA224.EVP_MD; 69 } else { 70 throw new NoSuchAlgorithmException("Unsupported algorithm: " + algorithm); 71 } 72 } 73 getDigestSizeBytesByJcaDigestAlgorithmStandardName(String algorithm)74 public static int getDigestSizeBytesByJcaDigestAlgorithmStandardName(String algorithm) 75 throws NoSuchAlgorithmException { 76 String algorithmUpper = algorithm.toUpperCase(Locale.US); 77 if (SHA256.JCA_NAME.equals(algorithmUpper)) { 78 return EvpMdRef.SHA256.SIZE_BYTES; 79 } else if (SHA512.JCA_NAME.equals(algorithmUpper)) { 80 return EvpMdRef.SHA512.SIZE_BYTES; 81 } else if (SHA1.JCA_NAME.equals(algorithmUpper)) { 82 return EvpMdRef.SHA1.SIZE_BYTES; 83 } else if (SHA384.JCA_NAME.equals(algorithmUpper)) { 84 return EvpMdRef.SHA384.SIZE_BYTES; 85 } else if (SHA224.JCA_NAME.equals(algorithmUpper)) { 86 return EvpMdRef.SHA224.SIZE_BYTES; 87 } else { 88 throw new NoSuchAlgorithmException("Unsupported algorithm: " + algorithm); 89 } 90 } 91 getJcaDigestAlgorithmStandardNameFromEVP_MD(long evpMdRef)92 public static String getJcaDigestAlgorithmStandardNameFromEVP_MD(long evpMdRef) { 93 if (evpMdRef == MD5.EVP_MD) { 94 return MD5.JCA_NAME; 95 } else if (evpMdRef == SHA1.EVP_MD) { 96 return SHA1.JCA_NAME; 97 } else if (evpMdRef == SHA224.EVP_MD) { 98 return SHA224.JCA_NAME; 99 } else if (evpMdRef == SHA256.EVP_MD) { 100 return SHA256.JCA_NAME; 101 } else if (evpMdRef == SHA384.EVP_MD) { 102 return SHA384.JCA_NAME; 103 } else if (evpMdRef == SHA512.EVP_MD) { 104 return SHA512.JCA_NAME; 105 } else { 106 throw new IllegalArgumentException("Unknown EVP_MD reference"); 107 } 108 } 109 110 public static final class MD5 { 111 public static final String JCA_NAME = "MD5"; 112 public static final String OID = "1.2.840.113549.2.5"; 113 public static final long EVP_MD = NativeCrypto.EVP_get_digestbyname("md5"); 114 public static final int SIZE_BYTES = NativeCrypto.EVP_MD_size(EVP_MD); 115 MD5()116 private MD5() {} 117 } 118 119 public static final class SHA1 { 120 public static final String JCA_NAME = "SHA-1"; 121 public static final String OID = "1.3.14.3.2.26"; 122 public static final long EVP_MD = NativeCrypto.EVP_get_digestbyname("sha1"); 123 public static final int SIZE_BYTES = NativeCrypto.EVP_MD_size(EVP_MD); SHA1()124 private SHA1() {} 125 } 126 127 public static final class SHA224 { 128 public static final String JCA_NAME = "SHA-224"; 129 public static final String OID = "2.16.840.1.101.3.4.2.4"; 130 public static final long EVP_MD = NativeCrypto.EVP_get_digestbyname("sha224"); 131 public static final int SIZE_BYTES = NativeCrypto.EVP_MD_size(EVP_MD); 132 SHA224()133 private SHA224() {} 134 } 135 136 public static final class SHA256 { 137 public static final String JCA_NAME = "SHA-256"; 138 public static final String OID = "2.16.840.1.101.3.4.2.1"; 139 public static final long EVP_MD = NativeCrypto.EVP_get_digestbyname("sha256"); 140 public static final int SIZE_BYTES = NativeCrypto.EVP_MD_size(EVP_MD); 141 SHA256()142 private SHA256() {} 143 } 144 145 public static final class SHA384 { 146 public static final String JCA_NAME = "SHA-384"; 147 public static final String OID = "2.16.840.1.101.3.4.2.2"; 148 public static final long EVP_MD = NativeCrypto.EVP_get_digestbyname("sha384"); 149 public static final int SIZE_BYTES = NativeCrypto.EVP_MD_size(EVP_MD); 150 SHA384()151 private SHA384() {} 152 } 153 154 public static final class SHA512 { 155 public static final String JCA_NAME = "SHA-512"; 156 public static final String OID = "2.16.840.1.101.3.4.2.3"; 157 public static final long EVP_MD = NativeCrypto.EVP_get_digestbyname("sha512"); 158 public static final int SIZE_BYTES = NativeCrypto.EVP_MD_size(EVP_MD); 159 SHA512()160 private SHA512() {} 161 } 162 EvpMdRef()163 private EvpMdRef() {} 164 } 165