~ubuntu-branches/ubuntu/trusty/pdns/trusty-backports

« back to all changes in this revision

Viewing changes to pdns/ext/polarssl/library/xtea.c

  • Committer: Package Import Robot
  • Author(s): Matthijs Möhlmann
  • Date: 2011-11-19 11:58:10 UTC
  • mfrom: (1.2.4)
  • Revision ID: package-import@ubuntu.com-20111119115810-5u926cmriehkt5j7
Tags: 3.0-1
* New upstream version (Closes: #624330, #626909, #617476, #498918, #500572)
  (Closes: #645539, #623036, #521791, #583161, #590285, #499396)
* Update Standards-Version to 3.9.2
* Add lua backend.
* Use new style dh instead of individual dh_* commands.
* Add Homepage to debian/control (Closes: #634947)
* Add pdnssec and dnsreplay utility.
* Use dbconfig-common to populate / upgrade databases.
* Update patch addconfigdir, do not parse ucf-dist files.
* Update manpage pdns_control and include a list of options (Closes: #621724)
* Add manpage for pdnssec.
* Add prerm scripts to the backends, stop the pdns server.
* Add patch from upstream to properly parse priority. (Closes: #533023)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *  An 32-bit implementation of the XTEA algorithm
 
3
 *
 
4
 *  Copyright (C) 2006-2010, Paul Bakker <polarssl_maintainer at polarssl.org>
 
5
 *  All rights reserved.
 
6
 *
 
7
 *  This program is free software; you can redistribute it and/or modify
 
8
 *  it under the terms of the GNU General Public License as published by
 
9
 *  the Free Software Foundation; either version 2 of the License, or
 
10
 *  (at your option) any later version.
 
11
 *
 
12
 *  This program is distributed in the hope that it will be useful,
 
13
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
 *  GNU General Public License for more details.
 
16
 *
 
17
 *  You should have received a copy of the GNU General Public License along
 
18
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 
19
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 
20
 */
 
21
 
 
22
#include "polarssl/config.h"
 
23
 
 
24
#if defined(POLARSSL_XTEA_C)
 
25
 
 
26
#include "polarssl/xtea.h"
 
27
 
 
28
#include <string.h>
 
29
 
 
30
/*
 
31
 * 32-bit integer manipulation macros (big endian)
 
32
 */
 
33
#ifndef GET_ULONG_BE
 
34
#define GET_ULONG_BE(n,b,i)                             \
 
35
{                                                       \
 
36
    (n) = ( (unsigned long) (b)[(i)    ] << 24 )        \
 
37
            | ( (unsigned long) (b)[(i) + 1] << 16 )        \
 
38
            | ( (unsigned long) (b)[(i) + 2] <<  8 )        \
 
39
            | ( (unsigned long) (b)[(i) + 3]       );       \
 
40
}
 
41
#endif
 
42
 
 
43
#ifndef PUT_ULONG_BE
 
44
#define PUT_ULONG_BE(n,b,i)                             \
 
45
{                                                       \
 
46
    (b)[(i)    ] = (unsigned char) ( (n) >> 24 );       \
 
47
    (b)[(i) + 1] = (unsigned char) ( (n) >> 16 );       \
 
48
    (b)[(i) + 2] = (unsigned char) ( (n) >>  8 );       \
 
49
    (b)[(i) + 3] = (unsigned char) ( (n)       );       \
 
50
}
 
51
#endif
 
52
 
 
53
/*
 
54
 * XTEA key schedule
 
55
 */
 
56
void xtea_setup( xtea_context *ctx, unsigned char key[16] )
 
57
{
 
58
    int i;
 
59
 
 
60
    memset(ctx, 0, sizeof(xtea_context));
 
61
 
 
62
    for( i = 0; i < 4; i++ )
 
63
    {
 
64
        GET_ULONG_BE( ctx->k[i], key, i << 2 );
 
65
    }
 
66
}
 
67
 
 
68
/*
 
69
 * XTEA encrypt function
 
70
 */
 
71
int xtea_crypt_ecb( xtea_context *ctx, int mode, unsigned char input[8],
 
72
                     unsigned char output[8])
 
73
{
 
74
    uint32_t *k, v0, v1, i;
 
75
 
 
76
    k = ctx->k;
 
77
    
 
78
    GET_ULONG_BE( v0, input, 0 );
 
79
    GET_ULONG_BE( v1, input, 4 );
 
80
 
 
81
    if( mode == XTEA_ENCRYPT )
 
82
    {
 
83
            uint32_t sum = 0, delta = 0x9E3779B9;
 
84
 
 
85
            for( i = 0; i < 32; i++ )
 
86
            {
 
87
                    v0 += (((v1 << 4) ^ (v1 >> 5)) + v1) ^ (sum + k[sum & 3]);
 
88
                    sum += delta;
 
89
                    v1 += (((v0 << 4) ^ (v0 >> 5)) + v0) ^ (sum + k[(sum>>11) & 3]);
 
90
            }
 
91
    }
 
92
    else /* XTEA_DECRYPT */
 
93
    {
 
94
            uint32_t delta = 0x9E3779B9, sum = delta * 32;
 
95
 
 
96
            for( i = 0; i < 32; i++ )
 
97
            {
 
98
                    v1 -= (((v0 << 4) ^ (v0 >> 5)) + v0) ^ (sum + k[(sum>>11) & 3]);
 
99
                    sum -= delta;
 
100
                    v0 -= (((v1 << 4) ^ (v1 >> 5)) + v1) ^ (sum + k[sum & 3]);
 
101
            }
 
102
    }
 
103
 
 
104
    PUT_ULONG_BE( v0, output, 0 );
 
105
    PUT_ULONG_BE( v1, output, 4 );
 
106
 
 
107
    return( 0 );
 
108
}
 
109
 
 
110
#if defined(POLARSSL_SELF_TEST)
 
111
 
 
112
#include <string.h>
 
113
#include <stdio.h>
 
114
 
 
115
/*
 
116
 * XTEA tests vectors (non-official)
 
117
 */
 
118
 
 
119
static const unsigned char xtea_test_key[6][16] =
 
120
{
 
121
   { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
 
122
     0x0c, 0x0d, 0x0e, 0x0f },
 
123
   { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
 
124
     0x0c, 0x0d, 0x0e, 0x0f },
 
125
   { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
 
126
     0x0c, 0x0d, 0x0e, 0x0f },
 
127
   { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
 
128
     0x00, 0x00, 0x00, 0x00 },
 
129
   { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
 
130
     0x00, 0x00, 0x00, 0x00 },
 
131
   { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
 
132
     0x00, 0x00, 0x00, 0x00 }
 
133
};
 
134
 
 
135
static const unsigned char xtea_test_pt[6][8] =
 
136
{
 
137
    { 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48 },
 
138
    { 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41 },
 
139
    { 0x5a, 0x5b, 0x6e, 0x27, 0x89, 0x48, 0xd7, 0x7f },
 
140
    { 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48 },
 
141
    { 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41 },
 
142
    { 0x70, 0xe1, 0x22, 0x5d, 0x6e, 0x4e, 0x76, 0x55 }
 
143
};
 
144
 
 
145
static const unsigned char xtea_test_ct[6][8] =
 
146
{
 
147
    { 0x49, 0x7d, 0xf3, 0xd0, 0x72, 0x61, 0x2c, 0xb5 },
 
148
    { 0xe7, 0x8f, 0x2d, 0x13, 0x74, 0x43, 0x41, 0xd8 },
 
149
    { 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41 },
 
150
    { 0xa0, 0x39, 0x05, 0x89, 0xf8, 0xb8, 0xef, 0xa5 },
 
151
    { 0xed, 0x23, 0x37, 0x5a, 0x82, 0x1a, 0x8c, 0x2d },
 
152
    { 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41 }
 
153
};
 
154
 
 
155
/*
 
156
 * Checkup routine
 
157
 */
 
158
int xtea_self_test( int verbose )
 
159
{
 
160
    int i;
 
161
    unsigned char buf[8];
 
162
    xtea_context ctx;
 
163
 
 
164
    for( i = 0; i < 6; i++ )
 
165
    {
 
166
        if( verbose != 0 )
 
167
            printf( "  XTEA test #%d: ", i + 1 );
 
168
 
 
169
        memcpy( buf, xtea_test_pt[i], 8 );
 
170
 
 
171
        xtea_setup( &ctx, (unsigned char *) xtea_test_key[i] );
 
172
        xtea_crypt_ecb( &ctx, XTEA_ENCRYPT, buf, buf );
 
173
 
 
174
        if( memcmp( buf, xtea_test_ct[i], 8 ) != 0 )
 
175
        {
 
176
            if( verbose != 0 )
 
177
                printf( "failed\n" );
 
178
 
 
179
            return( 1 );
 
180
        }
 
181
 
 
182
        if( verbose != 0 )
 
183
            printf( "passed\n" );
 
184
    }
 
185
 
 
186
    if( verbose != 0 )
 
187
        printf( "\n" );
 
188
 
 
189
    return( 0 );
 
190
}
 
191
 
 
192
#endif
 
193
 
 
194
#endif