• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_HKDF_H_
18 #define SYSTEM_KEYMASTER_HKDF_H_
19 
20 #include <hardware/keymaster_defs.h>
21 #include <keymaster/serializable.h>
22 
23 #include <UniquePtr.h>
24 
25 namespace keymaster {
26 
27 // Rfc5869HmacSha256Kdf implements the key derivation function specified in RFC 5869 (using
28 // SHA256) and outputs key material, as needed by ECIES.
29 // See https://tools.ietf.org/html/rfc5869 for details.
30 class Rfc5869HmacSha256Kdf {
31   public:
32     // |secret|: the input shared secret (or, from RFC 5869, the IKM).
33     // |salt|: an (optional) public salt / non-secret random value. While
34     // optional, callers are strongly recommended to provide a salt. There is no
35     // added security value in making this larger than the SHA-256 block size of
36     // 64 bytes.
37     // |info|: an (optional) label to distinguish different uses of HKDF. It is
38     // optional context and application specific information (can be a zero-length
39     // string).
40     // |key_bytes_to_generate|: the number of bytes of key material to generate.
41     Rfc5869HmacSha256Kdf(Buffer& secret, Buffer& salt, Buffer& info, size_t key_bytes_to_generate,
42                          keymaster_error_t* error);
43 
44     Rfc5869HmacSha256Kdf(const uint8_t* secret, size_t secret_len, const uint8_t* salt,
45                          size_t salt_len, const uint8_t* info, size_t info_len,
46                          size_t key_bytes_to_generate, keymaster_error_t* error);
47 
secret_key(Buffer * buf)48     bool secret_key(Buffer* buf) const {
49         return buf->Reinitialize(secret_key_.get(), secret_key_len_);
50     };
51 
52   private:
53     UniquePtr<uint8_t[]> output_;
54     UniquePtr<uint8_t[]> secret_key_;
55     size_t secret_key_len_;
56 };
57 
58 }  // namespace keymaster
59 
60 #endif  // SYSTEM_KEYMASTER_HKDF_H_
61