summaryrefslogtreecommitdiffstats
path: root/libtomcrypt/pk/asn1/der/bit
diff options
context:
space:
mode:
authorNikos Mavrogiannopoulos <nmav@gnutls.org>2010-07-19 20:10:26 +0200
committerNikos Mavrogiannopoulos <nmav@gnutls.org>2010-07-19 20:10:26 +0200
commit56eb45f752baa978a9ea3573faee44857678597d (patch)
tree3c7da78ba84669d0f988b98450e4e55779dfcb41 /libtomcrypt/pk/asn1/der/bit
parent035b7bf3544ab9927dc2d64cf1e3214237ff0f71 (diff)
Public Keys are being exported to SubjectPublicKeyInfo format, instead of custom formats. For RSA keys the PKCS #1 format can be used as well.
Diffstat (limited to 'libtomcrypt/pk/asn1/der/bit')
-rw-r--r--libtomcrypt/pk/asn1/der/bit/der_decode_bit_string.c8
-rw-r--r--libtomcrypt/pk/asn1/der/bit/der_encode_bit_string.c7
2 files changed, 11 insertions, 4 deletions
diff --git a/libtomcrypt/pk/asn1/der/bit/der_decode_bit_string.c b/libtomcrypt/pk/asn1/der/bit/der_decode_bit_string.c
index d126df3..c9f6368 100644
--- a/libtomcrypt/pk/asn1/der/bit/der_decode_bit_string.c
+++ b/libtomcrypt/pk/asn1/der/bit/der_decode_bit_string.c
@@ -18,11 +18,13 @@
#ifdef LTC_DER
+#define setbit(v, n) (v=((unsigned char)(v) | (1U << (unsigned char)(n))))
+
/**
Store a BIT STRING
@param in The DER encoded BIT STRING
@param inlen The size of the DER BIT STRING
- @param out [out] The array of bits stored (one per char)
+ @param out [out] The array of bits stored (8 per char)
@param outlen [in/out] The number of bits stored
@return CRYPT_OK if successful
*/
@@ -84,7 +86,9 @@ int der_decode_bit_string(const unsigned char *in, unsigned long inlen,
/* decode/store the bits */
for (y = 0; y < blen; y++) {
- out[y] = (in[x] & (1 << (7 - (y & 7)))) ? 1 : 0;
+ if (in[x] & (1 << (7 - (y & 7)))) {
+ setbit(out[y/8], 7-(y%8));
+ }
if ((y & 7) == 7) {
++x;
}
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;
}