summaryrefslogtreecommitdiffstats
path: root/libtomcrypt/pk/asn1/der/bit/der_encode_bit_string.c
diff options
context:
space:
mode:
Diffstat (limited to 'libtomcrypt/pk/asn1/der/bit/der_encode_bit_string.c')
-rw-r--r--libtomcrypt/pk/asn1/der/bit/der_encode_bit_string.c7
1 files changed, 5 insertions, 2 deletions
diff --git a/libtomcrypt/pk/asn1/der/bit/der_encode_bit_string.c b/libtomcrypt/pk/asn1/der/bit/der_encode_bit_string.c
index d77ea5a..d1b6064 100644
--- a/libtomcrypt/pk/asn1/der/bit/der_encode_bit_string.c
+++ b/libtomcrypt/pk/asn1/der/bit/der_encode_bit_string.c
@@ -18,9 +18,11 @@
#ifdef LTC_DER
+#define getbit(n, k) (((n) & ( 1 << (k) )) >> (k))
+
/**
Store a BIT STRING
- @param in The array of bits to store (one per char)
+ @param in The array of bits to store (8 per char)
@param inlen The number of bits tostore
@param out [out] The destination for the DER encoded BIT STRING
@param outlen [in/out] The max size and resulting size of the DER BIT STRING
@@ -68,7 +70,7 @@ int der_encode_bit_string(const unsigned char *in, unsigned long inlen,
/* store the bits in big endian format */
for (y = buf = 0; y < inlen; y++) {
- buf |= (in[y] ? 1 : 0) << (7 - (y & 7));
+ buf |= (getbit(in[y/8],7-y%8)?1:0) << (7 - (y & 7));
if ((y & 7) == 7) {
out[x++] = buf;
buf = 0;
@@ -78,6 +80,7 @@ int der_encode_bit_string(const unsigned char *in, unsigned long inlen,
if (inlen & 7) {
out[x++] = buf;
}
+
*outlen = x;
return CRYPT_OK;
}