comparison libtomcrypt/src/pk/asn1/der/object_identifier/der_encode_object_identifier.c @ 478:d4f32c3443ac dbclient-netcat-alike

propagate from branch 'au.asn.ucc.matt.dropbear' (head f21045c791002d81fc6b8dde6537ea481e513eb2) to branch 'au.asn.ucc.matt.dropbear.dbclient-netcat-alike' (head d1f69334581dc4c35f9ca16aa5355074c9dd315d)
author Matt Johnston <matt@ucc.asn.au>
date Sun, 14 Sep 2008 06:47:51 +0000
parents 0cbe8f6dbf9e
children f849a5ca2efc
comparison
equal deleted inserted replaced
296:6b41e2cbf071 478:d4f32c3443ac
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_object_identifier.c 14 @file der_encode_object_identifier.c
25 @return CRYPT_OK if successful 25 @return CRYPT_OK if successful
26 */ 26 */
27 int der_encode_object_identifier(unsigned long *words, unsigned long nwords, 27 int der_encode_object_identifier(unsigned long *words, unsigned long nwords,
28 unsigned char *out, unsigned long *outlen) 28 unsigned char *out, unsigned long *outlen)
29 { 29 {
30 unsigned long i, x, y, z, t, mask; 30 unsigned long i, x, y, z, t, mask, wordbuf;
31 int err; 31 int err;
32 32
33 LTC_ARGCHK(words != NULL); 33 LTC_ARGCHK(words != NULL);
34 LTC_ARGCHK(out != NULL); 34 LTC_ARGCHK(out != NULL);
35 LTC_ARGCHK(outlen != NULL); 35 LTC_ARGCHK(outlen != NULL);
37 /* check length */ 37 /* check length */
38 if ((err = der_length_object_identifier(words, nwords, &x)) != CRYPT_OK) { 38 if ((err = der_length_object_identifier(words, nwords, &x)) != CRYPT_OK) {
39 return err; 39 return err;
40 } 40 }
41 if (x > *outlen) { 41 if (x > *outlen) {
42 *outlen = x;
42 return CRYPT_BUFFER_OVERFLOW; 43 return CRYPT_BUFFER_OVERFLOW;
43 } 44 }
44 45
45 /* compute length to store OID data */ 46 /* compute length to store OID data */
46 z = 1; 47 z = 0;
47 for (y = 2; y < nwords; y++) { 48 wordbuf = words[0] * 40 + words[1];
48 t = der_object_identifier_bits(words[y]); 49 for (y = 1; y < nwords; y++) {
49 z += t/7 + ((t%7) ? 1 : 0); 50 t = der_object_identifier_bits(wordbuf);
51 z += t/7 + ((t%7) ? 1 : 0) + (wordbuf == 0 ? 1 : 0);
52 if (y < nwords - 1) {
53 wordbuf = words[y + 1];
54 }
50 } 55 }
51 56
52 /* store header + length */ 57 /* store header + length */
53 x = 0; 58 x = 0;
54 out[x++] = 0x06; 59 out[x++] = 0x06;
55 if (z < 128) { 60 if (z < 128) {
56 out[x++] = z; 61 out[x++] = (unsigned char)z;
57 } else if (z < 256) { 62 } else if (z < 256) {
58 out[x++] = 0x81; 63 out[x++] = 0x81;
59 out[x++] = z; 64 out[x++] = (unsigned char)z;
60 } else if (z < 65536UL) { 65 } else if (z < 65536UL) {
61 out[x++] = 0x82; 66 out[x++] = 0x82;
62 out[x++] = (z>>8)&255; 67 out[x++] = (unsigned char)((z>>8)&255);
63 out[x++] = z&255; 68 out[x++] = (unsigned char)(z&255);
64 } else { 69 } else {
65 return CRYPT_INVALID_ARG; 70 return CRYPT_INVALID_ARG;
66 } 71 }
67 72
68 /* store first byte */ 73 /* store first byte */
69 out[x++] = words[0] * 40 + words[1]; 74 wordbuf = words[0] * 40 + words[1];
70 75 for (i = 1; i < nwords; i++) {
71 for (i = 2; i < nwords; i++) {
72 /* store 7 bit words in little endian */ 76 /* store 7 bit words in little endian */
73 t = words[i] & 0xFFFFFFFF; 77 t = wordbuf & 0xFFFFFFFF;
74 if (t) { 78 if (t) {
75 y = x; 79 y = x;
76 mask = 0; 80 mask = 0;
77 while (t) { 81 while (t) {
78 out[x++] = (t & 0x7F) | mask; 82 out[x++] = (unsigned char)((t & 0x7F) | mask);
79 t >>= 7; 83 t >>= 7;
80 mask |= 0x80; /* upper bit is set on all but the last byte */ 84 mask |= 0x80; /* upper bit is set on all but the last byte */
81 } 85 }
82 /* now swap bytes y...x-1 */ 86 /* now swap bytes y...x-1 */
83 z = x - 1; 87 z = x - 1;
84 while (y < z) { 88 while (y < z) {
85 t = out[y]; out[y] = out[z]; out[z] = t; 89 t = out[y]; out[y] = out[z]; out[z] = (unsigned char)t;
86 ++y; 90 ++y;
87 --z; 91 --z;
88 } 92 }
89 } else { 93 } else {
90 /* zero word */ 94 /* zero word */
91 out[x++] = 0x00; 95 out[x++] = 0x00;
96 }
97
98 if (i < nwords - 1) {
99 wordbuf = words[i + 1];
92 } 100 }
93 } 101 }
94 102
95 *outlen = x; 103 *outlen = x;
96 return CRYPT_OK; 104 return CRYPT_OK;
97 } 105 }
98 106
99 #endif 107 #endif
100 108
101 /* $Source: /cvs/libtom/libtomcrypt/src/pk/asn1/der/object_identifier/der_encode_object_identifier.c,v $ */ 109 /* $Source: /cvs/libtom/libtomcrypt/src/pk/asn1/der/object_identifier/der_encode_object_identifier.c,v $ */
102 /* $Revision: 1.1 $ */ 110 /* $Revision: 1.6 $ */
103 /* $Date: 2005/05/16 15:08:11 $ */ 111 /* $Date: 2006/12/04 21:34:03 $ */