#include "api.h"
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include "elephant_200.h"
#include <DHT.h>
#define DHTPIN 2 // DHT PIN 2
#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
DHT dht(DHTPIN, DHTTYPE);
//codingan keccak.c
/**
* Based on the implementation by the Keccak Team, namely, Guido Bertoni, Joan Daemen,
* Michaël Peeters, Gilles Van Assche and Ronny Van Keer.
*/
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include "elephant_200.h"
#define maxNrRounds 18
#define nrLanes 25
#define index(x, y) (((x)%5)+5*((y)%5))
const BYTE KeccakRoundConstants[maxNrRounds] = {
0x01, 0x82, 0x8a, 0x00, 0x8b, 0x01, 0x81, 0x09, 0x8a,
0x88, 0x09, 0x0a, 0x8b, 0x8b, 0x89, 0x03, 0x02, 0x80
};
const unsigned int KeccakRhoOffsets[nrLanes] = {
0, 1, 6, 4, 3, 4, 4, 6, 7, 4, 3, 2, 3, 1, 7, 1, 5, 7, 5, 0, 2, 2, 5, 0, 6
};
#define ROL8(a, offset) ((offset != 0) ? ((((BYTE)a) << offset) ^ (((BYTE)a) >> (sizeof(BYTE)*8-offset))) : a)
void theta(BYTE *A)
{
unsigned int x, y;
BYTE C[5], D[5];
for(x=0; x<5; x++) {
C[x] = 0;
for(y=0; y<5; y++)
C[x] ^= A[index(x, y)];
}
for(x=0; x<5; x++)
D[x] = ROL8(C[(x+1)%5], 1) ^ C[(x+4)%5];
for(x=0; x<5; x++)
for(y=0; y<5; y++)
A[index(x, y)] ^= D[x];
}
void rho(BYTE *A)
{
for(unsigned int x=0; x<5; x++)
for(unsigned int y=0; y<5; y++)
A[index(x, y)] = ROL8(A[index(x, y)], KeccakRhoOffsets[index(x, y)]);
}
void pi(BYTE *A)
{
BYTE tempA[25];
for(unsigned int x=0; x<5; x++)
for(unsigned int y=0; y<5; y++)
tempA[index(x, y)] = A[index(x, y)];
for(unsigned int x=0; x<5; x++)
for(unsigned int y=0; y<5; y++)
A[index(0*x+1*y, 2*x+3*y)] = tempA[index(x, y)];
}
void chi(BYTE *A)
{
unsigned int x, y;
BYTE C[5];
for(y=0; y<5; y++) {
for(x=0; x<5; x++)
C[x] = A[index(x, y)] ^ ((~A[index(x+1, y)]) & A[index(x+2, y)]);
for(x=0; x<5; x++)
A[index(x, y)] = C[x];
}
}
void iota(BYTE *A, unsigned int indexRound)
{
A[index(0, 0)] ^= KeccakRoundConstants[indexRound];
}
void KeccakP200Round(BYTE *state, unsigned int indexRound)
{
theta(state);
rho(state);
pi(state);
chi(state);
iota(state, indexRound);
}
void permutation(BYTE* state)
{
for(unsigned int i=0; i<maxNrRounds; i++)
KeccakP200Round(state, i);
}
// lanjutan codingan encrypt.c
BYTE rotl(BYTE b)
{
return (b << 1) | (b >> 7);
}
int constcmp(const BYTE* a, const BYTE* b, SIZE length)
{
BYTE r = 0;
for (SIZE i = 0; i < length; ++i)
r |= a[i] ^ b[i];
return r;
}
// State should be BLOCK_SIZE bytes long
// Note: input may be equal to output
void lfsr_step(BYTE* output, BYTE* input)
{
BYTE temp = rotl(input[0]) ^ rotl(input[2]) ^ (input[13] << 1);
for(SIZE i = 0; i < BLOCK_SIZE - 1; ++i)
output[i] = input[i + 1];
output[BLOCK_SIZE - 1] = temp;
}
void xor_block(BYTE* state, const BYTE* block, SIZE size)
{
for(SIZE i = 0; i < size; ++i)
state[i] ^= block[i];
}
// Write the ith assocated data block to "output".
// The nonce is prepended and padding is added as required.
// adlen is the length of the associated data in bytes
void get_ad_block(BYTE* output, const BYTE* ad, SIZE adlen, const BYTE* npub, SIZE i)
{
SIZE len = 0;
// First block contains nonce
// Remark: nonce may not be longer then BLOCK_SIZE
if(i == 0) {
memcpy(output, npub, CRYPTO_NPUBBYTES);
len += CRYPTO_NPUBBYTES;
}
const SIZE block_offset = i * BLOCK_SIZE - (i != 0) * CRYPTO_NPUBBYTES;
// If adlen is divisible by BLOCK_SIZE, add an additional padding block
if(i != 0 && block_offset == adlen) {
memset(output, 0x00, BLOCK_SIZE);
output[0] = 0x01;
return;
}
const SIZE r_outlen = BLOCK_SIZE - len;
const SIZE r_adlen = adlen - block_offset;
// Fill with associated data if available
if(r_outlen <= r_adlen) { // enough AD
memcpy(output + len, ad + block_offset, r_outlen);
} else { // not enough AD, need to pad
if(r_adlen > 0) // ad might be nullptr
memcpy(output + len, ad + block_offset, r_adlen);
memset(output + len + r_adlen, 0x00, r_outlen - r_adlen);
output[len + r_adlen] = 0x01;
}
}
// Return the ith assocated data block.
// clen is the length of the ciphertext in bytes
void get_c_block(BYTE* output, const BYTE* c, SIZE clen, SIZE i)
{
const SIZE block_offset = i * BLOCK_SIZE;
// If clen is divisible by BLOCK_SIZE, add an additional padding block
if(block_offset == clen) {
memset(output, 0x00, BLOCK_SIZE);
output[0] = 0x01;
return;
}
const SIZE r_clen = clen - block_offset;
// Fill with ciphertext if available
if(BLOCK_SIZE <= r_clen) { // enough ciphertext
memcpy(output, c + block_offset, BLOCK_SIZE);
} else { // not enough ciphertext, need to pad
if(r_clen > 0) // c might be nullptr
memcpy(output, c + block_offset, r_clen);
memset(output + r_clen, 0x00, BLOCK_SIZE - r_clen);
output[r_clen] = 0x01;
}
}
// It is assumed that c is sufficiently long
// Also, tag and c should not overlap
void crypto_aead_impl(
BYTE* c, BYTE* tag, const BYTE* m, SIZE mlen, const BYTE* ad, SIZE adlen,
const BYTE* npub, const BYTE* k, int encrypt)
{
// Compute number of blocks
const SIZE nblocks_c = 1 + mlen / BLOCK_SIZE;
const SIZE nblocks_m = (mlen % BLOCK_SIZE) ? nblocks_c : nblocks_c - 1;
const SIZE nblocks_ad = 1 + (CRYPTO_NPUBBYTES + adlen) / BLOCK_SIZE;
const SIZE nb_it = (nblocks_c + 1 > nblocks_ad - 1) ? nblocks_c + 1 : nblocks_ad - 1;
// Storage for the expanded key L
BYTE expanded_key[BLOCK_SIZE] = {0};
memcpy(expanded_key, k, CRYPTO_KEYBYTES);
permutation(expanded_key);
// Buffers for storing previous, current and next mask
BYTE mask_buffer_1[BLOCK_SIZE] = {0};
BYTE mask_buffer_2[BLOCK_SIZE] = {0};
BYTE mask_buffer_3[BLOCK_SIZE] = {0};
memcpy(mask_buffer_2, expanded_key, BLOCK_SIZE);
BYTE* previous_mask = mask_buffer_1;
BYTE* current_mask = mask_buffer_2;
BYTE* next_mask = mask_buffer_3;
// Buffer to store current ciphertext/AD block
BYTE buffer[BLOCK_SIZE];
// Tag buffer and initialization of tag to zero
BYTE tag_buffer[BLOCK_SIZE] = {0};
get_ad_block(tag_buffer, ad, adlen, npub, 0);
SIZE offset = 0;
for(SIZE i = 0; i < nb_it; ++i) {
// Compute mask for the next message
lfsr_step(next_mask, current_mask);
if(i < nblocks_m) {
// Compute ciphertext block
memcpy(buffer, npub, CRYPTO_NPUBBYTES);
memset(buffer + CRYPTO_NPUBBYTES, 0, BLOCK_SIZE - CRYPTO_NPUBBYTES);
xor_block(buffer, current_mask, BLOCK_SIZE);
xor_block(buffer, next_mask, BLOCK_SIZE);
permutation(buffer);
xor_block(buffer, current_mask, BLOCK_SIZE);
xor_block(buffer, next_mask, BLOCK_SIZE);
const SIZE r_size = (i == nblocks_m - 1) ? mlen - offset : BLOCK_SIZE;
xor_block(buffer, m + offset, r_size);
memcpy(c + offset, buffer, r_size);
}
if(i > 0 && i <= nblocks_c) {
// Compute tag for ciphertext block
get_c_block(buffer, encrypt ? c : m, mlen, i - 1);
xor_block(buffer, previous_mask, BLOCK_SIZE);
xor_block(buffer, next_mask, BLOCK_SIZE);
permutation(buffer);
xor_block(buffer, previous_mask, BLOCK_SIZE);
xor_block(buffer, next_mask, BLOCK_SIZE);
xor_block(tag_buffer, buffer, BLOCK_SIZE);
}
// If there is any AD left, compute tag for AD block
if(i + 1 < nblocks_ad) {
get_ad_block(buffer, ad, adlen, npub, i + 1);
xor_block(buffer, next_mask, BLOCK_SIZE);
permutation(buffer);
xor_block(buffer, next_mask, BLOCK_SIZE);
xor_block(tag_buffer, buffer, BLOCK_SIZE);
}
// Cyclically shift the mask buffers
// Value of next_mask will be computed in the next iteration
BYTE* const temp = previous_mask;
previous_mask = current_mask;
current_mask = next_mask;
next_mask = temp;
offset += BLOCK_SIZE;
}
// Compute tag
xor_block(tag_buffer, expanded_key, BLOCK_SIZE);
permutation(tag_buffer);
xor_block(tag_buffer, expanded_key, BLOCK_SIZE);
memcpy(tag, tag_buffer, CRYPTO_ABYTES);
}
// Remark: c must be at least mlen + CRYPTO_ABYTES long
int crypto_aead_encrypt(
unsigned char *c, unsigned long long *clen,
const unsigned char *m, unsigned long long mlen,
const unsigned char *ad, unsigned long long adlen,
const unsigned char *nsec,
const unsigned char *npub,
const unsigned char *k)
{
(void)nsec;
*clen = mlen + CRYPTO_ABYTES;
BYTE tag[CRYPTO_ABYTES];
crypto_aead_impl(c, tag, m, mlen, ad, adlen, npub, k, 1);
memcpy(c + mlen, tag, CRYPTO_ABYTES);
return 0;
}
int crypto_aead_decrypt(
unsigned char *m, unsigned long long *mlen,
unsigned char *nsec,
const unsigned char *c, unsigned long long clen,
const unsigned char *ad, unsigned long long adlen,
const unsigned char *npub,
const unsigned char *k)
{
(void)nsec;
if(clen < CRYPTO_ABYTES)
return -1;
*mlen = clen - CRYPTO_ABYTES;
BYTE tag[CRYPTO_ABYTES];
crypto_aead_impl(m, tag, c, *mlen, ad, adlen, npub, k, 0);
return (constcmp(c + *mlen, tag, CRYPTO_ABYTES) == 0) ? 0 : -1;
}
//baru
//coding sendiri
void string2hexString(unsigned char* input, int clen, char* output)
{
int loop;
int i;
i=0;
loop=0;
for (i=0;i<clen;i+=2){
sprintf((char*)(output+i),"%02X", input[loop]);
loop+=1;
}
//insert NULL at the end of the output string
output[i++] = '\0';
}
void *hextobyte(char *hexstring, unsigned char* bytearray ) {
int i;
int str_len = strlen(hexstring);
for (i = 0; i < (str_len / 2); i++) {
sscanf(hexstring + 2*i, "%02x", &bytearray[i]);
}
}
unsigned long long clen;
unsigned long long mlen = 128; //output 160 character
unsigned char cipher[160];
unsigned char chex[160];
// String tuliskan; //variable untuk input
// const unsigned char key[32] = "12341234123412341234123412341234";
unsigned long time;
const unsigned char key[32] = {0x01,0x23,0x45,0x67,0x89,0xAB,0xCD,0xEF,0x01,0x23,0x45,0x67,0x89,0xAB,0xCD,0xEF};
const unsigned char nonce[24] = {0xAA,0xBB,0x00,0x00,0x00,0x00,0x01,0x11,0x11,0x11,0x11,0x11,0x01};
unsigned char plaintext[128];
unsigned long long plen;
unsigned long time1;
unsigned long time2;
unsigned long waktu;
struct enkripDataDHT22{
unsigned char h [16];
unsigned char t[16];
};
struct cipherDHT22{
unsigned char h[128+32];
unsigned char t[128+32];
unsigned long long clen_h;
unsigned long long clen_t;
};
enkripDataDHT22 dataDHT;
cipherDHT22 dataDHTenkripsi;
void setup() {
Serial.begin(9600);
Serial.println("DHT 22 Ready!");
dht.begin();
}
void loop() {
float h = dht.readHumidity(); // Ambil nilai Kelembaban
float t = dht.readTemperature(); // Ambil nilai Suhu
// char cr_suhu[13];
// char cr_kelembaban[13];
char cr_h[13];
char cr_t[13];
unsigned char ucr_h[13];
unsigned char ucr_t[13];
// double ucr_h[13];
// double ucr_t [13];
// sprintf(cr_h,2,2,ucr_h); //convert double to char
// sprintf(cr_t,2,2,ucr_t); //convert double to char
dtostrf(h,2,2,cr_h); //convert double to char
dtostrf(t,2,2,cr_t); //convert double to char
// Serial.println(cr_t);
// Serial.println(cr_h);
for(int i=0; i<sizeof(cr_h); i++){
dataDHT.h[i] = cr_h[i];
dataDHT.t[i] = cr_t[i];
}
// Serial.print("Time awal: ");
// time1 = micros();
// Serial.println(time1); //prints time since program started
//enkripsi humidity
crypto_aead_encrypt(dataDHTenkripsi.h, &clen, dataDHT.h, mlen, 0, 0, 0, nonce, key);
dataDHTenkripsi.clen_h = clen;
//dekrip humidity
crypto_aead_decrypt(plaintext, &plen, 0, dataDHTenkripsi.h, dataDHTenkripsi.clen_h, 0, 0, nonce, key);
//enkripsi temperature
crypto_aead_encrypt(dataDHTenkripsi.t, &clen, dataDHT.t, mlen, 0, 0, 0, nonce, key);
dataDHTenkripsi.clen_t = clen;
//dekrip temperature
crypto_aead_decrypt(plaintext, &plen, 0, dataDHTenkripsi.t, dataDHTenkripsi.clen_t, 0, 0, nonce, key);
//
Serial.print("Time awal: ");
time1 = micros();
Serial.println(time1); //prints time since program started
//
string2hexString(dataDHTenkripsi.h,clen, chex);
Serial.print("Cipher Humidity: ");
Serial.println((char*)chex);
Serial.println();
//
string2hexString(dataDHTenkripsi.h,dataDHTenkripsi.clen_h,chex);
Serial.print("Plaintext Humidity: ");
Serial.println((char*) plaintext);
Serial.println();
//
string2hexString(dataDHTenkripsi.t,clen, chex);
Serial.print("Cipher Temperature: ");
Serial.println((char*)chex);
Serial.println();
//
string2hexString(dataDHTenkripsi.t,dataDHTenkripsi.clen_t,chex);
Serial.print("Plaintext Temperature: ");
Serial.println((char*) plaintext);
Serial.println();
//untuk timer end
Serial.print("Time End:");
time2 = micros();
Serial.println(time2); //prints time since program started
//untuk process time encrypt
Serial.print ("Encryption time:");
waktu = (time2) - (time1) ;
// waktu = micros;
Serial.println(waktu);
}