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