add crypt_r
* missing/crypt.c (crypt_r, setkey_r, encrypt_r): add reentrant versions. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@55234 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
7e053ffeb6
commit
49895f21a6
@ -1,3 +1,8 @@
|
|||||||
|
Wed Jun 1 09:14:30 2016 Nobuyoshi Nakada <nobu@ruby-lang.org>
|
||||||
|
|
||||||
|
* missing/crypt.c (crypt_r, setkey_r, encrypt_r): add reentrant
|
||||||
|
versions.
|
||||||
|
|
||||||
Wed Jun 1 02:25:38 2016 Nobuyoshi Nakada <nobu@ruby-lang.org>
|
Wed Jun 1 02:25:38 2016 Nobuyoshi Nakada <nobu@ruby-lang.org>
|
||||||
|
|
||||||
* missing/crypt.c: fix size macros to use configured values
|
* missing/crypt.c: fix size macros to use configured values
|
||||||
|
133
missing/crypt.c
133
missing/crypt.c
@ -43,6 +43,7 @@ static char sccsid[] = "@(#)crypt.c 8.1 (Berkeley) 6/4/93";
|
|||||||
#include <pwd.h>
|
#include <pwd.h>
|
||||||
#endif
|
#endif
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
#ifndef _PASSWORD_EFMT1
|
#ifndef _PASSWORD_EFMT1
|
||||||
#define _PASSWORD_EFMT1 '_'
|
#define _PASSWORD_EFMT1 '_'
|
||||||
#endif
|
#endif
|
||||||
@ -107,9 +108,6 @@ static char sccsid[] = "@(#)crypt.c 8.1 (Berkeley) 6/4/93";
|
|||||||
#define LARGEDATA
|
#define LARGEDATA
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
void des_setkey(const char *key);
|
|
||||||
void des_cipher(const char *in, char *out, long salt, int num_iter);
|
|
||||||
|
|
||||||
/* compile with "-DSTATIC=int" when profiling */
|
/* compile with "-DSTATIC=int" when profiling */
|
||||||
#ifndef STATIC
|
#ifndef STATIC
|
||||||
#define STATIC static
|
#define STATIC static
|
||||||
@ -315,9 +313,6 @@ permute(unsigned char *cp, C_block *out, register C_block *p, int chars_in)
|
|||||||
}
|
}
|
||||||
#endif /* LARGEDATA */
|
#endif /* LARGEDATA */
|
||||||
|
|
||||||
STATIC void init_des(void);
|
|
||||||
STATIC void init_perm(C_block perm[64/CHUNKBITS][1<<CHUNKBITS], unsigned char p[64], int chars_in, int chars_out);
|
|
||||||
|
|
||||||
#ifdef DEBUG
|
#ifdef DEBUG
|
||||||
STATIC void prtab(const char *s, const unsigned char *t, int num_rows);
|
STATIC void prtab(const char *s, const unsigned char *t, int num_rows);
|
||||||
#endif
|
#endif
|
||||||
@ -465,30 +460,63 @@ static const unsigned char itoa64[] = /* 0..63 => ascii-64 */
|
|||||||
|
|
||||||
/* ===== Tables that are initialized at run time ==================== */
|
/* ===== Tables that are initialized at run time ==================== */
|
||||||
|
|
||||||
|
struct crypt_data {
|
||||||
|
|
||||||
static unsigned char a64toi[128]; /* ascii-64 => 0..63 */
|
unsigned char a64toi[128]; /* ascii-64 => 0..63 */
|
||||||
|
|
||||||
/* Initial key schedule permutation */
|
/* Initial key schedule permutation */
|
||||||
static C_block PC1ROT[64/CHUNKBITS][1<<CHUNKBITS];
|
C_block PC1ROT[64/CHUNKBITS][1<<CHUNKBITS];
|
||||||
|
|
||||||
/* Subsequent key schedule rotation permutations */
|
/* Subsequent key schedule rotation permutations */
|
||||||
static C_block PC2ROT[2][64/CHUNKBITS][1<<CHUNKBITS];
|
C_block PC2ROT[2][64/CHUNKBITS][1<<CHUNKBITS];
|
||||||
|
|
||||||
/* Initial permutation/expansion table */
|
/* Initial permutation/expansion table */
|
||||||
static C_block IE3264[32/CHUNKBITS][1<<CHUNKBITS];
|
C_block IE3264[32/CHUNKBITS][1<<CHUNKBITS];
|
||||||
|
|
||||||
/* Table that combines the S, P, and E operations. */
|
/* Table that combines the S, P, and E operations. */
|
||||||
static long SPE[2][8][64];
|
long SPE[2][8][64];
|
||||||
|
|
||||||
/* compressed/interleaved => final permutation table */
|
/* compressed/interleaved => final permutation table */
|
||||||
static C_block CF6464[64/CHUNKBITS][1<<CHUNKBITS];
|
C_block CF6464[64/CHUNKBITS][1<<CHUNKBITS];
|
||||||
|
|
||||||
|
/* The Key Schedule, filled in by des_setkey() or setkey(). */
|
||||||
|
#define KS_SIZE 16
|
||||||
|
C_block KS[KS_SIZE];
|
||||||
|
|
||||||
/* ==================================== */
|
/* ==================================== */
|
||||||
|
|
||||||
|
C_block constdatablock; /* encryption constant */
|
||||||
|
char cryptresult[1+4+4+11+1]; /* encrypted result */
|
||||||
|
int initialized;
|
||||||
|
};
|
||||||
|
|
||||||
static C_block constdatablock; /* encryption constant */
|
#define a64toi (data->a64toi)
|
||||||
static char cryptresult[1+4+4+11+1]; /* encrypted result */
|
#define PC1ROT (data->PC1ROT)
|
||||||
|
#define PC2ROT (data->PC2ROT)
|
||||||
|
#define IE3264 (data->IE3264)
|
||||||
|
#define SPE (data->SPE)
|
||||||
|
#define CF6464 (data->CF6464)
|
||||||
|
#define KS (data->KS)
|
||||||
|
#define constdatablock (data->constdatablock)
|
||||||
|
#define cryptresult (data->cryptresult)
|
||||||
|
#define des_ready (data->initialized)
|
||||||
|
|
||||||
|
char *crypt(const char *key, const char *setting);
|
||||||
|
void des_setkey(const char *key);
|
||||||
|
void des_cipher(const char *in, char *out, long salt, int num_iter);
|
||||||
|
void setkey(const char *key);
|
||||||
|
void encrypt(char *block, int flag);
|
||||||
|
|
||||||
|
char *crypt_r(const char *key, const char *setting, struct crypt_data *data);
|
||||||
|
void des_setkey_r(const char *key, struct crypt_data *data);
|
||||||
|
void des_cipher_r(const char *in, char *out, long salt, int num_iter, struct crypt_data *data);
|
||||||
|
void setkey_r(const char *key, struct crypt_data *data);
|
||||||
|
void encrypt_r(char *block, int flag, struct crypt_data *data);
|
||||||
|
|
||||||
|
STATIC void init_des(struct crypt_data *);
|
||||||
|
STATIC void init_perm(C_block perm[64/CHUNKBITS][1<<CHUNKBITS], unsigned char p[64], int chars_in, int chars_out);
|
||||||
|
|
||||||
|
static struct crypt_data default_crypt_data;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Return a pointer to static data consisting of the "setting"
|
* Return a pointer to static data consisting of the "setting"
|
||||||
@ -496,6 +524,16 @@ static char cryptresult[1+4+4+11+1]; /* encrypted result */
|
|||||||
*/
|
*/
|
||||||
char *
|
char *
|
||||||
crypt(const char *key, const char *setting)
|
crypt(const char *key, const char *setting)
|
||||||
|
{
|
||||||
|
return crypt_r(key, setting, &default_crypt_data);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Return a pointer to data consisting of the "setting" followed by an
|
||||||
|
* encryption produced by the "key" and "setting".
|
||||||
|
*/
|
||||||
|
char *
|
||||||
|
crypt_r(const char *key, const char *setting, struct crypt_data *data)
|
||||||
{
|
{
|
||||||
register char *encp;
|
register char *encp;
|
||||||
register long i;
|
register long i;
|
||||||
@ -509,7 +547,7 @@ crypt(const char *key, const char *setting)
|
|||||||
key++;
|
key++;
|
||||||
keyblock.b[i] = t;
|
keyblock.b[i] = t;
|
||||||
}
|
}
|
||||||
des_setkey((char *)keyblock.b); /* also initializes "a64toi" */
|
des_setkey_r((char *)keyblock.b, data); /* also initializes "a64toi" */
|
||||||
|
|
||||||
encp = &cryptresult[0];
|
encp = &cryptresult[0];
|
||||||
switch (*setting) {
|
switch (*setting) {
|
||||||
@ -518,14 +556,14 @@ crypt(const char *key, const char *setting)
|
|||||||
* Involve the rest of the password 8 characters at a time.
|
* Involve the rest of the password 8 characters at a time.
|
||||||
*/
|
*/
|
||||||
while (*key) {
|
while (*key) {
|
||||||
des_cipher((char *)&keyblock,
|
des_cipher_r((char *)&keyblock,
|
||||||
(char *)&keyblock, 0L, 1);
|
(char *)&keyblock, 0L, 1, data);
|
||||||
for (i = 0; i < 8; i++) {
|
for (i = 0; i < 8; i++) {
|
||||||
if ((t = 2*(unsigned char)(*key)) != 0)
|
if ((t = 2*(unsigned char)(*key)) != 0)
|
||||||
key++;
|
key++;
|
||||||
keyblock.b[i] ^= t;
|
keyblock.b[i] ^= t;
|
||||||
}
|
}
|
||||||
des_setkey((char *)keyblock.b);
|
des_setkey_r((char *)keyblock.b, data);
|
||||||
}
|
}
|
||||||
|
|
||||||
*encp++ = *setting++;
|
*encp++ = *setting++;
|
||||||
@ -555,8 +593,8 @@ crypt(const char *key, const char *setting)
|
|||||||
salt = (salt<<6) | a64toi[t];
|
salt = (salt<<6) | a64toi[t];
|
||||||
}
|
}
|
||||||
encp += salt_size;
|
encp += salt_size;
|
||||||
des_cipher((char *)&constdatablock, (char *)&rsltblock,
|
des_cipher_r((char *)&constdatablock, (char *)&rsltblock,
|
||||||
salt, num_iter);
|
salt, num_iter, data);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Encode the 64 cipher bits as 11 ascii characters.
|
* Encode the 64 cipher bits as 11 ascii characters.
|
||||||
@ -581,26 +619,25 @@ crypt(const char *key, const char *setting)
|
|||||||
return (cryptresult);
|
return (cryptresult);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
* The Key Schedule, filled in by des_setkey() or setkey().
|
|
||||||
*/
|
|
||||||
#define KS_SIZE 16
|
|
||||||
static C_block KS[KS_SIZE];
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Set up the key schedule from the key.
|
* Set up the key schedule from the key.
|
||||||
*/
|
*/
|
||||||
void
|
void
|
||||||
des_setkey(const char *key)
|
des_setkey(const char *key)
|
||||||
|
{
|
||||||
|
return des_setkey_r(key, &default_crypt_data);
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
des_setkey_r(const char *key, struct crypt_data *data)
|
||||||
{
|
{
|
||||||
register DCL_BLOCK(K, K0, K1);
|
register DCL_BLOCK(K, K0, K1);
|
||||||
register C_block *ptabp;
|
register C_block *ptabp;
|
||||||
register int i;
|
register int i;
|
||||||
static int des_ready = 0;
|
|
||||||
|
|
||||||
if (!des_ready) {
|
if (!des_ready) {
|
||||||
init_des();
|
memset(data, 0, sizeof(*data));
|
||||||
|
init_des(data);
|
||||||
des_ready = 1;
|
des_ready = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -626,6 +663,12 @@ des_setkey(const char *key)
|
|||||||
*/
|
*/
|
||||||
void
|
void
|
||||||
des_cipher(const char *in, char *out, long salt, int num_iter)
|
des_cipher(const char *in, char *out, long salt, int num_iter)
|
||||||
|
{
|
||||||
|
des_cipher_r(in, out, salt, num_iter, &default_crypt_data);
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
des_cipher_r(const char *in, char *out, long salt, int num_iter, struct crypt_data *data)
|
||||||
{
|
{
|
||||||
/* variables that we want in registers, most important first */
|
/* variables that we want in registers, most important first */
|
||||||
#if defined(pdp11)
|
#if defined(pdp11)
|
||||||
@ -741,12 +784,12 @@ des_cipher(const char *in, char *out, long salt, int num_iter)
|
|||||||
* done at compile time, if the compiler were capable of that sort of thing.
|
* done at compile time, if the compiler were capable of that sort of thing.
|
||||||
*/
|
*/
|
||||||
STATIC void
|
STATIC void
|
||||||
init_des(void)
|
init_des(struct crypt_data *data)
|
||||||
{
|
{
|
||||||
register int i, j;
|
register int i, j;
|
||||||
register long k;
|
register long k;
|
||||||
register int tableno;
|
register int tableno;
|
||||||
static unsigned char perm[64], tmp32[32]; /* "static" for speed */
|
unsigned char perm[64], tmp32[32];
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* table that converts chars "./0-9A-Za-z"to integers 0-63.
|
* table that converts chars "./0-9A-Za-z"to integers 0-63.
|
||||||
@ -908,6 +951,12 @@ init_perm(C_block perm[64/CHUNKBITS][1<<CHUNKBITS],
|
|||||||
*/
|
*/
|
||||||
void
|
void
|
||||||
setkey(const char *key)
|
setkey(const char *key)
|
||||||
|
{
|
||||||
|
setkey_r(key, &default_crypt_data);
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
setkey_r(const char *key, struct crypt_data *data)
|
||||||
{
|
{
|
||||||
register int i, j, k;
|
register int i, j, k;
|
||||||
C_block keyblock;
|
C_block keyblock;
|
||||||
@ -920,7 +969,7 @@ setkey(const char *key)
|
|||||||
}
|
}
|
||||||
keyblock.b[i] = k;
|
keyblock.b[i] = k;
|
||||||
}
|
}
|
||||||
des_setkey((char *)keyblock.b);
|
des_setkey_r((char *)keyblock.b, data);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -928,6 +977,12 @@ setkey(const char *key)
|
|||||||
*/
|
*/
|
||||||
void
|
void
|
||||||
encrypt(char *block, int flag)
|
encrypt(char *block, int flag)
|
||||||
|
{
|
||||||
|
encrypt_r(block, flag, &default_crypt_data);
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
encrypt_r(char *block, int flag, struct crypt_data *data)
|
||||||
{
|
{
|
||||||
register int i, j, k;
|
register int i, j, k;
|
||||||
C_block cblock;
|
C_block cblock;
|
||||||
@ -940,7 +995,7 @@ encrypt(char *block, int flag)
|
|||||||
}
|
}
|
||||||
cblock.b[i] = k;
|
cblock.b[i] = k;
|
||||||
}
|
}
|
||||||
des_cipher((char *)&cblock, (char *)&cblock, 0L, (flag ? -1: 1));
|
des_cipher_r((char *)&cblock, (char *)&cblock, 0L, (flag ? -1: 1), data);
|
||||||
for (i = 7; i >= 0; i--) {
|
for (i = 7; i >= 0; i--) {
|
||||||
k = cblock.b[i];
|
k = cblock.b[i];
|
||||||
for (j = 7; j >= 0; j--) {
|
for (j = 7; j >= 0; j--) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user