/usr/share/doc/glibc-doc/html/libc_32.html is in glibc-doc 2.15-0ubuntu10.18.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html401/loose.dtd">
<html>
<!-- This file documents the GNU C library.
This is Edition 0.13, last updated 2011-07-19,
of The GNU C Library Reference Manual, for version
2.14 (Ubuntu EGLIBC 2.15-0ubuntu10.18) .
Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2001, 2002,
2003, 2007, 2008, 2010, 2011 Free Software Foundation, Inc.
Permission is granted to copy, distribute and/or modify this document
under the terms of the GNU Free Documentation License, Version 1.3 or
any later version published by the Free Software Foundation; with the
Invariant Sections being "Free Software Needs Free Documentation"
and "GNU Lesser General Public License", the Front-Cover texts being
"A GNU Manual", and with the Back-Cover Texts as in (a) below. A
copy of the license is included in the section entitled "GNU Free
Documentation License".
(a) The FSF's Back-Cover Text is: "You have the freedom to
copy and modify this GNU manual. Buying copies from the FSF
supports it in developing GNU and promoting software freedom."
-->
<!-- Created on March 23, 2017 by texi2html 1.82
texi2html was written by:
Lionel Cons <Lionel.Cons@cern.ch> (original author)
Karl Berry <karl@freefriends.org>
Olaf Bachmann <obachman@mathematik.uni-kl.de>
and many others.
Maintained by: Many creative people.
Send bugs and suggestions to <texi2html-bug@nongnu.org>
-->
<head>
<title>The GNU C Library: 32. DES Encryption and Password Handling</title>
<meta name="description" content="The GNU C Library: 32. DES Encryption and Password Handling">
<meta name="keywords" content="The GNU C Library: 32. DES Encryption and Password Handling">
<meta name="resource-type" content="document">
<meta name="distribution" content="global">
<meta name="Generator" content="texi2html 1.82">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<style type="text/css">
<!--
a.summary-letter {text-decoration: none}
blockquote.smallquotation {font-size: smaller}
pre.display {font-family: serif}
pre.format {font-family: serif}
pre.menu-comment {font-family: serif}
pre.menu-preformatted {font-family: serif}
pre.smalldisplay {font-family: serif; font-size: smaller}
pre.smallexample {font-size: smaller}
pre.smallformat {font-family: serif; font-size: smaller}
pre.smalllisp {font-size: smaller}
span.roman {font-family:serif; font-weight:normal;}
span.sansserif {font-family:sans-serif; font-weight:normal;}
ul.toc {list-style: none}
-->
</style>
</head>
<body lang="en" bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#800080" alink="#FF0000">
<a name="Cryptographic-Functions"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="libc_31.html#String-Parameters" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#Legal-Problems" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="libc_31.html#System-Configuration" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_33.html#Debugging-Support" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="DES-Encryption-and-Password-Handling"></a>
<h1 class="chapter">32. DES Encryption and Password Handling</h1>
<p>On many systems, it is unnecessary to have any kind of user
authentication; for instance, a workstation which is not connected to a
network probably does not need any user authentication, because to use
the machine an intruder must have physical access.
</p>
<p>Sometimes, however, it is necessary to be sure that a user is authorized
to use some service a machine provides—for instance, to log in as a
particular user id (see section <a href="libc_29.html#Users-and-Groups">Users and Groups</a>). One traditional way of
doing this is for each user to choose a secret <em>password</em>; then, the
system can ask someone claiming to be a user what the user’s password
is, and if the person gives the correct password then the system can
grant the appropriate privileges.
</p>
<p>If all the passwords are just stored in a file somewhere, then this file
has to be very carefully protected. To avoid this, passwords are run
through a <em>one-way function</em>, a function which makes it difficult to
work out what its input was by looking at its output, before storing in
the file.
</p>
<p>The GNU C library provides a one-way function that is compatible with
the behavior of the <code>crypt</code> function introduced in FreeBSD 2.0.
It supports two one-way algorithms: one based on the MD5
message-digest algorithm that is compatible with modern BSD systems,
and the other based on the Data Encryption Standard (DES) that is
compatible with Unix systems.
</p>
<p>It also provides support for Secure RPC, and some library functions that
can be used to perform normal DES encryption.
</p>
<table class="menu" border="0" cellspacing="0">
<tr><td align="left" valign="top"><a href="#Legal-Problems">32.1 Legal Problems</a></td><td> </td><td align="left" valign="top"> This software can get you locked up, or worse.
</td></tr>
<tr><td align="left" valign="top"><a href="#getpass">32.2 Reading Passwords</a></td><td> </td><td align="left" valign="top"> Prompting the user for a password.
</td></tr>
<tr><td align="left" valign="top"><a href="#crypt">32.3 Encrypting Passwords</a></td><td> </td><td align="left" valign="top"> A one-way function for passwords.
</td></tr>
<tr><td align="left" valign="top"><a href="#DES-Encryption">32.4 DES Encryption</a></td><td> </td><td align="left" valign="top"> Routines for DES encryption.
</td></tr>
</table>
<hr size="6">
<a name="Legal-Problems"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Cryptographic-Functions" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#getpass" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="#Cryptographic-Functions" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#Cryptographic-Functions" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_33.html#Debugging-Support" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Legal-Problems-1"></a>
<h2 class="section">32.1 Legal Problems</h2>
<p>Because of the continuously changing state of the law, it’s not possible
to provide a definitive survey of the laws affecting cryptography.
Instead, this section warns you of some of the known trouble spots; this
may help you when you try to find out what the laws of your country are.
</p>
<p>Some countries require that you have a licence to use, possess, or import
cryptography. These countries are believed to include Byelorussia,
Burma, India, Indonesia, Israel, Kazakhstan, Pakistan, Russia, and Saudi
Arabia.
</p>
<p>Some countries restrict the transmission of encrypted messages by radio;
some telecommunications carriers restrict the transmission of encrypted
messages over their network.
</p>
<p>Many countries have some form of export control for encryption software.
The Wassenaar Arrangement is a multilateral agreement between 33
countries (Argentina, Australia, Austria, Belgium, Bulgaria, Canada, the
Czech Republic, Denmark, Finland, France, Germany, Greece, Hungary,
Ireland, Italy, Japan, Luxembourg, the Netherlands, New Zealand, Norway,
Poland, Portugal, the Republic of Korea, Romania, the Russian
Federation, the Slovak Republic, Spain, Sweden, Switzerland, Turkey,
Ukraine, the United Kingdom and the United States) which restricts some
kinds of encryption exports. Different countries apply the arrangement
in different ways; some do not allow the exception for certain kinds of
“public domain” software (which would include this library), some
only restrict the export of software in tangible form, and others impose
significant additional restrictions.
</p>
<p>The United States has additional rules. This software would generally
be exportable under 15 CFR 740.13(e), which permits exports of
“encryption source code” which is “publicly available” and which is
“not subject to an express agreement for the payment of a licensing fee or
royalty for commercial production or sale of any product developed with
the source code” to most countries.
</p>
<p>The rules in this area are continuously changing. If you know of any
information in this manual that is out-of-date, please report it to
the bug database. See section <a href="libc_36.html#Reporting-Bugs">Reporting Bugs</a>.
</p>
<hr size="6">
<a name="getpass"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Legal-Problems" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#crypt" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="#Cryptographic-Functions" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#Cryptographic-Functions" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_33.html#Debugging-Support" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Reading-Passwords"></a>
<h2 class="section">32.2 Reading Passwords</h2>
<p>When reading in a password, it is desirable to avoid displaying it on
the screen, to help keep it secret. The following function handles this
in a convenient way.
</p>
<dl>
<dt><a name="index-getpass"></a><u>Function:</u> char * <b>getpass</b><i> (const char *<var>prompt</var>)</i></dt>
<dd>
<p><code>getpass</code> outputs <var>prompt</var>, then reads a string in from the
terminal without echoing it. It tries to connect to the real terminal,
‘<tt>/dev/tty</tt>’, if possible, to encourage users not to put plaintext
passwords in files; otherwise, it uses <code>stdin</code> and <code>stderr</code>.
<code>getpass</code> also disables the INTR, QUIT, and SUSP characters on the
terminal using the <code>ISIG</code> terminal attribute (see section <a href="libc_17.html#Local-Modes">Local Modes</a>).
The terminal is flushed before and after <code>getpass</code>, so that
characters of a mistyped password are not accidentally visible.
</p>
<p>In other C libraries, <code>getpass</code> may only return the first
<code>PASS_MAX</code> bytes of a password. The GNU C library has no limit, so
<code>PASS_MAX</code> is undefined.
</p>
<p>The prototype for this function is in ‘<tt>unistd.h</tt>’. <code>PASS_MAX</code>
would be defined in ‘<tt>limits.h</tt>’.
</p></dd></dl>
<p>This precise set of operations may not suit all possible situations. In
this case, it is recommended that users write their own <code>getpass</code>
substitute. For instance, a very simple substitute is as follows:
</p>
<table><tr><td> </td><td><pre class="smallexample">#include <termios.h>
#include <stdio.h>
ssize_t
my_getpass (char **lineptr, size_t *n, FILE *stream)
{
struct termios old, new;
int nread;
/* <span class="roman">Turn echoing off and fail if we can't.</span> */
if (tcgetattr (fileno (stream), &old) != 0)
return -1;
new = old;
new.c_lflag &= ~ECHO;
if (tcsetattr (fileno (stream), TCSAFLUSH, &new) != 0)
return -1;
/* <span class="roman">Read the password.</span> */
nread = getline (lineptr, n, stream);
/* <span class="roman">Restore terminal.</span> */
(void) tcsetattr (fileno (stream), TCSAFLUSH, &old);
return nread;
}
</pre></td></tr></table>
<p>The substitute takes the same parameters as <code>getline</code>
(see section <a href="libc_12.html#Line-Input">Line-Oriented Input</a>); the user must print any prompt desired.
</p>
<hr size="6">
<a name="crypt"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#getpass" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#DES-Encryption" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="#Cryptographic-Functions" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#Cryptographic-Functions" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_33.html#Debugging-Support" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Encrypting-Passwords"></a>
<h2 class="section">32.3 Encrypting Passwords</h2>
<dl>
<dt><a name="index-crypt"></a><u>Function:</u> char * <b>crypt</b><i> (const char *<var>key</var>, const char *<var>salt</var>)</i></dt>
<dd>
<p>The <code>crypt</code> function takes a password, <var>key</var>, as a string, and
a <var>salt</var> character array which is described below, and returns a
printable ASCII string which starts with another salt. It is believed
that, given the output of the function, the best way to find a <var>key</var>
that will produce that output is to guess values of <var>key</var> until the
original value of <var>key</var> is found.
</p>
<p>The <var>salt</var> parameter does two things. Firstly, it selects which
algorithm is used, the MD5-based one or the DES-based one. Secondly, it
makes life harder for someone trying to guess passwords against a file
containing many passwords; without a <var>salt</var>, an intruder can make a
guess, run <code>crypt</code> on it once, and compare the result with all the
passwords. With a <var>salt</var>, the intruder must run <code>crypt</code> once
for each different salt.
</p>
<p>For the MD5-based algorithm, the <var>salt</var> should consist of the string
<code>$1$</code>, followed by up to 8 characters, terminated by either
another <code>$</code> or the end of the string. The result of <code>crypt</code>
will be the <var>salt</var>, followed by a <code>$</code> if the salt didn’t end
with one, followed by 22 characters from the alphabet
<code>./0-9A-Za-z</code>, up to 34 characters total. Every character in the
<var>key</var> is significant.
</p>
<p>For the DES-based algorithm, the <var>salt</var> should consist of two
characters from the alphabet <code>./0-9A-Za-z</code>, and the result of
<code>crypt</code> will be those two characters followed by 11 more from the
same alphabet, 13 in total. Only the first 8 characters in the
<var>key</var> are significant.
</p>
<p>The MD5-based algorithm has no limit on the useful length of the
password used, and is slightly more secure. It is therefore preferred
over the DES-based algorithm.
</p>
<p>When the user enters their password for the first time, the <var>salt</var>
should be set to a new string which is reasonably random. To verify a
password against the result of a previous call to <code>crypt</code>, pass
the result of the previous call as the <var>salt</var>.
</p></dd></dl>
<p>The following short program is an example of how to use <code>crypt</code> the
first time a password is entered. Note that the <var>salt</var> generation
is just barely acceptable; in particular, it is not unique between
machines, and in many applications it would not be acceptable to let an
attacker know what time the user’s password was last set.
</p>
<table><tr><td> </td><td><pre class="smallexample">#include <stdio.h>
#include <time.h>
#include <unistd.h>
#include <crypt.h>
int
main(void)
{
unsigned long seed[2];
char salt[] = "$1$........";
const char *const seedchars =
"./0123456789ABCDEFGHIJKLMNOPQRST"
"UVWXYZabcdefghijklmnopqrstuvwxyz";
char *password;
int i;
/* <span class="roman">Generate a (not very) random seed.
You should do it better than this...</span> */
seed[0] = time(NULL);
seed[1] = getpid() ^ (seed[0] >> 14 & 0x30000);
/* <span class="roman">Turn it into printable characters from `seedchars'.</span> */
for (i = 0; i < 8; i++)
salt[3+i] = seedchars[(seed[i/5] >> (i%5)*6) & 0x3f];
/* <span class="roman">Read in the user's password and encrypt it.</span> */
password = crypt(getpass("Password:"), salt);
/* <span class="roman">Print the results.</span> */
puts(password);
return 0;
}
</pre></td></tr></table>
<p>The next program shows how to verify a password. It prompts the user
for a password and prints “Access granted.” if the user types
<code>GNU libc manual</code>.
</p>
<table><tr><td> </td><td><pre class="smallexample">#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <crypt.h>
int
main(void)
{
/* <span class="roman">Hashed form of "GNU libc manual".</span> */
const char *const pass = "$1$/iSaq7rB$EoUw5jJPPvAPECNaaWzMK/";
char *result;
int ok;
</pre><pre class="smallexample"> /* <span class="roman">Read in the user's password and encrypt it,
passing the expected password in as the salt.</span> */
result = crypt(getpass("Password:"), pass);
</pre><pre class="smallexample">
/* <span class="roman">Test the result.</span> */
ok = strcmp (result, pass) == 0;
puts(ok ? "Access granted." : "Access denied.");
return ok ? 0 : 1;
}
</pre></td></tr></table>
<dl>
<dt><a name="index-crypt_005fr"></a><u>Function:</u> char * <b>crypt_r</b><i> (const char *<var>key</var>, const char *<var>salt</var>, struct crypt_data * <var>data</var>)</i></dt>
<dd>
<p>The <code>crypt_r</code> function does the same thing as <code>crypt</code>, but
takes an extra parameter which includes space for its result (among
other things), so it can be reentrant. <code>data->initialized</code> must be
cleared to zero before the first time <code>crypt_r</code> is called.
</p>
<p>The <code>crypt_r</code> function is a GNU extension.
</p></dd></dl>
<p>The <code>crypt</code> and <code>crypt_r</code> functions are prototyped in the
header ‘<tt>crypt.h</tt>’.
</p>
<hr size="6">
<a name="DES-Encryption"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#crypt" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="libc_33.html#Debugging-Support" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="#Cryptographic-Functions" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#Cryptographic-Functions" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_33.html#Debugging-Support" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="DES-Encryption-1"></a>
<h2 class="section">32.4 DES Encryption</h2>
<p>The Data Encryption Standard is described in the US Government Federal
Information Processing Standards (FIPS) 46-3 published by the National
Institute of Standards and Technology. The DES has been very thoroughly
analyzed since it was developed in the late 1970s, and no new
significant flaws have been found.
</p>
<p>However, the DES uses only a 56-bit key (plus 8 parity bits), and a
machine has been built in 1998 which can search through all possible
keys in about 6 days, which cost about US$200000; faster searches would
be possible with more money. This makes simple DES insecure for most
purposes, and NIST no longer permits new US government systems
to use simple DES.
</p>
<p>For serious encryption functionality, it is recommended that one of the
many free encryption libraries be used instead of these routines.
</p>
<p>The DES is a reversible operation which takes a 64-bit block and a
64-bit key, and produces another 64-bit block. Usually the bits are
numbered so that the most-significant bit, the first bit, of each block
is numbered 1.
</p>
<p>Under that numbering, every 8th bit of the key (the 8th, 16th, and so
on) is not used by the encryption algorithm itself. But the key must
have odd parity; that is, out of bits 1 through 8, and 9 through 16, and
so on, there must be an odd number of ‘1’ bits, and this completely
specifies the unused bits.
</p>
<dl>
<dt><a name="index-setkey"></a><u>Function:</u> void <b>setkey</b><i> (const char *<var>key</var>)</i></dt>
<dd>
<p>The <code>setkey</code> function sets an internal data structure to be an
expanded form of <var>key</var>. <var>key</var> is specified as an array of 64
bits each stored in a <code>char</code>, the first bit is <code>key[0]</code> and
the 64th bit is <code>key[63]</code>. The <var>key</var> should have the correct
parity.
</p></dd></dl>
<dl>
<dt><a name="index-encrypt"></a><u>Function:</u> void <b>encrypt</b><i> (char *<var>block</var>, int <var>edflag</var>)</i></dt>
<dd>
<p>The <code>encrypt</code> function encrypts <var>block</var> if
<var>edflag</var> is 0, otherwise it decrypts <var>block</var>, using a key
previously set by <code>setkey</code>. The result is
placed in <var>block</var>.
</p>
<p>Like <code>setkey</code>, <var>block</var> is specified as an array of 64 bits each
stored in a <code>char</code>, but there are no parity bits in <var>block</var>.
</p></dd></dl>
<dl>
<dt><a name="index-setkey_005fr"></a><u>Function:</u> void <b>setkey_r</b><i> (const char *<var>key</var>, struct crypt_data * <var>data</var>)</i></dt>
<dt><a name="index-encrypt_005fr"></a><u>Function:</u> void <b>encrypt_r</b><i> (char *<var>block</var>, int <var>edflag</var>, struct crypt_data * <var>data</var>)</i></dt>
<dd>
<p>These are reentrant versions of <code>setkey</code> and <code>encrypt</code>. The
only difference is the extra parameter, which stores the expanded
version of <var>key</var>. Before calling <code>setkey_r</code> the first time,
<code>data->initialized</code> must be cleared to zero.
</p></dd></dl>
<p>The <code>setkey_r</code> and <code>encrypt_r</code> functions are GNU extensions.
<code>setkey</code>, <code>encrypt</code>, <code>setkey_r</code>, and <code>encrypt_r</code> are
defined in ‘<tt>crypt.h</tt>’.
</p>
<dl>
<dt><a name="index-ecb_005fcrypt"></a><u>Function:</u> int <b>ecb_crypt</b><i> (char *<var>key</var>, char *<var>blocks</var>, unsigned <var>len</var>, unsigned <var>mode</var>)</i></dt>
<dd>
<p>The function <code>ecb_crypt</code> encrypts or decrypts one or more blocks
using DES. Each block is encrypted independently.
</p>
<p>The <var>blocks</var> and the <var>key</var> are stored packed in 8-bit bytes, so
that the first bit of the key is the most-significant bit of
<code>key[0]</code> and the 63rd bit of the key is stored as the
least-significant bit of <code>key[7]</code>. The <var>key</var> should have the
correct parity.
</p>
<p><var>len</var> is the number of bytes in <var>blocks</var>. It should be a
multiple of 8 (so that there is a whole number of blocks to encrypt).
<var>len</var> is limited to a maximum of <code>DES_MAXDATA</code> bytes.
</p>
<p>The result of the encryption replaces the input in <var>blocks</var>.
</p>
<p>The <var>mode</var> parameter is the bitwise OR of two of the following:
</p>
<dl compact="compact">
<dt> <code>DES_ENCRYPT</code>
<a name="index-DES_005fENCRYPT"></a>
</dt>
<dd><p>This constant, used in the <var>mode</var> parameter, specifies that
<var>blocks</var> is to be encrypted.
</p>
</dd>
<dt> <code>DES_DECRYPT</code>
<a name="index-DES_005fDECRYPT"></a>
</dt>
<dd><p>This constant, used in the <var>mode</var> parameter, specifies that
<var>blocks</var> is to be decrypted.
</p>
</dd>
<dt> <code>DES_HW</code>
<a name="index-DES_005fHW"></a>
</dt>
<dd><p>This constant, used in the <var>mode</var> parameter, asks to use a hardware
device. If no hardware device is available, encryption happens anyway,
but in software.
</p>
</dd>
<dt> <code>DES_SW</code>
<a name="index-DES_005fSW"></a>
</dt>
<dd><p>This constant, used in the <var>mode</var> parameter, specifies that no
hardware device is to be used.
</p></dd>
</dl>
<p>The result of the function will be one of these values:
</p>
<dl compact="compact">
<dt> <code>DESERR_NONE</code>
<a name="index-DESERR_005fNONE"></a>
</dt>
<dd><p>The encryption succeeded.
</p>
</dd>
<dt> <code>DESERR_NOHWDEVICE</code>
<a name="index-DESERR_005fNOHWDEVICE"></a>
</dt>
<dd><p>The encryption succeeded, but there was no hardware device available.
</p>
</dd>
<dt> <code>DESERR_HWERROR</code>
<a name="index-DESERR_005fHWERROR"></a>
</dt>
<dd><p>The encryption failed because of a hardware problem.
</p>
</dd>
<dt> <code>DESERR_BADPARAM</code>
<a name="index-DESERR_005fBADPARAM"></a>
</dt>
<dd><p>The encryption failed because of a bad parameter, for instance <var>len</var>
is not a multiple of 8 or <var>len</var> is larger than <code>DES_MAXDATA</code>.
</p></dd>
</dl>
</dd></dl>
<dl>
<dt><a name="index-DES_005fFAILED"></a><u>Function:</u> int <b>DES_FAILED</b><i> (int <var>err</var>)</i></dt>
<dd><p>This macro returns 1 if <var>err</var> is a ‘success’ result code from
<code>ecb_crypt</code> or <code>cbc_crypt</code>, and 0 otherwise.
</p></dd></dl>
<dl>
<dt><a name="index-cbc_005fcrypt"></a><u>Function:</u> int <b>cbc_crypt</b><i> (char *<var>key</var>, char *<var>blocks</var>, unsigned <var>len</var>, unsigned <var>mode</var>, char *<var>ivec</var>)</i></dt>
<dd>
<p>The function <code>cbc_crypt</code> encrypts or decrypts one or more blocks
using DES in Cipher Block Chaining mode.
</p>
<p>For encryption in CBC mode, each block is exclusive-ored with <var>ivec</var>
before being encrypted, then <var>ivec</var> is replaced with the result of
the encryption, then the next block is processed. Decryption is the
reverse of this process.
</p>
<p>This has the advantage that blocks which are the same before being
encrypted are very unlikely to be the same after being encrypted, making
it much harder to detect patterns in the data.
</p>
<p>Usually, <var>ivec</var> is set to 8 random bytes before encryption starts.
Then the 8 random bytes are transmitted along with the encrypted data
(without themselves being encrypted), and passed back in as <var>ivec</var>
for decryption. Another possibility is to set <var>ivec</var> to 8 zeroes
initially, and have the first the block encrypted consist of 8 random
bytes.
</p>
<p>Otherwise, all the parameters are similar to those for <code>ecb_crypt</code>.
</p></dd></dl>
<dl>
<dt><a name="index-des_005fsetparity"></a><u>Function:</u> void <b>des_setparity</b><i> (char *<var>key</var>)</i></dt>
<dd>
<p>The function <code>des_setparity</code> changes the 64-bit <var>key</var>, stored
packed in 8-bit bytes, to have odd parity by altering the low bits of
each byte.
</p></dd></dl>
<p>The <code>ecb_crypt</code>, <code>cbc_crypt</code>, and <code>des_setparity</code>
functions and their accompanying macros are all defined in the header
‘<tt>rpc/des_crypt.h</tt>’.
</p><hr size="6">
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Cryptographic-Functions" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="libc_33.html#Debugging-Support" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<p>
<font size="-1">
This document was generated by <em>root</em> on <em>March 23, 2017</em> using <a href="http://www.nongnu.org/texi2html/"><em>texi2html 1.82</em></a>.
</font>
<br>
</p>
</body>
</html>
|