|
1 | 1 | import { |
2 | 2 | decryptAndVerifySignedData, |
3 | 3 | encryptAndDetachSignData, |
| 4 | + decryptAndVerifyIncomingMessages, |
| 5 | + encryptAndAuthOutgoingMessages, |
4 | 6 | verifySignedData, |
5 | 7 | SIGNATURE_DATE_TOLERANCE_MS, |
6 | 8 | } from '../../../../src/tss/ecdsa-dkls/commsLayer'; |
@@ -165,3 +167,184 @@ describe('DKLS Communication Layer', function () { |
165 | 167 | }); |
166 | 168 | }); |
167 | 169 | }); |
| 170 | + |
| 171 | +describe('DKLS encryptAndAuthOutgoingMessages / decryptAndVerifyIncomingMessages', function () { |
| 172 | + let partyAKey: { publicKey: string; privateKey: string }; |
| 173 | + let partyBKey: { publicKey: string; privateKey: string }; |
| 174 | + let tampererKey: { publicKey: string; privateKey: string }; |
| 175 | + |
| 176 | + before(async function () { |
| 177 | + openpgp.config.rejectCurves = new Set(); |
| 178 | + partyAKey = await openpgp.generateKey({ |
| 179 | + userIDs: [{ name: 'partyA', email: 'a@test.com' }], |
| 180 | + curve: 'secp256k1', |
| 181 | + }); |
| 182 | + partyBKey = await openpgp.generateKey({ |
| 183 | + userIDs: [{ name: 'partyB', email: 'b@test.com' }], |
| 184 | + curve: 'secp256k1', |
| 185 | + }); |
| 186 | + tampererKey = await openpgp.generateKey({ |
| 187 | + userIDs: [{ name: 'tamperer', email: 'evil@test.com' }], |
| 188 | + curve: 'secp256k1', |
| 189 | + }); |
| 190 | + }); |
| 191 | + |
| 192 | + const partyAId = 0; |
| 193 | + const partyBId = 2; |
| 194 | + |
| 195 | + it('should sign signatureR and verify it on receive', async function () { |
| 196 | + const payload = Buffer.from('deadbeef', 'hex').toString('base64'); |
| 197 | + const signatureRBytes = Buffer.from('cafebabe', 'hex').toString('base64'); |
| 198 | + |
| 199 | + const encrypted = await encryptAndAuthOutgoingMessages( |
| 200 | + { |
| 201 | + p2pMessages: [], |
| 202 | + broadcastMessages: [{ from: partyAId, payload, signatureR: signatureRBytes }], |
| 203 | + }, |
| 204 | + [{ partyId: partyBId, gpgKey: partyBKey.publicKey }], |
| 205 | + [{ partyId: partyAId, gpgKey: partyAKey.privateKey }] |
| 206 | + ); |
| 207 | + |
| 208 | + const broadcastMsg = encrypted.broadcastMessages[0]; |
| 209 | + broadcastMsg.signatureR!.message.should.equal(signatureRBytes); |
| 210 | + broadcastMsg.signatureR!.signature.should.not.equal(''); |
| 211 | + |
| 212 | + const decrypted = await decryptAndVerifyIncomingMessages( |
| 213 | + { |
| 214 | + p2pMessages: [], |
| 215 | + broadcastMessages: [broadcastMsg], |
| 216 | + }, |
| 217 | + [{ partyId: partyAId, gpgKey: partyAKey.publicKey }], |
| 218 | + [{ partyId: partyBId, gpgKey: partyBKey.privateKey }] |
| 219 | + ); |
| 220 | + |
| 221 | + decrypted.broadcastMessages[0].signatureR!.should.equal(signatureRBytes); |
| 222 | + }); |
| 223 | + |
| 224 | + it('should reject a tampered signatureR on receive', async function () { |
| 225 | + const payload = Buffer.from('deadbeef', 'hex').toString('base64'); |
| 226 | + const signatureRBytes = Buffer.from('cafebabe', 'hex').toString('base64'); |
| 227 | + |
| 228 | + const encrypted = await encryptAndAuthOutgoingMessages( |
| 229 | + { |
| 230 | + p2pMessages: [], |
| 231 | + broadcastMessages: [{ from: partyAId, payload, signatureR: signatureRBytes }], |
| 232 | + }, |
| 233 | + [{ partyId: partyBId, gpgKey: partyBKey.publicKey }], |
| 234 | + [{ partyId: partyAId, gpgKey: partyAKey.privateKey }] |
| 235 | + ); |
| 236 | + |
| 237 | + const tampered = { |
| 238 | + ...encrypted.broadcastMessages[0], |
| 239 | + signatureR: { |
| 240 | + message: Buffer.from('00000000', 'hex').toString('base64'), |
| 241 | + signature: encrypted.broadcastMessages[0].signatureR!.signature, |
| 242 | + }, |
| 243 | + }; |
| 244 | + |
| 245 | + await decryptAndVerifyIncomingMessages( |
| 246 | + { p2pMessages: [], broadcastMessages: [tampered] }, |
| 247 | + [{ partyId: partyAId, gpgKey: partyAKey.publicKey }], |
| 248 | + [{ partyId: partyBId, gpgKey: partyBKey.privateKey }] |
| 249 | + ).should.be.rejectedWith(`Failed to authenticate signatureR in broadcast message from party: ${partyAId}`); |
| 250 | + }); |
| 251 | + |
| 252 | + it('should reject a signatureR signed by a different key', async function () { |
| 253 | + const payload = Buffer.from('deadbeef', 'hex').toString('base64'); |
| 254 | + const signatureRBytes = Buffer.from('cafebabe', 'hex').toString('base64'); |
| 255 | + |
| 256 | + const encrypted = await encryptAndAuthOutgoingMessages( |
| 257 | + { |
| 258 | + p2pMessages: [], |
| 259 | + broadcastMessages: [{ from: partyAId, payload, signatureR: signatureRBytes }], |
| 260 | + }, |
| 261 | + [{ partyId: partyBId, gpgKey: partyBKey.publicKey }], |
| 262 | + [{ partyId: partyAId, gpgKey: tampererKey.privateKey }] |
| 263 | + ); |
| 264 | + |
| 265 | + await decryptAndVerifyIncomingMessages( |
| 266 | + { p2pMessages: [], broadcastMessages: [encrypted.broadcastMessages[0]] }, |
| 267 | + [{ partyId: partyAId, gpgKey: partyAKey.publicKey }], |
| 268 | + [{ partyId: partyBId, gpgKey: partyBKey.privateKey }] |
| 269 | + ).should.be.rejectedWith(`Failed to authenticate broadcast message from party: ${partyAId}`); |
| 270 | + }); |
| 271 | + |
| 272 | + it('should handle broadcast messages without signatureR unchanged', async function () { |
| 273 | + const payload = Buffer.from('deadbeef', 'hex').toString('base64'); |
| 274 | + |
| 275 | + const encrypted = await encryptAndAuthOutgoingMessages( |
| 276 | + { |
| 277 | + p2pMessages: [], |
| 278 | + broadcastMessages: [{ from: partyAId, payload }], |
| 279 | + }, |
| 280 | + [{ partyId: partyBId, gpgKey: partyBKey.publicKey }], |
| 281 | + [{ partyId: partyAId, gpgKey: partyAKey.privateKey }] |
| 282 | + ); |
| 283 | + |
| 284 | + (encrypted.broadcastMessages[0].signatureR === undefined).should.equal(true); |
| 285 | + |
| 286 | + const decrypted = await decryptAndVerifyIncomingMessages( |
| 287 | + { p2pMessages: [], broadcastMessages: [encrypted.broadcastMessages[0]] }, |
| 288 | + [{ partyId: partyAId, gpgKey: partyAKey.publicKey }], |
| 289 | + [{ partyId: partyBId, gpgKey: partyBKey.privateKey }] |
| 290 | + ); |
| 291 | + |
| 292 | + (decrypted.broadcastMessages[0].signatureR === undefined).should.equal(true); |
| 293 | + }); |
| 294 | + |
| 295 | + it('should skip signatureR verification when signatureR field is omitted (soft downgrade)', async function () { |
| 296 | + const payload = Buffer.from('deadbeef', 'hex').toString('base64'); |
| 297 | + const signatureRBytes = Buffer.from('cafebabe', 'hex').toString('base64'); |
| 298 | + |
| 299 | + const encrypted = await encryptAndAuthOutgoingMessages( |
| 300 | + { |
| 301 | + p2pMessages: [], |
| 302 | + broadcastMessages: [{ from: partyAId, payload, signatureR: signatureRBytes }], |
| 303 | + }, |
| 304 | + [{ partyId: partyBId, gpgKey: partyBKey.publicKey }], |
| 305 | + [{ partyId: partyAId, gpgKey: partyAKey.privateKey }] |
| 306 | + ); |
| 307 | + |
| 308 | + const broadcastWithoutAuthR = { |
| 309 | + from: encrypted.broadcastMessages[0].from, |
| 310 | + payload: encrypted.broadcastMessages[0].payload, |
| 311 | + }; |
| 312 | + |
| 313 | + const decrypted = await decryptAndVerifyIncomingMessages( |
| 314 | + { p2pMessages: [], broadcastMessages: [broadcastWithoutAuthR] }, |
| 315 | + [{ partyId: partyAId, gpgKey: partyAKey.publicKey }], |
| 316 | + [{ partyId: partyBId, gpgKey: partyBKey.privateKey }] |
| 317 | + ); |
| 318 | + |
| 319 | + (decrypted.broadcastMessages[0].signatureR === undefined).should.equal(true); |
| 320 | + }); |
| 321 | + |
| 322 | + it('should reject when signatureR message is present but signature is empty string', async function () { |
| 323 | + const payload = Buffer.from('deadbeef', 'hex').toString('base64'); |
| 324 | + const signatureRBytes = Buffer.from('cafebabe', 'hex').toString('base64'); |
| 325 | + |
| 326 | + const encrypted = await encryptAndAuthOutgoingMessages( |
| 327 | + { |
| 328 | + p2pMessages: [], |
| 329 | + broadcastMessages: [{ from: partyAId, payload, signatureR: signatureRBytes }], |
| 330 | + }, |
| 331 | + [{ partyId: partyBId, gpgKey: partyBKey.publicKey }], |
| 332 | + [{ partyId: partyAId, gpgKey: partyAKey.privateKey }] |
| 333 | + ); |
| 334 | + |
| 335 | + const strippedSig = { |
| 336 | + ...encrypted.broadcastMessages[0], |
| 337 | + signatureR: { |
| 338 | + message: encrypted.broadcastMessages[0].signatureR!.message, |
| 339 | + signature: '', |
| 340 | + }, |
| 341 | + }; |
| 342 | + |
| 343 | + // Empty armor: OpenPGP fails parsing before verifySignedData returns false. |
| 344 | + await decryptAndVerifyIncomingMessages( |
| 345 | + { p2pMessages: [], broadcastMessages: [strippedSig] }, |
| 346 | + [{ partyId: partyAId, gpgKey: partyAKey.publicKey }], |
| 347 | + [{ partyId: partyBId, gpgKey: partyBKey.privateKey }] |
| 348 | + ).should.be.rejected(); |
| 349 | + }); |
| 350 | +}); |
0 commit comments