ripemd_test.cc
Go to the documentation of this file.
1 /* Copyright (c) 2016, Google Inc.
2  *
3  * Permission to use, copy, modify, and/or distribute this software for any
4  * purpose with or without fee is hereby granted, provided that the above
5  * copyright notice and this permission notice appear in all copies.
6  *
7  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
10  * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
12  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
13  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
14 
15 #include <openssl/ripemd.h>
16 
17 #include <memory>
18 
19 #include <stdio.h>
20 #include <string.h>
21 
22 #include <gtest/gtest.h>
23 
24 #include "../../crypto/internal.h"
25 #include "../../crypto/test/test_util.h"
26 
27 
29  const char *input;
31 };
32 
33 static const RIPEMDTestCase kRIPEMDTestCases[] = {
34  {"", {0x9c, 0x11, 0x85, 0xa5, 0xc5, 0xe9, 0xfc, 0x54, 0x61, 0x28,
35  0x08, 0x97, 0x7e, 0xe8, 0xf5, 0x48, 0xb2, 0x25, 0x8d, 0x31}},
36  {"a", {0x0b, 0xdc, 0x9d, 0x2d, 0x25, 0x6b, 0x3e, 0xe9, 0xda, 0xae,
37  0x34, 0x7b, 0xe6, 0xf4, 0xdc, 0x83, 0x5a, 0x46, 0x7f, 0xfe}},
38  {"abc", {0x8e, 0xb2, 0x08, 0xf7, 0xe0, 0x5d, 0x98, 0x7a, 0x9b, 0x04,
39  0x4a, 0x8e, 0x98, 0xc6, 0xb0, 0x87, 0xf1, 0x5a, 0x0b, 0xfc}},
40  {"message digest",
41  {0x5d, 0x06, 0x89, 0xef, 0x49, 0xd2, 0xfa, 0xe5, 0x72, 0xb8,
42  0x81, 0xb1, 0x23, 0xa8, 0x5f, 0xfa, 0x21, 0x59, 0x5f, 0x36}},
43  {"abcdefghijklmnopqrstuvwxyz",
44  {0xf7, 0x1c, 0x27, 0x10, 0x9c, 0x69, 0x2c, 0x1b, 0x56, 0xbb,
45  0xdc, 0xeb, 0x5b, 0x9d, 0x28, 0x65, 0xb3, 0x70, 0x8d, 0xbc}},
46  {"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
47  {0x12, 0xa0, 0x53, 0x38, 0x4a, 0x9c, 0x0c, 0x88, 0xe4, 0x05,
48  0xa0, 0x6c, 0x27, 0xdc, 0xf4, 0x9a, 0xda, 0x62, 0xeb, 0x2b}},
49  {"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",
50  {0xb0, 0xe2, 0x0b, 0x6e, 0x31, 0x16, 0x64, 0x02, 0x86, 0xed,
51  0x3a, 0x87, 0xa5, 0x71, 0x30, 0x79, 0xb2, 0x1f, 0x51, 0x89}},
52  {"1234567890123456789012345678901234567890123456789012345678901234567890123"
53  "4567890",
54  {0x9b, 0x75, 0x2e, 0x45, 0x57, 0x3d, 0x4b, 0x39, 0xf4, 0xdb,
55  0xd3, 0x32, 0x3c, 0xab, 0x82, 0xbf, 0x63, 0x32, 0x6b, 0xfb}},
56 };
57 
58 // TODO(davidben): Convert this file to GTest properly.
59 TEST(RIPEMDTest, RunTest) {
60  unsigned test_num = 0;
61  int ok = 1;
62 
63  for (const auto &test : kRIPEMDTestCases) {
64  test_num++;
65 
66  const size_t input_len = strlen(test.input);
67 
68  for (size_t stride = 0; stride <= input_len; stride++) {
70 
71  if (stride == 0) {
72  RIPEMD160(reinterpret_cast<const uint8_t *>(test.input), input_len,
73  digest);
74  } else {
77 
78  for (size_t done = 0; done < input_len;) {
79  const size_t remaining = input_len - done;
80  size_t todo = stride;
81  if (todo > remaining) {
82  todo = remaining;
83  }
84 
85  RIPEMD160_Update(&ctx, &test.input[done], todo);
86  done += todo;
87  }
88 
89  RIPEMD160_Final(digest, &ctx);
90  }
91 
92  if (OPENSSL_memcmp(digest, test.expected, sizeof(digest)) != 0) {
93  fprintf(stderr, "#%u: bad result with stride %u: ", test_num,
94  static_cast<unsigned>(stride));
95  hexdump(stderr, "", digest, sizeof(digest));
96  ok = 0;
97  }
98  }
99  }
100 
101  static const size_t kLargeBufSize = 1000000;
102  std::unique_ptr<uint8_t[]> buf(new uint8_t[kLargeBufSize]);
103  OPENSSL_memset(buf.get(), 'a', kLargeBufSize);
105  RIPEMD160(buf.get(), kLargeBufSize, digest);
106 
107  static const uint8_t kMillionADigest[RIPEMD160_DIGEST_LENGTH] = {
108  0x52, 0x78, 0x32, 0x43, 0xc1, 0x69, 0x7b, 0xdb, 0xe1, 0x6d,
109  0x37, 0xf9, 0x7f, 0x68, 0xf0, 0x83, 0x25, 0xdc, 0x15, 0x28};
110 
111  if (OPENSSL_memcmp(digest, kMillionADigest, sizeof(digest)) != 0) {
112  fprintf(stderr, u8"Digest incorrect for “million a's” test: ");
113  hexdump(stderr, "", digest, sizeof(digest));
114  ok = 0;
115  }
116 
117  EXPECT_EQ(1, ok);
118 }
kRIPEMDTestCases
static const RIPEMDTestCase kRIPEMDTestCases[]
Definition: ripemd_test.cc:33
RIPEMDTestCase::input
const char * input
Definition: ripemd_test.cc:29
OPENSSL_memcmp
static int OPENSSL_memcmp(const void *s1, const void *s2, size_t n)
Definition: third_party/boringssl-with-bazel/src/crypto/internal.h:811
ctx
Definition: benchmark-async.c:30
RIPEMD160_Final
int RIPEMD160_Final(uint8_t out[RIPEMD160_DIGEST_LENGTH], RIPEMD160_CTX *c)
Definition: ripemd.c:95
hexdump
static void hexdump(dump_out *out, const char *buf, size_t len)
Definition: string.cc:98
test
Definition: spinlock_test.cc:36
string.h
buf
voidpf void * buf
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:136
ctx
static struct test_ctx ctx
Definition: test-ipc-send-recv.c:65
uint8_t
unsigned char uint8_t
Definition: stdint-msvc2008.h:78
OPENSSL_memset
static void * OPENSSL_memset(void *dst, int c, size_t n)
Definition: third_party/boringssl-with-bazel/src/crypto/internal.h:835
python_utils.port_server.stderr
stderr
Definition: port_server.py:51
EXPECT_EQ
#define EXPECT_EQ(a, b)
Definition: iomgr/time_averaged_stats_test.cc:27
RIPEMDTestCase::expected
uint8_t expected[RIPEMD160_DIGEST_LENGTH]
Definition: ripemd_test.cc:30
ripemd.h
RIPEMDTestCase
Definition: ripemd_test.cc:28
done
struct tab * done
Definition: bloaty/third_party/zlib/examples/enough.c:176
TEST
TEST(RIPEMDTest, RunTest)
Definition: ripemd_test.cc:59
RIPEMD160_DIGEST_LENGTH
#define RIPEMD160_DIGEST_LENGTH
Definition: ripemd.h:69
RIPEMD160_Init
int RIPEMD160_Init(RIPEMD160_CTX *ctx)
Definition: ripemd.c:71
RIPEMD160state_st
Definition: ripemd.h:71
RIPEMD160_Update
int RIPEMD160_Update(RIPEMD160_CTX *c, const void *data, size_t len)
Definition: ripemd.c:89
ok
bool ok
Definition: async_end2end_test.cc:197
RIPEMD160
uint8_t * RIPEMD160(const uint8_t *data, size_t len, uint8_t out[RIPEMD160_DIGEST_LENGTH])
Definition: ripemd.c:732
RunTest
static void RunTest(RandomDistInterface &&r, int threads, const std::string &title)
Definition: qps_interarrival_test.cc:31
mkowners.todo
todo
Definition: mkowners.py:209


grpc
Author(s):
autogenerated on Fri May 16 2025 03:00:05