• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <string>
20 #include <vector>
21 
22 #include <fs_avb/fs_avb_util.h>
23 
24 uint8_t HexDigitToByte(char c);
25 
26 bool HexToBytes(const std::string &hex, std::vector<uint8_t> *bytes);
27 
28 // The abstract class of SHA algorithms.
29 class ShaHasher {
30  protected:
31   const uint32_t digest_size_;
32 
ShaHasher(uint32_t digest_size)33   ShaHasher(uint32_t digest_size) : digest_size_(digest_size) {}
34 
35  public:
~ShaHasher()36   virtual ~ShaHasher() {}
37 
GetDigestSize()38   uint32_t GetDigestSize() const { return digest_size_; }
39 
40   virtual bool CalculateDigest(const void *buffer, size_t size,
41                                const void *salt, uint32_t block_length,
42                                uint8_t *digest) const = 0;
43 };
44 
45 template <typename CTX_TYPE>
46 class ShaHasherImpl : public ShaHasher {
47  private:
48   typedef int (*InitFunc)(CTX_TYPE *);
49   typedef int (*UpdateFunc)(CTX_TYPE *sha, const void *data, size_t len);
50   typedef int (*FinalFunc)(uint8_t *md, CTX_TYPE *sha);
51 
52   const InitFunc init_func_;
53   const UpdateFunc update_func_;
54   const FinalFunc final_func_;
55 
56  public:
ShaHasherImpl(InitFunc init_func,UpdateFunc update_func,FinalFunc final_func,uint32_t digest_size)57   ShaHasherImpl(InitFunc init_func, UpdateFunc update_func,
58                 FinalFunc final_func, uint32_t digest_size)
59       : ShaHasher(digest_size),
60         init_func_(init_func),
61         update_func_(update_func),
62         final_func_(final_func) {}
63 
~ShaHasherImpl()64   ~ShaHasherImpl() {}
65 
CalculateDigest(const void * buffer,size_t size,const void * salt,uint32_t salt_length,uint8_t * digest)66   bool CalculateDigest(const void *buffer, size_t size, const void *salt,
67                        uint32_t salt_length, uint8_t *digest) const {
68     CTX_TYPE ctx;
69     if (init_func_(&ctx) != 1) {
70       return false;
71     }
72     if (update_func_(&ctx, salt, salt_length) != 1) {
73       return false;
74     }
75     if (update_func_(&ctx, buffer, size) != 1) {
76       return false;
77     }
78     if (final_func_(digest, &ctx) != 1) {
79       return false;
80     }
81     return true;
82   }
83 };
84 
85 // Creates a hasher with the parameters corresponding to the algorithm name.
86 std::unique_ptr<ShaHasher> CreateShaHasher(const std::string &algorithm);
87 
88 // Checks whether the public key is an official GSI key or not.
89 bool ValidatePublicKeyBlob(const std::string &key_blob_to_validate);
90 
91 uint32_t GetProductFirstApiLevel();
92 
93 uint32_t GetBoardApiLevel();
94