~ubuntu-branches/ubuntu/precise/gnupg2/precise-proposed

« back to all changes in this revision

Viewing changes to cipher/arcfour.c

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Urlichs
  • Date: 2006-01-24 04:31:42 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20060124043142-pbg192or6qxv3yk2
Tags: 1.9.20-1
* New Upstream version. Closes:#306890,#344530
  * Closes:#320490: gpg-protect-tool fails to decrypt PKCS-12 files 
* Depend on libopensc2-dev, not -1-. Closes:#348106

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* arcfour.c  -  The arcfour stream cipher
 
2
 *      Copyright (C) 2000 Free Software Foundation, Inc.
 
3
 *
 
4
 * For a description of the algorithm, see:
 
5
 *   Bruce Schneier: Applied Cryptography. John Wiley & Sons, 1996.
 
6
 *   ISBN 0-471-11709-9. Pages 397 ff.
 
7
 *
 
8
 * This file is part of GnuPG.
 
9
 *
 
10
 * GnuPG is free software; you can redistribute it and/or modify
 
11
 * it under the terms of the GNU General Public License as published by
 
12
 * the Free Software Foundation; either version 2 of the License, or
 
13
 * (at your option) any later version.
 
14
 *
 
15
 * GnuPG is distributed in the hope that it will be useful,
 
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
18
 * GNU General Public License for more details.
 
19
 *
 
20
 * You should have received a copy of the GNU General Public License
 
21
 * along with this program; if not, write to the Free Software
 
22
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
 
23
 */
 
24
 
 
25
 
 
26
#include <config.h>
 
27
#include <stdio.h>
 
28
#include <stdlib.h>
 
29
#include <string.h>
 
30
#include "types.h"
 
31
#include "g10lib.h"
 
32
#include "arcfour.h"
 
33
 
 
34
static const char *selftest(void);
 
35
 
 
36
 
 
37
typedef struct {
 
38
    int idx_i, idx_j;
 
39
    byte sbox[256];
 
40
} ARCFOUR_context;
 
41
 
 
42
 
 
43
static void
 
44
encrypt_stream( ARCFOUR_context *ctx,
 
45
                byte *outbuf, const byte *inbuf, unsigned int length )
 
46
{
 
47
    int t;  
 
48
    int i = ctx->idx_i;
 
49
    int j = ctx->idx_j;
 
50
    byte *sbox = ctx->sbox;
 
51
 
 
52
    while ( length-- ) {
 
53
        i = (i+1) % 256;
 
54
        j = (j + sbox[i]) % 256;
 
55
        t = sbox[i]; sbox[i] = sbox[j]; sbox[j] = t;
 
56
        *outbuf++ = *inbuf++ ^ sbox[(sbox[i] + sbox[j]) % 256];
 
57
    }
 
58
 
 
59
    ctx->idx_i = i;
 
60
    ctx->idx_j = j;
 
61
}
 
62
 
 
63
 
 
64
static int
 
65
arcfour_setkey( ARCFOUR_context *ctx, const byte *key, unsigned int keylen )
 
66
{
 
67
    static int initialized;
 
68
    static const char* selftest_failed;
 
69
    int i, j;
 
70
    byte karr[256];
 
71
 
 
72
    if( !initialized ) {
 
73
        initialized = 1;
 
74
        selftest_failed = selftest();
 
75
        if( selftest_failed )
 
76
            fprintf(stderr,"ARCFOUR selftest failed (%s)\n", selftest_failed );
 
77
    }
 
78
    if( selftest_failed )
 
79
        return GCRYERR_SELFTEST;
 
80
 
 
81
    if( keylen < 40 )
 
82
        return GCRYERR_INV_KEYLEN;
 
83
 
 
84
    ctx->idx_i = ctx->idx_j = 0;
 
85
    for (i=0; i < 256; i++ )
 
86
        ctx->sbox[i] = i;
 
87
    for (i=0; i < 256; i++ )
 
88
        karr[i] = key[i%keylen];
 
89
    for (i=j=0; i < 256; i++ ) {
 
90
        int t;
 
91
        j = (j + ctx->sbox[i] + karr[i]) % 256;
 
92
        t = ctx->sbox[i];
 
93
        ctx->sbox[i] = ctx->sbox[j];
 
94
        ctx->sbox[j] = t;
 
95
    } 
 
96
    memset( karr, 0, 256 );
 
97
 
 
98
    return 0;
 
99
}
 
100
 
 
101
 
 
102
static const char*
 
103
selftest(void)
 
104
{
 
105
    ARCFOUR_context ctx;
 
106
    byte scratch[16];      
 
107
    
 
108
    /* Test vector from Cryptlib labeled there:
 
109
     * "from the State/Commerce Department" */
 
110
    static const byte key_1[] =
 
111
        { 0x61, 0x8A, 0x63, 0xD2, 0xFB };
 
112
    static const byte plaintext_1[] =
 
113
        { 0xDC, 0xEE, 0x4C, 0xF9, 0x2C };
 
114
    static const byte ciphertext_1[] =
 
115
        { 0xF1, 0x38, 0x29, 0xC9, 0xDE };
 
116
 
 
117
    arcfour_setkey( &ctx, key_1, sizeof(key_1));
 
118
    encrypt_stream( &ctx, scratch, plaintext_1, sizeof(plaintext_1));
 
119
    if (memcmp (scratch, ciphertext_1, sizeof (ciphertext_1)))
 
120
        return "Arcfour encryption test 1 failed.";
 
121
    encrypt_stream(&ctx, scratch, scratch, sizeof(plaintext_1)); /* decrypt */
 
122
    if ( memcmp (scratch, plaintext_1, sizeof (plaintext_1)))
 
123
        return "Arcfour decryption test 1 failed.";
 
124
    return NULL;
 
125
}
 
126
 
 
127
 
 
128
/****************
 
129
 * Return some information about the algorithm.  We need algo here to
 
130
 * distinguish different flavors of the algorithm.
 
131
 * Returns: A pointer to string describing the algorithm or NULL if
 
132
 *          the ALGO is invalid.
 
133
 * NOTE: This is a special get_info function which is different from all
 
134
 * others because arcfour is a stream cipher.  We use this hack until
 
135
 * we have redesign the interface.
 
136
 */
 
137
const char *
 
138
arcfour_get_info( int algo, size_t *keylen, size_t *blocksize,
 
139
                   size_t *contextsize,
 
140
                   int  (**r_setkey)( void *c, byte *key, unsigned keylen ),
 
141
                   void (**r_stencrypt)( void *c, byte *outbuf,
 
142
                                       byte *inbuf, unsigned int nbytes ),
 
143
                   void (**r_stdecrypt)( void *c, byte *outbuf,
 
144
                                       byte *inbuf, unsigned int nbytes )
 
145
                 )
 
146
{
 
147
    *keylen = 128; /* arbitrary value */
 
148
    *blocksize = 1;
 
149
    *contextsize = sizeof(ARCFOUR_context);
 
150
    *(int  (**)(ARCFOUR_context*, byte*, unsigned))r_setkey
 
151
                                                        = arcfour_setkey;
 
152
    *(void (**)(ARCFOUR_context*, byte*, const byte*, unsigned))r_stencrypt
 
153
                                                        = encrypt_stream;
 
154
    *(void (**)(ARCFOUR_context*, byte*, const byte*, unsigned))r_stdecrypt
 
155
                                                        = encrypt_stream;
 
156
 
 
157
 
 
158
    if( algo == 301 )
 
159
        return "ARCFOUR";
 
160
    return NULL;
 
161
}
 
162
 
 
163
 
 
164
 
 
165