• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #ifndef STORAGE_DAEMON_CRYPTO_KEY_UTILS_H
16 #define STORAGE_DAEMON_CRYPTO_KEY_UTILS_H
17 
18 #include <string>
19 #include <memory>
20 
21 #include "securec.h"
22 #include "hks_type.h"
23 
24 namespace OHOS {
25 namespace StorageDaemon {
26 constexpr uint32_t CRYPTO_KEY_SECDISC_SIZE = 16384;
27 constexpr uint32_t CRYPTO_KEY_ALIAS_SIZE = 16;
28 constexpr uint32_t CRYPTO_AES_256_LEN = 256;
29 constexpr uint32_t CRYPTO_AES_AAD_LEN = 16;
30 constexpr uint32_t CRYPTO_AES_256_XTS_KEY_SIZE = 64;
31 static const std::string CRYPTO_NAME_PREFIXES[] = {"ext4", "f2fs", "fscrypt"};
32 
33 struct KeyBlob {
AllocKeyBlob34     bool Alloc(uint32_t len)
35     {
36         if (len > CRYPTO_KEY_SECDISC_SIZE) {
37             return false;
38         }
39         data = std::make_unique<uint8_t[]>(len);
40         size = len;
41         (void)memset_s(data.get(), size, 0, size);
42         return true;
43     }
ClearKeyBlob44     void Clear()
45     {
46         if (data != nullptr && size != 0) {
47             (void)memset_s(data.get(), size, 0, size);
48         }
49         size = 0;
50         data.reset(nullptr);
51     }
IsEmptyKeyBlob52     bool IsEmpty() const
53     {
54         return size == 0 || data.get() == nullptr;
55     }
ToStringKeyBlob56     std::string ToString() const
57     {
58         std::string hex;
59         const char *hexMap = "0123456789abcdef";
60         static_assert(sizeof(data[0]) == sizeof(char));
61         for (size_t i = 0; i < size; i++) {
62             hex = hex + hexMap[(data[i] & 0xF0) >> 4] + hexMap[data[i] & 0x0F]; // higher 4 bits
63         }
64         return hex;
65     }
ToHksBlobKeyBlob66     HksBlob ToHksBlob() const
67     {
68         return {size, data.get()};
69     }
70     uint32_t size { 0 };
71     std::unique_ptr<uint8_t[]> data { nullptr };
72 };
73 
74 struct KeyInfo {
75     uint8_t version { 0 };
76     KeyBlob key;
77     // the legacy interface use key_spec.u.descriptor
78     KeyBlob keyDesc;
79     // the v2 interface use the key_spec.u.identifier
80     KeyBlob keyId;
81 };
82 
83 struct KeyContext {
84     KeyBlob secDiscard;
85     KeyBlob alias;
86     KeyBlob encrypted;
87     KeyBlob nonce;
88     KeyBlob aad;
89 };
90 
91 struct UserAuth {
92     std::string token;
93     // synthetic
94 };
95 } // namespace StorageDaemon
96 } // namespace OHOS
97 
98 #endif // STORAGE_DAEMON_CRYPTO_KEY_UTILS_H
99