Coverage Summary for Class: AuthInitiateMessage (org.ethereum.net.rlpx)
Class |
Class, %
|
Method, %
|
Line, %
|
AuthInitiateMessage |
0%
(0/1)
|
0%
(0/7)
|
0%
(0/60)
|
1 /*
2 * This file is part of RskJ
3 * Copyright (C) 2017 RSK Labs Ltd.
4 * (derived from ethereumJ library, Copyright (c) 2016 <ether.camp>)
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 package org.ethereum.net.rlpx;
21
22 import org.bouncycastle.math.ec.ECPoint;
23 import org.ethereum.crypto.ECKey;
24 import org.ethereum.crypto.signature.ECDSASignature;
25 import org.ethereum.util.ByteUtil;
26
27 import static org.bouncycastle.util.BigIntegers.asUnsignedByteArray;
28 import static org.ethereum.util.ByteUtil.merge;
29
30 /**
31 * Authentication initiation message, to be wrapped inside
32 *
33 * Created by devrandom on 2015-04-07.
34 */
35 public class AuthInitiateMessage {
36 private ECDSASignature signature; // 65 bytes
37 byte[] ephemeralPublicHash; // 32 bytes
38 ECPoint publicKey; // 64 bytes - uncompressed and no type byte
39 byte[] nonce; // 32 bytes
40 boolean isTokenUsed; // 1 byte - 0x00 or 0x01
41
42 public AuthInitiateMessage() {
43 }
44
45 public static int getLength() {
46 return 65+32+64+32+1;
47 }
48
49 static AuthInitiateMessage decode(byte[] wire) {
50 AuthInitiateMessage message = new AuthInitiateMessage();
51 int offset = 0;
52 byte[] r = new byte[32];
53 byte[] s = new byte[32];
54 System.arraycopy(wire, offset, r, 0, 32);
55 offset += 32;
56 System.arraycopy(wire, offset, s, 0, 32);
57 offset += 32;
58 int v = wire[offset] + 27;
59 offset += 1;
60 message.signature = ECDSASignature.fromComponents(r, s, (byte)v);
61 message.ephemeralPublicHash = new byte[32];
62 System.arraycopy(wire, offset, message.ephemeralPublicHash, 0, 32);
63 offset += 32;
64 byte[] bytes = new byte[65];
65 System.arraycopy(wire, offset, bytes, 1, 64);
66 offset += 64;
67 bytes[0] = 0x04; // uncompressed
68 message.publicKey = ECKey.CURVE.getCurve().decodePoint(bytes);
69 message.nonce = new byte[32];
70 System.arraycopy(wire, offset, message.nonce, 0, 32);
71 offset += message.nonce.length;
72 byte tokenUsed = wire[offset];
73 offset += 1;
74 if (tokenUsed != 0x00 && tokenUsed != 0x01) {
75 throw new RuntimeException("invalid boolean"); // TODO specific exception
76 }
77 message.isTokenUsed = (tokenUsed == 0x01);
78 return message;
79 }
80
81 public byte[] encode() {
82
83 byte[] rsigPad = new byte[32];
84 byte[] rsig = asUnsignedByteArray(signature.getR());
85 System.arraycopy(rsig, 0, rsigPad, rsigPad.length - rsig.length, rsig.length);
86
87 byte[] ssigPad = new byte[32];
88 byte[] ssig = asUnsignedByteArray(signature.getS());
89 System.arraycopy(ssig, 0, ssigPad, ssigPad.length - ssig.length, ssig.length);
90
91 byte[] sigBytes = merge(rsigPad, ssigPad, new byte[]{EncryptionHandshake.recIdFromSignatureV(signature.getV())});
92
93 byte[] buffer = new byte[getLength()];
94 int offset = 0;
95 System.arraycopy(sigBytes, 0, buffer, offset, sigBytes.length);
96 offset += sigBytes.length;
97 System.arraycopy(ephemeralPublicHash, 0, buffer, offset, ephemeralPublicHash.length);
98 offset += ephemeralPublicHash.length;
99 byte[] publicBytes = publicKey.getEncoded(false);
100 System.arraycopy(publicBytes, 1, buffer, offset, publicBytes.length - 1);
101 offset += publicBytes.length - 1;
102 System.arraycopy(nonce, 0, buffer, offset, nonce.length);
103 offset += nonce.length;
104 buffer[offset] = (byte)(isTokenUsed ? 0x01 : 0x00);
105 offset += 1;
106 return buffer;
107 }
108
109 public ECDSASignature getSignature() {
110 return signature;
111 }
112
113 public void setSignature(ECDSASignature signature) {
114 this.signature = signature;
115 }
116
117 @Override
118 public String toString() {
119
120 byte[] sigBytes = merge(asUnsignedByteArray(signature.getR()),
121 asUnsignedByteArray(signature.getS()), new byte[]{EncryptionHandshake.recIdFromSignatureV(signature.getV())});
122
123 return "AuthInitiateMessage{" +
124 "\n sigBytes=" + ByteUtil.toHexString(sigBytes) +
125 "\n ephemeralPublicHash=" + ByteUtil.toHexString(ephemeralPublicHash) +
126 "\n publicKey=" + ByteUtil.toHexString(publicKey.getEncoded(false)) +
127 "\n nonce=" + ByteUtil.toHexString(nonce) +
128 "\n}";
129 }
130 }