comparison src/pk/asn1/der/octet/der_encode_octet_string.c @ 209:39d5d58461d6 libtomcrypt-orig LTC_1.05

Import of libtomcrypt 1.05
author Matt Johnston <matt@ucc.asn.au>
date Wed, 06 Jul 2005 03:53:40 +0000
parents
children d5faf4814ddb
comparison
equal deleted inserted replaced
191:1c15b283127b 209:39d5d58461d6
1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis
2 *
3 * LibTomCrypt is a library that provides various cryptographic
4 * algorithms in a highly modular and flexible manner.
5 *
6 * The library is free for all purposes without any express
7 * guarantee it works.
8 *
9 * Tom St Denis, [email protected], http://libtomcrypt.org
10 */
11 #include "tomcrypt.h"
12
13 /**
14 @file der_encode_octet_string.c
15 ASN.1 DER, encode a OCTET STRING, Tom St Denis
16 */
17
18
19 #ifdef LTC_DER
20
21 /**
22 Store an OCTET STRING
23 @param in The array of OCTETS to store (one per char)
24 @param inlen The number of OCTETS to store
25 @param out [out] The destination for the DER encoded OCTET STRING
26 @param outlen [in/out] The max size and resulting size of the DER OCTET STRING
27 @return CRYPT_OK if successful
28 */
29 int der_encode_octet_string(const unsigned char *in, unsigned long inlen,
30 unsigned char *out, unsigned long *outlen)
31 {
32 unsigned long x, y, len;
33 int err;
34
35 LTC_ARGCHK(in != NULL);
36 LTC_ARGCHK(out != NULL);
37 LTC_ARGCHK(outlen != NULL);
38
39 /* get the size */
40 if ((err = der_length_octet_string(inlen, &len)) != CRYPT_OK) {
41 return err;
42 }
43
44 /* too big? */
45 if (len > *outlen) {
46 return CRYPT_BUFFER_OVERFLOW;
47 }
48
49 /* encode the header+len */
50 x = 0;
51 out[x++] = 0x04;
52 if (inlen < 128) {
53 out[x++] = inlen;
54 } else if (inlen < 256) {
55 out[x++] = 0x81;
56 out[x++] = inlen;
57 } else if (inlen < 65536UL) {
58 out[x++] = 0x82;
59 out[x++] = (inlen>>8)&255;
60 out[x++] = inlen&255;
61 } else if (inlen < 16777216UL) {
62 out[x++] = 0x83;
63 out[x++] = (inlen>>16)&255;
64 out[x++] = (inlen>>8)&255;
65 out[x++] = inlen&255;
66 } else {
67 return CRYPT_INVALID_ARG;
68 }
69
70 /* store octets */
71 for (y = 0; y < inlen; y++) {
72 out[x++] = in[y];
73 }
74
75 /* retun length */
76 *outlen = x;
77
78 return CRYPT_OK;
79 }
80
81 #endif
82
83 /* $Source: /cvs/libtom/libtomcrypt/src/pk/asn1/der/octet/der_encode_octet_string.c,v $ */
84 /* $Revision: 1.1 $ */
85 /* $Date: 2005/05/16 15:08:11 $ */