forked from trustwallet/wallet-core
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTWPKCS8.cpp
More file actions
105 lines (94 loc) · 2.86 KB
/
TWPKCS8.cpp
File metadata and controls
105 lines (94 loc) · 2.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// Copyright © 2017-2020 Trust Wallet.
//
// This file is part of Trust. The full Trust copyright notice, including
// terms governing use, modification, and redistribution, is contained in the
// file LICENSE at the root of the source code distribution tree.
#include <TrustWalletCore/TWPKCS8.h>
#include "../PrivateKey.h"
using namespace TW;
TWData *_Nullable TWPKCS8EncodeED25519PrivateKey(TWData *_Nonnull privateKey) {
uint8_t* privateKeyBytes = TWDataBytes(privateKey);
size_t privateKeySize = TWDataSize(privateKey);
if (privateKeySize != PrivateKey::size) {
return nullptr;
}
size_t totlen = 16 + privateKeySize;
auto rv = TWDataCreateWithSize(totlen);
size_t idx = 0;
// sequence
TWDataSet(rv, idx++, 0x30);
TWDataSet(rv, idx++, (byte) (totlen - 2));
// version
TWDataSet(rv, idx++, 0x02);
TWDataSet(rv, idx++, 1);
// v1 - no public key included
TWDataSet(rv, idx++, 0);
// Algorithm Identifier
// sequence
TWDataSet(rv, idx++, 0x30);
TWDataSet(rv, idx++, 5);
// OID
// https://msdn.microsoft.com/en-us/library/windows/desktop/bb540809%28v=vs.85%29.aspx
TWDataSet(rv, idx++, 0x06);
TWDataSet(rv, idx++, 3);
TWDataSet(rv, idx++, (1 * 40) + 3);
TWDataSet(rv, idx++, 101);
TWDataSet(rv, idx++, (uint8_t) 112);
// params - absent
// PrivateKey
TWDataSet(rv, idx++, 0x04); // octet string
TWDataSet(rv, idx++, (uint8_t) (2 + privateKeySize));
// CurvePrivateKey
TWDataSet(rv, idx++, 0x04); // octet string
TWDataSet(rv, idx++, (uint8_t) privateKeySize);
// the key
TWDataReplaceBytes(rv, idx, privateKeySize, privateKeyBytes);
return rv;
}
TWData *_Nullable TWPKCS8DecodeED25519PrivateKey(TWData *_Nonnull data) {
uint8_t* dataBytes = TWDataBytes(data);
size_t dataSize = TWDataSize(data);
//
// Setup
//
size_t totlen = 48;
size_t idlen = 5;
//
// Pre-decoding check
//
if (dataSize != totlen) {
return nullptr;
}
int doid = dataBytes[11];
if (doid != 112) {
return nullptr;
}
//
// Decoding
//
int idx = 0;
if (dataBytes[idx++] != 0x30 ||
dataBytes[idx++] != (totlen - 2) ||
dataBytes[idx++] != 0x02 ||
dataBytes[idx++] != 1 ||
dataBytes[idx++] != 0 ||
dataBytes[idx++] != 0x30 ||
dataBytes[idx++] != idlen ||
dataBytes[idx++] != 0x06 ||
dataBytes[idx++] != 3 ||
dataBytes[idx++] != (1 * 40) + 3 ||
dataBytes[idx++] != 101) {
return nullptr;
}
idx++; // OID, checked above
if (dataBytes[idx++] != 0x04 ||
dataBytes[idx++] != 34) {
return nullptr;
}
if (dataBytes[idx++] != 0x04 ||
dataBytes[idx++] != 32) {
return nullptr;
}
TWData* rv = TWDataCreateWithBytes(dataBytes + idx, PrivateKey::size);
return rv;
}