Debian Codegraph @

crypto/verification_sas_methods.go

raw
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 //go:build !nosas
8 // +build !nosas
9
10 package crypto
11
12 import (
13 "fmt"
14
15 "maunium.net/go/mautrix/crypto/olm"
16 "maunium.net/go/mautrix/event"
17 "maunium.net/go/mautrix/id"
18 )
19
20 // SASData contains the data that users need to verify.
21 type SASData interface {
22 Type() event.SASMethod
23 }
24
25 // VerificationMethod describes a method for generating a SAS.
26 type VerificationMethod interface {
27 // GetVerificationSAS uses the user, device ID and key of the user who initiated the verification transaction,
28 // the user, device ID and key of the user who accepted, the transaction ID and the SAS object to generate a SAS.
29 // The SAS can be any type, such as an array of numbers or emojis.
30 GetVerificationSAS(initUserID id.UserID, initDeviceID id.DeviceID, initKey string,
31 acceptUserID id.UserID, acceptDeviceID id.DeviceID, acceptKey string,
32 transactionID string, sas *olm.SAS) (SASData, error)
33 // Type returns the type of this SAS method
34 Type() event.SASMethod
35 }
36
37 const sasInfoFormat = "MATRIX_KEY_VERIFICATION_SAS|%s|%s|%s|%s|%s|%s|%s"
38
39 // VerificationMethodDecimal describes the decimal SAS method.
40 type VerificationMethodDecimal struct{}
41
42 // DecimalSASData contains the verification numbers for the decimal SAS method.
43 type DecimalSASData [3]uint
44
45 // Type returns the decimal SAS method type.
46 func (DecimalSASData) Type() event.SASMethod {
47 return event.SASDecimal
48 }
49
50 // GetVerificationSAS generates the three numbers that need to match with the other device for a verification to be valid.
51 func (VerificationMethodDecimal) GetVerificationSAS(initUserID id.UserID, initDeviceID id.DeviceID, initKey string,
52 acceptUserID id.UserID, acceptDeviceID id.DeviceID, acceptKey string,
53 transactionID string, sas *olm.SAS) (SASData, error) {
54
55 sasInfo := fmt.Sprintf(sasInfoFormat,
56 initUserID, initDeviceID, initKey,
57 acceptUserID, acceptDeviceID, acceptKey,
58 transactionID)
59
60 sasBytes, err := sas.GenerateBytes([]byte(sasInfo), 5)
61 if err != nil {
62 return DecimalSASData{0, 0, 0}, err
63 }
64
65 numbers := DecimalSASData{
66 (uint(sasBytes[0])<<5 | uint(sasBytes[1])>>3) + 1000,
67 (uint(sasBytes[1]&0x7)<<10 | uint(sasBytes[2])<<2 | uint(sasBytes[3]>>6)) + 1000,
68 (uint(sasBytes[3]&0x3F)<<7 | uint(sasBytes[4])>>1) + 1000,
69 }
70
71 return numbers, nil
72 }
73
74 // Type returns the decimal SAS method type.
75 func (VerificationMethodDecimal) Type() event.SASMethod {
76 return event.SASDecimal
77 }
78
79 var allEmojis = [...]VerificationEmoji{
80 {'🐢', "Dog"},
81 {'🐱', "Cat"},
82 {'🦁', "Lion"},
83 {'🐎', "Horse"},
84 {'πŸ¦„', "Unicorn"},
85 {'🐷', "Pig"},
86 {'🐘', "Elephant"},
87 {'🐰', "Rabbit"},
88 {'🐼', "Panda"},
89 {'πŸ“', "Rooster"},
90 {'🐧', "Penguin"},
91 {'🐒', "Turtle"},
92 {'🐟', "Fish"},
93 {'πŸ™', "Octopus"},
94 {'πŸ¦‹', "Butterfly"},
95 {'🌷', "Flower"},
96 {'🌳', "Tree"},
97 {'🌡', "Cactus"},
98 {'πŸ„', "Mushroom"},
99 {'🌏', "Globe"},
100 {'πŸŒ™', "Moon"},
101 {'☁', "Cloud"},
102 {'πŸ”₯', "Fire"},
103 {'🍌', "Banana"},
104 {'🍎', "Apple"},
105 {'πŸ“', "Strawberry"},
106 {'🌽', "Corn"},
107 {'πŸ•', "Pizza"},
108 {'πŸŽ‚', "Cake"},
109 {'❀', "Heart"},
110 {'πŸ˜€', "Smiley"},
111 {'πŸ€–', "Robot"},
112 {'🎩', "Hat"},
113 {'πŸ‘“', "Glasses"},
114 {'πŸ”§', "Spanner"},
115 {'πŸŽ…', "Santa"},
116 {'πŸ‘', "Thumbs Up"},
117 {'β˜‚', "Umbrella"},
118 {'βŒ›', "Hourglass"},
119 {'⏰', "Clock"},
120 {'🎁', "Gift"},
121 {'πŸ’‘', "Light Bulb"},
122 {'πŸ“•', "Book"},
123 {'✏', "Pencil"},
124 {'πŸ“Ž', "Paperclip"},
125 {'βœ‚', "Scissors"},
126 {'πŸ”’', "Lock"},
127 {'πŸ”‘', "Key"},
128 {'πŸ”¨', "Hammer"},
129 {'☎', "Telephone"},
130 {'🏁', "Flag"},
131 {'πŸš‚', "Train"},
132 {'🚲', "Bicycle"},
133 {'✈', "Aeroplane"},
134 {'πŸš€', "Rocket"},
135 {'πŸ†', "Trophy"},
136 {'⚽', "Ball"},
137 {'🎸', "Guitar"},
138 {'🎺', "Trumpet"},
139 {'πŸ””', "Bell"},
140 {'βš“', "Anchor"},
141 {'🎧', "Headphones"},
142 {'πŸ“', "Folder"},
143 {'πŸ“Œ', "Pin"},
144 }
145
146 // VerificationEmoji describes an emoji that might be sent for verifying devices.
147 type VerificationEmoji struct {
148 Emoji rune
149 Description string
150 }
151
152 func (vm VerificationEmoji) GetEmoji() rune {
153 return vm.Emoji
154 }
155
156 func (vm VerificationEmoji) GetDescription() string {
157 return vm.Description
158 }
159
160 // EmojiSASData contains the verification emojis for the emoji SAS method.
161 type EmojiSASData [7]VerificationEmoji
162
163 // Type returns the emoji SAS method type.
164 func (EmojiSASData) Type() event.SASMethod {
165 return event.SASEmoji
166 }
167
168 // VerificationMethodEmoji describes the emoji SAS method.
169 type VerificationMethodEmoji struct{}
170
171 // GetVerificationSAS generates the three numbers that need to match with the other device for a verification to be valid.
172 func (VerificationMethodEmoji) GetVerificationSAS(initUserID id.UserID, initDeviceID id.DeviceID, initKey string,
173 acceptUserID id.UserID, acceptDeviceID id.DeviceID, acceptKey string,
174 transactionID string, sas *olm.SAS) (SASData, error) {
175
176 sasInfo := fmt.Sprintf(sasInfoFormat,
177 initUserID, initDeviceID, initKey,
178 acceptUserID, acceptDeviceID, acceptKey,
179 transactionID)
180
181 var emojis EmojiSASData
182 sasBytes, err := sas.GenerateBytes([]byte(sasInfo), 6)
183
184 if err != nil {
185 return emojis, err
186 }
187
188 sasNum := uint64(sasBytes[0])<<40 | uint64(sasBytes[1])<<32 | uint64(sasBytes[2])<<24 |
189 uint64(sasBytes[3])<<16 | uint64(sasBytes[4])<<8 | uint64(sasBytes[5])
190
191 for i := 0; i < len(emojis); i++ {
192 // take nth group of 6 bits
193 emojiIdx := (sasNum >> uint(48-(i+1)*6)) & 0x3F
194 emoji := allEmojis[emojiIdx]
195 emojis[i] = emoji
196 }
197
198 return emojis, nil
199 }
200
201 // Type returns the emoji SAS method type.
202 func (VerificationMethodEmoji) Type() event.SASMethod {
203 return event.SASEmoji
204 }