~ubuntu-branches/ubuntu/dapper/gnupg2/dapper

« back to all changes in this revision

Viewing changes to cipher/idea-stub.c

  • Committer: Bazaar Package Importer
  • Author(s): Andreas Mueller
  • Date: 2005-03-29 10:30:32 UTC
  • Revision ID: james.westby@ubuntu.com-20050329103032-sj42n2ain3ipx310
Tags: upstream-1.9.15
ImportĀ upstreamĀ versionĀ 1.9.15

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* idea-stub.c - Dummy module for the deprecated IDEA cipher.
 
2
 * Copyright (C) 2002, 2003 Free Software Foundation, Inc.
 
3
 *
 
4
 * This file is part of GnuPG.
 
5
 *
 
6
 * GnuPG is free software; you can redistribute it and/or modify
 
7
 * it under the terms of the GNU General Public License as published by
 
8
 * the Free Software Foundation; either version 2 of the License, or
 
9
 * (at your option) any later version.
 
10
 *
 
11
 * GnuPG is distributed in the hope that it will be useful,
 
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
 * GNU General Public License for more details.
 
15
 *
 
16
 * You should have received a copy of the GNU General Public License
 
17
 * along with this program; if not, write to the Free Software
 
18
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
 
19
 */
 
20
 
 
21
/* IDEA is a patented algorithm and therefore the use of IDEA in
 
22
   countries where this patent is valid can not be allowed due to the
 
23
   terms of the GNU General Public License.  Those restrictions are
 
24
   there to help protecting the freedom of software.  For more
 
25
   information on the nonsense of software patents and the general
 
26
   problem with this, please see http://www.noepatents.org.
 
27
 
 
28
   However for research purposes and in certain situations it might be
 
29
   useful to use this algorithm anyway.  
 
30
 
 
31
   We provide this stub which will dynload a idea module and is only 
 
32
   used if the configure run did't found statically linked file.
 
33
   See http://www.gnupg.org/why-not-dea.html for details.
 
34
*/
 
35
 
 
36
#include <config.h>
 
37
#include <stdio.h>
 
38
#include <stdlib.h>
 
39
#include <string.h>
 
40
#include <unistd.h>
 
41
#ifdef HAVE_DL_DLOPEN
 
42
#include <dlfcn.h>
 
43
#endif
 
44
#ifdef _WIN32
 
45
#include <windows.h>
 
46
#endif
 
47
#include "util.h"
 
48
#include "algorithms.h"
 
49
 
 
50
#ifndef RTLD_NOW
 
51
#define RTLD_NOW  1
 
52
#endif
 
53
 
 
54
#ifdef _WIN32
 
55
#define HAVE_DL_DLOPEN
 
56
#define USE_DYNAMIC_LINKING
 
57
 
 
58
static int last_error = 0;
 
59
    
 
60
void*
 
61
dlopen (const char *pathname, int mode)
 
62
{
 
63
  void *h = LoadLibrary (pathname);
 
64
  if (!h) 
 
65
    {
 
66
      log_error ("LoadLibrary failed: %s\n", w32_strerror (errno));
 
67
      last_error = 1;
 
68
      return NULL;
 
69
    }
 
70
  return h;
 
71
}
 
72
 
 
73
int
 
74
dlclose ( void *handle )
 
75
{
 
76
  last_error = 0;
 
77
  return FreeLibrary (handle);
 
78
}
 
79
 
 
80
 
 
81
const char*
 
82
dlerror (void)
 
83
{
 
84
  if (last_error)
 
85
    return w32_strerror (0);
 
86
  return NULL;
 
87
}
 
88
 
 
89
void*
 
90
dlsym (void *handle, const char *name)
 
91
{
 
92
  void *h = GetProcAddress (handle, name);
 
93
  if (!h)
 
94
    {
 
95
      log_error ("GetProcAddress failed: %s\n", w32_strerror (errno));
 
96
      last_error = 1;
 
97
    }
 
98
  return h;
 
99
}
 
100
#endif /*_WIN32*/
 
101
 
 
102
/* We do only support dlopen and the Windows emulation of it. */
 
103
#ifndef HAVE_DL_DLOPEN
 
104
#undef USE_DYNAMIC_LINKING
 
105
#endif
 
106
 
 
107
typedef
 
108
const char *(*INFO_FNC)(int, size_t*, size_t*, size_t*,
 
109
                        int  (**)( void *, const byte *, unsigned),
 
110
                        void (**)( void *, byte *, const byte *),
 
111
                        void (**)( void *, byte *, const byte *));
 
112
 
 
113
static INFO_FNC
 
114
load_module (const char *name)
 
115
{
 
116
#ifdef USE_DYNAMIC_LINKING
 
117
  const char *err;
 
118
  void *handle;
 
119
  void *sym;
 
120
 
 
121
#ifndef _WIN32
 
122
  /* Make sure we are not setuid. */
 
123
  if (getuid() != geteuid())
 
124
    log_bug("trying to load an extension while still setuid\n");
 
125
#endif
 
126
 
 
127
  handle = dlopen (name, RTLD_NOW);
 
128
  if (!handle)
 
129
    {
 
130
      err=dlerror();
 
131
      goto failure;
 
132
    }
 
133
 
 
134
  sym = dlsym (handle, "idea_get_info");
 
135
  if (dlerror ())
 
136
    sym = dlsym (handle, "_idea_get_info");
 
137
  if ((err=dlerror())) 
 
138
    goto failure;
 
139
 
 
140
  return sym;
 
141
  
 
142
 failure:
 
143
  log_info ("invalid module `%s': %s\n", name?name:"???", err?err:"???");
 
144
  if (handle)
 
145
      dlclose (handle);
 
146
#endif /*USE_DYNAMIC_LINKING*/
 
147
  return NULL;
 
148
}
 
149
 
 
150
const char *
 
151
idea_get_info( int algo, size_t *keylen,
 
152
               size_t *blocksize, size_t *contextsize,
 
153
               int (**r_setkey)( void *c, const byte *key, unsigned keylen ),
 
154
               void (**r_encrypt)( void *c, byte *outbuf, const byte *inbuf ),
 
155
               void (**r_decrypt)( void *c, byte *outbuf, const byte *inbuf )
 
156
               )
 
157
{
 
158
  static int initialized;
 
159
  static INFO_FNC info_fnc;
 
160
  const char *rstr;
 
161
  int i;
 
162
 
 
163
  if (!initialized)
 
164
    {
 
165
      initialized = 1;
 
166
      for (i=0; (rstr = dynload_enum_module_names (i)); i++)
 
167
        {
 
168
          info_fnc = load_module (rstr);
 
169
          if (info_fnc)
 
170
            break;
 
171
        }
 
172
    }
 
173
  if (!info_fnc)
 
174
    return NULL; /* dynloadable module not found. */
 
175
  rstr = info_fnc (algo, keylen, blocksize, contextsize,
 
176
                   r_setkey, r_encrypt, r_decrypt);
 
177
  if (rstr && *keylen == 128 && *blocksize == 8
 
178
      && *r_setkey && *r_encrypt && r_decrypt)
 
179
    return rstr;
 
180
  return NULL;
 
181
}