Giter VIP home page Giter VIP logo

dmaixner / esp8266-chachapoly Goto Github PK

View Code? Open in Web Editor NEW
6.0 2.0 1.0 10 KB

Simple encapsulation library for [Crypto](https://github.com/rweather/arduinolibs/tree/master/libraries/Crypto) library, used for encrypting and decrypting with ChaCha20 and Poly1305 algorithms.

License: Apache License 2.0

C++ 100.00%
chacha20-poly1305 chacha20 poly1305 cipher decipher encryption encrypt decryption decrypt security

esp8266-chachapoly's Introduction

ESP8266 ChaCha20 and Poly1305 helper library

Simple encapsulation library for Crypto library, used for encrypting and decrypting with ChaCha20 and Poly1305 algorithms.

ChaCha20-Poly1305 is an AEAD, Authenticated Encryption with Additional Data cipher. It uses four components:

  • A secret key.
  • A unique initialization value - aka the IV. It must be unique between encrypt invocations.
  • Non-secret, additional data. This data will not be encrypted, but will be authenticated - this is the AD in AEAD and tag is generated from this and IV with usage of key.
  • The message to be encrypted.

It works in two parts, first is encryption with ChaCha20 and second is generating hash with Poly1305. Final output is ciphertext with MAC (Message Authentication Code).

More details.

Provided methods

    void encrypt(const byte key[CHA_CHA_POLY_KEY_SIZE],              // input: secret key
                 const byte iv[CHA_CHA_POLY_IV_SIZE],                // input: IV
                 const byte auth[CHA_CHA_POLY_AUTH_SIZE],            // input: authentication message
                 const byte plainText[CHA_CHA_POLY_MESSAGE_SIZE],    // input: message to be encrypted
                 byte cipherText[CHA_CHA_POLY_MESSAGE_SIZE],         // output: encrypted message
                 byte tag[CHA_CHA_POLY_TAG_SIZE]);                   // output: authentication tag

    bool decrypt(const byte key[CHA_CHA_POLY_KEY_SIZE],              // input: secret key
                 const byte iv[CHA_CHA_POLY_IV_SIZE],                // input: IV
                 const byte auth[CHA_CHA_POLY_AUTH_SIZE],            // input: authentication message
                 const byte cipherText[CHA_CHA_POLY_MESSAGE_SIZE],   // input: encrypted message
                 byte plainText[CHA_CHA_POLY_MESSAGE_SIZE],          // output: decrypted message
                 const byte tag[CHA_CHA_POLY_TAG_SIZE]);             // input: authentication tag

    // generates random IV
    void generateIv(byte iv[CHA_CHA_POLY_IV_SIZE]);

Size of elements

#define CHA_CHA_POLY_KEY_SIZE 32
#define CHA_CHA_POLY_IV_SIZE 12
#define CHA_CHA_POLY_AUTH_SIZE 16
#define CHA_CHA_POLY_TAG_SIZE 16
#define CHA_CHA_POLY_MESSAGE_SIZE 60

Usage

    byte key[CHA_CHA_POLY_KEY_SIZE] = {
        0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
        0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18,
        0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28,
        0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38};
    byte auth[CHA_CHA_POLY_AUTH_SIZE] = {
        0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
        0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18};
    byte iv[CHA_CHA_POLY_IV_SIZE];
    ChaChaPolyCipher.generateIv(iv);

    // construct plain text message
    byte plainText[CHA_CHA_POLY_MESSAGE_SIZE];
    String plain = "{\"my secret message\"}";
    plain.getBytes(plainText, CHA_CHA_POLY_MESSAGE_SIZE);

    // encrypt plain text message from plainText to cipherText
    byte cipherText[CHA_CHA_POLY_MESSAGE_SIZE];
    byte tag[CHA_CHA_POLY_TAG_SIZE];
    ChaChaPolyCipher.encrypt(key, iv, auth, plainText, cipherText, tag);

    // decrypt message from cipherText to plainText
    // output is valid only if result is true
    bool result = ChaChaPolyCipher.decrypt(key, iv, auth, cipherText, plainText, tag);

esp8266-chachapoly's People

Contributors

dmaixner avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

nikito7

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.