Cross Reference: des.c
xref
: /
osnet-11
/
usr
/
src
/
lib
/
libcrypt
/
common
/
des.c
Home
History
Annotate
Line#
Navigate
Download
Search
only in
./
2
N/A
/*
2
N/A
* CDDL HEADER START
2
N/A
*
2
N/A
* The contents of this file are subject to the terms of the
2
N/A
* Common Development and Distribution License (the "License").
2
N/A
* You may not use this file except in compliance with the License.
2
N/A
*
2
N/A
* You can obtain a copy of the license at
usr
/
src
/
OPENSOLARIS.LICENSE
2
N/A
* or
http://www.opensolaris.org/os/licensing
.
2
N/A
* See the License for the specific language governing permissions
2
N/A
* and limitations under the License.
2
N/A
*
2
N/A
* When distributing Covered Code, include this CDDL HEADER in each
2
N/A
* file and include the License file at
usr
/
src
/
OPENSOLARIS.LICENSE
.
2
N/A
* If applicable, add the following below this CDDL HEADER, with the
2
N/A
* fields enclosed by brackets "[]" replaced with your own identifying
2
N/A
* information: Portions Copyright [yyyy] [name of copyright owner]
2
N/A
*
2
N/A
* CDDL HEADER END
2
N/A
*/
2
N/A
2
N/A
/*
2
N/A
* Copyright 2008 Sun Microsystems, Inc. All rights reserved.
2
N/A
* Use is subject to license terms.
2
N/A
*/
2
N/A
2
N/A
/* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
2
N/A
/* All Rights Reserved */
2
N/A
2
N/A
/*
2
N/A
* Portions of this source code were derived from Berkeley 4.3 BSD
2
N/A
* under license from the Regents of the University of California.
2
N/A
*/
2
N/A
2
N/A
#
pragma
ident
"%Z%%M% %I% %E% SMI"
2
N/A
2
N/A
/*
2
N/A
* DES encryption library routines
2
N/A
*/
2
N/A
2
N/A
#
include
<
sys
/
types.h
>
2
N/A
#
include
<
rpc
/
des_crypt.h
>
2
N/A
#
include
<
sys
/
stat.h
>
2
N/A
#
include
<
fcntl.h
>
2
N/A
#
include
<
unistd.h
>
2
N/A
#
include
<
stropts.h
>
2
N/A
#
ifdef
sun
2
N/A
#
include
<
sys
/
ioctl.h
>
2
N/A
#
include
<
sys
/
des.h
>
2
N/A
#
ifdef
_KERNEL
2
N/A
#
include
<
sys
/
conf.h
>
2
N/A
#
define
getdesfd
() (
cdevsw
[
11
].
d_open
(0, 0) ? -
1
: 0)
2
N/A
#
define
ioctl
(a, b, c) (
cdevsw
[
11
].
d_ioctl
(0, b, c, 0) ? -
1
: 0)
2
N/A
#
ifndef
CRYPT
2
N/A
#
define
__des_crypt
(a, b, c) 0
2
N/A
#
endif
2
N/A
#
else
2
N/A
#
define
getdesfd
() (
open
(
"/
dev
/
des
"
, 0, 0))
2
N/A
#
endif
2
N/A
#
else
2
N/A
#
include
<
des
/
des.h
>
2
N/A
#
endif
2
N/A
2
N/A
#
include
"
des_soft.h
"
2
N/A
2
N/A
/*
2
N/A
* To see if chip is installed
2
N/A
*/
2
N/A
#
define
UNOPENED
(-
2
)
2
N/A
2
N/A
/*
2
N/A
* Copy 8 bytes
2
N/A
*/
2
N/A
#
define
COPY8
(
src
,
dst
) { \
2
N/A
char
*a = (
char
*)
dst
; \
2
N/A
char
*b = (
char
*)
src
; \
2
N/A
*a++ = *b++; *a++ = *b++; *a++ = *b++; *a++ = *b++; \
2
N/A
*a++ = *b++; *a++ = *b++; *a++ = *b++; *a++ = *b++; \
2
N/A
}
2
N/A
2
N/A
/*
2
N/A
* Copy multiple of 8 bytes
2
N/A
*/
2
N/A
#
define
DESCOPY
(
src
,
dst
,
len
) { \
2
N/A
char
*a = (
char
*)
dst
; \
2
N/A
char
*b = (
char
*)
src
; \
2
N/A
int
i; \
2
N/A
for
(i = (
int
)
len
; i > 0; i -=
8
) { \
2
N/A
*a++ = *b++; *a++ = *b++; *a++ = *b++; *a++ = *b++; \
2
N/A
*a++ = *b++; *a++ = *b++; *a++ = *b++; *a++ = *b++; \
2
N/A
} \
2
N/A
}
2
N/A
static
int
common_crypt
(
char
*,
char
*,
unsigned
,
unsigned
,
struct
desparams
*);
2
N/A
2
N/A
/*
2
N/A
* CBC mode encryption
2
N/A
*/
2
N/A
int
2
N/A
cbc_crypt
(
char
*
key
,
char
*
buf
,
size_t
len
,
unsigned
int
mode
,
char
*
ivec
)
2
N/A
{
2
N/A
int
err
= 0;
2
N/A
2
N/A
/* EXPORT DELETE START */
2
N/A
struct
desparams
dp
;
2
N/A
2
N/A
dp
.
des_mode
=
CBC
;
2
N/A
COPY8
(
ivec
,
dp
.
des_ivec
);
2
N/A
err
=
common_crypt
(
key
,
buf
,
len
,
mode
, &
dp
);
2
N/A
COPY8
(
dp
.
des_ivec
,
ivec
);
2
N/A
/* EXPORT DELETE END */
2
N/A
return
(
err
);
2
N/A
}
2
N/A
2
N/A
2
N/A
/*
2
N/A
* ECB mode encryption
2
N/A
*/
2
N/A
int
2
N/A
ecb_crypt
(
char
*
key
,
char
*
buf
,
size_t
len
,
unsigned
int
mode
)
2
N/A
{
2
N/A
int
ret
= 0;
2
N/A
2
N/A
/* EXPORT DELETE START */
2
N/A
struct
desparams
dp
;
2
N/A
2
N/A
dp
.
des_mode
=
ECB
;
2
N/A
ret
=
common_crypt
(
key
,
buf
,
len
,
mode
, &
dp
);
2
N/A
/* EXPORT DELETE END */
2
N/A
return
(
ret
);
2
N/A
}
2
N/A
2
N/A
2
N/A
/* EXPORT DELETE START */
2
N/A
/*
2
N/A
* Common code to cbc_crypt() & ecb_crypt()
2
N/A
*/
2
N/A
static
int
2
N/A
common_crypt
(
char
*
key
,
char
*
buf
,
unsigned
len
,
2
N/A
unsigned
mode
,
struct
desparams
*
desp
)
2
N/A
{
2
N/A
int
desdev
;
2
N/A
int
res
;
2
N/A
int
g_desfd
=
UNOPENED
;
2
N/A
2
N/A
if
((
len
%
8
) != 0 ||
len
>
DES_MAXDATA
) {
2
N/A
return
(
DESERR_BADPARAM
);
2
N/A
}
2
N/A
desp
->
des_dir
=
2
N/A
((
mode
&
DES_DIRMASK
) ==
DES_ENCRYPT
) ?
ENCRYPT
:
DECRYPT
;
2
N/A
2
N/A
desdev
=
mode
&
DES_DEVMASK
;
2
N/A
COPY8
(
key
,
desp
->
des_key
);
2
N/A
#
ifdef
sun
2
N/A
if
(
desdev
==
DES_HW
) {
2
N/A
if
(
g_desfd
< 0) {
2
N/A
if
(
g_desfd
== -
1
|| (
g_desfd
=
getdesfd
()) < 0) {
2
N/A
goto
software
;
/* no hardware device */
2
N/A
}
2
N/A
}
2
N/A
2
N/A
/*
2
N/A
* hardware
2
N/A
*/
2
N/A
desp
->
des_len
=
len
;
2
N/A
if
(
len
<=
DES_QUICKLEN
) {
2
N/A
DESCOPY
(
buf
,
desp
->
des_data
,
len
);
2
N/A
res
=
ioctl
(
g_desfd
, (
int
)
DESIOCQUICK
, (
char
*)
desp
);
2
N/A
DESCOPY
(
desp
->
des_data
,
buf
,
len
);
2
N/A
}
else
{
2
N/A
desp
->
des_buf
= (
uchar_t
*)
buf
;
2
N/A
res
=
ioctl
(
g_desfd
, (
int
)
DESIOCBLOCK
, (
char
*)
desp
);
2
N/A
}
2
N/A
return
(
res
== 0 ?
DESERR_NONE
:
DESERR_HWERROR
);
2
N/A
}
2
N/A
software
:
2
N/A
#
endif
2
N/A
/*
2
N/A
* software
2
N/A
*/
2
N/A
if
(!
__des_crypt
(
buf
,
len
,
desp
)) {
2
N/A
return
(
DESERR_HWERROR
);
2
N/A
}
2
N/A
return
(
desdev
==
DES_SW
?
DESERR_NONE
:
DESERR_NOHWDEVICE
);
2
N/A
}
2
N/A
/* EXPORT DELETE END */