1 /* 2 * Copyright (C) 2021 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 #pragma once 18 19 #include <optional> 20 #include <string> 21 #include <vector> 22 23 #include <fs_avb/fs_avb_util.h> 24 25 uint8_t HexDigitToByte(char c); 26 27 bool HexToBytes(const std::string &hex, std::vector<uint8_t> *bytes); 28 29 std::string BytesToHex(const std::vector<uint8_t> &bytes); 30 31 // The abstract class of SHA algorithms. 32 class ShaHasher { 33 protected: 34 const uint32_t digest_size_; 35 ShaHasher(uint32_t digest_size)36 ShaHasher(uint32_t digest_size) : digest_size_(digest_size) {} 37 38 public: ~ShaHasher()39 virtual ~ShaHasher() {} 40 GetDigestSize()41 uint32_t GetDigestSize() const { return digest_size_; } 42 43 virtual bool CalculateDigest(const void *buffer, size_t size, 44 const void *salt, uint32_t block_length, 45 uint8_t *digest) const = 0; 46 }; 47 48 template <typename CTX_TYPE> 49 class ShaHasherImpl : public ShaHasher { 50 private: 51 typedef int (*InitFunc)(CTX_TYPE *); 52 typedef int (*UpdateFunc)(CTX_TYPE *sha, const void *data, size_t len); 53 typedef int (*FinalFunc)(uint8_t *md, CTX_TYPE *sha); 54 55 const InitFunc init_func_; 56 const UpdateFunc update_func_; 57 const FinalFunc final_func_; 58 59 public: ShaHasherImpl(InitFunc init_func,UpdateFunc update_func,FinalFunc final_func,uint32_t digest_size)60 ShaHasherImpl(InitFunc init_func, UpdateFunc update_func, 61 FinalFunc final_func, uint32_t digest_size) 62 : ShaHasher(digest_size), 63 init_func_(init_func), 64 update_func_(update_func), 65 final_func_(final_func) {} 66 ~ShaHasherImpl()67 ~ShaHasherImpl() {} 68 CalculateDigest(const void * buffer,size_t size,const void * salt,uint32_t salt_length,uint8_t * digest)69 bool CalculateDigest(const void *buffer, size_t size, const void *salt, 70 uint32_t salt_length, uint8_t *digest) const { 71 CTX_TYPE ctx; 72 if (init_func_(&ctx) != 1) { 73 return false; 74 } 75 if (update_func_(&ctx, salt, salt_length) != 1) { 76 return false; 77 } 78 if (update_func_(&ctx, buffer, size) != 1) { 79 return false; 80 } 81 if (final_func_(digest, &ctx) != 1) { 82 return false; 83 } 84 return true; 85 } 86 }; 87 88 // Creates a hasher with the parameters corresponding to the algorithm name. 89 std::unique_ptr<ShaHasher> CreateShaHasher(const std::string &algorithm); 90 91 // Checks whether the public key is an official GSI key or not. 92 bool ValidatePublicKeyBlob(const std::string &key_blob_to_validate); 93 94 uint32_t GetSdkLevel(); 95 96 uint32_t GetProductFirstApiLevel(); 97 98 uint32_t GetVendorApiLevel(); 99 100 // Return board API level on GRF devices. 101 // On non-GRF, this function will return std::nullopt. 102 std::optional<uint32_t> GetBoardApiLevel(); 103 104 bool IsReleasedAndroidVersion(); 105 106 bool IsAutomotiveDevice(); 107 108 bool IsTvDevice(); 109 110 bool IsGoDevice(); 111