| 
									
										
										
										
											2021-03-23 17:50:02 -07:00
										 |  |  | // Copyright 2021 Signal Messenger, LLC
 | 
					
						
							|  |  |  | // SPDX-License-Identifier: AGPL-3.0-only
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import { assert } from 'chai'; | 
					
						
							|  |  |  | import crypto from 'crypto'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-07 14:27:40 -07:00
										 |  |  | import { | 
					
						
							| 
									
										
										
										
											2021-09-23 17:49:05 -07:00
										 |  |  |   CipherType, | 
					
						
							| 
									
										
										
										
											2021-04-07 14:27:40 -07:00
										 |  |  |   HashType, | 
					
						
							|  |  |  |   hash, | 
					
						
							|  |  |  |   sign, | 
					
						
							|  |  |  |   encrypt, | 
					
						
							|  |  |  |   decrypt, | 
					
						
							| 
									
										
										
										
											2021-09-23 17:49:05 -07:00
										 |  |  | } from '../../Crypto'; | 
					
						
							| 
									
										
										
										
											2021-03-23 17:50:02 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-23 17:49:05 -07:00
										 |  |  | describe('SignalContext.Crypto', () => { | 
					
						
							| 
									
										
										
										
											2021-03-23 17:50:02 -07:00
										 |  |  |   describe('hash', () => { | 
					
						
							|  |  |  |     it('returns SHA512 hash of the input', () => { | 
					
						
							| 
									
										
										
										
											2021-09-23 17:49:05 -07:00
										 |  |  |       const result = hash(HashType.size512, Buffer.from('signal')); | 
					
						
							| 
									
										
										
										
											2021-03-23 17:50:02 -07:00
										 |  |  |       assert.strictEqual( | 
					
						
							|  |  |  |         Buffer.from(result).toString('base64'), | 
					
						
							|  |  |  |         'WxneQjrfSlY95Bi+SAzDAr2cf3mxUXePeNYn6DILN4a8NFr9VelTbP5tGHdthi+' + | 
					
						
							|  |  |  |           'mrJLqMZd1I6w8CxCnmJ/OFw==' | 
					
						
							|  |  |  |       ); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   describe('sign', () => { | 
					
						
							|  |  |  |     it('returns hmac SHA256 hash of the input', () => { | 
					
						
							| 
									
										
										
										
											2021-09-23 17:49:05 -07:00
										 |  |  |       const result = sign(Buffer.from('secret'), Buffer.from('signal')); | 
					
						
							| 
									
										
										
										
											2021-03-23 17:50:02 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |       assert.strictEqual( | 
					
						
							|  |  |  |         Buffer.from(result).toString('base64'), | 
					
						
							|  |  |  |         '5ewbITW27c1F7dluF9KwGcVQSxmZp6mpVhPj3ww1Sh8=' | 
					
						
							|  |  |  |       ); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   describe('encrypt+decrypt', () => { | 
					
						
							|  |  |  |     it('returns original input', () => { | 
					
						
							| 
									
										
										
										
											2021-09-23 17:49:05 -07:00
										 |  |  |       const iv = crypto.randomBytes(16); | 
					
						
							|  |  |  |       const key = crypto.randomBytes(32); | 
					
						
							|  |  |  |       const input = Buffer.from('plaintext'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       const ciphertext = encrypt(CipherType.AES256CBC, { | 
					
						
							|  |  |  |         key, | 
					
						
							|  |  |  |         iv, | 
					
						
							|  |  |  |         plaintext: input, | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |       const plaintext = decrypt(CipherType.AES256CBC, { | 
					
						
							|  |  |  |         key, | 
					
						
							|  |  |  |         iv, | 
					
						
							|  |  |  |         ciphertext, | 
					
						
							|  |  |  |       }); | 
					
						
							| 
									
										
										
										
											2021-03-23 17:50:02 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |       assert.strictEqual(Buffer.from(plaintext).toString(), 'plaintext'); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | }); |