| 1 |
// Copyright (c) 2020 Tulir Asokan |
| 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 ssss_test |
| 8 |
|
| 9 |
import ( |
| 10 |
"encoding/json" |
| 11 |
"errors" |
| 12 |
"testing" |
| 13 |
|
| 14 |
"github.com/stretchr/testify/assert" |
| 15 |
|
| 16 |
"maunium.net/go/mautrix/crypto/ssss" |
| 17 |
"maunium.net/go/mautrix/event" |
| 18 |
) |
| 19 |
|
| 20 |
const key1CrossSigningMasterKey = ` |
| 21 |
{ |
| 22 |
"encrypted": { |
| 23 |
"gEJqbfSEMnP5JXXcukpXEX1l0aI3MDs0": { |
| 24 |
"iv": "BpKP9nQJTE9jrsAssoxPqQ==", |
| 25 |
"ciphertext": "fNRiiiidezjerTgV+G6pUtmeF3izzj5re/mVvY0hO2kM6kYGrxLuIu2ej80=", |
| 26 |
"mac": "/gWGDGMyOLmbJp+aoSLh5JxCs0AdS6nAhjzpe+9G2Q0=" |
| 27 |
} |
| 28 |
} |
| 29 |
} |
| 30 |
` |
| 31 |
|
| 32 |
var key1CrossSigningMasterKeyDecrypted = []byte{ |
| 33 |
0x68, 0xf9, 0x7f, 0xd1, 0x92, 0x2e, 0xec, 0xf6, |
| 34 |
0xb8, 0x2b, 0xb8, 0x90, 0xd2, 0x4d, 0x06, 0x52, |
| 35 |
0x98, 0x4e, 0x7a, 0x1d, 0x70, 0x3b, 0x9e, 0x86, |
| 36 |
0x7b, 0x7e, 0xba, 0xf7, 0xfe, 0xb9, 0x5b, 0x6f, |
| 37 |
} |
| 38 |
|
| 39 |
func getEncryptedMasterKey() *ssss.EncryptedAccountDataEventContent { |
| 40 |
var eadec ssss.EncryptedAccountDataEventContent |
| 41 |
err := json.Unmarshal([]byte(key1CrossSigningMasterKey), &eadec) |
| 42 |
if err != nil { |
| 43 |
panic(err) |
| 44 |
} |
| 45 |
return &eadec |
| 46 |
} |
| 47 |
|
| 48 |
func TestKey_Decrypt_Success(t *testing.T) { |
| 49 |
key := getKey1() |
| 50 |
emk := getEncryptedMasterKey() |
| 51 |
decrypted, err := emk.Decrypt(event.AccountDataCrossSigningMaster.Type, key) |
| 52 |
assert.NoError(t, err) |
| 53 |
assert.Equal(t, key1CrossSigningMasterKeyDecrypted, decrypted) |
| 54 |
} |
| 55 |
|
| 56 |
func TestKey_Decrypt_WrongKey(t *testing.T) { |
| 57 |
key := getKey2() |
| 58 |
emk := getEncryptedMasterKey() |
| 59 |
decrypted, err := emk.Decrypt(event.AccountDataCrossSigningMaster.Type, key) |
| 60 |
assert.True(t, errors.Is(err, ssss.ErrNotEncryptedForKey), "unexpected error %v", err) |
| 61 |
assert.Nil(t, decrypted) |
| 62 |
} |
| 63 |
|
| 64 |
func TestKey_Decrypt_FakeKey(t *testing.T) { |
| 65 |
key := getKey2() |
| 66 |
key.ID = key1ID |
| 67 |
emk := getEncryptedMasterKey() |
| 68 |
decrypted, err := emk.Decrypt(event.AccountDataCrossSigningMaster.Type, key) |
| 69 |
assert.True(t, errors.Is(err, ssss.ErrKeyDataMACMismatch), "unexpected error %v", err) |
| 70 |
assert.Nil(t, decrypted) |
| 71 |
} |
| 72 |
|
| 73 |
func TestKey_Decrypt_WrongType(t *testing.T) { |
| 74 |
key := getKey1() |
| 75 |
emk := getEncryptedMasterKey() |
| 76 |
decrypted, err := emk.Decrypt(event.AccountDataCrossSigningSelf.Type, key) |
| 77 |
assert.True(t, errors.Is(err, ssss.ErrKeyDataMACMismatch), "unexpected error %v", err) |
| 78 |
assert.Nil(t, decrypted) |
| 79 |
} |
| 80 |
|
| 81 |
func TestKey_Encrypt(t *testing.T) { |
| 82 |
key1 := getKey1() |
| 83 |
var evtType = "net.maunium.data" |
| 84 |
var data = []byte{0xde, 0xad, 0xbe, 0xef} |
| 85 |
encrypted := key1.Encrypt(evtType, data) |
| 86 |
decrypted, err := key1.Decrypt(evtType, encrypted) |
| 87 |
assert.NoError(t, err) |
| 88 |
assert.Equal(t, data, decrypted) |
| 89 |
} |