ruby-vnc 1.1.0 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/cipher/des.rb DELETED
@@ -1,439 +0,0 @@
1
- module Cipher
2
- #
3
- # = Brief
4
- #
5
- # The Cipher::DES class allows for encryption and decryption of plain
6
- # text using the "Data Encryption Standard". This version is the modified
7
- # version which is part of the VNC authentication scheme.
8
- #
9
- # Usage is pretty straight forward:
10
- #
11
- # des = Cipher::DES.new 'mysecretkey', :encrypt
12
- # str = des.update 'plain text'
13
- # str << des.update 'more plain text'
14
- # str << final
15
- #
16
- # Or just use the shortcut class methods:
17
- #
18
- # str = Cipher::DES.encrypt 'mysecretkey', 'plain text'
19
- #
20
- # = About
21
- #
22
- # This code was ported from the file "d3des.c", for portability reasons.
23
- # It is not expected to be quick, but is only being used currently for the
24
- # VNC authentication handshake. If you wanted to cipher a lot of text, you
25
- # should probably compile the original C as an extension.
26
- #
27
- # I've included the following copyright info from the C source verbatim:
28
- #
29
- # This is D3DES (V5.09) by Richard Outerbridge with the double and
30
- # triple-length support removed for use in VNC. Also the bytebit[] array
31
- # has been reversed so that the most significant bit in each byte of the
32
- # key is ignored, not the least significant.
33
- #
34
- # These changes are:
35
- # Copyright (C) 1999 AT&T Laboratories Cambridge. All Rights Reserved.
36
- #
37
- # This software is distributed in the hope that it will be useful,
38
- # but WITHOUT ANY WARRANTY; without even the implied warranty of
39
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
40
- #
41
- # D3DES (V5.09)
42
- #
43
- # A portable, public domain, version of the Data Encryption Standard.
44
- #
45
- # Written with Symantec's THINK (Lightspeed) C by Richard Outerbridge.
46
- # Thanks to: Dan Hoey for his excellent Initial and Inverse permutation
47
- # code; Jim Gillogly & Phil Karn for the DES key schedule code; Dennis
48
- # Ferguson, Eric Young and Dana How for comparing notes; and Ray Lau,
49
- # for humouring me on.
50
- #
51
- # Copyright (c) 1988,1989,1990,1991,1992 by Richard Outerbridge.
52
- # (GEnie : OUTER; CIS : [71755,204]) Graven Imagery, 1992.
53
- #
54
- class DES
55
- BLOCK_SIZE = 8
56
-
57
- attr_reader :key, :mode
58
-
59
- # Create a des cipher object. +key+ should be cipher key to use, and +mode+ should
60
- # be either <tt>:encrypt</tt> or <tt>:decrypt</tt>.
61
- #
62
- # It will expand +key+ to be 8 bytes by padding with null bytes. If it is longer than
63
- # 8 bytes, the additional data is discarded.
64
- def initialize key, mode
65
- unless [:encrypt, :decrypt].include? mode
66
- raise ArgumentError, 'invalid mode argument - %s' % mode
67
- end
68
- @mode = mode
69
-
70
- # ensure key is 8 bytes. pad with nulls as needed
71
- key = key[0, BLOCK_SIZE]
72
- key << 0.chr * (BLOCK_SIZE - key.length)
73
- @key = key
74
-
75
- # now expand the key schedule
76
- @keys = self.class.send :prepare_key_stage2, self.class.send(:prepare_key_stage1, key, mode)
77
-
78
- # this internal buffer is used because we must process data in chunks of 8 bytes
79
- @buf = ''
80
- end
81
-
82
- # This updates the cipher with +data+, returning any available ciphered output. The +data+ is
83
- # processed in blocks of 8 bytes, so any residual is added to an internal buffer.
84
- def update data
85
- result = ''
86
- data = @buf + data unless @buf.empty?
87
- num_blocks, residual = data.length.divmod BLOCK_SIZE
88
- num_blocks.times do |i|
89
- block = data[i * BLOCK_SIZE, BLOCK_SIZE].unpack('N2')
90
- result << self.class.send(:desfunc, block, @keys).pack('N2')
91
- end
92
- @buf = residual == 0 ? '' : data[-residual..-1]
93
- result
94
- end
95
-
96
- # This flushes the internal buffer by padding it out with null bytes, and doing a final
97
- # DES round. Note that this means the ciphered text is always padded out to a multiple of
98
- # 8 bytes.
99
- def final
100
- if @buf.empty?
101
- ''
102
- else
103
- update 0.chr * (BLOCK_SIZE - @buf.length)
104
- end
105
- end
106
-
107
- # A shortcut method to create a cipher object using +key+, and fully encrypt +data+
108
- def self.encrypt key, data
109
- des = new key, :encrypt
110
- des.update(data) << des.final
111
- end
112
-
113
- # A shortcut method to create a cipher object using +key+, and fully decrypt +data+
114
- def self.decrypt key, data
115
- des = new key, :decrypt
116
- des.update(data) << des.final
117
- end
118
-
119
- class << self #:nodoc: all
120
- BYTEBIT = [
121
- 01, 02, 04, 010, 020, 040, 0100, 0200
122
- ]
123
-
124
- BIGBYTE = [
125
- 0x800000, 0x400000, 0x200000, 0x100000,
126
- 0x080000, 0x040000, 0x020000, 0x010000,
127
- 0x008000, 0x004000, 0x002000, 0x001000,
128
- 0x000800, 0x000400, 0x000200, 0x000100,
129
- 0x000080, 0x000040, 0x000020, 0x000010,
130
- 0x000008, 0x000004, 0x000002, 0x000001
131
- ]
132
-
133
- # Use the key schedule specified in the Standard (ANSI X3.92-1981).
134
-
135
- PC1 = [
136
- 56, 48, 40, 32, 24, 16, 8, 0, 57, 49, 41, 33, 25, 17,
137
- 9, 1, 58, 50, 42, 34, 26, 18, 10, 2, 59, 51, 43, 35,
138
- 62, 54, 46, 38, 30, 22, 14, 6, 61, 53, 45, 37, 29, 21,
139
- 13, 5, 60, 52, 44, 36, 28, 20, 12, 4, 27, 19, 11, 3
140
- ]
141
-
142
- TOTROT = [
143
- 1, 2, 4, 6, 8, 10, 12, 14, 15, 17, 19, 21, 23, 25, 27, 28
144
- ]
145
-
146
- PC2 = [
147
- 13, 16, 10, 23, 0, 4, 2, 27, 14, 5, 20, 9,
148
- 22, 18, 11, 3, 25, 7, 15, 6, 26, 19, 12, 1,
149
- 40, 51, 30, 36, 46, 54, 29, 39, 50, 44, 32, 47,
150
- 43, 48, 38, 55, 33, 52, 45, 41, 49, 35, 28, 31
151
- ]
152
-
153
- private
154
-
155
- #
156
- # Prepares +key+ to be used
157
- #
158
- # +key+:: String
159
- # +mode+:: :encrypt or :decrypt
160
- #
161
- # Thanks to James Gillogly & Phil Karn!
162
- #
163
- # corresponds to "deskey"
164
- #
165
- def prepare_key_stage1 key, mode
166
- pcr = [nil] * 56
167
- kn = [nil] * 32
168
-
169
- pc1m = (0...56).map do |j|
170
- l = PC1[j]
171
- m = l & 07
172
- (key[l >> 3].ord & BYTEBIT[m]) != 0 ? 1 : 0;
173
- end
174
-
175
- 16.times do |i|
176
- m = mode == :encrypt ? i << 1 : (15 - i) << 1
177
- n = m + 1
178
- kn[m] = kn[n] = 0
179
- 28.times do |j|
180
- l = (j + TOTROT[i]) % 28
181
- pcr[j] = pc1m[l]
182
- pcr[j + 28] = pc1m[l + 28]
183
- end
184
- 24.times do |j|
185
- kn[m] |= BIGBYTE[j] if pcr[PC2[j]] != 0
186
- kn[n] |= BIGBYTE[j] if pcr[PC2[j+24]] != 0
187
- end
188
- end
189
-
190
- kn
191
- end
192
-
193
- # corresponds to "cookey"
194
- def prepare_key_stage2(raw1)
195
- cook = []
196
-
197
- 16.times do |i|
198
- a = raw1[i * 2 + 0]
199
- b = raw1[i * 2 + 1]
200
- x = (a & 0x00fc0000) << 6
201
- x |= (a & 0x00000fc0) << 10
202
- x |= (b & 0x00fc0000) >> 10
203
- x |= (b & 0x00000fc0) >> 6
204
- cook << x
205
- x = (a & 0x0003f000) << 12
206
- x |= (a & 0x0000003f) << 16
207
- x |= (b & 0x0003f000) >> 4
208
- x |= (b & 0x0000003f)
209
- cook << x
210
- end
211
-
212
- cook
213
- end
214
-
215
- SP1 = [
216
- 0x01010400, 0x00000000, 0x00010000, 0x01010404,
217
- 0x01010004, 0x00010404, 0x00000004, 0x00010000,
218
- 0x00000400, 0x01010400, 0x01010404, 0x00000400,
219
- 0x01000404, 0x01010004, 0x01000000, 0x00000004,
220
- 0x00000404, 0x01000400, 0x01000400, 0x00010400,
221
- 0x00010400, 0x01010000, 0x01010000, 0x01000404,
222
- 0x00010004, 0x01000004, 0x01000004, 0x00010004,
223
- 0x00000000, 0x00000404, 0x00010404, 0x01000000,
224
- 0x00010000, 0x01010404, 0x00000004, 0x01010000,
225
- 0x01010400, 0x01000000, 0x01000000, 0x00000400,
226
- 0x01010004, 0x00010000, 0x00010400, 0x01000004,
227
- 0x00000400, 0x00000004, 0x01000404, 0x00010404,
228
- 0x01010404, 0x00010004, 0x01010000, 0x01000404,
229
- 0x01000004, 0x00000404, 0x00010404, 0x01010400,
230
- 0x00000404, 0x01000400, 0x01000400, 0x00000000,
231
- 0x00010004, 0x00010400, 0x00000000, 0x01010004
232
- ]
233
-
234
- SP2 = [
235
- 0x80108020, 0x80008000, 0x00008000, 0x00108020,
236
- 0x00100000, 0x00000020, 0x80100020, 0x80008020,
237
- 0x80000020, 0x80108020, 0x80108000, 0x80000000,
238
- 0x80008000, 0x00100000, 0x00000020, 0x80100020,
239
- 0x00108000, 0x00100020, 0x80008020, 0x00000000,
240
- 0x80000000, 0x00008000, 0x00108020, 0x80100000,
241
- 0x00100020, 0x80000020, 0x00000000, 0x00108000,
242
- 0x00008020, 0x80108000, 0x80100000, 0x00008020,
243
- 0x00000000, 0x00108020, 0x80100020, 0x00100000,
244
- 0x80008020, 0x80100000, 0x80108000, 0x00008000,
245
- 0x80100000, 0x80008000, 0x00000020, 0x80108020,
246
- 0x00108020, 0x00000020, 0x00008000, 0x80000000,
247
- 0x00008020, 0x80108000, 0x00100000, 0x80000020,
248
- 0x00100020, 0x80008020, 0x80000020, 0x00100020,
249
- 0x00108000, 0x00000000, 0x80008000, 0x00008020,
250
- 0x80000000, 0x80100020, 0x80108020, 0x00108000
251
- ]
252
-
253
- SP3 = [
254
- 0x00000208, 0x08020200, 0x00000000, 0x08020008,
255
- 0x08000200, 0x00000000, 0x00020208, 0x08000200,
256
- 0x00020008, 0x08000008, 0x08000008, 0x00020000,
257
- 0x08020208, 0x00020008, 0x08020000, 0x00000208,
258
- 0x08000000, 0x00000008, 0x08020200, 0x00000200,
259
- 0x00020200, 0x08020000, 0x08020008, 0x00020208,
260
- 0x08000208, 0x00020200, 0x00020000, 0x08000208,
261
- 0x00000008, 0x08020208, 0x00000200, 0x08000000,
262
- 0x08020200, 0x08000000, 0x00020008, 0x00000208,
263
- 0x00020000, 0x08020200, 0x08000200, 0x00000000,
264
- 0x00000200, 0x00020008, 0x08020208, 0x08000200,
265
- 0x08000008, 0x00000200, 0x00000000, 0x08020008,
266
- 0x08000208, 0x00020000, 0x08000000, 0x08020208,
267
- 0x00000008, 0x00020208, 0x00020200, 0x08000008,
268
- 0x08020000, 0x08000208, 0x00000208, 0x08020000,
269
- 0x00020208, 0x00000008, 0x08020008, 0x00020200
270
- ]
271
-
272
- SP4 = [
273
- 0x00802001, 0x00002081, 0x00002081, 0x00000080,
274
- 0x00802080, 0x00800081, 0x00800001, 0x00002001,
275
- 0x00000000, 0x00802000, 0x00802000, 0x00802081,
276
- 0x00000081, 0x00000000, 0x00800080, 0x00800001,
277
- 0x00000001, 0x00002000, 0x00800000, 0x00802001,
278
- 0x00000080, 0x00800000, 0x00002001, 0x00002080,
279
- 0x00800081, 0x00000001, 0x00002080, 0x00800080,
280
- 0x00002000, 0x00802080, 0x00802081, 0x00000081,
281
- 0x00800080, 0x00800001, 0x00802000, 0x00802081,
282
- 0x00000081, 0x00000000, 0x00000000, 0x00802000,
283
- 0x00002080, 0x00800080, 0x00800081, 0x00000001,
284
- 0x00802001, 0x00002081, 0x00002081, 0x00000080,
285
- 0x00802081, 0x00000081, 0x00000001, 0x00002000,
286
- 0x00800001, 0x00002001, 0x00802080, 0x00800081,
287
- 0x00002001, 0x00002080, 0x00800000, 0x00802001,
288
- 0x00000080, 0x00800000, 0x00002000, 0x00802080
289
- ]
290
-
291
- SP5 = [
292
- 0x00000100, 0x02080100, 0x02080000, 0x42000100,
293
- 0x00080000, 0x00000100, 0x40000000, 0x02080000,
294
- 0x40080100, 0x00080000, 0x02000100, 0x40080100,
295
- 0x42000100, 0x42080000, 0x00080100, 0x40000000,
296
- 0x02000000, 0x40080000, 0x40080000, 0x00000000,
297
- 0x40000100, 0x42080100, 0x42080100, 0x02000100,
298
- 0x42080000, 0x40000100, 0x00000000, 0x42000000,
299
- 0x02080100, 0x02000000, 0x42000000, 0x00080100,
300
- 0x00080000, 0x42000100, 0x00000100, 0x02000000,
301
- 0x40000000, 0x02080000, 0x42000100, 0x40080100,
302
- 0x02000100, 0x40000000, 0x42080000, 0x02080100,
303
- 0x40080100, 0x00000100, 0x02000000, 0x42080000,
304
- 0x42080100, 0x00080100, 0x42000000, 0x42080100,
305
- 0x02080000, 0x00000000, 0x40080000, 0x42000000,
306
- 0x00080100, 0x02000100, 0x40000100, 0x00080000,
307
- 0x00000000, 0x40080000, 0x02080100, 0x40000100
308
- ]
309
-
310
- SP6 = [
311
- 0x20000010, 0x20400000, 0x00004000, 0x20404010,
312
- 0x20400000, 0x00000010, 0x20404010, 0x00400000,
313
- 0x20004000, 0x00404010, 0x00400000, 0x20000010,
314
- 0x00400010, 0x20004000, 0x20000000, 0x00004010,
315
- 0x00000000, 0x00400010, 0x20004010, 0x00004000,
316
- 0x00404000, 0x20004010, 0x00000010, 0x20400010,
317
- 0x20400010, 0x00000000, 0x00404010, 0x20404000,
318
- 0x00004010, 0x00404000, 0x20404000, 0x20000000,
319
- 0x20004000, 0x00000010, 0x20400010, 0x00404000,
320
- 0x20404010, 0x00400000, 0x00004010, 0x20000010,
321
- 0x00400000, 0x20004000, 0x20000000, 0x00004010,
322
- 0x20000010, 0x20404010, 0x00404000, 0x20400000,
323
- 0x00404010, 0x20404000, 0x00000000, 0x20400010,
324
- 0x00000010, 0x00004000, 0x20400000, 0x00404010,
325
- 0x00004000, 0x00400010, 0x20004010, 0x00000000,
326
- 0x20404000, 0x20000000, 0x00400010, 0x20004010
327
- ]
328
-
329
- SP7 = [
330
- 0x00200000, 0x04200002, 0x04000802, 0x00000000,
331
- 0x00000800, 0x04000802, 0x00200802, 0x04200800,
332
- 0x04200802, 0x00200000, 0x00000000, 0x04000002,
333
- 0x00000002, 0x04000000, 0x04200002, 0x00000802,
334
- 0x04000800, 0x00200802, 0x00200002, 0x04000800,
335
- 0x04000002, 0x04200000, 0x04200800, 0x00200002,
336
- 0x04200000, 0x00000800, 0x00000802, 0x04200802,
337
- 0x00200800, 0x00000002, 0x04000000, 0x00200800,
338
- 0x04000000, 0x00200800, 0x00200000, 0x04000802,
339
- 0x04000802, 0x04200002, 0x04200002, 0x00000002,
340
- 0x00200002, 0x04000000, 0x04000800, 0x00200000,
341
- 0x04200800, 0x00000802, 0x00200802, 0x04200800,
342
- 0x00000802, 0x04000002, 0x04200802, 0x04200000,
343
- 0x00200800, 0x00000000, 0x00000002, 0x04200802,
344
- 0x00000000, 0x00200802, 0x04200000, 0x00000800,
345
- 0x04000002, 0x04000800, 0x00000800, 0x00200002
346
- ]
347
-
348
- SP8 = [
349
- 0x10001040, 0x00001000, 0x00040000, 0x10041040,
350
- 0x10000000, 0x10001040, 0x00000040, 0x10000000,
351
- 0x00040040, 0x10040000, 0x10041040, 0x00041000,
352
- 0x10041000, 0x00041040, 0x00001000, 0x00000040,
353
- 0x10040000, 0x10000040, 0x10001000, 0x00001040,
354
- 0x00041000, 0x00040040, 0x10040040, 0x10041000,
355
- 0x00001040, 0x00000000, 0x00000000, 0x10040040,
356
- 0x10000040, 0x10001000, 0x00041040, 0x00040000,
357
- 0x00041040, 0x00040000, 0x10041000, 0x00001000,
358
- 0x00000040, 0x10040040, 0x00001000, 0x00041040,
359
- 0x10001000, 0x00000040, 0x10000040, 0x10040000,
360
- 0x10040040, 0x10000000, 0x00040000, 0x10001040,
361
- 0x00000000, 0x10041040, 0x00040040, 0x10000040,
362
- 0x10040000, 0x10001000, 0x10001040, 0x00000000,
363
- 0x10041040, 0x00041000, 0x00041000, 0x00001040,
364
- 0x00001040, 0x00040040, 0x10000000, 0x10041000
365
- ]
366
-
367
- def desfunc block, keys
368
- leftt = block[0]
369
- right = block[1]
370
-
371
- work = ((leftt >> 4) ^ right) & 0x0f0f0f0f
372
- right ^= work
373
- leftt ^= (work << 4)
374
- work = ((leftt >> 16) ^ right) & 0x0000ffff
375
- right ^= work
376
- leftt ^= (work << 16)
377
- work = ((right >> 2) ^ leftt) & 0x33333333
378
- leftt ^= work
379
- right ^= (work << 2)
380
- work = ((right >> 8) ^ leftt) & 0x00ff00ff
381
- leftt ^= work
382
- right ^= (work << 8)
383
- right = ((right << 1) | ((right >> 31) & 1)) & 0xffffffff
384
- work = (leftt ^ right) & 0xaaaaaaaa
385
- leftt ^= work
386
- right ^= work
387
- leftt = ((leftt << 1) | ((leftt >> 31) & 1)) & 0xffffffff
388
-
389
- 8.times do |i|
390
- work = (right << 28) | (right >> 4)
391
- work ^= keys[i * 4 + 0]
392
- fval = SP7[ work & 0x3f]
393
- fval |= SP5[(work >> 8) & 0x3f]
394
- fval |= SP3[(work >> 16) & 0x3f]
395
- fval |= SP1[(work >> 24) & 0x3f]
396
- work = right ^ keys[i * 4 + 1]
397
- fval |= SP8[ work & 0x3f]
398
- fval |= SP6[(work >> 8) & 0x3f]
399
- fval |= SP4[(work >> 16) & 0x3f]
400
- fval |= SP2[(work >> 24) & 0x3f]
401
- leftt ^= fval
402
- work = (leftt << 28) | (leftt >> 4)
403
- work ^= keys[i * 4 + 2]
404
- fval = SP7[ work & 0x3f]
405
- fval |= SP5[(work >> 8) & 0x3f]
406
- fval |= SP3[(work >> 16) & 0x3f]
407
- fval |= SP1[(work >> 24) & 0x3f]
408
- work = leftt ^ keys[i * 4 + 3]
409
- fval |= SP8[ work & 0x3f]
410
- fval |= SP6[(work >> 8) & 0x3f]
411
- fval |= SP4[(work >> 16) & 0x3f]
412
- fval |= SP2[(work >> 24) & 0x3f]
413
- right ^= fval
414
- end
415
-
416
- right = ((right << 31) | (right >> 1)) & 0xffffffff
417
- work = (leftt ^ right) & 0xaaaaaaaa
418
- leftt ^= work
419
- right ^= work
420
- leftt = ((leftt << 31) | (leftt >> 1)) & 0xffffffff
421
- work = ((leftt >> 8) ^ right) & 0x00ff00ff
422
- right ^= work
423
- leftt ^= (work << 8)
424
- work = ((leftt >> 2) ^ right) & 0x33333333
425
- right ^= work
426
- leftt ^= (work << 2)
427
- work = ((right >> 16) ^ leftt) & 0x0000ffff
428
- leftt ^= work
429
- right ^= (work << 16)
430
- work = ((right >> 4) ^ leftt) & 0x0f0f0f0f
431
- leftt ^= work
432
- right ^= (work << 4)
433
-
434
- [right, leftt]
435
- end
436
- end
437
- end
438
- end
439
-
@@ -1,142 +0,0 @@
1
- require File.dirname(__FILE__) + '/spec_helper'
2
- require 'cipher/des'
3
-
4
- describe 'Cipher::DES' do
5
- DES = Cipher::DES
6
-
7
- DATA = [
8
- ['0xcafecafecafecafe', :encrypt, [
9
- 11571950, 6765055, 15777490, 16705475, 7667282, 16705355, 6747478, 16709450, 7259479,
10
- 16578410, 7323963, 16580202, 11502011, 16580218, 12520091, 12451450, 3759051, 13328317,
11
- 3242445, 14375869, 1142229, 13851581, 5598645, 13843389, 14132645, 15940541, 14386855,
12
- 7551935, 16356014, 7813567, 14794410, 12511175
13
- ], [
14
- 738924839, 154022719, 1010515743, 185740803, 490290973, 252849675, 420822813, 789982986,
15
- 454377245, 739708714, 453263161, 1010503466, 721829689, 943394618, 791293753, 807083834,
16
- 237974078, 353056061, 203896366, 386741565, 70726702, 370488637, 355873838, 372585277,
17
- 890649646, 975516477, 907680814, 942093119, 1040850214, 422458175, 943337247, 455749127
18
- ], [276147755, 314564801]],
19
- ['0xcafecafecafecafe', :decrypt, [
20
- 14794410, 12511175, 16356014, 7813567, 14386855, 7551935, 14132645, 15940541, 5598645,
21
- 13843389, 1142229, 13851581, 3242445, 14375869, 3759051, 13328317, 12520091, 12451450,
22
- 11502011, 16580218, 7323963, 16580202, 7259479, 16578410, 6747478, 16709450, 7667282,
23
- 16705355, 15777490, 16705475, 11571950, 6765055
24
- ], [
25
- 943337247, 455749127, 1040850214, 422458175, 907680814, 942093119, 890649646, 975516477,
26
- 355873838, 372585277, 70726702, 370488637, 203896366, 386741565, 237974078, 353056061,
27
- 791293753, 807083834, 721829689, 943394618, 453263161, 1010503466, 454377245, 739708714,
28
- 420822813, 789982986, 490290973, 252849675, 1010515743, 185740803, 738924839, 154022719
29
- ], [3868695016, 412139341]],
30
- ['0xdeadbeefdeadbeef', :encrypt, [
31
- 7466991, 16631125, 16695036, 16227052, 14614126, 5292027, 15400830, 4176957, 15531887,
32
- 7044594, 14942075, 3008831, 15597555, 15162582, 16252891, 13468671, 11426815, 16115512,
33
- 12548989, 7945806, 9404409, 16576702, 14647293, 2457327, 14680009, 12499187, 6029295,
34
- 10997623, 16383439, 2076626, 14630590, 13906815
35
- ], [
36
- 473906965, 506403861, 1060846891, 725367084, 926487615, 791546683, 977080112, 792607549,
37
- 993860151, 254752562, 943524644, 1060838975, 993999155, 523449622, 1027552015, 1058740287,
38
- 724516124, 624893496, 791486009, 926749454, 591347458, 926486334, 926878011, 926750511,
39
- 926887715, 1057565491, 373238077, 1060060215, 1043793727, 521091602, 926561549, 859702079
40
- ], [1755543026, 929731926]],
41
- ['0xdeadbeefdeadbeef', :decrypt, [
42
- 14630590, 13906815, 16383439, 2076626, 6029295, 10997623, 14680009, 12499187, 14647293,
43
- 2457327, 9404409, 16576702, 12548989, 7945806, 11426815, 16115512, 16252891, 13468671,
44
- 15597555, 15162582, 14942075, 3008831, 15531887, 7044594, 15400830, 4176957, 14614126,
45
- 5292027, 16695036, 16227052, 7466991, 16631125
46
- ], [
47
- 926561549, 859702079, 1043793727, 521091602, 373238077, 1060060215, 926887715, 1057565491,
48
- 926878011, 926750511, 591347458, 926486334, 791486009, 926749454, 724516124, 624893496,
49
- 1027552015, 1058740287, 993999155, 523449622, 943524644, 1060838975, 993860151, 254752562,
50
- 977080112, 792607549, 926487615, 791546683, 1060846891, 725367084, 473906965, 506403861
51
- ], [2531611598, 527835150]]
52
- ]
53
-
54
- describe '(private class methods)' do
55
- it 'can prepare keys for encryption and decryption' do
56
- DATA.each do |hex_key, mode, stage1, stage2, expect|
57
- key = [hex_key[2..-1]].pack('H*')
58
- DES.send(:prepare_key_stage1, key, mode).should == stage1
59
- DES.send(:prepare_key_stage2, stage1).should == stage2
60
- end
61
- end
62
-
63
- it 'can prepare perform a DES round on a block of data' do
64
- DATA.each do |hex_key, mode, stage1, stage2, expect|
65
- DES.send(:desfunc, [0, 0], stage2).should == expect
66
- end
67
- end
68
- end
69
-
70
- describe '#initialize' do
71
- it 'can create a DES object from a key and a mode' do
72
- hex_key, mode, stage1, stage2, expect = DATA[0]
73
- key = [hex_key[2..-1]].pack('H*')
74
- des = DES.new key, mode
75
- des.key.should == key
76
- des.mode.should == mode
77
- des.instance_variable_get(:@buf).should == ''
78
- des.instance_variable_get(:@keys).should == stage2
79
- end
80
-
81
- it 'will reject invalid modes' do
82
- lambda { DES.new 'key', :encryptify }.should raise_error(ArgumentError)
83
- end
84
-
85
- it 'expands or truncates the key to 8 bytes' do
86
- DES.new('my-really-long-key', :encrypt).key.should == 'my-reall'
87
- DES.new('key', :encrypt).key.should == "key\000\000\000\000\000"
88
- end
89
- end
90
-
91
- describe '#update' do
92
- before :each do
93
- hex_key, mode, stage1, stage2, @expect = DATA[0]
94
- key = [hex_key[2..-1]].pack('H*')
95
- @des = DES.new key, mode
96
- end
97
-
98
- it 'will return the data in ciphered form' do
99
- @des.update([0, 0].pack('N2')).should == @expect.pack('N2')
100
- end
101
-
102
- it 'will store the residual in buffer' do
103
- @des.update([0].pack('N')).should == ''
104
- @des.instance_variable_get(:@buf).should == [0].pack('N')
105
- @des.update([0].pack('N')).should == @expect.pack('N2')
106
- @des.instance_variable_get(:@buf).should == ''
107
- end
108
- end
109
-
110
- describe '#final' do
111
- before :each do
112
- hex_key, mode, stage1, stage2, @expect = DATA[0]
113
- key = [hex_key[2..-1]].pack('H*')
114
- @des = DES.new key, mode
115
- end
116
-
117
- it 'will flush the buffer by padding with null bytes' do
118
- @des.final.should == ''
119
- @des.update([0].pack('N')).should == ''
120
- @des.final.should == @expect.pack('N2')
121
- end
122
- end
123
-
124
- describe '.encrypt' do
125
- it 'is a shortcut class method for DES encryption' do
126
- hex_key, mode, stage1, stage2, expect = DATA[0]
127
- key = [hex_key[2..-1]].pack('H*')
128
- mode.should == :encrypt
129
- DES.encrypt(key, [0].pack('N')).should == expect.pack('N2')
130
- end
131
- end
132
-
133
- describe '.decrypt' do
134
- it 'is a shortcut class method for DES decryption' do
135
- hex_key, mode, stage1, stage2, expect = DATA[1]
136
- key = [hex_key[2..-1]].pack('H*')
137
- mode.should == :decrypt
138
- DES.decrypt(key, [0].pack('N')).should == expect.pack('N2')
139
- end
140
- end
141
- end
142
-