tomcrypt/src/pk/asn1/der/integer/der_encode_integer.c

131 lines
3.3 KiB
C
Raw Normal View History

2004-10-30 03:00:26 +00:00
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
*
* LibTomCrypt is a library that provides various cryptographic
* algorithms in a highly modular and flexible manner.
*
* The library is free for all purposes without any express
* guarantee it works.
*
2006-04-06 19:48:32 +00:00
* Tom St Denis, tomstdenis@gmail.com, http://libtomcrypt.com
2004-10-30 03:00:26 +00:00
*/
2004-12-30 23:55:53 +00:00
#include "tomcrypt.h"
2004-10-30 03:00:26 +00:00
2004-12-30 23:55:53 +00:00
/**
@file der_encode_integer.c
ASN.1 DER, encode an integer, Tom St Denis
*/
#ifdef LTC_DER
2004-10-30 03:00:26 +00:00
/* Exports a positive bignum as DER format (upto 2^32 bytes in size) */
2004-12-30 23:55:53 +00:00
/**
Store a mp_int integer
@param num The first mp_int to encode
@param out [out] The destination for the DER encoded integers
@param outlen [in/out] The max size and resulting size of the DER encoded integers
@return CRYPT_OK if successful
*/
2005-08-01 16:36:47 +00:00
int der_encode_integer(void *num, unsigned char *out, unsigned long *outlen)
2004-10-30 03:00:26 +00:00
{
2005-06-09 00:08:13 +00:00
unsigned long tmplen, y;
2004-10-30 03:00:26 +00:00
int err, leading_zero;
2004-12-30 23:55:53 +00:00
LTC_ARGCHK(num != NULL);
LTC_ARGCHK(out != NULL);
LTC_ARGCHK(outlen != NULL);
2004-10-30 03:00:26 +00:00
/* find out how big this will be */
if ((err = der_length_integer(num, &tmplen)) != CRYPT_OK) {
return err;
}
if (*outlen < tmplen) {
2006-06-18 01:37:50 +00:00
*outlen = tmplen;
2004-10-30 03:00:26 +00:00
return CRYPT_BUFFER_OVERFLOW;
}
2005-08-01 16:36:47 +00:00
if (mp_cmp_d(num, 0) != LTC_MP_LT) {
2005-06-09 00:08:13 +00:00
/* we only need a leading zero if the msb of the first byte is one */
2005-08-01 16:36:47 +00:00
if ((mp_count_bits(num) & 7) == 0 || mp_iszero(num) == LTC_MP_YES) {
2005-06-09 00:08:13 +00:00
leading_zero = 1;
} else {
leading_zero = 0;
}
/* get length of num in bytes (plus 1 since we force the msbyte to zero) */
y = mp_unsigned_bin_size(num) + leading_zero;
2004-10-30 03:00:26 +00:00
} else {
leading_zero = 0;
2005-06-09 00:08:13 +00:00
y = mp_count_bits(num);
y = y + (8 - (y & 7));
y = y >> 3;
2006-05-29 23:12:56 +00:00
if (((mp_cnt_lsb(num)+1)==mp_count_bits(num)) && ((mp_count_bits(num)&7)==0)) --y;
2005-06-09 00:08:13 +00:00
}
2004-10-30 03:00:26 +00:00
/* now store initial data */
*out++ = 0x02;
if (y < 128) {
/* short form */
*out++ = (unsigned char)y;
2005-06-09 00:08:13 +00:00
} else if (y < 256) {
*out++ = 0x81;
*out++ = y;
} else if (y < 65536UL) {
*out++ = 0x82;
*out++ = (y>>8)&255;
*out++ = y;
} else if (y < 16777216UL) {
*out++ = 0x83;
*out++ = (y>>16)&255;
*out++ = (y>>8)&255;
*out++ = y;
2004-10-30 03:00:26 +00:00
} else {
2005-06-09 00:08:13 +00:00
return CRYPT_INVALID_ARG;
2004-10-30 03:00:26 +00:00
}
/* now store msbyte of zero if num is non-zero */
if (leading_zero) {
*out++ = 0x00;
}
/* if it's not zero store it as big endian */
2005-08-01 16:36:47 +00:00
if (mp_cmp_d(num, 0) == LTC_MP_GT) {
2004-10-30 03:00:26 +00:00
/* now store the mpint */
2005-08-01 16:36:47 +00:00
if ((err = mp_to_unsigned_bin(num, out)) != CRYPT_OK) {
return err;
2004-10-30 03:00:26 +00:00
}
2005-08-01 16:36:47 +00:00
} else if (mp_iszero(num) != LTC_MP_YES) {
void *tmp;
2006-04-06 19:48:32 +00:00
2005-06-09 00:08:13 +00:00
/* negative */
2005-08-01 16:36:47 +00:00
if (mp_init(&tmp) != CRYPT_OK) {
2005-06-09 00:08:13 +00:00
return CRYPT_MEM;
}
/* 2^roundup and subtract */
y = mp_count_bits(num);
y = y + (8 - (y & 7));
2006-05-29 23:12:56 +00:00
if (((mp_cnt_lsb(num)+1)==mp_count_bits(num)) && ((mp_count_bits(num)&7)==0)) y -= 8;
2005-08-01 16:36:47 +00:00
if (mp_2expt(tmp, y) != CRYPT_OK || mp_add(tmp, num, tmp) != CRYPT_OK) {
mp_clear(tmp);
2005-06-09 00:08:13 +00:00
return CRYPT_MEM;
}
2005-08-01 16:36:47 +00:00
if ((err = mp_to_unsigned_bin(tmp, out)) != CRYPT_OK) {
mp_clear(tmp);
return err;
2005-06-09 00:08:13 +00:00
}
2005-08-01 16:36:47 +00:00
mp_clear(tmp);
2004-10-30 03:00:26 +00:00
}
/* we good */
*outlen = tmplen;
return CRYPT_OK;
}
2004-12-30 23:55:53 +00:00
#endif
2005-06-09 00:08:13 +00:00
/* $Source$ */
/* $Revision$ */
/* $Date$ */