Giter Club home page Giter Club logo

chi_io's Introduction

chi_io

Pure Python read/write encryption/decryption of encrypted Tombo chi files. If you are looking for an easy to use with safe and sane defaults for encryption do NOT use this (there a more modern and better best-practices available since 2004), this is intended to be compatible with Tombo, Kumagusu, MiniNoteViewer, etc. Tombo chi files are encrypted with blowfish and thus vulnerable to a 32-bit Birthday Attack.

https://github.com/clach04/chi_io

Extracted from https://hg.sr.ht/~clach04/pytombo

Library originally supported Python 2.1, 2.2, 2.4, 2.4, 2.5, 2.6, 2.7. Now only targets Python 2.7 and 3.x. Use older version shipped with PyTombo for Python < 2.7.

Can be used standalone, used by Puren Tonbo https://github.com/clach04/puren_tonbo/ which supports different encryption formats/ciphers.

Getting Started

Assuming a local checkout:

python -m pip install -r requirements.txt  # runs faster Py2 and Py3, slower alternative is `python -m pip install blowfish` Python 3 only

python test_chi.py

Examples

Command line tool chi_io

echo test | env CHI_PASSWORD=test ./chi_tool.py  -e -s  | env CHI_PASSWORD=test ./chi_tool.py -s -v

echo test | ./chi_tool.py -p test -e -s  | ./chi_tool.py -p test -s -v

mkdir scratch
echo my data | python chi_tool.py -p test -e -o scratch/mynote.chi
echo test > scratch/password
od -c scratch/password
./chi_tool.py scratch/mynote.chi -P scratch/password
chi_tool.py scratch/mynote.chi | vim -  # decrypt a note and pipe into vim

Python code

In memory

Using https://peps.python.org/pep-0272/ like API

Python 3.10.4 (tags/v3.10.4:9d38120, Mar 23 2022, 23:13:41) [MSC v.1929 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import chi_io  # https://github.com/clach04/chi_io Python access to Tombo encrypted files
>>> chi_io.implementation
'using PyCrypto 3.17'
>>> plain_text = b'12345678'
>>> mypassword = b'testing'
>>> cipher = chi_io.PEP272LikeCipher(chi_io.CHI_cipher(mypassword))  # OPTIONAL! encryption and decryption will be faster on subsequent calls if the same password
 is used
>>> crypted_data = cipher.encrypt(plain_text)
>>> result_data = cipher.decrypt(crypted_data)
>>> assert plain_text == result_data

Using filenames

Python 2.7.10 (default, May 23 2015, 09:40:32) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import chi_io  # https://github.com/clach04/chi_io Python access to Tombo encrypted files
>>> chi_io.implementation
'using PyCrypto'
>>> plain_text = b'12345678'
>>> enc_fname = 'chi_io_test1.chi'
>>> mypassword = b'testing'
>>> mypassword = chi_io.CHI_cipher(mypassword)  # OPTIONAL! encryption and decryption will be faster on subsequent calls if the same password is used
>>> chi_io.write_encrypted_file(enc_fname, mypassword, plain_text)
>>> read_plain_text = chi_io.read_encrypted_file(enc_fname, mypassword)
>>> assert plain_text == read_plain_text

python chi_io.py some_existing_file.chi  # will be prompted for password to decrypt existing file
env LANG=C.UTF-8 python chi_io.py some_existing_file.chi  # will be prompted for password to decrypt existing file

NOTE write_encrypted_file() and read_encrypted_file() can take either file names or file-like objects.

Tests

python test_chi.py
env NO_PYCRYPTO=true python test_chi.py  # force usage of Pure Python Blowfish (slower)

NOTES

  • PyCrypto will work fine but PyCryptodome is preferred.
    • The known vulnerability in PyCryptodome is not in the Blowfish implementation
  • Blowfish is not recommended by its author! Neither is ECB mode which Tombo uses (note Tombo does some additional bit fiddling but using Tombo CHI encryption for sensitive files is not recommended)
  • GNU General Public License v3.0 https://github.com/jashandeep-sohi/python-blowfish the pure Python 3.4+ blowfish implementation works great, but is slower than PyCryptodome

Also see

Compatible with:

TODO

  • Refactor chi_io code
  • Implement Tombo chi Cipher that follows PEP 272
  • Update Pure python Blowfish (wrapper or upstream) to support Cipher PEP 272 API for Block Encryption Algorithms v1.0 https://www.python.org/dev/peps/pep-0272/
  • Check for pycryptodomex first

chi_io's People

Contributors

clach04 avatar

Watchers

 avatar  avatar  avatar

chi_io's Issues

command line interface

encrypt

chi_tool -e -p password inputfile -o outputfile
chi_tool -e -p password - -o outputfile # read from stdin
chi_tool -e -p password -o outputfile # also read from stdin

decrypt

chi_tool -d -p password inputfile -o outputfile
chi_tool -p password inputfile -o outputfile
chi_tool inputfile -o outputfile  # prompt for password
chi_tool -p password inputfile -o -  # stdout
chi_tool -p password inputfile  # also stdout
chi_tool -P password_file inputfile -o outputfile

If OS environment variable CHI_PASSWORD set use that as password instead of prompting.

If password keychain available use that instead of prompt. - #6

review random generator

  • Py 3.6 >= https://docs.python.org/3/library/secrets.html#module-secrets - secrets.token_bytes()

  • https://www.pycryptodome.org/src/random/random#Crypto.Random.get_random_bytes

  • see other personal code that uses alternative system random (os.urandom)

    try:
        from secrets import choice, randbelow  # Python 3.6 module
    except ImportError:
        import random as _py_random
        random = _py_random  # backup(, backup) plan
    
    # Potentially overkill, use system random numbers
    # Nested exception catching for older Python version
    try:
        try:
            random = _py_random.SystemRandom()
        except NotImplementedError:
            # not available
            pass
    except AttributeError:
        # Pre Python 2.4
        pass
    
    def randbelow(x):
        return random.randint(0, x)
    choice = random.choice
    

For completeness, (Linux) should not be needed:

try:
    urandom = os.urandom
except AttributeError:
    if os.path.exists('/dev/urandom'):
        def urandom(n):
            # UNIX ONLY
            f = open('/dev/urandom')
            result = f.read(n)
            f.close()
            return result
    else:
        def urandom(n):
           s = []
           for i in xrange(n):
              r = random.randint(0,255)
              s.append(chr(r))
           return ''.join(s)

Windows only:

win32prng.getRandomBytes

refactor - reduce duplicated code for both encryption and decryption

For the different APIs, class versus file IO functions. See #12

  • use PEP272LikeCipher() in read enc file function, read_encrypted_file()
  • use PEP272LikeCipher() in write enc file function write_encrypted_file()

NOTE comments in code on behavior differences:

[PEP272LikeCipher.decrypt()]code is almost identical to the code currently in read_encrypted_file(), difference is ChiIO exceptions are should catch all issues - RunTime exception is not raised unlike read_encrypted_file() for some bad inputs

in decrypt, for padding read issues:

  • function raises RuntimeError()
  • class raises UnsupportedFile()

Both raise BadPassword() but file code attempts to report filename.

file code also complains on missing password.

TODO pip toml file pyproject.toml - to avoid --use-pep517

Future proof from:

(py311venv) C:\code\py\puren_tonbo>python -m pip install --upgrade git+https://github.com/clach04/chi_io.git
Collecting git+https://github.com/clach04/chi_io.git
  Cloning https://github.com/clach04/chi_io.git to c:\users\clach04\appdata\local\temp\pip-req-build-hh3fq4mi
  Running command git clone --filter=blob:none --quiet https://github.com/clach04/chi_io.git 'C:\Users\clach04\AppData\Local\Temp\pip-req-build-hh3fq4mi'
  Resolved https://github.com/clach04/chi_io.git to commit e680521b61b85d30b70bad19449fef5846cced15
  Preparing metadata (setup.py) ... done
Installing collected packages: chi-io
  DEPRECATION: chi-io is being installed using the legacy 'setup.py install' method, because it does not have a 'pyproject.toml' and the 'wheel' package is not installed. pip 23.1 will enforce this behaviour change. A possible replacement is to enable the '--use-pep517' option. Discussion can be found at https://github.com/pypa/pip/issues/8559
  Running setup.py install for chi-io ... done
Successfully installed chi-io-1.0.0

Also see #8

refactor - performance

Use a profiler to find hotspots. There are inefficient pieces of code, for example

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.