| 1 |
// Copyright (c) 2020 Nikos Filippakis |
| 2 |
// |
| 3 |
// This Source Code Form is subject to the terms of the Mozilla Public |
| 4 |
// License, v. 2.0. If a copy of the MPL was not distributed with this |
| 5 |
// file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 6 |
|
| 7 |
package utils |
| 8 |
|
| 9 |
import ( |
| 10 |
"encoding/base64" |
| 11 |
"testing" |
| 12 |
) |
| 13 |
|
| 14 |
func TestAES256Ctr(t *testing.T) { |
| 15 |
expected := "Hello world" |
| 16 |
key, iv := GenAttachmentA256CTR() |
| 17 |
enc := XorA256CTR([]byte(expected), key, iv) |
| 18 |
dec := XorA256CTR(enc, key, iv) |
| 19 |
if string(dec) != expected { |
| 20 |
t.Errorf("Expected decrypted using generated key/iv to be `%v`, got %v", expected, string(dec)) |
| 21 |
} |
| 22 |
|
| 23 |
var key2 [AESCTRKeyLength]byte |
| 24 |
var iv2 [AESCTRIVLength]byte |
| 25 |
for i := 0; i < AESCTRKeyLength; i++ { |
| 26 |
key2[i] = byte(i) |
| 27 |
} |
| 28 |
for i := 0; i < AESCTRIVLength; i++ { |
| 29 |
iv2[i] = byte(i) + 32 |
| 30 |
} |
| 31 |
dec2 := XorA256CTR([]byte{0x29, 0xc3, 0xff, 0x02, 0x21, 0xaf, 0x67, 0x73, 0x6e, 0xad, 0x9d}, key2, iv2) |
| 32 |
if string(dec2) != expected { |
| 33 |
t.Errorf("Expected decrypted using constant key/iv to be `%v`, got %v", expected, string(dec2)) |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
func TestPBKDF(t *testing.T) { |
| 38 |
salt := make([]byte, 16) |
| 39 |
for i := 0; i < 16; i++ { |
| 40 |
salt[i] = byte(i) |
| 41 |
} |
| 42 |
key := PBKDF2SHA512([]byte("Hello world"), salt, 1000, 256) |
| 43 |
expected := "ffk9YdbVE1cgqOWgDaec0lH+rJzO+MuCcxpIn3Z6D0E=" |
| 44 |
keyB64 := base64.StdEncoding.EncodeToString([]byte(key)) |
| 45 |
if keyB64 != expected { |
| 46 |
t.Errorf("Expected base64 of generated key to be `%v`, got `%v`", expected, keyB64) |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
func TestDecodeSSSSKey(t *testing.T) { |
| 51 |
recoveryKey := "EsTL 2cTx 9Qy1 8TVd qGsn GDrD i5dT EEuX Qz8U P7hi Z7uu U8wZ" |
| 52 |
decoded := DecodeBase58RecoveryKey(recoveryKey) |
| 53 |
|
| 54 |
expected := "QCFDrXZYLEFnwf4NikVm62rYGJS2mNBEmAWLC3CgNPw=" |
| 55 |
decodedB64 := base64.StdEncoding.EncodeToString(decoded[:]) |
| 56 |
if expected != decodedB64 { |
| 57 |
t.Errorf("Expected decoded recovery key b64 to be `%v`, got `%v`", expected, decodedB64) |
| 58 |
} |
| 59 |
|
| 60 |
if encoded := EncodeBase58RecoveryKey(decoded); encoded != recoveryKey { |
| 61 |
t.Errorf("Expected recovery key to be `%v`, got `%v`", recoveryKey, encoded) |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
func TestKeyDerivationAndHMAC(t *testing.T) { |
| 66 |
recoveryKey := "EsUG Ddi6 e1Cm F4um g38u JN72 d37v Q2ry qCf2 rKgL E2MQ ZQz6" |
| 67 |
decoded := DecodeBase58RecoveryKey(recoveryKey) |
| 68 |
|
| 69 |
aesKey, hmacKey := DeriveKeysSHA256(decoded[:], "m.cross_signing.master") |
| 70 |
|
| 71 |
ciphertextBytes, err := base64.StdEncoding.DecodeString("Fx16KlJ9vkd3Dd6CafIq5spaH5QmK5BALMzbtFbQznG2j1VARKK+klc4/Qo=") |
| 72 |
if err != nil { |
| 73 |
t.Error(err) |
| 74 |
} |
| 75 |
|
| 76 |
calcMac := HMACSHA256B64(ciphertextBytes, hmacKey) |
| 77 |
expectedMac := "0DABPNIZsP9iTOh1o6EM0s7BfHHXb96dN7Eca88jq2E=" |
| 78 |
if calcMac != expectedMac { |
| 79 |
t.Errorf("Expected MAC `%v`, got `%v`", expectedMac, calcMac) |
| 80 |
} |
| 81 |
|
| 82 |
var ivBytes [AESCTRIVLength]byte |
| 83 |
decodedIV, _ := base64.StdEncoding.DecodeString("zxT/W5LpZ0Q819pfju6hZw==") |
| 84 |
copy(ivBytes[:], decodedIV) |
| 85 |
decrypted := string(XorA256CTR(ciphertextBytes, aesKey, ivBytes)) |
| 86 |
|
| 87 |
expectedDec := "Ec8eZDyvVkO3EDsEG6ej5c0cCHnX7PINqFXZjnaTV2s=" |
| 88 |
if expectedDec != decrypted { |
| 89 |
t.Errorf("Expected decrypted text to be `%v`, got `%v`", expectedDec, decrypted) |
| 90 |
} |
| 91 |
} |