]> pd.if.org Git - zpackage/blob - libtomcrypt/src/pk/asn1/der/object_identifier/der_encode_object_identifier.c
commit files needed for zpm-fetchurl
[zpackage] / libtomcrypt / src / pk / asn1 / der / object_identifier / der_encode_object_identifier.c
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 #include "tomcrypt.h"
10
11 /**
12   @file der_encode_object_identifier.c
13   ASN.1 DER, Encode Object Identifier, Tom St Denis
14 */
15
16 #ifdef LTC_DER
17 /**
18   Encode an OID
19   @param words   The words to encode  (upto 32-bits each)
20   @param nwords  The number of words in the OID
21   @param out     [out] Destination of OID data
22   @param outlen  [in/out] The max and resulting size of the OID
23   @return CRYPT_OK if successful
24 */
25 int der_encode_object_identifier(unsigned long *words, unsigned long  nwords,
26                                  unsigned char *out,   unsigned long *outlen)
27 {
28    unsigned long i, x, y, z, t, mask, wordbuf;
29    int           err;
30
31    LTC_ARGCHK(words  != NULL);
32    LTC_ARGCHK(out    != NULL);
33    LTC_ARGCHK(outlen != NULL);
34
35    /* check length */
36    if ((err = der_length_object_identifier(words, nwords, &x)) != CRYPT_OK) {
37       return err;
38    }
39    if (x > *outlen) {
40       *outlen = x;
41       return CRYPT_BUFFER_OVERFLOW;
42    }
43
44    /* compute length to store OID data */
45    z = 0;
46    wordbuf = words[0] * 40 + words[1];
47    for (y = 1; y < nwords; y++) {
48        t = der_object_identifier_bits(wordbuf);
49        z += t/7 + ((t%7) ? 1 : 0) + (wordbuf == 0 ? 1 : 0);
50        if (y < nwords - 1) {
51           wordbuf = words[y + 1];
52        }
53    }
54
55    /* store header + length */
56    x = 0;
57    out[x++] = 0x06;
58    if (z < 128) {
59       out[x++] = (unsigned char)z;
60    } else if (z < 256) {
61       out[x++] = 0x81;
62       out[x++] = (unsigned char)z;
63    } else if (z < 65536UL) {
64       out[x++] = 0x82;
65       out[x++] = (unsigned char)((z>>8)&255);
66       out[x++] = (unsigned char)(z&255);
67    } else {
68       return CRYPT_INVALID_ARG;
69    }
70
71    /* store first byte */
72    wordbuf = words[0] * 40 + words[1];
73    for (i = 1; i < nwords; i++) {
74       /* store 7 bit words in little endian */
75       t    = wordbuf & 0xFFFFFFFF;
76       if (t) {
77          y    = x;
78          mask = 0;
79          while (t) {
80             out[x++] = (unsigned char)((t & 0x7F) | mask);
81             t    >>= 7;
82             mask  |= 0x80;  /* upper bit is set on all but the last byte */
83          }
84          /* now swap bytes y...x-1 */
85          z = x - 1;
86          while (y < z) {
87             t = out[y]; out[y] = out[z]; out[z] = (unsigned char)t;
88             ++y;
89             --z;
90          }
91       } else {
92          /* zero word */
93          out[x++] = 0x00;
94       }
95
96       if (i < nwords - 1) {
97          wordbuf = words[i + 1];
98       }
99    }
100
101    *outlen = x;
102    return CRYPT_OK;
103 }
104
105 #endif
106
107 /* ref:         $Format:%D$ */
108 /* git commit:  $Format:%H$ */
109 /* commit time: $Format:%ai$ */