Mercurial > dropbear
comparison libtomcrypt/src/pk/asn1/der/bit/der_encode_bit_string.c @ 398:59c7938af2bd
merge of '1250b8af44b62d8f4fe0f8d9fc7e7a1cc34e7e1c'
and '7f8670ac3bb975f40967f3979d09d2199b7e90c8'
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Sat, 03 Feb 2007 08:20:30 +0000 |
parents | 0cbe8f6dbf9e |
children | f849a5ca2efc |
comparison
equal
deleted
inserted
replaced
396:e7c1a77d2921 | 398:59c7938af2bd |
---|---|
4 * algorithms in a highly modular and flexible manner. | 4 * algorithms in a highly modular and flexible manner. |
5 * | 5 * |
6 * The library is free for all purposes without any express | 6 * The library is free for all purposes without any express |
7 * guarantee it works. | 7 * guarantee it works. |
8 * | 8 * |
9 * Tom St Denis, [email protected], http://libtomcrypt.org | 9 * Tom St Denis, [email protected], http://libtomcrypt.com |
10 */ | 10 */ |
11 #include "tomcrypt.h" | 11 #include "tomcrypt.h" |
12 | 12 |
13 /** | 13 /** |
14 @file der_encode_bit_string.c | 14 @file der_encode_bit_string.c |
27 @return CRYPT_OK if successful | 27 @return CRYPT_OK if successful |
28 */ | 28 */ |
29 int der_encode_bit_string(const unsigned char *in, unsigned long inlen, | 29 int der_encode_bit_string(const unsigned char *in, unsigned long inlen, |
30 unsigned char *out, unsigned long *outlen) | 30 unsigned char *out, unsigned long *outlen) |
31 { | 31 { |
32 unsigned long len, x, y, buf; | 32 unsigned long len, x, y; |
33 unsigned char buf; | |
33 int err; | 34 int err; |
34 | 35 |
35 LTC_ARGCHK(in != NULL); | 36 LTC_ARGCHK(in != NULL); |
36 LTC_ARGCHK(out != NULL); | 37 LTC_ARGCHK(out != NULL); |
37 LTC_ARGCHK(outlen != NULL); | 38 LTC_ARGCHK(outlen != NULL); |
40 if ((err = der_length_bit_string(inlen, &len)) != CRYPT_OK) { | 41 if ((err = der_length_bit_string(inlen, &len)) != CRYPT_OK) { |
41 return err; | 42 return err; |
42 } | 43 } |
43 | 44 |
44 if (len > *outlen) { | 45 if (len > *outlen) { |
46 *outlen = len; | |
45 return CRYPT_BUFFER_OVERFLOW; | 47 return CRYPT_BUFFER_OVERFLOW; |
46 } | 48 } |
47 | 49 |
48 /* store header (include bit padding count in length) */ | 50 /* store header (include bit padding count in length) */ |
49 x = 0; | 51 x = 0; |
50 y = (inlen >> 3) + ((inlen&7) ? 1 : 0) + 1; | 52 y = (inlen >> 3) + ((inlen&7) ? 1 : 0) + 1; |
51 | 53 |
52 out[x++] = 0x03; | 54 out[x++] = 0x03; |
53 if (y < 128) { | 55 if (y < 128) { |
54 out[x++] = y; | 56 out[x++] = (unsigned char)y; |
55 } else if (y < 256) { | 57 } else if (y < 256) { |
56 out[x++] = 0x81; | 58 out[x++] = 0x81; |
57 out[x++] = y; | 59 out[x++] = (unsigned char)y; |
58 } else if (y < 65536) { | 60 } else if (y < 65536) { |
59 out[x++] = 0x82; | 61 out[x++] = 0x82; |
60 out[x++] = (y>>8)&255; | 62 out[x++] = (unsigned char)((y>>8)&255); |
61 out[x++] = y&255; | 63 out[x++] = (unsigned char)(y&255); |
62 } | 64 } |
63 | 65 |
64 /* store number of zero padding bits */ | 66 /* store number of zero padding bits */ |
65 out[x++] = (8 - inlen) & 7; | 67 out[x++] = (unsigned char)((8 - inlen) & 7); |
66 | 68 |
67 /* store the bits in big endian format */ | 69 /* store the bits in big endian format */ |
68 for (y = buf = 0; y < inlen; y++) { | 70 for (y = buf = 0; y < inlen; y++) { |
69 buf |= (in[y] ? 1 : 0) << (7 - (y & 7)); | 71 buf |= (in[y] ? 1 : 0) << (7 - (y & 7)); |
70 if ((y & 7) == 7) { | 72 if ((y & 7) == 7) { |
81 } | 83 } |
82 | 84 |
83 #endif | 85 #endif |
84 | 86 |
85 /* $Source: /cvs/libtom/libtomcrypt/src/pk/asn1/der/bit/der_encode_bit_string.c,v $ */ | 87 /* $Source: /cvs/libtom/libtomcrypt/src/pk/asn1/der/bit/der_encode_bit_string.c,v $ */ |
86 /* $Revision: 1.1 $ */ | 88 /* $Revision: 1.4 $ */ |
87 /* $Date: 2005/05/16 15:08:11 $ */ | 89 /* $Date: 2006/12/04 21:34:03 $ */ |