blob: 9727375e48993fbf72c87b66dbf68c58a807f96d [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
Thai Duong7689ed62015-03-20 16:50:18 -070017#include "hkdf.h"
18
Shawn Willden0f906ec2015-06-20 09:16:30 -060019#include <new>
20
21#include <keymaster/android_keymaster_utils.h>
22
23#include "hmac.h"
24
Thai Duong7689ed62015-03-20 16:50:18 -070025namespace keymaster {
26
Quan Nguyenf7538e02015-05-21 00:49:11 -070027bool Rfc5869Sha256Kdf::GenerateKey(const uint8_t* info, size_t info_len, uint8_t* output,
28 size_t output_len) {
29 if (!is_initialized_ || output == nullptr)
30 return false;
31 /**
32 * Step 1. Extract: PRK = HMAC-SHA256(actual_salt, secret)
33 * https://tools.ietf.org/html/rfc5869#section-2.2
34 */
Thai Duong7689ed62015-03-20 16:50:18 -070035 HmacSha256 prk_hmac;
Thai Duong60eebdc2015-03-25 17:20:24 -070036 bool result;
Quan Nguyenf7538e02015-05-21 00:49:11 -070037 if (salt_.get() != nullptr && salt_len_ > 0) {
38 result = prk_hmac.Init(salt_.get(), salt_len_);
Thai Duong60eebdc2015-03-25 17:20:24 -070039 } else {
Quan Nguyenf7538e02015-05-21 00:49:11 -070040 UniquePtr<uint8_t[]> zeros(new uint8_t[digest_size_]);
41 if (zeros.get() == nullptr)
42 return false;
43 /* If salt is not given, digest size of zeros are used. */
44 memset(zeros.get(), 0, digest_size_);
45 result = prk_hmac.Init(zeros.get(), digest_size_);
Thai Duong60eebdc2015-03-25 17:20:24 -070046 }
Quan Nguyenf7538e02015-05-21 00:49:11 -070047 if (!result)
Thai Duongfabacaf2015-03-25 20:14:57 -070048 return false;
Thai Duong7689ed62015-03-20 16:50:18 -070049
Quan Nguyenf7538e02015-05-21 00:49:11 -070050 UniquePtr<uint8_t[]> pseudo_random_key(new uint8_t[digest_size_]);
51 if (pseudo_random_key.get() == nullptr || digest_size_ != prk_hmac.DigestLength())
Thai Duongfabacaf2015-03-25 20:14:57 -070052 return false;
Quan Nguyenf7538e02015-05-21 00:49:11 -070053 result =
54 prk_hmac.Sign(secret_key_.get(), secret_key_len_, pseudo_random_key.get(), digest_size_);
55 if (!result)
Thai Duongfabacaf2015-03-25 20:14:57 -070056 return false;
Thai Duong7689ed62015-03-20 16:50:18 -070057
Quan Nguyenf7538e02015-05-21 00:49:11 -070058 /**
59 * Step 2. Expand: OUTPUT = HKDF-Expand(PRK, info)
60 * https://tools.ietf.org/html/rfc5869#section-2.3
61 */
62 const size_t num_blocks = (output_len + digest_size_ - 1) / digest_size_;
63 if (num_blocks >= 256u)
Thai Duongfabacaf2015-03-25 20:14:57 -070064 return false;
Thai Duong7689ed62015-03-20 16:50:18 -070065
Quan Nguyenf7538e02015-05-21 00:49:11 -070066 UniquePtr<uint8_t[]> buf(new uint8_t[digest_size_ + info_len + 1]);
67 UniquePtr<uint8_t[]> digest(new uint8_t[digest_size_]);
68 if (buf.get() == nullptr || digest.get() == nullptr)
69 return false;
Thai Duong7689ed62015-03-20 16:50:18 -070070 HmacSha256 hmac;
Quan Nguyenf7538e02015-05-21 00:49:11 -070071 result = hmac.Init(pseudo_random_key.get(), digest_size_);
72 if (!result)
Thai Duongfabacaf2015-03-25 20:14:57 -070073 return false;
Thai Duong7689ed62015-03-20 16:50:18 -070074
Quan Nguyenf7538e02015-05-21 00:49:11 -070075 for (size_t i = 0; i < num_blocks; i++) {
76 size_t block_input_len = 0;
77 if (i != 0) {
78 memcpy(buf.get(), digest.get(), digest_size_);
79 block_input_len = digest_size_;
Thai Duong60eebdc2015-03-25 17:20:24 -070080 }
Quan Nguyenf7538e02015-05-21 00:49:11 -070081 if (info != nullptr && info_len > 0)
82 memcpy(buf.get() + block_input_len, info, info_len);
83 block_input_len += info_len;
84 *(buf.get() + block_input_len++) = static_cast<uint8_t>(i + 1);
85 result = hmac.Sign(buf.get(), block_input_len, digest.get(), digest_size_);
Thai Duongfabacaf2015-03-25 20:14:57 -070086 if (!result)
87 return false;
Quan Nguyenf7538e02015-05-21 00:49:11 -070088 size_t block_output_len = digest_size_ < output_len - i * digest_size_
89 ? digest_size_
90 : output_len - i * digest_size_;
91 memcpy(output + i * digest_size_, digest.get(), block_output_len);
Thai Duong7689ed62015-03-20 16:50:18 -070092 }
Thai Duongfabacaf2015-03-25 20:14:57 -070093 return true;
Thai Duong7689ed62015-03-20 16:50:18 -070094}
95
96} // namespace keymaster