changeset c66c6d9161d2 in /home/hg/repos/gajim-plugins
author: Bahtiar `kalkin-` Gadimov bahtiar@gadimov.de branches: details:gajim-plugins?cmd=changeset;node=c66c6d9161d2 description: Replace omemo symlink with actual files
This solves issues for our windows friends
diffstat:
omemo/lib/python-omemo/src/omemo/__init__.py | 1 - omemo/lib/python-omemo/src/omemo/aes_gcm.py | 160 ------ omemo/lib/python-omemo/src/omemo/db_helpers.py | 13 - omemo/lib/python-omemo/src/omemo/encryption.py | 95 ---- omemo/lib/python-omemo/src/omemo/liteaxolotlstore.py | 130 ----- omemo/lib/python-omemo/src/omemo/liteidentitykeystore.py | 76 --- omemo/lib/python-omemo/src/omemo/liteprekeystore.py | 72 --- omemo/lib/python-omemo/src/omemo/litesessionstore.py | 88 --- omemo/lib/python-omemo/src/omemo/litesignedprekeystore.py | 77 --- omemo/lib/python-omemo/src/omemo/state.py | 332 -------------- omemo/omemo | 1 - omemo/omemo/__init__.py | 1 + omemo/omemo/aes_gcm.py | 160 ++++++ omemo/omemo/db_helpers.py | 13 + omemo/omemo/encryption.py | 95 ++++ omemo/omemo/liteaxolotlstore.py | 130 +++++ omemo/omemo/liteidentitykeystore.py | 76 +++ omemo/omemo/liteprekeystore.py | 72 +++ omemo/omemo/litesessionstore.py | 88 +++ omemo/omemo/litesignedprekeystore.py | 77 +++ omemo/omemo/state.py | 332 ++++++++++++++ 21 files changed, 1044 insertions(+), 1045 deletions(-)
diffs (truncated from 2174 to 300 lines):
diff -r 1dcdf1104bac -r c66c6d9161d2 omemo/lib/python-omemo/src/omemo/__init__.py --- a/omemo/lib/python-omemo/src/omemo/__init__.py Mon Apr 25 05:51:43 2016 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,1 +0,0 @@ -__version__ = "0.1.0" diff -r 1dcdf1104bac -r c66c6d9161d2 omemo/lib/python-omemo/src/omemo/aes_gcm.py --- a/omemo/lib/python-omemo/src/omemo/aes_gcm.py Mon Apr 25 05:51:43 2016 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,160 +0,0 @@ -# -*- coding: utf-8 -*- -# -# Copyright 2014 Jonathan Zdziarski jonathan@zdziarski.com -# -# All rights reserved. -# -# Redistribution and use in source and binary forms, with or without -# modification, are permitted provided that the following conditions are met: -# -# 1. Redistributions of source code must retain the above copyright notice, this -# list of conditions and the following disclaimer. -# -# 2. Redistributions in binary form must reproduce the above copyright notice, -# this list of conditions and the following disclaimer in the documentation -# and/or other materials provided with the distribution. -# -# 3. Neither the name of the copyright holder nor the names of its contributors -# may be used to endorse or promote products derived from this software without -# specific prior written permission. -# -# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" -# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE -# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE -# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE -# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL -# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR -# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER -# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, -# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - -import logging -from struct import pack, unpack - -from Crypto.Cipher import AES -from Crypto.Util import strxor - -log = logging.getLogger('gajim.plugin_system.omemo') - - -def gcm_rightshift(vec): - for x in range(15, 0, -1): - c = vec[x] >> 1 - c |= (vec[x - 1] << 7) & 0x80 - vec[x] = c - vec[0] >>= 1 - return vec - - -def gcm_gf_mult(a, b): - mask = [0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01] - poly = [0x00, 0xe1] - - Z = [0] * 16 - V = [c for c in a] - - for x in range(128): - if b[x >> 3] & mask[x & 7]: - Z = [V[y] ^ Z[y] for y in range(16)] - bit = V[15] & 1 - V = gcm_rightshift(V) - V[0] ^= poly[bit] - return Z - - -def ghash(h, auth_data, data): - u = (16 - len(data)) % 16 - v = (16 - len(auth_data)) % 16 - - x = auth_data + chr(0) * v + data + chr(0) * u - x += pack('>QQ', len(auth_data) * 8, len(data) * 8) - - y = [0] * 16 - vec_h = [ord(c) for c in h] - - for i in range(0, len(x), 16): - block = [ord(c) for c in x[i:i + 16]] - y = [y[j] ^ block[j] for j in range(16)] - y = gcm_gf_mult(y, vec_h) - - return ''.join(chr(c) for c in y) - - -def inc32(block): - counter, = unpack('>L', block[12:]) - counter += 1 - return block[:12] + pack('>L', counter) - - -def gctr(k, icb, plaintext): - y = '' - if len(plaintext) == 0: - return y - - aes = AES.new(k) - cb = icb - - for i in range(0, len(plaintext), aes.block_size): - cb = inc32(cb) - encrypted = aes.encrypt(cb) - plaintext_block = plaintext[i:i + aes.block_size] - y += strxor.strxor(plaintext_block, encrypted[:len(plaintext_block)]) - - return y - - -def gcm_decrypt(k, iv, encrypted, auth_data, tag): - aes = AES.new(k) - h = aes.encrypt(chr(0) * aes.block_size) - - if len(iv) == 12: - y0 = iv + "\x00\x00\x00\x01" - else: - y0 = ghash(h, '', iv) - - decrypted = gctr(k, y0, encrypted) - s = ghash(h, auth_data, encrypted) - - t = aes.encrypt(y0) - T = strxor.strxor(s, t) - if T != tag: - raise ValueError('Decrypted data is invalid') - else: - return decrypted - - -def gcm_encrypt(k, iv, plaintext, auth_data): - aes = AES.new(k) - h = aes.encrypt(chr(0) * aes.block_size) - - if len(iv) == 12: - y0 = iv + "\x00\x00\x00\x01" - else: - y0 = ghash(h, '', iv) - - encrypted = gctr(k, y0, plaintext) - s = ghash(h, auth_data, encrypted) - - t = aes.encrypt(y0) - T = strxor.strxor(s, t) - return (encrypted, T) - - -def aes_encrypt(key, nonce, plaintext): - """ Use AES128 GCM with the given key and iv to encrypt the payload. """ - c, t = gcm_encrypt(key, nonce, plaintext, '') - result = c + t - log.info(result) - return result - - -def aes_decrypt(key, nonce, payload): - """ Use AES128 GCM with the given key and iv to decrypt the payload. """ - ciphertext = payload[:-16] - mac = payload[-16:] - return gcm_decrypt(key, nonce, ciphertext, '', mac) - - -class NoValidSessions(Exception): - pass diff -r 1dcdf1104bac -r c66c6d9161d2 omemo/lib/python-omemo/src/omemo/db_helpers.py --- a/omemo/lib/python-omemo/src/omemo/db_helpers.py Mon Apr 25 05:51:43 2016 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,13 +0,0 @@ - -def table_exists(db, name): - """ Check if the specified table exists in the db. """ - - q = """ SELECT name FROM sqlite_master - WHERE type='table' AND name=?; - """ - return db.execute(q, (name, )).fetchone() is not None - - -def user_version(db): - """ Return the value of PRAGMA user_version. """ - return db.execute('PRAGMA user_version').fetchone()[0] diff -r 1dcdf1104bac -r c66c6d9161d2 omemo/lib/python-omemo/src/omemo/encryption.py --- a/omemo/lib/python-omemo/src/omemo/encryption.py Mon Apr 25 05:51:43 2016 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,95 +0,0 @@ -# -*- coding: utf-8 -*- -# -# Copyright 2015 Bahtiar `kalkin-` Gadimov bahtiar@gadimov.de -# Copyright 2015 Daniel Gultsch daniel@cgultsch.de -# -# This file is part of Gajim-OMEMO plugin. -# -# The Gajim-OMEMO plugin is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by the Free -# Software Foundation, either version 3 of the License, or (at your option) any -# later version. -# -# Gajim-OMEMO is distributed in the hope that it will be useful, but WITHOUT ANY -# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR -# A PARTICULAR PURPOSE. See the GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License along with -# the Gajim-OMEMO plugin. If not, see http://www.gnu.org/licenses/. -# - -from .db_helpers import table_exists, user_version - - -class EncryptionState(): - """ Used to store if OMEMO is enabled or not between gajim restarts """ - - def __init__(self, dbConn): - """ - :type dbConn: Connection - """ - self.dbConn = migrate(dbConn) - - def activate(self, jid): - q = """INSERT OR REPLACE INTO encryption_state (jid, encryption) - VALUES (?, 1) """ - - c = self.dbConn.cursor() - c.execute(q, (jid, )) - self.dbConn.commit() - - def deactivate(self, jid): - q = """INSERT OR REPLACE INTO encryption_state (jid, encryption) - VALUES (?, 0)""" - - c = self.dbConn.cursor() - c.execute(q, (jid, )) - self.dbConn.commit() - - def is_active(self, jid): - q = 'SELECT encryption FROM encryption_state where jid = ?;' - c = self.dbConn.cursor() - c.execute(q, (jid, )) - result = c.fetchone() - if result is None: - return False - return result[0] == 1 - - -def migrate(dbConn): - """ Creates the encryption_state table and migrates it if needed. - """ - if user_version(dbConn) == 0: - create_table = """ CREATE TABLE encryption_state ( - id INTEGER PRIMARY KEY AUTOINCREMENT, - jid TEXT UNIQUE, - encryption INTEGER, - timestamp NUMBER DEFAULT CURRENT_TIMESTAMP - ); - """ - - if table_exists(dbConn, 'encryption_state'): - # Given a database which already has `encryption_state` and has - # `user_version` 0, we assume the database needs migration and - # migrate it to add an `ID INTEGER AUTOINCREMENT` column - migrate_sql = """ - BEGIN TRANSACTION; - ALTER TABLE encryption_state RENAME TO encryption_state_back; - %s - INSERT INTO encryption_state(jid, encryption, timestamp) - SELECT jid, encryption, timestamp FROM encryption_state_back; - DROP TABLE encryption_state_back; - PRAGMA user_version=1; - END TRANSACTION ; - """ % (create_table) - dbConn.executescript(migrate_sql) - else: - # The database has `user_version` 0 and has no `encryption_state - # table, so crate it! - dbConn.executescript(""" BEGIN TRANSACTION; - %s - PRAGMA user_version=1; - END TRANSACTION; - """ % (create_table)) - - return dbConn diff -r 1dcdf1104bac -r c66c6d9161d2 omemo/lib/python-omemo/src/omemo/liteaxolotlstore.py --- a/omemo/lib/python-omemo/src/omemo/liteaxolotlstore.py Mon Apr 25 05:51:43 2016 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,130 +0,0 @@ -# -*- coding: utf-8 -*- -# -# Copyright 2015 Tarek Galal tare2.galal@gmail.com -# -# This file is part of Gajim-OMEMO plugin. -# -# The Gajim-OMEMO plugin is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by the Free -# Software Foundation, either version 3 of the License, or (at your option) any -# later version. -#