| 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 |
} |