Skip to content

Commit 6963b36

Browse files
committed
NOnion: key types refactoring
Renamed ExpandedBlindedPrivateKey to Ed25519PrivateKey, which now can be either NormalEd25519PrivateKey or ExpandedEd25519PrivateKey. Implemented length checks for both. Renamed BlindedPublicKey to ED25519PublicKey.
1 parent ff994c7 commit 6963b36

File tree

6 files changed

+70
-24
lines changed

6 files changed

+70
-24
lines changed

NOnion/Constants.fs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,12 @@ module Constants =
3434
[<Literal>]
3535
let IdentityKeyLength = 20
3636

37+
[<Literal>]
38+
let Ed25519PrivateKeyLength = 32
39+
40+
[<Literal>]
41+
let ExpandedEd25519PrivateKeyLength = 64
42+
3743
let internal SupportedProtocolVersion: array<uint16> = [| 3us |]
3844

3945
(*

NOnion/Crypto/HiddenServicesCipher.fs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,14 +88,15 @@ module HiddenServicesCipher =
8888
Ed25519Clamp blindingFactor
8989

9090
match Ed25519.CalculateBlindedPublicKey(publicKey, blindingFactor) with
91-
| true, output -> BlindedPublicKey output
91+
| true, output -> ED25519PublicKey output
9292
| false, _ -> failwith "can't calculate blinded public key"
9393

9494
let CalculateExpandedBlindedPrivateKey
9595
(blindingFactor: array<byte>)
9696
(masterPrivateKey: array<byte>)
97-
: ExpandedBlindedPrivateKey =
98-
let expandedMasterPrivateKey = Array.zeroCreate 64
97+
: ExpandedEd25519PrivateKey =
98+
let expandedMasterPrivateKey =
99+
Array.zeroCreate Constants.ExpandedEd25519PrivateKeyLength
99100

100101
let hashEngine = Sha512Digest()
101102
hashEngine.BlockUpdate(masterPrivateKey, 0, masterPrivateKey.Length)
@@ -112,14 +113,14 @@ module HiddenServicesCipher =
112113
"Derive temporary signing key hash input"
113114
)
114115
with
115-
| true, output -> ExpandedBlindedPrivateKey output
116+
| true, output -> ExpandedEd25519PrivateKey output
116117
| false, _ -> failwith "can't calculate blinded private key"
117118

118119

119120
let BuildBlindedPublicKey
120121
(periodNumber: uint64, periodLength: uint64)
121122
(publicKey: array<byte>)
122-
: BlindedPublicKey =
123+
: ED25519PublicKey =
123124
let blindingFactor =
124125
CalculateBlindingFactor periodNumber periodLength publicKey
125126

NOnion/Directory/TorDirectory.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -583,7 +583,7 @@ type TorDirectory =
583583
|> Async.StartAsTask
584584

585585
member self.GetResponsibleHiddenServiceDirectories
586-
(BlindedPublicKey blindedPublicKey)
586+
(ED25519PublicKey blindedPublicKey)
587587
(sharedRandomValue: string)
588588
(periodNumber: uint64)
589589
(periodLength: uint64)

NOnion/KeyTypes.fs

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,55 @@
11
namespace NOnion
22

33

4-
type ExpandedBlindedPrivateKey = ExpandedBlindedPrivateKey of array<byte>
4+
type NormalEd25519PrivateKey(bytes: array<byte>) =
5+
do
6+
if bytes.Length <> Constants.Ed25519PrivateKeyLength then
7+
failwithf
8+
"Invalid private key (length=%d), %d expected"
9+
bytes.Length
10+
Constants.Ed25519PrivateKeyLength
11+
12+
member self.ToByteArray() =
13+
bytes
14+
15+
type ExpandedEd25519PrivateKey(bytes: array<byte>) =
16+
do
17+
if bytes.Length <> Constants.ExpandedEd25519PrivateKeyLength then
18+
failwithf
19+
"Invalid private key (length=%d), %d expected"
20+
bytes.Length
21+
Constants.ExpandedEd25519PrivateKeyLength
22+
23+
member self.ToByteArray() =
24+
bytes
25+
26+
type Ed25519PrivateKey =
27+
| NormalEd25519 of NormalEd25519PrivateKey
28+
| ExpandedEd25519 of ExpandedEd25519PrivateKey
29+
30+
static member FromBytes(bytes: array<byte>) : Ed25519PrivateKey =
31+
if bytes.Length = Constants.ExpandedEd25519PrivateKeyLength then
32+
ExpandedEd25519 <| ExpandedEd25519PrivateKey bytes
33+
elif bytes.Length = Constants.Ed25519PrivateKeyLength then
34+
NormalEd25519 <| NormalEd25519PrivateKey bytes
35+
else
36+
failwithf
37+
"Invalid private key (length=%d), private key should either be %d (standard ed25519) or %d bytes (expanded ed25519 key)"
38+
bytes.Length
39+
Constants.Ed25519PrivateKeyLength
40+
Constants.ExpandedEd25519PrivateKeyLength
41+
42+
member self.ToByteArray() =
43+
match self with
44+
| NormalEd25519 key -> key.ToByteArray()
45+
| ExpandedEd25519 key -> key.ToByteArray()
546

6-
type BlindedPublicKey =
7-
| BlindedPublicKey of array<byte>
47+
type ED25519PublicKey =
48+
| ED25519PublicKey of array<byte>
849

950
member self.ToByteArray() =
1051
match self with
11-
| BlindedPublicKey bytes -> bytes
52+
| ED25519PublicKey bytes -> bytes
1253

1354
type NTorOnionKey(bytes: array<byte>) =
1455
do

NOnion/Services/TorServiceHost.fs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -531,10 +531,10 @@ type TorServiceHost
531531
:?> Ed25519PublicKeyParameters)
532532
.GetEncoded())
533533
(descriptorSigningPublicKey.GetEncoded()
534-
|> BlindedPublicKey)
534+
|> ED25519PublicKey)
535535
(descriptorSigningPrivateKey.GetEncoded
536536
()
537-
|> ExpandedBlindedPrivateKey)
537+
|> Ed25519PrivateKey.FromBytes)
538538
Constants.HiddenServices.Descriptor.CertificateLifetime
539539

540540
let encKeyBytes =
@@ -560,10 +560,10 @@ type TorServiceHost
560560
CertType.IntroPointEncKeySignedByDescriptorSigningKey
561561
convertedX25519Key
562562
(descriptorSigningPublicKey.GetEncoded()
563-
|> BlindedPublicKey)
563+
|> ED25519PublicKey)
564564
(descriptorSigningPrivateKey.GetEncoded
565565
()
566-
|> ExpandedBlindedPrivateKey)
566+
|> Ed25519PrivateKey.FromBytes)
567567
Constants.HiddenServices.Descriptor.CertificateLifetime
568568

569569
{
@@ -690,7 +690,7 @@ type TorServiceHost
690690
CertType.ShortTermDescriptorSigningKeyByBlindedPublicKey
691691
(descriptorSigningPublicKey.GetEncoded())
692692
blindedPublicKey
693-
blindedPrivateKey
693+
(ExpandedEd25519 blindedPrivateKey)
694694
Constants.HiddenServices.Descriptor.CertificateLifetime
695695

696696
HiddenServiceFirstLayerDescriptorDocument.CreateNew

NOnion/Utility/CertificateUtil.fs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,8 @@ type Certificate =
7070
static member CreateNew
7171
certType
7272
(certifiedKey: array<byte>)
73-
(BlindedPublicKey signingPublicKey)
74-
(ExpandedBlindedPrivateKey signingPrivateKey)
73+
(ED25519PublicKey signingPublicKey)
74+
(signingPrivateKey: Ed25519PrivateKey)
7575
(lifetime: TimeSpan)
7676
=
7777
let unsignedCertificate =
@@ -101,13 +101,14 @@ type Certificate =
101101
let unsignedCertificateBytes = unsignedCertificate.ToBytes true
102102

103103
let signature =
104-
if signingPrivateKey.Length = 32 then
104+
match signingPrivateKey with
105+
| NormalEd25519 privateKey ->
105106
//Standard private key, we can sign with bouncycastle
106107
let signer = Ed25519Signer()
107108

108109
signer.Init(
109110
true,
110-
Ed25519PrivateKeyParameters(signingPrivateKey, 0)
111+
Ed25519PrivateKeyParameters(privateKey.ToByteArray(), 0)
111112
)
112113

113114
signer.BlockUpdate(
@@ -117,21 +118,18 @@ type Certificate =
117118
)
118119

119120
signer.GenerateSignature()
120-
elif signingPrivateKey.Length = 64 then
121+
| ExpandedEd25519 privateKey ->
121122
//Expanded private key, we have to sign with Chaos.NaCl
122123
let signature = Array.zeroCreate<byte> 64
123124

124125
Ed25519.SignWithPrehashedPrivateKey(
125126
ArraySegment signature,
126127
ArraySegment unsignedCertificateBytes,
127-
ArraySegment signingPrivateKey,
128+
ArraySegment(privateKey.ToByteArray()),
128129
ArraySegment signingPublicKey
129130
)
130131

131132
signature
132-
else
133-
failwith
134-
"Invalid private key, private key should either be 32 (standard ed25519) or 64 bytes (expanded ed25519 key)"
135133

136134
{ unsignedCertificate with
137135
Signature = signature

0 commit comments

Comments
 (0)