• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2020 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 <crypto_utils/android_pubkey.h>
18 #include <fuzzer/FuzzedDataProvider.h>
19 #include <string.h>
20 #include <memory>
21 #include <openssl/obj_mac.h>
22 #include <openssl/rsa.h>
23 #include <cstdio>
24 #include <limits>
25 
26 #define ANDROID_PUBKEY_MODULUS_SIZE_WORDS (ANDROID_PUBKEY_MODULUS_SIZE / 4)
27 
LLVMFuzzerTestOneInput(const uint8_t * data,std::size_t size)28 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, std::size_t size) {
29     if (size < 2050) {
30         return 0;
31     }
32 
33     FuzzedDataProvider fdp(data, size);
34 
35     uint8_t buffer[ANDROID_PUBKEY_ENCODED_SIZE];
36     uint32_t modulus_size_words = ANDROID_PUBKEY_MODULUS_SIZE_WORDS;
37     memcpy(buffer, &modulus_size_words, sizeof(uint32_t));
38 
39     uint32_t n0inv = fdp.ConsumeIntegralInRange<uint32_t>(0,std::numeric_limits<uint32_t>::max());
40     memcpy(buffer+sizeof(uint32_t), &n0inv, sizeof(uint32_t));
41 
42     std::string s = fdp.ConsumeBytesAsString(ANDROID_PUBKEY_MODULUS_SIZE);
43     uint8_t* modulus = (uint8_t*)s.c_str();
44     memcpy(buffer+sizeof(uint32_t)*2, modulus,
45            sizeof(uint8_t)*ANDROID_PUBKEY_MODULUS_SIZE);
46 
47     std::string ss = fdp.ConsumeBytesAsString(ANDROID_PUBKEY_MODULUS_SIZE);
48     uint8_t* rr = (uint8_t*)ss.c_str();
49     memcpy(buffer+sizeof(uint32_t)*2+ANDROID_PUBKEY_MODULUS_SIZE, rr,
50            sizeof(uint8_t)*ANDROID_PUBKEY_MODULUS_SIZE);
51 
52     int flip = fdp.ConsumeIntegralInRange<uint32_t>(0,1);
53     buffer[ANDROID_PUBKEY_ENCODED_SIZE-1] = (uint8_t)(flip == 0 ? 3 : 65537);
54 
55     RSA* new_key = nullptr;
56     android_pubkey_decode(buffer, sizeof(uint8_t)*ANDROID_PUBKEY_ENCODED_SIZE, &new_key);
57 
58     uint8_t key_data[ANDROID_PUBKEY_ENCODED_SIZE];
59     android_pubkey_encode(new_key, key_data, sizeof(key_data));
60 
61     assert(0 == memcmp(buffer, key_data, sizeof(buffer)));
62 
63     RSA_free(new_key);
64 
65     return 0;
66 }
67