DES and 3DES Encryption Library for Arduino and Raspberry Pi
Spaniakos - DES and 3DES Encryption Library for Arduino and Raspberry Pi
DES.h
1 /*
2  DES.h - Library for DES and 3DES encryption and decryption
3  Ported to the Arduino platform 2013 by Tim Riemann
4 
5  Original version taken from the AVR-Crypto-Lib
6  (http://www.das-labor.org/wiki/AVR-Crypto-Lib)
7  Copyright (C) 2006-2010 Daniel Otte (daniel.otte@rub.de)
8 
9  This program is free software: you can redistribute it and/or modify
10  it under the terms of the GNU General Public License as published by
11  the Free Software Foundation, either version 3 of the License, or
12  (at your option) any later version.
13 
14  This program is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  GNU General Public License for more details.
18 
19  You should have received a copy of the GNU General Public License
20  along with this program. If not, see <http://www.gnu.org/licenses/>.
21 */
28 #ifndef DES_h
29 #define DES_h
30 
31 #include "DES_config.h"
32 
33 /* the FIPS 46-3 (1999-10-25) name for triple DES is triple data encryption algorithm so TDEA.
34  * Also we only implement the three key mode */
35 
36 #define tdea_enc tdes_enc
37 #define tdea_dec tdes_dec
38 
39 class DES
40 {
41  public:
54  void encrypt(void* out, const void* in, const void* key);
55 
68  void decrypt(void* out, const void* in, const uint8_t* key);
69 
82  void tripleEncrypt(void* out, void* in, const void* key);
83 
96  void tripleDecrypt(void* out, void* in, const uint8_t* key);
97 
103  DES();
104 
113  void init(const void* m_key,unsigned long long int IVCl);
114 
122  void init(const void* m_key);
123 
131  void change_key(const void* m_key);
132 
140  void set_IV(unsigned long long int IVCl);
141 
148  void get_IV(byte *out);
149 
156  unsigned long long int get_IV_int();
157 
164  void iv_inc();
165 
173  byte* get_key();
174 
182  int get_size();
183 
189  void set_size(int sizel);
190 
199  void calc_size_n_pad(int p_size);
200 
211  void padPlaintext(void* in,byte* out);
212 
222  bool CheckPad(byte* in,int size);
223 
233  void tdesCbcEncipher(byte* in,byte* out);
234 
244  void tdesCbcDecipher(byte* in,byte* out);
245 
255  void printArray(byte output[],bool p_pad = true);
256 
264  void printArray(byte output[],int sizel);
265 
275  void do_3des_encrypt(byte *plain,int size_p,byte *cipher,const void *key, bool inc = false);
276 
286  void do_3des_decrypt(byte *cipher,int size_c,byte *plain,const void *key, unsigned long long int ivl);
287  #if defined(DES_LINUX)
288 
293  double millis();
294  #endif
295  private:
303  void permute(const uint8_t *ptable, const uint8_t *in, uint8_t *out);
304 
308  void changeendian32(uint32_t * a);
309 
315  inline void shiftkey(uint8_t *key);
316 
322  inline void shiftkey_inv(uint8_t *key);
323 
330  inline uint64_t splitin6bitwords(uint64_t a);
331 
338  inline uint8_t substitute(uint8_t a, uint8_t * sbp);
339 
348  uint32_t des_f(uint32_t r, uint8_t* kr);
349  byte key[24];
350  unsigned long long int IVC;
351  byte iv[8];
352  int pad;
353  int size;
354  #if defined(DES_LINUX)
355  timeval tv;
356  #endif
357 
358  byte arr_pad[7];
359 };
360 
361 #endif
362 
byte iv[8]
Definition: DES.h:351
void changeendian32(uint32_t *a)
Definition: DES.cpp:252
int get_size()
getter method for size
Definition: DES.cpp:425
void calc_size_n_pad(int p_size)
calculates the size of the plaintext and the padding
Definition: DES.cpp:438
unsigned long long int IVC
Definition: DES.h:350
void shiftkey(uint8_t *key)
Definition: DES.cpp:261
Definition: DES.h:39
unsigned long long int get_IV_int()
Definition: DES.cpp:226
void tripleEncrypt(void *out, void *in, const void *key)
encrypt a block with Tripple-DES
Definition: DES.cpp:397
DES()
DES constructor.
Definition: DES.cpp:176
uint32_t des_f(uint32_t r, uint8_t *kr)
Definition: DES.cpp:298
void get_IV(byte *out)
Definition: DES.cpp:220
void set_size(int sizel)
Definition: DES.cpp:431
void shiftkey_inv(uint8_t *key)
Definition: DES.cpp:269
void do_3des_encrypt(byte *plain, int size_p, byte *cipher, const void *key, bool inc=false)
Definition: DES.cpp:568
void encrypt(void *out, const void *in, const void *key)
decrypt a block with DES
Definition: DES.cpp:326
int size
Definition: DES.h:353
void tdesCbcEncipher(byte *in, byte *out)
the main encrypt 3DES with IV function
Definition: DES.cpp:475
int pad
Definition: DES.h:352
byte arr_pad[7]
Definition: DES.h:358
uint64_t splitin6bitwords(uint64_t a)
Definition: DES.cpp:278
void printArray(byte output[], bool p_pad=true)
Definition: DES.cpp:540
void set_IV(unsigned long long int IVCl)
Definition: DES.cpp:213
bool CheckPad(byte *in, int size)
check the if the padding is correct
Definition: DES.cpp:460
void tripleDecrypt(void *out, void *in, const uint8_t *key)
decrypt a block with Tripple-DES
Definition: DES.cpp:405
void init(const void *m_key, unsigned long long int IVCl)
initiallize the key and IVC and IV array
Definition: DES.cpp:192
byte key[24]
Definition: DES.h:349
void permute(const uint8_t *ptable, const uint8_t *in, uint8_t *out)
Definition: DES.cpp:232
void padPlaintext(void *in, byte *out)
pads the plaintext
Definition: DES.cpp:450
void do_3des_decrypt(byte *cipher, int size_c, byte *plain, const void *key, unsigned long long int ivl)
Definition: DES.cpp:581
void iv_inc()
Definition: DES.cpp:413
uint8_t substitute(uint8_t a, uint8_t *sbp)
Definition: DES.cpp:288
double millis()
Definition: DES.cpp:591
void change_key(const void *m_key)
change the key for DEs and 3DES
Definition: DES.cpp:206
byte * get_key()
getter method for key
Definition: DES.cpp:419
timeval tv
Definition: DES.h:355
void decrypt(void *out, const void *in, const uint8_t *key)
encrypt a block with DES
Definition: DES.cpp:362
void tdesCbcDecipher(byte *in, byte *out)
the main decrypt 3DES with IV function
Definition: DES.cpp:508