1
0
mirror of git://git.gnupg.org/gnupg.git synced 2024-11-10 21:38:50 +01:00
gnupg/cipher/misc.c

169 lines
3.5 KiB
C
Raw Normal View History

1998-01-07 21:47:46 +01:00
/* misc.c - utility functions
1998-02-24 19:50:46 +01:00
* Copyright (C) 1998 Free Software Foundation, Inc.
1998-01-07 21:47:46 +01:00
*
1998-02-24 19:50:46 +01:00
* This file is part of GNUPG.
1998-01-07 21:47:46 +01:00
*
1998-02-24 19:50:46 +01:00
* GNUPG is free software; you can redistribute it and/or modify
1998-01-07 21:47:46 +01:00
* 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.
*
1998-02-24 19:50:46 +01:00
* GNUPG is distributed in the hope that it will be useful,
1998-01-07 21:47:46 +01:00
* 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
*/
#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include "util.h"
#include "cipher.h"
static struct { const char *name; int algo;} pubkey_names[] = {
{ "RSA", PUBKEY_ALGO_RSA },
{ "RSA-E", PUBKEY_ALGO_RSA_E },
{ "RSA-S", PUBKEY_ALGO_RSA_S },
{ "ELGAMAL", PUBKEY_ALGO_ELGAMAL },
{ "ELG", PUBKEY_ALGO_ELGAMAL },
{ "DSA", PUBKEY_ALGO_DSA },
{NULL} };
1998-04-30 18:56:10 +02:00
/* Note: the first string is the one used by ascii armor */
1998-01-07 21:47:46 +01:00
static struct { const char *name; int algo;} digest_names[] = {
{ "MD5", DIGEST_ALGO_MD5 },
{ "SHA1", DIGEST_ALGO_SHA1 },
{ "SHA-1", DIGEST_ALGO_SHA1 },
1998-04-30 18:56:10 +02:00
{ "RIPEMD160", DIGEST_ALGO_RMD160 },
1998-01-07 21:47:46 +01:00
{ "RMD160", DIGEST_ALGO_RMD160 },
{ "RMD-160", DIGEST_ALGO_RMD160 },
{ "RIPE-MD-160", DIGEST_ALGO_RMD160 },
1998-04-30 16:06:01 +02:00
{ "TIGER", DIGEST_ALGO_TIGER },
1998-01-07 21:47:46 +01:00
{NULL} };
1998-02-16 21:05:02 +01:00
1998-01-07 21:47:46 +01:00
/****************
* Map a string to the pubkey algo
*/
int
string_to_pubkey_algo( const char *string )
{
int i;
const char *s;
1998-01-16 22:15:24 +01:00
for(i=0; (s=pubkey_names[i].name); i++ )
1998-01-07 21:47:46 +01:00
if( !stricmp( s, string ) )
return pubkey_names[i].algo;
return 0;
}
1998-02-16 21:05:02 +01:00
/****************
* Map a pubkey algo to a string
*/
const char *
pubkey_algo_to_string( int algo )
{
int i;
for(i=0; pubkey_names[i].name; i++ )
if( pubkey_names[i].algo == algo )
return pubkey_names[i].name;
return NULL;
}
1998-01-07 21:47:46 +01:00
/****************
* Map a string to the digest algo
*/
int
string_to_digest_algo( const char *string )
{
int i;
const char *s;
1998-01-16 22:15:24 +01:00
for(i=0; (s=digest_names[i].name); i++ )
1998-01-07 21:47:46 +01:00
if( !stricmp( s, string ) )
return digest_names[i].algo;
return 0;
}
1998-02-16 21:05:02 +01:00
/****************
* Map a digest algo to a string
*/
const char *
digest_algo_to_string( int algo )
{
int i;
for(i=0; digest_names[i].name; i++ )
if( digest_names[i].algo == algo )
return digest_names[i].name;
return NULL;
}
1998-01-07 21:47:46 +01:00
int
check_pubkey_algo( int algo )
1998-04-08 21:49:02 +02:00
{
return check_pubkey_algo2( algo, 0 );
}
/****************
* a usage of 0 means: don't care
*/
int
check_pubkey_algo2( int algo, unsigned usage )
1998-01-07 21:47:46 +01:00
{
switch( algo ) {
1998-03-09 22:44:06 +01:00
case PUBKEY_ALGO_DSA:
1998-04-08 21:49:02 +02:00
if( usage & 2 )
return G10ERR_WR_PUBKEY_ALGO;
return 0;
case PUBKEY_ALGO_ELGAMAL:
return 0;
1998-01-07 21:47:46 +01:00
#ifdef HAVE_RSA_CIPHER
case PUBKEY_ALGO_RSA:
return 0;
1998-04-08 21:49:02 +02:00
#endif
1998-01-07 21:47:46 +01:00
default:
return G10ERR_PUBKEY_ALGO;
}
}
int
check_digest_algo( int algo )
{
switch( algo ) {
1998-04-30 16:06:01 +02:00
#ifdef WITH_TIGER_HASH
case DIGEST_ALGO_TIGER:
#endif
1998-01-07 21:47:46 +01:00
case DIGEST_ALGO_MD5:
case DIGEST_ALGO_RMD160:
case DIGEST_ALGO_SHA1:
return 0;
default:
return G10ERR_DIGEST_ALGO;
}
}