summaryrefslogtreecommitdiff
path: root/verity
diff options
context:
space:
mode:
authorTianjie Xu <xunchang@google.com>2018-07-23 15:11:23 -0700
committerTianjie Xu <xunchang@google.com>2018-07-25 00:36:11 -0700
commit64edcd872e0ea4d3afb63aa1672adfdf278cc994 (patch)
tree64c68e1d6b6fb2e2bd43e3049020567c3e799148 /verity
parent148a756acd1234de415c76de58066e26938c3295 (diff)
downloadextras-64edcd872e0ea4d3afb63aa1672adfdf278cc994.tar.gz
Export headers for libverity_tree
Also factor out a function to convert a hex string to the byte array. Bug: 25170618 Test: unit tests pass Change-Id: Id9252d58d8de065bce332e806e2302dd18a54a8c
Diffstat (limited to 'verity')
-rw-r--r--verity/Android.bp1
-rw-r--r--verity/build_verity_tree.cpp2
-rw-r--r--verity/build_verity_tree_main.cpp17
-rw-r--r--verity/build_verity_tree_test.cpp2
-rw-r--r--verity/hash_tree_builder.cpp27
-rw-r--r--verity/include/verity/build_verity_tree.h (renamed from verity/build_verity_tree.h)0
-rw-r--r--verity/include/verity/hash_tree_builder.h (renamed from verity/hash_tree_builder.h)5
7 files changed, 37 insertions, 17 deletions
diff --git a/verity/Android.bp b/verity/Android.bp
index d1d81fac..526e7f27 100644
--- a/verity/Android.bp
+++ b/verity/Android.bp
@@ -92,6 +92,7 @@ cc_library_static {
"verity_tree_defaults",
],
+ export_include_dirs: ["include"],
srcs: [
"build_verity_tree.cpp",
"build_verity_tree_utils.cpp",
diff --git a/verity/build_verity_tree.cpp b/verity/build_verity_tree.cpp
index 27f9255f..9edc81a1 100644
--- a/verity/build_verity_tree.cpp
+++ b/verity/build_verity_tree.cpp
@@ -14,7 +14,7 @@
* limitations under the License.
*/
-#include "build_verity_tree.h"
+#include "verity/build_verity_tree.h"
#include <android-base/logging.h>
#include <android-base/unique_fd.h>
diff --git a/verity/build_verity_tree_main.cpp b/verity/build_verity_tree_main.cpp
index 76164cc5..ca693610 100644
--- a/verity/build_verity_tree_main.cpp
+++ b/verity/build_verity_tree_main.cpp
@@ -29,9 +29,8 @@
#include <android-base/logging.h>
#include <android-base/parseint.h>
#include <android-base/unique_fd.h>
-#include <openssl/bn.h>
-#include "build_verity_tree.h"
+#include "verity/build_verity_tree.h"
static void usage(void) {
printf(
@@ -75,19 +74,11 @@ int main(int argc, char** argv) {
salt.clear();
salt.insert(salt.end(), optarg, &optarg[strlen(optarg)]);
break;
- case 'A': {
- BIGNUM* bn = nullptr;
- if (!BN_hex2bn(&bn, optarg)) {
- LOG(ERROR) << "Failed to convert salt from hex";
+ case 'A':
+ if (!HashTreeBuilder::ParseBytesArrayFromString(optarg, &salt)) {
return 1;
}
- size_t salt_size = BN_num_bytes(bn);
- salt.resize(salt_size);
- if (BN_bn2bin(bn, salt.data()) != salt_size) {
- LOG(ERROR) << "Failed to convert salt to bytes";
- return 1;
- }
- } break;
+ break;
case 'h':
usage();
return 1;
diff --git a/verity/build_verity_tree_test.cpp b/verity/build_verity_tree_test.cpp
index 50f6e397..c1cb6a39 100644
--- a/verity/build_verity_tree_test.cpp
+++ b/verity/build_verity_tree_test.cpp
@@ -25,7 +25,7 @@
#include <openssl/evp.h>
#include "build_verity_tree_utils.h"
-#include "hash_tree_builder.h"
+#include "verity/hash_tree_builder.h"
// The hex string we are using in build_image.py
// aee087a5be3b982978c923f566a94613496b417f2af592639bc80d141e34dfe7
diff --git a/verity/hash_tree_builder.cpp b/verity/hash_tree_builder.cpp
index 41649900..27909255 100644
--- a/verity/hash_tree_builder.cpp
+++ b/verity/hash_tree_builder.cpp
@@ -14,15 +14,17 @@
* limitations under the License.
*/
-#include "hash_tree_builder.h"
+#include "verity/hash_tree_builder.h"
#include <algorithm>
+#include <memory>
#include <android-base/file.h>
#include <android-base/logging.h>
#include <android-base/stringprintf.h>
#include <android-base/strings.h>
#include <android-base/unique_fd.h>
+#include <openssl/bn.h>
#include "build_verity_tree_utils.h"
@@ -67,6 +69,29 @@ std::string HashTreeBuilder::BytesArrayToString(
return result;
}
+bool HashTreeBuilder::ParseBytesArrayFromString(
+ const std::string& hex_string, std::vector<unsigned char>* bytes) {
+ if (hex_string.size() % 2 != 0) {
+ LOG(ERROR) << "Hex string size must be even number " << hex_string;
+ return false;
+ }
+
+ BIGNUM* bn = nullptr;
+ if (!BN_hex2bn(&bn, hex_string.c_str())) {
+ LOG(ERROR) << "Failed to parse hex in " << hex_string;
+ return false;
+ }
+ std::unique_ptr<BIGNUM, decltype(&BN_free)> guard(bn, BN_free);
+
+ size_t bytes_size = BN_num_bytes(bn);
+ bytes->resize(bytes_size);
+ if (BN_bn2bin(bn, bytes->data()) != bytes_size) {
+ LOG(ERROR) << "Failed to convert hex to bytes " << hex_string;
+ return false;
+ }
+ return true;
+}
+
uint64_t HashTreeBuilder::CalculateSize(uint64_t input_size) const {
uint64_t verity_blocks = 0;
size_t level_blocks;
diff --git a/verity/build_verity_tree.h b/verity/include/verity/build_verity_tree.h
index 9ca40005..9ca40005 100644
--- a/verity/build_verity_tree.h
+++ b/verity/include/verity/build_verity_tree.h
diff --git a/verity/hash_tree_builder.h b/verity/include/verity/hash_tree_builder.h
index 2a1ee56c..d933b488 100644
--- a/verity/hash_tree_builder.h
+++ b/verity/include/verity/hash_tree_builder.h
@@ -55,7 +55,10 @@ class HashTreeBuilder {
// Converts |bytes| to string for hexdump.
static std::string BytesArrayToString(
const std::vector<unsigned char>& bytes);
-
+ // Inverse of the above function. It parses the input hex string and stores
+ // the result in |bytes|.
+ static bool ParseBytesArrayFromString(const std::string& str,
+ std::vector<unsigned char>* bytes);
// Returns the hash function given the name of the hash algorithm. Returns
// nullptr if the algorithm is unrecongnized or not supported.
static const EVP_MD* HashFunction(const std::string& hash_name);