wxMaxima
Loading...
Searching...
No Matches
pdfrijndael.h
Go to the documentation of this file.
1
2// Name: pdfrijndael.h
3// Purpose:
4// Author: Ulrich Telle
5// Created: 2006-11-25
6// Copyright: (c) Ulrich Telle (Copyright for original code see below)
7// Licence: wxWindows licence
8//
9// Class renamed to wxPdfRijndael for use in wxPdfDocument (U. Telle)
10// Otherwise the code is unchanged
12
14
15#ifndef _PDF_RIJNDAEL_H_
16#define _PDF_RIJNDAEL_H_
17
18// File : rijndael.h
19// Creation date : Sun Nov 5 2000 03:21:05 CEST
20// Author : Szymon Stefanek (stefanek@tin.it)
21//
22// Another implementation of the Rijndael cipher.
23// This is intended to be an easily usable library file.
24// This code is public domain.
25// Based on the Vincent Rijmen and K.U.Leuven implementation 2.4.
26//
27// Original Copyright notice:
28//
29// rijndael-alg-fst.c v2.4 April '2000
30// rijndael-alg-fst.h
31// rijndael-api-fst.c
32// rijndael-api-fst.h
33//
34// Optimised ANSI C code
35//
36// authors: v1.0: Antoon Bosselaers
37// v2.0: Vincent Rijmen, K.U.Leuven
38// v2.3: Paulo Barreto
39// v2.4: Vincent Rijmen, K.U.Leuven
40//
41// This code is placed in the public domain.
42//
43
44//
45// This implementation works on 128 , 192 , 256 bit keys
46// and on 128 bit blocks
47//
48
49//
50// Example of usage:
51//
52// // Input data
53// unsigned char key[32]; // The key
54// initializeYour256BitKey(); // Obviously initialized with sth
55// const unsigned char * plainText = getYourPlainText(); // Your plain text
56// int plainTextLen = strlen(plainText); // Plain text length
57//
58// // Encrypting
59// Rijndael rin;
60// unsigned char output[plainTextLen + 16];
61//
62// rin.init(Rijndael::CBC,Rijndael::Encrypt,key,Rijndael::Key32Bytes);
63// // It is a good idea to check the error code
64// int len = rin.padEncrypt(plainText,len,output);
65// if(len >= 0)useYourEncryptedText();
66// else encryptError(len);
67//
68// // Decrypting: we can reuse the same object
69// unsigned char output2[len];
70// rin.init(Rijndael::CBC,Rijndael::Decrypt,key,Rijndael::Key32Bytes));
71// len = rin.padDecrypt(output,len,output2);
72// if(len >= 0)useYourDecryptedText();
73// else decryptError(len);
74//
75
76#define _MAX_KEY_COLUMNS (256/32)
77#define _MAX_ROUNDS 14
78#define MAX_IV_SIZE 16
79
80// We assume that unsigned int is 32 bits long....
81typedef unsigned char UINT8;
82typedef unsigned int UINT32;
83typedef unsigned short UINT16;
84
85// Error codes
86#define RIJNDAEL_SUCCESS 0
87#define RIJNDAEL_UNSUPPORTED_MODE -1
88#define RIJNDAEL_UNSUPPORTED_DIRECTION -2
89#define RIJNDAEL_UNSUPPORTED_KEY_LENGTH -3
90#define RIJNDAEL_BAD_KEY -4
91#define RIJNDAEL_NOT_INITIALIZED -5
92#define RIJNDAEL_BAD_DIRECTION -6
93#define RIJNDAEL_CORRUPTED_DATA -7
94
95// wxPdfDocument headers
96#include "wx/pdfdocdef.h"
97
98#ifndef STRICT_ALIGN
99#define STRICT_ALIGN 0
100#endif
101
103class WXDLLIMPEXP_PDFDOC wxPdfRijndael
104{
105public:
106 enum Direction { Encrypt , Decrypt };
107 enum Mode { ECB , CBC , CFB1 };
108 enum KeyLength { Key16Bytes , Key24Bytes , Key32Bytes };
109 //
110 // Creates a Rijndael cipher object
111 // You have to call init() before you can encrypt or decrypt stuff
112 //
115protected:
116 // Internal stuff
117 enum State { Valid , Invalid };
118
119 State m_state;
120 Mode m_mode;
121 Direction m_direction;
122 UINT8 m_initVector[MAX_IV_SIZE];
123 UINT32 m_uRounds;
124 UINT8 m_expandedKey[_MAX_ROUNDS+1][4][4];
125public:
127 // API
129
130 // init(): Initializes the crypt session
131 // Returns RIJNDAEL_SUCCESS or an error code
132 // mode : Rijndael::ECB, Rijndael::CBC or Rijndael::CFB1
133 // You have to use the same mode for encrypting and decrypting
134 // dir : Rijndael::Encrypt or Rijndael::Decrypt
135 // A cipher instance works only in one direction
136 // (Well , it could be easily modified to work in both
137 // directions with a single init() call, but it looks
138 // useless to me...anyway , it is a matter of generating
139 // two expanded keys)
140 // key : array of unsigned octets , it can be 16 , 24 or 32 bytes long
141 // this CAN be binary data (it is not expected to be null terminated)
142 // keyLen : Rijndael::Key16Bytes , Rijndael::Key24Bytes or Rijndael::Key32Bytes
143 // initVector: initialization vector, you will usually use 0 here
144 int init(Mode mode,Direction dir,const UINT8 *key,KeyLength keyLen,UINT8 * initVector = 0);
145 // Encrypts the input array (can be binary data)
146 // The input array length must be a multiple of 16 bytes, the remaining part
147 // is DISCARDED.
148 // so it actually encrypts inputLen / 128 blocks of input and puts it in outBuffer
149 // Input len is in BITS!
150 // outBuffer must be at least inputLen / 8 bytes long.
151 // Returns the encrypted buffer length in BITS or an error code < 0 in case of error
152 int blockEncrypt(const UINT8 *input, int inputLen, UINT8 *outBuffer);
153 // Encrypts the input array (can be binary data)
154 // The input array can be any length , it is automatically padded on a 16 byte boundary.
155 // Input len is in BYTES!
156 // outBuffer must be at least (inputLen + 16) bytes long
157 // Returns the encrypted buffer length in BYTES or an error code < 0 in case of error
158 int padEncrypt(const UINT8 *input, int inputOctets, UINT8 *outBuffer);
159 // Decrypts the input vector
160 // Input len is in BITS!
161 // outBuffer must be at least inputLen / 8 bytes long
162 // Returns the decrypted buffer length in BITS and an error code < 0 in case of error
163 int blockDecrypt(const UINT8 *input, int inputLen, UINT8 *outBuffer);
164 // Decrypts the input vector
165 // Input len is in BYTES!
166 // outBuffer must be at least inputLen bytes long
167 // Returns the decrypted buffer length in BYTES and an error code < 0 in case of error
168 int padDecrypt(const UINT8 *input, int inputOctets, UINT8 *outBuffer);
169protected:
170 void keySched(UINT8 key[_MAX_KEY_COLUMNS][4]);
171 void keyEncToDec();
172 void encrypt(const UINT8 a[16], UINT8 b[16]);
173 void decrypt(const UINT8 a[16], UINT8 b[16]);
174};
175
176#endif // _PDFRIJNDAEL_H_
const wxChar * key
The name in the config file.
Definition: ToolBarSections.cpp:37
Class implementing the Rijndael cipher. (For internal use only)
Definition: pdfrijndael.h:104
Compile time switches for the wxPdfDocument component.
unsigned int UINT32
A type definition for a .
Definition: structcmd.h:52