Code Monkey home page Code Monkey logo

auto-cryptanalysis's Introduction

Auto Cryptanalysis

This project implements python module for automatic cryptanalysis of Substitution Permutation Network ciphers by performing extensive linear and differential characteristic search and finding keybits

Structure

The project is structured as follows:

  • The cryptanalysis directory contains the main Python module for cryptanalysis.
  • The tests directory contains unit tests for the module
  • The examples directory contains examples for using the module
  • The docs directory contains html documentation autogenerated from code doc-strings

Installation

Pip

The project can be installed directly from pip

pip install cryptanalysis

Otherwise clone and install is also viable

git clone https://github.com/deut-erium/auto-cryptanalysis.git
cd auto-cryptanalysis
pip install .

Requirements

This project requires Python3.6+ and the following Python packages:

  • z3-solver
  • tqdm

Requirements are auto installed as a part of the installation process but

You can also install these packages using pip:

pip install -r requirements.txt

Usage

import random
import cryptanalysis

sbox_size = 6 # bits
pbox_size = sbox_size * 16 # 16 sboxes
num_rounds = 4
sbox = list(range(2**sbox_size))
pbox = list(range(pbox_size))
# random pbox and sbox
random.shuffle(sbox)
random.shuffle(pbox)

random_key = random.randint(0, (2**pbox_size) - 1)
# random spn instance whose key is unknown to us
spn = cryptanalysis.SPN(sbox, pbox, random_key, num_rounds)

d_c = cryptanalysis.differential_cryptanalysis.DifferentialCryptanalysis(sbox, pbox, num_rounds+1)
# override batch_encrypt with the oracle

max_num_encryptions = 50000
def batch_encrypt(plaintexts):
    return [spn.encrypt(i) for i in plaintexts]

d_c.batch_encrypt = batch_encrypt
differential_characteristics = d_c.characteristic_searcher.search_exclusive_masks()
last_round_key_blocks = d_c.find_last_roundkey(differential_characteristics, max_num_encryptions//16)

print("recovered last round key:",last_round_key_blocks)
print("original last round key:",d_c.int_to_list(spn.round_keys[-1]))

Tests

You can run the tests using the following command:

python -m unittest discover

Documentation

Read the documentation
Autogenerated documentation from code doc-strings can be found under docs

Contributing

Please feel free to submit pull requests or create issues if you find any bugs or have any suggestions for improvements.
List of ideas to implement/TODO is present in CONTRIBUTING.md

License

This project is licensed under the GPL License.

auto-cryptanalysis's People

Contributors

deut-erium avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

auto-cryptanalysis's Issues

prune_level

Hi! I have to develop an app similar to yours and I found a bug that could affect the usage of the tool. If you provide an unsat level of pruning the tool won't do anything. So when you try checking that if the provided level is sat you should first push the context and then add the bias constraint with prune_level. If it's sat, pop the context and then add again the constraint. It should look something like this:
`else:
self.solver.push()

            self.add_ddt_constraints(prune_level)
            if self.solver.check() == sat:
                self.solver.pop()

                self.prune_level = prune_level
                self.add_ddt_constraints(prune_level)
            else:
                print(
                    "The provided pruning lever results in an unsat system! Searching for optimal pruning level..."
                )
                print()
                self.solver.pop()
                self.init_differential_characteristic_solver(-1)`

I changed the names so that this pruning integrates in the code I developed.

Wrong Results or Incorrect Usage

Hi! I have to implement a tool similar to yours for my bachelor degree, and before starting I wanted to try this one with the paper I want to analyze. Your tool gives me wrong results for the differential characteristic of the cipher discussed in "A Tutorial on Linear and Differential Cryptanalysis" by Howard M. Heys. Maybe I am using it wrongly, but something is definitely not working. Thanks!

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.