Skip to content

Commit 269e6e1

Browse files
committed
NOnion: use BouncyCastle types
Use BouncyCastle types for Ed25519PrivateKey.NormalEd25519 and ED25519PublicKey.
1 parent 6963b36 commit 269e6e1

File tree

5 files changed

+27
-34
lines changed

5 files changed

+27
-34
lines changed

NOnion/Crypto/HiddenServicesCipher.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ module HiddenServicesCipher =
8888
Ed25519Clamp blindingFactor
8989

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

9494
let CalculateExpandedBlindedPrivateKey

NOnion/Directory/TorDirectory.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -654,7 +654,7 @@ type TorDirectory =
654654
Array.concat
655655
[
656656
"store-at-idx" |> Encoding.ASCII.GetBytes
657-
blindedPublicKey
657+
blindedPublicKey.GetEncoded()
658658
replicaNum
659659
|> uint64
660660
|> IntegerSerialization.FromUInt64ToBigEndianByteArray

NOnion/KeyTypes.fs

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,7 @@
11
namespace NOnion
22

3+
open Org.BouncyCastle.Crypto.Parameters
34

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
145

156
type ExpandedEd25519PrivateKey(bytes: array<byte>) =
167
do
@@ -23,15 +14,16 @@ type ExpandedEd25519PrivateKey(bytes: array<byte>) =
2314
member self.ToByteArray() =
2415
bytes
2516

17+
[<RequireQualifiedAccess>]
2618
type Ed25519PrivateKey =
27-
| NormalEd25519 of NormalEd25519PrivateKey
28-
| ExpandedEd25519 of ExpandedEd25519PrivateKey
19+
| Normal of Ed25519PrivateKeyParameters
20+
| Expanded of ExpandedEd25519PrivateKey
2921

3022
static member FromBytes(bytes: array<byte>) : Ed25519PrivateKey =
3123
if bytes.Length = Constants.ExpandedEd25519PrivateKeyLength then
32-
ExpandedEd25519 <| ExpandedEd25519PrivateKey bytes
24+
Expanded <| ExpandedEd25519PrivateKey bytes
3325
elif bytes.Length = Constants.Ed25519PrivateKeyLength then
34-
NormalEd25519 <| NormalEd25519PrivateKey bytes
26+
Normal <| Ed25519PrivateKeyParameters(bytes, 0)
3527
else
3628
failwithf
3729
"Invalid private key (length=%d), private key should either be %d (standard ed25519) or %d bytes (expanded ed25519 key)"
@@ -41,15 +33,18 @@ type Ed25519PrivateKey =
4133

4234
member self.ToByteArray() =
4335
match self with
44-
| NormalEd25519 key -> key.ToByteArray()
45-
| ExpandedEd25519 key -> key.ToByteArray()
36+
| Normal key -> key.GetEncoded()
37+
| Expanded key -> key.ToByteArray()
4638

4739
type ED25519PublicKey =
48-
| ED25519PublicKey of array<byte>
40+
| ED25519PublicKey of Ed25519PublicKeyParameters
41+
42+
static member FromBytes(bytes: array<byte>) : ED25519PublicKey =
43+
ED25519PublicKey <| Ed25519PublicKeyParameters(bytes, 0)
4944

5045
member self.ToByteArray() =
5146
match self with
52-
| ED25519PublicKey bytes -> bytes
47+
| ED25519PublicKey publicKeyParams -> publicKeyParams.GetEncoded()
5348

5449
type NTorOnionKey(bytes: array<byte>) =
5550
do

NOnion/Services/TorServiceHost.fs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -530,11 +530,10 @@ type TorServiceHost
530530
((info.AuthKey.Public
531531
:?> Ed25519PublicKeyParameters)
532532
.GetEncoded())
533-
(descriptorSigningPublicKey.GetEncoded()
533+
(descriptorSigningPublicKey
534534
|> ED25519PublicKey)
535-
(descriptorSigningPrivateKey.GetEncoded
536-
()
537-
|> Ed25519PrivateKey.FromBytes)
535+
(descriptorSigningPrivateKey
536+
|> Ed25519PrivateKey.Normal)
538537
Constants.HiddenServices.Descriptor.CertificateLifetime
539538

540539
let encKeyBytes =
@@ -559,11 +558,10 @@ type TorServiceHost
559558
Certificate.CreateNew
560559
CertType.IntroPointEncKeySignedByDescriptorSigningKey
561560
convertedX25519Key
562-
(descriptorSigningPublicKey.GetEncoded()
561+
(descriptorSigningPublicKey
563562
|> ED25519PublicKey)
564-
(descriptorSigningPrivateKey.GetEncoded
565-
()
566-
|> Ed25519PrivateKey.FromBytes)
563+
(descriptorSigningPrivateKey
564+
|> Ed25519PrivateKey.Normal)
567565
Constants.HiddenServices.Descriptor.CertificateLifetime
568566

569567
{
@@ -690,7 +688,7 @@ type TorServiceHost
690688
CertType.ShortTermDescriptorSigningKeyByBlindedPublicKey
691689
(descriptorSigningPublicKey.GetEncoded())
692690
blindedPublicKey
693-
(ExpandedEd25519 blindedPrivateKey)
691+
(Ed25519PrivateKey.Expanded blindedPrivateKey)
694692
Constants.HiddenServices.Descriptor.CertificateLifetime
695693

696694
HiddenServiceFirstLayerDescriptorDocument.CreateNew

NOnion/Utility/CertificateUtil.fs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ type Certificate =
9292
CertificateExtension.Type =
9393
CertificateExtensionType.SignedWithEd25519Key
9494
Flags = 0uy
95-
Data = signingPublicKey
95+
Data = signingPublicKey.GetEncoded()
9696
}
9797
)
9898
Signature = Array.empty
@@ -102,13 +102,13 @@ type Certificate =
102102

103103
let signature =
104104
match signingPrivateKey with
105-
| NormalEd25519 privateKey ->
105+
| Ed25519PrivateKey.Normal privateKey ->
106106
//Standard private key, we can sign with bouncycastle
107107
let signer = Ed25519Signer()
108108

109109
signer.Init(
110110
true,
111-
Ed25519PrivateKeyParameters(privateKey.ToByteArray(), 0)
111+
Ed25519PrivateKeyParameters(privateKey.GetEncoded(), 0)
112112
)
113113

114114
signer.BlockUpdate(
@@ -118,15 +118,15 @@ type Certificate =
118118
)
119119

120120
signer.GenerateSignature()
121-
| ExpandedEd25519 privateKey ->
121+
| Ed25519PrivateKey.Expanded privateKey ->
122122
//Expanded private key, we have to sign with Chaos.NaCl
123123
let signature = Array.zeroCreate<byte> 64
124124

125125
Ed25519.SignWithPrehashedPrivateKey(
126126
ArraySegment signature,
127127
ArraySegment unsignedCertificateBytes,
128128
ArraySegment(privateKey.ToByteArray()),
129-
ArraySegment signingPublicKey
129+
ArraySegment(signingPublicKey.GetEncoded())
130130
)
131131

132132
signature

0 commit comments

Comments
 (0)