comparison libtomcrypt/src/pk/asn1/der/set/der_encode_set.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_encode_set.c 12 @file der_encode_set.c
16 */ 14 */
17 15
18 #ifdef LTC_DER 16 #ifdef LTC_DER
19 17
20 /* LTC define to ASN.1 TAG */ 18 /* LTC define to ASN.1 TAG */
21 static int ltc_to_asn1(int v) 19 static int _ltc_to_asn1(ltc_asn1_type v)
22 { 20 {
23 switch (v) { 21 switch (v) {
24 case LTC_ASN1_BOOLEAN: return 0x01; 22 case LTC_ASN1_BOOLEAN: return 0x01;
25 case LTC_ASN1_INTEGER: 23 case LTC_ASN1_INTEGER:
26 case LTC_ASN1_SHORT_INTEGER: return 0x02; 24 case LTC_ASN1_SHORT_INTEGER: return 0x02;
25 case LTC_ASN1_RAW_BIT_STRING:
27 case LTC_ASN1_BIT_STRING: return 0x03; 26 case LTC_ASN1_BIT_STRING: return 0x03;
28 case LTC_ASN1_OCTET_STRING: return 0x04; 27 case LTC_ASN1_OCTET_STRING: return 0x04;
29 case LTC_ASN1_NULL: return 0x05; 28 case LTC_ASN1_NULL: return 0x05;
30 case LTC_ASN1_OBJECT_IDENTIFIER: return 0x06; 29 case LTC_ASN1_OBJECT_IDENTIFIER: return 0x06;
31 case LTC_ASN1_UTF8_STRING: return 0x0C; 30 case LTC_ASN1_UTF8_STRING: return 0x0C;
32 case LTC_ASN1_PRINTABLE_STRING: return 0x13; 31 case LTC_ASN1_PRINTABLE_STRING: return 0x13;
32 case LTC_ASN1_TELETEX_STRING: return 0x14;
33 case LTC_ASN1_IA5_STRING: return 0x16; 33 case LTC_ASN1_IA5_STRING: return 0x16;
34 case LTC_ASN1_UTCTIME: return 0x17; 34 case LTC_ASN1_UTCTIME: return 0x17;
35 case LTC_ASN1_GENERALIZEDTIME: return 0x18;
35 case LTC_ASN1_SEQUENCE: return 0x30; 36 case LTC_ASN1_SEQUENCE: return 0x30;
36 case LTC_ASN1_SET: 37 case LTC_ASN1_SET:
37 case LTC_ASN1_SETOF: return 0x31; 38 case LTC_ASN1_SETOF: return 0x31;
38 default: return -1; 39 case LTC_ASN1_CHOICE:
40 case LTC_ASN1_CONSTRUCTED:
41 case LTC_ASN1_CONTEXT_SPECIFIC:
42 case LTC_ASN1_EOL: return -1;
39 } 43 }
40 } 44 return -1;
41 45 }
42 46
43 static int qsort_helper(const void *a, const void *b) 47
48 static int _qsort_helper(const void *a, const void *b)
44 { 49 {
45 ltc_asn1_list *A = (ltc_asn1_list *)a, *B = (ltc_asn1_list *)b; 50 ltc_asn1_list *A = (ltc_asn1_list *)a, *B = (ltc_asn1_list *)b;
46 int r; 51 int r;
47 52
48 r = ltc_to_asn1(A->type) - ltc_to_asn1(B->type); 53 r = _ltc_to_asn1(A->type) - _ltc_to_asn1(B->type);
49 54
50 /* for QSORT the order is UNDEFINED if they are "equal" which means it is NOT DETERMINISTIC. So we force it to be :-) */ 55 /* for QSORT the order is UNDEFINED if they are "equal" which means it is NOT DETERMINISTIC. So we force it to be :-) */
51 if (r == 0) { 56 if (r == 0) {
52 /* their order in the original list now determines the position */ 57 /* their order in the original list now determines the position */
53 return A->used - B->used; 58 return A->used - B->used;
54 } else { 59 } else {
55 return r; 60 return r;
56 } 61 }
57 } 62 }
58 63
59 /* 64 /*
60 Encode a SET type 65 Encode a SET type
61 @param list The list of items to encode 66 @param list The list of items to encode
62 @param inlen The number of items in the list 67 @param inlen The number of items in the list
63 @param out [out] The destination 68 @param out [out] The destination
64 @param outlen [in/out] The size of the output 69 @param outlen [in/out] The size of the output
65 @return CRYPT_OK on success 70 @return CRYPT_OK on success
66 */ 71 */
67 int der_encode_set(ltc_asn1_list *list, unsigned long inlen, 72 int der_encode_set(ltc_asn1_list *list, unsigned long inlen,
68 unsigned char *out, unsigned long *outlen) 73 unsigned char *out, unsigned long *outlen)
69 { 74 {
70 ltc_asn1_list *copy; 75 ltc_asn1_list *copy;
71 unsigned long x; 76 unsigned long x;
72 int err; 77 int err;
73 78
74 /* make copy of list */ 79 /* make copy of list */
75 copy = XCALLOC(inlen, sizeof(*copy)); 80 copy = XCALLOC(inlen, sizeof(*copy));
76 if (copy == NULL) { 81 if (copy == NULL) {
77 return CRYPT_MEM; 82 return CRYPT_MEM;
78 } 83 }
79 84
80 /* fill in used member with index so we can fully sort it */ 85 /* fill in used member with index so we can fully sort it */
81 for (x = 0; x < inlen; x++) { 86 for (x = 0; x < inlen; x++) {
82 copy[x] = list[x]; 87 copy[x] = list[x];
83 copy[x].used = x; 88 copy[x].used = x;
84 } 89 }
85 90
86 /* sort it by the "type" field */ 91 /* sort it by the "type" field */
87 XQSORT(copy, inlen, sizeof(*copy), &qsort_helper); 92 XQSORT(copy, inlen, sizeof(*copy), &_qsort_helper);
88 93
89 /* call der_encode_sequence_ex() */ 94 /* call der_encode_sequence_ex() */
90 err = der_encode_sequence_ex(copy, inlen, out, outlen, LTC_ASN1_SET); 95 err = der_encode_sequence_ex(copy, inlen, out, outlen, LTC_ASN1_SET);
91 96
92 /* free list */ 97 /* free list */
93 XFREE(copy); 98 XFREE(copy);
94 99
95 return err; 100 return err;
96 } 101 }
97 102
98 103
99 #endif 104 #endif
100 105
101 /* $Source$ */ 106 /* ref: $Format:%D$ */
102 /* $Revision$ */ 107 /* git commit: $Format:%H$ */
103 /* $Date$ */ 108 /* commit time: $Format:%ai$ */