-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSOAEP.js
221 lines (184 loc) · 6.81 KB
/
SOAEP.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/*
* SOAEP.js
* See SOAEP.readme.txt for further information.
*
* Copyright(c) 2009 Atsushi Oka [ http://oka.nu/ ]
* This script file is distributed under the LGPL
*/
// Sub-Optimal Asymmetric Encryption Padding
function initSOAEP( packages ) {
__unit( "SOAEP.js" );
__uses( "packages.js" );
// __uses( "trace.js" );
// __uses( "elapse.js" );
__uses( "binary.js" );
__uses( "Cipher.js" );
__uses( "SecureRandom.js" );
var SecureRandom = __import( this,"titaniumcore.crypto.SecureRandom" );
var Cipher = __import( this,"titaniumcore.crypto.Cipher" );
// var encrypt = Cipher.create( algorithm, Cipher.ENCRYPT, Cipher.ECB, Cipher.NO_PADDING );
// var decrypt = Cipher.create( algorithm, Cipher.DECRYPT, Cipher.ECB, Cipher.NO_PADDING );
// var inputText = dataBytes.concat();
// var cipherText = encrypt.execute( keyBytes, inputText.concat() );
// var outputText = decrypt.execute( keyBytes, cipherText.concat() );
function SOAEP( random, cipherAlgorithm, withBitPadding ) {
if ( withBitPadding == null ) {
withBitPadding = true;
}
this.random = random == null ? new SecureRandom() : random ;
this.cipherAlgorithm = cipherAlgorithm == null ? Cipher.algorithm( Cipher.TWOFISH ) : cipherAlgorithm;
this.withBitPadding = withBitPadding;
}
function maxsize( length ){
var blocksize = this.cipherAlgorithm.blocksize;
return length-blocksize + ( this.withBitPadding ? -1 : 0 );
}
function encode( input, length ) {
// initialization
var blocksize = this.cipherAlgorithm.blocksize;
// if length is not specified, calculate the sufficient length for the length of input data.
if ( length == null ) {
length = Math.ceil( (1+input.length) / blocksize ) * blocksize + blocksize;
}
// alert( ""+input.length+"/"+length );
if ( 0!=(length % blocksize ) ) {
throw "SOAEP.encode() error : length("+length+") must be a multiple of " + blocksize + " since the block size of " + this.cipherAlgorithm.name + " is " +blocksize;
}
var blockCount = Math.floor( length / blocksize );
// var maxsize = length - blocksize -1;
var maxsize = this.maxsize( length );
// trim input data if input data length exceeds the specified length by "length" parameter.
// -1 for the terminater byte.
if ( maxsize < input.length ) {
throw "SOAEP.encode() error : the size of input data (" + input.length + " bytes) must not exceed " + maxsize + "byte. \n"+maxsize+"(maxsize)="+length+"(bit-length of the RSA key )-" + blocksize+"( blocksize of the cipher algorithm)" + ( this.withBitPadding ? "-1(size of the terminator byte)" : "" ) ;
// input = input.slice( 0, length-blocksize -1 );
}
// Create output array.
var output = new Array( length );
// Create a random token block. Use it as synmmetoric cipher key later.
var randomized = new Array( blocksize );
this.random.nextBytes( randomized );
// Copy input text to output
for ( var i=0; i<input.length; i++ ) {
output[i] = input[i];
}
// Pad input data with bit-padding-method to make it fit to the length.
if ( this.withBitPadding ) {
output[input.length] = 0x80;
for ( var i=input.length+1; i<length-blocksize; i++ ) {
output[i] = 0x00;
}
} else {
for ( var i=input.length; i<length-blocksize; i++ ) {
output[i] = 0x00;
}
}
// Copy the random token block to the last block in output.
for ( var i=length-blocksize,j=0; i<length; i++,j++ ) {
output[i] = randomized[j];
}
// Encode blocks as CBC mode in reverse order.
var iv = randomized.concat();
this.cipherAlgorithm.open( randomized );
for ( var idx=blockCount-2; 0<=idx; idx-- ) {
var offset = idx*blocksize;
for ( var i=offset,j=0; j<blocksize; i++,j++ ) {
output[i] ^= iv[j];
}
this.cipherAlgorithm.encrypt( output,offset );
iv = output.slice( offset, offset+blocksize );
}
this.cipherAlgorithm.close();
// Encode the blocks again in normal order.
// Use first block as cipher-key and iv.
var firstblock = output.slice( 0, blocksize );
iv = firstblock;
this.cipherAlgorithm.open( firstblock );
for ( var idx=1; idx<blockCount; idx++ ) {
var offset = idx*blocksize;
for ( var i=offset,j=0; j<blocksize; i++,j++ ) {
output[i] ^= iv[j];
}
this.cipherAlgorithm.encrypt( output, offset );
iv = output.slice( offset, offset+blocksize );
}
this.cipherAlgorithm.close();
return output;
}
function decode( input ) {
var length = input.length
// initialization
var blocksize = this.cipherAlgorithm.blocksize;
if ( 0!=length % blocksize ) {
throw "SOAEP.decode() error : length "+length+" must be a multiple of " + blocksize;
}
var blockCount = Math.floor( length / blocksize );
// create output array.
var output = input.concat();
// Decode 1
// Use first block as cipher-key and iv.
var firstblock = output.slice( 0, blocksize );
var iv = firstblock;
this.cipherAlgorithm.open( firstblock );
for ( var idx=1; idx<blockCount; idx++ ) {
var offset = idx*blocksize;
var iv2 = output.slice( offset, offset+blocksize );
this.cipherAlgorithm.decrypt( output, offset );
for ( var i=offset,j=0; j<blocksize; i++,j++ ) {
output[i] ^= iv[j];
// trace("i="+i)
}
iv = iv2;
}
// Decode 2
// Encode blocks as CBC mode in reverse order.
var lastblock_offset = (blockCount-1)*blocksize;
var lastblock = output.slice( lastblock_offset, lastblock_offset + blocksize );
var iv = lastblock;
this.cipherAlgorithm.open( lastblock );
for ( var idx=blockCount-2; 0<=idx; idx-- ) {
var offset = idx*blocksize;
var iv2=output.slice( offset, offset+blocksize );
this.cipherAlgorithm.decrypt( output, offset );
for ( var i=offset,j=0; j<blocksize; i++,j++ ) {
output[i] ^= iv[j];
}
iv = iv2;
}
this.cipherAlgorithm.close();
// trace( "SOAEP:" + base16(output ) );
// Chop the remaining by bit-padding-method.
if ( this.withBitPadding ) {
for ( var i=lastblock_offset-1; 0<=i; i-- ) {
// trace( output[i].toString(16) );
if ( output[i]==0x80 ) {
output = output.slice( 0,i );
break;
} else if ( output[i] ==0x00 ) {
} else {
throw "decode() : found illegal character 0x" + output[i].toString(16).toUpperCase();
//output = output.slice( 0,i );
//break;
}
}
} else {
output = output.slice( 0,lastblock_offset );
}
return output;
}
function blocksize(){
return this.cipherAlgorithm.blocksize;
}
SOAEP.prototype.encode = encode;
SOAEP.prototype.decode = decode;
SOAEP.prototype.maxsize = maxsize;
SOAEP.prototype.blocksize = blocksize;
function create( random, cipherAlgorithm, withBitPadding ) {
return new SOAEP( random, cipherAlgorithm, withBitPadding );
}
SOAEP.create = create;
// __package( packages, path ).SOAEP = SOAEP;
__export( packages, "titaniumcore.crypto.SOAEP", SOAEP );
}
initSOAEP( this );
// vim:ts=8 sw=4:noexpandtab: