]> git.ipfire.org Git - thirdparty/pdns.git/commitdiff
import AES code from Brian Gladman, plus wrapper to PowerDNS dns_random
authorBert Hubert <bert.hubert@netherlabs.nl>
Sun, 16 Mar 2008 14:42:41 +0000 (14:42 +0000)
committerBert Hubert <bert.hubert@netherlabs.nl>
Sun, 16 Mar 2008 14:42:41 +0000 (14:42 +0000)
git-svn-id: svn://svn.powerdns.com/pdns/trunk/pdns@1159 d19b8d6e-7fed-0310-83ef-9ca221ded41b

12 files changed:
pdns/aes/aes.h [new file with mode: 0644]
pdns/aes/aes_modes.c [new file with mode: 0644]
pdns/aes/aescpp.h [new file with mode: 0644]
pdns/aes/aescrypt.c [new file with mode: 0644]
pdns/aes/aeskey.c [new file with mode: 0644]
pdns/aes/aesopt.h [new file with mode: 0644]
pdns/aes/aestab.c [new file with mode: 0644]
pdns/aes/aestab.h [new file with mode: 0644]
pdns/aes/brg_endian.h [new file with mode: 0644]
pdns/aes/brg_types.h [new file with mode: 0644]
pdns/aes/dns_random.cc [new file with mode: 0644]
pdns/docs/pdns.sgml

diff --git a/pdns/aes/aes.h b/pdns/aes/aes.h
new file mode 100644 (file)
index 0000000..ef2da2c
--- /dev/null
@@ -0,0 +1,205 @@
+/*\r
+ ---------------------------------------------------------------------------\r
+ Copyright (c) 1998-2007, Brian Gladman, Worcester, UK. All rights reserved.\r
+\r
+ LICENSE TERMS\r
+\r
+ The free distribution and use of this software is allowed (with or without\r
+ changes) provided that:\r
+\r
+  1. source code distributions include the above copyright notice, this\r
+     list of conditions and the following disclaimer;\r
+\r
+  2. binary distributions include the above copyright notice, this list\r
+     of conditions and the following disclaimer in their documentation;\r
+\r
+  3. the name of the copyright holder is not used to endorse products\r
+     built using this software without specific written permission.\r
+\r
+ DISCLAIMER\r
+\r
+ This software is provided 'as is' with no explicit or implied warranties\r
+ in respect of its properties, including, but not limited to, correctness\r
+ and/or fitness for purpose.\r
+ ---------------------------------------------------------------------------\r
+ Issue Date: 20/12/2007\r
+\r
+ This file contains the definitions required to use AES in C. See aesopt.h\r
+ for optimisation details.\r
+*/\r
+\r
+#ifndef _AES_H\r
+#define _AES_H\r
+\r
+#include <stdlib.h>\r
+\r
+/*  This include is used to find 8 & 32 bit unsigned integer types  */\r
+#include "brg_types.h"\r
+\r
+#if defined(__cplusplus)\r
+extern "C"\r
+{\r
+#endif\r
+\r
+#define AES_128     /* if a fast 128 bit key scheduler is needed    */\r
+#define AES_192     /* if a fast 192 bit key scheduler is needed    */\r
+#define AES_256     /* if a fast 256 bit key scheduler is needed    */\r
+#define AES_VAR     /* if variable key size scheduler is needed     */\r
+#define AES_MODES   /* if support is needed for modes               */\r
+\r
+/* The following must also be set in assembler files if being used  */\r
+\r
+#define AES_ENCRYPT /* if support for encryption is needed          */\r
+#define AES_DECRYPT /* if support for decryption is needed          */\r
+#define AES_REV_DKS /* define to reverse decryption key schedule    */\r
+\r
+#define AES_BLOCK_SIZE  16  /* the AES block size in bytes          */\r
+#define N_COLS           4  /* the number of columns in the state   */\r
+\r
+/* The key schedule length is 11, 13 or 15 16-byte blocks for 128,  */\r
+/* 192 or 256-bit keys respectively. That is 176, 208 or 240 bytes  */\r
+/* or 44, 52 or 60 32-bit words.                                    */\r
+\r
+#if defined( AES_VAR ) || defined( AES_256 )\r
+#define KS_LENGTH       60\r
+#elif defined( AES_192 )\r
+#define KS_LENGTH       52\r
+#else\r
+#define KS_LENGTH       44\r
+#endif\r
+\r
+#define AES_RETURN INT_RETURN\r
+\r
+/* the character array 'inf' in the following structures is used    */\r
+/* to hold AES context information. This AES code uses cx->inf.b[0] */\r
+/* to hold the number of rounds multiplied by 16. The other three   */\r
+/* elements can be used by code that implements additional modes    */\r
+\r
+typedef union\r
+{   uint_32t l;\r
+    uint_8t b[4];\r
+} aes_inf;\r
+\r
+typedef struct\r
+{   uint_32t ks[KS_LENGTH];\r
+    aes_inf inf;\r
+} aes_encrypt_ctx;\r
+\r
+typedef struct\r
+{   uint_32t ks[KS_LENGTH];\r
+    aes_inf inf;\r
+} aes_decrypt_ctx;\r
+\r
+/* This routine must be called before first use if non-static       */\r
+/* tables are being used                                            */\r
+\r
+AES_RETURN aes_init(void);\r
+\r
+/* Key lengths in the range 16 <= key_len <= 32 are given in bytes, */\r
+/* those in the range 128 <= key_len <= 256 are given in bits       */\r
+\r
+#if defined( AES_ENCRYPT )\r
+\r
+#if defined( AES_128 ) || defined( AES_VAR)\r
+AES_RETURN aes_encrypt_key128(const unsigned char *key, aes_encrypt_ctx cx[1]);\r
+#endif\r
+\r
+#if defined( AES_192 ) || defined( AES_VAR)\r
+AES_RETURN aes_encrypt_key192(const unsigned char *key, aes_encrypt_ctx cx[1]);\r
+#endif\r
+\r
+#if defined( AES_256 ) || defined( AES_VAR)\r
+AES_RETURN aes_encrypt_key256(const unsigned char *key, aes_encrypt_ctx cx[1]);\r
+#endif\r
+\r
+#if defined( AES_VAR )\r
+AES_RETURN aes_encrypt_key(const unsigned char *key, int key_len, aes_encrypt_ctx cx[1]);\r
+#endif\r
+\r
+AES_RETURN aes_encrypt(const unsigned char *in, unsigned char *out, const aes_encrypt_ctx cx[1]);\r
+\r
+#endif\r
+\r
+#if defined( AES_DECRYPT )\r
+\r
+#if defined( AES_128 ) || defined( AES_VAR)\r
+AES_RETURN aes_decrypt_key128(const unsigned char *key, aes_decrypt_ctx cx[1]);\r
+#endif\r
+\r
+#if defined( AES_192 ) || defined( AES_VAR)\r
+AES_RETURN aes_decrypt_key192(const unsigned char *key, aes_decrypt_ctx cx[1]);\r
+#endif\r
+\r
+#if defined( AES_256 ) || defined( AES_VAR)\r
+AES_RETURN aes_decrypt_key256(const unsigned char *key, aes_decrypt_ctx cx[1]);\r
+#endif\r
+\r
+#if defined( AES_VAR )\r
+AES_RETURN aes_decrypt_key(const unsigned char *key, int key_len, aes_decrypt_ctx cx[1]);\r
+#endif\r
+\r
+AES_RETURN aes_decrypt(const unsigned char *in, unsigned char *out, const aes_decrypt_ctx cx[1]);\r
+\r
+#endif\r
+\r
+#if defined( AES_MODES )\r
+\r
+/* Multiple calls to the following subroutines for multiple block   */\r
+/* ECB, CBC, CFB, OFB and CTR mode encryption can be used to handle */\r
+/* long messages incremantally provided that the context AND the iv */\r
+/* are preserved between all such calls.  For the ECB and CBC modes */\r
+/* each individual call within a series of incremental calls must   */\r
+/* process only full blocks (i.e. len must be a multiple of 16) but */\r
+/* the CFB, OFB and CTR mode calls can handle multiple incremental  */\r
+/* calls of any length. Each mode is reset when a new AES key is    */\r
+/* set but ECB and CBC operations can be reset without setting a    */\r
+/* new key by setting a new IV value.  To reset CFB, OFB and CTR    */\r
+/* without setting the key, aes_mode_reset() must be called and the */\r
+/* IV must be set.  NOTE: All these calls update the IV on exit so  */\r
+/* this has to be reset if a new operation with the same IV as the  */\r
+/* previous one is required (or decryption follows encryption with  */\r
+/* the same IV array).                                              */\r
+\r
+AES_RETURN aes_test_alignment_detection(unsigned int n);\r
+\r
+AES_RETURN aes_ecb_encrypt(const unsigned char *ibuf, unsigned char *obuf,\r
+                    int len, const aes_encrypt_ctx cx[1]);\r
+\r
+AES_RETURN aes_ecb_decrypt(const unsigned char *ibuf, unsigned char *obuf,\r
+                    int len, const aes_decrypt_ctx cx[1]);\r
+\r
+AES_RETURN aes_cbc_encrypt(const unsigned char *ibuf, unsigned char *obuf,\r
+                    int len, unsigned char *iv, const aes_encrypt_ctx cx[1]);\r
+\r
+AES_RETURN aes_cbc_decrypt(const unsigned char *ibuf, unsigned char *obuf,\r
+                    int len, unsigned char *iv, const aes_decrypt_ctx cx[1]);\r
+\r
+AES_RETURN aes_mode_reset(aes_encrypt_ctx cx[1]);\r
+\r
+AES_RETURN aes_cfb_encrypt(const unsigned char *ibuf, unsigned char *obuf,\r
+                    int len, unsigned char *iv, aes_encrypt_ctx cx[1]);\r
+\r
+AES_RETURN aes_cfb_decrypt(const unsigned char *ibuf, unsigned char *obuf,\r
+                    int len, unsigned char *iv, aes_encrypt_ctx cx[1]);\r
+\r
+#define aes_ofb_encrypt aes_ofb_crypt\r
+#define aes_ofb_decrypt aes_ofb_crypt\r
+\r
+AES_RETURN aes_ofb_crypt(const unsigned char *ibuf, unsigned char *obuf,\r
+                    int len, unsigned char *iv, aes_encrypt_ctx cx[1]);\r
+\r
+typedef void cbuf_inc(unsigned char *cbuf);\r
+\r
+#define aes_ctr_encrypt aes_ctr_crypt\r
+#define aes_ctr_decrypt aes_ctr_crypt\r
+\r
+AES_RETURN aes_ctr_crypt(const unsigned char *ibuf, unsigned char *obuf,\r
+            int len, unsigned char *cbuf, cbuf_inc ctr_inc, aes_encrypt_ctx cx[1]);\r
+\r
+#endif\r
+\r
+#if defined(__cplusplus)\r
+}\r
+#endif\r
+\r
+#endif\r
diff --git a/pdns/aes/aes_modes.c b/pdns/aes/aes_modes.c
new file mode 100644 (file)
index 0000000..ed52635
--- /dev/null
@@ -0,0 +1,918 @@
+/*\r
+ ---------------------------------------------------------------------------\r
+ Copyright (c) 1998-2007, Brian Gladman, Worcester, UK. All rights reserved.\r
+\r
+ LICENSE TERMS\r
+\r
+ The free distribution and use of this software is allowed (with or without\r
+ changes) provided that:\r
+\r
+  1. source code distributions include the above copyright notice, this\r
+     list of conditions and the following disclaimer;\r
+\r
+  2. binary distributions include the above copyright notice, this list\r
+     of conditions and the following disclaimer in their documentation;\r
+\r
+  3. the name of the copyright holder is not used to endorse products\r
+     built using this software without specific written permission.\r
+\r
+ DISCLAIMER\r
+\r
+ This software is provided 'as is' with no explicit or implied warranties\r
+ in respect of its properties, including, but not limited to, correctness\r
+ and/or fitness for purpose.\r
+ ---------------------------------------------------------------------------\r
+ Issue Date: 20/12/2007\r
+\r
+ These subroutines implement multiple block AES modes for ECB, CBC, CFB,\r
+ OFB and CTR encryption,  The code provides support for the VIA Advanced\r
+ Cryptography Engine (ACE).\r
+\r
+ NOTE: In the following subroutines, the AES contexts (ctx) must be\r
+ 16 byte aligned if VIA ACE is being used\r
+*/\r
+\r
+#include <string.h>\r
+#include <assert.h>\r
+\r
+#include "aesopt.h"\r
+\r
+#if defined( AES_MODES )\r
+#if defined(__cplusplus)\r
+extern "C"\r
+{\r
+#endif\r
+\r
+#if defined( _MSC_VER ) && ( _MSC_VER > 800 )\r
+#pragma intrinsic(memcpy)\r
+#endif\r
+\r
+#define BFR_BLOCKS      8\r
+\r
+/* These values are used to detect long word alignment in order to */\r
+/* speed up some buffer operations. This facility may not work on  */\r
+/* some machines so this define can be commented out if necessary  */\r
+\r
+#define FAST_BUFFER_OPERATIONS\r
+\r
+#define lp32(x)         ((uint_32t*)(x))\r
+\r
+#if defined( USE_VIA_ACE_IF_PRESENT )\r
+\r
+#include "aes_via_ace.h"\r
+\r
+#pragma pack(16)\r
+\r
+aligned_array(unsigned long,    enc_gen_table, 12, 16) =    NEH_ENC_GEN_DATA;\r
+aligned_array(unsigned long,   enc_load_table, 12, 16) =   NEH_ENC_LOAD_DATA;\r
+aligned_array(unsigned long, enc_hybrid_table, 12, 16) = NEH_ENC_HYBRID_DATA;\r
+aligned_array(unsigned long,    dec_gen_table, 12, 16) =    NEH_DEC_GEN_DATA;\r
+aligned_array(unsigned long,   dec_load_table, 12, 16) =   NEH_DEC_LOAD_DATA;\r
+aligned_array(unsigned long, dec_hybrid_table, 12, 16) = NEH_DEC_HYBRID_DATA;\r
+\r
+/* NOTE: These control word macros must only be used after  */\r
+/* a key has been set up because they depend on key size    */\r
+\r
+#if NEH_KEY_TYPE == NEH_LOAD\r
+#define kd_adr(c)   ((uint_8t*)(c)->ks)\r
+#elif NEH_KEY_TYPE == NEH_GENERATE\r
+#define kd_adr(c)   ((uint_8t*)(c)->ks + (c)->inf.b[0])\r
+#else\r
+#define kd_adr(c)   ((uint_8t*)(c)->ks + ((c)->inf.b[0] == 160 ? 160 : 0))\r
+#endif\r
+\r
+#else\r
+\r
+#define aligned_array(type, name, no, stride) type name[no]\r
+#define aligned_auto(type, name, no, stride)  type name[no]\r
+\r
+#endif\r
+\r
+#if defined( _MSC_VER ) && _MSC_VER > 1200\r
+\r
+#define via_cwd(cwd, ty, dir, len) \\r
+    unsigned long* cwd = (dir##_##ty##_table + ((len - 128) >> 4))\r
+\r
+#else\r
+\r
+#define via_cwd(cwd, ty, dir, len)              \\r
+    aligned_auto(unsigned long, cwd, 4, 16);    \\r
+    cwd[1] = cwd[2] = cwd[3] = 0;               \\r
+    cwd[0] = neh_##dir##_##ty##_key(len)\r
+\r
+#endif\r
+\r
+/* test the code for detecting and setting pointer alignment */\r
+\r
+AES_RETURN aes_test_alignment_detection(unsigned int n)        /* 4 <= n <= 16 */\r
+{      uint_8t p[16];\r
+       uint_32t i, count_eq = 0, count_neq = 0;\r
+\r
+       if(n < 4 || n > 16)\r
+               return EXIT_FAILURE;\r
+\r
+       for(i = 0; i < n; ++i)\r
+       {\r
+               uint_8t *qf = ALIGN_FLOOR(p + i, n),\r
+                               *qh =  ALIGN_CEIL(p + i, n);\r
+               \r
+               if(qh == qf)\r
+                       ++count_eq;\r
+               else if(qh == qf + n)\r
+                       ++count_neq;\r
+               else\r
+                       return EXIT_FAILURE;\r
+       }\r
+       return (count_eq != 1 || count_neq != n - 1 ? EXIT_FAILURE : EXIT_SUCCESS);\r
+}\r
+\r
+AES_RETURN aes_mode_reset(aes_encrypt_ctx ctx[1])\r
+{\r
+    ctx->inf.b[2] = 0;\r
+    return EXIT_SUCCESS;\r
+}\r
+\r
+AES_RETURN aes_ecb_encrypt(const unsigned char *ibuf, unsigned char *obuf,\r
+                    int len, const aes_encrypt_ctx ctx[1])\r
+{   int nb = len >> 4;\r
+\r
+    if(len & (AES_BLOCK_SIZE - 1))\r
+        return EXIT_FAILURE;\r
+\r
+#if defined( USE_VIA_ACE_IF_PRESENT )\r
+\r
+    if(ctx->inf.b[1] == 0xff)\r
+    {   uint_8t *ksp = (uint_8t*)(ctx->ks);\r
+        via_cwd(cwd, hybrid, enc, 2 * ctx->inf.b[0] - 192);\r
+\r
+        if(ALIGN_OFFSET( ctx, 16 ))\r
+            return EXIT_FAILURE;\r
+\r
+        if(!ALIGN_OFFSET( ibuf, 16 ) && !ALIGN_OFFSET( obuf, 16 ))\r
+        {\r
+            via_ecb_op5(ksp,cwd,ibuf,obuf,nb);\r
+        }\r
+        else\r
+        {   aligned_auto(uint_8t, buf, BFR_BLOCKS * AES_BLOCK_SIZE, 16);\r
+            uint_8t *ip, *op;\r
+\r
+            while(nb)\r
+            {\r
+                int m = (nb > BFR_BLOCKS ? BFR_BLOCKS : nb);\r
+\r
+                ip = (ALIGN_OFFSET( ibuf, 16 ) ? buf : ibuf);\r
+                op = (ALIGN_OFFSET( obuf, 16 ) ? buf : obuf);\r
+\r
+                if(ip != ibuf)\r
+                    memcpy(buf, ibuf, m * AES_BLOCK_SIZE);\r
+\r
+                via_ecb_op5(ksp,cwd,ip,op,m);\r
+\r
+                if(op != obuf)\r
+                    memcpy(obuf, buf, m * AES_BLOCK_SIZE);\r
+\r
+                ibuf += m * AES_BLOCK_SIZE;\r
+                obuf += m * AES_BLOCK_SIZE;\r
+                nb -= m;\r
+            }\r
+        }\r
+\r
+        return EXIT_SUCCESS;\r
+    }\r
+\r
+#endif\r
+\r
+#if !defined( ASSUME_VIA_ACE_PRESENT )\r
+    while(nb--)\r
+    {\r
+        if(aes_encrypt(ibuf, obuf, ctx) != EXIT_SUCCESS)\r
+                       return EXIT_FAILURE;\r
+        ibuf += AES_BLOCK_SIZE;\r
+        obuf += AES_BLOCK_SIZE;\r
+    }\r
+#endif\r
+    return EXIT_SUCCESS;\r
+}\r
+\r
+AES_RETURN aes_ecb_decrypt(const unsigned char *ibuf, unsigned char *obuf,\r
+                    int len, const aes_decrypt_ctx ctx[1])\r
+{   int nb = len >> 4;\r
+\r
+    if(len & (AES_BLOCK_SIZE - 1))\r
+        return EXIT_FAILURE;\r
+\r
+#if defined( USE_VIA_ACE_IF_PRESENT )\r
+\r
+    if(ctx->inf.b[1] == 0xff)\r
+    {   uint_8t *ksp = kd_adr(ctx);\r
+        via_cwd(cwd, hybrid, dec, 2 * ctx->inf.b[0] - 192);\r
+\r
+        if(ALIGN_OFFSET( ctx, 16 ))\r
+            return EXIT_FAILURE;\r
+\r
+        if(!ALIGN_OFFSET( ibuf, 16 ) && !ALIGN_OFFSET( obuf, 16 ))\r
+        {\r
+            via_ecb_op5(ksp,cwd,ibuf,obuf,nb);\r
+        }\r
+        else\r
+        {   aligned_auto(uint_8t, buf, BFR_BLOCKS * AES_BLOCK_SIZE, 16);\r
+            uint_8t *ip, *op;\r
+\r
+            while(nb)\r
+            {\r
+                int m = (nb > BFR_BLOCKS ? BFR_BLOCKS : nb);\r
+\r
+                ip = (ALIGN_OFFSET( ibuf, 16 ) ? buf : ibuf);\r
+                op = (ALIGN_OFFSET( obuf, 16 ) ? buf : obuf);\r
+\r
+                if(ip != ibuf)\r
+                    memcpy(buf, ibuf, m * AES_BLOCK_SIZE);\r
+\r
+                via_ecb_op5(ksp,cwd,ip,op,m);\r
+\r
+                if(op != obuf)\r
+                    memcpy(obuf, buf, m * AES_BLOCK_SIZE);\r
+\r
+                ibuf += m * AES_BLOCK_SIZE;\r
+                obuf += m * AES_BLOCK_SIZE;\r
+                nb -= m;\r
+            }\r
+        }\r
+\r
+        return EXIT_SUCCESS;\r
+    }\r
+\r
+#endif\r
+\r
+#if !defined( ASSUME_VIA_ACE_PRESENT )\r
+    while(nb--)\r
+    {\r
+        if(aes_decrypt(ibuf, obuf, ctx) != EXIT_SUCCESS)\r
+                       return EXIT_FAILURE;\r
+        ibuf += AES_BLOCK_SIZE;\r
+        obuf += AES_BLOCK_SIZE;\r
+    }\r
+#endif\r
+    return EXIT_SUCCESS;\r
+}\r
+\r
+AES_RETURN aes_cbc_encrypt(const unsigned char *ibuf, unsigned char *obuf,\r
+                    int len, unsigned char *iv, const aes_encrypt_ctx ctx[1])\r
+{   int nb = len >> 4;\r
+\r
+    if(len & (AES_BLOCK_SIZE - 1))\r
+        return EXIT_FAILURE;\r
+\r
+#if defined( USE_VIA_ACE_IF_PRESENT )\r
+\r
+    if(ctx->inf.b[1] == 0xff)\r
+    {   uint_8t *ksp = (uint_8t*)(ctx->ks), *ivp = iv;\r
+        aligned_auto(uint_8t, liv, AES_BLOCK_SIZE, 16);\r
+        via_cwd(cwd, hybrid, enc, 2 * ctx->inf.b[0] - 192);\r
+\r
+        if(ALIGN_OFFSET( ctx, 16 ))\r
+            return EXIT_FAILURE;\r
+\r
+        if(ALIGN_OFFSET( iv, 16 ))   /* ensure an aligned iv */\r
+        {\r
+            ivp = liv;\r
+            memcpy(liv, iv, AES_BLOCK_SIZE);\r
+        }\r
+\r
+        if(!ALIGN_OFFSET( ibuf, 16 ) && !ALIGN_OFFSET( obuf, 16 ) && !ALIGN_OFFSET( iv, 16 ))\r
+        {\r
+            via_cbc_op7(ksp,cwd,ibuf,obuf,nb,ivp,ivp);\r
+        }\r
+        else\r
+        {   aligned_auto(uint_8t, buf, BFR_BLOCKS * AES_BLOCK_SIZE, 16);\r
+            uint_8t *ip, *op;\r
+\r
+            while(nb)\r
+            {\r
+                int m = (nb > BFR_BLOCKS ? BFR_BLOCKS : nb);\r
+\r
+                ip = (ALIGN_OFFSET( ibuf, 16 ) ? buf : ibuf);\r
+                op = (ALIGN_OFFSET( obuf, 16 ) ? buf : obuf);\r
+\r
+                if(ip != ibuf)\r
+                    memcpy(buf, ibuf, m * AES_BLOCK_SIZE);\r
+\r
+                via_cbc_op7(ksp,cwd,ip,op,m,ivp,ivp);\r
+\r
+                if(op != obuf)\r
+                    memcpy(obuf, buf, m * AES_BLOCK_SIZE);\r
+\r
+                ibuf += m * AES_BLOCK_SIZE;\r
+                obuf += m * AES_BLOCK_SIZE;\r
+                nb -= m;\r
+            }\r
+        }\r
+\r
+        if(iv != ivp)\r
+            memcpy(iv, ivp, AES_BLOCK_SIZE);\r
+\r
+        return EXIT_SUCCESS;\r
+    }\r
+\r
+#endif\r
+\r
+#if !defined( ASSUME_VIA_ACE_PRESENT )\r
+# ifdef FAST_BUFFER_OPERATIONS\r
+    if(!ALIGN_OFFSET( ibuf, 4 ) && !ALIGN_OFFSET( iv, 4 ))\r
+        while(nb--)\r
+        {\r
+            lp32(iv)[0] ^= lp32(ibuf)[0];\r
+            lp32(iv)[1] ^= lp32(ibuf)[1];\r
+            lp32(iv)[2] ^= lp32(ibuf)[2];\r
+            lp32(iv)[3] ^= lp32(ibuf)[3];\r
+            if(aes_encrypt(iv, iv, ctx) != EXIT_SUCCESS)\r
+                               return EXIT_FAILURE;\r
+            memcpy(obuf, iv, AES_BLOCK_SIZE);\r
+            ibuf += AES_BLOCK_SIZE;\r
+            obuf += AES_BLOCK_SIZE;\r
+        }\r
+    else\r
+# endif\r
+        while(nb--)\r
+        {\r
+            iv[ 0] ^= ibuf[ 0]; iv[ 1] ^= ibuf[ 1];\r
+            iv[ 2] ^= ibuf[ 2]; iv[ 3] ^= ibuf[ 3];\r
+            iv[ 4] ^= ibuf[ 4]; iv[ 5] ^= ibuf[ 5];\r
+            iv[ 6] ^= ibuf[ 6]; iv[ 7] ^= ibuf[ 7];\r
+            iv[ 8] ^= ibuf[ 8]; iv[ 9] ^= ibuf[ 9];\r
+            iv[10] ^= ibuf[10]; iv[11] ^= ibuf[11];\r
+            iv[12] ^= ibuf[12]; iv[13] ^= ibuf[13];\r
+            iv[14] ^= ibuf[14]; iv[15] ^= ibuf[15];\r
+            if(aes_encrypt(iv, iv, ctx) != EXIT_SUCCESS)\r
+                               return EXIT_FAILURE;\r
+            memcpy(obuf, iv, AES_BLOCK_SIZE);\r
+            ibuf += AES_BLOCK_SIZE;\r
+            obuf += AES_BLOCK_SIZE;\r
+        }\r
+#endif\r
+    return EXIT_SUCCESS;\r
+}\r
+\r
+AES_RETURN aes_cbc_decrypt(const unsigned char *ibuf, unsigned char *obuf,\r
+                    int len, unsigned char *iv, const aes_decrypt_ctx ctx[1])\r
+{   unsigned char tmp[AES_BLOCK_SIZE];\r
+    int nb = len >> 4;\r
+\r
+    if(len & (AES_BLOCK_SIZE - 1))\r
+        return EXIT_FAILURE;\r
+\r
+#if defined( USE_VIA_ACE_IF_PRESENT )\r
+\r
+    if(ctx->inf.b[1] == 0xff)\r
+    {   uint_8t *ksp = kd_adr(ctx), *ivp = iv;\r
+        aligned_auto(uint_8t, liv, AES_BLOCK_SIZE, 16);\r
+        via_cwd(cwd, hybrid, dec, 2 * ctx->inf.b[0] - 192);\r
+\r
+        if(ALIGN_OFFSET( ctx, 16 ))\r
+            return EXIT_FAILURE;\r
+\r
+        if(ALIGN_OFFSET( iv, 16 ))   /* ensure an aligned iv */\r
+        {\r
+            ivp = liv;\r
+            memcpy(liv, iv, AES_BLOCK_SIZE);\r
+        }\r
+\r
+        if(!ALIGN_OFFSET( ibuf, 16 ) && !ALIGN_OFFSET( obuf, 16 ) && !ALIGN_OFFSET( iv, 16 ))\r
+        {\r
+            via_cbc_op6(ksp,cwd,ibuf,obuf,nb,ivp);\r
+        }\r
+        else\r
+        {   aligned_auto(uint_8t, buf, BFR_BLOCKS * AES_BLOCK_SIZE, 16);\r
+            uint_8t *ip, *op;\r
+\r
+            while(nb)\r
+            {\r
+                int m = (nb > BFR_BLOCKS ? BFR_BLOCKS : nb);\r
+\r
+                ip = (ALIGN_OFFSET( ibuf, 16 ) ? buf : ibuf);\r
+                op = (ALIGN_OFFSET( obuf, 16 ) ? buf : obuf);\r
+\r
+                if(ip != ibuf)\r
+                    memcpy(buf, ibuf, m * AES_BLOCK_SIZE);\r
+\r
+                via_cbc_op6(ksp,cwd,ip,op,m,ivp);\r
+\r
+                if(op != obuf)\r
+                    memcpy(obuf, buf, m * AES_BLOCK_SIZE);\r
+\r
+                ibuf += m * AES_BLOCK_SIZE;\r
+                obuf += m * AES_BLOCK_SIZE;\r
+                nb -= m;\r
+            }\r
+        }\r
+\r
+        if(iv != ivp)\r
+            memcpy(iv, ivp, AES_BLOCK_SIZE);\r
+\r
+        return EXIT_SUCCESS;\r
+    }\r
+#endif\r
+\r
+#if !defined( ASSUME_VIA_ACE_PRESENT )\r
+# ifdef FAST_BUFFER_OPERATIONS\r
+    if(!ALIGN_OFFSET( obuf, 4 ) && !ALIGN_OFFSET( iv, 4 ))\r
+        while(nb--)\r
+        {\r
+            memcpy(tmp, ibuf, AES_BLOCK_SIZE);\r
+            if(aes_decrypt(ibuf, obuf, ctx) != EXIT_SUCCESS)\r
+                               return EXIT_FAILURE;\r
+            lp32(obuf)[0] ^= lp32(iv)[0];\r
+            lp32(obuf)[1] ^= lp32(iv)[1];\r
+            lp32(obuf)[2] ^= lp32(iv)[2];\r
+            lp32(obuf)[3] ^= lp32(iv)[3];\r
+            memcpy(iv, tmp, AES_BLOCK_SIZE);\r
+            ibuf += AES_BLOCK_SIZE;\r
+            obuf += AES_BLOCK_SIZE;\r
+        }\r
+    else\r
+# endif\r
+        while(nb--)\r
+        {\r
+            memcpy(tmp, ibuf, AES_BLOCK_SIZE);\r
+            if(aes_decrypt(ibuf, obuf, ctx) != EXIT_SUCCESS)\r
+                               return EXIT_FAILURE;\r
+            obuf[ 0] ^= iv[ 0]; obuf[ 1] ^= iv[ 1];\r
+            obuf[ 2] ^= iv[ 2]; obuf[ 3] ^= iv[ 3];\r
+            obuf[ 4] ^= iv[ 4]; obuf[ 5] ^= iv[ 5];\r
+            obuf[ 6] ^= iv[ 6]; obuf[ 7] ^= iv[ 7];\r
+            obuf[ 8] ^= iv[ 8]; obuf[ 9] ^= iv[ 9];\r
+            obuf[10] ^= iv[10]; obuf[11] ^= iv[11];\r
+            obuf[12] ^= iv[12]; obuf[13] ^= iv[13];\r
+            obuf[14] ^= iv[14]; obuf[15] ^= iv[15];\r
+            memcpy(iv, tmp, AES_BLOCK_SIZE);\r
+            ibuf += AES_BLOCK_SIZE;\r
+            obuf += AES_BLOCK_SIZE;\r
+        }\r
+#endif\r
+    return EXIT_SUCCESS;\r
+}\r
+\r
+AES_RETURN aes_cfb_encrypt(const unsigned char *ibuf, unsigned char *obuf,\r
+                    int len, unsigned char *iv, aes_encrypt_ctx ctx[1])\r
+{   int cnt = 0, b_pos = (int)ctx->inf.b[2], nb;\r
+\r
+    if(b_pos)           /* complete any partial block   */\r
+    {\r
+        while(b_pos < AES_BLOCK_SIZE && cnt < len)\r
+            *obuf++ = iv[b_pos++] ^= *ibuf++, cnt++;\r
+\r
+        b_pos = (b_pos == AES_BLOCK_SIZE ? 0 : b_pos);\r
+    }\r
+\r
+    if((nb = (len - cnt) >> 4) != 0)    /* process whole blocks */\r
+    {\r
+#if defined( USE_VIA_ACE_IF_PRESENT )\r
+\r
+        if(ctx->inf.b[1] == 0xff)\r
+        {   int m;\r
+            uint_8t *ksp = (uint_8t*)(ctx->ks), *ivp = iv;\r
+            aligned_auto(uint_8t, liv, AES_BLOCK_SIZE, 16);\r
+            via_cwd(cwd, hybrid, enc, 2 * ctx->inf.b[0] - 192);\r
+\r
+            if(ALIGN_OFFSET( ctx, 16 ))\r
+                return EXIT_FAILURE;\r
+\r
+            if(ALIGN_OFFSET( iv, 16 ))   /* ensure an aligned iv */\r
+            {\r
+                ivp = liv;\r
+                memcpy(liv, iv, AES_BLOCK_SIZE);\r
+            }\r
+\r
+            if(!ALIGN_OFFSET( ibuf, 16 ) && !ALIGN_OFFSET( obuf, 16 ))\r
+            {\r
+                via_cfb_op7(ksp, cwd, ibuf, obuf, nb, ivp, ivp);\r
+                ibuf += nb * AES_BLOCK_SIZE;\r
+                obuf += nb * AES_BLOCK_SIZE;\r
+                cnt  += nb * AES_BLOCK_SIZE;\r
+            }\r
+            else    /* input, output or both are unaligned  */\r
+            {   aligned_auto(uint_8t, buf, BFR_BLOCKS * AES_BLOCK_SIZE, 16);\r
+                uint_8t *ip, *op;\r
+\r
+                while(nb)\r
+                {\r
+                    m = (nb > BFR_BLOCKS ? BFR_BLOCKS : nb), nb -= m;\r
+\r
+                    ip = (ALIGN_OFFSET( ibuf, 16 ) ? buf : ibuf);\r
+                    op = (ALIGN_OFFSET( obuf, 16 ) ? buf : obuf);\r
+\r
+                    if(ip != ibuf)\r
+                        memcpy(buf, ibuf, m * AES_BLOCK_SIZE);\r
+\r
+                    via_cfb_op7(ksp, cwd, ip, op, m, ivp, ivp);\r
+\r
+                    if(op != obuf)\r
+                        memcpy(obuf, buf, m * AES_BLOCK_SIZE);\r
+\r
+                    ibuf += m * AES_BLOCK_SIZE;\r
+                    obuf += m * AES_BLOCK_SIZE;\r
+                    cnt  += m * AES_BLOCK_SIZE;\r
+                }\r
+            }\r
+\r
+            if(ivp != iv)\r
+                memcpy(iv, ivp, AES_BLOCK_SIZE);\r
+        }\r
+#else\r
+# ifdef FAST_BUFFER_OPERATIONS\r
+        if(!ALIGN_OFFSET( ibuf, 4 ) && !ALIGN_OFFSET( obuf, 4 ) && !ALIGN_OFFSET( iv, 4 ))\r
+            while(cnt + AES_BLOCK_SIZE <= len)\r
+            {\r
+                assert(b_pos == 0);\r
+                if(aes_encrypt(iv, iv, ctx) != EXIT_SUCCESS)\r
+                                       return EXIT_FAILURE;\r
+                lp32(obuf)[0] = lp32(iv)[0] ^= lp32(ibuf)[0];\r
+                lp32(obuf)[1] = lp32(iv)[1] ^= lp32(ibuf)[1];\r
+                lp32(obuf)[2] = lp32(iv)[2] ^= lp32(ibuf)[2];\r
+                lp32(obuf)[3] = lp32(iv)[3] ^= lp32(ibuf)[3];\r
+                ibuf += AES_BLOCK_SIZE;\r
+                obuf += AES_BLOCK_SIZE;\r
+                cnt  += AES_BLOCK_SIZE;\r
+            }\r
+        else\r
+# endif\r
+            while(cnt + AES_BLOCK_SIZE <= len)\r
+            {\r
+                assert(b_pos == 0);\r
+                if(aes_encrypt(iv, iv, ctx) != EXIT_SUCCESS)\r
+                                       return EXIT_FAILURE;\r
+                obuf[ 0] = iv[ 0] ^= ibuf[ 0]; obuf[ 1] = iv[ 1] ^= ibuf[ 1];\r
+                obuf[ 2] = iv[ 2] ^= ibuf[ 2]; obuf[ 3] = iv[ 3] ^= ibuf[ 3];\r
+                obuf[ 4] = iv[ 4] ^= ibuf[ 4]; obuf[ 5] = iv[ 5] ^= ibuf[ 5];\r
+                obuf[ 6] = iv[ 6] ^= ibuf[ 6]; obuf[ 7] = iv[ 7] ^= ibuf[ 7];\r
+                obuf[ 8] = iv[ 8] ^= ibuf[ 8]; obuf[ 9] = iv[ 9] ^= ibuf[ 9];\r
+                obuf[10] = iv[10] ^= ibuf[10]; obuf[11] = iv[11] ^= ibuf[11];\r
+                obuf[12] = iv[12] ^= ibuf[12]; obuf[13] = iv[13] ^= ibuf[13];\r
+                obuf[14] = iv[14] ^= ibuf[14]; obuf[15] = iv[15] ^= ibuf[15];\r
+                ibuf += AES_BLOCK_SIZE;\r
+                obuf += AES_BLOCK_SIZE;\r
+                cnt  += AES_BLOCK_SIZE;\r
+            }\r
+#endif\r
+    }\r
+\r
+    while(cnt < len)\r
+    {\r
+        if(!b_pos && aes_encrypt(iv, iv, ctx) != EXIT_SUCCESS)\r
+                       return EXIT_FAILURE;\r
+\r
+        while(cnt < len && b_pos < AES_BLOCK_SIZE)\r
+            *obuf++ = iv[b_pos++] ^= *ibuf++, cnt++;\r
+\r
+        b_pos = (b_pos == AES_BLOCK_SIZE ? 0 : b_pos);\r
+    }\r
+\r
+    ctx->inf.b[2] = b_pos;\r
+    return EXIT_SUCCESS;\r
+}\r
+\r
+AES_RETURN aes_cfb_decrypt(const unsigned char *ibuf, unsigned char *obuf,\r
+                    int len, unsigned char *iv, aes_encrypt_ctx ctx[1])\r
+{   int cnt = 0, b_pos = (int)ctx->inf.b[2], nb;\r
+\r
+    if(b_pos)           /* complete any partial block   */\r
+    {   uint_8t t;\r
+\r
+        while(b_pos < AES_BLOCK_SIZE && cnt < len)\r
+            t = *ibuf++, *obuf++ = t ^ iv[b_pos], iv[b_pos++] = t, cnt++;\r
+\r
+        b_pos = (b_pos == AES_BLOCK_SIZE ? 0 : b_pos);\r
+    }\r
+\r
+    if((nb = (len - cnt) >> 4) != 0)    /* process whole blocks */\r
+    {\r
+#if defined( USE_VIA_ACE_IF_PRESENT )\r
+\r
+        if(ctx->inf.b[1] == 0xff)\r
+        {   int m;\r
+            uint_8t *ksp = (uint_8t*)(ctx->ks), *ivp = iv;\r
+            aligned_auto(uint_8t, liv, AES_BLOCK_SIZE, 16);\r
+            via_cwd(cwd, hybrid, dec, 2 * ctx->inf.b[0] - 192);\r
+\r
+            if(ALIGN_OFFSET( ctx, 16 ))\r
+                return EXIT_FAILURE;\r
+\r
+            if(ALIGN_OFFSET( iv, 16 ))   /* ensure an aligned iv */\r
+            {\r
+                ivp = liv;\r
+                memcpy(liv, iv, AES_BLOCK_SIZE);\r
+            }\r
+\r
+            if(!ALIGN_OFFSET( ibuf, 16 ) && !ALIGN_OFFSET( obuf, 16 ))\r
+            {\r
+                via_cfb_op6(ksp, cwd, ibuf, obuf, nb, ivp);\r
+                ibuf += nb * AES_BLOCK_SIZE;\r
+                obuf += nb * AES_BLOCK_SIZE;\r
+                cnt  += nb * AES_BLOCK_SIZE;\r
+            }\r
+            else    /* input, output or both are unaligned  */\r
+            {   aligned_auto(uint_8t, buf, BFR_BLOCKS * AES_BLOCK_SIZE, 16);\r
+                uint_8t *ip, *op;\r
+\r
+                while(nb)\r
+                {\r
+                    m = (nb > BFR_BLOCKS ? BFR_BLOCKS : nb), nb -= m;\r
+\r
+                    ip = (ALIGN_OFFSET( ibuf, 16 ) ? buf : ibuf);\r
+                    op = (ALIGN_OFFSET( obuf, 16 ) ? buf : obuf);\r
+\r
+                    if(ip != ibuf)  /* input buffer is not aligned */\r
+                        memcpy(buf, ibuf, m * AES_BLOCK_SIZE);\r
+\r
+                    via_cfb_op6(ksp, cwd, ip, op, m, ivp);\r
+\r
+                    if(op != obuf)  /* output buffer is not aligned */\r
+                        memcpy(obuf, buf, m * AES_BLOCK_SIZE);\r
+\r
+                    ibuf += m * AES_BLOCK_SIZE;\r
+                    obuf += m * AES_BLOCK_SIZE;\r
+                    cnt  += m * AES_BLOCK_SIZE;\r
+                }\r
+            }\r
+\r
+            if(ivp != iv)\r
+                memcpy(iv, ivp, AES_BLOCK_SIZE);\r
+        }\r
+#else\r
+# ifdef FAST_BUFFER_OPERATIONS\r
+        if(!ALIGN_OFFSET( ibuf, 4 ) && !ALIGN_OFFSET( obuf, 4 ) &&!ALIGN_OFFSET( iv, 4 ))\r
+            while(cnt + AES_BLOCK_SIZE <= len)\r
+            {   uint_32t t;\r
+\r
+                assert(b_pos == 0);\r
+                if(aes_encrypt(iv, iv, ctx) != EXIT_SUCCESS)\r
+                                       return EXIT_FAILURE;\r
+                t = lp32(ibuf)[0], lp32(obuf)[0] = t ^ lp32(iv)[0], lp32(iv)[0] = t;\r
+                t = lp32(ibuf)[1], lp32(obuf)[1] = t ^ lp32(iv)[1], lp32(iv)[1] = t;\r
+                t = lp32(ibuf)[2], lp32(obuf)[2] = t ^ lp32(iv)[2], lp32(iv)[2] = t;\r
+                t = lp32(ibuf)[3], lp32(obuf)[3] = t ^ lp32(iv)[3], lp32(iv)[3] = t;\r
+                ibuf += AES_BLOCK_SIZE;\r
+                obuf += AES_BLOCK_SIZE;\r
+                cnt  += AES_BLOCK_SIZE;\r
+            }\r
+        else\r
+# endif\r
+            while(cnt + AES_BLOCK_SIZE <= len)\r
+            {   uint_8t t;\r
+\r
+                assert(b_pos == 0);\r
+                if(aes_encrypt(iv, iv, ctx) != EXIT_SUCCESS)\r
+                                       return EXIT_FAILURE;\r
+                t = ibuf[ 0], obuf[ 0] = t ^ iv[ 0], iv[ 0] = t;\r
+                t = ibuf[ 1], obuf[ 1] = t ^ iv[ 1], iv[ 1] = t;\r
+                t = ibuf[ 2], obuf[ 2] = t ^ iv[ 2], iv[ 2] = t;\r
+                t = ibuf[ 3], obuf[ 3] = t ^ iv[ 3], iv[ 3] = t;\r
+                t = ibuf[ 4], obuf[ 4] = t ^ iv[ 4], iv[ 4] = t;\r
+                t = ibuf[ 5], obuf[ 5] = t ^ iv[ 5], iv[ 5] = t;\r
+                t = ibuf[ 6], obuf[ 6] = t ^ iv[ 6], iv[ 6] = t;\r
+                t = ibuf[ 7], obuf[ 7] = t ^ iv[ 7], iv[ 7] = t;\r
+                t = ibuf[ 8], obuf[ 8] = t ^ iv[ 8], iv[ 8] = t;\r
+                t = ibuf[ 9], obuf[ 9] = t ^ iv[ 9], iv[ 9] = t;\r
+                t = ibuf[10], obuf[10] = t ^ iv[10], iv[10] = t;\r
+                t = ibuf[11], obuf[11] = t ^ iv[11], iv[11] = t;\r
+                t = ibuf[12], obuf[12] = t ^ iv[12], iv[12] = t;\r
+                t = ibuf[13], obuf[13] = t ^ iv[13], iv[13] = t;\r
+                t = ibuf[14], obuf[14] = t ^ iv[14], iv[14] = t;\r
+                t = ibuf[15], obuf[15] = t ^ iv[15], iv[15] = t;\r
+                ibuf += AES_BLOCK_SIZE;\r
+                obuf += AES_BLOCK_SIZE;\r
+                cnt  += AES_BLOCK_SIZE;\r
+            }\r
+#endif\r
+    }\r
+\r
+    while(cnt < len)\r
+    {   uint_8t t;\r
+\r
+        if(!b_pos && aes_encrypt(iv, iv, ctx) != EXIT_SUCCESS)\r
+                       return EXIT_FAILURE;\r
+\r
+        while(cnt < len && b_pos < AES_BLOCK_SIZE)\r
+            t = *ibuf++, *obuf++ = t ^ iv[b_pos], iv[b_pos++] = t, cnt++;\r
+\r
+        b_pos = (b_pos == AES_BLOCK_SIZE ? 0 : b_pos);\r
+    }\r
+\r
+    ctx->inf.b[2] = b_pos;\r
+    return EXIT_SUCCESS;\r
+}\r
+\r
+AES_RETURN aes_ofb_crypt(const unsigned char *ibuf, unsigned char *obuf,\r
+                    int len, unsigned char *iv, aes_encrypt_ctx ctx[1])\r
+{   int cnt = 0, b_pos = (int)ctx->inf.b[2], nb;\r
+\r
+    if(b_pos)           /* complete any partial block   */\r
+    {\r
+        while(b_pos < AES_BLOCK_SIZE && cnt < len)\r
+            *obuf++ = iv[b_pos++] ^ *ibuf++, cnt++;\r
+\r
+        b_pos = (b_pos == AES_BLOCK_SIZE ? 0 : b_pos);\r
+    }\r
+\r
+    if((nb = (len - cnt) >> 4) != 0)   /* process whole blocks */\r
+    {\r
+#if defined( USE_VIA_ACE_IF_PRESENT )\r
+\r
+        if(ctx->inf.b[1] == 0xff)\r
+        {   int m;\r
+            uint_8t *ksp = (uint_8t*)(ctx->ks), *ivp = iv;\r
+            aligned_auto(uint_8t, liv, AES_BLOCK_SIZE, 16);\r
+            via_cwd(cwd, hybrid, enc, 2 * ctx->inf.b[0] - 192);\r
+\r
+            if(ALIGN_OFFSET( ctx, 16 ))\r
+                return EXIT_FAILURE;\r
+\r
+            if(ALIGN_OFFSET( iv, 16 ))   /* ensure an aligned iv */\r
+            {\r
+                ivp = liv;\r
+                memcpy(liv, iv, AES_BLOCK_SIZE);\r
+            }\r
+\r
+            if(!ALIGN_OFFSET( ibuf, 16 ) && !ALIGN_OFFSET( obuf, 16 ))\r
+            {\r
+                via_ofb_op6(ksp, cwd, ibuf, obuf, nb, ivp);\r
+                ibuf += nb * AES_BLOCK_SIZE;\r
+                obuf += nb * AES_BLOCK_SIZE;\r
+                cnt  += nb * AES_BLOCK_SIZE;\r
+            }\r
+            else    /* input, output or both are unaligned  */\r
+        {   aligned_auto(uint_8t, buf, BFR_BLOCKS * AES_BLOCK_SIZE, 16);\r
+            uint_8t *ip, *op;\r
+\r
+                while(nb)\r
+                {\r
+                    m = (nb > BFR_BLOCKS ? BFR_BLOCKS : nb), nb -= m;\r
+\r
+                    ip = (ALIGN_OFFSET( ibuf, 16 ) ? buf : ibuf);\r
+                    op = (ALIGN_OFFSET( obuf, 16 ) ? buf : obuf);\r
+\r
+                    if(ip != ibuf)\r
+                        memcpy(buf, ibuf, m * AES_BLOCK_SIZE);\r
+\r
+                    via_ofb_op6(ksp, cwd, ip, op, m, ivp);\r
+\r
+                    if(op != obuf)\r
+                        memcpy(obuf, buf, m * AES_BLOCK_SIZE);\r
+\r
+                    ibuf += m * AES_BLOCK_SIZE;\r
+                    obuf += m * AES_BLOCK_SIZE;\r
+                    cnt  += m * AES_BLOCK_SIZE;\r
+                }\r
+            }\r
+\r
+            if(ivp != iv)\r
+                memcpy(iv, ivp, AES_BLOCK_SIZE);\r
+        }\r
+#else\r
+# ifdef FAST_BUFFER_OPERATIONS\r
+        if(!ALIGN_OFFSET( ibuf, 4 ) && !ALIGN_OFFSET( obuf, 4 ) && !ALIGN_OFFSET( iv, 4 ))\r
+            while(cnt + AES_BLOCK_SIZE <= len)\r
+            {\r
+                assert(b_pos == 0);\r
+                if(aes_encrypt(iv, iv, ctx) != EXIT_SUCCESS)\r
+                                       return EXIT_FAILURE;\r
+                lp32(obuf)[0] = lp32(iv)[0] ^ lp32(ibuf)[0];\r
+                lp32(obuf)[1] = lp32(iv)[1] ^ lp32(ibuf)[1];\r
+                lp32(obuf)[2] = lp32(iv)[2] ^ lp32(ibuf)[2];\r
+                lp32(obuf)[3] = lp32(iv)[3] ^ lp32(ibuf)[3];\r
+                ibuf += AES_BLOCK_SIZE;\r
+                obuf += AES_BLOCK_SIZE;\r
+                cnt  += AES_BLOCK_SIZE;\r
+            }\r
+        else\r
+# endif\r
+            while(cnt + AES_BLOCK_SIZE <= len)\r
+            {\r
+                assert(b_pos == 0);\r
+                if(aes_encrypt(iv, iv, ctx) != EXIT_SUCCESS)\r
+                                       return EXIT_FAILURE;\r
+                obuf[ 0] = iv[ 0] ^ ibuf[ 0]; obuf[ 1] = iv[ 1] ^ ibuf[ 1];\r
+                obuf[ 2] = iv[ 2] ^ ibuf[ 2]; obuf[ 3] = iv[ 3] ^ ibuf[ 3];\r
+                obuf[ 4] = iv[ 4] ^ ibuf[ 4]; obuf[ 5] = iv[ 5] ^ ibuf[ 5];\r
+                obuf[ 6] = iv[ 6] ^ ibuf[ 6]; obuf[ 7] = iv[ 7] ^ ibuf[ 7];\r
+                obuf[ 8] = iv[ 8] ^ ibuf[ 8]; obuf[ 9] = iv[ 9] ^ ibuf[ 9];\r
+                obuf[10] = iv[10] ^ ibuf[10]; obuf[11] = iv[11] ^ ibuf[11];\r
+                obuf[12] = iv[12] ^ ibuf[12]; obuf[13] = iv[13] ^ ibuf[13];\r
+                obuf[14] = iv[14] ^ ibuf[14]; obuf[15] = iv[15] ^ ibuf[15];\r
+                ibuf += AES_BLOCK_SIZE;\r
+                obuf += AES_BLOCK_SIZE;\r
+                cnt  += AES_BLOCK_SIZE;\r
+            }\r
+#endif\r
+    }\r
+\r
+    while(cnt < len)\r
+    {\r
+        if(!b_pos && aes_encrypt(iv, iv, ctx) != EXIT_SUCCESS)\r
+                       return EXIT_FAILURE;\r
+\r
+        while(cnt < len && b_pos < AES_BLOCK_SIZE)\r
+            *obuf++ = iv[b_pos++] ^ *ibuf++, cnt++;\r
+\r
+        b_pos = (b_pos == AES_BLOCK_SIZE ? 0 : b_pos);\r
+    }\r
+\r
+    ctx->inf.b[2] = b_pos;\r
+    return EXIT_SUCCESS;\r
+}\r
+\r
+#define BFR_LENGTH  (BFR_BLOCKS * AES_BLOCK_SIZE)\r
+\r
+AES_RETURN aes_ctr_crypt(const unsigned char *ibuf, unsigned char *obuf,\r
+            int len, unsigned char *cbuf, cbuf_inc ctr_inc, aes_encrypt_ctx ctx[1])\r
+{   uint_8t *ip;\r
+    int     i, blen, b_pos = (int)(ctx->inf.b[2]);\r
+\r
+#if defined( USE_VIA_ACE_IF_PRESENT )\r
+    aligned_auto(uint_8t, buf, BFR_LENGTH, 16);\r
+    if(ctx->inf.b[1] == 0xff && ALIGN_OFFSET( ctx, 16 ))\r
+        return EXIT_FAILURE;\r
+#else\r
+    uint_8t buf[BFR_LENGTH];\r
+#endif\r
+\r
+    if(b_pos)\r
+    {\r
+        memcpy(buf, cbuf, AES_BLOCK_SIZE);\r
+        if(aes_ecb_encrypt(buf, buf, AES_BLOCK_SIZE, ctx) != EXIT_SUCCESS)\r
+                       return EXIT_FAILURE;\r
+        while(b_pos < AES_BLOCK_SIZE && len)\r
+            *obuf++ = *ibuf++ ^ buf[b_pos++], --len;\r
+        if(len)\r
+            ctr_inc(cbuf), b_pos = 0;\r
+    }\r
+\r
+    while(len)\r
+    {\r
+        blen = (len > BFR_LENGTH ? BFR_LENGTH : len), len -= blen;\r
+\r
+        for(i = 0, ip = buf; i < (blen >> 4); ++i)\r
+        {\r
+            memcpy(ip, cbuf, AES_BLOCK_SIZE);\r
+            ctr_inc(cbuf);\r
+            ip += AES_BLOCK_SIZE;\r
+        }\r
+\r
+        if(blen & (AES_BLOCK_SIZE - 1))\r
+            memcpy(ip, cbuf, AES_BLOCK_SIZE), i++;\r
+\r
+#if defined( USE_VIA_ACE_IF_PRESENT )\r
+        if(ctx->inf.b[1] == 0xff)\r
+        {\r
+            via_cwd(cwd, hybrid, enc, 2 * ctx->inf.b[0] - 192);\r
+            via_ecb_op5((ctx->ks),cwd,buf,buf,i);\r
+        }\r
+        else\r
+#endif\r
+        if(aes_ecb_encrypt(buf, buf, i * AES_BLOCK_SIZE, ctx) != EXIT_SUCCESS)\r
+                       return EXIT_FAILURE;\r
+\r
+        i = 0; ip = buf;\r
+# ifdef FAST_BUFFER_OPERATIONS\r
+        if(!ALIGN_OFFSET( ibuf, 4 ) && !ALIGN_OFFSET( obuf, 4 ) && !ALIGN_OFFSET( ip, 4 ))\r
+            while(i + AES_BLOCK_SIZE <= blen)\r
+            {\r
+                lp32(obuf)[0] = lp32(ibuf)[0] ^ lp32(ip)[0];\r
+                lp32(obuf)[1] = lp32(ibuf)[1] ^ lp32(ip)[1];\r
+                lp32(obuf)[2] = lp32(ibuf)[2] ^ lp32(ip)[2];\r
+                lp32(obuf)[3] = lp32(ibuf)[3] ^ lp32(ip)[3];\r
+                i += AES_BLOCK_SIZE;\r
+                ip += AES_BLOCK_SIZE;\r
+                ibuf += AES_BLOCK_SIZE;\r
+                obuf += AES_BLOCK_SIZE;\r
+            }\r
+        else\r
+#endif\r
+            while(i + AES_BLOCK_SIZE <= blen)\r
+            {\r
+                obuf[ 0] = ibuf[ 0] ^ ip[ 0]; obuf[ 1] = ibuf[ 1] ^ ip[ 1];\r
+                obuf[ 2] = ibuf[ 2] ^ ip[ 2]; obuf[ 3] = ibuf[ 3] ^ ip[ 3];\r
+                obuf[ 4] = ibuf[ 4] ^ ip[ 4]; obuf[ 5] = ibuf[ 5] ^ ip[ 5];\r
+                obuf[ 6] = ibuf[ 6] ^ ip[ 6]; obuf[ 7] = ibuf[ 7] ^ ip[ 7];\r
+                obuf[ 8] = ibuf[ 8] ^ ip[ 8]; obuf[ 9] = ibuf[ 9] ^ ip[ 9];\r
+                obuf[10] = ibuf[10] ^ ip[10]; obuf[11] = ibuf[11] ^ ip[11];\r
+                obuf[12] = ibuf[12] ^ ip[12]; obuf[13] = ibuf[13] ^ ip[13];\r
+                obuf[14] = ibuf[14] ^ ip[14]; obuf[15] = ibuf[15] ^ ip[15];\r
+                i += AES_BLOCK_SIZE;\r
+                ip += AES_BLOCK_SIZE;\r
+                ibuf += AES_BLOCK_SIZE;\r
+                obuf += AES_BLOCK_SIZE;\r
+            }\r
+\r
+        while(i++ < blen)\r
+            *obuf++ = *ibuf++ ^ ip[b_pos++];\r
+    }\r
+\r
+    ctx->inf.b[2] = b_pos;\r
+    return EXIT_SUCCESS;\r
+}\r
+\r
+#if defined(__cplusplus)\r
+}\r
+#endif\r
+#endif\r
diff --git a/pdns/aes/aescpp.h b/pdns/aes/aescpp.h
new file mode 100644 (file)
index 0000000..7c21f71
--- /dev/null
@@ -0,0 +1,148 @@
+/*\r
+ ---------------------------------------------------------------------------\r
+ Copyright (c) 1998-2007, Brian Gladman, Worcester, UK. All rights reserved.\r
+\r
+ LICENSE TERMS\r
+\r
+ The free distribution and use of this software is allowed (with or without\r
+ changes) provided that:\r
+\r
+  1. source code distributions include the above copyright notice, this\r
+     list of conditions and the following disclaimer;\r
+\r
+  2. binary distributions include the above copyright notice, this list\r
+     of conditions and the following disclaimer in their documentation;\r
+\r
+  3. the name of the copyright holder is not used to endorse products\r
+     built using this software without specific written permission.\r
+\r
+ DISCLAIMER\r
+\r
+ This software is provided 'as is' with no explicit or implied warranties\r
+ in respect of its properties, including, but not limited to, correctness\r
+ and/or fitness for purpose.\r
+ ---------------------------------------------------------------------------\r
+ Issue Date: 20/12/2007\r
+\r
+ This file contains the definitions required to use AES (Rijndael) in C++.\r
+*/\r
+\r
+#ifndef _AESCPP_H\r
+#define _AESCPP_H\r
+\r
+#include "aes.h"\r
+\r
+#if defined( AES_ENCRYPT )\r
+\r
+class AESencrypt\r
+{\r
+public:\r
+    aes_encrypt_ctx cx[1];\r
+    AESencrypt(void) { aes_init(); };\r
+#if defined(AES_128)\r
+    AESencrypt(const unsigned char key[])\r
+        {   aes_encrypt_key128(key, cx); }\r
+    AES_RETURN key128(const unsigned char key[])\r
+        {   return aes_encrypt_key128(key, cx); }\r
+#endif\r
+#if defined(AES_192)\r
+    AES_RETURN key192(const unsigned char key[])\r
+        {   return aes_encrypt_key192(key, cx); }\r
+#endif\r
+#if defined(AES_256)\r
+    AES_RETURN key256(const unsigned char key[])\r
+        {   return aes_encrypt_key256(key, cx); }\r
+#endif\r
+#if defined(AES_VAR)\r
+    AES_RETURN key(const unsigned char key[], int key_len)\r
+        {   return aes_encrypt_key(key, key_len, cx); }\r
+#endif\r
+    AES_RETURN encrypt(const unsigned char in[], unsigned char out[]) const\r
+        {   return aes_encrypt(in, out, cx);  }\r
+#ifndef AES_MODES\r
+    AES_RETURN ecb_encrypt(const unsigned char in[], unsigned char out[], int nb) const\r
+        {   while(nb--)\r
+            {   aes_encrypt(in, out, cx), in += AES_BLOCK_SIZE, out += AES_BLOCK_SIZE; }\r
+        }\r
+#endif\r
+#ifdef AES_MODES\r
+    AES_RETURN mode_reset(void)   { return aes_mode_reset(cx); }\r
+\r
+    AES_RETURN ecb_encrypt(const unsigned char in[], unsigned char out[], int nb) const\r
+        {   return aes_ecb_encrypt(in, out, nb, cx);  }\r
+\r
+    AES_RETURN cbc_encrypt(const unsigned char in[], unsigned char out[], int nb,\r
+                                    unsigned char iv[]) const\r
+        {   return aes_cbc_encrypt(in, out, nb, iv, cx);  }\r
+\r
+    AES_RETURN cfb_encrypt(const unsigned char in[], unsigned char out[], int nb,\r
+                                    unsigned char iv[])\r
+        {   return aes_cfb_encrypt(in, out, nb, iv, cx);  }\r
+\r
+    AES_RETURN cfb_decrypt(const unsigned char in[], unsigned char out[], int nb,\r
+                                    unsigned char iv[])\r
+        {   return aes_cfb_decrypt(in, out, nb, iv, cx);  }\r
+\r
+    AES_RETURN ofb_crypt(const unsigned char in[], unsigned char out[], int nb,\r
+                                    unsigned char iv[])\r
+        {   return aes_ofb_crypt(in, out, nb, iv, cx);  }\r
+\r
+    typedef void ctr_fn(unsigned char ctr[]);\r
+\r
+    AES_RETURN ctr_crypt(const unsigned char in[], unsigned char out[], int nb,\r
+                                    unsigned char iv[], ctr_fn cf)\r
+        {   return aes_ctr_crypt(in, out, nb, iv, cf, cx);  }\r
+\r
+#endif\r
+\r
+};\r
+\r
+#endif\r
+\r
+#if defined( AES_DECRYPT )\r
+\r
+class AESdecrypt\r
+{\r
+public:\r
+    aes_decrypt_ctx cx[1];\r
+    AESdecrypt(void) { aes_init(); };\r
+#if defined(AES_128)\r
+    AESdecrypt(const unsigned char key[])\r
+            { aes_decrypt_key128(key, cx); }\r
+    AES_RETURN key128(const unsigned char key[])\r
+            { return aes_decrypt_key128(key, cx); }\r
+#endif\r
+#if defined(AES_192)\r
+    AES_RETURN key192(const unsigned char key[])\r
+            { return aes_decrypt_key192(key, cx); }\r
+#endif\r
+#if defined(AES_256)\r
+    AES_RETURN key256(const unsigned char key[])\r
+            { return aes_decrypt_key256(key, cx); }\r
+#endif\r
+#if defined(AES_VAR)\r
+    AES_RETURN key(const unsigned char key[], int key_len)\r
+            { return aes_decrypt_key(key, key_len, cx); }\r
+#endif\r
+    AES_RETURN decrypt(const unsigned char in[], unsigned char out[]) const\r
+        {   return aes_decrypt(in, out, cx);  }\r
+#ifndef AES_MODES\r
+    AES_RETURN ecb_decrypt(const unsigned char in[], unsigned char out[], int nb) const\r
+        {   while(nb--)\r
+            {   aes_decrypt(in, out, cx), in += AES_BLOCK_SIZE, out += AES_BLOCK_SIZE; }\r
+        }\r
+#endif\r
+#ifdef AES_MODES\r
+\r
+    AES_RETURN ecb_decrypt(const unsigned char in[], unsigned char out[], int nb) const\r
+        {   return aes_ecb_decrypt(in, out, nb, cx);  }\r
+\r
+    AES_RETURN cbc_decrypt(const unsigned char in[], unsigned char out[], int nb,\r
+                                    unsigned char iv[]) const\r
+        {   return aes_cbc_decrypt(in, out, nb, iv, cx);  }\r
+#endif\r
+};\r
+\r
+#endif\r
+\r
+#endif\r
diff --git a/pdns/aes/aescrypt.c b/pdns/aes/aescrypt.c
new file mode 100644 (file)
index 0000000..e44da06
--- /dev/null
@@ -0,0 +1,301 @@
+/*\r
+ ---------------------------------------------------------------------------\r
+ Copyright (c) 1998-2007, Brian Gladman, Worcester, UK. All rights reserved.\r
+\r
+ LICENSE TERMS\r
+\r
+ The free distribution and use of this software is allowed (with or without\r
+ changes) provided that:\r
+\r
+  1. source code distributions include the above copyright notice, this\r
+     list of conditions and the following disclaimer;\r
+\r
+  2. binary distributions include the above copyright notice, this list\r
+     of conditions and the following disclaimer in their documentation;\r
+\r
+  3. the name of the copyright holder is not used to endorse products\r
+     built using this software without specific written permission.\r
+\r
+ DISCLAIMER\r
+\r
+ This software is provided 'as is' with no explicit or implied warranties\r
+ in respect of its properties, including, but not limited to, correctness\r
+ and/or fitness for purpose.\r
+ ---------------------------------------------------------------------------\r
+ Issue Date: 20/12/2007\r
+*/\r
+\r
+#include "aesopt.h"\r
+#include "aestab.h"\r
+\r
+#if defined(__cplusplus)\r
+extern "C"\r
+{\r
+#endif\r
+\r
+#define si(y,x,k,c) (s(y,c) = word_in(x, c) ^ (k)[c])\r
+#define so(y,x,c)   word_out(y, c, s(x,c))\r
+\r
+#if defined(ARRAYS)\r
+#define locals(y,x)     x[4],y[4]\r
+#else\r
+#define locals(y,x)     x##0,x##1,x##2,x##3,y##0,y##1,y##2,y##3\r
+#endif\r
+\r
+#define l_copy(y, x)    s(y,0) = s(x,0); s(y,1) = s(x,1); \\r
+                        s(y,2) = s(x,2); s(y,3) = s(x,3);\r
+#define state_in(y,x,k) si(y,x,k,0); si(y,x,k,1); si(y,x,k,2); si(y,x,k,3)\r
+#define state_out(y,x)  so(y,x,0); so(y,x,1); so(y,x,2); so(y,x,3)\r
+#define round(rm,y,x,k) rm(y,x,k,0); rm(y,x,k,1); rm(y,x,k,2); rm(y,x,k,3)\r
+\r
+#if ( FUNCS_IN_C & ENCRYPTION_IN_C )\r
+\r
+/* Visual C++ .Net v7.1 provides the fastest encryption code when using\r
+   Pentium optimiation with small code but this is poor for decryption\r
+   so we need to control this with the following VC++ pragmas\r
+*/\r
+\r
+#if defined( _MSC_VER ) && !defined( _WIN64 )\r
+#pragma optimize( "s", on )\r
+#endif\r
+\r
+/* Given the column (c) of the output state variable, the following\r
+   macros give the input state variables which are needed in its\r
+   computation for each row (r) of the state. All the alternative\r
+   macros give the same end values but expand into different ways\r
+   of calculating these values.  In particular the complex macro\r
+   used for dynamically variable block sizes is designed to expand\r
+   to a compile time constant whenever possible but will expand to\r
+   conditional clauses on some branches (I am grateful to Frank\r
+   Yellin for this construction)\r
+*/\r
+\r
+#define fwd_var(x,r,c)\\r
+ ( r == 0 ? ( c == 0 ? s(x,0) : c == 1 ? s(x,1) : c == 2 ? s(x,2) : s(x,3))\\r
+ : r == 1 ? ( c == 0 ? s(x,1) : c == 1 ? s(x,2) : c == 2 ? s(x,3) : s(x,0))\\r
+ : r == 2 ? ( c == 0 ? s(x,2) : c == 1 ? s(x,3) : c == 2 ? s(x,0) : s(x,1))\\r
+ :          ( c == 0 ? s(x,3) : c == 1 ? s(x,0) : c == 2 ? s(x,1) : s(x,2)))\r
+\r
+#if defined(FT4_SET)\r
+#undef  dec_fmvars\r
+#define fwd_rnd(y,x,k,c)    (s(y,c) = (k)[c] ^ four_tables(x,t_use(f,n),fwd_var,rf1,c))\r
+#elif defined(FT1_SET)\r
+#undef  dec_fmvars\r
+#define fwd_rnd(y,x,k,c)    (s(y,c) = (k)[c] ^ one_table(x,upr,t_use(f,n),fwd_var,rf1,c))\r
+#else\r
+#define fwd_rnd(y,x,k,c)    (s(y,c) = (k)[c] ^ fwd_mcol(no_table(x,t_use(s,box),fwd_var,rf1,c)))\r
+#endif\r
+\r
+#if defined(FL4_SET)\r
+#define fwd_lrnd(y,x,k,c)   (s(y,c) = (k)[c] ^ four_tables(x,t_use(f,l),fwd_var,rf1,c))\r
+#elif defined(FL1_SET)\r
+#define fwd_lrnd(y,x,k,c)   (s(y,c) = (k)[c] ^ one_table(x,ups,t_use(f,l),fwd_var,rf1,c))\r
+#else\r
+#define fwd_lrnd(y,x,k,c)   (s(y,c) = (k)[c] ^ no_table(x,t_use(s,box),fwd_var,rf1,c))\r
+#endif\r
+\r
+AES_RETURN aes_encrypt(const unsigned char *in, unsigned char *out, const aes_encrypt_ctx cx[1])\r
+{   uint_32t         locals(b0, b1);\r
+    const uint_32t   *kp;\r
+#if defined( dec_fmvars )\r
+    dec_fmvars; /* declare variables for fwd_mcol() if needed */\r
+#endif\r
+\r
+    if( cx->inf.b[0] != 10 * 16 && cx->inf.b[0] != 12 * 16 && cx->inf.b[0] != 14 * 16 )\r
+        return EXIT_FAILURE;\r
+\r
+    kp = cx->ks;\r
+    state_in(b0, in, kp);\r
+\r
+#if (ENC_UNROLL == FULL)\r
+\r
+    switch(cx->inf.b[0])\r
+    {\r
+    case 14 * 16:\r
+        round(fwd_rnd,  b1, b0, kp + 1 * N_COLS);\r
+        round(fwd_rnd,  b0, b1, kp + 2 * N_COLS);\r
+        kp += 2 * N_COLS;\r
+    case 12 * 16:\r
+        round(fwd_rnd,  b1, b0, kp + 1 * N_COLS);\r
+        round(fwd_rnd,  b0, b1, kp + 2 * N_COLS);\r
+        kp += 2 * N_COLS;\r
+    case 10 * 16:\r
+        round(fwd_rnd,  b1, b0, kp + 1 * N_COLS);\r
+        round(fwd_rnd,  b0, b1, kp + 2 * N_COLS);\r
+        round(fwd_rnd,  b1, b0, kp + 3 * N_COLS);\r
+        round(fwd_rnd,  b0, b1, kp + 4 * N_COLS);\r
+        round(fwd_rnd,  b1, b0, kp + 5 * N_COLS);\r
+        round(fwd_rnd,  b0, b1, kp + 6 * N_COLS);\r
+        round(fwd_rnd,  b1, b0, kp + 7 * N_COLS);\r
+        round(fwd_rnd,  b0, b1, kp + 8 * N_COLS);\r
+        round(fwd_rnd,  b1, b0, kp + 9 * N_COLS);\r
+        round(fwd_lrnd, b0, b1, kp +10 * N_COLS);\r
+    }\r
+\r
+#else\r
+\r
+#if (ENC_UNROLL == PARTIAL)\r
+    {   uint_32t    rnd;\r
+        for(rnd = 0; rnd < (cx->inf.b[0] >> 5) - 1; ++rnd)\r
+        {\r
+            kp += N_COLS;\r
+            round(fwd_rnd, b1, b0, kp);\r
+            kp += N_COLS;\r
+            round(fwd_rnd, b0, b1, kp);\r
+        }\r
+        kp += N_COLS;\r
+        round(fwd_rnd,  b1, b0, kp);\r
+#else\r
+    {   uint_32t    rnd;\r
+        for(rnd = 0; rnd < (cx->inf.b[0] >> 4) - 1; ++rnd)\r
+        {\r
+            kp += N_COLS;\r
+            round(fwd_rnd, b1, b0, kp);\r
+            l_copy(b0, b1);\r
+        }\r
+#endif\r
+        kp += N_COLS;\r
+        round(fwd_lrnd, b0, b1, kp);\r
+    }\r
+#endif\r
+\r
+    state_out(out, b0);\r
+    return EXIT_SUCCESS;\r
+}\r
+\r
+#endif\r
+\r
+#if ( FUNCS_IN_C & DECRYPTION_IN_C)\r
+\r
+/* Visual C++ .Net v7.1 provides the fastest encryption code when using\r
+   Pentium optimiation with small code but this is poor for decryption\r
+   so we need to control this with the following VC++ pragmas\r
+*/\r
+\r
+#if defined( _MSC_VER ) && !defined( _WIN64 )\r
+#pragma optimize( "t", on )\r
+#endif\r
+\r
+/* Given the column (c) of the output state variable, the following\r
+   macros give the input state variables which are needed in its\r
+   computation for each row (r) of the state. All the alternative\r
+   macros give the same end values but expand into different ways\r
+   of calculating these values.  In particular the complex macro\r
+   used for dynamically variable block sizes is designed to expand\r
+   to a compile time constant whenever possible but will expand to\r
+   conditional clauses on some branches (I am grateful to Frank\r
+   Yellin for this construction)\r
+*/\r
+\r
+#define inv_var(x,r,c)\\r
+ ( r == 0 ? ( c == 0 ? s(x,0) : c == 1 ? s(x,1) : c == 2 ? s(x,2) : s(x,3))\\r
+ : r == 1 ? ( c == 0 ? s(x,3) : c == 1 ? s(x,0) : c == 2 ? s(x,1) : s(x,2))\\r
+ : r == 2 ? ( c == 0 ? s(x,2) : c == 1 ? s(x,3) : c == 2 ? s(x,0) : s(x,1))\\r
+ :          ( c == 0 ? s(x,1) : c == 1 ? s(x,2) : c == 2 ? s(x,3) : s(x,0)))\r
+\r
+#if defined(IT4_SET)\r
+#undef  dec_imvars\r
+#define inv_rnd(y,x,k,c)    (s(y,c) = (k)[c] ^ four_tables(x,t_use(i,n),inv_var,rf1,c))\r
+#elif defined(IT1_SET)\r
+#undef  dec_imvars\r
+#define inv_rnd(y,x,k,c)    (s(y,c) = (k)[c] ^ one_table(x,upr,t_use(i,n),inv_var,rf1,c))\r
+#else\r
+#define inv_rnd(y,x,k,c)    (s(y,c) = inv_mcol((k)[c] ^ no_table(x,t_use(i,box),inv_var,rf1,c)))\r
+#endif\r
+\r
+#if defined(IL4_SET)\r
+#define inv_lrnd(y,x,k,c)   (s(y,c) = (k)[c] ^ four_tables(x,t_use(i,l),inv_var,rf1,c))\r
+#elif defined(IL1_SET)\r
+#define inv_lrnd(y,x,k,c)   (s(y,c) = (k)[c] ^ one_table(x,ups,t_use(i,l),inv_var,rf1,c))\r
+#else\r
+#define inv_lrnd(y,x,k,c)   (s(y,c) = (k)[c] ^ no_table(x,t_use(i,box),inv_var,rf1,c))\r
+#endif\r
+\r
+/* This code can work with the decryption key schedule in the   */\r
+/* order that is used for encrytpion (where the 1st decryption  */\r
+/* round key is at the high end ot the schedule) or with a key  */\r
+/* schedule that has been reversed to put the 1st decryption    */\r
+/* round key at the low end of the schedule in memory (when     */\r
+/* AES_REV_DKS is defined)                                      */\r
+\r
+#ifdef AES_REV_DKS\r
+#define key_ofs     0\r
+#define rnd_key(n)  (kp + n * N_COLS)\r
+#else\r
+#define key_ofs     1\r
+#define rnd_key(n)  (kp - n * N_COLS)\r
+#endif\r
+\r
+AES_RETURN aes_decrypt(const unsigned char *in, unsigned char *out, const aes_decrypt_ctx cx[1])\r
+{   uint_32t        locals(b0, b1);\r
+#if defined( dec_imvars )\r
+    dec_imvars; /* declare variables for inv_mcol() if needed */\r
+#endif\r
+    const uint_32t *kp;\r
+\r
+    if( cx->inf.b[0] != 10 * 16 && cx->inf.b[0] != 12 * 16 && cx->inf.b[0] != 14 * 16 )\r
+        return EXIT_FAILURE;\r
+\r
+    kp = cx->ks + (key_ofs ? (cx->inf.b[0] >> 2) : 0);\r
+    state_in(b0, in, kp);\r
+\r
+#if (DEC_UNROLL == FULL)\r
+\r
+    kp = cx->ks + (key_ofs ? 0 : (cx->inf.b[0] >> 2));\r
+    switch(cx->inf.b[0])\r
+    {\r
+    case 14 * 16:\r
+        round(inv_rnd,  b1, b0, rnd_key(-13));\r
+        round(inv_rnd,  b0, b1, rnd_key(-12));\r
+    case 12 * 16:\r
+        round(inv_rnd,  b1, b0, rnd_key(-11));\r
+        round(inv_rnd,  b0, b1, rnd_key(-10));\r
+    case 10 * 16:\r
+        round(inv_rnd,  b1, b0, rnd_key(-9));\r
+        round(inv_rnd,  b0, b1, rnd_key(-8));\r
+        round(inv_rnd,  b1, b0, rnd_key(-7));\r
+        round(inv_rnd,  b0, b1, rnd_key(-6));\r
+        round(inv_rnd,  b1, b0, rnd_key(-5));\r
+        round(inv_rnd,  b0, b1, rnd_key(-4));\r
+        round(inv_rnd,  b1, b0, rnd_key(-3));\r
+        round(inv_rnd,  b0, b1, rnd_key(-2));\r
+        round(inv_rnd,  b1, b0, rnd_key(-1));\r
+        round(inv_lrnd, b0, b1, rnd_key( 0));\r
+    }\r
+\r
+#else\r
+\r
+#if (DEC_UNROLL == PARTIAL)\r
+    {   uint_32t    rnd;\r
+        for(rnd = 0; rnd < (cx->inf.b[0] >> 5) - 1; ++rnd)\r
+        {\r
+            kp = rnd_key(1);\r
+            round(inv_rnd, b1, b0, kp);\r
+            kp = rnd_key(1);\r
+            round(inv_rnd, b0, b1, kp);\r
+        }\r
+        kp = rnd_key(1);\r
+        round(inv_rnd, b1, b0, kp);\r
+#else\r
+    {   uint_32t    rnd;\r
+        for(rnd = 0; rnd < (cx->inf.b[0] >> 4) - 1; ++rnd)\r
+        {\r
+            kp = rnd_key(1);\r
+            round(inv_rnd, b1, b0, kp);\r
+            l_copy(b0, b1);\r
+        }\r
+#endif\r
+        kp = rnd_key(1);\r
+        round(inv_lrnd, b0, b1, kp);\r
+        }\r
+#endif\r
+\r
+    state_out(out, b0);\r
+    return EXIT_SUCCESS;\r
+}\r
+\r
+#endif\r
+\r
+#if defined(__cplusplus)\r
+}\r
+#endif\r
diff --git a/pdns/aes/aeskey.c b/pdns/aes/aeskey.c
new file mode 100644 (file)
index 0000000..a052fe8
--- /dev/null
@@ -0,0 +1,555 @@
+/*\r
+ ---------------------------------------------------------------------------\r
+ Copyright (c) 1998-2007, Brian Gladman, Worcester, UK. All rights reserved.\r
+\r
+ LICENSE TERMS\r
+\r
+ The free distribution and use of this software is allowed (with or without\r
+ changes) provided that:\r
+\r
+  1. source code distributions include the above copyright notice, this\r
+     list of conditions and the following disclaimer;\r
+\r
+  2. binary distributions include the above copyright notice, this list\r
+     of conditions and the following disclaimer in their documentation;\r
+\r
+  3. the name of the copyright holder is not used to endorse products\r
+     built using this software without specific written permission.\r
+\r
+ DISCLAIMER\r
+\r
+ This software is provided 'as is' with no explicit or implied warranties\r
+ in respect of its properties, including, but not limited to, correctness\r
+ and/or fitness for purpose.\r
+ ---------------------------------------------------------------------------\r
+ Issue Date: 20/12/2007\r
+*/\r
+\r
+#include "aesopt.h"\r
+#include "aestab.h"\r
+\r
+#ifdef USE_VIA_ACE_IF_PRESENT\r
+#  include "aes_via_ace.h"\r
+#endif\r
+\r
+#if defined(__cplusplus)\r
+extern "C"\r
+{\r
+#endif\r
+\r
+/* Initialise the key schedule from the user supplied key. The key\r
+   length can be specified in bytes, with legal values of 16, 24\r
+   and 32, or in bits, with legal values of 128, 192 and 256. These\r
+   values correspond with Nk values of 4, 6 and 8 respectively.\r
+\r
+   The following macros implement a single cycle in the key\r
+   schedule generation process. The number of cycles needed\r
+   for each cx->n_col and nk value is:\r
+\r
+    nk =             4  5  6  7  8\r
+    ------------------------------\r
+    cx->n_col = 4   10  9  8  7  7\r
+    cx->n_col = 5   14 11 10  9  9\r
+    cx->n_col = 6   19 15 12 11 11\r
+    cx->n_col = 7   21 19 16 13 14\r
+    cx->n_col = 8   29 23 19 17 14\r
+*/\r
+\r
+#if defined( REDUCE_CODE_SIZE )\r
+#  define ls_box ls_sub\r
+   uint_32t ls_sub(const uint_32t t, const uint_32t n);\r
+#  define inv_mcol im_sub\r
+   uint_32t im_sub(const uint_32t x);\r
+#  ifdef ENC_KS_UNROLL\r
+#    undef ENC_KS_UNROLL\r
+#  endif\r
+#  ifdef DEC_KS_UNROLL\r
+#    undef DEC_KS_UNROLL\r
+#  endif\r
+#endif\r
+\r
+#if (FUNCS_IN_C & ENC_KEYING_IN_C)\r
+\r
+#if defined(AES_128) || defined( AES_VAR )\r
+\r
+#define ke4(k,i) \\r
+{   k[4*(i)+4] = ss[0] ^= ls_box(ss[3],3) ^ t_use(r,c)[i]; \\r
+    k[4*(i)+5] = ss[1] ^= ss[0]; \\r
+    k[4*(i)+6] = ss[2] ^= ss[1]; \\r
+    k[4*(i)+7] = ss[3] ^= ss[2]; \\r
+}\r
+\r
+AES_RETURN aes_encrypt_key128(const unsigned char *key, aes_encrypt_ctx cx[1])\r
+{   uint_32t    ss[4];\r
+\r
+    cx->ks[0] = ss[0] = word_in(key, 0);\r
+    cx->ks[1] = ss[1] = word_in(key, 1);\r
+    cx->ks[2] = ss[2] = word_in(key, 2);\r
+    cx->ks[3] = ss[3] = word_in(key, 3);\r
+\r
+#ifdef ENC_KS_UNROLL\r
+    ke4(cx->ks, 0);  ke4(cx->ks, 1);\r
+    ke4(cx->ks, 2);  ke4(cx->ks, 3);\r
+    ke4(cx->ks, 4);  ke4(cx->ks, 5);\r
+    ke4(cx->ks, 6);  ke4(cx->ks, 7);\r
+    ke4(cx->ks, 8);\r
+#else\r
+    {   uint_32t i;\r
+        for(i = 0; i < 9; ++i)\r
+            ke4(cx->ks, i);\r
+    }\r
+#endif\r
+    ke4(cx->ks, 9);\r
+    cx->inf.l = 0;\r
+    cx->inf.b[0] = 10 * 16;\r
+\r
+#ifdef USE_VIA_ACE_IF_PRESENT\r
+    if(VIA_ACE_AVAILABLE)\r
+        cx->inf.b[1] = 0xff;\r
+#endif\r
+    return EXIT_SUCCESS;\r
+}\r
+\r
+#endif\r
+\r
+#if defined(AES_192) || defined( AES_VAR )\r
+\r
+#define kef6(k,i) \\r
+{   k[6*(i)+ 6] = ss[0] ^= ls_box(ss[5],3) ^ t_use(r,c)[i]; \\r
+    k[6*(i)+ 7] = ss[1] ^= ss[0]; \\r
+    k[6*(i)+ 8] = ss[2] ^= ss[1]; \\r
+    k[6*(i)+ 9] = ss[3] ^= ss[2]; \\r
+}\r
+\r
+#define ke6(k,i) \\r
+{   kef6(k,i); \\r
+    k[6*(i)+10] = ss[4] ^= ss[3]; \\r
+    k[6*(i)+11] = ss[5] ^= ss[4]; \\r
+}\r
+\r
+AES_RETURN aes_encrypt_key192(const unsigned char *key, aes_encrypt_ctx cx[1])\r
+{   uint_32t    ss[6];\r
+\r
+    cx->ks[0] = ss[0] = word_in(key, 0);\r
+    cx->ks[1] = ss[1] = word_in(key, 1);\r
+    cx->ks[2] = ss[2] = word_in(key, 2);\r
+    cx->ks[3] = ss[3] = word_in(key, 3);\r
+    cx->ks[4] = ss[4] = word_in(key, 4);\r
+    cx->ks[5] = ss[5] = word_in(key, 5);\r
+\r
+#ifdef ENC_KS_UNROLL\r
+    ke6(cx->ks, 0);  ke6(cx->ks, 1);\r
+    ke6(cx->ks, 2);  ke6(cx->ks, 3);\r
+    ke6(cx->ks, 4);  ke6(cx->ks, 5);\r
+    ke6(cx->ks, 6);\r
+#else\r
+    {   uint_32t i;\r
+        for(i = 0; i < 7; ++i)\r
+            ke6(cx->ks, i);\r
+    }\r
+#endif\r
+    kef6(cx->ks, 7);\r
+    cx->inf.l = 0;\r
+    cx->inf.b[0] = 12 * 16;\r
+\r
+#ifdef USE_VIA_ACE_IF_PRESENT\r
+    if(VIA_ACE_AVAILABLE)\r
+        cx->inf.b[1] = 0xff;\r
+#endif\r
+    return EXIT_SUCCESS;\r
+}\r
+\r
+#endif\r
+\r
+#if defined(AES_256) || defined( AES_VAR )\r
+\r
+#define kef8(k,i) \\r
+{   k[8*(i)+ 8] = ss[0] ^= ls_box(ss[7],3) ^ t_use(r,c)[i]; \\r
+    k[8*(i)+ 9] = ss[1] ^= ss[0]; \\r
+    k[8*(i)+10] = ss[2] ^= ss[1]; \\r
+    k[8*(i)+11] = ss[3] ^= ss[2]; \\r
+}\r
+\r
+#define ke8(k,i) \\r
+{   kef8(k,i); \\r
+    k[8*(i)+12] = ss[4] ^= ls_box(ss[3],0); \\r
+    k[8*(i)+13] = ss[5] ^= ss[4]; \\r
+    k[8*(i)+14] = ss[6] ^= ss[5]; \\r
+    k[8*(i)+15] = ss[7] ^= ss[6]; \\r
+}\r
+\r
+AES_RETURN aes_encrypt_key256(const unsigned char *key, aes_encrypt_ctx cx[1])\r
+{   uint_32t    ss[8];\r
+\r
+    cx->ks[0] = ss[0] = word_in(key, 0);\r
+    cx->ks[1] = ss[1] = word_in(key, 1);\r
+    cx->ks[2] = ss[2] = word_in(key, 2);\r
+    cx->ks[3] = ss[3] = word_in(key, 3);\r
+    cx->ks[4] = ss[4] = word_in(key, 4);\r
+    cx->ks[5] = ss[5] = word_in(key, 5);\r
+    cx->ks[6] = ss[6] = word_in(key, 6);\r
+    cx->ks[7] = ss[7] = word_in(key, 7);\r
+\r
+#ifdef ENC_KS_UNROLL\r
+    ke8(cx->ks, 0); ke8(cx->ks, 1);\r
+    ke8(cx->ks, 2); ke8(cx->ks, 3);\r
+    ke8(cx->ks, 4); ke8(cx->ks, 5);\r
+#else\r
+    {   uint_32t i;\r
+        for(i = 0; i < 6; ++i)\r
+            ke8(cx->ks,  i);\r
+    }\r
+#endif\r
+    kef8(cx->ks, 6);\r
+    cx->inf.l = 0;\r
+    cx->inf.b[0] = 14 * 16;\r
+\r
+#ifdef USE_VIA_ACE_IF_PRESENT\r
+    if(VIA_ACE_AVAILABLE)\r
+        cx->inf.b[1] = 0xff;\r
+#endif\r
+    return EXIT_SUCCESS;\r
+}\r
+\r
+#endif\r
+\r
+#if defined( AES_VAR )\r
+\r
+AES_RETURN aes_encrypt_key(const unsigned char *key, int key_len, aes_encrypt_ctx cx[1])\r
+{   \r
+    switch(key_len)\r
+    {\r
+    case 16: case 128: return aes_encrypt_key128(key, cx);\r
+    case 24: case 192: return aes_encrypt_key192(key, cx);\r
+    case 32: case 256: return aes_encrypt_key256(key, cx);\r
+    default: return EXIT_FAILURE;\r
+    }\r
+}\r
+\r
+#endif\r
+\r
+#endif\r
+\r
+#if (FUNCS_IN_C & DEC_KEYING_IN_C)\r
+\r
+/* this is used to store the decryption round keys  */\r
+/* in forward or reverse order                      */\r
+\r
+#ifdef AES_REV_DKS\r
+#define v(n,i)  ((n) - (i) + 2 * ((i) & 3))\r
+#else\r
+#define v(n,i)  (i)\r
+#endif\r
+\r
+#if DEC_ROUND == NO_TABLES\r
+#define ff(x)   (x)\r
+#else\r
+#define ff(x)   inv_mcol(x)\r
+#if defined( dec_imvars )\r
+#define d_vars  dec_imvars\r
+#endif\r
+#endif\r
+\r
+#if defined(AES_128) || defined( AES_VAR )\r
+\r
+#define k4e(k,i) \\r
+{   k[v(40,(4*(i))+4)] = ss[0] ^= ls_box(ss[3],3) ^ t_use(r,c)[i]; \\r
+    k[v(40,(4*(i))+5)] = ss[1] ^= ss[0]; \\r
+    k[v(40,(4*(i))+6)] = ss[2] ^= ss[1]; \\r
+    k[v(40,(4*(i))+7)] = ss[3] ^= ss[2]; \\r
+}\r
+\r
+#if 1\r
+\r
+#define kdf4(k,i) \\r
+{   ss[0] = ss[0] ^ ss[2] ^ ss[1] ^ ss[3]; \\r
+    ss[1] = ss[1] ^ ss[3]; \\r
+    ss[2] = ss[2] ^ ss[3]; \\r
+    ss[4] = ls_box(ss[(i+3) % 4], 3) ^ t_use(r,c)[i]; \\r
+    ss[i % 4] ^= ss[4]; \\r
+    ss[4] ^= k[v(40,(4*(i)))];   k[v(40,(4*(i))+4)] = ff(ss[4]); \\r
+    ss[4] ^= k[v(40,(4*(i))+1)]; k[v(40,(4*(i))+5)] = ff(ss[4]); \\r
+    ss[4] ^= k[v(40,(4*(i))+2)]; k[v(40,(4*(i))+6)] = ff(ss[4]); \\r
+    ss[4] ^= k[v(40,(4*(i))+3)]; k[v(40,(4*(i))+7)] = ff(ss[4]); \\r
+}\r
+\r
+#define kd4(k,i) \\r
+{   ss[4] = ls_box(ss[(i+3) % 4], 3) ^ t_use(r,c)[i]; \\r
+    ss[i % 4] ^= ss[4]; ss[4] = ff(ss[4]); \\r
+    k[v(40,(4*(i))+4)] = ss[4] ^= k[v(40,(4*(i)))]; \\r
+    k[v(40,(4*(i))+5)] = ss[4] ^= k[v(40,(4*(i))+1)]; \\r
+    k[v(40,(4*(i))+6)] = ss[4] ^= k[v(40,(4*(i))+2)]; \\r
+    k[v(40,(4*(i))+7)] = ss[4] ^= k[v(40,(4*(i))+3)]; \\r
+}\r
+\r
+#define kdl4(k,i) \\r
+{   ss[4] = ls_box(ss[(i+3) % 4], 3) ^ t_use(r,c)[i]; ss[i % 4] ^= ss[4]; \\r
+    k[v(40,(4*(i))+4)] = (ss[0] ^= ss[1]) ^ ss[2] ^ ss[3]; \\r
+    k[v(40,(4*(i))+5)] = ss[1] ^ ss[3]; \\r
+    k[v(40,(4*(i))+6)] = ss[0]; \\r
+    k[v(40,(4*(i))+7)] = ss[1]; \\r
+}\r
+\r
+#else\r
+\r
+#define kdf4(k,i) \\r
+{   ss[0] ^= ls_box(ss[3],3) ^ t_use(r,c)[i]; k[v(40,(4*(i))+ 4)] = ff(ss[0]); \\r
+    ss[1] ^= ss[0]; k[v(40,(4*(i))+ 5)] = ff(ss[1]); \\r
+    ss[2] ^= ss[1]; k[v(40,(4*(i))+ 6)] = ff(ss[2]); \\r
+    ss[3] ^= ss[2]; k[v(40,(4*(i))+ 7)] = ff(ss[3]); \\r
+}\r
+\r
+#define kd4(k,i) \\r
+{   ss[4] = ls_box(ss[3],3) ^ t_use(r,c)[i]; \\r
+    ss[0] ^= ss[4]; ss[4] = ff(ss[4]); k[v(40,(4*(i))+ 4)] = ss[4] ^= k[v(40,(4*(i)))]; \\r
+    ss[1] ^= ss[0]; k[v(40,(4*(i))+ 5)] = ss[4] ^= k[v(40,(4*(i))+ 1)]; \\r
+    ss[2] ^= ss[1]; k[v(40,(4*(i))+ 6)] = ss[4] ^= k[v(40,(4*(i))+ 2)]; \\r
+    ss[3] ^= ss[2]; k[v(40,(4*(i))+ 7)] = ss[4] ^= k[v(40,(4*(i))+ 3)]; \\r
+}\r
+\r
+#define kdl4(k,i) \\r
+{   ss[0] ^= ls_box(ss[3],3) ^ t_use(r,c)[i]; k[v(40,(4*(i))+ 4)] = ss[0]; \\r
+    ss[1] ^= ss[0]; k[v(40,(4*(i))+ 5)] = ss[1]; \\r
+    ss[2] ^= ss[1]; k[v(40,(4*(i))+ 6)] = ss[2]; \\r
+    ss[3] ^= ss[2]; k[v(40,(4*(i))+ 7)] = ss[3]; \\r
+}\r
+\r
+#endif\r
+\r
+AES_RETURN aes_decrypt_key128(const unsigned char *key, aes_decrypt_ctx cx[1])\r
+{   uint_32t    ss[5];\r
+#if defined( d_vars )\r
+        d_vars;\r
+#endif\r
+    cx->ks[v(40,(0))] = ss[0] = word_in(key, 0);\r
+    cx->ks[v(40,(1))] = ss[1] = word_in(key, 1);\r
+    cx->ks[v(40,(2))] = ss[2] = word_in(key, 2);\r
+    cx->ks[v(40,(3))] = ss[3] = word_in(key, 3);\r
+\r
+#ifdef DEC_KS_UNROLL\r
+     kdf4(cx->ks, 0); kd4(cx->ks, 1);\r
+     kd4(cx->ks, 2);  kd4(cx->ks, 3);\r
+     kd4(cx->ks, 4);  kd4(cx->ks, 5);\r
+     kd4(cx->ks, 6);  kd4(cx->ks, 7);\r
+     kd4(cx->ks, 8);  kdl4(cx->ks, 9);\r
+#else\r
+    {   uint_32t i;\r
+        for(i = 0; i < 10; ++i)\r
+            k4e(cx->ks, i);\r
+#if !(DEC_ROUND == NO_TABLES)\r
+        for(i = N_COLS; i < 10 * N_COLS; ++i)\r
+            cx->ks[i] = inv_mcol(cx->ks[i]);\r
+#endif\r
+    }\r
+#endif\r
+    cx->inf.l = 0;\r
+    cx->inf.b[0] = 10 * 16;\r
+\r
+#ifdef USE_VIA_ACE_IF_PRESENT\r
+    if(VIA_ACE_AVAILABLE)\r
+        cx->inf.b[1] = 0xff;\r
+#endif\r
+    return EXIT_SUCCESS;\r
+}\r
+\r
+#endif\r
+\r
+#if defined(AES_192) || defined( AES_VAR )\r
+\r
+#define k6ef(k,i) \\r
+{   k[v(48,(6*(i))+ 6)] = ss[0] ^= ls_box(ss[5],3) ^ t_use(r,c)[i]; \\r
+    k[v(48,(6*(i))+ 7)] = ss[1] ^= ss[0]; \\r
+    k[v(48,(6*(i))+ 8)] = ss[2] ^= ss[1]; \\r
+    k[v(48,(6*(i))+ 9)] = ss[3] ^= ss[2]; \\r
+}\r
+\r
+#define k6e(k,i) \\r
+{   k6ef(k,i); \\r
+    k[v(48,(6*(i))+10)] = ss[4] ^= ss[3]; \\r
+    k[v(48,(6*(i))+11)] = ss[5] ^= ss[4]; \\r
+}\r
+\r
+#define kdf6(k,i) \\r
+{   ss[0] ^= ls_box(ss[5],3) ^ t_use(r,c)[i]; k[v(48,(6*(i))+ 6)] = ff(ss[0]); \\r
+    ss[1] ^= ss[0]; k[v(48,(6*(i))+ 7)] = ff(ss[1]); \\r
+    ss[2] ^= ss[1]; k[v(48,(6*(i))+ 8)] = ff(ss[2]); \\r
+    ss[3] ^= ss[2]; k[v(48,(6*(i))+ 9)] = ff(ss[3]); \\r
+    ss[4] ^= ss[3]; k[v(48,(6*(i))+10)] = ff(ss[4]); \\r
+    ss[5] ^= ss[4]; k[v(48,(6*(i))+11)] = ff(ss[5]); \\r
+}\r
+\r
+#define kd6(k,i) \\r
+{   ss[6] = ls_box(ss[5],3) ^ t_use(r,c)[i]; \\r
+    ss[0] ^= ss[6]; ss[6] = ff(ss[6]); k[v(48,(6*(i))+ 6)] = ss[6] ^= k[v(48,(6*(i)))]; \\r
+    ss[1] ^= ss[0]; k[v(48,(6*(i))+ 7)] = ss[6] ^= k[v(48,(6*(i))+ 1)]; \\r
+    ss[2] ^= ss[1]; k[v(48,(6*(i))+ 8)] = ss[6] ^= k[v(48,(6*(i))+ 2)]; \\r
+    ss[3] ^= ss[2]; k[v(48,(6*(i))+ 9)] = ss[6] ^= k[v(48,(6*(i))+ 3)]; \\r
+    ss[4] ^= ss[3]; k[v(48,(6*(i))+10)] = ss[6] ^= k[v(48,(6*(i))+ 4)]; \\r
+    ss[5] ^= ss[4]; k[v(48,(6*(i))+11)] = ss[6] ^= k[v(48,(6*(i))+ 5)]; \\r
+}\r
+\r
+#define kdl6(k,i) \\r
+{   ss[0] ^= ls_box(ss[5],3) ^ t_use(r,c)[i]; k[v(48,(6*(i))+ 6)] = ss[0]; \\r
+    ss[1] ^= ss[0]; k[v(48,(6*(i))+ 7)] = ss[1]; \\r
+    ss[2] ^= ss[1]; k[v(48,(6*(i))+ 8)] = ss[2]; \\r
+    ss[3] ^= ss[2]; k[v(48,(6*(i))+ 9)] = ss[3]; \\r
+}\r
+\r
+AES_RETURN aes_decrypt_key192(const unsigned char *key, aes_decrypt_ctx cx[1])\r
+{   uint_32t    ss[7];\r
+#if defined( d_vars )\r
+        d_vars;\r
+#endif\r
+    cx->ks[v(48,(0))] = ss[0] = word_in(key, 0);\r
+    cx->ks[v(48,(1))] = ss[1] = word_in(key, 1);\r
+    cx->ks[v(48,(2))] = ss[2] = word_in(key, 2);\r
+    cx->ks[v(48,(3))] = ss[3] = word_in(key, 3);\r
+\r
+#ifdef DEC_KS_UNROLL\r
+    cx->ks[v(48,(4))] = ff(ss[4] = word_in(key, 4));\r
+    cx->ks[v(48,(5))] = ff(ss[5] = word_in(key, 5));\r
+    kdf6(cx->ks, 0); kd6(cx->ks, 1);\r
+    kd6(cx->ks, 2);  kd6(cx->ks, 3);\r
+    kd6(cx->ks, 4);  kd6(cx->ks, 5);\r
+    kd6(cx->ks, 6);  kdl6(cx->ks, 7);\r
+#else\r
+    cx->ks[v(48,(4))] = ss[4] = word_in(key, 4);\r
+    cx->ks[v(48,(5))] = ss[5] = word_in(key, 5);\r
+    {   uint_32t i;\r
+\r
+        for(i = 0; i < 7; ++i)\r
+            k6e(cx->ks, i);\r
+        k6ef(cx->ks, 7);\r
+#if !(DEC_ROUND == NO_TABLES)\r
+        for(i = N_COLS; i < 12 * N_COLS; ++i)\r
+            cx->ks[i] = inv_mcol(cx->ks[i]);\r
+#endif\r
+    }\r
+#endif\r
+    cx->inf.l = 0;\r
+    cx->inf.b[0] = 12 * 16;\r
+\r
+#ifdef USE_VIA_ACE_IF_PRESENT\r
+    if(VIA_ACE_AVAILABLE)\r
+        cx->inf.b[1] = 0xff;\r
+#endif\r
+    return EXIT_SUCCESS;\r
+}\r
+\r
+#endif\r
+\r
+#if defined(AES_256) || defined( AES_VAR )\r
+\r
+#define k8ef(k,i) \\r
+{   k[v(56,(8*(i))+ 8)] = ss[0] ^= ls_box(ss[7],3) ^ t_use(r,c)[i]; \\r
+    k[v(56,(8*(i))+ 9)] = ss[1] ^= ss[0]; \\r
+    k[v(56,(8*(i))+10)] = ss[2] ^= ss[1]; \\r
+    k[v(56,(8*(i))+11)] = ss[3] ^= ss[2]; \\r
+}\r
+\r
+#define k8e(k,i) \\r
+{   k8ef(k,i); \\r
+    k[v(56,(8*(i))+12)] = ss[4] ^= ls_box(ss[3],0); \\r
+    k[v(56,(8*(i))+13)] = ss[5] ^= ss[4]; \\r
+    k[v(56,(8*(i))+14)] = ss[6] ^= ss[5]; \\r
+    k[v(56,(8*(i))+15)] = ss[7] ^= ss[6]; \\r
+}\r
+\r
+#define kdf8(k,i) \\r
+{   ss[0] ^= ls_box(ss[7],3) ^ t_use(r,c)[i]; k[v(56,(8*(i))+ 8)] = ff(ss[0]); \\r
+    ss[1] ^= ss[0]; k[v(56,(8*(i))+ 9)] = ff(ss[1]); \\r
+    ss[2] ^= ss[1]; k[v(56,(8*(i))+10)] = ff(ss[2]); \\r
+    ss[3] ^= ss[2]; k[v(56,(8*(i))+11)] = ff(ss[3]); \\r
+    ss[4] ^= ls_box(ss[3],0); k[v(56,(8*(i))+12)] = ff(ss[4]); \\r
+    ss[5] ^= ss[4]; k[v(56,(8*(i))+13)] = ff(ss[5]); \\r
+    ss[6] ^= ss[5]; k[v(56,(8*(i))+14)] = ff(ss[6]); \\r
+    ss[7] ^= ss[6]; k[v(56,(8*(i))+15)] = ff(ss[7]); \\r
+}\r
+\r
+#define kd8(k,i) \\r
+{   ss[8] = ls_box(ss[7],3) ^ t_use(r,c)[i]; \\r
+    ss[0] ^= ss[8]; ss[8] = ff(ss[8]); k[v(56,(8*(i))+ 8)] = ss[8] ^= k[v(56,(8*(i)))]; \\r
+    ss[1] ^= ss[0]; k[v(56,(8*(i))+ 9)] = ss[8] ^= k[v(56,(8*(i))+ 1)]; \\r
+    ss[2] ^= ss[1]; k[v(56,(8*(i))+10)] = ss[8] ^= k[v(56,(8*(i))+ 2)]; \\r
+    ss[3] ^= ss[2]; k[v(56,(8*(i))+11)] = ss[8] ^= k[v(56,(8*(i))+ 3)]; \\r
+    ss[8] = ls_box(ss[3],0); \\r
+    ss[4] ^= ss[8]; ss[8] = ff(ss[8]); k[v(56,(8*(i))+12)] = ss[8] ^= k[v(56,(8*(i))+ 4)]; \\r
+    ss[5] ^= ss[4]; k[v(56,(8*(i))+13)] = ss[8] ^= k[v(56,(8*(i))+ 5)]; \\r
+    ss[6] ^= ss[5]; k[v(56,(8*(i))+14)] = ss[8] ^= k[v(56,(8*(i))+ 6)]; \\r
+    ss[7] ^= ss[6]; k[v(56,(8*(i))+15)] = ss[8] ^= k[v(56,(8*(i))+ 7)]; \\r
+}\r
+\r
+#define kdl8(k,i) \\r
+{   ss[0] ^= ls_box(ss[7],3) ^ t_use(r,c)[i]; k[v(56,(8*(i))+ 8)] = ss[0]; \\r
+    ss[1] ^= ss[0]; k[v(56,(8*(i))+ 9)] = ss[1]; \\r
+    ss[2] ^= ss[1]; k[v(56,(8*(i))+10)] = ss[2]; \\r
+    ss[3] ^= ss[2]; k[v(56,(8*(i))+11)] = ss[3]; \\r
+}\r
+\r
+AES_RETURN aes_decrypt_key256(const unsigned char *key, aes_decrypt_ctx cx[1])\r
+{   uint_32t    ss[9];\r
+#if defined( d_vars )\r
+        d_vars;\r
+#endif\r
+    cx->ks[v(56,(0))] = ss[0] = word_in(key, 0);\r
+    cx->ks[v(56,(1))] = ss[1] = word_in(key, 1);\r
+    cx->ks[v(56,(2))] = ss[2] = word_in(key, 2);\r
+    cx->ks[v(56,(3))] = ss[3] = word_in(key, 3);\r
+\r
+#ifdef DEC_KS_UNROLL\r
+    cx->ks[v(56,(4))] = ff(ss[4] = word_in(key, 4));\r
+    cx->ks[v(56,(5))] = ff(ss[5] = word_in(key, 5));\r
+    cx->ks[v(56,(6))] = ff(ss[6] = word_in(key, 6));\r
+    cx->ks[v(56,(7))] = ff(ss[7] = word_in(key, 7));\r
+    kdf8(cx->ks, 0); kd8(cx->ks, 1);\r
+    kd8(cx->ks, 2);  kd8(cx->ks, 3);\r
+    kd8(cx->ks, 4);  kd8(cx->ks, 5);\r
+    kdl8(cx->ks, 6);\r
+#else\r
+    cx->ks[v(56,(4))] = ss[4] = word_in(key, 4);\r
+    cx->ks[v(56,(5))] = ss[5] = word_in(key, 5);\r
+    cx->ks[v(56,(6))] = ss[6] = word_in(key, 6);\r
+    cx->ks[v(56,(7))] = ss[7] = word_in(key, 7);\r
+    {   uint_32t i;\r
+\r
+        for(i = 0; i < 6; ++i)\r
+            k8e(cx->ks,  i);\r
+        k8ef(cx->ks,  6);\r
+#if !(DEC_ROUND == NO_TABLES)\r
+        for(i = N_COLS; i < 14 * N_COLS; ++i)\r
+            cx->ks[i] = inv_mcol(cx->ks[i]);\r
+#endif\r
+    }\r
+#endif\r
+    cx->inf.l = 0;\r
+    cx->inf.b[0] = 14 * 16;\r
+\r
+#ifdef USE_VIA_ACE_IF_PRESENT\r
+    if(VIA_ACE_AVAILABLE)\r
+        cx->inf.b[1] = 0xff;\r
+#endif\r
+    return EXIT_SUCCESS;\r
+}\r
+\r
+#endif\r
+\r
+#if defined( AES_VAR )\r
+\r
+AES_RETURN aes_decrypt_key(const unsigned char *key, int key_len, aes_decrypt_ctx cx[1])\r
+{\r
+    switch(key_len)\r
+    {\r
+    case 16: case 128: return aes_decrypt_key128(key, cx);\r
+    case 24: case 192: return aes_decrypt_key192(key, cx);\r
+    case 32: case 256: return aes_decrypt_key256(key, cx);\r
+    default: return EXIT_FAILURE;\r
+    }\r
+}\r
+\r
+#endif\r
+\r
+#endif\r
+\r
+#if defined(__cplusplus)\r
+}\r
+#endif\r
diff --git a/pdns/aes/aesopt.h b/pdns/aes/aesopt.h
new file mode 100644 (file)
index 0000000..eb4e956
--- /dev/null
@@ -0,0 +1,746 @@
+/*\r
+ ---------------------------------------------------------------------------\r
+ Copyright (c) 1998-2007, Brian Gladman, Worcester, UK. All rights reserved.\r
+\r
+ LICENSE TERMS\r
+\r
+ The free distribution and use of this software is allowed (with or without\r
+ changes) provided that:\r
+\r
+  1. source code distributions include the above copyright notice, this\r
+     list of conditions and the following disclaimer;\r
+\r
+  2. binary distributions include the above copyright notice, this list\r
+     of conditions and the following disclaimer in their documentation;\r
+\r
+  3. the name of the copyright holder is not used to endorse products\r
+     built using this software without specific written permission.\r
+\r
+ DISCLAIMER\r
+\r
+ This software is provided 'as is' with no explicit or implied warranties\r
+ in respect of its properties, including, but not limited to, correctness\r
+ and/or fitness for purpose.\r
+ ---------------------------------------------------------------------------\r
+ Issue Date: 20/12/2007\r
+\r
+ This file contains the compilation options for AES (Rijndael) and code\r
+ that is common across encryption, key scheduling and table generation.\r
+\r
+ OPERATION\r
+\r
+ These source code files implement the AES algorithm Rijndael designed by\r
+ Joan Daemen and Vincent Rijmen. This version is designed for the standard\r
+ block size of 16 bytes and for key sizes of 128, 192 and 256 bits (16, 24\r
+ and 32 bytes).\r
+\r
+ This version is designed for flexibility and speed using operations on\r
+ 32-bit words rather than operations on bytes.  It can be compiled with\r
+ either big or little endian internal byte order but is faster when the\r
+ native byte order for the processor is used.\r
+\r
+ THE CIPHER INTERFACE\r
+\r
+ The cipher interface is implemented as an array of bytes in which lower\r
+ AES bit sequence indexes map to higher numeric significance within bytes.\r
+\r
+  uint_8t                 (an unsigned  8-bit type)\r
+  uint_32t                (an unsigned 32-bit type)\r
+  struct aes_encrypt_ctx  (structure for the cipher encryption context)\r
+  struct aes_decrypt_ctx  (structure for the cipher decryption context)\r
+  AES_RETURN                the function return type\r
+\r
+  C subroutine calls:\r
+\r
+  AES_RETURN aes_encrypt_key128(const unsigned char *key, aes_encrypt_ctx cx[1]);\r
+  AES_RETURN aes_encrypt_key192(const unsigned char *key, aes_encrypt_ctx cx[1]);\r
+  AES_RETURN aes_encrypt_key256(const unsigned char *key, aes_encrypt_ctx cx[1]);\r
+  AES_RETURN aes_encrypt(const unsigned char *in, unsigned char *out,\r
+                                                  const aes_encrypt_ctx cx[1]);\r
+\r
+  AES_RETURN aes_decrypt_key128(const unsigned char *key, aes_decrypt_ctx cx[1]);\r
+  AES_RETURN aes_decrypt_key192(const unsigned char *key, aes_decrypt_ctx cx[1]);\r
+  AES_RETURN aes_decrypt_key256(const unsigned char *key, aes_decrypt_ctx cx[1]);\r
+  AES_RETURN aes_decrypt(const unsigned char *in, unsigned char *out,\r
+                                                  const aes_decrypt_ctx cx[1]);\r
+\r
+ IMPORTANT NOTE: If you are using this C interface with dynamic tables make sure that\r
+ you call aes_init() before AES is used so that the tables are initialised.\r
+\r
+ C++ aes class subroutines:\r
+\r
+     Class AESencrypt  for encryption\r
+\r
+      Construtors:\r
+          AESencrypt(void)\r
+          AESencrypt(const unsigned char *key) - 128 bit key\r
+      Members:\r
+          AES_RETURN key128(const unsigned char *key)\r
+          AES_RETURN key192(const unsigned char *key)\r
+          AES_RETURN key256(const unsigned char *key)\r
+          AES_RETURN encrypt(const unsigned char *in, unsigned char *out) const\r
+\r
+      Class AESdecrypt  for encryption\r
+      Construtors:\r
+          AESdecrypt(void)\r
+          AESdecrypt(const unsigned char *key) - 128 bit key\r
+      Members:\r
+          AES_RETURN key128(const unsigned char *key)\r
+          AES_RETURN key192(const unsigned char *key)\r
+          AES_RETURN key256(const unsigned char *key)\r
+          AES_RETURN decrypt(const unsigned char *in, unsigned char *out) const\r
+*/\r
+\r
+#if !defined( _AESOPT_H )\r
+#define _AESOPT_H\r
+\r
+#if defined( __cplusplus )\r
+#include "aescpp.h"\r
+#else\r
+#include "aes.h"\r
+#endif\r
+\r
+/*  PLATFORM SPECIFIC INCLUDES */\r
+\r
+#include "brg_endian.h"\r
+\r
+/*  CONFIGURATION - THE USE OF DEFINES\r
+\r
+    Later in this section there are a number of defines that control the\r
+    operation of the code.  In each section, the purpose of each define is\r
+    explained so that the relevant form can be included or excluded by\r
+    setting either 1's or 0's respectively on the branches of the related\r
+    #if clauses.  The following local defines should not be changed.\r
+*/\r
+\r
+#define ENCRYPTION_IN_C     1\r
+#define DECRYPTION_IN_C     2\r
+#define ENC_KEYING_IN_C     4\r
+#define DEC_KEYING_IN_C     8\r
+\r
+#define NO_TABLES           0\r
+#define ONE_TABLE           1\r
+#define FOUR_TABLES         4\r
+#define NONE                0\r
+#define PARTIAL             1\r
+#define FULL                2\r
+\r
+/*  --- START OF USER CONFIGURED OPTIONS --- */\r
+\r
+/*  1. BYTE ORDER WITHIN 32 BIT WORDS\r
+\r
+    The fundamental data processing units in Rijndael are 8-bit bytes. The\r
+    input, output and key input are all enumerated arrays of bytes in which\r
+    bytes are numbered starting at zero and increasing to one less than the\r
+    number of bytes in the array in question. This enumeration is only used\r
+    for naming bytes and does not imply any adjacency or order relationship\r
+    from one byte to another. When these inputs and outputs are considered\r
+    as bit sequences, bits 8*n to 8*n+7 of the bit sequence are mapped to\r
+    byte[n] with bit 8n+i in the sequence mapped to bit 7-i within the byte.\r
+    In this implementation bits are numbered from 0 to 7 starting at the\r
+    numerically least significant end of each byte (bit n represents 2^n).\r
+\r
+    However, Rijndael can be implemented more efficiently using 32-bit\r
+    words by packing bytes into words so that bytes 4*n to 4*n+3 are placed\r
+    into word[n]. While in principle these bytes can be assembled into words\r
+    in any positions, this implementation only supports the two formats in\r
+    which bytes in adjacent positions within words also have adjacent byte\r
+    numbers. This order is called big-endian if the lowest numbered bytes\r
+    in words have the highest numeric significance and little-endian if the\r
+    opposite applies.\r
+\r
+    This code can work in either order irrespective of the order used by the\r
+    machine on which it runs. Normally the internal byte order will be set\r
+    to the order of the processor on which the code is to be run but this\r
+    define can be used to reverse this in special situations\r
+\r
+    WARNING: Assembler code versions rely on PLATFORM_BYTE_ORDER being set.\r
+    This define will hence be redefined later (in section 4) if necessary\r
+*/\r
+\r
+#if 1\r
+#  define ALGORITHM_BYTE_ORDER PLATFORM_BYTE_ORDER\r
+#elif 0\r
+#  define ALGORITHM_BYTE_ORDER IS_LITTLE_ENDIAN\r
+#elif 0\r
+#  define ALGORITHM_BYTE_ORDER IS_BIG_ENDIAN\r
+#else\r
+#  error The algorithm byte order is not defined\r
+#endif\r
+\r
+/*  2. VIA ACE SUPPORT */\r
+\r
+#if defined( __GNUC__ ) && defined( __i386__ ) \\r
+ || defined( _WIN32   ) && defined( _M_IX86  ) \\r
+ && !(defined( _WIN64 ) || defined( _WIN32_WCE ) || defined( _MSC_VER ) && ( _MSC_VER <= 800 ))\r
+#  define VIA_ACE_POSSIBLE\r
+#endif\r
+\r
+/*  Define this option if support for the VIA ACE is required. This uses\r
+    inline assembler instructions and is only implemented for the Microsoft,\r
+    Intel and GCC compilers.  If VIA ACE is known to be present, then defining\r
+    ASSUME_VIA_ACE_PRESENT will remove the ordinary encryption/decryption\r
+    code.  If USE_VIA_ACE_IF_PRESENT is defined then VIA ACE will be used if\r
+    it is detected (both present and enabled) but the normal AES code will\r
+    also be present.\r
+\r
+    When VIA ACE is to be used, all AES encryption contexts MUST be 16 byte\r
+    aligned; other input/output buffers do not need to be 16 byte aligned\r
+    but there are very large performance gains if this can be arranged.\r
+    VIA ACE also requires the decryption key schedule to be in reverse\r
+    order (which later checks below ensure).\r
+*/\r
+\r
+#if 0 && defined( VIA_ACE_POSSIBLE ) && !defined( USE_VIA_ACE_IF_PRESENT )\r
+#  define USE_VIA_ACE_IF_PRESENT\r
+#endif\r
+\r
+#if 0 && defined( VIA_ACE_POSSIBLE ) && !defined( ASSUME_VIA_ACE_PRESENT )\r
+#  define ASSUME_VIA_ACE_PRESENT\r
+#  endif\r
+\r
+/*  3. ASSEMBLER SUPPORT\r
+\r
+    This define (which can be on the command line) enables the use of the\r
+    assembler code routines for encryption, decryption and key scheduling\r
+    as follows:\r
+\r
+    ASM_X86_V1C uses the assembler (aes_x86_v1.asm) with large tables for\r
+                encryption and decryption and but with key scheduling in C\r
+    ASM_X86_V2  uses assembler (aes_x86_v2.asm) with compressed tables for\r
+                encryption, decryption and key scheduling\r
+    ASM_X86_V2C uses assembler (aes_x86_v2.asm) with compressed tables for\r
+                encryption and decryption and but with key scheduling in C\r
+    ASM_AMD64_C uses assembler (aes_amd64.asm) with compressed tables for\r
+                encryption and decryption and but with key scheduling in C\r
+\r
+    Change one 'if 0' below to 'if 1' to select the version or define\r
+    as a compilation option.\r
+*/\r
+\r
+#if 0 && !defined( ASM_X86_V1C )\r
+#  define ASM_X86_V1C\r
+#elif 0 && !defined( ASM_X86_V2  )\r
+#  define ASM_X86_V2\r
+#elif 0 && !defined( ASM_X86_V2C )\r
+#  define ASM_X86_V2C\r
+#elif 0 && !defined( ASM_AMD64_C )\r
+#  define ASM_AMD64_C\r
+#endif\r
+\r
+#if (defined ( ASM_X86_V1C ) || defined( ASM_X86_V2 ) || defined( ASM_X86_V2C )) \\r
+      && !defined( _M_IX86 ) || defined( ASM_AMD64_C ) && !defined( _M_X64 )\r
+#  error Assembler code is only available for x86 and AMD64 systems\r
+#endif\r
+\r
+/*  4. FAST INPUT/OUTPUT OPERATIONS.\r
+\r
+    On some machines it is possible to improve speed by transferring the\r
+    bytes in the input and output arrays to and from the internal 32-bit\r
+    variables by addressing these arrays as if they are arrays of 32-bit\r
+    words.  On some machines this will always be possible but there may\r
+    be a large performance penalty if the byte arrays are not aligned on\r
+    the normal word boundaries. On other machines this technique will\r
+    lead to memory access errors when such 32-bit word accesses are not\r
+    properly aligned. The option SAFE_IO avoids such problems but will\r
+    often be slower on those machines that support misaligned access\r
+    (especially so if care is taken to align the input  and output byte\r
+    arrays on 32-bit word boundaries). If SAFE_IO is not defined it is\r
+    assumed that access to byte arrays as if they are arrays of 32-bit\r
+    words will not cause problems when such accesses are misaligned.\r
+*/\r
+#if 1 && !defined( _MSC_VER )\r
+#  define SAFE_IO\r
+#endif\r
+\r
+/*  5. LOOP UNROLLING\r
+\r
+    The code for encryption and decrytpion cycles through a number of rounds\r
+    that can be implemented either in a loop or by expanding the code into a\r
+    long sequence of instructions, the latter producing a larger program but\r
+    one that will often be much faster. The latter is called loop unrolling.\r
+    There are also potential speed advantages in expanding two iterations in\r
+    a loop with half the number of iterations, which is called partial loop\r
+    unrolling.  The following options allow partial or full loop unrolling\r
+    to be set independently for encryption and decryption\r
+*/\r
+#if 1\r
+#  define ENC_UNROLL  FULL\r
+#elif 0\r
+#  define ENC_UNROLL  PARTIAL\r
+#else\r
+#  define ENC_UNROLL  NONE\r
+#endif\r
+\r
+#if 1\r
+#  define DEC_UNROLL  FULL\r
+#elif 0\r
+#  define DEC_UNROLL  PARTIAL\r
+#else\r
+#  define DEC_UNROLL  NONE\r
+#endif\r
+\r
+#if 1\r
+#  define ENC_KS_UNROLL\r
+#endif\r
+\r
+#if 1\r
+#  define DEC_KS_UNROLL\r
+#endif\r
+\r
+/*  6. FAST FINITE FIELD OPERATIONS\r
+\r
+    If this section is included, tables are used to provide faster finite\r
+    field arithmetic (this has no effect if FIXED_TABLES is defined).\r
+*/\r
+#if 1\r
+#  define FF_TABLES\r
+#endif\r
+\r
+/*  7. INTERNAL STATE VARIABLE FORMAT\r
+\r
+    The internal state of Rijndael is stored in a number of local 32-bit\r
+    word varaibles which can be defined either as an array or as individual\r
+    names variables. Include this section if you want to store these local\r
+    varaibles in arrays. Otherwise individual local variables will be used.\r
+*/\r
+#if 1\r
+#  define ARRAYS\r
+#endif\r
+\r
+/*  8. FIXED OR DYNAMIC TABLES\r
+\r
+    When this section is included the tables used by the code are compiled\r
+    statically into the binary file.  Otherwise the subroutine aes_init()\r
+    must be called to compute them before the code is first used.\r
+*/\r
+#if 1 && !(defined( _MSC_VER ) && ( _MSC_VER <= 800 ))\r
+#  define FIXED_TABLES\r
+#endif\r
+\r
+/*  9. MASKING OR CASTING FROM LONGER VALUES TO BYTES\r
+\r
+    In some systems it is better to mask longer values to extract bytes \r
+    rather than using a cast. This option allows this choice.\r
+*/\r
+#if 0\r
+#  define to_byte(x)  ((uint_8t)(x))\r
+#else\r
+#  define to_byte(x)  ((x) & 0xff)\r
+#endif\r
+\r
+/*  10. TABLE ALIGNMENT\r
+\r
+    On some sytsems speed will be improved by aligning the AES large lookup\r
+    tables on particular boundaries. This define should be set to a power of\r
+    two giving the desired alignment. It can be left undefined if alignment\r
+    is not needed.  This option is specific to the Microsft VC++ compiler -\r
+    it seems to sometimes cause trouble for the VC++ version 6 compiler.\r
+*/\r
+\r
+#if 1 && defined( _MSC_VER ) && ( _MSC_VER >= 1300 )\r
+#  define TABLE_ALIGN 32\r
+#endif\r
+\r
+/*  11.  REDUCE CODE AND TABLE SIZE\r
+\r
+    This replaces some expanded macros with function calls if AES_ASM_V2 or\r
+    AES_ASM_V2C are defined\r
+*/\r
+\r
+#if 1 && (defined( ASM_X86_V2 ) || defined( ASM_X86_V2C ))\r
+#  define REDUCE_CODE_SIZE\r
+#endif\r
+\r
+/*  12. TABLE OPTIONS\r
+\r
+    This cipher proceeds by repeating in a number of cycles known as 'rounds'\r
+    which are implemented by a round function which can optionally be speeded\r
+    up using tables.  The basic tables are each 256 32-bit words, with either\r
+    one or four tables being required for each round function depending on\r
+    how much speed is required. The encryption and decryption round functions\r
+    are different and the last encryption and decrytpion round functions are\r
+    different again making four different round functions in all.\r
+\r
+    This means that:\r
+      1. Normal encryption and decryption rounds can each use either 0, 1\r
+         or 4 tables and table spaces of 0, 1024 or 4096 bytes each.\r
+      2. The last encryption and decryption rounds can also use either 0, 1\r
+         or 4 tables and table spaces of 0, 1024 or 4096 bytes each.\r
+\r
+    Include or exclude the appropriate definitions below to set the number\r
+    of tables used by this implementation.\r
+*/\r
+\r
+#if 1   /* set tables for the normal encryption round */\r
+#  define ENC_ROUND   FOUR_TABLES\r
+#elif 0\r
+#  define ENC_ROUND   ONE_TABLE\r
+#else\r
+#  define ENC_ROUND   NO_TABLES\r
+#endif\r
+\r
+#if 1   /* set tables for the last encryption round */\r
+#  define LAST_ENC_ROUND  FOUR_TABLES\r
+#elif 0\r
+#  define LAST_ENC_ROUND  ONE_TABLE\r
+#else\r
+#  define LAST_ENC_ROUND  NO_TABLES\r
+#endif\r
+\r
+#if 1   /* set tables for the normal decryption round */\r
+#  define DEC_ROUND   FOUR_TABLES\r
+#elif 0\r
+#  define DEC_ROUND   ONE_TABLE\r
+#else\r
+#  define DEC_ROUND   NO_TABLES\r
+#endif\r
+\r
+#if 1   /* set tables for the last decryption round */\r
+#  define LAST_DEC_ROUND  FOUR_TABLES\r
+#elif 0\r
+#  define LAST_DEC_ROUND  ONE_TABLE\r
+#else\r
+#  define LAST_DEC_ROUND  NO_TABLES\r
+#endif\r
+\r
+/*  The decryption key schedule can be speeded up with tables in the same\r
+    way that the round functions can.  Include or exclude the following\r
+    defines to set this requirement.\r
+*/\r
+#if 1\r
+#  define KEY_SCHED   FOUR_TABLES\r
+#elif 0\r
+#  define KEY_SCHED   ONE_TABLE\r
+#else\r
+#  define KEY_SCHED   NO_TABLES\r
+#endif\r
+\r
+/*  ---- END OF USER CONFIGURED OPTIONS ---- */\r
+\r
+/* VIA ACE support is only available for VC++ and GCC */\r
+\r
+#if !defined( _MSC_VER ) && !defined( __GNUC__ )\r
+#  if defined( ASSUME_VIA_ACE_PRESENT )\r
+#    undef ASSUME_VIA_ACE_PRESENT\r
+#  endif\r
+#  if defined( USE_VIA_ACE_IF_PRESENT )\r
+#    undef USE_VIA_ACE_IF_PRESENT\r
+#  endif\r
+#endif\r
+\r
+#if defined( ASSUME_VIA_ACE_PRESENT ) && !defined( USE_VIA_ACE_IF_PRESENT )\r
+#  define USE_VIA_ACE_IF_PRESENT\r
+#endif\r
+\r
+#if defined( USE_VIA_ACE_IF_PRESENT ) && !defined ( AES_REV_DKS )\r
+#  define AES_REV_DKS\r
+#endif\r
+\r
+/* Assembler support requires the use of platform byte order */\r
+\r
+#if ( defined( ASM_X86_V1C ) || defined( ASM_X86_V2C ) || defined( ASM_AMD64_C ) ) \\r
+    && (ALGORITHM_BYTE_ORDER != PLATFORM_BYTE_ORDER)\r
+#  undef  ALGORITHM_BYTE_ORDER\r
+#  define ALGORITHM_BYTE_ORDER PLATFORM_BYTE_ORDER\r
+#endif\r
+\r
+/* In this implementation the columns of the state array are each held in\r
+   32-bit words. The state array can be held in various ways: in an array\r
+   of words, in a number of individual word variables or in a number of\r
+   processor registers. The following define maps a variable name x and\r
+   a column number c to the way the state array variable is to be held.\r
+   The first define below maps the state into an array x[c] whereas the\r
+   second form maps the state into a number of individual variables x0,\r
+   x1, etc.  Another form could map individual state colums to machine\r
+   register names.\r
+*/\r
+\r
+#if defined( ARRAYS )\r
+#  define s(x,c) x[c]\r
+#else\r
+#  define s(x,c) x##c\r
+#endif\r
+\r
+/*  This implementation provides subroutines for encryption, decryption\r
+    and for setting the three key lengths (separately) for encryption\r
+    and decryption. Since not all functions are needed, masks are set\r
+    up here to determine which will be implemented in C\r
+*/\r
+\r
+#if !defined( AES_ENCRYPT )\r
+#  define EFUNCS_IN_C   0\r
+#elif defined( ASSUME_VIA_ACE_PRESENT ) || defined( ASM_X86_V1C ) \\r
+    || defined( ASM_X86_V2C ) || defined( ASM_AMD64_C )\r
+#  define EFUNCS_IN_C   ENC_KEYING_IN_C\r
+#elif !defined( ASM_X86_V2 )\r
+#  define EFUNCS_IN_C   ( ENCRYPTION_IN_C | ENC_KEYING_IN_C )\r
+#else\r
+#  define EFUNCS_IN_C   0\r
+#endif\r
+\r
+#if !defined( AES_DECRYPT )\r
+#  define DFUNCS_IN_C   0\r
+#elif defined( ASSUME_VIA_ACE_PRESENT ) || defined( ASM_X86_V1C ) \\r
+    || defined( ASM_X86_V2C ) || defined( ASM_AMD64_C )\r
+#  define DFUNCS_IN_C   DEC_KEYING_IN_C\r
+#elif !defined( ASM_X86_V2 )\r
+#  define DFUNCS_IN_C   ( DECRYPTION_IN_C | DEC_KEYING_IN_C )\r
+#else\r
+#  define DFUNCS_IN_C   0\r
+#endif\r
+\r
+#define FUNCS_IN_C  ( EFUNCS_IN_C | DFUNCS_IN_C )\r
+\r
+/* END OF CONFIGURATION OPTIONS */\r
+\r
+#define RC_LENGTH   (5 * (AES_BLOCK_SIZE / 4 - 2))\r
+\r
+/* Disable or report errors on some combinations of options */\r
+\r
+#if ENC_ROUND == NO_TABLES && LAST_ENC_ROUND != NO_TABLES\r
+#  undef  LAST_ENC_ROUND\r
+#  define LAST_ENC_ROUND  NO_TABLES\r
+#elif ENC_ROUND == ONE_TABLE && LAST_ENC_ROUND == FOUR_TABLES\r
+#  undef  LAST_ENC_ROUND\r
+#  define LAST_ENC_ROUND  ONE_TABLE\r
+#endif\r
+\r
+#if ENC_ROUND == NO_TABLES && ENC_UNROLL != NONE\r
+#  undef  ENC_UNROLL\r
+#  define ENC_UNROLL  NONE\r
+#endif\r
+\r
+#if DEC_ROUND == NO_TABLES && LAST_DEC_ROUND != NO_TABLES\r
+#  undef  LAST_DEC_ROUND\r
+#  define LAST_DEC_ROUND  NO_TABLES\r
+#elif DEC_ROUND == ONE_TABLE && LAST_DEC_ROUND == FOUR_TABLES\r
+#  undef  LAST_DEC_ROUND\r
+#  define LAST_DEC_ROUND  ONE_TABLE\r
+#endif\r
+\r
+#if DEC_ROUND == NO_TABLES && DEC_UNROLL != NONE\r
+#  undef  DEC_UNROLL\r
+#  define DEC_UNROLL  NONE\r
+#endif\r
+\r
+#if defined( bswap32 )\r
+#  define aes_sw32    bswap32\r
+#elif defined( bswap_32 )\r
+#  define aes_sw32    bswap_32\r
+#else\r
+#  define brot(x,n)   (((uint_32t)(x) <<  n) | ((uint_32t)(x) >> (32 - n)))\r
+#  define aes_sw32(x) ((brot((x),8) & 0x00ff00ff) | (brot((x),24) & 0xff00ff00))\r
+#endif\r
+\r
+/*  upr(x,n):  rotates bytes within words by n positions, moving bytes to\r
+               higher index positions with wrap around into low positions\r
+    ups(x,n):  moves bytes by n positions to higher index positions in\r
+               words but without wrap around\r
+    bval(x,n): extracts a byte from a word\r
+\r
+    WARNING:   The definitions given here are intended only for use with\r
+               unsigned variables and with shift counts that are compile\r
+               time constants\r
+*/\r
+\r
+#if ( ALGORITHM_BYTE_ORDER == IS_LITTLE_ENDIAN )\r
+#  define upr(x,n)      (((uint_32t)(x) << (8 * (n))) | ((uint_32t)(x) >> (32 - 8 * (n))))\r
+#  define ups(x,n)      ((uint_32t) (x) << (8 * (n)))\r
+#  define bval(x,n)     to_byte((x) >> (8 * (n)))\r
+#  define bytes2word(b0, b1, b2, b3)  \\r
+        (((uint_32t)(b3) << 24) | ((uint_32t)(b2) << 16) | ((uint_32t)(b1) << 8) | (b0))\r
+#endif\r
+\r
+#if ( ALGORITHM_BYTE_ORDER == IS_BIG_ENDIAN )\r
+#  define upr(x,n)      (((uint_32t)(x) >> (8 * (n))) | ((uint_32t)(x) << (32 - 8 * (n))))\r
+#  define ups(x,n)      ((uint_32t) (x) >> (8 * (n)))\r
+#  define bval(x,n)     to_byte((x) >> (24 - 8 * (n)))\r
+#  define bytes2word(b0, b1, b2, b3)  \\r
+        (((uint_32t)(b0) << 24) | ((uint_32t)(b1) << 16) | ((uint_32t)(b2) << 8) | (b3))\r
+#endif\r
+\r
+#if defined( SAFE_IO )\r
+#  define word_in(x,c)    bytes2word(((const uint_8t*)(x)+4*c)[0], ((const uint_8t*)(x)+4*c)[1], \\r
+                                   ((const uint_8t*)(x)+4*c)[2], ((const uint_8t*)(x)+4*c)[3])\r
+#  define word_out(x,c,v) { ((uint_8t*)(x)+4*c)[0] = bval(v,0); ((uint_8t*)(x)+4*c)[1] = bval(v,1); \\r
+                          ((uint_8t*)(x)+4*c)[2] = bval(v,2); ((uint_8t*)(x)+4*c)[3] = bval(v,3); }\r
+#elif ( ALGORITHM_BYTE_ORDER == PLATFORM_BYTE_ORDER )\r
+#  define word_in(x,c)    (*((uint_32t*)(x)+(c)))\r
+#  define word_out(x,c,v) (*((uint_32t*)(x)+(c)) = (v))\r
+#else\r
+#  define word_in(x,c)    aes_sw32(*((uint_32t*)(x)+(c)))\r
+#  define word_out(x,c,v) (*((uint_32t*)(x)+(c)) = aes_sw32(v))\r
+#endif\r
+\r
+/* the finite field modular polynomial and elements */\r
+\r
+#define WPOLY   0x011b\r
+#define BPOLY     0x1b\r
+\r
+/* multiply four bytes in GF(2^8) by 'x' {02} in parallel */\r
+\r
+#define m1  0x80808080\r
+#define m2  0x7f7f7f7f\r
+#define gf_mulx(x)  ((((x) & m2) << 1) ^ ((((x) & m1) >> 7) * BPOLY))\r
+\r
+/* The following defines provide alternative definitions of gf_mulx that might\r
+   give improved performance if a fast 32-bit multiply is not available. Note\r
+   that a temporary variable u needs to be defined where gf_mulx is used.\r
+\r
+#define gf_mulx(x) (u = (x) & m1, u |= (u >> 1), ((x) & m2) << 1) ^ ((u >> 3) | (u >> 6))\r
+#define m4  (0x01010101 * BPOLY)\r
+#define gf_mulx(x) (u = (x) & m1, ((x) & m2) << 1) ^ ((u - (u >> 7)) & m4)\r
+*/\r
+\r
+/* Work out which tables are needed for the different options   */\r
+\r
+#if defined( ASM_X86_V1C )\r
+#  if defined( ENC_ROUND )\r
+#    undef  ENC_ROUND\r
+#  endif\r
+#  define ENC_ROUND   FOUR_TABLES\r
+#  if defined( LAST_ENC_ROUND )\r
+#    undef  LAST_ENC_ROUND\r
+#  endif\r
+#  define LAST_ENC_ROUND  FOUR_TABLES\r
+#  if defined( DEC_ROUND )\r
+#    undef  DEC_ROUND\r
+#  endif\r
+#  define DEC_ROUND   FOUR_TABLES\r
+#  if defined( LAST_DEC_ROUND )\r
+#    undef  LAST_DEC_ROUND\r
+#  endif\r
+#  define LAST_DEC_ROUND  FOUR_TABLES\r
+#  if defined( KEY_SCHED )\r
+#    undef  KEY_SCHED\r
+#    define KEY_SCHED   FOUR_TABLES\r
+#  endif\r
+#endif\r
+\r
+#if ( FUNCS_IN_C & ENCRYPTION_IN_C ) || defined( ASM_X86_V1C )\r
+#  if ENC_ROUND == ONE_TABLE\r
+#    define FT1_SET\r
+#  elif ENC_ROUND == FOUR_TABLES\r
+#    define FT4_SET\r
+#  else\r
+#    define SBX_SET\r
+#  endif\r
+#  if LAST_ENC_ROUND == ONE_TABLE\r
+#    define FL1_SET\r
+#  elif LAST_ENC_ROUND == FOUR_TABLES\r
+#    define FL4_SET\r
+#  elif !defined( SBX_SET )\r
+#    define SBX_SET\r
+#  endif\r
+#endif\r
+\r
+#if ( FUNCS_IN_C & DECRYPTION_IN_C ) || defined( ASM_X86_V1C )\r
+#  if DEC_ROUND == ONE_TABLE\r
+#    define IT1_SET\r
+#  elif DEC_ROUND == FOUR_TABLES\r
+#    define IT4_SET\r
+#  else\r
+#    define ISB_SET\r
+#  endif\r
+#  if LAST_DEC_ROUND == ONE_TABLE\r
+#    define IL1_SET\r
+#  elif LAST_DEC_ROUND == FOUR_TABLES\r
+#    define IL4_SET\r
+#  elif !defined(ISB_SET)\r
+#    define ISB_SET\r
+#  endif\r
+#endif\r
+\r
+#if !(defined( REDUCE_CODE_SIZE ) && (defined( ASM_X86_V2 ) || defined( ASM_X86_V2C )))\r
+#  if ((FUNCS_IN_C & ENC_KEYING_IN_C) || (FUNCS_IN_C & DEC_KEYING_IN_C))\r
+#    if KEY_SCHED == ONE_TABLE\r
+#      if !defined( FL1_SET )  && !defined( FL4_SET ) \r
+#        define LS1_SET\r
+#      endif\r
+#    elif KEY_SCHED == FOUR_TABLES\r
+#      if !defined( FL4_SET )\r
+#        define LS4_SET\r
+#      endif\r
+#    elif !defined( SBX_SET )\r
+#      define SBX_SET\r
+#    endif\r
+#  endif\r
+#  if (FUNCS_IN_C & DEC_KEYING_IN_C)\r
+#    if KEY_SCHED == ONE_TABLE\r
+#      define IM1_SET\r
+#    elif KEY_SCHED == FOUR_TABLES\r
+#      define IM4_SET\r
+#    elif !defined( SBX_SET )\r
+#      define SBX_SET\r
+#    endif\r
+#  endif\r
+#endif\r
+\r
+/* generic definitions of Rijndael macros that use tables    */\r
+\r
+#define no_table(x,box,vf,rf,c) bytes2word( \\r
+    box[bval(vf(x,0,c),rf(0,c))], \\r
+    box[bval(vf(x,1,c),rf(1,c))], \\r
+    box[bval(vf(x,2,c),rf(2,c))], \\r
+    box[bval(vf(x,3,c),rf(3,c))])\r
+\r
+#define one_table(x,op,tab,vf,rf,c) \\r
+ (     tab[bval(vf(x,0,c),rf(0,c))] \\r
+  ^ op(tab[bval(vf(x,1,c),rf(1,c))],1) \\r
+  ^ op(tab[bval(vf(x,2,c),rf(2,c))],2) \\r
+  ^ op(tab[bval(vf(x,3,c),rf(3,c))],3))\r
+\r
+#define four_tables(x,tab,vf,rf,c) \\r
+ (  tab[0][bval(vf(x,0,c),rf(0,c))] \\r
+  ^ tab[1][bval(vf(x,1,c),rf(1,c))] \\r
+  ^ tab[2][bval(vf(x,2,c),rf(2,c))] \\r
+  ^ tab[3][bval(vf(x,3,c),rf(3,c))])\r
+\r
+#define vf1(x,r,c)  (x)\r
+#define rf1(r,c)    (r)\r
+#define rf2(r,c)    ((8+r-c)&3)\r
+\r
+/* perform forward and inverse column mix operation on four bytes in long word x in */\r
+/* parallel. NOTE: x must be a simple variable, NOT an expression in these macros.  */\r
+\r
+#if !(defined( REDUCE_CODE_SIZE ) && (defined( ASM_X86_V2 ) || defined( ASM_X86_V2C ))) \r
+\r
+#if defined( FM4_SET )      /* not currently used */\r
+#  define fwd_mcol(x)       four_tables(x,t_use(f,m),vf1,rf1,0)\r
+#elif defined( FM1_SET )    /* not currently used */\r
+#  define fwd_mcol(x)       one_table(x,upr,t_use(f,m),vf1,rf1,0)\r
+#else\r
+#  define dec_fmvars        uint_32t g2\r
+#  define fwd_mcol(x)       (g2 = gf_mulx(x), g2 ^ upr((x) ^ g2, 3) ^ upr((x), 2) ^ upr((x), 1))\r
+#endif\r
+\r
+#if defined( IM4_SET )\r
+#  define inv_mcol(x)       four_tables(x,t_use(i,m),vf1,rf1,0)\r
+#elif defined( IM1_SET )\r
+#  define inv_mcol(x)       one_table(x,upr,t_use(i,m),vf1,rf1,0)\r
+#else\r
+#  define dec_imvars        uint_32t g2, g4, g9\r
+#  define inv_mcol(x)       (g2 = gf_mulx(x), g4 = gf_mulx(g2), g9 = (x) ^ gf_mulx(g4), g4 ^= g9, \\r
+                            (x) ^ g2 ^ g4 ^ upr(g2 ^ g9, 3) ^ upr(g4, 2) ^ upr(g9, 1))\r
+#endif\r
+\r
+#if defined( FL4_SET )\r
+#  define ls_box(x,c)       four_tables(x,t_use(f,l),vf1,rf2,c)\r
+#elif defined( LS4_SET )\r
+#  define ls_box(x,c)       four_tables(x,t_use(l,s),vf1,rf2,c)\r
+#elif defined( FL1_SET )\r
+#  define ls_box(x,c)       one_table(x,upr,t_use(f,l),vf1,rf2,c)\r
+#elif defined( LS1_SET )\r
+#  define ls_box(x,c)       one_table(x,upr,t_use(l,s),vf1,rf2,c)\r
+#else\r
+#  define ls_box(x,c)       no_table(x,t_use(s,box),vf1,rf2,c)\r
+#endif\r
+\r
+#endif\r
+\r
+#if defined( ASM_X86_V1C ) && defined( AES_DECRYPT ) && !defined( ISB_SET )\r
+#  define ISB_SET\r
+#endif\r
+\r
+#endif\r
diff --git a/pdns/aes/aestab.c b/pdns/aes/aestab.c
new file mode 100644 (file)
index 0000000..0f86f87
--- /dev/null
@@ -0,0 +1,383 @@
+/*\r
+ ---------------------------------------------------------------------------\r
+ Copyright (c) 1998-2007, Brian Gladman, Worcester, UK. All rights reserved.\r
+\r
+ LICENSE TERMS\r
+\r
+ The free distribution and use of this software is allowed (with or without\r
+ changes) provided that:\r
+\r
+  1. source code distributions include the above copyright notice, this\r
+     list of conditions and the following disclaimer;\r
+\r
+  2. binary distributions include the above copyright notice, this list\r
+     of conditions and the following disclaimer in their documentation;\r
+\r
+  3. the name of the copyright holder is not used to endorse products\r
+     built using this software without specific written permission.\r
+\r
+ DISCLAIMER\r
+\r
+ This software is provided 'as is' with no explicit or implied warranties\r
+ in respect of its properties, including, but not limited to, correctness\r
+ and/or fitness for purpose.\r
+ ---------------------------------------------------------------------------\r
+ Issue Date: 20/12/2007\r
+*/\r
+\r
+#define DO_TABLES\r
+\r
+#include "aes.h"\r
+#include "aesopt.h"\r
+\r
+#if defined(FIXED_TABLES)\r
+\r
+#define sb_data(w) {\\r
+    w(0x63), w(0x7c), w(0x77), w(0x7b), w(0xf2), w(0x6b), w(0x6f), w(0xc5),\\r
+    w(0x30), w(0x01), w(0x67), w(0x2b), w(0xfe), w(0xd7), w(0xab), w(0x76),\\r
+    w(0xca), w(0x82), w(0xc9), w(0x7d), w(0xfa), w(0x59), w(0x47), w(0xf0),\\r
+    w(0xad), w(0xd4), w(0xa2), w(0xaf), w(0x9c), w(0xa4), w(0x72), w(0xc0),\\r
+    w(0xb7), w(0xfd), w(0x93), w(0x26), w(0x36), w(0x3f), w(0xf7), w(0xcc),\\r
+    w(0x34), w(0xa5), w(0xe5), w(0xf1), w(0x71), w(0xd8), w(0x31), w(0x15),\\r
+    w(0x04), w(0xc7), w(0x23), w(0xc3), w(0x18), w(0x96), w(0x05), w(0x9a),\\r
+    w(0x07), w(0x12), w(0x80), w(0xe2), w(0xeb), w(0x27), w(0xb2), w(0x75),\\r
+    w(0x09), w(0x83), w(0x2c), w(0x1a), w(0x1b), w(0x6e), w(0x5a), w(0xa0),\\r
+    w(0x52), w(0x3b), w(0xd6), w(0xb3), w(0x29), w(0xe3), w(0x2f), w(0x84),\\r
+    w(0x53), w(0xd1), w(0x00), w(0xed), w(0x20), w(0xfc), w(0xb1), w(0x5b),\\r
+    w(0x6a), w(0xcb), w(0xbe), w(0x39), w(0x4a), w(0x4c), w(0x58), w(0xcf),\\r
+    w(0xd0), w(0xef), w(0xaa), w(0xfb), w(0x43), w(0x4d), w(0x33), w(0x85),\\r
+    w(0x45), w(0xf9), w(0x02), w(0x7f), w(0x50), w(0x3c), w(0x9f), w(0xa8),\\r
+    w(0x51), w(0xa3), w(0x40), w(0x8f), w(0x92), w(0x9d), w(0x38), w(0xf5),\\r
+    w(0xbc), w(0xb6), w(0xda), w(0x21), w(0x10), w(0xff), w(0xf3), w(0xd2),\\r
+    w(0xcd), w(0x0c), w(0x13), w(0xec), w(0x5f), w(0x97), w(0x44), w(0x17),\\r
+    w(0xc4), w(0xa7), w(0x7e), w(0x3d), w(0x64), w(0x5d), w(0x19), w(0x73),\\r
+    w(0x60), w(0x81), w(0x4f), w(0xdc), w(0x22), w(0x2a), w(0x90), w(0x88),\\r
+    w(0x46), w(0xee), w(0xb8), w(0x14), w(0xde), w(0x5e), w(0x0b), w(0xdb),\\r
+    w(0xe0), w(0x32), w(0x3a), w(0x0a), w(0x49), w(0x06), w(0x24), w(0x5c),\\r
+    w(0xc2), w(0xd3), w(0xac), w(0x62), w(0x91), w(0x95), w(0xe4), w(0x79),\\r
+    w(0xe7), w(0xc8), w(0x37), w(0x6d), w(0x8d), w(0xd5), w(0x4e), w(0xa9),\\r
+    w(0x6c), w(0x56), w(0xf4), w(0xea), w(0x65), w(0x7a), w(0xae), w(0x08),\\r
+    w(0xba), w(0x78), w(0x25), w(0x2e), w(0x1c), w(0xa6), w(0xb4), w(0xc6),\\r
+    w(0xe8), w(0xdd), w(0x74), w(0x1f), w(0x4b), w(0xbd), w(0x8b), w(0x8a),\\r
+    w(0x70), w(0x3e), w(0xb5), w(0x66), w(0x48), w(0x03), w(0xf6), w(0x0e),\\r
+    w(0x61), w(0x35), w(0x57), w(0xb9), w(0x86), w(0xc1), w(0x1d), w(0x9e),\\r
+    w(0xe1), w(0xf8), w(0x98), w(0x11), w(0x69), w(0xd9), w(0x8e), w(0x94),\\r
+    w(0x9b), w(0x1e), w(0x87), w(0xe9), w(0xce), w(0x55), w(0x28), w(0xdf),\\r
+    w(0x8c), w(0xa1), w(0x89), w(0x0d), w(0xbf), w(0xe6), w(0x42), w(0x68),\\r
+    w(0x41), w(0x99), w(0x2d), w(0x0f), w(0xb0), w(0x54), w(0xbb), w(0x16) }\r
+\r
+#define isb_data(w) {\\r
+    w(0x52), w(0x09), w(0x6a), w(0xd5), w(0x30), w(0x36), w(0xa5), w(0x38),\\r
+    w(0xbf), w(0x40), w(0xa3), w(0x9e), w(0x81), w(0xf3), w(0xd7), w(0xfb),\\r
+    w(0x7c), w(0xe3), w(0x39), w(0x82), w(0x9b), w(0x2f), w(0xff), w(0x87),\\r
+    w(0x34), w(0x8e), w(0x43), w(0x44), w(0xc4), w(0xde), w(0xe9), w(0xcb),\\r
+    w(0x54), w(0x7b), w(0x94), w(0x32), w(0xa6), w(0xc2), w(0x23), w(0x3d),\\r
+    w(0xee), w(0x4c), w(0x95), w(0x0b), w(0x42), w(0xfa), w(0xc3), w(0x4e),\\r
+    w(0x08), w(0x2e), w(0xa1), w(0x66), w(0x28), w(0xd9), w(0x24), w(0xb2),\\r
+    w(0x76), w(0x5b), w(0xa2), w(0x49), w(0x6d), w(0x8b), w(0xd1), w(0x25),\\r
+    w(0x72), w(0xf8), w(0xf6), w(0x64), w(0x86), w(0x68), w(0x98), w(0x16),\\r
+    w(0xd4), w(0xa4), w(0x5c), w(0xcc), w(0x5d), w(0x65), w(0xb6), w(0x92),\\r
+    w(0x6c), w(0x70), w(0x48), w(0x50), w(0xfd), w(0xed), w(0xb9), w(0xda),\\r
+    w(0x5e), w(0x15), w(0x46), w(0x57), w(0xa7), w(0x8d), w(0x9d), w(0x84),\\r
+    w(0x90), w(0xd8), w(0xab), w(0x00), w(0x8c), w(0xbc), w(0xd3), w(0x0a),\\r
+    w(0xf7), w(0xe4), w(0x58), w(0x05), w(0xb8), w(0xb3), w(0x45), w(0x06),\\r
+    w(0xd0), w(0x2c), w(0x1e), w(0x8f), w(0xca), w(0x3f), w(0x0f), w(0x02),\\r
+    w(0xc1), w(0xaf), w(0xbd), w(0x03), w(0x01), w(0x13), w(0x8a), w(0x6b),\\r
+    w(0x3a), w(0x91), w(0x11), w(0x41), w(0x4f), w(0x67), w(0xdc), w(0xea),\\r
+    w(0x97), w(0xf2), w(0xcf), w(0xce), w(0xf0), w(0xb4), w(0xe6), w(0x73),\\r
+    w(0x96), w(0xac), w(0x74), w(0x22), w(0xe7), w(0xad), w(0x35), w(0x85),\\r
+    w(0xe2), w(0xf9), w(0x37), w(0xe8), w(0x1c), w(0x75), w(0xdf), w(0x6e),\\r
+    w(0x47), w(0xf1), w(0x1a), w(0x71), w(0x1d), w(0x29), w(0xc5), w(0x89),\\r
+    w(0x6f), w(0xb7), w(0x62), w(0x0e), w(0xaa), w(0x18), w(0xbe), w(0x1b),\\r
+    w(0xfc), w(0x56), w(0x3e), w(0x4b), w(0xc6), w(0xd2), w(0x79), w(0x20),\\r
+    w(0x9a), w(0xdb), w(0xc0), w(0xfe), w(0x78), w(0xcd), w(0x5a), w(0xf4),\\r
+    w(0x1f), w(0xdd), w(0xa8), w(0x33), w(0x88), w(0x07), w(0xc7), w(0x31),\\r
+    w(0xb1), w(0x12), w(0x10), w(0x59), w(0x27), w(0x80), w(0xec), w(0x5f),\\r
+    w(0x60), w(0x51), w(0x7f), w(0xa9), w(0x19), w(0xb5), w(0x4a), w(0x0d),\\r
+    w(0x2d), w(0xe5), w(0x7a), w(0x9f), w(0x93), w(0xc9), w(0x9c), w(0xef),\\r
+    w(0xa0), w(0xe0), w(0x3b), w(0x4d), w(0xae), w(0x2a), w(0xf5), w(0xb0),\\r
+    w(0xc8), w(0xeb), w(0xbb), w(0x3c), w(0x83), w(0x53), w(0x99), w(0x61),\\r
+    w(0x17), w(0x2b), w(0x04), w(0x7e), w(0xba), w(0x77), w(0xd6), w(0x26),\\r
+    w(0xe1), w(0x69), w(0x14), w(0x63), w(0x55), w(0x21), w(0x0c), w(0x7d) }\r
+\r
+#define mm_data(w) {\\r
+    w(0x00), w(0x01), w(0x02), w(0x03), w(0x04), w(0x05), w(0x06), w(0x07),\\r
+    w(0x08), w(0x09), w(0x0a), w(0x0b), w(0x0c), w(0x0d), w(0x0e), w(0x0f),\\r
+    w(0x10), w(0x11), w(0x12), w(0x13), w(0x14), w(0x15), w(0x16), w(0x17),\\r
+    w(0x18), w(0x19), w(0x1a), w(0x1b), w(0x1c), w(0x1d), w(0x1e), w(0x1f),\\r
+    w(0x20), w(0x21), w(0x22), w(0x23), w(0x24), w(0x25), w(0x26), w(0x27),\\r
+    w(0x28), w(0x29), w(0x2a), w(0x2b), w(0x2c), w(0x2d), w(0x2e), w(0x2f),\\r
+    w(0x30), w(0x31), w(0x32), w(0x33), w(0x34), w(0x35), w(0x36), w(0x37),\\r
+    w(0x38), w(0x39), w(0x3a), w(0x3b), w(0x3c), w(0x3d), w(0x3e), w(0x3f),\\r
+    w(0x40), w(0x41), w(0x42), w(0x43), w(0x44), w(0x45), w(0x46), w(0x47),\\r
+    w(0x48), w(0x49), w(0x4a), w(0x4b), w(0x4c), w(0x4d), w(0x4e), w(0x4f),\\r
+    w(0x50), w(0x51), w(0x52), w(0x53), w(0x54), w(0x55), w(0x56), w(0x57),\\r
+    w(0x58), w(0x59), w(0x5a), w(0x5b), w(0x5c), w(0x5d), w(0x5e), w(0x5f),\\r
+    w(0x60), w(0x61), w(0x62), w(0x63), w(0x64), w(0x65), w(0x66), w(0x67),\\r
+    w(0x68), w(0x69), w(0x6a), w(0x6b), w(0x6c), w(0x6d), w(0x6e), w(0x6f),\\r
+    w(0x70), w(0x71), w(0x72), w(0x73), w(0x74), w(0x75), w(0x76), w(0x77),\\r
+    w(0x78), w(0x79), w(0x7a), w(0x7b), w(0x7c), w(0x7d), w(0x7e), w(0x7f),\\r
+    w(0x80), w(0x81), w(0x82), w(0x83), w(0x84), w(0x85), w(0x86), w(0x87),\\r
+    w(0x88), w(0x89), w(0x8a), w(0x8b), w(0x8c), w(0x8d), w(0x8e), w(0x8f),\\r
+    w(0x90), w(0x91), w(0x92), w(0x93), w(0x94), w(0x95), w(0x96), w(0x97),\\r
+    w(0x98), w(0x99), w(0x9a), w(0x9b), w(0x9c), w(0x9d), w(0x9e), w(0x9f),\\r
+    w(0xa0), w(0xa1), w(0xa2), w(0xa3), w(0xa4), w(0xa5), w(0xa6), w(0xa7),\\r
+    w(0xa8), w(0xa9), w(0xaa), w(0xab), w(0xac), w(0xad), w(0xae), w(0xaf),\\r
+    w(0xb0), w(0xb1), w(0xb2), w(0xb3), w(0xb4), w(0xb5), w(0xb6), w(0xb7),\\r
+    w(0xb8), w(0xb9), w(0xba), w(0xbb), w(0xbc), w(0xbd), w(0xbe), w(0xbf),\\r
+    w(0xc0), w(0xc1), w(0xc2), w(0xc3), w(0xc4), w(0xc5), w(0xc6), w(0xc7),\\r
+    w(0xc8), w(0xc9), w(0xca), w(0xcb), w(0xcc), w(0xcd), w(0xce), w(0xcf),\\r
+    w(0xd0), w(0xd1), w(0xd2), w(0xd3), w(0xd4), w(0xd5), w(0xd6), w(0xd7),\\r
+    w(0xd8), w(0xd9), w(0xda), w(0xdb), w(0xdc), w(0xdd), w(0xde), w(0xdf),\\r
+    w(0xe0), w(0xe1), w(0xe2), w(0xe3), w(0xe4), w(0xe5), w(0xe6), w(0xe7),\\r
+    w(0xe8), w(0xe9), w(0xea), w(0xeb), w(0xec), w(0xed), w(0xee), w(0xef),\\r
+    w(0xf0), w(0xf1), w(0xf2), w(0xf3), w(0xf4), w(0xf5), w(0xf6), w(0xf7),\\r
+    w(0xf8), w(0xf9), w(0xfa), w(0xfb), w(0xfc), w(0xfd), w(0xfe), w(0xff) }\r
+\r
+#define rc_data(w) {\\r
+    w(0x01), w(0x02), w(0x04), w(0x08), w(0x10),w(0x20), w(0x40), w(0x80),\\r
+    w(0x1b), w(0x36) }\r
+\r
+#define h0(x)   (x)\r
+\r
+#define w0(p)   bytes2word(p, 0, 0, 0)\r
+#define w1(p)   bytes2word(0, p, 0, 0)\r
+#define w2(p)   bytes2word(0, 0, p, 0)\r
+#define w3(p)   bytes2word(0, 0, 0, p)\r
+\r
+#define u0(p)   bytes2word(f2(p), p, p, f3(p))\r
+#define u1(p)   bytes2word(f3(p), f2(p), p, p)\r
+#define u2(p)   bytes2word(p, f3(p), f2(p), p)\r
+#define u3(p)   bytes2word(p, p, f3(p), f2(p))\r
+\r
+#define v0(p)   bytes2word(fe(p), f9(p), fd(p), fb(p))\r
+#define v1(p)   bytes2word(fb(p), fe(p), f9(p), fd(p))\r
+#define v2(p)   bytes2word(fd(p), fb(p), fe(p), f9(p))\r
+#define v3(p)   bytes2word(f9(p), fd(p), fb(p), fe(p))\r
+\r
+#endif\r
+\r
+#if defined(FIXED_TABLES) || !defined(FF_TABLES)\r
+\r
+#define f2(x)   ((x<<1) ^ (((x>>7) & 1) * WPOLY))\r
+#define f4(x)   ((x<<2) ^ (((x>>6) & 1) * WPOLY) ^ (((x>>6) & 2) * WPOLY))\r
+#define f8(x)   ((x<<3) ^ (((x>>5) & 1) * WPOLY) ^ (((x>>5) & 2) * WPOLY) \\r
+                        ^ (((x>>5) & 4) * WPOLY))\r
+#define f3(x)   (f2(x) ^ x)\r
+#define f9(x)   (f8(x) ^ x)\r
+#define fb(x)   (f8(x) ^ f2(x) ^ x)\r
+#define fd(x)   (f8(x) ^ f4(x) ^ x)\r
+#define fe(x)   (f8(x) ^ f4(x) ^ f2(x))\r
+\r
+#else\r
+\r
+#define f2(x) ((x) ? pow[log[x] + 0x19] : 0)\r
+#define f3(x) ((x) ? pow[log[x] + 0x01] : 0)\r
+#define f9(x) ((x) ? pow[log[x] + 0xc7] : 0)\r
+#define fb(x) ((x) ? pow[log[x] + 0x68] : 0)\r
+#define fd(x) ((x) ? pow[log[x] + 0xee] : 0)\r
+#define fe(x) ((x) ? pow[log[x] + 0xdf] : 0)\r
+#define fi(x) ((x) ? pow[ 255 - log[x]] : 0)\r
+\r
+#endif\r
+\r
+#include "aestab.h"\r
+\r
+#if defined(__cplusplus)\r
+extern "C"\r
+{\r
+#endif\r
+\r
+#if defined(FIXED_TABLES)\r
+\r
+/* implemented in case of wrong call for fixed tables */\r
+\r
+AES_RETURN aes_init(void)\r
+{\r
+    return EXIT_SUCCESS;\r
+}\r
+\r
+#else   /* dynamic table generation */\r
+\r
+#if !defined(FF_TABLES)\r
+\r
+/*  Generate the tables for the dynamic table option\r
+\r
+    It will generally be sensible to use tables to compute finite\r
+    field multiplies and inverses but where memory is scarse this\r
+    code might sometimes be better. But it only has effect during\r
+    initialisation so its pretty unimportant in overall terms.\r
+*/\r
+\r
+/*  return 2 ^ (n - 1) where n is the bit number of the highest bit\r
+    set in x with x in the range 1 < x < 0x00000200.   This form is\r
+    used so that locals within fi can be bytes rather than words\r
+*/\r
+\r
+static uint_8t hibit(const uint_32t x)\r
+{   uint_8t r = (uint_8t)((x >> 1) | (x >> 2));\r
+\r
+    r |= (r >> 2);\r
+    r |= (r >> 4);\r
+    return (r + 1) >> 1;\r
+}\r
+\r
+/* return the inverse of the finite field element x */\r
+\r
+static uint_8t fi(const uint_8t x)\r
+{   uint_8t p1 = x, p2 = BPOLY, n1 = hibit(x), n2 = 0x80, v1 = 1, v2 = 0;\r
+\r
+    if(x < 2) return x;\r
+\r
+    for(;;)\r
+    {\r
+        if(!n1) return v1;\r
+\r
+        while(n2 >= n1)\r
+        {\r
+            n2 /= n1; p2 ^= p1 * n2; v2 ^= v1 * n2; n2 = hibit(p2);\r
+        }\r
+\r
+        if(!n2) return v2;\r
+\r
+        while(n1 >= n2)\r
+        {\r
+            n1 /= n2; p1 ^= p2 * n1; v1 ^= v2 * n1; n1 = hibit(p1);\r
+        }\r
+    }\r
+}\r
+\r
+#endif\r
+\r
+/* The forward and inverse affine transformations used in the S-box */\r
+\r
+#define fwd_affine(x) \\r
+    (w = (uint_32t)x, w ^= (w<<1)^(w<<2)^(w<<3)^(w<<4), 0x63^(uint_8t)(w^(w>>8)))\r
+\r
+#define inv_affine(x) \\r
+    (w = (uint_32t)x, w = (w<<1)^(w<<3)^(w<<6), 0x05^(uint_8t)(w^(w>>8)))\r
+\r
+static int init = 0;\r
+\r
+AES_RETURN aes_init(void)\r
+{   uint_32t  i, w;\r
+\r
+#if defined(FF_TABLES)\r
+\r
+    uint_8t  pow[512], log[256];\r
+\r
+    if(init)\r
+        return EXIT_SUCCESS;\r
+    /*  log and power tables for GF(2^8) finite field with\r
+        WPOLY as modular polynomial - the simplest primitive\r
+        root is 0x03, used here to generate the tables\r
+    */\r
+\r
+    i = 0; w = 1;\r
+    do\r
+    {\r
+        pow[i] = (uint_8t)w;\r
+        pow[i + 255] = (uint_8t)w;\r
+        log[w] = (uint_8t)i++;\r
+        w ^=  (w << 1) ^ (w & 0x80 ? WPOLY : 0);\r
+    }\r
+    while (w != 1);\r
+\r
+#else\r
+    if(init)\r
+        return EXIT_SUCCESS;\r
+#endif\r
+\r
+    for(i = 0, w = 1; i < RC_LENGTH; ++i)\r
+    {\r
+        t_set(r,c)[i] = bytes2word(w, 0, 0, 0);\r
+        w = f2(w);\r
+    }\r
+\r
+    for(i = 0; i < 256; ++i)\r
+    {   uint_8t    b;\r
+\r
+        b = fwd_affine(fi((uint_8t)i));\r
+        w = bytes2word(f2(b), b, b, f3(b));\r
+\r
+#if defined( SBX_SET )\r
+        t_set(s,box)[i] = b;\r
+#endif\r
+\r
+#if defined( FT1_SET )                 /* tables for a normal encryption round */\r
+        t_set(f,n)[i] = w;\r
+#endif\r
+#if defined( FT4_SET )\r
+        t_set(f,n)[0][i] = w;\r
+        t_set(f,n)[1][i] = upr(w,1);\r
+        t_set(f,n)[2][i] = upr(w,2);\r
+        t_set(f,n)[3][i] = upr(w,3);\r
+#endif\r
+        w = bytes2word(b, 0, 0, 0);\r
+\r
+#if defined( FL1_SET )            /* tables for last encryption round (may also   */\r
+        t_set(f,l)[i] = w;        /* be used in the key schedule)                 */\r
+#endif\r
+#if defined( FL4_SET )\r
+        t_set(f,l)[0][i] = w;\r
+        t_set(f,l)[1][i] = upr(w,1);\r
+        t_set(f,l)[2][i] = upr(w,2);\r
+        t_set(f,l)[3][i] = upr(w,3);\r
+#endif\r
+\r
+#if defined( LS1_SET )                 /* table for key schedule if t_set(f,l) above is*/\r
+        t_set(l,s)[i] = w;      /* not of the required form                     */\r
+#endif\r
+#if defined( LS4_SET )\r
+        t_set(l,s)[0][i] = w;\r
+        t_set(l,s)[1][i] = upr(w,1);\r
+        t_set(l,s)[2][i] = upr(w,2);\r
+        t_set(l,s)[3][i] = upr(w,3);\r
+#endif\r
+\r
+        b = fi(inv_affine((uint_8t)i));\r
+        w = bytes2word(fe(b), f9(b), fd(b), fb(b));\r
+\r
+#if defined( IM1_SET )                 /* tables for the inverse mix column operation  */\r
+        t_set(i,m)[b] = w;\r
+#endif\r
+#if defined( IM4_SET )\r
+        t_set(i,m)[0][b] = w;\r
+        t_set(i,m)[1][b] = upr(w,1);\r
+        t_set(i,m)[2][b] = upr(w,2);\r
+        t_set(i,m)[3][b] = upr(w,3);\r
+#endif\r
+\r
+#if defined( ISB_SET )\r
+        t_set(i,box)[i] = b;\r
+#endif\r
+#if defined( IT1_SET )                 /* tables for a normal decryption round */\r
+        t_set(i,n)[i] = w;\r
+#endif\r
+#if defined( IT4_SET )\r
+        t_set(i,n)[0][i] = w;\r
+        t_set(i,n)[1][i] = upr(w,1);\r
+        t_set(i,n)[2][i] = upr(w,2);\r
+        t_set(i,n)[3][i] = upr(w,3);\r
+#endif\r
+        w = bytes2word(b, 0, 0, 0);\r
+#if defined( IL1_SET )                 /* tables for last decryption round */\r
+        t_set(i,l)[i] = w;\r
+#endif\r
+#if defined( IL4_SET )\r
+        t_set(i,l)[0][i] = w;\r
+        t_set(i,l)[1][i] = upr(w,1);\r
+        t_set(i,l)[2][i] = upr(w,2);\r
+        t_set(i,l)[3][i] = upr(w,3);\r
+#endif\r
+    }\r
+    init = 1;\r
+    return EXIT_SUCCESS;\r
+}\r
+\r
+#endif\r
+\r
+#if defined(__cplusplus)\r
+}\r
+#endif\r
+\r
diff --git a/pdns/aes/aestab.h b/pdns/aes/aestab.h
new file mode 100644 (file)
index 0000000..2ad1b03
--- /dev/null
@@ -0,0 +1,174 @@
+/*\r
+ ---------------------------------------------------------------------------\r
+ Copyright (c) 1998-2007, Brian Gladman, Worcester, UK. All rights reserved.\r
+\r
+ LICENSE TERMS\r
+\r
+ The free distribution and use of this software is allowed (with or without\r
+ changes) provided that:\r
+\r
+  1. source code distributions include the above copyright notice, this\r
+     list of conditions and the following disclaimer;\r
+\r
+  2. binary distributions include the above copyright notice, this list\r
+     of conditions and the following disclaimer in their documentation;\r
+\r
+  3. the name of the copyright holder is not used to endorse products\r
+     built using this software without specific written permission.\r
+\r
+ DISCLAIMER\r
+\r
+ This software is provided 'as is' with no explicit or implied warranties\r
+ in respect of its properties, including, but not limited to, correctness\r
+ and/or fitness for purpose.\r
+ ---------------------------------------------------------------------------\r
+ Issue Date: 20/12/2007\r
+\r
+ This file contains the code for declaring the tables needed to implement\r
+ AES. The file aesopt.h is assumed to be included before this header file.\r
+ If there are no global variables, the definitions here can be used to put\r
+ the AES tables in a structure so that a pointer can then be added to the\r
+ AES context to pass them to the AES routines that need them.   If this\r
+ facility is used, the calling program has to ensure that this pointer is\r
+ managed appropriately.  In particular, the value of the t_dec(in,it) item\r
+ in the table structure must be set to zero in order to ensure that the\r
+ tables are initialised. In practice the three code sequences in aeskey.c\r
+ that control the calls to aes_init() and the aes_init() routine itself will\r
+ have to be changed for a specific implementation. If global variables are\r
+ available it will generally be preferable to use them with the precomputed\r
+ FIXED_TABLES option that uses static global tables.\r
+\r
+ The following defines can be used to control the way the tables\r
+ are defined, initialised and used in embedded environments that\r
+ require special features for these purposes\r
+\r
+    the 't_dec' construction is used to declare fixed table arrays\r
+    the 't_set' construction is used to set fixed table values\r
+    the 't_use' construction is used to access fixed table values\r
+\r
+    256 byte tables:\r
+\r
+        t_xxx(s,box)    => forward S box\r
+        t_xxx(i,box)    => inverse S box\r
+\r
+    256 32-bit word OR 4 x 256 32-bit word tables:\r
+\r
+        t_xxx(f,n)      => forward normal round\r
+        t_xxx(f,l)      => forward last round\r
+        t_xxx(i,n)      => inverse normal round\r
+        t_xxx(i,l)      => inverse last round\r
+        t_xxx(l,s)      => key schedule table\r
+        t_xxx(i,m)      => key schedule table\r
+\r
+    Other variables and tables:\r
+\r
+        t_xxx(r,c)      => the rcon table\r
+*/\r
+\r
+#if !defined( _AESTAB_H )\r
+#define _AESTAB_H\r
+\r
+#define t_dec(m,n) t_##m##n\r
+#define t_set(m,n) t_##m##n\r
+#define t_use(m,n) t_##m##n\r
+\r
+#if defined(FIXED_TABLES)\r
+#  if !defined( __GNUC__ ) && (defined( __MSDOS__ ) || defined( __WIN16__ ))\r
+/*   make tables far data to avoid using too much DGROUP space (PG) */\r
+#    define CONST const far\r
+#  else\r
+#    define CONST const\r
+#  endif\r
+#else\r
+#  define CONST\r
+#endif\r
+\r
+#if defined(__cplusplus)\r
+#  define EXTERN extern "C"\r
+#elif defined(DO_TABLES)\r
+#  define EXTERN\r
+#else\r
+#  define EXTERN extern\r
+#endif\r
+\r
+#if defined(_MSC_VER) && defined(TABLE_ALIGN)\r
+#define ALIGN __declspec(align(TABLE_ALIGN))\r
+#else\r
+#define ALIGN\r
+#endif\r
+\r
+#if defined( __WATCOMC__ ) && ( __WATCOMC__ >= 1100 )\r
+#  define XP_DIR __cdecl\r
+#else\r
+#  define XP_DIR\r
+#endif\r
+\r
+#if defined(DO_TABLES) && defined(FIXED_TABLES)\r
+#define d_1(t,n,b,e)       EXTERN ALIGN CONST XP_DIR t n[256]    =   b(e)\r
+#define d_4(t,n,b,e,f,g,h) EXTERN ALIGN CONST XP_DIR t n[4][256] = { b(e), b(f), b(g), b(h) }\r
+EXTERN ALIGN CONST uint_32t t_dec(r,c)[RC_LENGTH] = rc_data(w0);\r
+#else\r
+#define d_1(t,n,b,e)       EXTERN ALIGN CONST XP_DIR t n[256]\r
+#define d_4(t,n,b,e,f,g,h) EXTERN ALIGN CONST XP_DIR t n[4][256]\r
+EXTERN ALIGN CONST uint_32t t_dec(r,c)[RC_LENGTH];\r
+#endif\r
+\r
+#if defined( SBX_SET )\r
+    d_1(uint_8t, t_dec(s,box), sb_data, h0);\r
+#endif\r
+#if defined( ISB_SET )\r
+    d_1(uint_8t, t_dec(i,box), isb_data, h0);\r
+#endif\r
+\r
+#if defined( FT1_SET )\r
+    d_1(uint_32t, t_dec(f,n), sb_data, u0);\r
+#endif\r
+#if defined( FT4_SET )\r
+    d_4(uint_32t, t_dec(f,n), sb_data, u0, u1, u2, u3);\r
+#endif\r
+\r
+#if defined( FL1_SET )\r
+    d_1(uint_32t, t_dec(f,l), sb_data, w0);\r
+#endif\r
+#if defined( FL4_SET )\r
+    d_4(uint_32t, t_dec(f,l), sb_data, w0, w1, w2, w3);\r
+#endif\r
+\r
+#if defined( IT1_SET )\r
+    d_1(uint_32t, t_dec(i,n), isb_data, v0);\r
+#endif\r
+#if defined( IT4_SET )\r
+    d_4(uint_32t, t_dec(i,n), isb_data, v0, v1, v2, v3);\r
+#endif\r
+\r
+#if defined( IL1_SET )\r
+    d_1(uint_32t, t_dec(i,l), isb_data, w0);\r
+#endif\r
+#if defined( IL4_SET )\r
+    d_4(uint_32t, t_dec(i,l), isb_data, w0, w1, w2, w3);\r
+#endif\r
+\r
+#if defined( LS1_SET )\r
+#if defined( FL1_SET )\r
+#undef  LS1_SET\r
+#else\r
+    d_1(uint_32t, t_dec(l,s), sb_data, w0);\r
+#endif\r
+#endif\r
+\r
+#if defined( LS4_SET )\r
+#if defined( FL4_SET )\r
+#undef  LS4_SET\r
+#else\r
+    d_4(uint_32t, t_dec(l,s), sb_data, w0, w1, w2, w3);\r
+#endif\r
+#endif\r
+\r
+#if defined( IM1_SET )\r
+    d_1(uint_32t, t_dec(i,m), mm_data, v0);\r
+#endif\r
+#if defined( IM4_SET )\r
+    d_4(uint_32t, t_dec(i,m), mm_data, v0, v1, v2, v3);\r
+#endif\r
+\r
+#endif\r
diff --git a/pdns/aes/brg_endian.h b/pdns/aes/brg_endian.h
new file mode 100644 (file)
index 0000000..51438df
--- /dev/null
@@ -0,0 +1,133 @@
+/*\r
+ ---------------------------------------------------------------------------\r
+ Copyright (c) 1998-2007, Brian Gladman, Worcester, UK. All rights reserved.\r
+\r
+ LICENSE TERMS\r
+\r
+ The free distribution and use of this software is allowed (with or without\r
+ changes) provided that:\r
+\r
+  1. source code distributions include the above copyright notice, this\r
+     list of conditions and the following disclaimer;\r
+\r
+  2. binary distributions include the above copyright notice, this list\r
+     of conditions and the following disclaimer in their documentation;\r
+\r
+  3. the name of the copyright holder is not used to endorse products\r
+     built using this software without specific written permission.\r
+\r
+ DISCLAIMER\r
+\r
+ This software is provided 'as is' with no explicit or implied warranties\r
+ in respect of its properties, including, but not limited to, correctness\r
+ and/or fitness for purpose.\r
+ ---------------------------------------------------------------------------\r
+ Issue Date: 20/12/2007\r
+*/\r
+\r
+#ifndef _BRG_ENDIAN_H\r
+#define _BRG_ENDIAN_H\r
+\r
+#define IS_BIG_ENDIAN      4321 /* byte 0 is most significant (mc68k) */\r
+#define IS_LITTLE_ENDIAN   1234 /* byte 0 is least significant (i386) */\r
+\r
+/* Include files where endian defines and byteswap functions may reside */\r
+#if defined( __sun )\r
+#  include <sys/isa_defs.h>\r
+#elif defined( __FreeBSD__ ) || defined( __OpenBSD__ ) || defined( __NetBSD__ )\r
+#  include <sys/endian.h>\r
+#elif defined( BSD ) && ( BSD >= 199103 ) || defined( __APPLE__ ) || \\r
+      defined( __CYGWIN32__ ) || defined( __DJGPP__ ) || defined( __osf__ )\r
+#  include <machine/endian.h>\r
+#elif defined( __linux__ ) || defined( __GNUC__ ) || defined( __GNU_LIBRARY__ )\r
+#  if !defined( __MINGW32__ ) && !defined( _AIX )\r
+#    include <endian.h>\r
+#    if !defined( __BEOS__ )\r
+#      include <byteswap.h>\r
+#    endif\r
+#  endif\r
+#endif\r
+\r
+/* Now attempt to set the define for platform byte order using any  */\r
+/* of the four forms SYMBOL, _SYMBOL, __SYMBOL & __SYMBOL__, which  */\r
+/* seem to encompass most endian symbol definitions                 */\r
+\r
+#if defined( BIG_ENDIAN ) && defined( LITTLE_ENDIAN )\r
+#  if defined( BYTE_ORDER ) && BYTE_ORDER == BIG_ENDIAN\r
+#    define PLATFORM_BYTE_ORDER IS_BIG_ENDIAN\r
+#  elif defined( BYTE_ORDER ) && BYTE_ORDER == LITTLE_ENDIAN\r
+#    define PLATFORM_BYTE_ORDER IS_LITTLE_ENDIAN\r
+#  endif\r
+#elif defined( BIG_ENDIAN )\r
+#  define PLATFORM_BYTE_ORDER IS_BIG_ENDIAN\r
+#elif defined( LITTLE_ENDIAN )\r
+#  define PLATFORM_BYTE_ORDER IS_LITTLE_ENDIAN\r
+#endif\r
+\r
+#if defined( _BIG_ENDIAN ) && defined( _LITTLE_ENDIAN )\r
+#  if defined( _BYTE_ORDER ) && _BYTE_ORDER == _BIG_ENDIAN\r
+#    define PLATFORM_BYTE_ORDER IS_BIG_ENDIAN\r
+#  elif defined( _BYTE_ORDER ) && _BYTE_ORDER == _LITTLE_ENDIAN\r
+#    define PLATFORM_BYTE_ORDER IS_LITTLE_ENDIAN\r
+#  endif\r
+#elif defined( _BIG_ENDIAN )\r
+#  define PLATFORM_BYTE_ORDER IS_BIG_ENDIAN\r
+#elif defined( _LITTLE_ENDIAN )\r
+#  define PLATFORM_BYTE_ORDER IS_LITTLE_ENDIAN\r
+#endif\r
+\r
+#if defined( __BIG_ENDIAN ) && defined( __LITTLE_ENDIAN )\r
+#  if defined( __BYTE_ORDER ) && __BYTE_ORDER == __BIG_ENDIAN\r
+#    define PLATFORM_BYTE_ORDER IS_BIG_ENDIAN\r
+#  elif defined( __BYTE_ORDER ) && __BYTE_ORDER == __LITTLE_ENDIAN\r
+#    define PLATFORM_BYTE_ORDER IS_LITTLE_ENDIAN\r
+#  endif\r
+#elif defined( __BIG_ENDIAN )\r
+#  define PLATFORM_BYTE_ORDER IS_BIG_ENDIAN\r
+#elif defined( __LITTLE_ENDIAN )\r
+#  define PLATFORM_BYTE_ORDER IS_LITTLE_ENDIAN\r
+#endif\r
+\r
+#if defined( __BIG_ENDIAN__ ) && defined( __LITTLE_ENDIAN__ )\r
+#  if defined( __BYTE_ORDER__ ) && __BYTE_ORDER__ == __BIG_ENDIAN__\r
+#    define PLATFORM_BYTE_ORDER IS_BIG_ENDIAN\r
+#  elif defined( __BYTE_ORDER__ ) && __BYTE_ORDER__ == __LITTLE_ENDIAN__\r
+#    define PLATFORM_BYTE_ORDER IS_LITTLE_ENDIAN\r
+#  endif\r
+#elif defined( __BIG_ENDIAN__ )\r
+#  define PLATFORM_BYTE_ORDER IS_BIG_ENDIAN\r
+#elif defined( __LITTLE_ENDIAN__ )\r
+#  define PLATFORM_BYTE_ORDER IS_LITTLE_ENDIAN\r
+#endif\r
+\r
+/*  if the platform byte order could not be determined, then try to */\r
+/*  set this define using common machine defines                    */\r
+#if !defined(PLATFORM_BYTE_ORDER)\r
+\r
+#if   defined( __alpha__ ) || defined( __alpha ) || defined( i386 )       || \\r
+      defined( __i386__ )  || defined( _M_I86 )  || defined( _M_IX86 )    || \\r
+      defined( __OS2__ )   || defined( sun386 )  || defined( __TURBOC__ ) || \\r
+      defined( vax )       || defined( vms )     || defined( VMS )        || \\r
+      defined( __VMS )     || defined( _M_X64 )\r
+#  define PLATFORM_BYTE_ORDER IS_LITTLE_ENDIAN\r
+\r
+#elif defined( AMIGA )   || defined( applec )    || defined( __AS400__ )  || \\r
+      defined( _CRAY )   || defined( __hppa )    || defined( __hp9000 )   || \\r
+      defined( ibm370 )  || defined( mc68000 )   || defined( m68k )       || \\r
+      defined( __MRC__ ) || defined( __MVS__ )   || defined( __MWERKS__ ) || \\r
+      defined( sparc )   || defined( __sparc)    || defined( SYMANTEC_C ) || \\r
+      defined( __VOS__ ) || defined( __TIGCC__ ) || defined( __TANDEM )   || \\r
+      defined( THINK_C ) || defined( __VMCMS__ ) || defined( _AIX )\r
+#  define PLATFORM_BYTE_ORDER IS_BIG_ENDIAN\r
+\r
+#elif 0     /* **** EDIT HERE IF NECESSARY **** */\r
+#  define PLATFORM_BYTE_ORDER IS_LITTLE_ENDIAN\r
+#elif 0     /* **** EDIT HERE IF NECESSARY **** */\r
+#  define PLATFORM_BYTE_ORDER IS_BIG_ENDIAN\r
+#else\r
+#  error Please edit lines 126 or 128 in brg_endian.h to set the platform byte order\r
+#endif\r
+\r
+#endif\r
+\r
+#endif\r
diff --git a/pdns/aes/brg_types.h b/pdns/aes/brg_types.h
new file mode 100644 (file)
index 0000000..5911581
--- /dev/null
@@ -0,0 +1,223 @@
+/*\r
+ ---------------------------------------------------------------------------\r
+ Copyright (c) 1998-2007, Brian Gladman, Worcester, UK. All rights reserved.\r
+\r
+ LICENSE TERMS\r
+\r
+ The free distribution and use of this software is allowed (with or without\r
+ changes) provided that:\r
+\r
+  1. source code distributions include the above copyright notice, this\r
+     list of conditions and the following disclaimer;\r
+\r
+  2. binary distributions include the above copyright notice, this list\r
+     of conditions and the following disclaimer in their documentation;\r
+\r
+  3. the name of the copyright holder is not used to endorse products\r
+     built using this software without specific written permission.\r
+\r
+ DISCLAIMER\r
+\r
+ This software is provided 'as is' with no explicit or implied warranties\r
+ in respect of its properties, including, but not limited to, correctness\r
+ and/or fitness for purpose.\r
+ ---------------------------------------------------------------------------\r
+ Issue Date: 20/12/2007\r
+\r
+ The unsigned integer types defined here are of the form uint_<nn>t where\r
+ <nn> is the length of the type; for example, the unsigned 32-bit type is\r
+ 'uint_32t'.  These are NOT the same as the 'C99 integer types' that are\r
+ defined in the inttypes.h and stdint.h headers since attempts to use these\r
+ types have shown that support for them is still highly variable.  However,\r
+ since the latter are of the form uint<nn>_t, a regular expression search\r
+ and replace (in VC++ search on 'uint_{:z}t' and replace with 'uint\1_t')\r
+ can be used to convert the types used here to the C99 standard types.\r
+*/\r
+\r
+#ifndef _BRG_TYPES_H\r
+#define _BRG_TYPES_H\r
+\r
+#if defined(__cplusplus)\r
+extern "C" {\r
+#endif\r
+\r
+#include <limits.h>\r
+\r
+#if defined( _MSC_VER ) && ( _MSC_VER >= 1300 )\r
+#  include <stddef.h>\r
+#  define ptrint_t intptr_t\r
+#elif defined( __GNUC__ ) && ( __GNUC__ >= 3 )\r
+#  include <stdint.h>\r
+#  define ptrint_t intptr_t\r
+#else\r
+#  define ptrint_t int\r
+#endif\r
+\r
+#ifndef BRG_UI8\r
+#  define BRG_UI8\r
+#  if UCHAR_MAX == 255u\r
+     typedef unsigned char uint_8t;\r
+#  else\r
+#    error Please define uint_8t as an 8-bit unsigned integer type in brg_types.h\r
+#  endif\r
+#endif\r
+\r
+#ifndef BRG_UI16\r
+#  define BRG_UI16\r
+#  if USHRT_MAX == 65535u\r
+     typedef unsigned short uint_16t;\r
+#  else\r
+#    error Please define uint_16t as a 16-bit unsigned short type in brg_types.h\r
+#  endif\r
+#endif\r
+\r
+#ifndef BRG_UI32\r
+#  define BRG_UI32\r
+#  if UINT_MAX == 4294967295u\r
+#    define li_32(h) 0x##h##u\r
+     typedef unsigned int uint_32t;\r
+#  elif ULONG_MAX == 4294967295u\r
+#    define li_32(h) 0x##h##ul\r
+     typedef unsigned long uint_32t;\r
+#  elif defined( _CRAY )\r
+#    error This code needs 32-bit data types, which Cray machines do not provide\r
+#  else\r
+#    error Please define uint_32t as a 32-bit unsigned integer type in brg_types.h\r
+#  endif\r
+#endif\r
+\r
+#ifndef BRG_UI64\r
+#  if defined( __BORLANDC__ ) && !defined( __MSDOS__ )\r
+#    define BRG_UI64\r
+#    define li_64(h) 0x##h##ui64\r
+     typedef unsigned __int64 uint_64t;\r
+#  elif defined( _MSC_VER ) && ( _MSC_VER < 1300 )    /* 1300 == VC++ 7.0 */\r
+#    define BRG_UI64\r
+#    define li_64(h) 0x##h##ui64\r
+     typedef unsigned __int64 uint_64t;\r
+#  elif defined( __sun ) && defined(ULONG_MAX) && ULONG_MAX == 0xfffffffful\r
+#    define BRG_UI64\r
+#    define li_64(h) 0x##h##ull\r
+     typedef unsigned long long uint_64t;\r
+#  elif defined( __MVS__ )\r
+#    define BRG_UI64\r
+#    define li_64(h) 0x##h##ull\r
+     typedef unsigned int long long uint_64t;\r
+#  elif defined( UINT_MAX ) && UINT_MAX > 4294967295u\r
+#    if UINT_MAX == 18446744073709551615u\r
+#      define BRG_UI64\r
+#      define li_64(h) 0x##h##u\r
+       typedef unsigned int uint_64t;\r
+#    endif\r
+#  elif defined( ULONG_MAX ) && ULONG_MAX > 4294967295u\r
+#    if ULONG_MAX == 18446744073709551615ul\r
+#      define BRG_UI64\r
+#      define li_64(h) 0x##h##ul\r
+       typedef unsigned long uint_64t;\r
+#    endif\r
+#  elif defined( ULLONG_MAX ) && ULLONG_MAX > 4294967295u\r
+#    if ULLONG_MAX == 18446744073709551615ull\r
+#      define BRG_UI64\r
+#      define li_64(h) 0x##h##ull\r
+       typedef unsigned long long uint_64t;\r
+#    endif\r
+#  elif defined( ULONG_LONG_MAX ) && ULONG_LONG_MAX > 4294967295u\r
+#    if ULONG_LONG_MAX == 18446744073709551615ull\r
+#      define BRG_UI64\r
+#      define li_64(h) 0x##h##ull\r
+       typedef unsigned long long uint_64t;\r
+#    endif\r
+#  endif\r
+#endif\r
+\r
+#if !defined( BRG_UI64 )\r
+#  if defined( NEED_UINT_64T )\r
+#    error Please define uint_64t as an unsigned 64 bit type in brg_types.h\r
+#  endif\r
+#endif\r
+\r
+#ifndef RETURN_VALUES\r
+#  define RETURN_VALUES\r
+#  if defined( DLL_EXPORT )\r
+#    if defined( _MSC_VER ) || defined ( __INTEL_COMPILER )\r
+#      define VOID_RETURN    __declspec( dllexport ) void __stdcall\r
+#      define INT_RETURN     __declspec( dllexport ) int  __stdcall\r
+#    elif defined( __GNUC__ )\r
+#      define VOID_RETURN    __declspec( __dllexport__ ) void\r
+#      define INT_RETURN     __declspec( __dllexport__ ) int\r
+#    else\r
+#      error Use of the DLL is only available on the Microsoft, Intel and GCC compilers\r
+#    endif\r
+#  elif defined( DLL_IMPORT )\r
+#    if defined( _MSC_VER ) || defined ( __INTEL_COMPILER )\r
+#      define VOID_RETURN    __declspec( dllimport ) void __stdcall\r
+#      define INT_RETURN     __declspec( dllimport ) int  __stdcall\r
+#    elif defined( __GNUC__ )\r
+#      define VOID_RETURN    __declspec( __dllimport__ ) void\r
+#      define INT_RETURN     __declspec( __dllimport__ ) int\r
+#    else\r
+#      error Use of the DLL is only available on the Microsoft, Intel and GCC compilers\r
+#    endif\r
+#  elif defined( __WATCOMC__ )\r
+#    define VOID_RETURN  void __cdecl\r
+#    define INT_RETURN   int  __cdecl\r
+#  else\r
+#    define VOID_RETURN  void\r
+#    define INT_RETURN   int\r
+#  endif\r
+#endif\r
+\r
+/*     These defines are used to detect and set the memory alignment of pointers.\r
+    Note that offsets are in bytes.\r
+\r
+       ALIGN_OFFSET(x,n)                       return the positive or zero offset of \r
+                                                               the memory addressed by the pointer 'x' \r
+                                                               from an address that is aligned on an \r
+                                                               'n' byte boundary ('n' is a power of 2)\r
+\r
+       ALIGN_FLOOR(x,n)                        return a pointer that points to memory\r
+                                                               that is aligned on an 'n' byte boundary \r
+                                                               and is not higher than the memory address\r
+                                                               pointed to by 'x' ('n' is a power of 2)\r
+\r
+       ALIGN_CEIL(x,n)                         return a pointer that points to memory\r
+                                                               that is aligned on an 'n' byte boundary \r
+                                                               and is not lower than the memory address\r
+                                                               pointed to by 'x' ('n' is a power of 2)\r
+*/\r
+\r
+#define ALIGN_OFFSET(x,n)      (((ptrint_t)(x)) & ((n) - 1))\r
+#define ALIGN_FLOOR(x,n)       ((uint_8t*)(x) - ( ((ptrint_t)(x)) & ((n) - 1)))\r
+#define ALIGN_CEIL(x,n)                ((uint_8t*)(x) + (-((ptrint_t)(x)) & ((n) - 1)))\r
+\r
+/*  These defines are used to declare buffers in a way that allows\r
+    faster operations on longer variables to be used.  In all these\r
+    defines 'size' must be a power of 2 and >= 8. NOTE that the \r
+    buffer size is in bytes but the type length is in bits\r
+\r
+    UNIT_TYPEDEF(x,size)        declares a variable 'x' of length \r
+                                'size' bits\r
+\r
+    BUFR_TYPEDEF(x,size,bsize)  declares a buffer 'x' of length 'bsize' \r
+                                bytes defined as an array of variables\r
+                                each of 'size' bits (bsize must be a \r
+                                multiple of size / 8)\r
+\r
+    UNIT_CAST(x,size)           casts a variable to a type of \r
+                                length 'size' bits\r
+\r
+    UPTR_CAST(x,size)           casts a pointer to a pointer to a \r
+                                varaiable of length 'size' bits\r
+*/\r
+\r
+#define UI_TYPE(size)               uint_##size##t\r
+#define UNIT_TYPEDEF(x,size)        typedef UI_TYPE(size) x\r
+#define BUFR_TYPEDEF(x,size,bsize)  typedef UI_TYPE(size) x[bsize / (size >> 3)]\r
+#define UNIT_CAST(x,size)           ((UI_TYPE(size) )(x))  \r
+#define UPTR_CAST(x,size)           ((UI_TYPE(size)*)(x))\r
+\r
+#if defined(__cplusplus)\r
+}\r
+#endif\r
+\r
+#endif\r
diff --git a/pdns/aes/dns_random.cc b/pdns/aes/dns_random.cc
new file mode 100644 (file)
index 0000000..fd77f6a
--- /dev/null
@@ -0,0 +1,65 @@
+#include "aescpp.h"
+#include <iostream>
+#include <sys/types.h>
+#include <unistd.h>
+#include <sys/time.h>
+#include "dns_random.hh"
+
+using namespace std;
+
+static aes_encrypt_ctx g_cx;
+static unsigned char g_counter[16];
+static uint32_t g_in;
+
+void dns_random_init(const char data[16])
+{
+  aes_init();
+
+  aes_encrypt_key128((const unsigned char*)data, &g_cx);
+  struct timeval now;
+  gettimeofday(&now, 0);
+
+  memcpy(g_counter, &now.tv_usec, sizeof(now.tv_usec));
+  memcpy(g_counter+sizeof(now.tv_usec), &now.tv_sec, sizeof(now.tv_sec));
+  g_in = getpid() | (getppid()<<16);
+
+  srandom(now.tv_usec);
+}
+
+static void counterIncrement(unsigned char* counter)
+{
+  if(!++counter[0])
+    if(!++counter[1])
+      if(!++counter[2])
+       if(!++counter[3])
+         if(!++counter[4])
+           if(!++counter[5])
+             if(!++counter[6])
+               if(!++counter[7])
+                 if(!++counter[8])
+                   if(!++counter[9])
+                     if(!++counter[10])
+                       if(!++counter[11])
+                         if(!++counter[12])
+                           if(!++counter[13])
+                             if(!++counter[14])
+                               ++counter[15];
+  
+}
+
+unsigned int dns_random(unsigned int n)
+{
+  uint32_t out;
+  aes_ctr_encrypt((unsigned char*) &g_in, (unsigned char*)& out, sizeof(g_in), g_counter, counterIncrement, &g_cx);
+  return out % n;
+}
+
+#if 0
+int main()
+{
+  dns_random_init("0123456789abcdef");
+
+  for(int n = 0; n < 16; n++)
+    cerr<<dns_random(16384)<<endl;
+}
+#endif
index 937b49c971c0149fed8355e1d38367d5e9b23e89..8b57884373427f67f6fb225059d48f8545d01346 100644 (file)
@@ -7369,6 +7369,17 @@ local0.err                        /var/log/pdns.err
              </para>
            </listitem>
          </varlistentry>
+         <varlistentry>
+           <term>entropy-source</term>
+           <listitem>
+             <para>
+               From version 3.1.5 onwards, PowerDNS can read entropy from a (hardware) source. This is used for generating random numbers
+               which are very hard to predict. Generally on UNIX platforms, this source will be 
+               <filename>/dev/urandom</filename>, which will always supply random numbers, even if entropy is lacking. 
+               Change to <filename>/dev/random</filename> if PowerDNS should block waiting for enough entropy to arrive.
+             </para>
+           </listitem>
+         </varlistentry>
          <varlistentry>
            <term>export-etc-hosts</term>
            <listitem>
@@ -10570,7 +10581,7 @@ insert into domains (id,name,type) values (domains_id_sequence.nextval,'netherla
                <para>
                  Called to determine if a certain host is a supermaster for a certain domain name.
                  Default: <command>
-                   select account from supermasters where ip='%s' and nameserver='%s'");
+                   select account from supermasters where ip='%s' and nameserver='%s');
                  </command>
                </para>
              </listitem>
@@ -13429,6 +13440,39 @@ POSSIBILITY OF SUCH DAMAGES.
                     END OF TERMS AND CONDITIONS
       </para>
   </appendix>
+  <appendix id="further-copyrights"><title>Further copyright statements</title>
+    <sect1><title>AES implementation by Brian Gladman</title>
+    <para>
+      Since version 3.1.5, PowerDNS contains AES code by Brian Gladman, to which
+      the following applies:
+    </para>
+    <para>
+ Copyright &copy; 1998-2007, Brian Gladman, Worcester, UK. All rights reserved.
+    </para>
+    <para>
+     LICENSE TERMS
+    </para>
+    <para>
+ The free distribution and use of this software is allowed (with or without
+ changes) provided that:
+ </para><para>
+  1. source code distributions include the above copyright notice, this
+     list of conditions and the following disclaimer;
+ </para><para>
+  2. binary distributions include the above copyright notice, this list
+     of conditions and the following disclaimer in their documentation;
+ </para><para>
+  3. the name of the copyright holder is not used to endorse products
+     built using this software without specific written permission.
+ </para><para>
+ DISCLAIMER
+ </para><para>
+ This software is provided 'as is' with no explicit or implied warranties
+ in respect of its properties, including, but not limited to, correctness
+ and/or fitness for purpose.
+ </para><para>
+</sect1>
+  </appendix>
 
   </book>
 <!-- Keep this comment at the end of the file