Skip to content

Commit f2573d8

Browse files
committed
Ed25519: Add tor key-blinding function
Upstream PR: CodesInChaos#18
1 parent 48fa073 commit f2573d8

1 file changed

Lines changed: 24 additions & 0 deletions

File tree

Chaos.NaCl/Ed25519.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,5 +143,29 @@ public static void KeyExchange(ArraySegment<byte> sharedKey, ArraySegment<byte>
143143
FieldOperations.fe_tobytes(sharedKey.Array, sharedKey.Offset, ref sharedMontgomeryX);
144144
MontgomeryCurve25519.KeyExchangeOutputHashNaCl(sharedKey.Array, sharedKey.Offset);
145145
}
146+
147+
public static bool CalculateBlindedPublicKey(byte[] publicKey, byte[] blindingFator, out byte[] output)
148+
{
149+
if (publicKey is null)
150+
throw new ArgumentNullException("publicKey.Array");
151+
if (publicKey.Length != PublicKeySizeInBytes)
152+
throw new ArgumentException("publicKey.Count != 32");
153+
154+
output = new byte[PublicKeySizeInBytes];
155+
156+
byte[] zeros = new byte[PublicKeySizeInBytes];
157+
byte[] pkCopy = new byte[PublicKeySizeInBytes];
158+
Array.Copy(publicKey, pkCopy, PublicKeySizeInBytes);
159+
pkCopy[31] ^= (1 << 7);
160+
161+
if (GroupOperations.ge_frombytes_negate_vartime(out var A, pkCopy, 0) != 0)
162+
return false;
163+
164+
/* There isn't a regular ge_scalarmult -- we have to do tweak*A + zero*B. */
165+
GroupOperations.ge_double_scalarmult_vartime(out var Aprime, blindingFator, ref A, zeros);
166+
GroupOperations.ge_tobytes(output,0, ref Aprime);
167+
168+
return true;
169+
}
146170
}
147171
}

0 commit comments

Comments
 (0)