|
1 | 1 | using System; |
2 | 2 | using System.Runtime.InteropServices; |
| 3 | +using System.Security.Cryptography; |
| 4 | +using System.Text; |
3 | 5 |
|
4 | 6 | namespace PSModule |
5 | 7 | { |
@@ -139,6 +141,161 @@ public static int crypto_scalarmult_base(byte[] publicKey, byte[] privateKey) |
139 | 141 | return Native.crypto_scalarmult_base(publicKey, privateKey); |
140 | 142 | } |
141 | 143 |
|
| 144 | + // ---------- Base64-centric high-level API (see issue #52) ---------- |
| 145 | + // These helpers do base64/UTF-8 encoding and native interop in a single managed call, |
| 146 | + // avoiding the overhead of multiple PowerShell-level method invocations on the hot path. |
| 147 | + |
| 148 | + public sealed class KeyPairBase64 |
| 149 | + { |
| 150 | + public string PublicKey { get; } |
| 151 | + public string PrivateKey { get; } |
| 152 | + |
| 153 | + internal KeyPairBase64(string publicKey, string privateKey) |
| 154 | + { |
| 155 | + PublicKey = publicKey; |
| 156 | + PrivateKey = privateKey; |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + public static KeyPairBase64 GenerateKeyPairBase64() |
| 161 | + { |
| 162 | + var publicKey = new byte[PublicKeyBytes]; |
| 163 | + var privateKey = new byte[SecretKeyBytes]; |
| 164 | + try |
| 165 | + { |
| 166 | + if (Native.crypto_box_keypair(publicKey, privateKey) != 0) |
| 167 | + { |
| 168 | + throw new InvalidOperationException("Key pair generation failed."); |
| 169 | + } |
| 170 | + return new KeyPairBase64(Convert.ToBase64String(publicKey), Convert.ToBase64String(privateKey)); |
| 171 | + } |
| 172 | + finally |
| 173 | + { |
| 174 | + CryptographicOperations.ZeroMemory(privateKey); |
| 175 | + } |
| 176 | + } |
| 177 | + |
| 178 | + public static KeyPairBase64 GenerateKeyPairBase64(string seedText) |
| 179 | + { |
| 180 | + ArgumentNullException.ThrowIfNull(seedText); |
| 181 | + var publicKey = new byte[PublicKeyBytes]; |
| 182 | + var privateKey = new byte[SecretKeyBytes]; |
| 183 | + var seedSource = Encoding.UTF8.GetBytes(seedText); |
| 184 | + var seed = new byte[SeedBytes]; |
| 185 | + try |
| 186 | + { |
| 187 | + if (!SHA256.TryHashData(seedSource, seed, out var written) || written != SeedBytes) |
| 188 | + { |
| 189 | + throw new InvalidOperationException("Failed to derive seed bytes from input."); |
| 190 | + } |
| 191 | + if (Native.crypto_box_seed_keypair(publicKey, privateKey, seed) != 0) |
| 192 | + { |
| 193 | + throw new InvalidOperationException("Seeded key pair generation failed."); |
| 194 | + } |
| 195 | + return new KeyPairBase64(Convert.ToBase64String(publicKey), Convert.ToBase64String(privateKey)); |
| 196 | + } |
| 197 | + finally |
| 198 | + { |
| 199 | + CryptographicOperations.ZeroMemory(privateKey); |
| 200 | + CryptographicOperations.ZeroMemory(seed); |
| 201 | + CryptographicOperations.ZeroMemory(seedSource); |
| 202 | + } |
| 203 | + } |
| 204 | + |
| 205 | + public static string DerivePublicKeyBase64(string privateKeyBase64) |
| 206 | + { |
| 207 | + ArgumentNullException.ThrowIfNull(privateKeyBase64); |
| 208 | + var privateKey = DecodeBase64Exact(privateKeyBase64, SecretKeyBytes, nameof(privateKeyBase64)); |
| 209 | + var publicKey = new byte[PublicKeyBytes]; |
| 210 | + try |
| 211 | + { |
| 212 | + if (Native.crypto_scalarmult_base(publicKey, privateKey) != 0) |
| 213 | + { |
| 214 | + throw new InvalidOperationException("Unable to derive public key from private key."); |
| 215 | + } |
| 216 | + return Convert.ToBase64String(publicKey); |
| 217 | + } |
| 218 | + finally |
| 219 | + { |
| 220 | + CryptographicOperations.ZeroMemory(privateKey); |
| 221 | + } |
| 222 | + } |
| 223 | + |
| 224 | + public static string SealBase64(string plaintext, string publicKeyBase64) |
| 225 | + { |
| 226 | + ArgumentNullException.ThrowIfNull(plaintext); |
| 227 | + ArgumentNullException.ThrowIfNull(publicKeyBase64); |
| 228 | + var publicKey = DecodeBase64Exact(publicKeyBase64, PublicKeyBytes, nameof(publicKeyBase64)); |
| 229 | + var message = Encoding.UTF8.GetBytes(plaintext); |
| 230 | + var ciphertext = new byte[message.Length + SealBytes]; |
| 231 | + if (Native.crypto_box_seal(ciphertext, message, (ulong)message.LongLength, publicKey) != 0) |
| 232 | + { |
| 233 | + throw new InvalidOperationException("Encryption failed."); |
| 234 | + } |
| 235 | + return Convert.ToBase64String(ciphertext); |
| 236 | + } |
| 237 | + |
| 238 | + public static string OpenSealBase64(string ciphertextBase64, string privateKeyBase64) |
| 239 | + { |
| 240 | + return OpenSealBase64Core(ciphertextBase64, privateKeyBase64, publicKeyBase64: null); |
| 241 | + } |
| 242 | + |
| 243 | + public static string OpenSealBase64(string ciphertextBase64, string privateKeyBase64, string publicKeyBase64) |
| 244 | + { |
| 245 | + return OpenSealBase64Core(ciphertextBase64, privateKeyBase64, publicKeyBase64); |
| 246 | + } |
| 247 | + |
| 248 | + private static string OpenSealBase64Core(string ciphertextBase64, string privateKeyBase64, string publicKeyBase64) |
| 249 | + { |
| 250 | + ArgumentNullException.ThrowIfNull(ciphertextBase64); |
| 251 | + ArgumentNullException.ThrowIfNull(privateKeyBase64); |
| 252 | + |
| 253 | + var ciphertext = Convert.FromBase64String(ciphertextBase64); |
| 254 | + if (ciphertext.Length < SealBytes) |
| 255 | + { |
| 256 | + throw new ArgumentException($"Invalid sealed box. Expected at least {SealBytes} bytes but got {ciphertext.Length}.", nameof(ciphertextBase64)); |
| 257 | + } |
| 258 | + var privateKey = DecodeBase64Exact(privateKeyBase64, SecretKeyBytes, nameof(privateKeyBase64)); |
| 259 | + var publicKey = new byte[PublicKeyBytes]; |
| 260 | + var decrypted = new byte[ciphertext.Length - SealBytes]; |
| 261 | + try |
| 262 | + { |
| 263 | + if (string.IsNullOrEmpty(publicKeyBase64)) |
| 264 | + { |
| 265 | + if (Native.crypto_scalarmult_base(publicKey, privateKey) != 0) |
| 266 | + { |
| 267 | + throw new InvalidOperationException("Unable to derive public key from private key."); |
| 268 | + } |
| 269 | + } |
| 270 | + else |
| 271 | + { |
| 272 | + var providedPk = DecodeBase64Exact(publicKeyBase64, PublicKeyBytes, nameof(publicKeyBase64)); |
| 273 | + Buffer.BlockCopy(providedPk, 0, publicKey, 0, PublicKeyBytes); |
| 274 | + } |
| 275 | + |
| 276 | + if (Native.crypto_box_seal_open(decrypted, ciphertext, (ulong)ciphertext.LongLength, publicKey, privateKey) != 0) |
| 277 | + { |
| 278 | + throw new InvalidOperationException("Decryption failed."); |
| 279 | + } |
| 280 | + return Encoding.UTF8.GetString(decrypted); |
| 281 | + } |
| 282 | + finally |
| 283 | + { |
| 284 | + CryptographicOperations.ZeroMemory(privateKey); |
| 285 | + CryptographicOperations.ZeroMemory(decrypted); |
| 286 | + } |
| 287 | + } |
| 288 | + |
| 289 | + private static byte[] DecodeBase64Exact(string value, int expectedLength, string parameterName) |
| 290 | + { |
| 291 | + var bytes = Convert.FromBase64String(value); |
| 292 | + if (bytes.Length != expectedLength) |
| 293 | + { |
| 294 | + throw new ArgumentException($"Invalid base64 value. Expected {expectedLength} bytes but got {bytes.Length}.", parameterName); |
| 295 | + } |
| 296 | + return bytes; |
| 297 | + } |
| 298 | + |
142 | 299 | private static int GetRequiredLength(UIntPtr length) |
143 | 300 | { |
144 | 301 | var value = length.ToUInt64(); |
|
0 commit comments