/**
 *--------------------------------------------------------------------\n
 *          HSLU T&A Hochschule Luzern Technik+Architektur            \n
 *--------------------------------------------------------------------\n
 *
 * \brief         ASYD assignment SW02
 * \file
 * \author        Basil Estermann, basil.estermann@stud.hslu.ch
 *                Dario Troxler, dario.troxler@stud.hslu.ch
 *                Simon Frei, simon.frei@stud.hslu.ch
 *                Jonas Arnold, jonas.arnold@stud.hslu.ch
 * \date          02.03.2023
 *
 *--------------------------------------------------------------------
 */

#include <stdint.h>
#include "XTEA-CBC.h"
#include "XTEA.h"

#define INITIALIZATION_VECTOR { 'COFE', 'SIMI' }

void encipher_CBC(unsigned int num_cycles, uint32_t v[2], uint32_t const k[4]) {
    static uint32_t v_prev[2] = INITIALIZATION_VECTOR;
    v[0] = v[0] ^ v_prev[0];
    v[1] = v[1] ^ v_prev[1];
    encipher(num_cycles, v, k);
    v_prev[0] = v[0];
    v_prev[1] = v[1];
}

void decipher_CBC(unsigned int num_cycles, uint32_t v[2], uint32_t const k[4]) {
    static uint32_t v_prev[2] = INITIALIZATION_VECTOR;
    uint32_t v_input[2] = { v[0], v[1] };
    decipher(num_cycles, v, k);
    v[0] = v[0] ^ v_prev[0];
    v[1] = v[1] ^ v_prev[1];
    v_prev[0] = v_input[0];
    v_prev[1] = v_input[1];
}
