Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
wine-mirror
GitHub Repository: wine-mirror/wine
Path: blob/master/libs/tomcrypt/src/pk/rsa/rsa_export.c
4396 views
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 rsa_export.c
13
Export RSA PKCS keys, Tom St Denis
14
*/
15
16
#ifdef LTC_MRSA
17
18
/**
19
This will export either an RSAPublicKey or RSAPrivateKey [defined in PKCS #1 v2.1]
20
@param out [out] Destination of the packet
21
@param outlen [in/out] The max size and resulting size of the packet
22
@param type The type of exported key (PK_PRIVATE or PK_PUBLIC)
23
@param key The RSA key to export
24
@return CRYPT_OK if successful
25
*/
26
int rsa_export(unsigned char *out, unsigned long *outlen, int type, rsa_key *key)
27
{
28
unsigned long zero=0;
29
int err;
30
LTC_ARGCHK(out != NULL);
31
LTC_ARGCHK(outlen != NULL);
32
LTC_ARGCHK(key != NULL);
33
34
/* type valid? */
35
if (!(key->type == PK_PRIVATE) && (type == PK_PRIVATE)) {
36
return CRYPT_PK_INVALID_TYPE;
37
}
38
39
if (type == PK_PRIVATE) {
40
/* private key */
41
/* output is
42
Version, n, e, d, p, q, d mod (p-1), d mod (q - 1), 1/q mod p
43
*/
44
return der_encode_sequence_multi(out, outlen,
45
LTC_ASN1_SHORT_INTEGER, 1UL, &zero,
46
LTC_ASN1_INTEGER, 1UL, key->N,
47
LTC_ASN1_INTEGER, 1UL, key->e,
48
LTC_ASN1_INTEGER, 1UL, key->d,
49
LTC_ASN1_INTEGER, 1UL, key->p,
50
LTC_ASN1_INTEGER, 1UL, key->q,
51
LTC_ASN1_INTEGER, 1UL, key->dP,
52
LTC_ASN1_INTEGER, 1UL, key->dQ,
53
LTC_ASN1_INTEGER, 1UL, key->qP,
54
LTC_ASN1_EOL, 0UL, NULL);
55
} else {
56
/* public key */
57
unsigned long tmplen, *ptmplen;
58
unsigned char* tmp = NULL;
59
60
if (type & PK_STD) {
61
tmplen = (unsigned long)(mp_count_bits(key->N) / 8) * 2 + 8;
62
tmp = XMALLOC(tmplen);
63
ptmplen = &tmplen;
64
if (tmp == NULL) {
65
return CRYPT_MEM;
66
}
67
}
68
else {
69
tmp = out;
70
ptmplen = outlen;
71
}
72
73
err = der_encode_sequence_multi(tmp, ptmplen,
74
LTC_ASN1_INTEGER, 1UL, key->N,
75
LTC_ASN1_INTEGER, 1UL, key->e,
76
LTC_ASN1_EOL, 0UL, NULL);
77
78
if ((err != CRYPT_OK) || !(type & PK_STD)) {
79
goto finish;
80
}
81
82
err = der_encode_subject_public_key_info(out, outlen,
83
PKA_RSA, tmp, tmplen, LTC_ASN1_NULL, NULL, 0);
84
85
finish:
86
if (tmp != out)
87
XFREE(tmp);
88
return err;
89
90
}
91
}
92
93
#endif /* LTC_MRSA */
94
95