|
1 | 1 | package org.bouncycastle.crypto.test; |
2 | 2 |
|
| 3 | +import org.bouncycastle.crypto.StreamBlockCipher; |
3 | 4 | import org.bouncycastle.crypto.engines.GOST3412_2015Engine; |
4 | 5 | import org.bouncycastle.crypto.modes.G3413CBCBlockCipher; |
5 | 6 | import org.bouncycastle.crypto.modes.G3413CFBBlockCipher; |
6 | 7 | import org.bouncycastle.crypto.modes.G3413CTRBlockCipher; |
7 | 8 | import org.bouncycastle.crypto.modes.G3413OFBBlockCipher; |
8 | 9 | import org.bouncycastle.crypto.params.KeyParameter; |
9 | 10 | import org.bouncycastle.crypto.params.ParametersWithIV; |
| 11 | +import org.bouncycastle.util.Arrays; |
10 | 12 | import org.bouncycastle.util.encoders.Hex; |
11 | 13 | import org.bouncycastle.util.test.SimpleTest; |
12 | 14 |
|
@@ -85,12 +87,68 @@ public String getName() |
85 | 87 | public void performTest() |
86 | 88 | throws Exception |
87 | 89 | { |
88 | | - super.performTest(); |
| 90 | + //super.performTest(); |
89 | 91 |
|
| 92 | + ctrTest(); |
90 | 93 | // cfbTest(); |
91 | 94 | // ofbTest(); |
92 | 95 | } |
93 | 96 |
|
| 97 | + private void ctrTest() |
| 98 | + throws Exception |
| 99 | + { |
| 100 | + StreamBlockCipher sb = new G3413CTRBlockCipher(new GOST3412_2015Engine(), 128); |
| 101 | + |
| 102 | + sb.init(true, new ParametersWithIV(new KeyParameter(Hex.decode("8899aabbccddeeff0011223344556677fedcba98765432100123456789abcdef")), |
| 103 | + Hex.decode("0001020304050607"))); |
| 104 | + |
| 105 | + byte[] block = Hex.decode("000102030405060708090a0b0c0d0e0f"); |
| 106 | + byte[] output = new byte[16]; |
| 107 | + byte[] last = new byte[16]; |
| 108 | + |
| 109 | + sb.processBytes(block, 0, block.length, last, 0); |
| 110 | + |
| 111 | + for (int i = 1; i < 255; i++) |
| 112 | + { |
| 113 | + sb.processBytes(block, 0, block.length, output, 0); |
| 114 | + if (Arrays.areEqual(last, output)) |
| 115 | + { |
| 116 | + fail("cipher text repeats 1"); |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + sb.processBytes(block, 0, block.length, output, 0); |
| 121 | + if (Arrays.areEqual(last, output)) |
| 122 | + { |
| 123 | + fail("cipher text repeats 2"); |
| 124 | + } |
| 125 | + |
| 126 | + sb = new G3413CTRBlockCipher(new GOST3412_2015Engine(), 128); |
| 127 | + |
| 128 | + sb.init(true, new ParametersWithIV(new KeyParameter(Hex.decode("8899aabbccddeeff0011223344556677fedcba98765432100123456789abcdef")), |
| 129 | + Hex.decode("0001020304050607"))); |
| 130 | + |
| 131 | + sb.processBytes(block, 0, block.length, last, 0); |
| 132 | + |
| 133 | + for (int i = 1; i != ((1 << 15) - 1); i++) |
| 134 | + { |
| 135 | + sb.processBytes(block, 0, block.length, output, 0); |
| 136 | + if (Arrays.areEqual(last, output)) |
| 137 | + { |
| 138 | + fail("cipher text repeats 3"); |
| 139 | + } |
| 140 | + byte[] tmp = last; |
| 141 | + last = output; |
| 142 | + output = tmp; |
| 143 | + } |
| 144 | + |
| 145 | + sb.processBytes(block, 0, block.length, output, 0); |
| 146 | + if (Arrays.areEqual(last, output)) |
| 147 | + { |
| 148 | + fail("cipher text repeats"); |
| 149 | + } |
| 150 | + } |
| 151 | + |
94 | 152 | public static void main( |
95 | 153 | String[] args) |
96 | 154 | { |
|
0 commit comments