comparison libtomcrypt/src/pk/dsa/dsa_export.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 e9dba7abd939
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 dsa_export.c 12 @file dsa_export.c
25 @param key The key to export 23 @param key The key to export
26 @return CRYPT_OK if successful 24 @return CRYPT_OK if successful
27 */ 25 */
28 int dsa_export(unsigned char *out, unsigned long *outlen, int type, dsa_key *key) 26 int dsa_export(unsigned char *out, unsigned long *outlen, int type, dsa_key *key)
29 { 27 {
30 unsigned char flags[1]; 28 unsigned long zero=0;
29 int err, std;
31 30
32 LTC_ARGCHK(out != NULL); 31 LTC_ARGCHK(out != NULL);
33 LTC_ARGCHK(outlen != NULL); 32 LTC_ARGCHK(outlen != NULL);
34 LTC_ARGCHK(key != NULL); 33 LTC_ARGCHK(key != NULL);
34
35 std = type & PK_STD;
36 type &= ~PK_STD;
35 37
36 /* can we store the static header? */ 38 /* can we store the static header? */
37 if (type == PK_PRIVATE && key->type != PK_PRIVATE) { 39 if (type == PK_PRIVATE && key->type != PK_PRIVATE) {
38 return CRYPT_PK_TYPE_MISMATCH; 40 return CRYPT_PK_TYPE_MISMATCH;
39 } 41 }
40 42
41 if (type != PK_PUBLIC && type != PK_PRIVATE) { 43 if (type != PK_PUBLIC && type != PK_PRIVATE) {
42 return CRYPT_INVALID_ARG; 44 return CRYPT_INVALID_ARG;
43 } 45 }
44 46
45 flags[0] = (type != PK_PUBLIC) ? 1 : 0; 47 if (type == PK_PRIVATE) {
48 if (std) {
49 return der_encode_sequence_multi(out, outlen,
50 LTC_ASN1_SHORT_INTEGER, 1UL, &zero,
51 LTC_ASN1_INTEGER, 1UL, key->p,
52 LTC_ASN1_INTEGER, 1UL, key->q,
53 LTC_ASN1_INTEGER, 1UL, key->g,
54 LTC_ASN1_INTEGER, 1UL, key->y,
55 LTC_ASN1_INTEGER, 1UL, key->x,
56 LTC_ASN1_EOL, 0UL, NULL);
57 }
58 else {
59 unsigned char flags[1];
60 flags[0] = 1;
61 return der_encode_sequence_multi(out, outlen,
62 LTC_ASN1_BIT_STRING, 1UL, flags,
63 LTC_ASN1_INTEGER, 1UL, key->g,
64 LTC_ASN1_INTEGER, 1UL, key->p,
65 LTC_ASN1_INTEGER, 1UL, key->q,
66 LTC_ASN1_INTEGER, 1UL, key->y,
67 LTC_ASN1_INTEGER, 1UL, key->x,
68 LTC_ASN1_EOL, 0UL, NULL);
69 }
70 } else {
71 if (std) {
72 unsigned long tmplen = (mp_count_bits(key->y) / 8) + 8;
73 unsigned char* tmp = XMALLOC(tmplen);
74 ltc_asn1_list int_list[3];
46 75
47 if (type == PK_PRIVATE) { 76 if (tmp == NULL) {
48 return der_encode_sequence_multi(out, outlen, 77 return CRYPT_MEM;
49 LTC_ASN1_BIT_STRING, 1UL, flags, 78 }
50 LTC_ASN1_INTEGER, 1UL, key->g, 79
51 LTC_ASN1_INTEGER, 1UL, key->p, 80 err = der_encode_integer(key->y, tmp, &tmplen);
52 LTC_ASN1_INTEGER, 1UL, key->q, 81 if (err != CRYPT_OK) {
53 LTC_ASN1_INTEGER, 1UL, key->y, 82 goto error;
54 LTC_ASN1_INTEGER, 1UL, key->x, 83 }
55 LTC_ASN1_EOL, 0UL, NULL); 84
56 } else { 85 LTC_SET_ASN1(int_list, 0, LTC_ASN1_INTEGER, key->p, 1UL);
57 return der_encode_sequence_multi(out, outlen, 86 LTC_SET_ASN1(int_list, 1, LTC_ASN1_INTEGER, key->q, 1UL);
58 LTC_ASN1_BIT_STRING, 1UL, flags, 87 LTC_SET_ASN1(int_list, 2, LTC_ASN1_INTEGER, key->g, 1UL);
59 LTC_ASN1_INTEGER, 1UL, key->g, 88
60 LTC_ASN1_INTEGER, 1UL, key->p, 89 err = der_encode_subject_public_key_info(out, outlen, PKA_DSA, tmp,
61 LTC_ASN1_INTEGER, 1UL, key->q, 90 tmplen, LTC_ASN1_SEQUENCE, int_list,
62 LTC_ASN1_INTEGER, 1UL, key->y, 91 sizeof(int_list) / sizeof(int_list[0]));
63 LTC_ASN1_EOL, 0UL, NULL); 92
93 error:
94 XFREE(tmp);
95 return err;
96 }
97 else {
98 unsigned char flags[1];
99 flags[0] = 0;
100 return der_encode_sequence_multi(out, outlen,
101 LTC_ASN1_BIT_STRING, 1UL, flags,
102 LTC_ASN1_INTEGER, 1UL, key->g,
103 LTC_ASN1_INTEGER, 1UL, key->p,
104 LTC_ASN1_INTEGER, 1UL, key->q,
105 LTC_ASN1_INTEGER, 1UL, key->y,
106 LTC_ASN1_EOL, 0UL, NULL);
107 }
64 } 108 }
65 } 109 }
66 110
67 #endif 111 #endif
68 112
69 113
70 /* $Source$ */ 114 /* ref: $Format:%D$ */
71 /* $Revision$ */ 115 /* git commit: $Format:%H$ */
72 /* $Date$ */ 116 /* commit time: $Format:%ai$ */