Debian Codegraph @

crypto/ssss/meta.go

raw
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
8
9 import (
10 "encoding/base64"
11 "fmt"
12 "strings"
13
14 "maunium.net/go/mautrix/crypto/utils"
15 )
16
17 // KeyMetadata represents server-side metadata about a SSSS key. The metadata can be used to get
18 // the actual SSSS key from a passphrase or recovery key.
19 type KeyMetadata struct {
20 id string
21
22 Algorithm Algorithm `json:"algorithm"`
23 IV string `json:"iv"`
24 MAC string `json:"mac"`
25 Passphrase *PassphraseMetadata `json:"passphrase,omitempty"`
26 }
27
28 // VerifyRecoveryKey verifies that the given passphrase is valid and returns the computed SSSS key.
29 func (kd *KeyMetadata) VerifyPassphrase(passphrase string) (*Key, error) {
30 ssssKey, err := kd.Passphrase.GetKey(passphrase)
31 if err != nil {
32 return nil, err
33 } else if !kd.VerifyKey(ssssKey) {
34 return nil, ErrIncorrectSSSSKey
35 }
36
37 return &Key{
38 ID: kd.id,
39 Key: ssssKey,
40 Metadata: kd,
41 }, nil
42 }
43
44 // VerifyRecoveryKey verifies that the given recovery key is valid and returns the decoded SSSS key.
45 func (kd *KeyMetadata) VerifyRecoveryKey(recoverKey string) (*Key, error) {
46 ssssKey := utils.DecodeBase58RecoveryKey(recoverKey)
47 if ssssKey == nil {
48 return nil, ErrInvalidRecoveryKey
49 } else if !kd.VerifyKey(ssssKey) {
50 return nil, ErrIncorrectSSSSKey
51 }
52
53 return &Key{
54 ID: kd.id,
55 Key: ssssKey,
56 Metadata: kd,
57 }, nil
58 }
59
60 // VerifyKey verifies the SSSS key is valid by calculating and comparing its MAC.
61 func (kd *KeyMetadata) VerifyKey(key []byte) bool {
62 return strings.ReplaceAll(kd.MAC, "=", "") == strings.ReplaceAll(kd.calculateHash(key), "=", "")
63 }
64
65 // calculateHash calculates the hash used for checking if the key is entered correctly as described
66 // in the spec: https://matrix.org/docs/spec/client_server/unstable#m-secret-storage-v1-aes-hmac-sha2
67 func (kd *KeyMetadata) calculateHash(key []byte) string {
68 aesKey, hmacKey := utils.DeriveKeysSHA256(key, "")
69
70 var ivBytes [utils.AESCTRIVLength]byte
71 _, _ = base64.StdEncoding.Decode(ivBytes[:], []byte(kd.IV))
72
73 cipher := utils.XorA256CTR(make([]byte, utils.AESCTRKeyLength), aesKey, ivBytes)
74
75 return utils.HMACSHA256B64(cipher, hmacKey)
76 }
77
78 // PassphraseMetadata represents server-side metadata about a SSSS key passphrase.
79 type PassphraseMetadata struct {
80 Algorithm PassphraseAlgorithm `json:"algorithm"`
81 Iterations int `json:"iterations"`
82 Salt string `json:"salt"`
83 Bits int `json:"bits"`
84 }
85
86 // GetKey gets the SSSS key from the passphrase.
87 func (pd *PassphraseMetadata) GetKey(passphrase string) ([]byte, error) {
88 if pd == nil {
89 return nil, ErrNoPassphrase
90 }
91
92 if pd.Algorithm != PassphraseAlgorithmPBKDF2 {
93 return nil, fmt.Errorf("%w: %s", ErrUnsupportedPassphraseAlgorithm, pd.Algorithm)
94 }
95
96 bits := 256
97 if pd.Bits != 0 {
98 bits = pd.Bits
99 }
100
101 return utils.PBKDF2SHA512([]byte(passphrase), []byte(pd.Salt), pd.Iterations, bits), nil
102 }