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 #include <keymaster/km_openssl/hkdf.h>
18
19 #include <keymaster/android_keymaster_utils.h>
20 #include <keymaster/km_openssl/hmac.h>
21
22 namespace keymaster {
23
GenerateKey(const uint8_t * info,size_t info_len,uint8_t * output,size_t output_len)24 bool Rfc5869Sha256Kdf::GenerateKey(const uint8_t* info, size_t info_len, uint8_t* output,
25 size_t output_len) {
26 if (!is_initialized_ || output == nullptr) return false;
27 /**
28 * Step 1. Extract: PRK = HMAC-SHA256(actual_salt, secret)
29 * https://tools.ietf.org/html/rfc5869#section-2.2
30 */
31 HmacSha256 prk_hmac;
32 bool result;
33 if (salt_.get() != nullptr && salt_len_ > 0) {
34 result = prk_hmac.Init(salt_.get(), salt_len_);
35 } else {
36 UniquePtr<uint8_t[]> zeros(new (std::nothrow) uint8_t[digest_size_]);
37 if (zeros.get() == nullptr) return false;
38 /* If salt is not given, digest size of zeros are used. */
39 memset(zeros.get(), 0, digest_size_);
40 result = prk_hmac.Init(zeros.get(), digest_size_);
41 }
42 if (!result) return false;
43
44 UniquePtr<uint8_t[]> pseudo_random_key(new (std::nothrow) uint8_t[digest_size_]);
45 if (pseudo_random_key.get() == nullptr || digest_size_ != prk_hmac.DigestLength()) return false;
46 result =
47 prk_hmac.Sign(secret_key_.get(), secret_key_len_, pseudo_random_key.get(), digest_size_);
48 if (!result) return false;
49
50 /**
51 * Step 2. Expand: OUTPUT = HKDF-Expand(PRK, info)
52 * https://tools.ietf.org/html/rfc5869#section-2.3
53 */
54 const size_t num_blocks = (output_len + digest_size_ - 1) / digest_size_;
55 if (num_blocks >= 256u) return false;
56
57 UniquePtr<uint8_t[]> buf(new (std::nothrow) uint8_t[digest_size_ + info_len + 1]);
58 UniquePtr<uint8_t[]> digest(new (std::nothrow) uint8_t[digest_size_]);
59 if (buf.get() == nullptr || digest.get() == nullptr) return false;
60 HmacSha256 hmac;
61 result = hmac.Init(pseudo_random_key.get(), digest_size_);
62 if (!result) return false;
63
64 for (size_t i = 0; i < num_blocks; i++) {
65 size_t block_input_len = 0;
66 if (i != 0) {
67 memcpy(buf.get(), digest.get(), digest_size_);
68 block_input_len = digest_size_;
69 }
70 if (info != nullptr && info_len > 0) memcpy(buf.get() + block_input_len, info, info_len);
71 block_input_len += info_len;
72 *(buf.get() + block_input_len++) = static_cast<uint8_t>(i + 1);
73 result = hmac.Sign(buf.get(), block_input_len, digest.get(), digest_size_);
74 if (!result) return false;
75 size_t block_output_len = digest_size_ < output_len - i * digest_size_
76 ? digest_size_
77 : output_len - i * digest_size_;
78 memcpy(output + i * digest_size_, digest.get(), block_output_len);
79 }
80 return true;
81 }
82
83 } // namespace keymaster
84