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