• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2017 Google Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //      http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 //
15 ////////////////////////////////////////////////////////////////////////////////
16 
17 package com.google.crypto.tink.internal;
18 
19 import static org.junit.Assert.assertArrayEquals;
20 import static org.junit.Assert.assertEquals;
21 import static org.junit.Assert.assertTrue;
22 
23 import com.google.crypto.tink.subtle.Hex;
24 import com.google.crypto.tink.subtle.Random;
25 import java.util.Arrays;
26 import org.junit.Test;
27 import org.junit.runner.RunWith;
28 import org.junit.runners.JUnit4;
29 
30 /** Unit tests for Ed25519. */
31 @RunWith(JUnit4.class)
32 public class Ed25519Test {
33   @Test
testGroupOrder()34   public void testGroupOrder() throws Exception {
35     assertEquals(32, Ed25519.GROUP_ORDER.length);
36     byte[] result = Ed25519.scalarMultWithBaseToBytes(Ed25519.GROUP_ORDER);
37     assertEquals(1, result[0]);
38     for (int i = 1; i < 32; i++) {
39       assertEquals(0, result[i]);
40     }
41   }
42 
43   /** Test whether sign/verify method accidentally changes the public key or hashedPrivateKey. */
44   @Test
testUnmodifiedKey()45   public void testUnmodifiedKey() throws Exception {
46     byte[] privateKey = Random.randBytes(Field25519.FIELD_LEN);
47     byte[] hashedPrivateKey = Ed25519.getHashedScalar(privateKey);
48     byte[] originalHashedPrivateKey =
49         Arrays.copyOfRange(hashedPrivateKey, 0, hashedPrivateKey.length);
50     byte[] publicKey = Ed25519.scalarMultWithBaseToBytes(hashedPrivateKey);
51     byte[] originalPublicKey = Arrays.copyOfRange(publicKey, 0, publicKey.length);
52     for (int i = 0; i < 64; i++) {
53       byte[] msg = Random.randBytes(1024);
54       byte[] sig = Ed25519.sign(msg, publicKey, hashedPrivateKey);
55       assertTrue(Ed25519.verify(msg, sig, publicKey));
56       assertArrayEquals(originalHashedPrivateKey, hashedPrivateKey);
57       assertArrayEquals(originalPublicKey, publicKey);
58     }
59   }
60 
61   /** Test for https://github.com/google/tink/issues/224. */
62   @Test
testScalarMultWithBase()63   public void testScalarMultWithBase() throws Exception {
64     byte[] scalar = Hex.decode("521784c403e6fb32d48e0da85969a82f5952856bde4471a42b3fa56fd8b96c0d");
65     Object unused = Ed25519.scalarMultWithBaseToBytes(scalar);
66   }
67 }
68