|
| 1 | +package org.bouncycastle.crypto.test; |
| 2 | + |
| 3 | +import java.security.SecureRandom; |
| 4 | + |
| 5 | +import org.bouncycastle.crypto.AsymmetricCipherKeyPair; |
| 6 | +import org.bouncycastle.crypto.Signer; |
| 7 | +import org.bouncycastle.crypto.generators.Ed25519KeyPairGenerator; |
| 8 | +import org.bouncycastle.crypto.params.Ed25519KeyGenerationParameters; |
| 9 | +import org.bouncycastle.crypto.params.Ed25519PrivateKeyParameters; |
| 10 | +import org.bouncycastle.crypto.params.Ed25519PublicKeyParameters; |
| 11 | +import org.bouncycastle.crypto.signers.Ed25519Signer; |
| 12 | +import org.bouncycastle.crypto.signers.Ed25519ctxSigner; |
| 13 | +import org.bouncycastle.crypto.signers.Ed25519phSigner; |
| 14 | +import org.bouncycastle.math.ec.rfc8032.Ed25519; |
| 15 | +import org.bouncycastle.util.Arrays; |
| 16 | +import org.bouncycastle.util.encoders.Base64; |
| 17 | +import org.bouncycastle.util.encoders.Hex; |
| 18 | +import org.bouncycastle.util.test.SimpleTest; |
| 19 | + |
| 20 | +public class Ed25519Test |
| 21 | + extends SimpleTest |
| 22 | +{ |
| 23 | + private static final SecureRandom RANDOM = new SecureRandom(); |
| 24 | + |
| 25 | + public String getName() |
| 26 | + { |
| 27 | + return "Ed25519"; |
| 28 | + } |
| 29 | + |
| 30 | + public static void main(String[] args) |
| 31 | + { |
| 32 | + runTest(new Ed25519Test()); |
| 33 | + } |
| 34 | + |
| 35 | + public void performTest() |
| 36 | + throws Exception |
| 37 | + { |
| 38 | + basicSigTest(); |
| 39 | + |
| 40 | + for (int i = 0; i < 10; ++i) |
| 41 | + { |
| 42 | + testConsistency(Ed25519.Algorithm.Ed25519, null); |
| 43 | + |
| 44 | + byte[] context = randomContext(RANDOM.nextInt() & 255); |
| 45 | + testConsistency(Ed25519.Algorithm.Ed25519ctx, context); |
| 46 | + testConsistency(Ed25519.Algorithm.Ed25519ph, context); |
| 47 | + } |
| 48 | + |
| 49 | + } |
| 50 | + |
| 51 | + private void basicSigTest() |
| 52 | + throws Exception |
| 53 | + { |
| 54 | + Ed25519PrivateKeyParameters privateKey = new Ed25519PrivateKeyParameters( |
| 55 | + Hex.decode("9d61b19deffd5a60ba844af492ec2cc44449c5697b326919703bac031cae7f60")); |
| 56 | + Ed25519PublicKeyParameters publicKey = new Ed25519PublicKeyParameters( |
| 57 | + Hex.decode("d75a980182b10ab7d54bfed3c964073a0ee172f3daa62325af021a68f707511a")); |
| 58 | + |
| 59 | + byte[] sig = Hex.decode("e5564300c360ac729086e2cc806e828a84877f1eb8e5d974d873e065224901555fb8821590a33bacc61e39701cf9b46bd25bf5f0595bbe24655141438e7a100b"); |
| 60 | + |
| 61 | + Signer signer = new Ed25519Signer(); |
| 62 | + |
| 63 | + signer.init(true, privateKey); |
| 64 | + |
| 65 | + isTrue(areEqual(sig, signer.generateSignature())); |
| 66 | + |
| 67 | + signer.init(false, publicKey); |
| 68 | + |
| 69 | + isTrue(signer.verifySignature(sig)); |
| 70 | + } |
| 71 | + |
| 72 | + private Signer createSigner(int algorithm, byte[] context) |
| 73 | + { |
| 74 | + switch (algorithm) |
| 75 | + { |
| 76 | + case Ed25519.Algorithm.Ed25519: |
| 77 | + return new Ed25519Signer(); |
| 78 | + case Ed25519.Algorithm.Ed25519ctx: |
| 79 | + return new Ed25519ctxSigner(context); |
| 80 | + case Ed25519.Algorithm.Ed25519ph: |
| 81 | + return new Ed25519phSigner(context); |
| 82 | + default: |
| 83 | + throw new IllegalArgumentException("algorithm"); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + private byte[] randomContext(int length) |
| 88 | + { |
| 89 | + byte[] context = new byte[length]; |
| 90 | + RANDOM.nextBytes(context); |
| 91 | + return context; |
| 92 | + } |
| 93 | + |
| 94 | + private void testConsistency(int algorithm, byte[] context) |
| 95 | + throws Exception |
| 96 | + { |
| 97 | + Ed25519KeyPairGenerator kpg = new Ed25519KeyPairGenerator(); |
| 98 | + kpg.init(new Ed25519KeyGenerationParameters(RANDOM)); |
| 99 | + |
| 100 | + AsymmetricCipherKeyPair kp = kpg.generateKeyPair(); |
| 101 | + Ed25519PrivateKeyParameters privateKey = (Ed25519PrivateKeyParameters)kp.getPrivate(); |
| 102 | + Ed25519PublicKeyParameters publicKey = (Ed25519PublicKeyParameters)kp.getPublic(); |
| 103 | + |
| 104 | + byte[] msg = new byte[RANDOM.nextInt() & 255]; |
| 105 | + RANDOM.nextBytes(msg); |
| 106 | + |
| 107 | + Signer signer = createSigner(algorithm, context); |
| 108 | + signer.init(true, privateKey); |
| 109 | + signer.update(msg, 0, msg.length); |
| 110 | + byte[] signature = signer.generateSignature(); |
| 111 | + |
| 112 | + Signer verifier = createSigner(algorithm, context); |
| 113 | + |
| 114 | + { |
| 115 | + verifier.init(false, publicKey); |
| 116 | + verifier.update(msg, 0, msg.length); |
| 117 | + boolean shouldVerify = verifier.verifySignature(signature); |
| 118 | + |
| 119 | + if (!shouldVerify) |
| 120 | + { |
| 121 | + fail("Ed25519(" + algorithm + ") signature failed to verify"); |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + { |
| 126 | + byte[] wrongLengthSignature = Arrays.append(signature, (byte)0x00); |
| 127 | + |
| 128 | + verifier.init(false, publicKey); |
| 129 | + verifier.update(msg, 0, msg.length); |
| 130 | + boolean shouldNotVerify = verifier.verifySignature(wrongLengthSignature); |
| 131 | + |
| 132 | + if (shouldNotVerify) |
| 133 | + { |
| 134 | + fail("Ed25519(" + algorithm + ") wrong length signature incorrectly verified"); |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + if (msg.length > 0) |
| 139 | + { |
| 140 | + boolean shouldNotVerify = verifier.verifySignature(signature); |
| 141 | + |
| 142 | + if (shouldNotVerify) |
| 143 | + { |
| 144 | + fail("Ed25519(" + algorithm + ") wrong length failure did not reset verifier"); |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + { |
| 149 | + byte[] badSignature = Arrays.clone(signature); |
| 150 | + badSignature[(RANDOM.nextInt() >>> 1) % badSignature.length] ^= 1 << (RANDOM.nextInt() & 7); |
| 151 | + |
| 152 | + verifier.init(false, publicKey); |
| 153 | + verifier.update(msg, 0, msg.length); |
| 154 | + boolean shouldNotVerify = verifier.verifySignature(badSignature); |
| 155 | + |
| 156 | + if (shouldNotVerify) |
| 157 | + { |
| 158 | + fail("Ed25519(" + algorithm + ") bad signature incorrectly verified"); |
| 159 | + } |
| 160 | + } |
| 161 | + } |
| 162 | +} |
0 commit comments