From acc33c18e2ea0e18cb0b2970a14c32ddbd7db833 Mon Sep 17 00:00:00 2001 From: Nick Mathewson Date: Wed, 16 Apr 2003 15:24:09 +0000 Subject: Tests for crypto; more tests for buffers svn:r234 --- src/common/crypto.c | 14 +++++++++----- src/common/test.h | 10 ++++++++++ 2 files changed, 19 insertions(+), 5 deletions(-) (limited to 'src/common') diff --git a/src/common/crypto.c b/src/common/crypto.c index 3ddb854f64..626e2c48e3 100644 --- a/src/common/crypto.c +++ b/src/common/crypto.c @@ -13,6 +13,7 @@ #include #include +#include #include "crypto.h" #include "../or/or.h" @@ -290,10 +291,10 @@ int crypto_pk_read_private_key_from_filename(crypto_pk_env_t *env, unsigned char if (strspn(keyfile,CONFIG_LEGAL_FILENAME_CHARACTERS) == strlen(keyfile)) /* filename contains legal characters only */ { /* open the keyfile */ - f_pr=fopen(keyfile,"r"); + f_pr=fopen(keyfile,"rb"); if (!f_pr) return -1; - + /* read the private key */ retval = crypto_pk_read_private_key_from_file(env, f_pr); fclose(f_pr); @@ -460,8 +461,9 @@ int crypto_pk_set_key(crypto_pk_env_t *env, unsigned char *key) case CRYPTO_PK_RSA: if (!env->key) return -1; - memcpy((void *)env->key, (void *)key, sizeof(RSA)); /* XXX BUG XXX you can't memcpy an RSA, it's got a bunch of subpointers */ + assert(0); + memcpy((void *)env->key, (void *)key, sizeof(RSA)); break; default : return -1; @@ -530,9 +532,11 @@ int crypto_pk_public_encrypt(crypto_pk_env_t *env, unsigned char *from, int from int crypto_pk_private_decrypt(crypto_pk_env_t *env, unsigned char *from, int fromlen, unsigned char *to, int padding) { assert(env && from && to); - + switch(env->type) { - case CRYPTO_PK_RSA: + case CRYPTO_PK_RSA: + if (!(((RSA*)env->key)->p)) + return -1; return RSA_private_decrypt(fromlen, from, to, (RSA *)env->key, padding); default: return -1; diff --git a/src/common/test.h b/src/common/test.h index fe37100940..8be3afd77e 100644 --- a/src/common/test.h +++ b/src/common/test.h @@ -88,6 +88,16 @@ return; \ } STMT_END +#define test_memneq(expr1, expr2, len) \ + STMT_BEGIN if(memcmp(expr1,expr2,len)) { printf("."); } else { \ + printf("\nFile %s: line %d (%s): Assertion failed: (%s!=%s)\n", \ + __FILE__, \ + __LINE__, \ + __PRETTY_FUNCTION__, \ + #expr1, #expr2); \ + return; \ + } STMT_END + #endif /* -- cgit v1.2.3-54-g00ecf