Remove openssl/evp.h dependency from test_crypto.c

This commit is contained in:
Nick Mathewson 2017-03-25 12:13:31 +01:00
parent 1a14e5be91
commit a4964466a5
2 changed files with 55 additions and 38 deletions

View File

@ -15,8 +15,6 @@
#include "crypto_ed25519.h" #include "crypto_ed25519.h"
#include "ed25519_vectors.inc" #include "ed25519_vectors.inc"
#include <openssl/evp.h>
/** Run unit tests for Diffie-Hellman functionality. */ /** Run unit tests for Diffie-Hellman functionality. */
static void static void
test_crypto_dh(void *arg) test_crypto_dh(void *arg)
@ -1444,28 +1442,6 @@ test_crypto_digest_names(void *arg)
; ;
} }
#ifndef OPENSSL_1_1_API
#define EVP_ENCODE_CTX_new() tor_malloc_zero(sizeof(EVP_ENCODE_CTX))
#define EVP_ENCODE_CTX_free(ctx) tor_free(ctx)
#endif
/** Encode src into dest with OpenSSL's EVP Encode interface, returning the
* length of the encoded data in bytes.
*/
static int
base64_encode_evp(char *dest, char *src, size_t srclen)
{
const unsigned char *s = (unsigned char*)src;
EVP_ENCODE_CTX *ctx = EVP_ENCODE_CTX_new();
int len, ret;
EVP_EncodeInit(ctx);
EVP_EncodeUpdate(ctx, (unsigned char *)dest, &len, s, (int)srclen);
EVP_EncodeFinal(ctx, (unsigned char *)(dest + len), &ret);
EVP_ENCODE_CTX_free(ctx);
return ret+ len;
}
/** Run unit tests for misc crypto formatting functionality (base64, base32, /** Run unit tests for misc crypto formatting functionality (base64, base32,
* fingerprints, etc) */ * fingerprints, etc) */
static void static void
@ -1521,20 +1497,6 @@ test_crypto_formats(void *arg)
tt_assert(digest_from_base64(data3, "###") < 0); tt_assert(digest_from_base64(data3, "###") < 0);
for (i = 0; i < 256; i++) {
/* Test the multiline format Base64 encoder with 0 .. 256 bytes of
* output against OpenSSL.
*/
const size_t enclen = base64_encode_size(i, BASE64_ENCODE_MULTILINE);
data1[i] = i;
j = base64_encode(data2, 1024, data1, i, BASE64_ENCODE_MULTILINE);
tt_int_op(j, OP_EQ, enclen);
j = base64_encode_evp(data3, data1, i);
tt_int_op(j, OP_EQ, enclen);
tt_mem_op(data2, OP_EQ, data3, enclen);
tt_int_op(j, OP_EQ, strlen(data2));
}
/* Encoding SHA256 */ /* Encoding SHA256 */
crypto_rand(data2, DIGEST256_LEN); crypto_rand(data2, DIGEST256_LEN);
memset(data2, 100, 1024); memset(data2, 100, 1024);

View File

@ -8,6 +8,9 @@
#define CRYPTO_PRIVATE #define CRYPTO_PRIVATE
#include "crypto.h" #include "crypto.h"
#include "util.h"
#include "util_format.h"
#include "compat.h"
#include "test.h" #include "test.h"
#include <openssl/evp.h> #include <openssl/evp.h>
@ -46,7 +49,59 @@ test_crypto_rng_engine(void *arg)
; ;
} }
#ifndef OPENSSL_1_1_API
#define EVP_ENCODE_CTX_new() tor_malloc_zero(sizeof(EVP_ENCODE_CTX))
#define EVP_ENCODE_CTX_free(ctx) tor_free(ctx)
#endif
/** Encode src into dest with OpenSSL's EVP Encode interface, returning the
* length of the encoded data in bytes.
*/
static int
base64_encode_evp(char *dest, char *src, size_t srclen)
{
const unsigned char *s = (unsigned char*)src;
EVP_ENCODE_CTX *ctx = EVP_ENCODE_CTX_new();
int len, ret;
EVP_EncodeInit(ctx);
EVP_EncodeUpdate(ctx, (unsigned char *)dest, &len, s, (int)srclen);
EVP_EncodeFinal(ctx, (unsigned char *)(dest + len), &ret);
EVP_ENCODE_CTX_free(ctx);
return ret+ len;
}
static void
test_crypto_base64_encode_matches(void *arg)
{
(void)arg;
int i, j;
char data1[1024];
char data2[1024];
char data3[1024];
for (i = 0; i < 256; i++) {
/* Test the multiline format Base64 encoder with 0 .. 256 bytes of
* output against OpenSSL.
*/
const size_t enclen = base64_encode_size(i, BASE64_ENCODE_MULTILINE);
data1[i] = i;
j = base64_encode(data2, 1024, data1, i, BASE64_ENCODE_MULTILINE);
tt_int_op(j, OP_EQ, enclen);
j = base64_encode_evp(data3, data1, i);
tt_int_op(j, OP_EQ, enclen);
tt_mem_op(data2, OP_EQ, data3, enclen);
tt_int_op(j, OP_EQ, strlen(data2));
}
done:
;
}
struct testcase_t crypto_openssl_tests[] = { struct testcase_t crypto_openssl_tests[] = {
{ "rng_engine", test_crypto_rng_engine, TT_FORK, NULL, NULL }, { "rng_engine", test_crypto_rng_engine, TT_FORK, NULL, NULL },
{ "base64_encode_match", test_crypto_base64_encode_matches,
TT_FORK, NULL, NULL },
END_OF_TESTCASES END_OF_TESTCASES
}; };