blob: 8bd466af69022a883bb81e10aac0dd5a181b078e [file] [log] [blame]
Thai Duong7689ed62015-03-20 16:50:18 -07001/*
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
Janis Danisevskisf54cc932017-05-10 15:29:10 -070017#include <keymaster/km_openssl/hkdf.h>
Shawn Willden0f906ec2015-06-20 09:16:30 -060018
19#include <keymaster/android_keymaster_utils.h>
Janis Danisevskisf54cc932017-05-10 15:29:10 -070020#include <keymaster/km_openssl/hmac.h>
Shawn Willden0f906ec2015-06-20 09:16:30 -060021
Thai Duong7689ed62015-03-20 16:50:18 -070022namespace keymaster {
23
Quan Nguyenf7538e02015-05-21 00:49:11 -070024bool Rfc5869Sha256Kdf::GenerateKey(const uint8_t* info, size_t info_len, uint8_t* output,
25 size_t output_len) {
Shawn Willden32873522020-12-14 22:29:46 -070026 if (!is_initialized_ || output == nullptr) return false;
Quan Nguyenf7538e02015-05-21 00:49:11 -070027 /**
28 * Step 1. Extract: PRK = HMAC-SHA256(actual_salt, secret)
29 * https://tools.ietf.org/html/rfc5869#section-2.2
30 */
Thai Duong7689ed62015-03-20 16:50:18 -070031 HmacSha256 prk_hmac;
Thai Duong60eebdc2015-03-25 17:20:24 -070032 bool result;
Quan Nguyenf7538e02015-05-21 00:49:11 -070033 if (salt_.get() != nullptr && salt_len_ > 0) {
34 result = prk_hmac.Init(salt_.get(), salt_len_);
Thai Duong60eebdc2015-03-25 17:20:24 -070035 } else {
Shawn Willden32873522020-12-14 22:29:46 -070036 UniquePtr<uint8_t[]> zeros(new (std::nothrow) uint8_t[digest_size_]);
37 if (zeros.get() == nullptr) return false;
Quan Nguyenf7538e02015-05-21 00:49:11 -070038 /* 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_);
Thai Duong60eebdc2015-03-25 17:20:24 -070041 }
Shawn Willden32873522020-12-14 22:29:46 -070042 if (!result) return false;
Thai Duong7689ed62015-03-20 16:50:18 -070043
Shawn Willden32873522020-12-14 22:29:46 -070044 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;
Quan Nguyenf7538e02015-05-21 00:49:11 -070046 result =
47 prk_hmac.Sign(secret_key_.get(), secret_key_len_, pseudo_random_key.get(), digest_size_);
Shawn Willden32873522020-12-14 22:29:46 -070048 if (!result) return false;
Thai Duong7689ed62015-03-20 16:50:18 -070049
Quan Nguyenf7538e02015-05-21 00:49:11 -070050 /**
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_;
Shawn Willden32873522020-12-14 22:29:46 -070055 if (num_blocks >= 256u) return false;
Thai Duong7689ed62015-03-20 16:50:18 -070056
Shawn Willden32873522020-12-14 22:29:46 -070057 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;
Thai Duong7689ed62015-03-20 16:50:18 -070060 HmacSha256 hmac;
Quan Nguyenf7538e02015-05-21 00:49:11 -070061 result = hmac.Init(pseudo_random_key.get(), digest_size_);
Shawn Willden32873522020-12-14 22:29:46 -070062 if (!result) return false;
Thai Duong7689ed62015-03-20 16:50:18 -070063
Quan Nguyenf7538e02015-05-21 00:49:11 -070064 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_;
Thai Duong60eebdc2015-03-25 17:20:24 -070069 }
Shawn Willden32873522020-12-14 22:29:46 -070070 if (info != nullptr && info_len > 0) memcpy(buf.get() + block_input_len, info, info_len);
Quan Nguyenf7538e02015-05-21 00:49:11 -070071 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_);
Shawn Willden32873522020-12-14 22:29:46 -070074 if (!result) return false;
Quan Nguyenf7538e02015-05-21 00:49:11 -070075 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);
Thai Duong7689ed62015-03-20 16:50:18 -070079 }
Thai Duongfabacaf2015-03-25 20:14:57 -070080 return true;
Thai Duong7689ed62015-03-20 16:50:18 -070081}
82
83} // namespace keymaster