Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions bn_deprecated.c
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,9 @@ mp_err mp_toom_sqr(const mp_int *a, mp_int *b)
#ifdef S_MP_REVERSE_C
void bn_reverse(unsigned char *s, int len)
{
if (len < 0) {
return MP_VAL;
if (len > 0) {
s_mp_reverse(s, (size_t)len);
}
s_mp_reverse(s, (size_t)len);
}
#endif
#ifdef BN_MP_TC_AND_C
Expand Down Expand Up @@ -316,4 +315,18 @@ mp_err mp_toradix(const mp_int *a, char *str, int radix)
return mp_to_radix(a, str, SIZE_MAX, NULL, radix);
}
#endif
#ifdef BN_MP_IMPORT_C
mp_err mp_import(mp_int *rop, size_t count, int order, size_t size, int endian, size_t nails,
const void *op)
{
return mp_unpack(rop, count, order, size, endian, nails, op);
}
#endif
#ifdef BN_MP_EXPORT_C
mp_err mp_export(void *rop, size_t *countp, int order, size_t size,
int endian, size_t nails, const mp_int *op)
{
return mp_pack(rop, SIZE_MAX, countp, order, size, endian, nails, op);
}
#endif
#endif
42 changes: 20 additions & 22 deletions bn_mp_export.c → bn_mp_pack.c
Original file line number Diff line number Diff line change
@@ -1,49 +1,46 @@
#include "tommath_private.h"
#ifdef BN_MP_EXPORT_C
#ifdef BN_MP_PACK_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis */
/* SPDX-License-Identifier: Unlicense */

/* based on gmp's mpz_export.
* see http://gmplib.org/manual/Integer-Import-and-Export.html
*/
mp_err mp_export(void *rop, size_t *countp, int order, size_t size,
int endian, size_t nails, const mp_int *op)
mp_err mp_pack(void *rop, size_t maxcount, size_t *written, mp_order order, size_t size,
mp_endian endian, size_t nails, const mp_int *op)
{
mp_err err;
size_t odd_nails, nail_bytes, i, j, bits, count;
size_t odd_nails, nail_bytes, i, j, count;
unsigned char odd_nail_mask;

mp_int t;

count = mp_pack_count(op, nails, size);

if (count > maxcount) {
return MP_BUF;
}

if ((err = mp_init_copy(&t, op)) != MP_OKAY) {
return err;
}

if (endian == 0) {
union {
unsigned int i;
char c[4];
} lint;
lint.i = 0x01020304;

endian = (lint.c[0] == '\x04') ? -1 : 1;
if (endian == MP_NATIVE_ENDIAN) {
MP_GET_ENDIANNESS(endian);
}

odd_nails = (nails % 8u);
odd_nail_mask = 0xff;
for (i = 0; i < odd_nails; ++i) {
for (i = 0u; i < odd_nails; ++i) {
odd_nail_mask ^= (unsigned char)(1u << (7u - i));
}
nail_bytes = nails / 8u;

bits = (size_t)mp_count_bits(&t);
count = (bits / ((size * 8u) - nails)) + (((bits % ((size * 8u) - nails)) != 0u) ? 1u : 0u);

for (i = 0; i < count; ++i) {
for (j = 0; j < size; ++j) {
for (i = 0u; i < count; ++i) {
for (j = 0u; j < size; ++j) {
unsigned char *byte = (unsigned char *)rop +
(((order == -1) ? i : ((count - 1u) - i)) * size) +
((endian == -1) ? j : ((size - 1u) - j));
(((order == MP_LSB_FIRST) ? i : ((count - 1u) - i)) * size) +
((endian == MP_LITTLE_ENDIAN) ? j : ((size - 1u) - j));

if (j >= (size - nail_bytes)) {
*byte = 0;
Expand All @@ -55,11 +52,12 @@ mp_err mp_export(void *rop, size_t *countp, int order, size_t size,
if ((err = mp_div_2d(&t, (j == ((size - nail_bytes) - 1u)) ? (int)(8u - odd_nails) : 8, &t, NULL)) != MP_OKAY) {
goto LBL_ERR;
}

}
}

if (countp != NULL) {
*countp = count;
if (written != NULL) {
*written = count;
}
err = MP_OKAY;

Expand Down
12 changes: 12 additions & 0 deletions bn_mp_pack_count.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#include "tommath_private.h"
#ifdef BN_MP_PACK_COUNT_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis */
/* SPDX-License-Identifier: Unlicense */

size_t mp_pack_count(const mp_int *a, size_t nails, size_t size)
{
size_t bits = (size_t)mp_count_bits(a);
return ((bits / ((size * 8u) - nails)) + (((bits % ((size * 8u) - nails)) != 0u) ? 1u : 0u));
}

#endif
18 changes: 7 additions & 11 deletions bn_mp_to_radix.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,11 @@ mp_err mp_to_radix(const mp_int *a, char *str, size_t maxlen, size_t *written, i
mp_digit d;
char *_s = str;


/* If we want to fill a bucket we need a bucket in the first place. */
if (str == NULL) {
return MP_VAL;
}

/* check range of radix and size*/
if ((maxlen < 2u) || (radix < 2) || (radix > 64)) {
if (maxlen < 2u) {
return MP_BUF;
}
if ((radix < 2) || (radix > 64)) {
return MP_VAL;
}

Expand Down Expand Up @@ -57,10 +54,8 @@ mp_err mp_to_radix(const mp_int *a, char *str, size_t maxlen, size_t *written, i
while (!MP_IS_ZERO(&t)) {
if (--maxlen < 1u) {
/* no more room */
/* TODO: It could mimic mp_to_radix_n if that is not an error
or at least not this error (MP_ITER or a new one?). */
err = MP_VAL;
break;
err = MP_BUF;
goto LBL_ERR;
}
if ((err = mp_div_d(&t, (mp_digit)radix, &t, &d)) != MP_OKAY) {
goto LBL_ERR;
Expand All @@ -76,6 +71,7 @@ mp_err mp_to_radix(const mp_int *a, char *str, size_t maxlen, size_t *written, i
/* append a NULL so the string is properly terminated */
*str = '\0';
digs++;

if (written != NULL) {
*written = (a->sign == MP_NEG) ? (digs + 1u): digs;
}
Expand Down
3 changes: 1 addition & 2 deletions bn_mp_to_sbin.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@ mp_err mp_to_sbin(const mp_int *a, unsigned char *buf, size_t maxlen, size_t *wr
{
mp_err err;
if (maxlen == 0u) {
return MP_VAL;
return MP_BUF;
}

if ((err = mp_to_ubin(a, buf + 1, maxlen - 1u, written)) != MP_OKAY) {
return err;
}
Expand Down
27 changes: 9 additions & 18 deletions bn_mp_to_ubin.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,43 +6,34 @@
/* store in unsigned [big endian] format */
mp_err mp_to_ubin(const mp_int *a, unsigned char *buf, size_t maxlen, size_t *written)
{
size_t x;
size_t x, count;
mp_err err;
mp_int t;

if (buf == NULL) {
return MP_MEM;
}

if (maxlen == 0u) {
return MP_VAL;
count = mp_ubin_size(a);
if (count > maxlen) {
return MP_BUF;
}

if ((err = mp_init_copy(&t, a)) != MP_OKAY) {
return err;
}

x = 0u;
while (!MP_IS_ZERO(&t)) {
if (maxlen == 0u) {
err = MP_VAL;
goto LBL_ERR;
}
maxlen--;
for (x = count; x --> 0;) {
#ifndef MP_8BIT
buf[x++] = (unsigned char)(t.dp[0] & 255u);
buf[x] = (unsigned char)(t.dp[0] & 255u);
#else
buf[x++] = (unsigned char)(t.dp[0] | ((t.dp[1] & 1u) << 7));
buf[x] = (unsigned char)(t.dp[0] | ((t.dp[1] & 1u) << 7));
#endif
if ((err = mp_div_2d(&t, 8, &t, NULL)) != MP_OKAY) {
goto LBL_ERR;
}
}
s_mp_reverse(buf, x);

if (written != NULL) {
*written = x;
*written = count;
}

LBL_ERR:
mp_clear(&t);
return err;
Expand Down
20 changes: 7 additions & 13 deletions bn_mp_import.c → bn_mp_unpack.c
Original file line number Diff line number Diff line change
@@ -1,28 +1,22 @@
#include "tommath_private.h"
#ifdef BN_MP_IMPORT_C
#ifdef BN_MP_UNPACK_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis */
/* SPDX-License-Identifier: Unlicense */

/* based on gmp's mpz_import.
* see http://gmplib.org/manual/Integer-Import-and-Export.html
*/
mp_err mp_import(mp_int *rop, size_t count, int order, size_t size,
int endian, size_t nails, const void *op)
mp_err mp_unpack(mp_int *rop, size_t count, mp_order order, size_t size,
mp_endian endian, size_t nails, const void *op)
{
mp_err err;
size_t odd_nails, nail_bytes, i, j;
unsigned char odd_nail_mask;

mp_zero(rop);

if (endian == 0) {
union {
unsigned int i;
char c[4];
} lint;
lint.i = 0x01020304;

endian = (lint.c[0] == '\x04') ? -1 : 1;
if (endian == MP_NATIVE_ENDIAN) {
MP_GET_ENDIANNESS(endian);
}

odd_nails = (nails % 8u);
Expand All @@ -35,8 +29,8 @@ mp_err mp_import(mp_int *rop, size_t count, int order, size_t size,
for (i = 0; i < count; ++i) {
for (j = 0; j < (size - nail_bytes); ++j) {
unsigned char byte = *((const unsigned char *)op +
(((order == 1) ? i : ((count - 1u) - i)) * size) +
((endian == 1) ? (j + nail_bytes) : (((size - 1u) - j) - nail_bytes)));
(((order == MP_MSB_FIRST) ? i : ((count - 1u) - i)) * size) +
((endian == MP_BIG_ENDIAN) ? (j + nail_bytes) : (((size - 1u) - j) - nail_bytes)));

if ((err = mp_mul_2d(rop, (j == 0u) ? (int)(8u - odd_nails) : 8, rop)) != MP_OKAY) {
return err;
Expand Down
55 changes: 45 additions & 10 deletions demo/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -1260,8 +1260,6 @@ static int test_mp_read_radix(void)
if (mp_init_multi(&a, NULL)!= MP_OKAY) goto LTM_ERR;

if ((err = mp_read_radix(&a, "123456", 10)) != MP_OKAY) goto LTM_ERR;
/* Must fail */
if ((err = mp_to_radix(&a, NULL, SIZE_MAX, NULL, 10)) != MP_VAL) goto LTM_ERR;

if ((err = mp_to_radix(&a, buf, SIZE_MAX, &written, 10)) != MP_OKAY) goto LTM_ERR;
printf(" '123456' a == %s, length = %zu\n", buf, written);
Expand Down Expand Up @@ -2315,7 +2313,7 @@ static int test_mp_read_write_ubin(void)

size = mp_ubin_size(&a);
printf("mp_to_ubin_size %zu\n", size);
buf = MP_MALLOC(sizeof(*buf) * size);
buf = malloc(sizeof(*buf) * size);
if (buf == NULL) {
fprintf(stderr, "test_read_write_binaries (u) failed to allocate %zu bytes\n",
sizeof(*buf) * size);
Expand All @@ -2335,9 +2333,7 @@ static int test_mp_read_write_ubin(void)
mp_clear_multi(&a, &b, &c, NULL);
return EXIT_SUCCESS;
LTM_ERR:
if (buf != NULL) {
free(buf);
}
free(buf);
mp_clear_multi(&a, &b, &c, NULL);
return EXIT_FAILURE;
}
Expand All @@ -2358,7 +2354,7 @@ static int test_mp_read_write_sbin(void)

size = mp_sbin_size(&a);
printf("mp_to_sbin_size %zu\n", size);
buf = MP_MALLOC(sizeof(*buf) * size);
buf = malloc(sizeof(*buf) * size);
if (buf == NULL) {
fprintf(stderr, "test_read_write_binaries (s) failed to allocate %zu bytes\n",
sizeof(*buf) * size);
Expand All @@ -2379,13 +2375,51 @@ static int test_mp_read_write_sbin(void)
mp_clear_multi(&a, &b, &c, NULL);
return EXIT_SUCCESS;
LTM_ERR:
if (buf != NULL) {
free(buf);
}
free(buf);
mp_clear_multi(&a, &b, &c, NULL);
return EXIT_FAILURE;
}

static int test_mp_pack_unpack(void)
{
mp_int a, b;
int err;
size_t written, count;
unsigned char *buf = NULL;

mp_order order = MP_LSB_FIRST;
mp_endian endianess = MP_NATIVE_ENDIAN;

if ((err = mp_init_multi(&a, &b, NULL)) != MP_OKAY) goto LTM_ERR;
if ((err = mp_rand(&a, 15)) != MP_OKAY) goto LTM_ERR;

count = mp_pack_count(&a, 0, 1);

buf = malloc(count);
if (buf == NULL) {
fprintf(stderr, "test_pack_unpack failed to allocate\n");
goto LTM_ERR;
}

if ((err = mp_pack((void *)buf, count, &written, order, 1,
endianess, 0, &a)) != MP_OKAY) goto LTM_ERR;
if ((err = mp_unpack(&b, count, order, 1,
endianess, 0, (const void *)buf)) != MP_OKAY) goto LTM_ERR;

if (mp_cmp(&a, &b) != MP_EQ) {
fprintf(stderr, "pack/unpack cycle failed\n");
goto LTM_ERR;
}

free(buf);
mp_clear_multi(&a, &b, NULL);
return EXIT_SUCCESS;
LTM_ERR:
free(buf);
mp_clear_multi(&a, &b, NULL);
return EXIT_FAILURE;
}

static int unit_tests(int argc, char **argv)
{
static const struct {
Expand All @@ -2405,6 +2439,7 @@ static int unit_tests(int argc, char **argv)
T1(mp_decr, MP_DECR),
T1(mp_div_3, MP_DIV_3),
T1(mp_dr_reduce, MP_DR_REDUCE),
T2(mp_pack_unpack,MP_PACK, MP_UNPACK),
T2(mp_fread_fwrite, MP_FREAD, MP_FWRITE),
T1(mp_get_u32, MP_GET_I32),
T1(mp_get_u64, MP_GET_I64),
Expand Down
Loading