#include <polarssl/config.h>
#include <polarssl/rsa.h>
#include <polarssl/md2.h>
#include <polarssl/md4.h>
#include <polarssl/md5.h>
#include <polarssl/sha1.h>
#include <polarssl/sha256.h>
#include <polarssl/sha512.h>
#include <polarssl/entropy.h>
#include <polarssl/ctr_drbg.h>
#include "polarssl/bignum.h"
#include <inttypes.h>
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
Go to the source code of this file.
Data Structures | |
struct | rnd_buf_info |
struct | rnd_pseudo_info |
Info structure for the pseudo random function. More... | |
Defines | |
#define | ciL (sizeof(t_uint)) |
NOT random function, to match test vectors. | |
#define | CHARS_TO_LIMBS(i) (((i) + ciL - 1) / ciL) |
#define | TEST_SUITE_ACTIVE |
#define | TEST_ASSERT(TEST) |
Functions | |
static int | unhexify (unsigned char *obuf, const char *ibuf) |
static void | hexify (unsigned char *obuf, const unsigned char *ibuf, int len) |
static int | rnd_std_rand (void *rng_state, unsigned char *output, size_t len) |
This function just returns data from rand(). | |
static int | rnd_zero_rand (void *rng_state, unsigned char *output, size_t len) |
This function only returns zeros. | |
static int | rnd_buffer_rand (void *rng_state, unsigned char *output, size_t len) |
This function returns random based on a buffer it receives. | |
static int | rnd_pseudo_rand (void *rng_state, unsigned char *output, size_t len) |
This function returns random based on a pseudo random function. | |
static int | not_rnd_mpi (void *in, unsigned char *out, size_t len) |
static int | test_assert (int correct, char *test) |
int | verify_string (char **str) |
int | verify_int (char *str, int *value) |
void | test_suite_rsa_pkcs1_sign (char *message_hex_string, int padding_mode, int digest, int mod, int radix_P, char *input_P, int radix_Q, char *input_Q, int radix_N, char *input_N, int radix_E, char *input_E, char *result_hex_str, int result) |
void | test_suite_rsa_pkcs1_verify (char *message_hex_string, int padding_mode, int digest, int mod, int radix_N, char *input_N, int radix_E, char *input_E, char *result_hex_str, int result) |
void | test_suite_rsa_pkcs1_sign_raw (char *message_hex_string, char *hash_result_string, int padding_mode, int mod, int radix_P, char *input_P, int radix_Q, char *input_Q, int radix_N, char *input_N, int radix_E, char *input_E, char *result_hex_str) |
void | test_suite_rsa_pkcs1_verify_raw (char *message_hex_string, char *hash_result_string, int padding_mode, int mod, int radix_N, char *input_N, int radix_E, char *input_E, char *result_hex_str, int correct) |
void | test_suite_rsa_pkcs1_encrypt (char *message_hex_string, int padding_mode, int mod, int radix_N, char *input_N, int radix_E, char *input_E, char *result_hex_str, int result) |
void | test_suite_rsa_pkcs1_encrypt_bad_rng (char *message_hex_string, int padding_mode, int mod, int radix_N, char *input_N, int radix_E, char *input_E, char *result_hex_str, int result) |
void | test_suite_rsa_pkcs1_decrypt (char *message_hex_string, int padding_mode, int mod, int radix_P, char *input_P, int radix_Q, char *input_Q, int radix_N, char *input_N, int radix_E, char *input_E, int max_output, char *result_hex_str, int result) |
void | test_suite_rsa_public (char *message_hex_string, int mod, int radix_N, char *input_N, int radix_E, char *input_E, char *result_hex_str, int result) |
void | test_suite_rsa_private (char *message_hex_string, int mod, int radix_P, char *input_P, int radix_Q, char *input_Q, int radix_N, char *input_N, int radix_E, char *input_E, char *result_hex_str, int result) |
void | test_suite_rsa_check_privkey_null () |
void | test_suite_rsa_check_pubkey (int radix_N, char *input_N, int radix_E, char *input_E, int result) |
void | test_suite_rsa_check_privkey (int mod, int radix_P, char *input_P, int radix_Q, char *input_Q, int radix_N, char *input_N, int radix_E, char *input_E, int radix_D, char *input_D, int radix_DP, char *input_DP, int radix_DQ, char *input_DQ, int radix_QP, char *input_QP, int result) |
void | test_suite_rsa_selftest () |
int | dep_check (char *str) |
int | dispatch_test (int cnt, char *params[50]) |
int | get_line (FILE *f, char *buf, size_t len) |
int | parse_arguments (char *buf, size_t len, char *params[50]) |
int | main () |
Variables | |
static int | test_errors = 0 |
#define CHARS_TO_LIMBS | ( | i | ) | (((i) + ciL - 1) / ciL) |
Definition at line 266 of file test_suite_rsa.c.
#define ciL (sizeof(t_uint)) |
NOT random function, to match test vectors.
The following are equivalent: mpi_fill_random( x, strlen( str ) / 2, not_rnd, str ); mpi_read_string( x, 16, str ); Warning: no other use is supported!
Definition at line 265 of file test_suite_rsa.c.
#define TEST_ASSERT | ( | TEST | ) |
do { test_assert( (TEST) ? 1 : 0, #TEST ); \ if( test_errors) return; \ } while (0)
Definition at line 315 of file test_suite_rsa.c.
#define TEST_SUITE_ACTIVE |
Definition at line 300 of file test_suite_rsa.c.
int dep_check | ( | char * | str | ) |
Definition at line 962 of file test_suite_rsa.c.
int dispatch_test | ( | int | cnt, | |
char * | params[50] | |||
) |
Definition at line 1052 of file test_suite_rsa.c.
References test_suite_rsa_check_privkey(), test_suite_rsa_check_privkey_null(), test_suite_rsa_check_pubkey(), test_suite_rsa_pkcs1_decrypt(), test_suite_rsa_pkcs1_encrypt(), test_suite_rsa_pkcs1_encrypt_bad_rng(), test_suite_rsa_pkcs1_sign(), test_suite_rsa_pkcs1_sign_raw(), test_suite_rsa_pkcs1_verify(), test_suite_rsa_pkcs1_verify_raw(), test_suite_rsa_private(), test_suite_rsa_public(), test_suite_rsa_selftest(), verify_int(), and verify_string().
int get_line | ( | FILE * | f, | |
char * | buf, | |||
size_t | len | |||
) |
Definition at line 1565 of file test_suite_rsa.c.
static void hexify | ( | unsigned char * | obuf, | |
const unsigned char * | ibuf, | |||
int | len | |||
) | [static] |
Definition at line 101 of file test_suite_rsa.c.
Referenced by test_suite_rsa_pkcs1_decrypt(), test_suite_rsa_pkcs1_encrypt(), test_suite_rsa_pkcs1_encrypt_bad_rng(), test_suite_rsa_pkcs1_sign(), test_suite_rsa_pkcs1_sign_raw(), test_suite_rsa_private(), and test_suite_rsa_public().
int main | ( | ) |
Definition at line 1642 of file test_suite_rsa.c.
References buffer_data::buf, dep_check(), dispatch_test(), get_line(), parse_arguments(), and test_errors.
static int not_rnd_mpi | ( | void * | in, | |
unsigned char * | out, | |||
size_t | len | |||
) | [static] |
Definition at line 267 of file test_suite_rsa.c.
References CHARS_TO_LIMBS, mpi_read_string(), mpi::n, mpi::p, and mpi::s.
int parse_arguments | ( | char * | buf, | |
size_t | len, | |||
char * | params[50] | |||
) |
Definition at line 1581 of file test_suite_rsa.c.
static int rnd_buffer_rand | ( | void * | rng_state, | |
unsigned char * | output, | |||
size_t | len | |||
) | [static] |
This function returns random based on a buffer it receives.
rng_state shall be a pointer to a rnd_buf_info structure.
The number of bytes released from the buffer on each call to the random function is specified by per_call. (Can be between 1 and 4)
After the buffer is empty it will return rand();
Definition at line 179 of file test_suite_rsa.c.
References rnd_buf_info::buf, rnd_buf_info::length, and rnd_std_rand().
static int rnd_pseudo_rand | ( | void * | rng_state, | |
unsigned char * | output, | |||
size_t | len | |||
) | [static] |
This function returns random based on a pseudo random function.
This means the results should be identical on all systems. Pseudo random is based on the XTEA encryption algorithm to generate pseudorandom.
rng_state shall be a pointer to a rnd_pseudo_info structure.
Definition at line 225 of file test_suite_rsa.c.
References rnd_pseudo_info::key, PUT_UINT32_BE, rnd_std_rand(), rnd_pseudo_info::v0, and rnd_pseudo_info::v1.
Referenced by test_suite_rsa_pkcs1_decrypt(), test_suite_rsa_pkcs1_encrypt(), test_suite_rsa_pkcs1_sign(), test_suite_rsa_pkcs1_sign_raw(), and test_suite_rsa_private().
static int rnd_std_rand | ( | void * | rng_state, | |
unsigned char * | output, | |||
size_t | len | |||
) | [static] |
This function just returns data from rand().
Although predictable and often similar on multiple runs, this does not result in identical random on each run. So do not use this if the results of a test depend on the random data that is generated.
rng_state shall be NULL.
Definition at line 134 of file test_suite_rsa.c.
Referenced by rnd_buffer_rand(), and rnd_pseudo_rand().
static int rnd_zero_rand | ( | void * | rng_state, | |
unsigned char * | output, | |||
size_t | len | |||
) | [static] |
This function only returns zeros.
rng_state shall be NULL.
Definition at line 152 of file test_suite_rsa.c.
Referenced by test_suite_rsa_pkcs1_encrypt_bad_rng().
static int test_assert | ( | int | correct, | |
char * | test | |||
) | [static] |
Definition at line 302 of file test_suite_rsa.c.
References test_errors.
void test_suite_rsa_check_privkey | ( | int | mod, | |
int | radix_P, | |||
char * | input_P, | |||
int | radix_Q, | |||
char * | input_Q, | |||
int | radix_N, | |||
char * | input_N, | |||
int | radix_E, | |||
char * | input_E, | |||
int | radix_D, | |||
char * | input_D, | |||
int | radix_DP, | |||
char * | input_DP, | |||
int | radix_DQ, | |||
char * | input_DQ, | |||
int | radix_QP, | |||
char * | input_QP, | |||
int | result | |||
) |
Definition at line 872 of file test_suite_rsa.c.
References rsa_context::D, rsa_context::DP, rsa_context::DQ, rsa_context::E, rsa_context::len, mpi_read_string(), rsa_context::N, rsa_context::P, rsa_context::Q, rsa_context::QP, rsa_check_privkey(), rsa_free(), rsa_init(), RSA_PKCS_V15, and TEST_ASSERT.
Referenced by dispatch_test().
void test_suite_rsa_check_privkey_null | ( | ) |
Definition at line 843 of file test_suite_rsa.c.
References POLARSSL_ERR_RSA_KEY_CHECK_FAILED, rsa_check_privkey(), and TEST_ASSERT.
Referenced by dispatch_test().
void test_suite_rsa_check_pubkey | ( | int | radix_N, | |
char * | input_N, | |||
int | radix_E, | |||
char * | input_E, | |||
int | result | |||
) |
Definition at line 851 of file test_suite_rsa.c.
References rsa_context::E, mpi_read_string(), rsa_context::N, rsa_check_pubkey(), rsa_free(), rsa_init(), RSA_PKCS_V15, and TEST_ASSERT.
Referenced by dispatch_test().
void test_suite_rsa_pkcs1_decrypt | ( | char * | message_hex_string, | |
int | padding_mode, | |||
int | mod, | |||
int | radix_P, | |||
char * | input_P, | |||
int | radix_Q, | |||
char * | input_Q, | |||
int | radix_N, | |||
char * | input_N, | |||
int | radix_E, | |||
char * | input_E, | |||
int | max_output, | |||
char * | result_hex_str, | |||
int | result | |||
) |
Definition at line 701 of file test_suite_rsa.c.
References rsa_context::D, rsa_context::DP, rsa_context::DQ, rsa_context::E, hexify(), rsa_context::len, mpi_free(), mpi_gcd(), mpi_init(), mpi_inv_mod(), mpi_mod_mpi(), mpi_mul_mpi(), mpi_read_string(), mpi_sub_int(), rsa_context::N, rsa_context::P, rsa_context::Q, rsa_context::QP, rnd_pseudo_rand(), rsa_check_privkey(), rsa_free(), rsa_init(), rsa_pkcs1_decrypt(), RSA_PRIVATE, TEST_ASSERT, and unhexify().
Referenced by dispatch_test().
void test_suite_rsa_pkcs1_encrypt | ( | char * | message_hex_string, | |
int | padding_mode, | |||
int | mod, | |||
int | radix_N, | |||
char * | input_N, | |||
int | radix_E, | |||
char * | input_E, | |||
char * | result_hex_str, | |||
int | result | |||
) |
Definition at line 629 of file test_suite_rsa.c.
References rsa_context::E, hexify(), rsa_context::len, mpi_read_string(), rsa_context::N, rnd_pseudo_rand(), rsa_check_pubkey(), rsa_free(), rsa_init(), rsa_pkcs1_encrypt(), RSA_PUBLIC, TEST_ASSERT, and unhexify().
Referenced by dispatch_test().
void test_suite_rsa_pkcs1_encrypt_bad_rng | ( | char * | message_hex_string, | |
int | padding_mode, | |||
int | mod, | |||
int | radix_N, | |||
char * | input_N, | |||
int | radix_E, | |||
char * | input_E, | |||
char * | result_hex_str, | |||
int | result | |||
) |
Definition at line 666 of file test_suite_rsa.c.
References rsa_context::E, hexify(), rsa_context::len, mpi_read_string(), rsa_context::N, rnd_zero_rand(), rsa_check_pubkey(), rsa_free(), rsa_init(), rsa_pkcs1_encrypt(), RSA_PUBLIC, TEST_ASSERT, and unhexify().
Referenced by dispatch_test().
void test_suite_rsa_pkcs1_sign | ( | char * | message_hex_string, | |
int | padding_mode, | |||
int | digest, | |||
int | mod, | |||
int | radix_P, | |||
char * | input_P, | |||
int | radix_Q, | |||
char * | input_Q, | |||
int | radix_N, | |||
char * | input_N, | |||
int | radix_E, | |||
char * | input_E, | |||
char * | result_hex_str, | |||
int | result | |||
) |
Definition at line 455 of file test_suite_rsa.c.
References rsa_context::D, rsa_context::DP, rsa_context::DQ, rsa_context::E, hexify(), rsa_context::len, md(), md_info_from_type(), mpi_free(), mpi_gcd(), mpi_init(), mpi_inv_mod(), mpi_mod_mpi(), mpi_mul_mpi(), mpi_read_string(), mpi_sub_int(), rsa_context::N, rsa_context::P, rsa_context::Q, rsa_context::QP, rnd_pseudo_rand(), rsa_check_privkey(), rsa_free(), rsa_init(), rsa_pkcs1_sign(), RSA_PRIVATE, TEST_ASSERT, and unhexify().
Referenced by dispatch_test().
void test_suite_rsa_pkcs1_sign_raw | ( | char * | message_hex_string, | |
char * | hash_result_string, | |||
int | padding_mode, | |||
int | mod, | |||
int | radix_P, | |||
char * | input_P, | |||
int | radix_Q, | |||
char * | input_Q, | |||
int | radix_N, | |||
char * | input_N, | |||
int | radix_E, | |||
char * | input_E, | |||
char * | result_hex_str | |||
) |
Definition at line 544 of file test_suite_rsa.c.
References rsa_context::D, rsa_context::DP, rsa_context::DQ, rsa_context::E, hexify(), rsa_context::len, mpi_free(), mpi_gcd(), mpi_init(), mpi_inv_mod(), mpi_mod_mpi(), mpi_mul_mpi(), mpi_read_string(), mpi_sub_int(), rsa_context::N, rsa_context::P, POLARSSL_MD_NONE, rsa_context::Q, rsa_context::QP, rnd_pseudo_rand(), rsa_check_privkey(), rsa_free(), rsa_init(), rsa_pkcs1_sign(), RSA_PRIVATE, TEST_ASSERT, and unhexify().
Referenced by dispatch_test().
void test_suite_rsa_pkcs1_verify | ( | char * | message_hex_string, | |
int | padding_mode, | |||
int | digest, | |||
int | mod, | |||
int | radix_N, | |||
char * | input_N, | |||
int | radix_E, | |||
char * | input_E, | |||
char * | result_hex_str, | |||
int | result | |||
) |
Definition at line 512 of file test_suite_rsa.c.
References rsa_context::E, rsa_context::len, md(), md_info_from_type(), mpi_read_string(), rsa_context::N, rsa_check_pubkey(), rsa_free(), rsa_init(), rsa_pkcs1_verify(), RSA_PUBLIC, TEST_ASSERT, and unhexify().
Referenced by dispatch_test().
void test_suite_rsa_pkcs1_verify_raw | ( | char * | message_hex_string, | |
char * | hash_result_string, | |||
int | padding_mode, | |||
int | mod, | |||
int | radix_N, | |||
char * | input_N, | |||
int | radix_E, | |||
char * | input_E, | |||
char * | result_hex_str, | |||
int | correct | |||
) |
Definition at line 598 of file test_suite_rsa.c.
References rsa_context::E, rsa_context::len, mpi_read_string(), rsa_context::N, POLARSSL_MD_NONE, rsa_check_pubkey(), rsa_free(), rsa_init(), rsa_pkcs1_verify(), RSA_PUBLIC, TEST_ASSERT, and unhexify().
Referenced by dispatch_test().
void test_suite_rsa_private | ( | char * | message_hex_string, | |
int | mod, | |||
int | radix_P, | |||
char * | input_P, | |||
int | radix_Q, | |||
char * | input_Q, | |||
int | radix_N, | |||
char * | input_N, | |||
int | radix_E, | |||
char * | input_E, | |||
char * | result_hex_str, | |||
int | result | |||
) |
Definition at line 786 of file test_suite_rsa.c.
References rsa_context::D, rsa_context::DP, rsa_context::DQ, rsa_context::E, hexify(), rsa_context::len, mpi_free(), mpi_gcd(), mpi_init(), mpi_inv_mod(), mpi_mod_mpi(), mpi_mul_mpi(), mpi_read_string(), mpi_sub_int(), rsa_context::N, rsa_context::P, rsa_context::Q, rsa_context::QP, rnd_pseudo_rand(), rsa_check_privkey(), rsa_free(), rsa_init(), RSA_PKCS_V15, rsa_private(), TEST_ASSERT, and unhexify().
Referenced by dispatch_test().
void test_suite_rsa_public | ( | char * | message_hex_string, | |
int | mod, | |||
int | radix_N, | |||
char * | input_N, | |||
int | radix_E, | |||
char * | input_E, | |||
char * | result_hex_str, | |||
int | result | |||
) |
Definition at line 754 of file test_suite_rsa.c.
References rsa_context::E, hexify(), rsa_context::len, mpi_read_string(), rsa_context::N, rsa_check_pubkey(), rsa_free(), rsa_init(), RSA_PKCS_V15, rsa_public(), TEST_ASSERT, and unhexify().
Referenced by dispatch_test().
void test_suite_rsa_selftest | ( | ) |
Definition at line 950 of file test_suite_rsa.c.
References rsa_self_test(), and TEST_ASSERT.
Referenced by dispatch_test().
static int unhexify | ( | unsigned char * | obuf, | |
const char * | ibuf | |||
) | [static] |
Definition at line 67 of file test_suite_rsa.c.
Referenced by test_suite_rsa_pkcs1_decrypt(), test_suite_rsa_pkcs1_encrypt(), test_suite_rsa_pkcs1_encrypt_bad_rng(), test_suite_rsa_pkcs1_sign(), test_suite_rsa_pkcs1_sign_raw(), test_suite_rsa_pkcs1_verify(), test_suite_rsa_pkcs1_verify_raw(), test_suite_rsa_private(), and test_suite_rsa_public().
int verify_int | ( | char * | str, | |
int * | value | |||
) |
Definition at line 335 of file test_suite_rsa.c.
References POLARSSL_ERR_RSA_BAD_INPUT_DATA, POLARSSL_ERR_RSA_INVALID_PADDING, POLARSSL_ERR_RSA_KEY_CHECK_FAILED, POLARSSL_ERR_RSA_OUTPUT_TOO_LARGE, POLARSSL_ERR_RSA_RNG_FAILED, POLARSSL_ERR_RSA_VERIFY_FAILED, POLARSSL_MD_MD2, POLARSSL_MD_MD4, POLARSSL_MD_MD5, POLARSSL_MD_SHA1, POLARSSL_MD_SHA224, POLARSSL_MD_SHA256, POLARSSL_MD_SHA384, POLARSSL_MD_SHA512, and RSA_PKCS_V15.
int verify_string | ( | char ** | str | ) |
Definition at line 320 of file test_suite_rsa.c.
int test_errors = 0 [static] |
Definition at line 294 of file test_suite_rsa.c.
Referenced by main(), and test_assert().