Blame sunrpc/xcrypt.c

Packit 6c4009
/*
Packit 6c4009
 * Copyright (c) 2010, Oracle America, Inc.
Packit 6c4009
 *
Packit 6c4009
 * Redistribution and use in source and binary forms, with or without
Packit 6c4009
 * modification, are permitted provided that the following conditions are
Packit 6c4009
 * met:
Packit 6c4009
 *
Packit 6c4009
 *     * Redistributions of source code must retain the above copyright
Packit 6c4009
 *       notice, this list of conditions and the following disclaimer.
Packit 6c4009
 *     * Redistributions in binary form must reproduce the above
Packit 6c4009
 *       copyright notice, this list of conditions and the following
Packit 6c4009
 *       disclaimer in the documentation and/or other materials
Packit 6c4009
 *       provided with the distribution.
Packit 6c4009
 *     * Neither the name of the "Oracle America, Inc." nor the names of its
Packit 6c4009
 *       contributors may be used to endorse or promote products derived
Packit 6c4009
 *       from this software without specific prior written permission.
Packit 6c4009
 *
Packit 6c4009
 *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
Packit 6c4009
 *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
Packit 6c4009
 *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
Packit 6c4009
 *   FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
Packit 6c4009
 *   COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
Packit 6c4009
 *   INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
Packit 6c4009
 *   DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
Packit 6c4009
 *   GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
Packit 6c4009
 *   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
Packit 6c4009
 *   WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
Packit 6c4009
 *   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
Packit 6c4009
 *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Packit 6c4009
 */
Packit 6c4009
Packit 6c4009
#if 0
Packit 6c4009
#ident	"@(#)xcrypt.c	1.11	94/08/23 SMI"
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
#if !defined(lint) && defined(SCCSIDS)
Packit 6c4009
static char sccsid[] = "@(#)xcrypt.c 1.3 89/03/24 Copyr 1986 Sun Micro";
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
/*
Packit 6c4009
 * xcrypt.c: Hex encryption/decryption and utility routines
Packit 6c4009
 */
Packit 6c4009
Packit 6c4009
#include <ctype.h>
Packit 6c4009
#include <stdio.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
#include <sys/types.h>
Packit 6c4009
#include <rpc/des_crypt.h>
Packit 6c4009
#include <shlib-compat.h>
Packit 6c4009
Packit 6c4009
static const char hex[16] =
Packit 6c4009
{
Packit 6c4009
  '0', '1', '2', '3', '4', '5', '6', '7',
Packit 6c4009
  '8', '9', 'a', 'b', 'c', 'd', 'e', 'f',
Packit 6c4009
};
Packit 6c4009
Packit 6c4009
Packit 6c4009
#ifdef _LIBC
Packit 6c4009
# define hexval(c) \
Packit 6c4009
  (c >= '0' && c <= '9'							      \
Packit 6c4009
   ? c - '0'								      \
Packit 6c4009
   : ({	int upp = toupper (c);						      \
Packit 6c4009
	upp >= 'A' && upp <= 'Z' ? upp - 'A' + 10 : -1; }))
Packit 6c4009
#else
Packit 6c4009
static char hexval (char);
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
static void hex2bin (int, char *, char *);
Packit 6c4009
static void bin2hex (int, unsigned char *, char *);
Packit 6c4009
void passwd2des_internal (char *pw, char *key);
Packit 6c4009
#ifdef _LIBC
Packit 6c4009
libc_hidden_proto (passwd2des_internal)
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
/*
Packit 6c4009
 * Turn password into DES key
Packit 6c4009
 */
Packit 6c4009
void
Packit 6c4009
passwd2des_internal (char *pw, char *key)
Packit 6c4009
{
Packit 6c4009
  int i;
Packit 6c4009
Packit 6c4009
  memset (key, 0, 8);
Packit 6c4009
  for (i = 0; *pw && i < 8; ++i)
Packit 6c4009
    key[i] ^= *pw++ << 1;
Packit 6c4009
Packit 6c4009
  des_setparity (key);
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
#ifdef _LIBC
Packit 6c4009
libc_hidden_def (passwd2des_internal)
Packit 6c4009
libc_sunrpc_symbol(passwd2des_internal, passwd2des, GLIBC_2_1)
Packit 6c4009
#else
Packit 6c4009
void passwd2des (char *pw, char *key)
Packit 6c4009
{
Packit 6c4009
  return passwd2des_internal (pw, key);
Packit 6c4009
}
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
/*
Packit 6c4009
 * Encrypt a secret key given passwd
Packit 6c4009
 * The secret key is passed and returned in hex notation.
Packit 6c4009
 * Its length must be a multiple of 16 hex digits (64 bits).
Packit 6c4009
 */
Packit 6c4009
int
Packit 6c4009
xencrypt (char *secret, char *passwd)
Packit 6c4009
{
Packit 6c4009
  char key[8];
Packit 6c4009
  char ivec[8];
Packit 6c4009
  char *buf;
Packit 6c4009
  int err;
Packit 6c4009
  int len;
Packit 6c4009
Packit 6c4009
  len = strlen (secret) / 2;
Packit 6c4009
  buf = malloc ((unsigned) len);
Packit 6c4009
  hex2bin (len, secret, buf);
Packit 6c4009
  passwd2des_internal (passwd, key);
Packit 6c4009
  memset (ivec, 0, 8);
Packit 6c4009
Packit 6c4009
  err = cbc_crypt (key, buf, len, DES_ENCRYPT | DES_HW, ivec);
Packit 6c4009
  if (DES_FAILED (err))
Packit 6c4009
    {
Packit 6c4009
      free (buf);
Packit 6c4009
      return 0;
Packit 6c4009
    }
Packit 6c4009
  bin2hex (len, (unsigned char *) buf, secret);
Packit 6c4009
  free (buf);
Packit 6c4009
  return 1;
Packit 6c4009
}
Packit 6c4009
libc_hidden_nolink_sunrpc (xencrypt, GLIBC_2_0)
Packit 6c4009
Packit 6c4009
/*
Packit 6c4009
 * Decrypt secret key using passwd
Packit 6c4009
 * The secret key is passed and returned in hex notation.
Packit 6c4009
 * Once again, the length is a multiple of 16 hex digits
Packit 6c4009
 */
Packit 6c4009
int
Packit 6c4009
xdecrypt (char *secret, char *passwd)
Packit 6c4009
{
Packit 6c4009
  char key[8];
Packit 6c4009
  char ivec[8];
Packit 6c4009
  char *buf;
Packit 6c4009
  int err;
Packit 6c4009
  int len;
Packit 6c4009
Packit 6c4009
  len = strlen (secret) / 2;
Packit 6c4009
  buf = malloc ((unsigned) len);
Packit 6c4009
Packit 6c4009
  hex2bin (len, secret, buf);
Packit 6c4009
  passwd2des_internal (passwd, key);
Packit 6c4009
  memset (ivec, 0, 8);
Packit 6c4009
Packit 6c4009
  err = cbc_crypt (key, buf, len, DES_DECRYPT | DES_HW, ivec);
Packit 6c4009
  if (DES_FAILED (err))
Packit 6c4009
    {
Packit 6c4009
      free (buf);
Packit 6c4009
      return 0;
Packit 6c4009
    }
Packit 6c4009
  bin2hex (len, (unsigned char *) buf, secret);
Packit 6c4009
  free (buf);
Packit 6c4009
  return 1;
Packit 6c4009
}
Packit 6c4009
#ifdef EXPORT_RPC_SYMBOLS
Packit 6c4009
libc_hidden_def (xdecrypt)
Packit 6c4009
#else
Packit 6c4009
libc_hidden_nolink_sunrpc (xdecrypt, GLIBC_2_1)
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
/*
Packit 6c4009
 * Hex to binary conversion
Packit 6c4009
 */
Packit 6c4009
static void
Packit 6c4009
hex2bin (int len, char *hexnum, char *binnum)
Packit 6c4009
{
Packit 6c4009
  int i;
Packit 6c4009
Packit 6c4009
  for (i = 0; i < len; i++)
Packit 6c4009
    *binnum++ = 16 * hexval (hexnum[2 * i]) + hexval (hexnum[2 * i + 1]);
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
/*
Packit 6c4009
 * Binary to hex conversion
Packit 6c4009
 */
Packit 6c4009
static void
Packit 6c4009
bin2hex (int len, unsigned char *binnum, char *hexnum)
Packit 6c4009
{
Packit 6c4009
  int i;
Packit 6c4009
  unsigned val;
Packit 6c4009
Packit 6c4009
  for (i = 0; i < len; i++)
Packit 6c4009
    {
Packit 6c4009
      val = binnum[i];
Packit 6c4009
      hexnum[i * 2] = hex[val >> 4];
Packit 6c4009
      hexnum[i * 2 + 1] = hex[val & 0xf];
Packit 6c4009
    }
Packit 6c4009
  hexnum[len * 2] = 0;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
#ifndef _LIBC
Packit 6c4009
static char
Packit 6c4009
hexval (char c)
Packit 6c4009
{
Packit 6c4009
  if (c >= '0' && c <= '9')
Packit 6c4009
    return (c - '0');
Packit 6c4009
  else if (c >= 'a' && c <= 'z')
Packit 6c4009
    return (c - 'a' + 10);
Packit 6c4009
  else if (c >= 'A' && c <= 'Z')
Packit 6c4009
    return (c - 'A' + 10);
Packit 6c4009
  else
Packit 6c4009
    return -1;
Packit 6c4009
}
Packit 6c4009
#endif