diff options
author | Mattias Nissler <mnissler@google.com> | 2016-06-14 08:38:18 +0000 |
---|---|---|
committer | android-build-merger <android-build-merger@google.com> | 2016-06-14 08:38:18 +0000 |
commit | 6ac66b5f88edc7c1a7d2df9fc4671602e2d6979d (patch) | |
tree | bfebb63a26aa04488d18ea409817cfc81f08c83d | |
parent | 66bd688711f6c7beee43bd9774b6792281bc509e (diff) | |
parent | 495f9ee73930daca9992db691f3b577bf1abed7e (diff) | |
download | nvram-6ac66b5f88edc7c1a7d2df9fc4671602e2d6979d.tar.gz |
Isolate crypto implementation details.
am: 495f9ee739
Change-Id: Ib218e4d5fcf4a2b0e0e4dfcc1c24c8795a63afe6
-rw-r--r-- | core/Android.mk | 8 | ||||
-rw-r--r-- | core/crypto.h | 45 | ||||
-rw-r--r-- | core/crypto_boringssl.cpp | 48 | ||||
-rw-r--r-- | core/nvram_manager.cpp | 36 | ||||
-rw-r--r-- | core/rules.mk | 2 | ||||
-rw-r--r-- | core/tests/Android.mk | 4 | ||||
-rw-r--r-- | hal/Android.mk | 9 |
7 files changed, 125 insertions, 27 deletions
diff --git a/core/Android.mk b/core/Android.mk index 7eefb48..4da770c 100644 --- a/core/Android.mk +++ b/core/Android.mk @@ -19,27 +19,27 @@ LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) LOCAL_MODULE := libnvram-core LOCAL_SRC_FILES := \ + crypto_boringssl.cpp \ nvram_manager.cpp \ persistence.cpp LOCAL_CFLAGS := -Wall -Werror -Wextra LOCAL_CLANG := true LOCAL_C_INCLUDES := $(LOCAL_PATH)/include LOCAL_EXPORT_C_INCLUDE_DIRS := $(LOCAL_PATH)/include -LOCAL_STATIC_LIBRARIES := libconstrainedcrypto -LOCAL_SHARED_LIBRARIES := libnvram-messages +LOCAL_SHARED_LIBRARIES := libnvram-messages libcrypto include $(BUILD_STATIC_LIBRARY) include $(CLEAR_VARS) LOCAL_MODULE := libnvram-core-host LOCAL_SRC_FILES := \ + crypto_boringssl.cpp \ nvram_manager.cpp \ persistence.cpp LOCAL_CFLAGS := -Wall -Werror -Wextra LOCAL_CLANG := true LOCAL_C_INCLUDES := $(LOCAL_PATH)/include LOCAL_EXPORT_C_INCLUDE_DIRS := $(LOCAL_PATH)/include -LOCAL_STATIC_LIBRARIES := libconstrainedcrypto -LOCAL_SHARED_LIBRARIES := libnvram-messages-host +LOCAL_SHARED_LIBRARIES := libnvram-messages-host libcrypto-host include $(BUILD_HOST_STATIC_LIBRARY) include $(call all-makefiles-under,$(LOCAL_PATH)) diff --git a/core/crypto.h b/core/crypto.h new file mode 100644 index 0000000..b97fb17 --- /dev/null +++ b/core/crypto.h @@ -0,0 +1,45 @@ +/* + * Copyright (C) 2016 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef NVRAM_CORE_CRYPTO_H_ +#define NVRAM_CORE_CRYPTO_H_ + +extern "C" { +#include <stddef.h> +#include <stdint.h> +} // extern "C" + +namespace nvram { +namespace crypto { + +// Size of a SHA-256 digest in bytes. +constexpr size_t kSHA256DigestSize = 32; + +// Computes the SHA-256 digest of the |data_size| input bytes stored at |data|. +// The digest is written to |digest|, which is a buffer of size |digest_size|. +// Note that |digest_size| doesn't have to match SHA-256's output size of 32 +// bytes. If it doesn't the digest is truncated or zero-padded as necessary. +// +// Returns true if the digest was computed successfully, false otherwise. +void SHA256(const uint8_t* data, + size_t data_size, + uint8_t* digest, + size_t digest_size); + +} // namespace crypto +} // namespave nvram + +#endif // NVRAM_CORE_CRYPTO_H_ diff --git a/core/crypto_boringssl.cpp b/core/crypto_boringssl.cpp new file mode 100644 index 0000000..23897b5 --- /dev/null +++ b/core/crypto_boringssl.cpp @@ -0,0 +1,48 @@ +/* + * Copyright (C) 2016 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "crypto.h" + +extern "C" { +#include <string.h> + +#include <openssl/mem.h> +#include <openssl/sha.h> +} // extern "C" + +namespace nvram { +namespace crypto { + +void SHA256(const uint8_t* data, + size_t data_size, + uint8_t* digest, + size_t digest_size) { + // SHA256 requires an output buffer of at least SHA256_DIGEST_LENGTH. + // |digest_size| might be less, so store the digest in a local buffer. + uint8_t buffer[SHA256_DIGEST_LENGTH]; + ::SHA256(data, data_size, buffer); + + // Copy the result to |digest|. + if (digest_size < sizeof(buffer)) { + memcpy(digest, buffer, digest_size); + } else { + memcpy(digest, buffer, sizeof(buffer)); + memset(digest + sizeof(buffer), 0, digest_size - sizeof(buffer)); + } +} + +} // namespace crypto +} // namespace nvram diff --git a/core/nvram_manager.cpp b/core/nvram_manager.cpp index d5aee05..a1eb9c1 100644 --- a/core/nvram_manager.cpp +++ b/core/nvram_manager.cpp @@ -21,9 +21,10 @@ extern "C" { #include <string.h> } // extern "C" -#include <constrainedcrypto/sha256.h> #include <nvram/core/logger.h> +#include "crypto.h" + using namespace nvram::storage; namespace nvram { @@ -211,8 +212,9 @@ nvram_result_t NvramManager::CreateSpace(const CreateSpaceRequest& request, return NV_RESULT_INVALID_PARAMETER; } if ((controls & (1 << NV_CONTROL_WRITE_EXTEND)) != 0 && - request.size != SHA256_DIGEST_SIZE) { - NVRAM_LOG_INFO("Write-extended space size must be %d.", SHA256_DIGEST_SIZE); + request.size != crypto::kSHA256DigestSize) { + NVRAM_LOG_INFO("Write-extended space size must be %zu.", + crypto::kSHA256DigestSize); return NV_RESULT_INVALID_PARAMETER; } @@ -385,22 +387,18 @@ nvram_result_t NvramManager::WriteSpace(const WriteSpaceRequest& request, Blob& contents = space_record.persistent.contents; if (space_record.persistent.HasControl(NV_CONTROL_WRITE_EXTEND)) { - // Compute the hash of existing contents concatenated with input. - SHA256_CTX sha256_context; - SHA256_init(&sha256_context); - SHA256_update(&sha256_context, contents.data(), contents.size()); - SHA256_update(&sha256_context, request.buffer.data(), - request.buffer.size()); - - // Make sure to handle both short and long space sizes gracefully, - // truncating or extending with 0 bytes as necessary. Even though - // |CreateSpace()| rejects |NV_CONTROL_WRITE_EXTEND| spaces that are not of - // size |SHA256_DIGEST_SIZE|, it's better to avoid any assumptions about - // data read from storage. - size_t hash_size = - min(contents.size(), static_cast<size_t>(SHA256_DIGEST_SIZE)); - memcpy(contents.data(), SHA256_final(&sha256_context), hash_size); - memset(contents.data() + hash_size, 0x0, contents.size() - hash_size); + // Concatenate the current space |contents| with the input data. + Blob sha256_input; + if (!sha256_input.Resize(contents.size() + request.buffer.size())) { + return NV_RESULT_INTERNAL_ERROR; + } + memcpy(sha256_input.data(), contents.data(), contents.size()); + memcpy(sha256_input.data() + contents.size(), request.buffer.data(), + request.buffer.size()); + + // Compute the SHA-256 digest and write it back to |contents|. + crypto::SHA256(sha256_input.data(), sha256_input.size(), contents.data(), + contents.size()); } else { if (contents.size() < request.buffer.size()) { return NV_RESULT_INVALID_PARAMETER; diff --git a/core/rules.mk b/core/rules.mk index c53a2e6..ed2f0a4 100644 --- a/core/rules.mk +++ b/core/rules.mk @@ -22,12 +22,14 @@ LOCAL_DIR := $(GET_LOCAL_DIR) MODULE := $(LOCAL_DIR) MODULE_SRCS += \ + $(LOCAL_DIR)/crypto_boringssl.cpp \ $(LOCAL_DIR)/nvram_manager.cpp \ $(LOCAL_DIR)/persistence.cpp MODULE_CPPFLAGS := -Wall -Werror -Wextra -std=c++11 MODULE_DEPS += \ + external/openssl \ lib/libc-trusty \ lib/libstdc++-trusty \ system/nvram/messages diff --git a/core/tests/Android.mk b/core/tests/Android.mk index ed956ba..003bfdf 100644 --- a/core/tests/Android.mk +++ b/core/tests/Android.mk @@ -24,6 +24,6 @@ LOCAL_SRC_FILES := \ nvram_manager_test.cpp LOCAL_CFLAGS := -Wall -Werror -Wextra -DHAS_GTEST LOCAL_CLANG := true -LOCAL_STATIC_LIBRARIES := libnvram-core-host libconstrainedcrypto -LOCAL_SHARED_LIBRARIES := libnvram-messages-host +LOCAL_STATIC_LIBRARIES := libnvram-core-host +LOCAL_SHARED_LIBRARIES := libnvram-messages-host libcrypto-host include $(BUILD_HOST_NATIVE_TEST) diff --git a/hal/Android.mk b/hal/Android.mk index b23c9a9..6f8a753 100644 --- a/hal/Android.mk +++ b/hal/Android.mk @@ -55,8 +55,13 @@ LOCAL_SRC_FILES := \ fake_nvram_storage.cpp LOCAL_CLANG := true LOCAL_CFLAGS := -Wall -Werror -Wextra -LOCAL_STATIC_LIBRARIES := libnvram-core libconstrainedcrypto -LOCAL_SHARED_LIBRARIES := libnvram-messages libminijail libcutils libbase +LOCAL_STATIC_LIBRARIES := libnvram-core +LOCAL_SHARED_LIBRARIES := \ + libnvram-messages \ + libcrypto \ + libminijail \ + libcutils \ + libbase LOCAL_INIT_RC := fake-nvram.rc LOCAL_REQUIRED_MODULES := fake-nvram-seccomp.policy LOCAL_MODULE_TAGS := optional |