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/iso18033kdf.h>
18 
19 #include <openssl/evp.h>
20 
21 #include <keymaster/km_openssl/openssl_utils.h>
22 
23 namespace keymaster {
24 
min(size_t a,size_t b)25 inline size_t min(size_t a, size_t b) {
26     return (a < b) ? a : b;
27 }
28 
GenerateKey(const uint8_t * info,size_t info_len,uint8_t * output,size_t output_len)29 bool Iso18033Kdf::GenerateKey(const uint8_t* info, size_t info_len, uint8_t* output,
30                               size_t output_len) {
31     if (!is_initialized_ || output == nullptr) return false;
32 
33     /* Check whether output length is too long as specified in ISO/IEC 18033-2. */
34     if ((0xFFFFFFFFULL + start_counter_) * digest_size_ < (uint64_t)output_len) return false;
35 
36     EVP_MD_CTX ctx;
37     EvpMdCtxCleaner ctxCleaner(&ctx);
38     EVP_MD_CTX_init(&ctx);
39 
40     size_t num_blocks = (output_len + digest_size_ - 1) / digest_size_;
41     UniquePtr<uint8_t[]> counter(new (std::nothrow) uint8_t[4]);
42     UniquePtr<uint8_t[]> digest_result(new (std::nothrow) uint8_t[digest_size_]);
43     if (!counter.get() || !digest_result.get()) return false;
44     for (size_t block = 0; block < num_blocks; block++) {
45         switch (digest_type_) {
46         case KM_DIGEST_SHA1:
47             if (!EVP_DigestInit_ex(&ctx, EVP_sha1(), nullptr /* default digest */)) return false;
48             break;
49         case KM_DIGEST_SHA_2_256:
50             if (!EVP_DigestInit_ex(&ctx, EVP_sha256(), nullptr /* default digest */)) return false;
51             break;
52         default:
53             return false;
54         }
55 
56         if (!EVP_DigestUpdate(&ctx, secret_key_.get(), secret_key_len_) ||
57             !Uint32ToBigEndianByteArray(block + start_counter_, counter.get()) ||
58             !EVP_DigestUpdate(&ctx, counter.get(), 4))
59             return false;
60 
61         if (info != nullptr && info_len > 0) {
62             if (!EVP_DigestUpdate(&ctx, info, info_len)) return false;
63         }
64 
65         /* OpenSSL does not accept size_t parameter. */
66         uint32_t uint32_digest_size_ = digest_size_;
67         if (!EVP_DigestFinal_ex(&ctx, digest_result.get(), &uint32_digest_size_) ||
68             uint32_digest_size_ != digest_size_)
69             return false;
70 
71         size_t block_start = digest_size_ * block;
72         size_t block_length = min(digest_size_, output_len - block_start);
73         memcpy(output + block_start, digest_result.get(), block_length);
74     }
75     return true;
76 }
77 
78 }  // namespace keymaster
79