aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/app/src/main/java/com/wireguard/crypto/Keypair.java
blob: c2f2654b6e51277afbb51c197169e54c9882aa3e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/*
 * Copyright © 2018 Samuel Holland <samuel@sholland.org>
 * Copyright © 2018 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
 * SPDX-License-Identifier: GPL-2.0-or-later
 */

package com.wireguard.crypto;

import java.security.SecureRandom;

/**
 * Represents a Curve25519 keypair as used by WireGuard.
 */

public class Keypair {
    private final byte[] privateKey;
    private final byte[] publicKey;

    public Keypair() {
        this(generatePrivateKey());
    }

    private Keypair(final byte[] privateKey) {
        this.privateKey = privateKey;
        publicKey = generatePublicKey(privateKey);
    }

    public Keypair(final String privateKey) {
        this(KeyEncoding.keyFromBase64(privateKey));
    }

    @SuppressWarnings("MagicNumber")
    private static byte[] generatePrivateKey() {
        final SecureRandom secureRandom = new SecureRandom();
        final byte[] privateKey = new byte[KeyEncoding.KEY_LENGTH];
        secureRandom.nextBytes(privateKey);
        privateKey[0] &= 248;
        privateKey[31] &= 127;
        privateKey[31] |= 64;
        return privateKey;
    }

    private static byte[] generatePublicKey(final byte[] privateKey) {
        final byte[] publicKey = new byte[KeyEncoding.KEY_LENGTH];
        Curve25519.eval(publicKey, 0, privateKey, null);
        return publicKey;
    }

    public String getPrivateKey() {
        return KeyEncoding.keyToBase64(privateKey);
    }

    public String getPublicKey() {
        return KeyEncoding.keyToBase64(publicKey);
    }
}