/usr/lib/python3/dist-packages/axolotl/sessioncipher.py is in python3-axolotl 0.1.39-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 | # -*- coding: utf-8 -*-
import sys
import Crypto.Cipher.AES as AES
from Crypto.Util import Counter
from .ecc.curve import Curve
from .sessionbuilder import SessionBuilder
from .util.byteutil import ByteUtil
from .state.sessionstate import SessionState
from .protocol.whispermessage import WhisperMessage
from .protocol.prekeywhispermessage import PreKeyWhisperMessage
from .nosessionexception import NoSessionException
from .invalidmessageexception import InvalidMessageException
from .duplicatemessagexception import DuplicateMessageException
if sys.version_info >= (3, 0):
unicode = str
import logging
logger = logging.getLogger(__name__)
class SessionCipher:
def __init__(self, sessionStore, preKeyStore, signedPreKeyStore, identityKeyStore, recepientId, deviceId):
self.sessionStore = sessionStore
self.preKeyStore = preKeyStore
self.recipientId = recepientId
self.deviceId = deviceId
self.sessionBuilder = SessionBuilder(sessionStore, preKeyStore, signedPreKeyStore,
identityKeyStore, recepientId, deviceId)
def encrypt(self, paddedMessage):
"""
:type paddedMessage: str
"""
# TODO: make this less ugly and python 2 and 3 compatible
# paddedMessage = bytearray(paddedMessage.encode() if (sys.version_info >= (3, 0) and not type(paddedMessage) in (bytes, bytearray)) or type(paddedMessage) is unicode else paddedMessage)
if (sys.version_info >= (3, 0) and
not type(paddedMessage) in (bytes, bytearray)) or type(paddedMessage) is unicode:
paddedMessage = bytearray(paddedMessage.encode())
else:
paddedMessage = bytearray(paddedMessage)
sessionRecord = self.sessionStore.loadSession(self.recipientId, self.deviceId)
sessionState = sessionRecord.getSessionState()
chainKey = sessionState.getSenderChainKey()
messageKeys = chainKey.getMessageKeys()
senderEphemeral = sessionState.getSenderRatchetKey()
previousCounter = sessionState.getPreviousCounter()
sessionVersion = sessionState.getSessionVersion()
ciphertextBody = self.getCiphertext(sessionVersion, messageKeys, paddedMessage)
ciphertextMessage = WhisperMessage(sessionVersion, messageKeys.getMacKey(),
senderEphemeral, chainKey.getIndex(),
previousCounter, ciphertextBody,
sessionState.getLocalIdentityKey(),
sessionState.getRemoteIdentityKey())
if sessionState.hasUnacknowledgedPreKeyMessage():
items = sessionState.getUnacknowledgedPreKeyMessageItems()
localRegistrationid = sessionState.getLocalRegistrationId()
ciphertextMessage = PreKeyWhisperMessage(sessionVersion, localRegistrationid, items.getPreKeyId(),
items.getSignedPreKeyId(), items.getBaseKey(),
sessionState.getLocalIdentityKey(),
ciphertextMessage)
sessionState.setSenderChainKey(chainKey.getNextChainKey())
self.sessionStore.storeSession(self.recipientId, self.deviceId, sessionRecord)
return ciphertextMessage
def decryptMsg(self, ciphertext, textMsg=True):
"""
:type ciphertext: WhisperMessage
:type textMsg: Bool set this to False if you are decrypting bytes
instead of string
"""
if not self.sessionStore.containsSession(self.recipientId, self.deviceId):
raise NoSessionException("No session for: %s, %s" % (self.recipientId, self.deviceId))
sessionRecord = self.sessionStore.loadSession(self.recipientId, self.deviceId)
plaintext = self.decryptWithSessionRecord(sessionRecord, ciphertext)
self.sessionStore.storeSession(self.recipientId, self.deviceId, sessionRecord)
return plaintext
def decryptPkmsg(self, ciphertext, textMsg=True):
"""
:type ciphertext: PreKeyWhisperMessage
"""
sessionRecord = self.sessionStore.loadSession(self.recipientId, self.deviceId)
unsignedPreKeyId = self.sessionBuilder.process(sessionRecord, ciphertext)
plaintext = self.decryptWithSessionRecord(sessionRecord, ciphertext.getWhisperMessage())
# callback.handlePlaintext(plaintext)
self.sessionStore.storeSession(self.recipientId, self.deviceId, sessionRecord)
if unsignedPreKeyId is not None:
self.preKeyStore.removePreKey(unsignedPreKeyId)
return plaintext
def decryptWithSessionRecord(self, sessionRecord, cipherText):
"""
:type sessionRecord: SessionRecord
:type cipherText: WhisperMessage
"""
previousStates = sessionRecord.getPreviousSessionStates()
exceptions = []
try:
sessionState = SessionState(sessionRecord.getSessionState())
plaintext = self.decryptWithSessionState(sessionState, cipherText)
sessionRecord.setState(sessionState)
return plaintext
except InvalidMessageException as e:
exceptions.append(e)
for i in range(0, len(previousStates)):
previousState = previousStates[i]
try:
promotedState = SessionState(previousState)
plaintext = self.decryptWithSessionState(promotedState, cipherText)
previousStates.pop(i)
sessionRecord.promoteState(promotedState)
return plaintext
except InvalidMessageException as e:
exceptions.append(e)
raise InvalidMessageException("No valid sessions", exceptions)
def decryptWithSessionState(self, sessionState, ciphertextMessage):
if not sessionState.hasSenderChain():
raise InvalidMessageException("Uninitialized session!")
if ciphertextMessage.getMessageVersion() != sessionState.getSessionVersion():
raise InvalidMessageException("Message version %s, but session version %s" % (ciphertextMessage.getMessageVersion, sessionState.getSessionVersion()))
messageVersion = ciphertextMessage.getMessageVersion()
theirEphemeral = ciphertextMessage.getSenderRatchetKey()
counter = ciphertextMessage.getCounter()
chainKey = self.getOrCreateChainKey(sessionState, theirEphemeral)
messageKeys = self.getOrCreateMessageKeys(sessionState, theirEphemeral, chainKey, counter)
ciphertextMessage.verifyMac(messageVersion,
sessionState.getRemoteIdentityKey(),
sessionState.getLocalIdentityKey(),
messageKeys.getMacKey())
plaintext = self.getPlaintext(messageVersion, messageKeys, ciphertextMessage.getBody())
sessionState.clearUnacknowledgedPreKeyMessage()
return plaintext
def getOrCreateChainKey(self, sessionState, ECPublickKey_theirEphemeral):
theirEphemeral = ECPublickKey_theirEphemeral
if sessionState.hasReceiverChain(theirEphemeral):
return sessionState.getReceiverChainKey(theirEphemeral)
else:
rootKey = sessionState.getRootKey()
ourEphemeral = sessionState.getSenderRatchetKeyPair()
receiverChain = rootKey.createChain(theirEphemeral, ourEphemeral)
ourNewEphemeral = Curve.generateKeyPair()
senderChain = receiverChain[0].createChain(theirEphemeral, ourNewEphemeral)
sessionState.setRootKey(senderChain[0])
sessionState.addReceiverChain(theirEphemeral, receiverChain[1])
sessionState.setPreviousCounter(max(sessionState.getSenderChainKey().getIndex() - 1, 0))
sessionState.setSenderChain(ourNewEphemeral, senderChain[1])
return receiverChain[1]
def getOrCreateMessageKeys(self, sessionState, ECPublicKey_theirEphemeral, chainKey, counter):
theirEphemeral = ECPublicKey_theirEphemeral
if chainKey.getIndex() > counter:
if sessionState.hasMessageKeys(theirEphemeral, counter):
return sessionState.removeMessageKeys(theirEphemeral, counter)
else:
raise DuplicateMessageException("Received message with old counter: %s, %s" % (chainKey.getIndex(),
counter))
if counter - chainKey.getIndex() > 2000:
raise InvalidMessageException("Over 2000 messages into the future!")
while chainKey.getIndex() < counter:
messageKeys = chainKey.getMessageKeys()
sessionState.setMessageKeys(theirEphemeral, messageKeys)
chainKey = chainKey.getNextChainKey()
sessionState.setReceiverChainKey(theirEphemeral, chainKey.getNextChainKey())
return chainKey.getMessageKeys()
def getCiphertext(self, version, messageKeys, plainText):
"""
:type version: int
:type messageKeys: MessageKeys
:type plainText: bytearray
"""
cipher = None
if version >= 3:
cipher = self.getCipher(messageKeys.getCipherKey(), messageKeys.getIv())
else:
cipher = self.getCipher_v2(messageKeys.getCipherKey(), messageKeys.getCounter())
return cipher.encrypt(bytes(plainText))
def getPlaintext(self, version, messageKeys, cipherText):
cipher = None
if version >= 3:
cipher = self.getCipher(messageKeys.getCipherKey(), messageKeys.getIv())
else:
cipher = self.getCipher_v2(messageKeys.getCipherKey(), messageKeys.getCounter())
return cipher.decrypt(cipherText)
def getCipher(self, key, iv):
# Cipher.getInstance("AES/CBC/PKCS5Padding");
# cipher = AES.new(key, AES.MODE_CBC, IV = iv)
# return cipher
return AESCipher(key, iv)
def getCipher_v2(self, key, counter):
# AES/CTR/NoPadding
# counterbytes = struct.pack('>L', counter) + (b'\x00' * 12)
# counterint = struct.unpack(">L", counterbytes)[0]
# counterint = int.from_bytes(counterbytes, byteorder='big')
ctr = Counter.new(128, initial_value=counter)
cipher = AES.new(key, AES.MODE_CTR, counter=ctr)
return cipher
BS = 16
if sys.version_info >= (3, 0):
pad = lambda s: s + ((BS - len(s) % BS) * chr(BS - len(s) % BS)).encode()
unpad = lambda s : s[0:-s[-1]]
else:
pad = lambda s: s + (BS - len(s) % BS) * chr(BS - len(s) % BS)
unpad = lambda s : s[0:-ord(s[-1])]
class AESCipher:
def __init__(self, key, iv):
self.key = key
self.iv = iv
self.cipher = AES.new(key, AES.MODE_CBC, IV = iv)
def unpad(self, data):
unpadLength = data[-1]
if type(unpadLength) is int:
cmp = bytes([data[-unpadLength]] * unpadLength)
else:
unpadLength = ord(unpadLength)
cmp = data[-unpadLength] * unpadLength
if data[-unpadLength:] != cmp:
raise ValueError("Data not properly padded \n %s" % data)
return data[0:-unpadLength]
def encrypt(self, raw):
# if sys.version_info >= (3,0):
# rawPadded = pad(raw.decode()).encode()
# else:
rawPadded = pad(raw)
try:
return self.cipher.encrypt(rawPadded)
except ValueError:
raise
def decrypt(self, enc):
return self.unpad(self.cipher.decrypt(enc))
|