Code Monkey home page Code Monkey logo

rfc7539's Introduction

RFC7539

PyPI

Travis

About

RFC7539 is an IETF specification for an authenticated encryption algorithm that will be incorporated into TLSv1.3. It is comprised of a stream cipher (ChaCha20) and a MAC (Poly1305), both written by Daniel J. Bernstein. The C implementations for both of these primitives are taken from the NSS library (the reason being that openSSL has license incompatibilities and also requires the openSSL headers which is more overhead than we need to implement these fairly basic primitives). The NSS code has been slightly modified to account for the 96 bit nonce and 32 bit counter specified in the RFC.

Installation

Method 1

pip install rfc7539

Method 2

git clone https://github.com/AntonKueltz/rfc7539.git
cd rfc7539
python setup.py install

Basic API

aead.encrypt_and_tag

Takes a key, nonce, plaintext and additional data and returns a ciphertext and MAC.

def encrypt_and_tag(
    key: bytes,
    nonce: bytes,
    plaintext: bytes,
    aad: bytes
) -> (bytes, bytes)

aead.verify_and_decrypt

Takes a key, nonce, ciphertext, MAC and additional data and returns a plaintext.

def verify_and_decrypt(
    key: bytes,
    nonce: bytes,
    ciphertext: bytes,
    mac: bytes, 
    aad: bytes
) -> bytes

Example Usage

You should use the authenticated encryption mode unless you really need to use one of the primitives by itself:

from rfc7539 import aead
from os import urandom

key = urandom(32)  # key is 32 bytes
nonce = b'thisisanonce'  # nonce is 12 bytes (DO NOT REUSE A NONCE WITH THE SAME KEY)
message = b'Some message to be encrypted'
additional_data = b'Some additional data'  # this will not be encrypted but will be verified for integrity

# encryption
ciphertext, mac = aead.encrypt_and_tag(key, nonce, message, additional_data)

# decryption (which yields plaintext == message)
plaintext = aead.verify_and_decrypt(key, nonce, ciphertext, mac, additional_data)

Note that all operations in this package work on bytes. You'll need to call e.g. encode() on strings before passing them as arguments.

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.