aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2016-06-01 05:00:55 +0000
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2016-06-01 05:00:55 +0000
commit7657852ad35c76d82e66116e413e11753fe918c3 (patch)
tree3dcad5e8e16e6a49ef78e8d2fece9ed5ebebf87b
parent040acbb687ce3d47b99437afc411004eb5c84557 (diff)
downloadruby-7657852ad35c76d82e66116e413e11753fe918c3.tar.gz
crypt_r.c: DES tables
* missing/crypt_r.c (des_tables): move sharable DES constant tables. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@55242 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r--crypt.h21
-rw-r--r--missing/crypt_r.c55
2 files changed, 45 insertions, 31 deletions
diff --git a/crypt.h b/crypt.h
index ca7669498f..2048f76b1a 100644
--- a/crypt.h
+++ b/crypt.h
@@ -216,34 +216,13 @@ typedef union {
#define CHUNKBITS (1<<LGCHUNKBITS)
#endif
-/* ===== Tables that are initialized at run time ==================== */
-
struct crypt_data {
-
- unsigned char a64toi[128]; /* ascii-64 => 0..63 */
-
- /* Initial key schedule permutation */
- C_block PC1ROT[64/CHUNKBITS][1<<CHUNKBITS];
-
- /* Subsequent key schedule rotation permutations */
- C_block PC2ROT[2][64/CHUNKBITS][1<<CHUNKBITS];
-
- /* Initial permutation/expansion table */
- C_block IE3264[32/CHUNKBITS][1<<CHUNKBITS];
-
- /* Table that combines the S, P, and E operations. */
- long SPE[2][8][64];
-
- /* compressed/interleaved => final permutation table */
- 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;
};
diff --git a/missing/crypt_r.c b/missing/crypt_r.c
index 1b6d62ae6f..a622725b3f 100644
--- a/missing/crypt_r.c
+++ b/missing/crypt_r.c
@@ -289,19 +289,46 @@ static const unsigned char CIFP[] = { /* compressed/interleaved permutation */
static const unsigned char itoa64[] = /* 0..63 => ascii-64 */
"./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
+/* ===== Tables that are initialized at run time ==================== */
+
+typedef struct {
+ /* table that converts chars "./0-9A-Za-z"to integers 0-63. */
+ unsigned char a64toi[128];
+
+ /* Initial key schedule permutation */
+ C_block PC1ROT[64/CHUNKBITS][1<<CHUNKBITS];
+
+ /* Subsequent key schedule rotation permutations */
+ C_block PC2ROT[2][64/CHUNKBITS][1<<CHUNKBITS];
+
+ /* Initial permutation/expansion table */
+ C_block IE3264[32/CHUNKBITS][1<<CHUNKBITS];
+
+ /* Table that combines the S, P, and E operations. */
+ long SPE[2][8][64];
+
+ /* compressed/interleaved => final permutation table */
+ C_block CF6464[64/CHUNKBITS][1<<CHUNKBITS];
+
+ int ready;
+} des_tables_t;
+static des_tables_t des_tables[1];
+
+static const C_block constdatablock; /* encryption constant */
+
+#define des_tables ((const des_tables_t *)des_tables)
+#define a64toi (des_tables->a64toi)
+#define PC1ROT (des_tables->PC1ROT)
+#define PC2ROT (des_tables->PC2ROT)
+#define IE3264 (des_tables->IE3264)
+#define SPE (des_tables->SPE)
+#define CF6464 (des_tables->CF6464)
-#define a64toi (data->a64toi)
-#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)
-STATIC void init_des(struct crypt_data *);
+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);
static void des_setkey_r(const unsigned char *key, struct crypt_data *data);
@@ -421,7 +448,7 @@ des_setkey_r(const unsigned char *key, struct crypt_data *data)
if (!des_ready) {
memset(data, 0, sizeof(*data));
- init_des(data);
+ init_des();
des_ready = 1;
}
@@ -556,19 +583,25 @@ des_cipher_r(const unsigned char *in, unsigned char *out, long salt, int num_ite
#endif
}
+#undef des_tables
+#undef KS
+#undef cryptresult
+#undef des_ready
/*
* Initialize various tables. This need only be done once. It could even be
* done at compile time, if the compiler were capable of that sort of thing.
*/
STATIC void
-init_des(struct crypt_data *data)
+init_des(void)
{
register int i, j;
register long k;
register int tableno;
unsigned char perm[64], tmp32[32];
+ if (des_tables->ready) return;
+
/*
* table that converts chars "./0-9A-Za-z"to integers 0-63.
*/
@@ -695,6 +728,8 @@ init_des(struct crypt_data *data)
TO_SIX_BIT(SPE[1][tableno][j], k);
}
}
+
+ des_tables->ready = 1;
}
/*