aboutsummaryrefslogtreecommitdiffstats
path: root/cipher/idea-stub.c
blob: 05581ae90f9ce129127950345a1ecf43f78a516b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/* idea-stub.c - Dummy module for the deprecated IDEA cipher.
 *	Copyright (C) 2002 Free Software Foundation, Inc.
 *
 * This file is part of GnuPG.
 *
 * GnuPG is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * GnuPG is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
 */

/* IDEA is a patented algorithm and therefore the use of IDEA in
   countries where this patent is valid can not be allowed due to the
   terms of the GNU General Public License.  Those restrictions are
   there to help protecting the freedom of software.  For more
   information on the nonsense of software patents and the general
   problem with this, please see http://www.noepatents.org.

   However for research purposes and in certain situations it might be
   useful to use this algorithm anyway.  

   We provide this stub which will dynload a idea module and is only 
   used if the configure run did't found statically linked file.
   See http://www.gnupg.org/why-not-dea.html for details.
*/

#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#ifdef HAVE_DL_DLOPEN
#include <dlfcn.h>
#endif
#ifdef __MINGW32__
#include <windows.h>
#endif
#include "util.h"
#include "algorithms.h"

#ifndef RTLD_NOW
#define RTLD_NOW  1
#endif


#ifdef __MINGW32__
#define HAVE_DL_DLOPEN
#define USE_DYNAMIC_LINKING

static int last_error = 0;
    
void*
dlopen (const char *pathname, int mode)
{
  void *h = LoadLibrary (pathname);
  if (!h) 
    {
      log_error ("LoadLibrary failed ec=%d\n", (int)GetLastError());
      last_error = 1;
      return NULL;
    }
  return h;
}

int
dlclose ( void *handle )
{
  last_error = 0;
  return FreeLibrary (handle);
}

char*
dlerror (void)
{
  static char dlerrstr[10];
  if (last_error)
    {
      sprintf(dlerrstr, "%d", (int)GetLastError() );
      return dlerrstr;
    }
  return NULL;
}

void*
dlsym ( void *handle, const char *name )
{
  void *h = GetProcAddress (handle, name);
  if (!h)
    {
      log_error ("GetProcAddress failed ec=%d\n", (int)GetLastError());
      last_error = 1;
    }
  return h;
}
#endif /*__MINGW32__*/

/* We do only support dlopen and the Windows emulation of it. */
#ifndef HAVE_DL_DLOPEN
#undef USE_DYNAMIC_LINKING
#endif


static void *
load_module (const char *name)
{
#ifdef USE_DYNAMIC_LINKING
  const char *err;
  void *handle;
  void *sym;

#ifndef __MINGW32__
  /* Make sure we are not setuid. */
  if (getuid() != geteuid())
    log_bug("trying to load an extension while still setuid\n");
#endif

  handle = dlopen (name, RTLD_NOW);
  if (!handle)
    {
      err=dlerror();
      goto failure;
    }

  sym = dlsym (handle, "idea_get_info");
  if (dlerror ())
    sym = dlsym (handle, "_idea_get_info");
  if ((err=dlerror())) 
    goto failure;

  return sym;
  
 failure:
  log_info ("invalid module `%s': %s\n", name?name:"???", err?err:"???");
  if (handle)
      dlclose (handle);
#endif /*USE_DYNAMIC_LINKING*/
  return NULL;
}

#ifdef __riscos__
typedef
const char *(*INFO_CAST)(int, size_t*, size_t*, size_t*,
                         int  (**)( void *, byte *, unsigned),
                         void (**)( void *, byte *, byte *),
                         void (**)( void *, byte *, byte *));
#endif /* __riscos__ */

const char *
idea_get_info( int algo, size_t *keylen,
		   size_t *blocksize, size_t *contextsize,
		   int	(**r_setkey)( void *c, byte *key, unsigned keylen ),
		   void (**r_encrypt)( void *c, byte *outbuf, byte *inbuf ),
		   void (**r_decrypt)( void *c, byte *outbuf, byte *inbuf )
		 )
{
  static int initialized;
  static const char * (*info_fnc)(int, size_t*, size_t*, size_t*,
                                  int  (**)( void *, byte *, unsigned),
                                  void (**)( void *, byte *, byte *),
                                  void (**)( void *, byte *, byte *));
  const char *rstr;
  int i;

  if (!initialized)
    {
      initialized = 1;
      for (i=0; (rstr = dynload_enum_module_names (i)); i++)
        {
#ifndef __riscos__
          info_fnc = load_module (rstr);
#else /* __riscos__ */
          info_fnc = (INFO_CAST) load_module (rstr);
#endif /* __riscos__ */
          if (info_fnc)
            break;
        }
    }
  if (!info_fnc)
    return NULL; /* dynloadable module not found. */
  rstr = info_fnc (algo, keylen, blocksize, contextsize,
                   r_setkey, r_encrypt, r_decrypt);
  if (rstr && *keylen == 128 && *blocksize == 8
      && *r_setkey && *r_encrypt && r_decrypt)
    return rstr;
  return NULL;
}