aboutsummaryrefslogtreecommitdiff
path: root/cc/internal/md_util.cc
blob: acca9fe3046a8c6802bceaf73ce158d93b079a4f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// Copyright 2021 Google LLC
//
// 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/internal/md_util.h"

#include <stdint.h>

#include <string>

#include "absl/status/status.h"
#include "absl/strings/str_cat.h"
#include "absl/strings/string_view.h"
#include "openssl/evp.h"
#include "tink/internal/err_util.h"
#include "tink/internal/util.h"
#include "tink/subtle/common_enums.h"
#include "tink/subtle/subtle_util.h"
#include "tink/util/status.h"
#include "tink/util/statusor.h"

namespace crypto {
namespace tink {
namespace internal {

util::StatusOr<const EVP_MD *> EvpHashFromHashType(subtle::HashType hash_type) {
  switch (hash_type) {
    case subtle::HashType::SHA1:
      return EVP_sha1();
    case subtle::HashType::SHA224:
      return EVP_sha224();
    case subtle::HashType::SHA256:
      return EVP_sha256();
    case subtle::HashType::SHA384:
      return EVP_sha384();
    case subtle::HashType::SHA512:
      return EVP_sha512();
    default:
      return util::Status(
          absl::StatusCode::kUnimplemented,
          absl::StrCat("Unsupported hash ", subtle::EnumToString(hash_type)));
  }
}

util::Status IsHashTypeSafeForSignature(subtle::HashType sig_hash) {
  switch (sig_hash) {
    case subtle::HashType::SHA256:
    case subtle::HashType::SHA384:
    case subtle::HashType::SHA512:
      return util::OkStatus();
    case subtle::HashType::SHA1:
    case subtle::HashType::SHA224:
      return util::Status(
          absl::StatusCode::kInvalidArgument,
          absl::StrCat("Hash function ", subtle::EnumToString(sig_hash),
                       " is not safe for digital signature"));
    default:
      return util::Status(absl::StatusCode::kInvalidArgument,
                          "Unsupported hash function");
  }
}

util::StatusOr<std::string> ComputeHash(absl::string_view input,
                                        const EVP_MD &hasher) {
  input = EnsureStringNonNull(input);
  std::string digest;
  subtle::ResizeStringUninitialized(&digest, EVP_MAX_MD_SIZE);
  uint32_t digest_length = 0;
  if (EVP_Digest(input.data(), input.length(),
                 reinterpret_cast<uint8_t *>(&digest[0]), &digest_length,
                 &hasher, /*impl=*/nullptr) != 1) {
    return util::Status(absl::StatusCode::kInternal,
                        absl::StrCat("Openssl internal error computing hash: ",
                                     internal::GetSslErrors()));
  }
  digest.resize(digest_length);
  return digest;
}

}  // namespace internal
}  // namespace tink
}  // namespace crypto