Files
heimdal/lib/des/test_bn.c

208 lines
4.5 KiB
C

/*
* Copyright (c) 2006 Kungliga Tekniska Högskolan
* (Royal Institute of Technology, Stockholm, Sweden).
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#ifdef RCSID
RCSID("$Id$");
#endif
#include <sys/types.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <bn.h>
static int
set_get(unsigned long num)
{
BIGNUM *bn;
bn = BN_new();
if (!BN_set_word(bn, num))
return 1;
if (BN_get_word(bn) != num)
return 1;
BN_free(bn);
return 0;
}
#define CHECK(x) do { ret += x; } while(0)
static int
test_BN_set_get(void)
{
int ret = 0;
CHECK(set_get(0));
CHECK(set_get(1));
CHECK(set_get(0xff));
CHECK(set_get(0x1ff));
CHECK(set_get(0xffff));
CHECK(set_get(0xf000));
CHECK(set_get(ULONG_MAX / 2));
CHECK(set_get(ULONG_MAX - 1));
return ret;
}
static int
test_BN_bit(void)
{
BIGNUM *bn;
int ret = 0;
bn = BN_new();
/* test setting and getting of "word" */
if (!BN_set_word(bn, 1))
return 1;
if (!BN_is_bit_set(bn, 0))
ret += 1;
if (!BN_is_bit_set(bn, 0))
ret += 1;
if (!BN_set_word(bn, 2))
return 1;
if (!BN_is_bit_set(bn, 1))
ret += 1;
if (!BN_set_word(bn, 3))
return 1;
if (!BN_is_bit_set(bn, 0))
ret += 1;
if (!BN_is_bit_set(bn, 1))
ret += 1;
if (!BN_set_word(bn, 0x100))
return 1;
if (!BN_is_bit_set(bn, 8))
ret += 1;
if (!BN_set_word(bn, 0x1000))
return 1;
if (!BN_is_bit_set(bn, 12))
ret += 1;
/* test bitsetting */
if (!BN_set_word(bn, 1))
return 1;
if (!BN_set_bit(bn, 1))
return 1;
if (BN_get_word(bn) != 3)
return 1;
if (!BN_clear_bit(bn, 0))
return 1;
if (BN_get_word(bn) != 2)
return 1;
/* test bitsetting past end of current end */
BN_clear(bn);
if (!BN_set_bit(bn, 12))
return 1;
if (BN_get_word(bn) != 0x1000)
return 1;
/* test bit and byte counting functions */
if (BN_num_bits(bn) != 13)
return 1;
if (BN_num_bytes(bn) != 2)
return 1;
BN_free(bn);
return ret;
}
struct ietest {
char *data;
size_t len;
unsigned long num;
} ietests[] = {
{ "", 0, 0 },
{ "\x01", 1, 1 },
{ "\x02", 1, 2 },
{ "\xf2", 1, 0xf2 },
{ "\x01\x00", 2, 256 }
};
static int
test_BN_import_export(void)
{
BIGNUM *bn;
int ret = 0;
int i;
bn = BN_new();
for (i = 0; i < sizeof(ietests)/sizeof(ietests[0]); i++) {
size_t len;
unsigned char *p;
if (!BN_bin2bn((unsigned char*)ietests[i].data, ietests[i].len, bn))
return 1;
if (BN_get_word(bn) != ietests[i].num)
return 1;
len = BN_num_bytes(bn);
if (len != ietests[i].len)
return 1;
p = malloc(len + 1);
p[len] = 0xf4;
BN_bn2bin(bn, p);
if (p[len] != 0xf4)
return 1;
if (memcmp(p, ietests[i].data, ietests[i].len) != 0)
return 1;
free(p);
}
BN_free(bn);
return ret;
}
int
main(int argc, char **argv)
{
int ret = 0;
ret += test_BN_set_get();
ret += test_BN_bit();
ret += test_BN_import_export();
return ret;
}