Blame sunrpc/des_crypt.c

Packit 6c4009
/*
Packit 6c4009
 * des_crypt.c, DES encryption library routines
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
#include <sys/types.h>
Packit 6c4009
#include <rpc/des_crypt.h>
Packit 6c4009
#include <shlib-compat.h>
Packit 6c4009
#include "des.h"
Packit 6c4009
Packit 6c4009
extern int _des_crypt (char *, unsigned, struct desparams *);
Packit 6c4009
Packit 6c4009
/*
Packit 6c4009
 * Copy 8 bytes
Packit 6c4009
 */
Packit 6c4009
#define COPY8(src, dst) { \
Packit 6c4009
	register char *a = (char *) dst; \
Packit 6c4009
	register char *b = (char *) src; \
Packit 6c4009
	*a++ = *b++; *a++ = *b++; *a++ = *b++; *a++ = *b++; \
Packit 6c4009
	*a++ = *b++; *a++ = *b++; *a++ = *b++; *a++ = *b++; \
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
/*
Packit 6c4009
 * Copy multiple of 8 bytes
Packit 6c4009
 */
Packit 6c4009
#define DESCOPY(src, dst, len) { \
Packit 6c4009
	register char *a = (char *) dst; \
Packit 6c4009
	register char *b = (char *) src; \
Packit 6c4009
	register int i; \
Packit 6c4009
	for (i = (int) len; i > 0; i -= 8) { \
Packit 6c4009
		*a++ = *b++; *a++ = *b++; *a++ = *b++; *a++ = *b++; \
Packit 6c4009
		*a++ = *b++; *a++ = *b++; *a++ = *b++; *a++ = *b++; \
Packit 6c4009
	} \
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
/*
Packit 6c4009
 * Common code to cbc_crypt() & ecb_crypt()
Packit 6c4009
 */
Packit 6c4009
static int
Packit 6c4009
common_crypt (char *key, char *buf, register unsigned len,
Packit 6c4009
	      unsigned mode, register struct desparams *desp)
Packit 6c4009
{
Packit 6c4009
  register int desdev;
Packit 6c4009
Packit 6c4009
  if ((len % 8) != 0 || len > DES_MAXDATA)
Packit 6c4009
    return DESERR_BADPARAM;
Packit 6c4009
Packit 6c4009
  desp->des_dir =
Packit 6c4009
    ((mode & DES_DIRMASK) == DES_ENCRYPT) ? ENCRYPT : DECRYPT;
Packit 6c4009
Packit 6c4009
  desdev = mode & DES_DEVMASK;
Packit 6c4009
  COPY8 (key, desp->des_key);
Packit 6c4009
  /*
Packit 6c4009
   * software
Packit 6c4009
   */
Packit 6c4009
  if (!_des_crypt (buf, len, desp))
Packit 6c4009
    return DESERR_HWERROR;
Packit 6c4009
Packit 6c4009
  return desdev == DES_SW ? DESERR_NONE : DESERR_NOHWDEVICE;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
/* Note: these cannot be excluded from the build yet, because they are
Packit 6c4009
   still used internally.  */
Packit 6c4009
Packit 6c4009
/*
Packit 6c4009
 * CBC mode encryption
Packit 6c4009
 */
Packit 6c4009
int
Packit 6c4009
cbc_crypt (char *key, char *buf, unsigned int len, unsigned int mode,
Packit 6c4009
	   char *ivec)
Packit 6c4009
{
Packit 6c4009
  int err;
Packit 6c4009
  struct desparams dp;
Packit 6c4009
Packit 6c4009
  dp.des_mode = CBC;
Packit 6c4009
  COPY8 (ivec, dp.des_ivec);
Packit 6c4009
  err = common_crypt (key, buf, len, mode, &dp;;
Packit 6c4009
  COPY8 (dp.des_ivec, ivec);
Packit 6c4009
  return err;
Packit 6c4009
}
Packit 6c4009
hidden_nolink (cbc_crypt, libc, GLIBC_2_1)
Packit 6c4009
Packit 6c4009
/*
Packit 6c4009
 * ECB mode encryption
Packit 6c4009
 */
Packit 6c4009
int
Packit 6c4009
ecb_crypt (char *key, char *buf, unsigned int len, unsigned int mode)
Packit 6c4009
{
Packit 6c4009
  struct desparams dp;
Packit 6c4009
Packit 6c4009
  dp.des_mode = ECB;
Packit 6c4009
  return common_crypt (key, buf, len, mode, &dp;;
Packit 6c4009
}
Packit 6c4009
hidden_nolink (ecb_crypt, libc, GLIBC_2_1)