|
7 | 7 |
|
8 | 8 | public final class TweetNaclFastTest { |
9 | 9 | private static final String TAG = "TweetNaclFastTest"; |
| 10 | + |
| 11 | + /** |
| 12 | + * Curve25519 test vectors to help ensure correctness and interoperability |
| 13 | + * copied from Kalium project (https://github.com/abstractj/kalium) |
| 14 | + */ |
| 15 | + |
| 16 | + public static final String BOB_PRIVATE_KEY = "5dab087e624a8a4b79e17f8b83800ee66f3bb1292618b6fd1c2f8b27ff88e0eb"; |
| 17 | + public static final String BOB_PUBLIC_KEY = "de9edb7d7b7dc1b4d35b61c2ece435373f8343c85b78674dadfc7e146f882b4f"; |
| 18 | + |
| 19 | + public static final String ALICE_PRIVATE_KEY = "77076d0a7318a57d3c16c17251b26645df4c2f87ebc0992ab177fba51db92c2a"; |
| 20 | + public static final String ALICE_PUBLIC_KEY = "8520f0098930a754748b7ddcb43ef75a0dbf3a0d26381af4eba4a98eaa9b4e6a"; |
| 21 | + public static final String ALICE_MULT_BOB = "4a5d9d5ba4ce2de1728e3bf480350f25e07e21c947d19e3376f09b3c1e161742"; |
| 22 | + |
| 23 | + public static final String BOX_NONCE = "69696ee955b62b73cd62bda875fc73d68219e0036b7a0b37"; |
| 24 | + public static final String BOX_MESSAGE = "be075fc53c81f2d5cf141316ebeb0c7b5228c52a4c62cbd44b66849b64244ffc" + |
| 25 | + "e5ecbaaf33bd751a1ac728d45e6c61296cdc3c01233561f41db66cce314adb31" + |
| 26 | + "0e3be8250c46f06dceea3a7fa1348057e2f6556ad6b1318a024a838f21af1fde" + |
| 27 | + "048977eb48f59ffd4924ca1c60902e52f0a089bc76897040e082f93776384864" + |
| 28 | + "5e0705"; |
| 29 | + public static final String BOX_CIPHERTEXT = "f3ffc7703f9400e52a7dfb4b3d3305d98e993b9f48681273c29650ba32fc76ce" + |
| 30 | + "48332ea7164d96a4476fb8c531a1186ac0dfc17c98dce87b4da7f011ec48c972" + |
| 31 | + "71d2c20f9b928fe2270d6fb863d51738b48eeee314a7cc8ab932164548e526ae" + |
| 32 | + "90224368517acfeabd6bb3732bc0e9da99832b61ca01b6de56244a9e88d5f9b3" + |
| 33 | + "7973f622a43d14a6599b1f654cb45a74e355a5"; |
| 34 | + |
| 35 | + public static final String SECRET_KEY = "1b27556473e985d462cd51197a9a46c76009549eac6474f206c4ee0844f68389"; |
| 36 | + |
| 37 | + public static final String SIGN_PRIVATE = "b18e1d0045995ec3d010c387ccfeb984d783af8fbb0f40fa7db126d889f6dadd"; |
| 38 | + public static final String SIGN_MESSAGE = "916c7d1d268fc0e77c1bef238432573c39be577bbea0998936add2b50a653171" + |
| 39 | + "ce18a542b0b7f96c1691a3be6031522894a8634183eda38798a0c5d5d79fbd01" + |
| 40 | + "dd04a8646d71873b77b221998a81922d8105f892316369d5224c9983372d2313" + |
| 41 | + "c6b1f4556ea26ba49d46e8b561e0fc76633ac9766e68e21fba7edca93c4c7460" + |
| 42 | + "376d7f3ac22ff372c18f613f2ae2e856af40"; |
| 43 | + public static final String SIGN_SIGNATURE = "6bd710a368c1249923fc7a1610747403040f0cc30815a00f9ff548a896bbda0b" + |
| 44 | + "4eb2ca19ebcf917f0f34200a9edbad3901b64ab09cc5ef7b9bcc3c40c0ff7509"; |
| 45 | + public static final String SIGN_PUBLIC = "77f48b59caeda77751ed138b0ec667ff50f8768c25d48309a8f386a2bad187fb"; |
| 46 | + |
| 47 | + private boolean testBoxKalium() throws UnsupportedEncodingException { |
10 | 48 |
|
| 49 | + Log.d(TAG, "testBoxKalium: test vectors from Kalium project"); |
| 50 | + |
| 51 | + // explicit nonce |
| 52 | + byte [] theNonce = TweetNaclFast.hexDecode(BOX_NONCE); |
| 53 | + Log.d(TAG, "BOX_NONCE: " + "\"" + TweetNaclFast.hexEncodeToString(theNonce) + "\""); |
| 54 | + |
| 55 | + |
| 56 | + // keypair A |
| 57 | + byte [] ska = TweetNaclFast.hexDecode(ALICE_PRIVATE_KEY); |
| 58 | + TweetNaclFast.Box.KeyPair ka = TweetNaclFast.Box.keyPair_fromSecretKey(ska); |
| 59 | + |
| 60 | + Log.d(TAG, "ska: " + "\"" + TweetNaclFast.hexEncodeToString(ka.getSecretKey()) + "\""); |
| 61 | + Log.d(TAG, "pka: " + "\"" + TweetNaclFast.hexEncodeToString(ka.getPublicKey()) + "\""); |
| 62 | + |
| 63 | + // keypair B |
| 64 | + byte [] skb = TweetNaclFast.hexDecode(BOB_PRIVATE_KEY); |
| 65 | + TweetNaclFast.Box.KeyPair kb = TweetNaclFast.Box.keyPair_fromSecretKey(skb); |
| 66 | + |
| 67 | + Log.d(TAG, "skb: " + "\"" + TweetNaclFast.hexEncodeToString(kb.getSecretKey()) + "\""); |
| 68 | + Log.d(TAG, "pkb: " + "\"" + TweetNaclFast.hexEncodeToString(kb.getPublicKey()) + "\""); |
| 69 | + |
| 70 | + // peer A -> B |
| 71 | + TweetNaclFast.Box pabFast = new TweetNaclFast.Box(kb.getPublicKey(), ka.getSecretKey()); |
| 72 | + |
| 73 | + // peer B -> A |
| 74 | + TweetNaclFast.Box pbaFast = new TweetNaclFast.Box(ka.getPublicKey(), kb.getSecretKey()); |
| 75 | + |
| 76 | + // messages |
| 77 | + |
| 78 | + Log.d(TAG, "BOX_MESSAGE: \n" + BOX_MESSAGE.toUpperCase()); |
| 79 | + Log.d(TAG, "BOX_CIPHERTEXT: \n" + BOX_CIPHERTEXT.toUpperCase()); |
| 80 | + |
| 81 | + // cipher A -> B |
| 82 | + byte [] cabFast = pabFast.box(TweetNaclFast.hexDecode(BOX_MESSAGE), theNonce); |
| 83 | + Log.d(TAG, "cabFast: \n" + TweetNaclFast.hexEncodeToString(cabFast)); |
| 84 | + |
| 85 | + if(BOX_CIPHERTEXT.toUpperCase().equals(TweetNaclFast.hexEncodeToString(cabFast))) { |
| 86 | + Log.d(TAG, "\n TweetNaclFast is compatible with Kalium test vector for Box::box"); |
| 87 | + } else { |
| 88 | + Log.d(TAG, "\n\n!!! TweetNaclFast Box::box/open failed Kalium compatibility !!!\n"); |
| 89 | + return false; |
| 90 | + } |
| 91 | + |
| 92 | + byte [] mbaFastFast = pbaFast.open(cabFast, theNonce); |
| 93 | + Log.d(TAG, "mbaFastFast: \n" + TweetNaclFast.hexEncodeToString(mbaFastFast)); |
| 94 | + |
| 95 | + if(BOX_MESSAGE.toUpperCase().equals(TweetNaclFast.hexEncodeToString(mbaFastFast))) { |
| 96 | + Log.d(TAG, "\n TweetNaclFast is compatible with Kalium test vector for Box::open"); |
| 97 | + } else { |
| 98 | + Log.d(TAG, "\n\n!!! TweetNaclFast Box::box/open failed Kalium compatibility !!!\n"); |
| 99 | + return false; |
| 100 | + } |
| 101 | + |
| 102 | + return true; |
| 103 | + } |
| 104 | + |
| 105 | + |
11 | 106 | private boolean testBox() throws UnsupportedEncodingException { |
12 | 107 | // keypair A |
13 | 108 | byte [] ska = new byte[32]; for (int i = 0; i < 32; i ++) ska[i] = 0; |
@@ -446,6 +541,7 @@ public void run() { |
446 | 541 | testSecretBoxNonce(); |
447 | 542 | testBox(); |
448 | 543 | testBoxNonce(); |
| 544 | + testBoxKalium(); |
449 | 545 |
|
450 | 546 | testHash(); |
451 | 547 | testSign(); |
|
0 commit comments