comparison libtomcrypt/src/pk/asn1/der/sequence/der_length_sequence.c @ 1471:6dba84798cd5

Update to libtomcrypt 1.18.1, merged with Dropbear changes
author Matt Johnston <matt@ucc.asn.au>
date Fri, 09 Feb 2018 21:44:05 +0800
parents f849a5ca2efc
children
comparison
equal deleted inserted replaced
1470:8bba51a55704 1471:6dba84798cd5
3 * LibTomCrypt is a library that provides various cryptographic 3 * LibTomCrypt is a library that provides various cryptographic
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 *
9 * Tom St Denis, [email protected], http://libtom.org
10 */ 8 */
11 #include "tomcrypt.h" 9 #include "tomcrypt.h"
12 10
13 /** 11 /**
14 @file der_length_sequence.c 12 @file der_length_sequence.c
16 */ 14 */
17 15
18 #ifdef LTC_DER 16 #ifdef LTC_DER
19 17
20 /** 18 /**
21 Get the length of a DER sequence 19 Get the length of a DER sequence
22 @param list The sequences of items in the SEQUENCE 20 @param list The sequences of items in the SEQUENCE
23 @param inlen The number of items 21 @param inlen The number of items
24 @param outlen [out] The length required in octets to store it 22 @param outlen [out] The length required in octets to store it
25 @return CRYPT_OK on success 23 @return CRYPT_OK on success
26 */ 24 */
27 int der_length_sequence(ltc_asn1_list *list, unsigned long inlen, 25 int der_length_sequence(ltc_asn1_list *list, unsigned long inlen,
28 unsigned long *outlen) 26 unsigned long *outlen)
29 { 27 {
30 int err, type; 28 return der_length_sequence_ex(list, inlen, outlen, NULL);
31 unsigned long size, x, y, z, i; 29 }
30
31 int der_length_sequence_ex(ltc_asn1_list *list, unsigned long inlen,
32 unsigned long *outlen, unsigned long *payloadlen)
33 {
34 int err;
35 ltc_asn1_type type;
36 unsigned long size, x, y, i, z;
32 void *data; 37 void *data;
33 38
34 LTC_ARGCHK(list != NULL); 39 LTC_ARGCHK(list != NULL);
35 LTC_ARGCHK(outlen != NULL); 40 LTC_ARGCHK(outlen != NULL);
36 41
39 for (i = 0; i < inlen; i++) { 44 for (i = 0; i < inlen; i++) {
40 type = list[i].type; 45 type = list[i].type;
41 size = list[i].size; 46 size = list[i].size;
42 data = list[i].data; 47 data = list[i].data;
43 48
44 if (type == LTC_ASN1_EOL) { 49 if (type == LTC_ASN1_EOL) {
45 break; 50 break;
46 } 51 }
47 52
48 switch (type) { 53 switch (type) {
49 case LTC_ASN1_BOOLEAN: 54 case LTC_ASN1_BOOLEAN:
50 if ((err = der_length_boolean(&x)) != CRYPT_OK) { 55 if ((err = der_length_boolean(&x)) != CRYPT_OK) {
51 goto LBL_ERR; 56 goto LBL_ERR;
52 } 57 }
53 y += x; 58 y += x;
54 break; 59 break;
55 60
56 case LTC_ASN1_INTEGER: 61 case LTC_ASN1_INTEGER:
57 if ((err = der_length_integer(data, &x)) != CRYPT_OK) { 62 if ((err = der_length_integer(data, &x)) != CRYPT_OK) {
58 goto LBL_ERR; 63 goto LBL_ERR;
59 } 64 }
60 y += x; 65 y += x;
66 } 71 }
67 y += x; 72 y += x;
68 break; 73 break;
69 74
70 case LTC_ASN1_BIT_STRING: 75 case LTC_ASN1_BIT_STRING:
76 case LTC_ASN1_RAW_BIT_STRING:
71 if ((err = der_length_bit_string(size, &x)) != CRYPT_OK) { 77 if ((err = der_length_bit_string(size, &x)) != CRYPT_OK) {
72 goto LBL_ERR; 78 goto LBL_ERR;
73 } 79 }
74 y += x; 80 y += x;
75 break; 81 break;
97 goto LBL_ERR; 103 goto LBL_ERR;
98 } 104 }
99 y += x; 105 y += x;
100 break; 106 break;
101 107
108 case LTC_ASN1_TELETEX_STRING:
109 if ((err = der_length_teletex_string(data, size, &x)) != CRYPT_OK) {
110 goto LBL_ERR;
111 }
112 y += x;
113 break;
114
102 case LTC_ASN1_PRINTABLE_STRING: 115 case LTC_ASN1_PRINTABLE_STRING:
103 if ((err = der_length_printable_string(data, size, &x)) != CRYPT_OK) { 116 if ((err = der_length_printable_string(data, size, &x)) != CRYPT_OK) {
104 goto LBL_ERR; 117 goto LBL_ERR;
105 } 118 }
106 y += x; 119 y += x;
107 break; 120 break;
108 121
109 case LTC_ASN1_UTCTIME: 122 case LTC_ASN1_UTCTIME:
110 if ((err = der_length_utctime(data, &x)) != CRYPT_OK) { 123 if ((err = der_length_utctime(data, &x)) != CRYPT_OK) {
124 goto LBL_ERR;
125 }
126 y += x;
127 break;
128
129 case LTC_ASN1_GENERALIZEDTIME:
130 if ((err = der_length_generalizedtime(data, &x)) != CRYPT_OK) {
111 goto LBL_ERR; 131 goto LBL_ERR;
112 } 132 }
113 y += x; 133 y += x;
114 break; 134 break;
115 135
127 goto LBL_ERR; 147 goto LBL_ERR;
128 } 148 }
129 y += x; 149 y += x;
130 break; 150 break;
131 151
132 152
133 default: 153 case LTC_ASN1_CHOICE:
154 case LTC_ASN1_CONSTRUCTED:
155 case LTC_ASN1_CONTEXT_SPECIFIC:
156 case LTC_ASN1_EOL:
134 err = CRYPT_INVALID_ARG; 157 err = CRYPT_INVALID_ARG;
135 goto LBL_ERR; 158 goto LBL_ERR;
136 } 159 }
137 } 160 }
138 161
153 err = CRYPT_INVALID_ARG; 176 err = CRYPT_INVALID_ARG;
154 goto LBL_ERR; 177 goto LBL_ERR;
155 } 178 }
156 179
157 /* store size */ 180 /* store size */
181 if (payloadlen) *payloadlen = z;
158 *outlen = y; 182 *outlen = y;
159 err = CRYPT_OK; 183 err = CRYPT_OK;
160 184
161 LBL_ERR: 185 LBL_ERR:
162 return err; 186 return err;
163 } 187 }
164 188
165 #endif 189 #endif
166 190
167 /* $Source$ */ 191 /* ref: $Format:%D$ */
168 /* $Revision$ */ 192 /* git commit: $Format:%H$ */
169 /* $Date$ */ 193 /* commit time: $Format:%ai$ */