Skip to content

Commit

Permalink
Pr/101 (#102)
Browse files Browse the repository at this point in the history
* Add lightweight crypto implementation including tests (No round reduction)

* Add round reduction to lightweight crypto implementation including tests for encrypt decrypt for round reduced functions
Added method of view returning its copy in vector

* Coding standard: change absolute paths, use #pragma once

* Added exceptions for missing lightweight tests
  • Loading branch information
Bender250 authored Mar 7, 2019
1 parent 7ed6eb5 commit 0c19784
Show file tree
Hide file tree
Showing 65 changed files with 9,967 additions and 1 deletion.
24 changes: 24 additions & 0 deletions streams/block/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,30 @@ add_library(block STATIC EXCLUDE_FROM_ALL
ciphers/shacal2/shacal2_factory
ciphers/xtea/xtea
ciphers/xtea/xtea_factory

ciphers/lightweight/lightweight.h
ciphers/lightweight/common/cipher.h
ciphers/lightweight/common/rotations/rot8.h
ciphers/lightweight/common/rotations/rot16.h
ciphers/lightweight/common/rotations/rot32.h

ciphers/lightweight/chaskey/chaskey
ciphers/lightweight/fantomas/fantomas
ciphers/lightweight/hight/hight
ciphers/lightweight/lblock/lblock
ciphers/lightweight/lea/lea
ciphers/lightweight/led/led
ciphers/lightweight/piccolo/piccolo
ciphers/lightweight/pride/pride
ciphers/lightweight/pride/pride_functions.h
ciphers/lightweight/prince/prince
ciphers/lightweight/rc5-20/rc5_20
ciphers/lightweight/rectangle/rectangle
ciphers/lightweight/road_runner/road_runner
ciphers/lightweight/robin/robin
ciphers/lightweight/robin_star/robin_star
ciphers/lightweight/sparx/sparx
ciphers/lightweight/twine/twine
)

target_link_libraries(block eacirc-core)
19 changes: 19 additions & 0 deletions streams/block/block_factory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,25 @@ std::unique_ptr<block_cipher> make_block_cipher(const std::string &name,
if (name == "NOEKEON") return std::make_unique<noekeon_factory>(round);
if (name == "SHACAL2") return std::make_unique<shacal2_factory>(round);
if (name == "XTEA") return std::make_unique<xtea_factory>(round);
if (name == "CHASKEY") return std::make_unique<chaskey>(round);
if (name == "FANTOMAS") return std::make_unique<fantomas>(round);
if (name == "HIGHT") return std::make_unique<hight>(round);
if (name == "LBLOCK") return std::make_unique<lblock>(round);
if (name == "LEA") return std::make_unique<lea>(round);
if (name == "LED") return std::make_unique<led>(round);
if (name == "PICCOLO") return std::make_unique<piccolo>(round);
if (name == "PRIDE") return std::make_unique<pride>(round, encrypt);
if (name == "PRINCE") return std::make_unique<prince>(round);
if (name == "RC5-20") return std::make_unique<rc5_20>(round);
if (name == "RECTANGLE-K80") return std::make_unique<rectangle_k80>(round);
if (name == "RECTANGLE-K128") return std::make_unique<rectangle_k128>(round);
if (name == "ROAD-RUNNER-K80") return std::make_unique<road_runner_k80>(round, encrypt);
if (name == "ROAD-RUNNER-K128") return std::make_unique<road_runner_k128>(round, encrypt);
if (name == "ROBIN") return std::make_unique<robin>(round);
if (name == "ROBIN-STAR") return std::make_unique<robin_star>(round);
if (name == "SPARX-B64") return std::make_unique<sparx_b64>(round);
if (name == "SPARX-B128") return std::make_unique<sparx_b128>(round);
if (name == "TWINE") return std::make_unique<twine>(round);
// clang-format on

throw std::runtime_error("requested block cipher named \"" + name +
Expand Down
16 changes: 16 additions & 0 deletions streams/block/block_factory.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,22 @@
#include "ciphers/tea/tea.h"
#include "ciphers/twofish/twofish.h"
#include "ciphers/xtea/xtea_factory.h"
#include "ciphers/lightweight/chaskey/chaskey.h"
#include "ciphers/lightweight/fantomas/fantomas.h"
#include "ciphers/lightweight/hight/hight.h"
#include "ciphers/lightweight/lblock/lblock.h"
#include "ciphers/lightweight/lea/lea.h"
#include "ciphers/lightweight/led/led.h"
#include "ciphers/lightweight/piccolo/piccolo.h"
#include "ciphers/lightweight/pride/pride.h"
#include "ciphers/lightweight/prince/prince.h"
#include "ciphers/lightweight/rc5-20/rc5_20.h"
#include "ciphers/lightweight/rectangle/rectangle.h"
#include "ciphers/lightweight/road_runner/road_runner.h"
#include "ciphers/lightweight/robin/robin.h"
#include "ciphers/lightweight/robin_star/robin_star.h"
#include "ciphers/lightweight/sparx/sparx.h"
#include "ciphers/lightweight/twine/twine.h"

namespace block {
std::unique_ptr<block_cipher> make_block_cipher(const std::string &name,
Expand Down
90 changes: 90 additions & 0 deletions streams/block/ciphers/lightweight/chaskey/chaskey.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
//
// Created by mhajas on 7/6/18.
//

#include "chaskey.h"
#include <streams/block/ciphers/lightweight/common/cipher.h>

namespace block {

uint32_t chaskey::rol(uint32_t x, const uint8_t n) {
return (x << n) | (x >> (32 - n));
}

uint32_t chaskey::ror(uint32_t x, const uint8_t n) {
return (x >> n) | (x << (32 - n));
}

void chaskey::Encrypt(uint8_t *block) {
uint32_t *v = (uint32_t *) block;
uint32_t *k = (uint32_t *) _key;
uint8_t i;

/* Whitening */
v[0] ^= READ_ROUND_KEY_DOUBLE_WORD(k[0]);
v[1] ^= READ_ROUND_KEY_DOUBLE_WORD(k[1]);
v[2] ^= READ_ROUND_KEY_DOUBLE_WORD(k[2]);
v[3] ^= READ_ROUND_KEY_DOUBLE_WORD(k[3]);

/* Chaskey permutation*/
for (i = 0; i < _rounds; ++i) {
v[0] += v[1];
v[1] = rol(v[1], 5);
v[1] ^= v[0];
v[0] = rol(v[0], 16);
v[2] += v[3];
v[3] = rol(v[3], 8);
v[3] ^= v[2];
v[0] += v[3];
v[3] = rol(v[3], 13);
v[3] ^= v[0];
v[2] += v[1];
v[1] = rol(v[1], 7);
v[1] ^= v[2];
v[2] = rol(v[2], 16);
}

/* Whitening */
v[0] ^= READ_ROUND_KEY_DOUBLE_WORD(k[0]);
v[1] ^= READ_ROUND_KEY_DOUBLE_WORD(k[1]);
v[2] ^= READ_ROUND_KEY_DOUBLE_WORD(k[2]);
v[3] ^= READ_ROUND_KEY_DOUBLE_WORD(k[3]);
}

void chaskey::Decrypt(uint8_t *block) {
uint32_t *v = (uint32_t *) block;
uint32_t *k = (uint32_t *) _key;
uint8_t i;

/* Whitening */
v[0] ^= READ_ROUND_KEY_DOUBLE_WORD(k[0]);
v[1] ^= READ_ROUND_KEY_DOUBLE_WORD(k[1]);
v[2] ^= READ_ROUND_KEY_DOUBLE_WORD(k[2]);
v[3] ^= READ_ROUND_KEY_DOUBLE_WORD(k[3]);

/* Chaskey permutation */
for (i = 0; i < _rounds; ++i) {
v[2] = ror(v[2], 16);
v[1] ^= v[2];
v[1] = ror(v[1], 7);
v[2] -= v[1];
v[3] ^= v[0];
v[3] = ror(v[3], 13);
v[0] -= v[3];
v[3] ^= v[2];
v[3] = ror(v[3], 8);
v[2] -= v[3];
v[0] = ror(v[0], 16);
v[1] ^= v[0];
v[1] = ror(v[1], 5);
v[0] -= v[1];
}

/* Whitening */
v[0] ^= READ_ROUND_KEY_DOUBLE_WORD(k[0]);
v[1] ^= READ_ROUND_KEY_DOUBLE_WORD(k[1]);
v[2] ^= READ_ROUND_KEY_DOUBLE_WORD(k[2]);
v[3] ^= READ_ROUND_KEY_DOUBLE_WORD(k[3]);
}

}
32 changes: 32 additions & 0 deletions streams/block/ciphers/lightweight/chaskey/chaskey.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//
// Created by mhajas on 7/6/18.
//

#pragma once

#include <streams/block/ciphers/lightweight/lightweight.h>
#include "../../../block_cipher.h"


#define CHASKEY_BLOCK_SIZE 16
#define CHASKEY_KEY_SIZE 16
#define CHASKEY_ROUND_KEYS_SIZE 16
#define CHASKEY_NUMBER_OF_ROUNDS 16

namespace block {

class chaskey : public lightweight<CHASKEY_KEY_SIZE, CHASKEY_BLOCK_SIZE> {

static inline uint32_t rol(uint32_t x, const uint8_t n);

static inline uint32_t ror(uint32_t x, const uint8_t n);
public:
chaskey(size_t rounds) : lightweight(rounds) {};

void Encrypt(uint8_t *block) override;

void Decrypt(uint8_t *block) override;
};

} //namespace block

122 changes: 122 additions & 0 deletions streams/block/ciphers/lightweight/common/cipher.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
/*
*
* University of Luxembourg
* Laboratory of Algorithmics, Cryptology and Security (LACS)
*
* FELICS - Fair Evaluation of Lightweight Cryptographic Systems
*
* Copyright (C) 2015 University of Luxembourg
*
* Written in 2015 by Daniel Dinu <dumitru-daniel.dinu@uni.lu> and
* Yann Le Corre <yann.lecorre@uni.lu>
*
* This file is part of FELICS.
*
* FELICS is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* FELICS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <http://www.gnu.org/licenses/>.
*
*/

#pragma once

/*
*
* Optimization levels
* ... OPTIMIZATION_LEVEL_0 - O0
* ... OPTIMIZATION_LEVEL_1 - O1
* ... OPTIMIZATION_LEVEL_2 - O2
* ... OPTIMIZATION_LEVEL_3 - O3 = defualt
*
*/
#define OPTIMIZATION_LEVEL_0 __attribute__((optimize("O0")))
#define OPTIMIZATION_LEVEL_1 __attribute__((optimize("O1")))
#define OPTIMIZATION_LEVEL_2 __attribute__((optimize("O2")))
#define OPTIMIZATION_LEVEL_3 __attribute__((optimize("O3")))


/*
*
* SCENARIO values:
* ... SCENARIO_0 0 - cipher operation: encrypt & decrypt one data block
* ... SCENARIO_1 1 - scenario 1: encrypt & decrypt data in CBC mode
* ... SCENARIO_2 2 - scenario 2: encrypt & decrypt data in CTR mode
*
*/
#define SCENARIO_0 0
#define SCENARIO_1 1
#define SCENARIO_2 2

#ifndef SCENARIO
#define SCENARIO SCENARIO_0
#endif


/*
*
* MEASURE_CYCLE_COUNT values:
* ... MEASURE_CYCLE_COUNT_DISABLED 0 - measure cycle count is disabled
* ... MEASURE_CYCLE_COUNT_ENABLED 1 - measure cycle count is enabled
*
*/
#define MEASURE_CYCLE_COUNT_DISABLED 0
#define MEASURE_CYCLE_COUNT_ENABLED 1

#ifndef MEASURE_CYCLE_COUNT
#define MEASURE_CYCLE_COUNT MEASURE_CYCLE_COUNT_DISABLED
#endif


/*
*
* Align memory boundaries in bytes
*
*/
#define ALIGN_PC_BOUNDRY 64
#define ALIGN_AVR_BOUNDRY 2
#define ALIGN_MSP_BOUNDRY 2
#define ALIGN_ARM_BOUNDRY 8

#define ALIGNED __attribute__ ((aligned(ALIGN_PC_BOUNDRY)))

/*
*
* RAM data types
*
*/
#define RAM_DATA_BYTE uint8_t ALIGNED
#define RAM_DATA_WORD uint16_t ALIGNED
#define RAM_DATA_DOUBLE_WORD uint32_t ALIGNED

#define READ_RAM_DATA_BYTE(x) x
#define READ_RAM_DATA_WORD(x) x
#define READ_RAM_DATA_DOUBLE_WORD(x) x


/*
*
* Flash/ROM data types
*
*/

#define ROM_DATA_BYTE const uint8_t ALIGNED
#define ROM_DATA_WORD const uint16_t ALIGNED
#define ROM_DATA_DOUBLE_WORD const uint32_t ALIGNED

#define READ_ROM_DATA_BYTE(x) x
#define READ_ROM_DATA_WORD(x) x
#define READ_ROM_DATA_DOUBLE_WORD(x) x

#define READ_ROUND_KEY_BYTE(x) READ_RAM_DATA_BYTE(x)
#define READ_ROUND_KEY_WORD(x) READ_RAM_DATA_WORD(x)
#define READ_ROUND_KEY_DOUBLE_WORD(x) READ_RAM_DATA_DOUBLE_WORD(x)

Loading

0 comments on commit 0c19784

Please sign in to comment.