Skip to content
Snippets Groups Projects
  • Haris Andrianakis's avatar
    Use Portable Protocol Buffers (go/portableproto) for Tink C++. · 290eb9ad
    Haris Andrianakis authored
    Changes:
    - Renamed all references of something_cc_proto to something_portable_proto.
    - Added //cc/util/protobuf_helper which creates a namespace portable_port that maps to the correct namespace.
    - Replaced all deps to //net/proto2/ with //cc/util:protobuf_helper.
    - Replaced all instances of the proto2:: namespace with portable_proto::
    - Deleted the line 'namespace util = crypto::tink::util' because it was conflicting with //util/task/status.h (a dependency of portable_proto_lib). Also, it wasn't really used in any of the files.
    
    Notes:
    - When adding a new proto you will need to whitelist it in proto/portable_tink_filter.asciipb
    - For google3 (BUILD) we should use portable_proto_lib, for opensource (BUILD.bazel) we should use cc_proto_lib.
    PiperOrigin-RevId: 193084414
    GitOrigin-RevId: 266a0dbb7f66549655e521350b70b10d4a872286
    290eb9ad
hkdf.cc 2.39 KiB
// Copyright 2017 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
///////////////////////////////////////////////////////////////////////////////

#include "tink/subtle/hkdf.h"

#include "tink/subtle/subtle_util_boringssl.h"
#include "tink/subtle/common_enums.h"
#include "tink/util/status.h"
#include "tink/util/statusor.h"
#include "openssl/evp.h"
#include "openssl/hkdf.h"


namespace crypto {
namespace tink {
namespace subtle {

// static
util::StatusOr<std::string> Hkdf::ComputeHkdf(HashType hash,
                                              absl::string_view ikm,
                                              absl::string_view salt,
                                              absl::string_view info,
                                              size_t out_len) {
  auto status_or_evp_md = SubtleUtilBoringSSL::EvpHash(hash);
  if (!status_or_evp_md.ok()) {
    return status_or_evp_md.status();
  }
  std::unique_ptr<uint8_t[]> out_key(new uint8_t[out_len]);
  if (1 != HKDF(out_key.get(), out_len, status_or_evp_md.ValueOrDie(),
                reinterpret_cast<const uint8_t *>(ikm.data()), ikm.size(),
                reinterpret_cast<const uint8_t *>(salt.data()), salt.size(),
                reinterpret_cast<const uint8_t *>(info.data()), info.size())) {
    return util::Status(util::error::INTERNAL, "BoringSSL's HKDF failed");
  }
  return std::string(reinterpret_cast<const char *>(out_key.get()), out_len);
}

// static
util::StatusOr<std::string> Hkdf::ComputeEciesHkdfSymmetricKey(
    HashType hash,
    absl::string_view kem_bytes,
    absl::string_view shared_secret,
    absl::string_view salt,
    absl::string_view info,
    size_t out_len) {
  std::string ikm(kem_bytes);
  std::string shared_secret_string(shared_secret);
  ikm.append(shared_secret_string);
  return Hkdf::ComputeHkdf(hash, ikm, salt, info, out_len);
}

}  // namespace subtle
}  // namespace tink
}  // namespace crypto