Blame view

RIOT/sys/include/crypto/3des.h 4.96 KB
fb11e647   vrobic   reseau statique a...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
  /*
   * Copyright (C) 2013 Freie Universitรคt Berlin, Computer Systems & Telematics
   *
   * This file is subject to the terms and conditions of the GNU Lesser
   * General Public License v2.1. See the file LICENSE in the top level
   * directory for more details.
   */
  
  /**
   * @ingroup     sys_crypto
   * @{
   *
   * @file
   * @brief       Headers for the implementation of the 3DES cipher-algorithm
   *
   * @author      Freie Universitaet Berlin, Computer Systems & Telematics,
   * @author      Nicolai Schmittberger (nicolai.schmittberger@fu-berlin.de)
   * @author      Zakaria Kasmi <zkasmi@inf.fu-berlin.de>
   */
  
  #include <stdio.h>
  #include <stdarg.h>
  #include <string.h>
  #include <stdlib.h>
  #include <stdint.h>
  
  #include "crypto/ciphers.h"
  
  #ifndef THREEDES_H_
  #define THREEDES_H_
  
  #ifdef __cplusplus
  extern "C" {
  #endif
  
  #define THREEDES_BLOCK_SIZE    8
  #define THREEDES_MAX_KEY_SIZE 24
  
  #define ROLc(x, y) \
          ((((unsigned long) (x) << (unsigned long) ((y) & 31)) | \
           (((unsigned long) (x) & 0xFFFFFFFFUL) >>               \
             (unsigned long) (32 - ((y) & 31)))) & 0xFFFFFFFFUL)
  
  #define RORc(x, y) \
          (((((unsigned long) (x) & 0xFFFFFFFFUL) >> \
              (unsigned long) ((y) & 31)) | \
             ((unsigned long) (x) << (unsigned long) (32 - ((y) & 31)))) & \
                                    0xFFFFFFFFUL)
  
  //source: http://hostap.epitest.fi/wpa_supplicant/devel/common_8h.html
  #define WPA_GET_BE32(a) \
                          ((((uint32_t) (a)[0]) << 24) | \
                           (((uint32_t) (a)[1]) << 16) | \
                           (((uint32_t) (a)[2]) << 8 ) |  ((uint32_t) (a)[3]))
  
  //source: http://hostap.epitest.fi/wpa_supplicant/devel/common_8h.html
  #define WPA_PUT_BE32(a, val)                                           \
         do {                                                            \
               (a)[0] = (uint8_t) ((((uint32_t) (val)) >> 24) & 0xff);   \
               (a)[1] = (uint8_t) ((((uint32_t) (val)) >> 16) & 0xff);   \
               (a)[2] = (uint8_t) ((((uint32_t) (val)) >> 8) & 0xff);    \
               (a)[3] = (uint8_t) (((uint32_t)  (val)) & 0xff);          \
          } while (0)
  
  
  
  
  static const uint32_t bytebit[8] = {0200, 0100, 040, 020, 010, 04, 02, 01};
  
  static const uint32_t bigbyte[24] = {
      0x800000UL,  0x400000UL,  0x200000UL,  0x100000UL,
      0x80000UL,   0x40000UL,   0x20000UL,   0x10000UL,
      0x8000UL,    0x4000UL,    0x2000UL,    0x1000UL,
      0x800UL,     0x400UL,     0x200UL,     0x100UL,
      0x80UL,      0x40UL,      0x20UL,      0x10UL,
      0x8UL,       0x4UL,       0x2UL,       0x1L
  };
  /**
   * @brief   initializes the 3DES Cipher-algorithm with the passed
   *          parameters
   *
   * @param   context     the cipher_context_t-struct to save the
   *                      initialization of the cipher in
   * @param   key         a pointer to the key
   * @param   keySize     the size of the key
  
   *
   * @return  CIPHER_INIT_SUCCESS if the initialization was successful.
   *          The command may be unsuccessful if the key size is not valid.
   *          CIPHER_ERR_BAD_CONTEXT_SIZE if CIPHER_MAX_CONTEXT_SIZE has not been defined (which means that the cipher has not been included in the build)
   */
  int tripledes_init(cipher_context_t *context, const uint8_t* key, uint8_t keySize);
  
  /**
   * @brief   encrypts one plain-block and saves the result in crypt.
   *          encrypts one blocksize long block of plaintext pointed to by
   *          plain to one blocksize long block of ciphertext which will be
   *          written to the the memory-area pointed to by crypt
   *
   * @param   context   the cipher_context_t-struct to use for this
   *                    encryption
   * @param   plain     a pointer to the plaintext-block (of size
   *                    blocksize)
   * @param   crypt     a pointer to the place where the ciphertext will
   *                    be stored
   *
   * @return  -1 if no space for the key could be malloced
   *                      -2 if the key could not be setup correctly
   *                       1 if encryption was successful
   */
  int tripledes_encrypt(const cipher_context_t *context, const uint8_t *plain, uint8_t *crypt);
  
  /**
   * @brief  decrypts one cipher-block and saves the plain-block in plain.
   *         decrypts one blocksize long block of ciphertext pointed to by
   *         crypt to one blocksize long block of plaintext and stores the
   *         plaintext in the memory-area pointed to by plain
   *
   * @param   context   the cipher_context_t-struct to use for this
   *                    decryption
   * @param   crypt     a pointer to the ciphertext-block (of size blocksize)
   *                    to be decrypted
   * @param   plain     a pointer to the place where the decrypted plaintext
   *                    will be stored
   *
   * @return  -1 if no space for the key could be malloced
   *          -2 if the key could not be setup correctly
   *           1 if decryption was successful
   */
  int tripledes_decrypt(const cipher_context_t *context, const uint8_t *crypt, uint8_t *plain);
  
  #ifdef __cplusplus
  }
  #endif
  
  /** @} */
  #endif /* THREEDES_H_ */