forked from signalapp/Signal-Desktop
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use non-subtle crypto in libsignal-protocol
- Loading branch information
1 parent
9e9d1c8
commit 919259c
Showing
6 changed files
with
120 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// Copyright 2021 Signal Messenger, LLC | ||
// SPDX-License-Identifier: AGPL-3.0-only | ||
|
||
import { assert } from 'chai'; | ||
import crypto from 'crypto'; | ||
|
||
import { typedArrayToArrayBuffer as toArrayBuffer } from '../../Crypto'; | ||
import { hash, sign, encrypt, decrypt } from '../../util/synchronousCrypto'; | ||
|
||
describe('synchronousCrypto', () => { | ||
describe('hash', () => { | ||
it('returns SHA512 hash of the input', () => { | ||
const result = hash(toArrayBuffer(Buffer.from('signal'))); | ||
assert.strictEqual( | ||
Buffer.from(result).toString('base64'), | ||
'WxneQjrfSlY95Bi+SAzDAr2cf3mxUXePeNYn6DILN4a8NFr9VelTbP5tGHdthi+' + | ||
'mrJLqMZd1I6w8CxCnmJ/OFw==' | ||
); | ||
}); | ||
}); | ||
|
||
describe('sign', () => { | ||
it('returns hmac SHA256 hash of the input', () => { | ||
const result = sign( | ||
toArrayBuffer(Buffer.from('secret')), | ||
toArrayBuffer(Buffer.from('signal')) | ||
); | ||
|
||
assert.strictEqual( | ||
Buffer.from(result).toString('base64'), | ||
'5ewbITW27c1F7dluF9KwGcVQSxmZp6mpVhPj3ww1Sh8=' | ||
); | ||
}); | ||
}); | ||
|
||
describe('encrypt+decrypt', () => { | ||
it('returns original input', () => { | ||
const iv = crypto.randomBytes(16); | ||
const key = crypto.randomBytes(32); | ||
const input = Buffer.from('plaintext'); | ||
|
||
const ciphertext = encrypt(key, input, iv); | ||
const plaintext = decrypt(key, ciphertext, iv); | ||
|
||
assert.strictEqual(Buffer.from(plaintext).toString(), 'plaintext'); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// Copyright 2021 Signal Messenger, LLC | ||
// SPDX-License-Identifier: AGPL-3.0-only | ||
|
||
import crypto from 'crypto'; | ||
|
||
import { typedArrayToArrayBuffer as toArrayBuffer } from '../Crypto'; | ||
|
||
export function sign(key: ArrayBuffer, data: ArrayBuffer): ArrayBuffer { | ||
return toArrayBuffer( | ||
crypto | ||
.createHmac('sha256', Buffer.from(key)) | ||
.update(Buffer.from(data)) | ||
.digest() | ||
); | ||
} | ||
|
||
export function hash(data: ArrayBuffer): ArrayBuffer { | ||
return toArrayBuffer( | ||
crypto.createHash('sha512').update(Buffer.from(data)).digest() | ||
); | ||
} | ||
|
||
export function encrypt( | ||
key: ArrayBuffer, | ||
data: ArrayBuffer, | ||
iv: ArrayBuffer | ||
): ArrayBuffer { | ||
const cipher = crypto.createCipheriv( | ||
'aes-256-cbc', | ||
Buffer.from(key), | ||
Buffer.from(iv) | ||
); | ||
const encrypted = Buffer.concat([ | ||
cipher.update(Buffer.from(data)), | ||
cipher.final(), | ||
]); | ||
|
||
return toArrayBuffer(encrypted); | ||
} | ||
|
||
export function decrypt( | ||
key: ArrayBuffer, | ||
data: ArrayBuffer, | ||
iv: ArrayBuffer | ||
): ArrayBuffer { | ||
const cipher = crypto.createDecipheriv( | ||
'aes-256-cbc', | ||
Buffer.from(key), | ||
Buffer.from(iv) | ||
); | ||
const decrypted = Buffer.concat([ | ||
cipher.update(Buffer.from(data)), | ||
cipher.final(), | ||
]); | ||
|
||
return toArrayBuffer(decrypted); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters