|
1 | 1 | package org.bouncycastle.crypto.threshold.test; |
2 | 2 |
|
| 3 | +import java.io.IOException; |
3 | 4 | import java.security.SecureRandom; |
4 | 5 | import java.util.Arrays; |
5 | 6 |
|
|
9 | 10 | import org.bouncycastle.crypto.threshold.ShamirSplitSecret; |
10 | 11 | import org.bouncycastle.crypto.threshold.ShamirSplitSecretShare; |
11 | 12 | import org.bouncycastle.util.test.FixedSecureRandom; |
| 13 | + |
12 | 14 | import org.junit.Assert; |
13 | 15 |
|
14 | 16 | public class ShamirSecretSplitterTest |
15 | 17 | extends TestCase |
16 | 18 | { |
17 | 19 | public static void main(String[] args) |
| 20 | + throws IOException |
18 | 21 | { |
19 | 22 | ShamirSecretSplitterTest test = new ShamirSecretSplitterTest(); |
20 | 23 | test.performTest(); |
21 | 24 | } |
22 | 25 |
|
23 | 26 | public void performTest() |
| 27 | + throws IOException |
24 | 28 | { |
| 29 | + testShamirSecretSplitterSplitAround(); |
25 | 30 | testPolynomial(); |
26 | 31 | testShamirSecretSplitter(); |
27 | 32 | } |
28 | 33 |
|
| 34 | + public void testShamirSecretSplitterSplitAround() |
| 35 | + throws IOException |
| 36 | + { |
| 37 | + int l = 9, m = 3, n = 9; |
| 38 | + ShamirSecretSplitter.Algorithm algorithm = ShamirSecretSplitter.Algorithm.AES; |
| 39 | + ShamirSecretSplitter.Mode mode = ShamirSecretSplitter.Mode.Table; |
| 40 | + ShamirSecretSplitter splitter = new ShamirSecretSplitter(algorithm, mode, l, m, n, new SecureRandom());//, secretshare); |
| 41 | + byte[] seed = new byte[l]; |
| 42 | + SecureRandom random = new SecureRandom(); |
| 43 | + random.nextBytes(seed); |
| 44 | + ShamirSplitSecretShare ss = new ShamirSplitSecretShare(seed); |
| 45 | + ShamirSplitSecret splitSecret = splitter.splitAround(ss); |
| 46 | + ShamirSplitSecretShare[] secretShares = splitSecret.getSecretShares(); |
| 47 | + Assert.assertTrue(Arrays.equals(secretShares[0].getEncoded(), seed)); |
| 48 | + |
| 49 | + ShamirSplitSecretShare[] secretShares1 = new ShamirSplitSecretShare[]{secretShares[0], secretShares[1], secretShares[2]}; |
| 50 | + ShamirSplitSecret splitSecret1 = new ShamirSplitSecret(algorithm, mode, secretShares1); |
| 51 | + byte[] secret1 = splitSecret1.recombine(); |
| 52 | + |
| 53 | + ShamirSplitSecretShare[] secretShares4 = new ShamirSplitSecretShare[]{secretShares[1], secretShares[2], secretShares[5]}; |
| 54 | + ShamirSplitSecret splitSecret4 = new ShamirSplitSecret(algorithm, mode, secretShares4); |
| 55 | + byte[] secret4 = splitSecret4.recombine(); |
| 56 | + |
| 57 | + ShamirSplitSecretShare[] secretShares2 = new ShamirSplitSecretShare[]{secretShares[4], secretShares[7], secretShares[8]}; |
| 58 | + ShamirSplitSecret splitSecret2 = new ShamirSplitSecret(algorithm, mode, secretShares2); |
| 59 | + byte[] secret2 = splitSecret2.recombine(); |
| 60 | + |
| 61 | + Assert.assertTrue(Arrays.equals(secret1, secret2)); |
| 62 | + |
| 63 | + // not enough secret shares cannot correctly recover the secret |
| 64 | + ShamirSplitSecretShare[] secretShares3 = new ShamirSplitSecretShare[]{secretShares[3], secretShares[6]}; |
| 65 | + ShamirSplitSecret splitSecret3 = new ShamirSplitSecret(algorithm, mode, secretShares3); |
| 66 | + byte[] secret3 = splitSecret3.recombine(); |
| 67 | + Assert.assertFalse(Arrays.equals(secret1, secret3)); |
| 68 | + } |
| 69 | + |
29 | 70 | public void testShamirSecretSplitter() |
| 71 | + throws IOException |
30 | 72 | { |
31 | 73 | int l = 9, m = 3, n = 9; |
32 | 74 | ShamirSecretSplitter.Algorithm algorithm = ShamirSecretSplitter.Algorithm.AES; |
33 | 75 | ShamirSecretSplitter.Mode mode = ShamirSecretSplitter.Mode.Table; |
34 | | - ShamirSecretSplitter splitter = new ShamirSecretSplitter(algorithm, mode, l, m, n, new SecureRandom()); |
35 | | - ShamirSplitSecret splitSecret = splitter.split(); |
36 | | - ShamirSplitSecretShare[] secretShares = splitSecret.getSecretShare(); |
| 76 | + ShamirSecretSplitter splitter = new ShamirSecretSplitter(algorithm, mode, l, m, n, new SecureRandom());//, secretshare); |
| 77 | + ShamirSplitSecret splitSecret = splitter.split(); //integers multiply/ divide |
| 78 | + ShamirSplitSecretShare[] secretShares = splitSecret.getSecretShares(); |
37 | 79 |
|
38 | 80 | ShamirSplitSecretShare[] secretShares1 = new ShamirSplitSecretShare[]{secretShares[0], secretShares[1], secretShares[2]}; |
39 | 81 | ShamirSplitSecret splitSecret1 = new ShamirSplitSecret(algorithm, mode, secretShares1); |
@@ -825,6 +867,7 @@ public String getName() |
825 | 867 | } |
826 | 868 |
|
827 | 869 | public void testPolynomial() |
| 870 | + throws IOException |
828 | 871 | { |
829 | 872 | testPolynoimial1(new PolynomialFactory() |
830 | 873 | { |
@@ -888,6 +931,7 @@ public ShamirSplitSecret newInstance(ShamirSplitSecretShare[] secretShares) |
888 | 931 | } |
889 | 932 |
|
890 | 933 | private void testPolynoimial1(PolynomialFactory polynomialFactory) |
| 934 | + throws IOException |
891 | 935 | { |
892 | 936 | ShamirSecretSplitter splitter = polynomialFactory.newInstance(5, 2, 2, getSecureRandom(TV011B_TV1_SR)); |
893 | 937 | testMatrixMultiplication(splitter, TV011B_TV1_SPLITS); |
@@ -916,6 +960,7 @@ private void testPolynoimial1(PolynomialFactory polynomialFactory) |
916 | 960 | } |
917 | 961 |
|
918 | 962 | private void testPolynoimial2(PolynomialFactory polynomialFactory) |
| 963 | + throws IOException |
919 | 964 | { |
920 | 965 | ShamirSecretSplitter poly = polynomialFactory.newInstance(5, 2, 2, getSecureRandom(TV011D_TV1_SR)); |
921 | 966 | testMatrixMultiplication(poly, TV011D_TV1_SPLITS); |
@@ -967,17 +1012,19 @@ static ShamirSplitSecretShare[] getShamirSplitSecretShareArray(int[] rr, byte[][ |
967 | 1012 | } |
968 | 1013 |
|
969 | 1014 | static void testMatrixMultiplication(ShamirSecretSplitter poly, byte[][] splits) |
| 1015 | + throws IOException |
970 | 1016 | { |
971 | | - ShamirSplitSecretShare[] secretShares = poly.split().getSecretShare(); |
| 1017 | + ShamirSplitSecretShare[] secretShares = poly.split().getSecretShares(); |
972 | 1018 | byte[][] result = new byte[splits.length][splits[0].length]; |
973 | 1019 | for (int i = 0; i < result.length; ++i) |
974 | 1020 | { |
975 | | - result[i] = secretShares[i].getSecretShare(); |
| 1021 | + result[i] = secretShares[i].getEncoded(); |
976 | 1022 | } |
977 | 1023 | assertEquals(Arrays.deepToString(splits), Arrays.deepToString(result)); |
978 | 1024 | } |
979 | 1025 |
|
980 | 1026 | public void testRecombine(ShamirSplitSecret splitSecret, byte[] secret) |
| 1027 | + throws IOException |
981 | 1028 | { |
982 | 1029 | byte[] result = splitSecret.recombine(); |
983 | 1030 | assertTrue(Arrays.equals(secret, result)); |
|
0 commit comments