@@ -5,9 +5,10 @@ var ece = require('./ece.js');
55var base64 = require ( 'urlsafe-base64' ) ;
66var assert = require ( 'assert' ) ;
77
8- // Usage: node <this> <iterations> <maxsize>
8+ // Usage: node <this> <iterations> <maxsize|plaintext >
99var count = parseInt ( process . argv [ 2 ] , 10 ) || 20 ;
1010var maxLen = 100 ;
11+ var minLen = 3 ;
1112var plaintext = null ;
1213if ( process . argv . length >= 4 ) {
1314 if ( ! isNaN ( parseInt ( process . argv [ 3 ] , 10 ) ) ) {
@@ -41,10 +42,14 @@ function validate() {
4142 } ) ;
4243}
4344
44- function encryptDecrypt ( length , encryptParams , decryptParams ) {
45- decryptParams = decryptParams || encryptParams ;
46- logbuf ( 'Salt' , encryptParams . salt ) ;
47- var input = plaintext || crypto . randomBytes ( Math . min ( length , maxLen ) ) ;
45+ function encryptDecrypt ( length , encryptParams , decryptParams , oldVersion ) {
46+ if ( oldVersion ) {
47+ decryptParams . salt = base64 . encode ( crypto . randomBytes ( 16 ) ) ;
48+ encryptParams . salt = decryptParams . salt ;
49+ logbuf ( 'Salt' , encryptParams . salt ) ;
50+ }
51+ var input = plaintext ||
52+ crypto . randomBytes ( Math . max ( minLen , Math . min ( length , maxLen ) ) ) ;
4853 // var input = new Buffer('I am the walrus');
4954 logbuf ( 'Input' , input ) ;
5055 var encrypted = ece . encrypt ( input , encryptParams ) ;
@@ -55,52 +60,57 @@ function encryptDecrypt(length, encryptParams, decryptParams) {
5560 log ( '----- OK' ) ;
5661}
5762
58- function useExplicitKey ( ) {
59- var length = crypto . randomBytes ( 4 ) ;
63+ function useExplicitKey ( oldVersion ) {
64+ var length = crypto . randomBytes ( 4 ) . readUInt16BE ( 0 ) ;
6065 var params = {
6166 key : base64 . encode ( crypto . randomBytes ( 16 ) ) ,
62- salt : base64 . encode ( crypto . randomBytes ( 16 ) ) ,
63- rs : length . readUInt16BE ( 0 ) + 1
67+ rs : length + minLen
6468 } ;
6569 logbuf ( 'Key' , params . key ) ;
66- encryptDecrypt ( length . readUInt16BE ( 2 ) , params ) ;
70+ encryptDecrypt ( length , params , params , oldVersion ) ;
6771}
6872
69- function authenticationSecret ( ) {
70- var length = crypto . randomBytes ( 4 ) ;
73+ function authenticationSecret ( oldVersion ) {
74+ var length = crypto . randomBytes ( 4 ) . readUInt16BE ( 0 ) ;
7175 var params = {
7276 key : base64 . encode ( crypto . randomBytes ( 16 ) ) ,
73- salt : base64 . encode ( crypto . randomBytes ( 16 ) ) ,
74- rs : length . readUInt16BE ( 0 ) + 1 ,
77+ rs : length + minLen ,
7578 authSecret : base64 . encode ( crypto . randomBytes ( 16 ) )
7679 } ;
7780 logbuf ( 'Key' , params . key ) ;
7881 logbuf ( 'Context' , params . authSecret ) ;
79- encryptDecrypt ( length . readUInt16BE ( 2 ) , params ) ;
82+ encryptDecrypt ( length , params , params , oldVersion ) ;
8083}
8184
82- function exactlyOneRecord ( ) {
83- var length = Math . min ( crypto . randomBytes ( 2 ) . readUInt16BE ( 0 ) , maxLen ) ;
85+ function exactlyOneRecord ( oldVersion ) {
86+ var length = Math . min ( crypto . randomBytes ( 2 ) . readUInt16BE ( 0 ) + 1 , maxLen ) ;
8487 var params = {
8588 key : base64 . encode ( crypto . randomBytes ( 16 ) ) ,
86- salt : base64 . encode ( crypto . randomBytes ( 16 ) ) ,
87- rs : length + 1
89+ rs : length + 2 // add exactly the padding
8890 } ;
89- encryptDecrypt ( length , params ) ;
91+ encryptDecrypt ( length , params , params , oldVersion ) ;
9092}
9193
92- function detectTruncation ( ) {
93- var length = Math . min ( crypto . randomBytes ( 2 ) . readUInt16BE ( 0 ) , maxLen ) ;
94+ function detectTruncation ( oldVersion ) {
95+ var length = Math . min ( crypto . randomBytes ( 2 ) . readUInt16BE ( 0 ) + minLen , maxLen ) ;
9496 var params = {
9597 key : base64 . encode ( crypto . randomBytes ( 16 ) ) ,
96- salt : base64 . encode ( crypto . randomBytes ( 16 ) ) ,
97- rs : length + 1
98+ rs : length // so we get two records
9899 } ;
99- logbuf ( 'Salt' , params . salt ) ;
100+ var headerLen ;
101+ if ( oldVersion ) {
102+ params . salt = base64 . encode ( crypto . randomBytes ( 16 ) ) ;
103+ logbuf ( 'Salt' , params . salt ) ;
104+ headerLen = 0 ;
105+ } else {
106+ headerLen = 21 ; // no keyid
107+ }
100108 var input = crypto . randomBytes ( Math . min ( length , maxLen ) ) ;
101109 logbuf ( 'Input' , input ) ;
102110 var encrypted = ece . encrypt ( input , params ) ;
103- encrypted = encrypted . slice ( 0 , length + 1 + 16 ) ;
111+ var chunkLen = headerLen + params . rs + 16 ;
112+ assert . ok ( chunkLen < encrypted . length ) ;
113+ encrypted = encrypted . slice ( 0 , chunkLen ) ;
104114 logbuf ( 'Encrypted' , encrypted ) ;
105115 var ok = false ;
106116 try {
@@ -114,20 +124,19 @@ function detectTruncation() {
114124 }
115125}
116126
117- function useKeyId ( ) {
118- var length = crypto . randomBytes ( 4 ) ;
127+ function useKeyId ( oldVersion ) {
128+ var length = crypto . randomBytes ( 4 ) . readUInt16BE ( 0 ) ;
119129 var keyid = base64 . encode ( crypto . randomBytes ( 16 ) ) ;
120130 var key = crypto . randomBytes ( 16 ) ;
121131 ece . saveKey ( keyid , key ) ;
122132 var params = {
123133 keyid : keyid ,
124- salt : base64 . encode ( crypto . randomBytes ( 16 ) ) ,
125- rs : length . readUInt16BE ( 0 ) + 1
134+ rs : length + minLen
126135 } ;
127- encryptDecrypt ( length . readUInt16BE ( 2 ) , params ) ;
136+ encryptDecrypt ( length , params , params , oldVersion ) ;
128137}
129138
130- function useDH ( ) {
139+ function useDH ( oldVersion ) {
131140 // the static key is used by the receiver
132141 var staticKey = crypto . createECDH ( 'prime256v1' ) ;
133142 staticKey . generateKeys ( ) ;
@@ -148,34 +157,36 @@ function useDH() {
148157 logbuf ( 'Sender private' , ephemeralKey . getPrivateKey ( ) ) ;
149158 logbuf ( 'Sender public' , ephemeralKey . getPublicKey ( ) ) ;
150159
151- var length = crypto . randomBytes ( 4 ) ;
160+ var length = crypto . randomBytes ( 4 ) . readUInt16BE ( 0 ) ;
152161 var encryptParams = {
153162 keyid : ephemeralKeyId ,
154163 dh : base64 . encode ( staticKey . getPublicKey ( ) ) ,
155164 salt : base64 . encode ( crypto . randomBytes ( 16 ) ) ,
156- rs : length . readUInt16BE ( 0 ) + 1
165+ rs : length + minLen
157166 } ;
158167 var decryptParams = {
159168 keyid : staticKeyId ,
160169 dh : base64 . encode ( ephemeralKey . getPublicKey ( ) ) ,
161170 salt : encryptParams . salt ,
162171 rs : encryptParams . rs
163172 } ;
164- encryptDecrypt ( length . readUInt16BE ( 2 ) , encryptParams , decryptParams ) ;
173+ encryptDecrypt ( length , encryptParams , decryptParams , oldVersion ) ;
165174}
166175
167176validate ( ) ;
168177var i ;
169178for ( i = 0 ; i < count ; ++ i ) {
170- [ useExplicitKey ,
171- authenticationSecret ,
172- exactlyOneRecord ,
173- detectTruncation ,
174- useKeyId ,
175- useDH ,
176- ] . forEach ( function ( f ) {
177- log ( 'Test: ' + f . name ) ;
178- f ( ) ;
179+ [ true , false ] . forEach ( function ( oldVersion ) {
180+ [ useExplicitKey ,
181+ authenticationSecret ,
182+ exactlyOneRecord ,
183+ detectTruncation ,
184+ useKeyId ,
185+ useDH ,
186+ ] . forEach ( function ( f ) {
187+ log ( ( oldVersion ? 'aesgcm' : 'aes128gcm' ) + ' Test: ' + f . name ) ;
188+ f ( oldVersion ) ;
189+ } ) ;
179190 } ) ;
180191}
181192
0 commit comments