1 |
3a515b92
|
cagy
|
'use strict';
|
2 |
|
|
|
3 |
|
|
var assert = require('assert');
|
4 |
|
|
|
5 |
|
|
var des = require('../');
|
6 |
|
|
var utils = des.utils;
|
7 |
|
|
|
8 |
|
|
var fixtures = require('./fixtures');
|
9 |
|
|
var bin = fixtures.bin;
|
10 |
|
|
|
11 |
|
|
describe('utils', function() {
|
12 |
|
|
describe('IP', function() {
|
13 |
|
|
it('should permute properly', function() {
|
14 |
|
|
var out = new Array(2);
|
15 |
|
|
var inp = [
|
16 |
|
|
bin('00000001 00100011 01000101 01100111'),
|
17 |
|
|
bin('10001001 10101011 11001101 11101111')
|
18 |
|
|
];
|
19 |
|
|
|
20 |
|
|
utils.ip(inp[0], inp[1], out, 0);
|
21 |
|
|
|
22 |
|
|
var expected = [
|
23 |
|
|
bin('11001100 00000000 11001100 11111111'),
|
24 |
|
|
bin('11110000 10101010 11110000 10101010')
|
25 |
|
|
];
|
26 |
|
|
|
27 |
|
|
assert.deepEqual(out, expected);
|
28 |
|
|
});
|
29 |
|
|
|
30 |
|
|
it('should rev-permute properly', function() {
|
31 |
|
|
var out = new Array(2);
|
32 |
|
|
var inp = [
|
33 |
|
|
bin('11001100 00000000 11001100 11111111'),
|
34 |
|
|
bin('11110000 10101010 11110000 10101010')
|
35 |
|
|
];
|
36 |
|
|
|
37 |
|
|
utils.rip(inp[0], inp[1], out, 0);
|
38 |
|
|
|
39 |
|
|
var expected = [
|
40 |
|
|
bin('00000001 00100011 01000101 01100111'),
|
41 |
|
|
bin('10001001 10101011 11001101 11101111')
|
42 |
|
|
];
|
43 |
|
|
|
44 |
|
|
assert.deepEqual(out, expected);
|
45 |
|
|
});
|
46 |
|
|
});
|
47 |
|
|
|
48 |
|
|
describe('PC1', function() {
|
49 |
|
|
it('should permute properly', function() {
|
50 |
|
|
var out = new Array(2);
|
51 |
|
|
var inp = [
|
52 |
|
|
bin('00010011 00110100 01010111 01111001'),
|
53 |
|
|
bin('10011011 10111100 11011111 11110001')
|
54 |
|
|
];
|
55 |
|
|
|
56 |
|
|
utils.pc1(inp[0], inp[1], out, 0);
|
57 |
|
|
|
58 |
|
|
var expected = [
|
59 |
|
|
bin('1111000 0110011 0010101 0101111'),
|
60 |
|
|
bin('0101010 1011001 1001111 0001111')
|
61 |
|
|
];
|
62 |
|
|
|
63 |
|
|
assert.deepEqual(out, expected);
|
64 |
|
|
});
|
65 |
|
|
});
|
66 |
|
|
|
67 |
|
|
describe('r28shl', function() {
|
68 |
|
|
it('should shl properly', function() {
|
69 |
|
|
assert.equal(utils.r28shl(bin('1111000011001100101010101111'), 1),
|
70 |
|
|
bin('1110000110011001010101011111'));
|
71 |
|
|
|
72 |
|
|
assert.equal(utils.r28shl(bin('0101010101100110011110001111'), 1),
|
73 |
|
|
bin('1010101011001100111100011110'));
|
74 |
|
|
|
75 |
|
|
assert.equal(utils.r28shl(bin('1111000011001100101010101111'), 4),
|
76 |
|
|
bin('0000110011001010101011111111'));
|
77 |
|
|
|
78 |
|
|
assert.equal(utils.r28shl(bin('0101010101100110011110001111'), 4),
|
79 |
|
|
bin('0101011001100111100011110101'));
|
80 |
|
|
});
|
81 |
|
|
});
|
82 |
|
|
|
83 |
|
|
describe('PC2', function() {
|
84 |
|
|
it('should permute properly', function() {
|
85 |
|
|
var out = new Array(2);
|
86 |
|
|
var inp = [
|
87 |
|
|
bin('1110000 1100110 0101010 1011111'),
|
88 |
|
|
bin('1010101 0110011 0011110 0011110')
|
89 |
|
|
];
|
90 |
|
|
|
91 |
|
|
utils.pc2(inp[0], inp[1], out, 0);
|
92 |
|
|
|
93 |
|
|
var expected = [
|
94 |
|
|
bin('000110 110000 001011 101111'),
|
95 |
|
|
bin('111111 000111 000001 110010')
|
96 |
|
|
];
|
97 |
|
|
|
98 |
|
|
assert.deepEqual(out, expected);
|
99 |
|
|
});
|
100 |
|
|
});
|
101 |
|
|
|
102 |
|
|
describe('readUInt32BE', function() {
|
103 |
|
|
it('should read number properly', function() {
|
104 |
|
|
var a = [ 0xde, 0xad, 0xbe, 0xef ];
|
105 |
|
|
var o = utils.readUInt32BE(a, 0);
|
106 |
|
|
assert.equal(o, 0xdeadbeef);
|
107 |
|
|
});
|
108 |
|
|
});
|
109 |
|
|
|
110 |
|
|
describe('writeUInt32BE', function() {
|
111 |
|
|
it('should read number properly', function() {
|
112 |
|
|
var a = [ 0, 0, 0, 0 ];
|
113 |
|
|
utils.writeUInt32BE(a, 0xdeadbeef, 0);
|
114 |
|
|
var expected = [ 0xde, 0xad, 0xbe, 0xef ];
|
115 |
|
|
assert.deepEqual(a, expected);
|
116 |
|
|
});
|
117 |
|
|
});
|
118 |
|
|
|
119 |
|
|
describe('expand', function() {
|
120 |
|
|
it('should expand', function() {
|
121 |
|
|
var out = [ 0, 0 ];
|
122 |
|
|
utils.expand(bin('1111 0000 1010 1010 1111 0000 1010 1010'), out, 0);
|
123 |
|
|
var expected = [
|
124 |
|
|
bin('011110 100001 010101 010101'),
|
125 |
|
|
bin('011110 100001 010101 010101')
|
126 |
|
|
];
|
127 |
|
|
assert.deepEqual(out, expected);
|
128 |
|
|
});
|
129 |
|
|
|
130 |
|
|
it('should expand with low 1', function() {
|
131 |
|
|
var out = [ 0, 0 ];
|
132 |
|
|
utils.expand(bin('1111 0000 1010 1010 1111 0000 1010 1011'), out, 0);
|
133 |
|
|
var expected = [
|
134 |
|
|
bin('111110 100001 010101 010101'),
|
135 |
|
|
bin('011110 100001 010101 010111')
|
136 |
|
|
];
|
137 |
|
|
assert.deepEqual(out, expected);
|
138 |
|
|
});
|
139 |
|
|
|
140 |
|
|
it('should expand with low 1', function() {
|
141 |
|
|
var out = [ 0, 0 ];
|
142 |
|
|
utils.expand(bin('10100010 01011100 00001011 11110100'), out, 0);
|
143 |
|
|
var expected = [
|
144 |
|
|
bin('010100 000100 001011 111000'),
|
145 |
|
|
bin('000001 010111 111110 101001')
|
146 |
|
|
];
|
147 |
|
|
assert.deepEqual(out, expected);
|
148 |
|
|
});
|
149 |
|
|
});
|
150 |
|
|
|
151 |
|
|
describe('substitute', function() {
|
152 |
|
|
it('should substitute', function() {
|
153 |
|
|
var input = [
|
154 |
|
|
bin('011000 010001 011110 111010'),
|
155 |
|
|
bin('100001 100110 010100 100111')
|
156 |
|
|
];
|
157 |
|
|
var output = utils.substitute(input[0], input[1]);
|
158 |
|
|
assert.equal(output, bin('0101 1100 1000 0010 1011 0101 1001 0111'));
|
159 |
|
|
});
|
160 |
|
|
});
|
161 |
|
|
|
162 |
|
|
describe('permute', function() {
|
163 |
|
|
it('should permute', function() {
|
164 |
|
|
var output = utils.permute(
|
165 |
|
|
bin('0101 1100 1000 0010 1011 0101 1001 0111'));
|
166 |
|
|
assert.equal(output, bin('0010 0011 0100 1010 1010 1001 1011 1011'));
|
167 |
|
|
});
|
168 |
|
|
});
|
169 |
|
|
});
|