1 /*
2 **
3 ** Copyright 2017, The Android Open Source Project
4 **
5 ** Licensed under the Apache License, Version 2.0 (the "License");
6 ** you may not use this file except in compliance with the License.
7 ** You may obtain a copy of the License at
8 **
9 **     http://www.apache.org/licenses/LICENSE-2.0
10 **
11 ** Unless required by applicable law or agreed to in writing, software
12 ** distributed under the License is distributed on an "AS IS" BASIS,
13 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 ** See the License for the specific language governing permissions and
15 ** limitations under the License.
16 */
17 
18 #include "PlatformSpecifics.h"
19 
20 #include <openssl/hmac.h>
21 #include <openssl/sha.h>
22 #include <time.h>
23 
24 namespace android {
25 namespace hardware {
26 namespace confirmationui {
27 namespace V1_0 {
28 namespace implementation {
29 
now()30 MonotonicClockTimeStamper::TimeStamp MonotonicClockTimeStamper::now() {
31     timespec ts;
32     if (!clock_gettime(CLOCK_BOOTTIME, &ts)) {
33         return TimeStamp(ts.tv_sec * UINT64_C(1000) + ts.tv_nsec / UINT64_C(1000000));
34     } else {
35         return {};
36     }
37 }
38 
hmac256(const support::auth_token_key_t & key,std::initializer_list<support::ByteBufferProxy> buffers)39 support::NullOr<support::hmac_t> HMacImplementation::hmac256(
40     const support::auth_token_key_t& key, std::initializer_list<support::ByteBufferProxy> buffers) {
41     HMAC_CTX hmacCtx;
42     HMAC_CTX_init(&hmacCtx);
43     if (!HMAC_Init_ex(&hmacCtx, key.data(), key.size(), EVP_sha256(), nullptr)) {
44         return {};
45     }
46     for (auto& buffer : buffers) {
47         if (!HMAC_Update(&hmacCtx, buffer.data(), buffer.size())) {
48             return {};
49         }
50     }
51     support::hmac_t result;
52     if (!HMAC_Final(&hmacCtx, result.data(), nullptr)) {
53         return {};
54     }
55     return result;
56 }
57 
58 }  // namespace implementation
59 }  // namespace V1_0
60 }  // namespace confirmationui
61 }  // namespace hardware
62 }  // namespace android
63