Add interceptors for the rmd160(3) from NetBSD

Summary:
RMD160Init, RMD160Update, RMD160Final, RMD160Transform, RMD160End,
RMD160File, RMD160Data - calculates the ``RIPEMD-160'' message digest.

Add a dedicated test for this API.

Reviewers: vitalybuka, joerg

Reviewed By: vitalybuka

Subscribers: kubamracek, llvm-commits, mgorny, #sanitizers

Tags: #sanitizers

Differential Revision: https://reviews.llvm.org/D54991

llvm-svn: 348678
This commit is contained in:
Kamil Rytarowski 2018-12-08 01:47:29 +00:00
parent e9bf12f82f
commit f9b8569d72
5 changed files with 234 additions and 0 deletions

View File

@ -8058,6 +8058,99 @@ INTERCEPTOR(char *, MD4Data, const unsigned char *data, unsigned int len,
#define INIT_MD4
#endif
#if SANITIZER_INTERCEPT_RMD160
INTERCEPTOR(void, RMD160Init, void *context) {
void *ctx;
COMMON_INTERCEPTOR_ENTER(ctx, RMD160Init, context);
REAL(RMD160Init)(context);
if (context)
COMMON_INTERCEPTOR_WRITE_RANGE(ctx, context, RMD160_CTX_sz);
}
INTERCEPTOR(void, RMD160Update, void *context, const u8 *data, unsigned len) {
void *ctx;
COMMON_INTERCEPTOR_ENTER(ctx, RMD160Update, context, data, len);
if (data && len > 0)
COMMON_INTERCEPTOR_READ_RANGE(ctx, data, len);
if (context)
COMMON_INTERCEPTOR_READ_RANGE(ctx, context, RMD160_CTX_sz);
REAL(RMD160Update)(context, data, len);
if (context)
COMMON_INTERCEPTOR_WRITE_RANGE(ctx, context, RMD160_CTX_sz);
}
INTERCEPTOR(void, RMD160Final, u8 digest[20], void *context) {
void *ctx;
COMMON_INTERCEPTOR_ENTER(ctx, RMD160Final, digest, context);
if (context)
COMMON_INTERCEPTOR_READ_RANGE(ctx, context, RMD160_CTX_sz);
REAL(RMD160Final)(digest, context);
if (digest)
COMMON_INTERCEPTOR_WRITE_RANGE(ctx, digest, sizeof(u8) * 20);
}
INTERCEPTOR(void, RMD160Transform, u32 state[5], u16 buffer[16]) {
void *ctx;
COMMON_INTERCEPTOR_ENTER(ctx, RMD160Transform, state, buffer);
if (state)
COMMON_INTERCEPTOR_READ_RANGE(ctx, state, sizeof(u32) * 5);
if (buffer)
COMMON_INTERCEPTOR_READ_RANGE(ctx, buffer, sizeof(u32) * 16);
REAL(RMD160Transform)(state, buffer);
if (state)
COMMON_INTERCEPTOR_WRITE_RANGE(ctx, state, sizeof(u32) * 5);
}
INTERCEPTOR(char *, RMD160End, void *context, char *buf) {
void *ctx;
COMMON_INTERCEPTOR_ENTER(ctx, RMD160End, context, buf);
if (context)
COMMON_INTERCEPTOR_READ_RANGE(ctx, context, RMD160_CTX_sz);
char *ret = REAL(RMD160End)(context, buf);
if (ret)
COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, RMD160_return_length);
return ret;
}
INTERCEPTOR(char *, RMD160File, char *filename, char *buf) {
void *ctx;
COMMON_INTERCEPTOR_ENTER(ctx, RMD160File, filename, buf);
if (filename)
COMMON_INTERCEPTOR_READ_RANGE(ctx, filename, REAL(strlen)(filename) + 1);
char *ret = REAL(RMD160File)(filename, buf);
if (ret)
COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, RMD160_return_length);
return ret;
}
INTERCEPTOR(char *, RMD160FileChunk, char *filename, char *buf, OFF_T offset,
OFF_T length) {
void *ctx;
COMMON_INTERCEPTOR_ENTER(ctx, RMD160FileChunk, filename, buf, offset, length);
if (filename)
COMMON_INTERCEPTOR_READ_RANGE(ctx, filename, REAL(strlen)(filename) + 1);
char *ret = REAL(RMD160FileChunk)(filename, buf, offset, length);
if (ret)
COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, RMD160_return_length);
return ret;
}
INTERCEPTOR(char *, RMD160Data, u8 *data, SIZE_T len, char *buf) {
void *ctx;
COMMON_INTERCEPTOR_ENTER(ctx, RMD160Data, data, len, buf);
if (data && len > 0)
COMMON_INTERCEPTOR_READ_RANGE(ctx, data, len);
char *ret = REAL(RMD160Data)(data, len, buf);
if (ret)
COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, RMD160_return_length);
return ret;
}
#define INIT_RMD160 \
COMMON_INTERCEPT_FUNCTION(RMD160Init); \
COMMON_INTERCEPT_FUNCTION(RMD160Update); \
COMMON_INTERCEPT_FUNCTION(RMD160Final); \
COMMON_INTERCEPT_FUNCTION(RMD160Transform); \
COMMON_INTERCEPT_FUNCTION(RMD160End); \
COMMON_INTERCEPT_FUNCTION(RMD160File); \
COMMON_INTERCEPT_FUNCTION(RMD160FileChunk); \
COMMON_INTERCEPT_FUNCTION(RMD160Data)
#else
#define INIT_RMD160
#endif
static void InitializeCommonInterceptors() {
static u64 metadata_mem[sizeof(MetadataHashMap) / sizeof(u64) + 1];
interceptor_metadata_map =
@ -8330,6 +8423,7 @@ static void InitializeCommonInterceptors() {
INIT_CAPSICUM;
INIT_SHA1;
INIT_MD4;
INIT_RMD160;
INIT___PRINTF_CHK;
}

View File

@ -537,5 +537,6 @@
#define SANITIZER_INTERCEPT_CAPSICUM SI_FREEBSD
#define SANITIZER_INTERCEPT_SHA1 SI_NETBSD
#define SANITIZER_INTERCEPT_MD4 SI_NETBSD
#define SANITIZER_INTERCEPT_RMD160 SI_NETBSD
#endif // #ifndef SANITIZER_PLATFORM_INTERCEPTORS_H

View File

@ -2107,6 +2107,9 @@ const unsigned SHA1_return_length = SHA1_DIGEST_STRING_LENGTH;
const unsigned MD4_CTX_sz = sizeof(MD4_CTX);
const unsigned MD4_return_length = MD4_DIGEST_STRING_LENGTH;
const unsigned RMD160_CTX_sz = sizeof(RMD160_CTX);
const unsigned RMD160_return_length = RMD160_DIGEST_STRING_LENGTH;
} // namespace __sanitizer
using namespace __sanitizer;

View File

@ -2231,6 +2231,9 @@ extern const unsigned SHA1_return_length;
extern const unsigned MD4_CTX_sz;
extern const unsigned MD4_return_length;
extern const unsigned RMD160_CTX_sz;
extern const unsigned RMD160_return_length;
} // namespace __sanitizer
#define CHECK_TYPE_SIZE(TYPE) \

View File

@ -0,0 +1,133 @@
// RUN: %clangxx -O0 -g %s -o %t && %run %t 2>&1 | FileCheck %s
#include <assert.h>
#include <rmd160.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void test1() {
RMD160_CTX ctx;
uint8_t entropy[] = { 0x11, 0x22, 0x33, 0x44, 0x55, 0x66 };
uint8_t digest[RMD160_DIGEST_LENGTH];
RMD160Init(&ctx);
RMD160Update(&ctx, entropy, __arraycount(entropy));
RMD160Final(digest, &ctx);
printf("test1: '");
for (size_t i = 0; i < __arraycount(digest); i++)
printf("%02x", digest[i]);
printf("'\n");
}
void test2() {
RMD160_CTX ctx;
uint8_t entropy[] = { 0x11, 0x22, 0x33, 0x44, 0x55, 0x66 };
char digest[RMD160_DIGEST_STRING_LENGTH];
RMD160Init(&ctx);
RMD160Update(&ctx, entropy, __arraycount(entropy));
char *p = RMD160End(&ctx, digest);
assert(p == digest);
printf("test2: '%s'\n", digest);
}
void test3() {
RMD160_CTX ctx;
uint8_t entropy[] = { 0x11, 0x22, 0x33, 0x44, 0x55, 0x66 };
RMD160Init(&ctx);
RMD160Update(&ctx, entropy, __arraycount(entropy));
char *p = RMD160End(&ctx, NULL);
assert(strlen(p) == RMD160_DIGEST_STRING_LENGTH - 1);
printf("test3: '%s'\n", p);
free(p);
}
void test4() {
char digest[RMD160_DIGEST_STRING_LENGTH];
char *p = RMD160File("/etc/fstab", digest);
assert(p == digest);
printf("test4: '%s'\n", p);
}
void test5() {
char *p = RMD160File("/etc/fstab", NULL);
assert(strlen(p) == RMD160_DIGEST_STRING_LENGTH - 1);
printf("test5: '%s'\n", p);
free(p);
}
void test6() {
char digest[RMD160_DIGEST_STRING_LENGTH];
char *p = RMD160FileChunk("/etc/fstab", digest, 10, 20);
assert(p == digest);
printf("test6: '%s'\n", p);
}
void test7() {
char *p = RMD160FileChunk("/etc/fstab", NULL, 10, 20);
assert(strlen(p) == RMD160_DIGEST_STRING_LENGTH - 1);
printf("test7: '%s'\n", p);
free(p);
}
void test8() {
uint8_t entropy[] = { 0x11, 0x22, 0x33, 0x44, 0x55, 0x66 };
char digest[RMD160_DIGEST_STRING_LENGTH];
char *p = RMD160Data(entropy, __arraycount(entropy), digest);
assert(p == digest);
printf("test8: '%s'\n", p);
}
void test9() {
uint8_t entropy[] = { 0x11, 0x22, 0x33, 0x44, 0x55, 0x66 };
char *p = RMD160Data(entropy, __arraycount(entropy), NULL);
assert(strlen(p) == RMD160_DIGEST_STRING_LENGTH - 1);
printf("test9: '%s'\n", p);
free(p);
}
int main(void) {
printf("RMD160\n");
test1();
test2();
test3();
test4();
test5();
test6();
test7();
test8();
test9();
// CHECK: RMD160
// CHECK: test1: '2787e5a006365df6e8e799315b669dc34866783c'
// CHECK: test2: '2787e5a006365df6e8e799315b669dc34866783c'
// CHECK: test3: '2787e5a006365df6e8e799315b669dc34866783c'
// CHECK: test4: '{{.*}}'
// CHECK: test5: '{{.*}}'
// CHECK: test6: '{{.*}}'
// CHECK: test7: '{{.*}}'
// CHECK: test8: '2787e5a006365df6e8e799315b669dc34866783c'
// CHECK: test9: '2787e5a006365df6e8e799315b669dc34866783c'
return 0;
}