aboutsummaryrefslogtreecommitdiff
path: root/cc/subtle/ed25519_verify_boringssl.cc
blob: 853bb220ec59bf85084f7e2e2609839545db81ae (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
94
95
96
97
98
99
100
// Copyright 2019 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/ed25519_verify_boringssl.h"

#include <cstring>
#include <memory>
#include <utility>

#include "absl/memory/memory.h"
#include "absl/status/status.h"
#include "absl/strings/str_format.h"
#include "absl/strings/string_view.h"
#include "openssl/evp.h"
#include "tink/internal/ec_util.h"
#include "tink/internal/util.h"
#include "tink/public_key_verify.h"
#include "tink/util/statusor.h"

namespace crypto {
namespace tink {
namespace subtle {

constexpr int kEd25519SignatureLenInBytes = 64;

util::StatusOr<std::unique_ptr<PublicKeyVerify>> Ed25519VerifyBoringSsl::New(
    absl::string_view public_key) {
  auto status = internal::CheckFipsCompatibility<Ed25519VerifyBoringSsl>();
  if (!status.ok()) return status;

  if (public_key.length() != internal::Ed25519KeyPubKeySize()) {
    return util::Status(
        absl::StatusCode::kInvalidArgument,
        absl::StrFormat("Invalid ED25519 public key size (%d). "
                        "The only valid size is %d.",
                        public_key.length(), internal::Ed25519KeyPubKeySize()));
  }

  // Generate a new EVP_PKEY key and populate it with the public key data.
  internal::SslUniquePtr<EVP_PKEY> ssl_pub_key(EVP_PKEY_new_raw_public_key(
      EVP_PKEY_ED25519, /*unused=*/nullptr,
      reinterpret_cast<const uint8_t *>(public_key.data()),
      internal::Ed25519KeyPrivKeySize()));
  if (ssl_pub_key == nullptr) {
    return util::Status(absl::StatusCode::kInternal,
                        "EVP_PKEY_new_raw_public_key failed");
  }

  return {absl::WrapUnique(new Ed25519VerifyBoringSsl(std::move(ssl_pub_key)))};
}

util::Status Ed25519VerifyBoringSsl::Verify(absl::string_view signature,
                                            absl::string_view data) const {
  signature = internal::EnsureStringNonNull(signature);
  data = internal::EnsureStringNonNull(data);

  if (signature.size() != kEd25519SignatureLenInBytes) {
    return util::Status(
        absl::StatusCode::kInvalidArgument,
        absl::StrFormat("Invalid ED25519 signature size (%d). "
                        "The signature must be %d bytes long.",
                        signature.size(), kEd25519SignatureLenInBytes));
  }

  internal::SslUniquePtr<EVP_MD_CTX> md_ctx(EVP_MD_CTX_create());
  // `type` must be set to nullptr with Ed25519.
  if (EVP_DigestVerifyInit(md_ctx.get(), /*pctx=*/nullptr, /*type=*/nullptr,
                           /*e=*/nullptr, public_key_.get()) != 1) {
    return util::Status(absl::StatusCode::kInternal,
                        "EVP_DigestVerifyInit failed.");
  }

  if (EVP_DigestVerify(
          md_ctx.get(),
          /*sig=*/reinterpret_cast<const uint8_t *>(signature.data()),
          signature.size(),
          /*data=*/reinterpret_cast<const uint8_t *>(data.data()),
          data.size()) != 1) {
    return util::Status(absl::StatusCode::kInternal, "Signature is not valid.");
  }

  return util::OkStatus();
}

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