File tree Expand file tree Collapse file tree 2 files changed +26
-4
lines changed
Expand file tree Collapse file tree 2 files changed +26
-4
lines changed Original file line number Diff line number Diff line change @@ -75,13 +75,13 @@ export default class SymmetricKey extends BigNumber {
7575 decrypt ( msg : number [ ] | string , enc ?: 'hex' | 'utf8' ) : string | number [ ] {
7676 msg = toArray ( msg , enc )
7777
78- if ( msg . length < 48 ) {
79- throw new Error ( 'Ciphertext too short' )
80- }
81-
8278 const ivLength = 32
8379 const tagLength = 16
8480
81+ if ( msg . length < ivLength + tagLength ) {
82+ throw new Error ( 'Ciphertext too short' )
83+ }
84+
8585 const iv = msg . slice ( 0 , ivLength )
8686 const tagStart = msg . length - tagLength
8787 const ciphertext = msg . slice ( ivLength , tagStart )
Original file line number Diff line number Diff line change @@ -102,5 +102,27 @@ describe('SymmetricKey', () => {
102102
103103 expect ( decrypted ) . toBe ( plaintext )
104104 } )
105+
106+ it ( 'throws "Ciphertext too short" for inputs shorter than IV + tag' , ( ) => {
107+ const shortCipherArray = new Array ( 47 ) . fill ( 0 )
108+ expect ( ( ) => {
109+ KEYS [ 0 ] . decrypt ( shortCipherArray )
110+ } ) . toThrow ( new Error ( 'Ciphertext too short' ) )
111+ } )
112+
113+ it ( 'throws "Ciphertext too short" for hex-encoded inputs shorter than IV + tag' , ( ) => {
114+ const shortBuffer = Buffer . alloc ( 47 , 0 )
115+ const shortHex = shortBuffer . toString ( 'hex' )
116+
117+ expect ( ( ) => {
118+ KEYS [ 0 ] . decrypt ( shortHex , 'hex' )
119+ } ) . toThrow ( new Error ( 'Ciphertext too short' ) )
120+ } )
121+
122+ it ( 'still throws "Decryption failed!" for structurally valid but wrong ciphertext' , ( ) => {
123+ expect ( ( ) => {
124+ KEYS [ 2 ] . decrypt ( CIPHERTEXT_1 , 'hex' )
125+ } ) . toThrow ( new Error ( 'Decryption failed!' ) )
126+ } )
105127 } )
106128} )
You can’t perform that action at this time.
0 commit comments