1 /*
2  * Copyright 2015 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 #ifndef SYSTEM_KEYMASTER_KDF_H_
18 #define SYSTEM_KEYMASTER_KDF_H_
19 
20 #include <hardware/keymaster_defs.h>
21 #include <keymaster/android_keymaster_utils.h>
22 #include <keymaster/serializable.h>
23 
24 #include <UniquePtr.h>
25 
26 namespace keymaster {
27 
28 /**
29  * A base class for wrapping different key derivation functions.
30  */
31 class Kdf {
32   public:
~Kdf()33     virtual ~Kdf() { memset_s(secret_key_.get(), 0, secret_key_len_); };
34     Kdf();
35     bool Init(keymaster_digest_t digest_type, const uint8_t* secret, size_t secret_len,
36               const uint8_t* salt, size_t salt_len);
37     virtual bool GenerateKey(const uint8_t* info, size_t info_len, uint8_t* output,
38                              size_t output_len) = 0;
39 
40   protected:
41     bool Uint32ToBigEndianByteArray(uint32_t number, uint8_t* output);
42     UniquePtr<uint8_t[]> secret_key_;
43     size_t secret_key_len_;
44     UniquePtr<uint8_t[]> salt_;
45     size_t salt_len_;
46     bool is_initialized_;
47     keymaster_digest_t digest_type_;
48     size_t digest_size_;
49 };
50 
51 }  // namespace keymaster
52 
53 #endif  // SYSTEM_KEYMASTER_KDF_H_
54