IBR-DTNSuite  0.10
AES128Stream.h
Go to the documentation of this file.
1 /*
2  * AES128Stream.h
3  *
4  * Copyright (C) 2011 IBR, TU Braunschweig
5  *
6  * Written-by: Johannes Morgenroth <morgenroth@ibr.cs.tu-bs.de>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  */
21 
22 #ifndef AES128STREAM_H_
23 #define AES128STREAM_H_
24 
25 #include <streambuf>
26 #include <ostream>
27 #include <sys/types.h>
28 #include <stdint.h>
30 #include "ibrcommon/ssl/gcm/gcm.h"
31 
32 namespace ibrcommon
33 {
43  {
44  public:
46  static const size_t key_size_in_bytes = 16;
48  static const size_t salt_len = sizeof(uint32_t);
50  static const size_t iv_len = 8;
52  static const size_t tag_len = 16;
54  static const size_t BUFF_SIZE = 2048;
55 
72  AES128Stream(const CipherMode mode, std::ostream& output, const unsigned char key[key_size_in_bytes], const uint32_t salt);
73  AES128Stream(const CipherMode mode, std::ostream& output, const unsigned char key[key_size_in_bytes], const uint32_t salt, const unsigned char iv[iv_len]);
74 
76  virtual ~AES128Stream();
77 
82  void getIV(unsigned char (&to_iv)[iv_len]) const;
83 
88  void getTag(unsigned char (&to_tag)[tag_len]);
89 
93  bool verify(const unsigned char (&verify_tag)[tag_len]);
94 
95  protected:
96  virtual void encrypt(char *buf, const size_t size);
97  virtual void decrypt(char *buf, const size_t size);
98 
99  private:
104  typedef struct {
105  uint32_t salt;
106  unsigned char initialisation_vector[iv_len];
107  } gcm_iv;
108 
110  gcm_iv _gcm_iv;
111 
113  gcm_ctx _ctx;
114 
118  unsigned char _used_initialisation_vector[iv_len];
119  };
120 }
121 
122 #endif /* AES128STREAM_H_ */