Naruto / simon-speck-c

example C language implementation of SIMON and SPECK lightweight block ciphers.
MIT License
23 stars 3 forks source link
cipher ciphers crypto crypto-library cryptography cryptography-library speck

Build Status Windows build status Coverity Scan Build Status FOSSA Status

simon-speck-c

simon and speck are lightweight block cipher algorithms, published by NSA.(iadgov/simon-speck)

this is one reference implementation example by C language.

support platforms are linux, iOS, Android ndk, macOS and Windows.

Supports

Samples

#include <speck/speck.h>
#include <stdio.h>
#include <string.h>
#include <cstdlib>
#include <random>

int main() {
    uint8_t key[16]; // when use 128/192, key size is 24. when use 128/256, key size is 32.
    uint8_t original_iv[16];
    uint8_t plain_text[16];
    uint8_t crypted_text[16];
    uint8_t decrypted_text[16];

    // generate key and iv
    std::random_device rd;
    std::mt19937 gen(rd());
    std::uniform_int_distribution<> dis;

    for(int i=0; i<sizeof(key); i++) {
        key[i] = static_cast<uint8_t>(dis(gen));
    }
    for(int i=0; i<sizeof(original_iv); i++) {
        original_iv[i] = static_cast<uint8_t>(dis(gen));
    }
    snprintf(reinterpret_cast<char *>(plain_text), sizeof(plain_text), "hello world!!!!");

    speck_ctx_t *ctx = speck_init(SPECK_ENCRYPT_TYPE_128_128, key, sizeof(key));
    // ECB test
    speck_ecb_encrypt(ctx, plain_text, crypted_text, sizeof(plain_text));
    speck_ecb_decrypt(ctx, crypted_text, decrypted_text, sizeof(crypted_text));
    printf("speck 128/128 ecb\n");
    printf("  plain:     %s\n", plain_text);
    printf("  decrypted: %s\n", decrypted_text);

    // CTR test
    uint8_t iv[16];
    memcpy(iv, original_iv, sizeof(iv));
    speck_ctr_encrypt(ctx, plain_text, crypted_text, sizeof(plain_text), iv, sizeof(iv));
    memcpy(iv, original_iv, sizeof(iv));
    speck_ctr_decrypt(ctx, crypted_text, decrypted_text, sizeof(crypted_text), iv, sizeof(iv));
    printf("speck 128/128 ctr\n");
    printf("  plain:     %s\n", plain_text);
    printf("  decrypted: %s\n", decrypted_text);
}

bindings

development

Requirements

common

platforms

build

develop build

on macOS or Linux.

rm -rf build
mkdir build
cd build
cmake -DENABLE_TESTING=ON -DCMAKE_BUILD_TYPE=Debug ..
cmake --build . --clean-first
ctest

release build

linux

./scripts/speck/build_linux.sh

shared library is outputted to libs/linux directory.

iOS

./scripts/speck/build_ios.sh

fat library(simulator, device) is outputted to libs/ios directory.

android

./scripts/speck/build_android.sh

shared librares of each architectures are outputted to libs/android.

macOS

./scripts/speck/build_mac.sh

bundle file is outputted to libs/mac directory.

windows

scripts\speck\build_win.bat

dll library is outputted to libs/windows directory.

benchmark

example enable avx2 benchmark on macOS or Linux.

rm -rf build
mkdir build
pushd build
cmake -DENABLE_TESTING=ON -DENABLE_BENCHMARK=ON -DENABLE_AVX2=ON -DCMAKE_BUILD_TYPE=Release ..
cmake --build . --clean-first
ctest .
./test/speck/speck128128/speck128128benchmark
./test/speck/speck128192/speck128192benchmark
./test/speck/speck128256/speck128256benchmark

benchmark program usage

./test/speck/speck128128/speck128128benchmark [test byte length] [test count]

# e.g.: test encrypt 8096 byte data by speck and speck ctr 128/128 at 50 times.
./test/speck/speck128128/speck128128benchmark 8096 50

License

FOSSA Status