|
1 |
| -/** |
2 |
| - * Copyright (C) 2016 langboost |
3 |
| - * |
4 |
| - * This program is free software: you can redistribute it and/or modify |
5 |
| - * it under the terms of the GNU General Public License as published by |
6 |
| - * the Free Software Foundation, either version 3 of the License, or |
7 |
| - * (at your option) any later version. |
8 |
| - * |
9 |
| - * This program is distributed in the hope that it will be useful, |
10 |
| - * but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 |
| - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 |
| - * GNU General Public License for more details. |
13 |
| - * |
14 |
| - * You should have received a copy of the GNU General Public License |
15 |
| - * along with this program. If not, see <http://www.gnu.org/licenses/>. |
16 |
| - */ |
17 |
| - |
18 |
| -using libsignal; |
| 1 | +using libsignal; |
19 | 2 | using libsignal.ecc;
|
20 | 3 | using Microsoft.VisualStudio.TestTools.UnitTesting;
|
| 4 | +using System; |
21 | 5 |
|
22 | 6 | namespace libsignal_test
|
23 | 7 | {
|
@@ -187,6 +171,59 @@ public void testSignature()
|
187 | 171 | }
|
188 | 172 | }
|
189 | 173 |
|
| 174 | + public void TestDecodeSize() |
| 175 | + { |
| 176 | + ECKeyPair keyPair = Curve.generateKeyPair(); |
| 177 | + byte[] serializedPublic = keyPair.getPublicKey().serialize(); |
| 178 | + |
| 179 | + ECPublicKey justRight = Curve.decodePoint(serializedPublic, 0); |
| 180 | + |
| 181 | + try |
| 182 | + { |
| 183 | + ECPublicKey tooSmall = Curve.decodePoint(serializedPublic, 1); |
| 184 | + Assert.Fail("Shouldn't decode"); |
| 185 | + } |
| 186 | + catch (InvalidKeyException e) |
| 187 | + { |
| 188 | + // good |
| 189 | + } |
| 190 | + |
| 191 | + try |
| 192 | + { |
| 193 | + ECPublicKey empty = Curve.decodePoint(new byte[0], 0); |
| 194 | + Assert.Fail("Shouldn't parse"); |
| 195 | + } |
| 196 | + catch (InvalidKeyException e) |
| 197 | + { |
| 198 | + // good |
| 199 | + } |
| 200 | + |
| 201 | + try |
| 202 | + { |
| 203 | + byte[] badKeyType = new byte[33]; |
| 204 | + Array.Copy(serializedPublic, 0, badKeyType, 0, serializedPublic.Length); |
| 205 | + badKeyType[0] = 0x01; |
| 206 | + Curve.decodePoint(badKeyType, 0); |
| 207 | + Assert.Fail("Should be bad key type"); |
| 208 | + } |
| 209 | + catch (InvalidKeyException e) |
| 210 | + { |
| 211 | + // good |
| 212 | + } |
| 213 | + |
| 214 | + byte[] extraSpace = new byte[serializedPublic.Length + 1]; |
| 215 | + Array.Copy(serializedPublic, 0, extraSpace, 0, serializedPublic.Length); |
| 216 | + ECPublicKey extra = Curve.decodePoint(extraSpace, 0); |
| 217 | + |
| 218 | + byte[] offsetSpace = new byte[serializedPublic.Length + 1]; |
| 219 | + Array.Copy(serializedPublic, 0, offsetSpace, 1, serializedPublic.Length); |
| 220 | + ECPublicKey offset = Curve.decodePoint(offsetSpace, 1); |
| 221 | + |
| 222 | + CollectionAssert.AreEqual(serializedPublic, justRight.serialize()); |
| 223 | + CollectionAssert.AreEqual(extra.serialize(), serializedPublic); |
| 224 | + CollectionAssert.AreEqual(offset.serialize(), serializedPublic); |
| 225 | + } |
| 226 | + |
190 | 227 | [TestMethod, TestCategory("libsignal.ecc")]
|
191 | 228 | public void testSignatureOverflow()
|
192 | 229 | {
|
|
0 commit comments