File: ed25519_signature_unittest.cc

package info (click to toggle)
chromium 139.0.7258.138-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 6,120,676 kB
  • sloc: cpp: 35,100,869; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (123 lines) | stat: -rw-r--r-- 4,251 bytes parent folder | download | duplicates (6)
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/web_package/signed_web_bundles/ed25519_signature.h"

#include <algorithm>
#include <vector>

#include "base/containers/span.h"
#include "base/test/gmock_expected_support.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace web_package {

namespace {

constexpr uint8_t kMessage[] = {'t', 'e', 's', 't', ' ', 'm',
                                'e', 's', 's', 'a', 'g', 'e'};

constexpr uint8_t kOtherMessage[] = {'o', 't', 'h', 'e', 'r', ' ', 'm',
                                     'e', 's', 's', 'a', 'g', 'e'};

// Signature and public key were generated with the following Go script:
//
// package main
//
// import (
//  "crypto/ed25519"
//  "fmt"
//  "strings"
// )
//
// func printHex(data []byte) string {
//  var result strings.Builder
//  for i, byte := range data {
//   if i > 0 {
//    result.WriteString(", ")
//   }
//   fmt.Fprintf(&result, "0x%02x", byte)
//  }
//  return result.String()
// }
//
// func main() {
//  public_key, private_key, _ := ed25519.GenerateKey(nil)
//  fmt.Print("Public Key: ")
//  fmt.Println(printHex(public_key))
//  fmt.Print("Signature: ")
//  fmt.Println(printHex(ed25519.Sign(private_key, []byte("test message"))))
// }
constexpr uint8_t kPublicKey[] = {
    0xaf, 0x07, 0xe8, 0xc1, 0x25, 0xb3, 0xd2, 0x14, 0x61, 0x2a, 0x8b,
    0x9e, 0x1e, 0x42, 0x41, 0x5c, 0x72, 0x48, 0xc9, 0x4b, 0xd3, 0x30,
    0xb3, 0x17, 0x2d, 0xa0, 0xe0, 0xe6, 0x7b, 0xf7, 0xdd, 0x79};

constexpr uint8_t kSignature[] = {
    0x0e, 0x1d, 0x57, 0xf6, 0xd1, 0xa7, 0x59, 0xa5, 0x96, 0x06, 0x1e,
    0xe3, 0x8e, 0x41, 0xc2, 0x30, 0x5b, 0x68, 0x84, 0xae, 0x5b, 0xad,
    0x57, 0x49, 0x7f, 0x23, 0x76, 0x1c, 0xb2, 0x09, 0xc1, 0x4b, 0x5b,
    0xb7, 0x4d, 0x69, 0xea, 0x9d, 0x54, 0x32, 0x58, 0xe7, 0x60, 0xcc,
    0x88, 0x75, 0xb6, 0xd0, 0xe1, 0x40, 0x37, 0x44, 0x73, 0x6f, 0xbb,
    0x15, 0x1e, 0x36, 0x99, 0x4d, 0x8a, 0x1b, 0xb5, 0x0e};

// This is a different signature with the first byte changed to 0xFF.
constexpr uint8_t kOtherSignature[] = {
    0xFF, 0x1d, 0x57, 0xf6, 0xd1, 0xa7, 0x59, 0xa5, 0x96, 0x06, 0x1e,
    0xe3, 0x8e, 0x41, 0xc2, 0x30, 0x5b, 0x68, 0x84, 0xae, 0x5b, 0xad,
    0x57, 0x49, 0x7f, 0x23, 0x76, 0x1c, 0xb2, 0x09, 0xc1, 0x4b, 0x5b,
    0xb7, 0x4d, 0x69, 0xea, 0x9d, 0x54, 0x32, 0x58, 0xe7, 0x60, 0xcc,
    0x88, 0x75, 0xb6, 0xd0, 0xe1, 0x40, 0x37, 0x44, 0x73, 0x6f, 0xbb,
    0x15, 0x1e, 0x36, 0x99, 0x4d, 0x8a, 0x1b, 0xb5, 0x0e};

}  // namespace

TEST(Ed25519SignatureTest, ValidSignatureFromVector) {
  std::vector<uint8_t> bytes(64);
  bytes[3] = 123;

  ASSERT_OK_AND_ASSIGN(auto signature, Ed25519Signature::Create(bytes));
  EXPECT_TRUE(std::ranges::equal(bytes, signature.bytes()));
}

TEST(Ed25519SignatureTest, ValidSignatureFromArray) {
  auto signature = Ed25519Signature::Create(base::span(kSignature));
  EXPECT_TRUE(std::ranges::equal(base::span(kSignature), signature.bytes()));
}

TEST(Ed25519SignatureTest, Equality) {
  auto signature1a = Ed25519Signature::Create(base::span(kSignature));
  auto signature1b = Ed25519Signature::Create(base::span(kSignature));
  auto signature2 = Ed25519Signature::Create(base::span(kOtherSignature));
  EXPECT_TRUE(signature1a == signature1a);
  EXPECT_TRUE(signature1a == signature1b);
  EXPECT_FALSE(signature1a == signature2);

  EXPECT_FALSE(signature1a != signature1a);
  EXPECT_FALSE(signature1a != signature1b);
  EXPECT_TRUE(signature1a != signature2);
}

TEST(Ed25519SignatureTest, InvalidSignature) {
  std::vector<uint8_t> bytes(17);
  bytes[3] = 123;

  auto signature = Ed25519Signature::Create(bytes);
  EXPECT_THAT(signature,
              base::test::ErrorIs("The signature has the wrong length. "
                                  "Expected 64, but got 17 bytes."));
}

TEST(Ed25519SignatureTest, Verify) {
  auto signature = Ed25519Signature::Create(base::span(kSignature));

  EXPECT_TRUE(signature.Verify(
      base::span(kMessage), Ed25519PublicKey::Create(base::span(kPublicKey))));

  EXPECT_FALSE(
      signature.Verify(base::span(kOtherMessage),
                       Ed25519PublicKey::Create(base::span(kPublicKey))));
}

}  // namespace web_package