summaryrefslogtreecommitdiffhomepage
path: root/libtomcrypt/src/pk/asn1/der/integer
diff options
context:
space:
mode:
Diffstat (limited to 'libtomcrypt/src/pk/asn1/der/integer')
-rw-r--r--libtomcrypt/src/pk/asn1/der/integer/der_decode_integer.c22
-rw-r--r--libtomcrypt/src/pk/asn1/der/integer/der_encode_integer.c54
-rw-r--r--libtomcrypt/src/pk/asn1/der/integer/der_length_integer.c15
3 files changed, 47 insertions, 44 deletions
diff --git a/libtomcrypt/src/pk/asn1/der/integer/der_decode_integer.c b/libtomcrypt/src/pk/asn1/der/integer/der_decode_integer.c
index e68b2c9..aef87a3 100644
--- a/libtomcrypt/src/pk/asn1/der/integer/der_decode_integer.c
+++ b/libtomcrypt/src/pk/asn1/der/integer/der_decode_integer.c
@@ -6,7 +6,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@gmail.com, http://libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://libtomcrypt.com
*/
#include "tomcrypt.h"
@@ -25,7 +25,7 @@
@param num The first mp_int to decode
@return CRYPT_OK if successful
*/
-int der_decode_integer(const unsigned char *in, unsigned long inlen, mp_int *num)
+int der_decode_integer(const unsigned char *in, unsigned long inlen, void *num)
{
unsigned long x, y, z;
int err;
@@ -56,7 +56,7 @@ int der_decode_integer(const unsigned char *in, unsigned long inlen, mp_int *num
}
/* no so read it */
- if ((err = mpi_to_ltc_error(mp_read_unsigned_bin(num, (unsigned char *)in + x, z))) != CRYPT_OK) {
+ if ((err = mp_read_unsigned_bin(num, (unsigned char *)in + x, z)) != CRYPT_OK) {
return err;
}
} else {
@@ -80,23 +80,23 @@ int der_decode_integer(const unsigned char *in, unsigned long inlen, mp_int *num
}
/* no so read it */
- if ((err = mpi_to_ltc_error(mp_read_unsigned_bin(num, (unsigned char *)in + x, y))) != CRYPT_OK) {
+ if ((err = mp_read_unsigned_bin(num, (unsigned char *)in + x, y)) != CRYPT_OK) {
return err;
}
}
/* see if it's negative */
if (in[x] & 0x80) {
- mp_int tmp;
- if (mp_init(&tmp) != MP_OKAY) {
+ void *tmp;
+ if (mp_init(&tmp) != CRYPT_OK) {
return CRYPT_MEM;
}
- if (mp_2expt(&tmp, mp_count_bits(num)) != MP_OKAY || mp_sub(num, &tmp, num) != MP_OKAY) {
- mp_clear(&tmp);
+ if (mp_2expt(tmp, mp_count_bits(num)) != CRYPT_OK || mp_sub(num, tmp, num) != CRYPT_OK) {
+ mp_clear(tmp);
return CRYPT_MEM;
}
- mp_clear(&tmp);
+ mp_clear(tmp);
}
return CRYPT_OK;
@@ -106,5 +106,5 @@ int der_decode_integer(const unsigned char *in, unsigned long inlen, mp_int *num
#endif
/* $Source: /cvs/libtom/libtomcrypt/src/pk/asn1/der/integer/der_decode_integer.c,v $ */
-/* $Revision: 1.2 $ */
-/* $Date: 2005/06/01 00:06:05 $ */
+/* $Revision: 1.4 $ */
+/* $Date: 2006/03/31 14:15:35 $ */
diff --git a/libtomcrypt/src/pk/asn1/der/integer/der_encode_integer.c b/libtomcrypt/src/pk/asn1/der/integer/der_encode_integer.c
index f0f41be..ff4fce6 100644
--- a/libtomcrypt/src/pk/asn1/der/integer/der_encode_integer.c
+++ b/libtomcrypt/src/pk/asn1/der/integer/der_encode_integer.c
@@ -6,7 +6,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@gmail.com, http://libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://libtomcrypt.com
*/
#include "tomcrypt.h"
@@ -26,7 +26,7 @@
@param outlen [in/out] The max size and resulting size of the DER encoded integers
@return CRYPT_OK if successful
*/
-int der_encode_integer(mp_int *num, unsigned char *out, unsigned long *outlen)
+int der_encode_integer(void *num, unsigned char *out, unsigned long *outlen)
{
unsigned long tmplen, y;
int err, leading_zero;
@@ -41,12 +41,13 @@ int der_encode_integer(mp_int *num, unsigned char *out, unsigned long *outlen)
}
if (*outlen < tmplen) {
+ *outlen = tmplen;
return CRYPT_BUFFER_OVERFLOW;
}
- if (mp_cmp_d(num, 0) != MP_LT) {
+ if (mp_cmp_d(num, 0) != LTC_MP_LT) {
/* we only need a leading zero if the msb of the first byte is one */
- if ((mp_count_bits(num) & 7) == 0 || mp_iszero(num) == MP_YES) {
+ if ((mp_count_bits(num) & 7) == 0 || mp_iszero(num) == LTC_MP_YES) {
leading_zero = 1;
} else {
leading_zero = 0;
@@ -59,7 +60,7 @@ int der_encode_integer(mp_int *num, unsigned char *out, unsigned long *outlen)
y = mp_count_bits(num);
y = y + (8 - (y & 7));
y = y >> 3;
-
+ if (((mp_cnt_lsb(num)+1)==mp_count_bits(num)) && ((mp_count_bits(num)&7)==0)) --y;
}
/* now store initial data */
@@ -69,16 +70,16 @@ int der_encode_integer(mp_int *num, unsigned char *out, unsigned long *outlen)
*out++ = (unsigned char)y;
} else if (y < 256) {
*out++ = 0x81;
- *out++ = y;
+ *out++ = (unsigned char)y;
} else if (y < 65536UL) {
*out++ = 0x82;
- *out++ = (y>>8)&255;
- *out++ = y;
+ *out++ = (unsigned char)((y>>8)&255);
+ *out++ = (unsigned char)y;
} else if (y < 16777216UL) {
*out++ = 0x83;
- *out++ = (y>>16)&255;
- *out++ = (y>>8)&255;
- *out++ = y;
+ *out++ = (unsigned char)((y>>16)&255);
+ *out++ = (unsigned char)((y>>8)&255);
+ *out++ = (unsigned char)y;
} else {
return CRYPT_INVALID_ARG;
}
@@ -89,31 +90,32 @@ int der_encode_integer(mp_int *num, unsigned char *out, unsigned long *outlen)
}
/* if it's not zero store it as big endian */
- if (mp_cmp_d(num, 0) == MP_GT) {
+ if (mp_cmp_d(num, 0) == LTC_MP_GT) {
/* now store the mpint */
- if ((err = mp_to_unsigned_bin(num, out)) != MP_OKAY) {
- return mpi_to_ltc_error(err);
+ if ((err = mp_to_unsigned_bin(num, out)) != CRYPT_OK) {
+ return err;
}
- } else if (mp_iszero(num) != MP_YES) {
- mp_int tmp;
+ } else if (mp_iszero(num) != LTC_MP_YES) {
+ void *tmp;
+
/* negative */
- if (mp_init(&tmp) != MP_OKAY) {
+ if (mp_init(&tmp) != CRYPT_OK) {
return CRYPT_MEM;
}
/* 2^roundup and subtract */
y = mp_count_bits(num);
y = y + (8 - (y & 7));
- if (mp_2expt(&tmp, y) != MP_OKAY || mp_add(&tmp, num, &tmp) != MP_OKAY) {
- mp_clear(&tmp);
+ if (((mp_cnt_lsb(num)+1)==mp_count_bits(num)) && ((mp_count_bits(num)&7)==0)) y -= 8;
+ if (mp_2expt(tmp, y) != CRYPT_OK || mp_add(tmp, num, tmp) != CRYPT_OK) {
+ mp_clear(tmp);
return CRYPT_MEM;
}
-
- if ((err = mp_to_unsigned_bin(&tmp, out)) != MP_OKAY) {
- mp_clear(&tmp);
- return mpi_to_ltc_error(err);
+ if ((err = mp_to_unsigned_bin(tmp, out)) != CRYPT_OK) {
+ mp_clear(tmp);
+ return err;
}
- mp_clear(&tmp);
+ mp_clear(tmp);
}
/* we good */
@@ -124,5 +126,5 @@ int der_encode_integer(mp_int *num, unsigned char *out, unsigned long *outlen)
#endif
/* $Source: /cvs/libtom/libtomcrypt/src/pk/asn1/der/integer/der_encode_integer.c,v $ */
-/* $Revision: 1.1 $ */
-/* $Date: 2005/05/16 15:08:11 $ */
+/* $Revision: 1.8 $ */
+/* $Date: 2006/12/04 21:34:03 $ */
diff --git a/libtomcrypt/src/pk/asn1/der/integer/der_length_integer.c b/libtomcrypt/src/pk/asn1/der/integer/der_length_integer.c
index 1bfee45..bcc331d 100644
--- a/libtomcrypt/src/pk/asn1/der/integer/der_length_integer.c
+++ b/libtomcrypt/src/pk/asn1/der/integer/der_length_integer.c
@@ -6,7 +6,7 @@
* The library is free for all purposes without any express
* guarantee it works.
*
- * Tom St Denis, tomstdenis@gmail.com, http://libtomcrypt.org
+ * Tom St Denis, tomstdenis@gmail.com, http://libtomcrypt.com
*/
#include "tomcrypt.h"
@@ -19,11 +19,11 @@
#ifdef LTC_DER
/**
Gets length of DER encoding of num
- @param num The mp_int to get the size of
+ @param num The int to get the size of
@param outlen [out] The length of the DER encoding for the given integer
@return CRYPT_OK if successful
*/
-int der_length_integer(mp_int *num, unsigned long *outlen)
+int der_length_integer(void *num, unsigned long *outlen)
{
unsigned long z, len;
int leading_zero;
@@ -31,11 +31,11 @@ int der_length_integer(mp_int *num, unsigned long *outlen)
LTC_ARGCHK(num != NULL);
LTC_ARGCHK(outlen != NULL);
- if (mp_cmp_d(num, 0) != MP_LT) {
+ if (mp_cmp_d(num, 0) != LTC_MP_LT) {
/* positive */
/* we only need a leading zero if the msb of the first byte is one */
- if ((mp_count_bits(num) & 7) == 0 || mp_iszero(num) == MP_YES) {
+ if ((mp_count_bits(num) & 7) == 0 || mp_iszero(num) == LTC_MP_YES) {
leading_zero = 1;
} else {
leading_zero = 0;
@@ -49,6 +49,7 @@ int der_length_integer(mp_int *num, unsigned long *outlen)
leading_zero = 0;
z = mp_count_bits(num);
z = z + (8 - (z & 7));
+ if (((mp_cnt_lsb(num)+1)==mp_count_bits(num)) && ((mp_count_bits(num)&7)==0)) --z;
len = z = z >> 3;
}
@@ -77,5 +78,5 @@ int der_length_integer(mp_int *num, unsigned long *outlen)
#endif
/* $Source: /cvs/libtom/libtomcrypt/src/pk/asn1/der/integer/der_length_integer.c,v $ */
-/* $Revision: 1.1 $ */
-/* $Date: 2005/05/16 15:08:11 $ */
+/* $Revision: 1.4 $ */
+/* $Date: 2006/04/22 01:22:55 $ */