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
17package libcore.java.security;
18
19import static java.nio.charset.StandardCharsets.UTF_8;
20
21import java.io.BufferedReader;
22import java.io.FileReader;
23import java.io.IOException;
24import java.io.InputStreamReader;
25import java.lang.reflect.InvocationTargetException;
26import java.lang.reflect.Method;
27import java.util.Arrays;
28import java.util.List;
29import java.util.regex.Matcher;
30import java.util.regex.Pattern;
31
32public class CpuFeatures {
33    private CpuFeatures() {}
34
35    public static boolean isAESHardwareAccelerated() {
36        List<String> features = getListFromCpuinfo("Features");
37        if (features != null && features.contains("aes")) {
38            return true;
39        }
40
41        List<String> flags = getListFromCpuinfo("flags");
42        if (flags != null && flags.contains("aes")) {
43            return true;
44        }
45
46        features = getCpuFeaturesMac();
47        if (features != null && features.contains("aes")) {
48            return true;
49        }
50
51        // If we're in an emulated ABI, Conscrypt's NativeCrypto might bridge to
52        // a library that has accelerated AES instructions. See if Conscrypt
53        // detects that condition.
54        Class<?> nativeCrypto = findNativeCrypto();
55        if (nativeCrypto != null) {
56            try {
57                Method EVP_has_aes_hardware =
58                        nativeCrypto.getDeclaredMethod("EVP_has_aes_hardware");
59                EVP_has_aes_hardware.setAccessible(true);
60                return ((Integer) EVP_has_aes_hardware.invoke(null)) == 1;
61            } catch (NoSuchMethodException ignored) {
62            } catch (SecurityException ignored) {
63            } catch (IllegalAccessException ignored) {
64            } catch (IllegalArgumentException ignored) {
65            } catch (InvocationTargetException e) {
66                throw new IllegalArgumentException(e);
67            }
68        }
69
70        return false;
71    }
72
73    private static Class<?> findNativeCrypto() {
74        for (String packageName : new String[]{"com.android.org.conscrypt", "org.conscrypt"}) {
75            String name = packageName + ".NativeCrypto";
76            try {
77                return Class.forName(name);
78            } catch (ClassNotFoundException e) {
79                // Try the next one.
80            }
81        }
82        return null;
83    }
84
85    private static String getFieldFromCpuinfo(String field) {
86        try {
87            @SuppressWarnings("DefaultCharset")
88            BufferedReader br = new BufferedReader(new FileReader("/proc/cpuinfo"));
89            Pattern p = Pattern.compile(field + "\\s*:\\s*(.*)");
90
91            try {
92                String line;
93                while ((line = br.readLine()) != null) {
94                    Matcher m = p.matcher(line);
95                    if (m.matches()) {
96                        return m.group(1);
97                    }
98                }
99            } finally {
100                br.close();
101            }
102        } catch (IOException ignored) {
103            // Ignored.
104        }
105
106        return null;
107    }
108
109    private static List<String> getListFromCpuinfo(String fieldName) {
110        String features = getFieldFromCpuinfo(fieldName);
111        if (features == null)
112            return null;
113
114        return Arrays.asList(features.split("\\s"));
115    }
116
117    private static List<String> getCpuFeaturesMac() {
118        try {
119            StringBuilder output = new StringBuilder();
120            Process proc = Runtime.getRuntime().exec("sysctl -a");
121            if (proc.waitFor() == 0) {
122                BufferedReader reader =
123                        new BufferedReader(new InputStreamReader(proc.getInputStream(), UTF_8));
124
125                final String linePrefix = "machdep.cpu.features:";
126
127                String line;
128                while ((line = reader.readLine()) != null) {
129                    line = line.toLowerCase();
130                    if (line.startsWith(linePrefix)) {
131                        // Strip the line prefix from the results.
132                        output.append(line.substring(linePrefix.length())).append(' ');
133                    }
134                }
135                if (output.length() > 0) {
136                    String outputString = output.toString();
137                    String[] parts = outputString.split("\\s+");
138                    return Arrays.asList(parts);
139                }
140            }
141        } catch (Exception ignored) {
142            // Ignored.
143        }
144
145        return null;
146    }
147}
148