NetSim Source Code Help
Loading...
Searching...
No Matches
Encryption.c
Go to the documentation of this file.
1/************************************************************************************
2 * Copyright (C) 2020 *
3 * TETCOS, Bangalore. India *
4 * *
5 * Tetcos owns the intellectual property rights in the Product and its content. *
6 * The copying, redistribution, reselling or publication of any or all of the *
7 * Product or its content without express prior written consent of Tetcos is *
8 * prohibited. Ownership and / or any other right relating to the software and all *
9 * intellectual property rights therein shall remain at all times with Tetcos. *
10 * *
11 * Author: Shashi Kant Suman *
12 * *
13 * ---------------------------------------------------------------------------------*/
14
15#include <stdint.h>
16//#define _ENCRYPT
17#define _XOR_ENCRYPT
18
19char xor_encrypt(char ch,long key)
20{
21 return (char)(ch^key);
22}
23
24/* encrypt
25 * Encrypt 64 bits with a 128 bit key using TEA
26 * From http://en.wikipedia.org/wiki/Tiny_Encryption_Algorithm
27 * Arguments:
28 * v - array of two 32 bit uints to be encoded in place
29 * k - array of four 32 bit uints to act as key
30 * Returns:
31 * v - encrypted result
32 * Side effects:
33 * None
34 */
35void encrypt (uint32_t* v, uint32_t* k) {
36 uint32_t v0=v[0], v1=v[1], sum=0, i; /* set up */
37 uint32_t delta=0x9e3779b9; /* a key schedule constant */
38 uint32_t k0=k[0], k1=k[1], k2=k[2], k3=k[3]; /* cache key */
39 for (i=0; i < 32; i++) { /* basic cycle start */
40 sum += delta;
41 v0 += ((v1<<4) + k0) ^ (v1 + sum) ^ ((v1>>5) + k1);
42 v1 += ((v0<<4) + k2) ^ (v0 + sum) ^ ((v0>>5) + k3);
43 } /* end cycle */
44 v[0]=v0; v[1]=v1;
45}
46
47void encryptBlock(uint8_t * data, uint32_t * len, uint32_t * key)
48{
49 uint32_t blocks, i;
50 uint32_t * data32;
51
52 // treat the data as 32 bit unsigned integers
53 data32 = (uint32_t *) data;
54
55 // Find the number of 8 byte blocks, add one for the length
56 blocks = (((*len) + 7) / 8) + 1;
57
58 // Set the last block to the original data length
59 data32[(blocks*2) - 1] = *len;
60
61 // Set the encrypted data length
62 *len = blocks * 8;
63
64 for(i = 0; i< blocks; i++)
65 {
66 encrypt(&data32[i*2], key);
67 }
68}
char xor_encrypt(char ch, long key)
Definition: Encryption.c:19
void encrypt(uint32_t *v, uint32_t *k)
Definition: Encryption.c:35
void encryptBlock(uint8_t *data, uint32_t *len, uint32_t *key)
Definition: Encryption.c:47