1 /*
2 * Copyright (C) 2015 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 #include <cpu-features.h>
18 #include <cutils/log.h>
19 #include <cutils/properties.h>
20 #include <jni.h>
21 #include <nativehelper/JNIHelp.h>
22 #include <openssl/aes.h>
23 #include <openssl/cpu.h>
24 #include <openssl/evp.h>
25 #include <stdint.h>
26 #include <string.h>
27 #include <sys/vfs.h>
28 #include <time.h>
29 #include <new>
30
31 #define TEST_EVP_CIPHER EVP_aes_256_cbc()
32 #define TEST_BUFSIZE (1 * 1024 * 1024) /* 1 MiB */
33 #define TEST_ITERATIONS 100 /* MiB */
34 #define TEST_THRESHOLD 2000 /* ms */
35
ns()36 static inline uint64_t ns()
37 {
38 struct timespec ts;
39 clock_gettime(CLOCK_MONOTONIC, &ts);
40 return (uint64_t)ts.tv_sec * 1000000000 + ts.tv_nsec;
41 }
42
43 /*
44 * Test if AES performance is sufficient to require encryption
45 */
android_security_cts_EncryptionTest_aesIsFast(JNIEnv * env,jobject)46 static jboolean android_security_cts_EncryptionTest_aesIsFast(JNIEnv *env, jobject)
47 {
48 EVP_CIPHER_CTX ctx;
49 uint8_t *buf;
50 uint8_t key[EVP_CIPHER_key_length(TEST_EVP_CIPHER)];
51 uint8_t iv[EVP_CIPHER_iv_length(TEST_EVP_CIPHER)];
52
53 memset(key, 0x42, sizeof(key));
54 memset(iv, 0x11, sizeof(iv));
55
56 EVP_CIPHER_CTX_init(&ctx);
57
58 if (!EVP_DecryptInit(&ctx, TEST_EVP_CIPHER, key, iv)) {
59 jniThrowException(env, "java/security/InvalidKeyException",
60 "EVP_DecryptInit failed");
61 return false;
62 }
63
64 buf = new (std::nothrow) uint8_t[TEST_BUFSIZE +
65 EVP_CIPHER_block_size(TEST_EVP_CIPHER)];
66
67 if (!buf) {
68 jniThrowException(env, "java/lang/OutOfMemoryError",
69 "Failed to allocate test buffer");
70 return false;
71 }
72
73 memset(buf, 0xF0, TEST_BUFSIZE);
74
75 int len;
76 uint64_t t = ns();
77
78 for (int i = 0; i < TEST_ITERATIONS; ++i) {
79 EVP_DecryptUpdate(&ctx, buf, &len, buf, TEST_BUFSIZE);
80 }
81
82 t = ns() - t;
83
84 delete[] buf;
85
86 unsigned long ms = (unsigned long)(t / 1000000);
87 double speed =
88 (double)(TEST_ITERATIONS * TEST_BUFSIZE / (1024 * 1024)) * 1000.0 / ms;
89
90 ALOGE("EncryptionTest::aesIsFast: %u iterations in %lu ms (%.01lf MiB/s) "
91 "(threshold %u ms)", TEST_ITERATIONS, ms, speed, TEST_THRESHOLD);
92
93 return ms < TEST_THRESHOLD;
94 }
95
96 static JNINativeMethod gMethods[] = {
97 { "aesIsFast", "()Z",
98 (void *) android_security_cts_EncryptionTest_aesIsFast }
99 };
100
register_android_security_cts_EncryptionTest(JNIEnv * env)101 int register_android_security_cts_EncryptionTest(JNIEnv* env)
102 {
103 jclass clazz = env->FindClass("android/security/cts/EncryptionTest");
104 return env->RegisterNatives(clazz, gMethods,
105 sizeof(gMethods) / sizeof(JNINativeMethod));
106 }
107