crypto
Published 29 Dec 2009, by
Anders Rundgren

No tags
« Back to documentation index
Show/hide line numbers
crypto.h Source File
crypto.h
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013 #ifndef _WEBPKI_CRYPTO_H_
00014 #define _WEBPKI_CRYPTO_H_
00015
00016 #include <limits.h>
00017
00018 #if INT_MAX == 32767
00019 typedef unsigned long CRYPTO_U32;
00020 #else
00021 typedef unsigned int CRYPTO_U32;
00022 #endif
00023
00024 namespace webpki
00025 {
00026
00027 class AESProvider
00028 {
00029 public:
00030
00031 AESProvider ();
00032
00033 static const int AES_BLOCK_SIZE = 16;
00034
00035 void setKey (const unsigned char* raw_key, int key_length, bool encrypt);
00036
00037 const char* encrypt (unsigned char* out, int& in_out_len, const unsigned char* in, const unsigned char* iv, bool pad);
00038
00039 private:
00040
00041 void AES_cbc_ecb_encrypt (const unsigned char* in, unsigned char* out, int length, const unsigned char* iv);
00042
00043 void AES_set_encrypt_key (const unsigned char* raw_key);
00044
00045 void AES_set_decrypt_key (const unsigned char* raw_key);
00046
00047 void AES_decrypt (const unsigned char* in, unsigned char* out);
00048
00049 void AES_encrypt (const unsigned char* in, unsigned char* out);
00050
00051 static const int AES_MAXNR = 14;
00052
00053 struct
00054 {
00055 CRYPTO_U32 rd_key[4 * (AES_MAXNR + 1)];
00056 int rounds;
00057 int length_in_bytes;
00058 } m_the_key;
00059
00060 bool m_encrypt;
00061
00062 const char* m_error;
00063 };
00064
00065
00066 class SHACore
00067 {
00068 public:
00069
00070 void update (const unsigned char* data, int length);
00071
00072 const char* doFinal (unsigned char* digest);
00073
00074 const char* doFinal (unsigned char* digest, const unsigned char* data, int length);
00075
00076 protected:
00077
00078 virtual void _init () = 0;
00079
00080 virtual void hash_block_data_order (const unsigned char* data, int num) = 0;
00081
00082 friend class HMACCore;
00083
00084 static const int SHA_LBLOCK = 16;
00085
00086 static const int SHA_CBLOCK = (SHA_LBLOCK * 4);
00087
00088 const char* m_error;
00089
00090 bool m_needs_init;
00091
00092 struct
00093 {
00094 CRYPTO_U32 h[8];
00095 CRYPTO_U32 Nl, Nh;
00096 CRYPTO_U32 data[SHA_LBLOCK];
00097 unsigned int num;
00098 int digest_length;
00099 } m_sha_ctx;
00100 };
00101
00102
00103 class SHA1Provider : public SHACore
00104 {
00105 public:
00106
00107 SHA1Provider ();
00108
00109 static const int DIGEST_LENGTH = 20;
00110
00111 private:
00112
00113 friend class HMAC_SHA1Provider;
00114
00115 virtual void _init ();
00116
00117 virtual void hash_block_data_order (const unsigned char* data, int num);
00118 };
00119
00120
00121 class SHA256Provider : public SHACore
00122 {
00123 public:
00124
00125 SHA256Provider ();
00126
00127 static const int DIGEST_LENGTH = 32;
00128
00129 private:
00130
00131 friend class HMAC_SHA256Provider;
00132
00133 virtual void _init ();
00134
00135 virtual void hash_block_data_order (const unsigned char* data, int num);
00136 };
00137
00138
00139 class HMACCore
00140 {
00141 public:
00142
00143 void init (const unsigned char* key, int key_length);
00144
00145 void update (const unsigned char* data, int length);
00146
00147 const char* doFinal (unsigned char* digest);
00148
00149 const char* doFinal (unsigned char* digest, const unsigned char* data, int length);
00150
00151 protected:
00152
00153 HMACCore (SHACore& outer, SHACore& inner);
00154
00155 private:
00156
00157 SHACore* m_outer_save;
00158
00159 SHACore* m_inner_save;
00160
00161 char* m_error;
00162 };
00163
00164
00165 class HMAC_SHA1Provider : public HMACCore
00166 {
00167 public:
00168
00169 HMAC_SHA1Provider ();
00170
00171 private:
00172
00173 SHA1Provider m_outer;
00174
00175 SHA1Provider m_inner;
00176 };
00177
00178
00179 class HMAC_SHA256Provider : public HMACCore
00180 {
00181 public:
00182
00183 HMAC_SHA256Provider ();
00184
00185 private:
00186
00187 SHA256Provider m_outer;
00188
00189 SHA256Provider m_inner;
00190 };
00191
00192 }
00193
00194 #endif