The friendly Operating System for the Internet of Things
cbcmode.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2013 Freie Universit├Ąt Berlin, Computer Systems & Telematics
3  *
4  * This file is subject to the terms and conditions of the GNU Lesser
5  * General Public License v2.1. See the file LICENSE in the top level
6  * directory for more details.
7  */
8 
24 #include <stdio.h>
25 #include <string.h>
26 #include <stdlib.h>
27 #include <stdint.h>
28 
29 #ifndef CBCMODE_H
30 #define CBCMODE_H
31 
32 #include "crypto/ciphers.h"
33 
34 #ifdef __cplusplus
35 extern "C" {
36 #endif
37 
38 #define MIN(a, b) ( ((a) < (b)) ? (a) : (b))
39 
40 #define DBG_CRYPTO 1
41 
42 
43 enum {
44  // we allocate some static buffers on the stack; they have to be less
45  // than this size
46  CBCMODE_MAX_BLOCK_SIZE = 8,
47  CBCMODE_MAX_BLOCK_SIZE_AES = 16
48 };
49 
50 /*// We run a simple state machine in the incremental decrypt:
51  //
52  // +--> ONE_BLOCK
53  // |
54  // ---|
55  // |
56  // +--> GENERAL --+---> TWO_LEFT_A ----> TWO_LEFT_B
57  // ^ |
58  // | |
59  // +------+
60  */
61 
62 enum {
63  ONE_BLOCK,
64  GENERAL,
65  TWO_LEFT_A,
66  TWO_LEFT_B
67 };
68 
69 #define FAIL 0
70 #define SUCCESS 1
71 
72 
79 typedef struct CBCModeContext {
81  uint8_t spill1 [CBCMODE_MAX_BLOCK_SIZE ];
83  uint8_t spill2 [CBCMODE_MAX_BLOCK_SIZE ];
85  uint8_t bsize;
87  uint16_t remaining;
89  uint16_t completed;
92  uint8_t accum;
94  uint8_t offset;
96  uint8_t state;
97 } /*__attribute__ ((packed)) */ CBCModeContext;
98 
99 
100 /*
101  * @brief Initialize the Mode. It uses the underlying BlockCipher's
102  * preferred block cipher mode, and passes the key and keySize
103  * parameters to the underlying BlockCipher.
104  *
105  * @param context structure to hold the opaque data from this
106  * initialization call. It should be passed to future
107  * invocations of this module which use this particular key.
108  * It also contains the opaque context for the underlying
109  * BlockCipher as well.
110  * @param keySize key size in bytes
111  * @param key pointer to the key
112  *
113  * @return Whether initialization was successful. The command may be
114  * unsuccessful if the key size is not valid for the given cipher
115  * implementation. It can also fail if the preferred block size of
116  * the cipher does not agree with the preferred size of the mode.
117  */
118 int block_cipher_mode_init(CipherModeContext *context, uint8_t key_size,
119  uint8_t *key);
120 
140 int block_cipher_mode_init0(CipherModeContext *context, uint8_t key_size,
141  uint8_t *key, uint8_t cipher_index);
142 
149 void dbg(uint8_t mode, const char *format, ...);
150 
158 void dump_buffer(char *bufName, uint8_t *buf, uint8_t size);
159 
181 int block_cipher_mode_encrypt(CipherModeContext *context, uint8_t *plain_blocks,
182  uint8_t *cipher_blocks, uint16_t num_bytes,
183  uint8_t *IV);
184 
206 int block_cipher_mode_decrypt(CipherModeContext *context,
207  uint8_t *cipher_blocks,
208  uint8_t *plain_blocks,
209  uint16_t num_bytes,
210  uint8_t *IV);
211 
212 #ifdef __cplusplus
213 }
214 #endif
215 
217 #endif /* CBCMODE_H */
uint8_t state
state enum
Definition: cbcmode.h:96
int block_cipher_mode_init0(CipherModeContext *context, uint8_t key_size, uint8_t *key, uint8_t cipher_index)
same as BlockCipherMode_init but with the possibility to specify the index of the cipher in the archi...
struct CBCModeContext CBCModeContext
The context for processing the en-/decryption in the CBC-Mode with CTS.
uint8_t spill2[CBCMODE_MAX_BLOCK_SIZE]
Spill-Block 2 for temporary usage.
Definition: cbcmode.h:83
uint8_t accum
TRUE iff spill1 is the accumulator and spill2 holds prev cipher text.
Definition: cbcmode.h:92
int block_cipher_mode_decrypt(CipherModeContext *context, uint8_t *cipher_blocks, uint8_t *plain_blocks, uint16_t num_bytes, uint8_t *IV)
Decrypts num_bytes of ciphertext blocks (each of size blockSize) using the key from the init phase...
uint8_t offset
into the accumulator
Definition: cbcmode.h:94
The context for processing the en-/decryption in the CBC-Mode with CTS.
Definition: cbcmode.h:79
void dbg(uint8_t mode, const char *format,...)
prints the debug-messages passed by dump_buffer
uint8_t bsize
the blocksize currently used
Definition: cbcmode.h:85
uint16_t remaining
how many more bytes of ciphertext do we need to recv
Definition: cbcmode.h:87
stdio.h wrapper for MSP430
Headers for the packet encryption class.
uint8_t spill1[CBCMODE_MAX_BLOCK_SIZE]
Spill-Block 1 for temporary usage.
Definition: cbcmode.h:81
void dump_buffer(char *bufName, uint8_t *buf, uint8_t size)
dumps the passed buffer to the console
stdlib.h wrapper for MSP430
int block_cipher_mode_encrypt(CipherModeContext *context, uint8_t *plain_blocks, uint8_t *cipher_blocks, uint16_t num_bytes, uint8_t *IV)
Encrypts num_bytes of plaintext blocks (each of size blockSize) using the key from the init phase...
uint16_t completed
how many bytes of plaintext we've deciphered.
Definition: cbcmode.h:89