Ruby  2.4.2p198(2017-09-14revision59899)
Data Structures | Macros | Functions | Variables
crypt.c File Reference
#include "ruby/missing.h"
#include "crypt.h"
#include <limits.h>
#include <stdio.h>
#include <string.h>
#include "des_tables.c"
Include dependency graph for crypt.c:

Go to the source code of this file.

Data Structures

struct  des_tables_t
 

Macros

#define _PASSWORD_EFMT1   '_'
 
#define numberof(array)   (int)(sizeof(array) / sizeof((array)[0]))
 
#define MUST_ALIGN
 
#define INIT_DES   0
 
#define INIT_DES   1
 
#define TO_SIX_BIT(rslt, src)
 
#define ZERO(d, d0, d1)   ((d0) = 0, (d1) = 0)
 
#define LOAD(d, d0, d1, bl)   ((d0) = (bl).b32.i0, (d1) = (bl).b32.i1)
 
#define LOADREG(d, d0, d1, s, s0, s1)   ((d0) = (s0), (d1) = (s1))
 
#define OR(d, d0, d1, bl)   ((d0) |= (bl).b32.i0, (d1) |= (bl).b32.i1)
 
#define STORE(s, s0, s1, bl)   ((bl).b32.i0 = (s0), (bl).b32.i1 = (s1))
 
#define DCL_BLOCK(d, d0, d1)   long d0, d1
 
#define PERM6464(d, d0, d1, cpp, p)   { C_block tblk; permute((cpp),&tblk,(p),8); LOAD ((d),(d0),(d1),tblk); }
 
#define PERM3264(d, d0, d1, cpp, p)   { C_block tblk; permute((cpp),&tblk,(p),4); LOAD ((d),(d0),(d1),tblk); }
 
#define A64TOI1(c)
 
#define A64TOI4(base)   A64TOI1(base+0), A64TOI1(base+1), A64TOI1(base+2), A64TOI1(base+3)
 
#define A64TOI16(base)   A64TOI4(base+0), A64TOI4(base+4), A64TOI4(base+8), A64TOI4(base+12)
 
#define A64TOI64(base)   A64TOI16(base+0x00), A64TOI16(base+0x10), A64TOI16(base+0x20), A64TOI16(base+0x30)
 
#define des_tables   ((const des_tables_t *)des_tables)
 
#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 KS   (data->KS)
 
#define cryptresult   (data->cryptresult)
 
#define SALT   salt
 
#define SPTAB(t, i)   (*(const unsigned long *)((const unsigned char *)(t) + (i)*(sizeof(long)/4)))
 
#define DOXOR(x, y, i)   k=B.b[(i)]; (x)^=SPTAB(SPE[0][(i)],k); (y)^=SPTAB(SPE[1][(i)],k);
 
#define CRUNCH(p0, p1, q0, q1)
 

Functions

STATIC void permute (const unsigned char *cp, C_block *out, register const C_block *p, int chars_in)
 
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)
 
static void des_cipher_r (const unsigned char *in, unsigned char *out, long salt, int num_iter, struct crypt_data *data)
 
char * crypt_r (const char *key, const char *setting, 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)
 

Variables

static const unsigned char IP []
 
static const unsigned char ExpandTr []
 
static const unsigned char PC1 []
 
static const unsigned char Rotates []
 
static const unsigned char PC2 []
 
static const unsigned char S [8][64]
 
static const unsigned char P32Tr []
 
static const unsigned char CIFP []
 
static const unsigned char itoa64 []
 
static const unsigned char a64toi [256]
 
static des_tables_t des_tables [1]
 
static const C_block constdatablock = {{0}}
 

Macro Definition Documentation

◆ _PASSWORD_EFMT1

#define _PASSWORD_EFMT1   '_'

Definition at line 49 of file crypt.c.

Referenced by crypt_r().

◆ A64TOI1

#define A64TOI1 (   c)
Value:
((c) == '.' ? 0 : \
(c) == '/' ? 1 : \
('0' <= (c) && (c) <= '9') ? (c) - '0' + 2 : \
('A' <= (c) && (c) <= 'Z') ? (c) - 'A' + 12 : \
('a' <= (c) && (c) <= 'z') ? (c) - 'a' + 38 : \
0)

◆ A64TOI16

#define A64TOI16 (   base)    A64TOI4(base+0), A64TOI4(base+4), A64TOI4(base+8), A64TOI4(base+12)

◆ A64TOI4

#define A64TOI4 (   base)    A64TOI1(base+0), A64TOI1(base+1), A64TOI1(base+2), A64TOI1(base+3)

◆ A64TOI64

#define A64TOI64 (   base)    A64TOI16(base+0x00), A64TOI16(base+0x10), A64TOI16(base+0x20), A64TOI16(base+0x30)

◆ CF6464

#define CF6464   (des_tables->CF6464)

Definition at line 361 of file crypt.c.

Referenced by des_cipher_r(), encrypt_r(), and init_des().

◆ CRUNCH

#define CRUNCH (   p0,
  p1,
  q0,
  q1 
)
Value:
k = ((q0) ^ (q1)) & SALT; \
B.b32.i0 = k ^ (q0) ^ kp->b32.i0; \
B.b32.i1 = k ^ (q1) ^ kp->b32.i1; \
kp += ks_inc; \
DOXOR((p0), (p1), 0); \
DOXOR((p0), (p1), 1); \
DOXOR((p0), (p1), 2); \
DOXOR((p0), (p1), 3); \
DOXOR((p0), (p1), 4); \
DOXOR((p0), (p1), 5); \
DOXOR((p0), (p1), 6); \
DOXOR((p0), (p1), 7);
#define SALT
#define DOXOR(x, y, i)

Referenced by des_cipher_r().

◆ cryptresult

#define cryptresult   (data->cryptresult)

Definition at line 370 of file crypt.c.

Referenced by crypt_r().

◆ DCL_BLOCK

#define DCL_BLOCK (   d,
  d0,
  d1 
)    long d0, d1

Definition at line 127 of file crypt.c.

Referenced by des_setkey_r(), and permute().

◆ des_tables

#define des_tables   ((const des_tables_t *)des_tables)

Definition at line 356 of file crypt.c.

◆ DOXOR

#define DOXOR (   x,
  y,
 
)    k=B.b[(i)]; (x)^=SPTAB(SPE[0][(i)],k); (y)^=SPTAB(SPE[1][(i)],k);

◆ IE3264

#define IE3264   (des_tables->IE3264)

Definition at line 359 of file crypt.c.

Referenced by des_cipher_r(), encrypt_r(), and init_des().

◆ INIT_DES [1/2]

#define INIT_DES   0

Definition at line 102 of file crypt.c.

◆ INIT_DES [2/2]

#define INIT_DES   1

Definition at line 102 of file crypt.c.

◆ KS

#define KS   (data->KS)

Definition at line 369 of file crypt.c.

Referenced by des_cipher_r(), and des_setkey_r().

◆ LOAD

#define LOAD (   d,
  d0,
  d1,
  bl 
)    ((d0) = (bl).b32.i0, (d1) = (bl).b32.i1)

Definition at line 123 of file crypt.c.

Referenced by des_cipher_r().

◆ LOADREG

#define LOADREG (   d,
  d0,
  d1,
  s,
  s0,
  s1 
)    ((d0) = (s0), (d1) = (s1))

Definition at line 124 of file crypt.c.

Referenced by des_cipher_r().

◆ MUST_ALIGN

#define MUST_ALIGN

Definition at line 80 of file crypt.c.

◆ numberof

#define numberof (   array)    (int)(sizeof(array) / sizeof((array)[0]))

Definition at line 53 of file crypt.c.

Referenced by des_setkey_r(), and encrypt_r().

◆ OR

#define OR (   d,
  d0,
  d1,
  bl 
)    ((d0) |= (bl).b32.i0, (d1) |= (bl).b32.i1)

Definition at line 125 of file crypt.c.

Referenced by permute().

◆ PC1ROT

#define PC1ROT   (des_tables->PC1ROT)

Definition at line 357 of file crypt.c.

Referenced by des_setkey_r(), encrypt_r(), and init_des().

◆ PC2ROT

#define PC2ROT   (des_tables->PC2ROT)

Definition at line 358 of file crypt.c.

Referenced by des_setkey_r(), encrypt_r(), and init_des().

◆ PERM3264

#define PERM3264 (   d,
  d0,
  d1,
  cpp,
 
)    { C_block tblk; permute((cpp),&tblk,(p),4); LOAD ((d),(d0),(d1),tblk); }

Definition at line 149 of file crypt.c.

Referenced by des_cipher_r().

◆ PERM6464

#define PERM6464 (   d,
  d0,
  d1,
  cpp,
 
)    { C_block tblk; permute((cpp),&tblk,(p),8); LOAD ((d),(d0),(d1),tblk); }

Definition at line 147 of file crypt.c.

Referenced by des_cipher_r(), and des_setkey_r().

◆ SALT

#define SALT   salt

◆ SPE

#define SPE   (des_tables->SPE)

Definition at line 360 of file crypt.c.

Referenced by encrypt_r(), and init_des().

◆ SPTAB

#define SPTAB (   t,
 
)    (*(const unsigned long *)((const unsigned char *)(t) + (i)*(sizeof(long)/4)))

◆ STORE

#define STORE (   s,
  s0,
  s1,
  bl 
)    ((bl).b32.i0 = (s0), (bl).b32.i1 = (s1))

Definition at line 126 of file crypt.c.

Referenced by des_cipher_r(), des_setkey_r(), and permute().

◆ TO_SIX_BIT

#define TO_SIX_BIT (   rslt,
  src 
)
Value:
{ \
C_block cvt; \
cvt.b[0] = (unsigned char)(src); (src) >>= 6; \
cvt.b[1] = (unsigned char)(src); (src) >>= 6; \
cvt.b[2] = (unsigned char)(src); (src) >>= 6; \
cvt.b[3] = (unsigned char)(src); \
(rslt) = (cvt.b32.i0 & 0x3f3f3f3fL) << 2; \
}

Definition at line 110 of file crypt.c.

Referenced by des_cipher_r(), and init_des().

◆ ZERO

#define ZERO (   d,
  d0,
  d1 
)    ((d0) = 0, (d1) = 0)

Definition at line 122 of file crypt.c.

Referenced by permute().

Function Documentation

◆ crypt_r()

char* crypt_r ( const char *  key,
const char *  setting,
struct crypt_data data 
)

Definition at line 396 of file crypt.c.

References _PASSWORD_EFMT1, a64toi, C_block::b, cryptresult, des_cipher_r(), des_setkey_r(), itoa64, and L.

Referenced by rb_str_crypt().

◆ des_cipher_r()

void des_cipher_r ( const unsigned char *  in,
unsigned char *  out,
long  salt,
int  num_iter,
struct crypt_data data 
)
static

Definition at line 514 of file crypt.c.

References C_block::b, B, CF6464, CRUNCH, IE3264, KS, KS_SIZE, L, LOAD, LOADREG, PERM3264, PERM6464, R, R0, R1, STATIC, STORE, and TO_SIX_BIT.

Referenced by crypt_r(), and encrypt_r().

◆ des_setkey_r()

static void des_setkey_r ( const unsigned char *  key,
struct crypt_data data 
)
static

Definition at line 484 of file crypt.c.

References C_block::b, DCL_BLOCK, init_des(), KS, numberof, PC1ROT, PC2ROT, PERM6464, Rotates, and STORE.

Referenced by crypt_r(), and setkey_r().

◆ encrypt_r()

void encrypt_r ( char *  block,
int  flag,
struct crypt_data data 
)

Definition at line 835 of file crypt.c.

References C_block::b, CF6464, des_cipher_r(), IE3264, init_des(), L, main(), numberof, PC1ROT, PC2ROT, SPE, and STATIC.

Referenced by setkey_r().

◆ init_des()

STATIC void init_des ( void  )

Definition at line 634 of file crypt.c.

References CF6464, CIFP, ExpandTr, IE3264, init_perm(), IP, P32Tr, PC1, PC1ROT, PC2, PC2ROT, des_tables_t::ready, Rotates, S, SPE, STATIC, and TO_SIX_BIT.

Referenced by des_setkey_r(), and encrypt_r().

◆ init_perm()

STATIC void init_perm ( C_block  perm[64/CHUNKBITS][1<< CHUNKBITS],
unsigned char  p[64],
int  chars_in,
int  chars_out 
)

Definition at line 776 of file crypt.c.

References CHUNKBITS, key, LGCHUNKBITS, setkey(), and setkey_r().

Referenced by init_des().

◆ permute()

STATIC void permute ( const unsigned char *  cp,
C_block out,
register const C_block p,
int  chars_in 
)

Definition at line 153 of file crypt.c.

References CHUNKBITS, D, DCL_BLOCK, OR, STATIC, STORE, and ZERO.

◆ setkey_r()

void setkey_r ( const char *  key,
struct crypt_data data 
)

Definition at line 807 of file crypt.c.

References C_block::b, des_setkey_r(), encrypt(), and encrypt_r().

Referenced by init_perm().

Variable Documentation

◆ a64toi

const unsigned char a64toi[256]
static
Initial value:
= {
#define A64TOI1(c)
#define A64TOI4(base)
#define A64TOI16(base)
#define A64TOI64(base)
A64TOI64(0x00), A64TOI64(0x40),
A64TOI64(0x00), A64TOI64(0x40),
}
#define A64TOI64(base)

Definition at line 318 of file crypt.c.

Referenced by crypt_r().

◆ CIFP

const unsigned char CIFP[]
static
Initial value:
= {
1, 2, 3, 4, 17, 18, 19, 20,
5, 6, 7, 8, 21, 22, 23, 24,
9, 10, 11, 12, 25, 26, 27, 28,
13, 14, 15, 16, 29, 30, 31, 32,
33, 34, 35, 36, 49, 50, 51, 52,
37, 38, 39, 40, 53, 54, 55, 56,
41, 42, 43, 44, 57, 58, 59, 60,
45, 46, 47, 48, 61, 62, 63, 64,
}

Definition at line 301 of file crypt.c.

Referenced by init_des().

◆ constdatablock

const C_block constdatablock = {{0}}
static

Definition at line 367 of file crypt.c.

◆ des_tables

des_tables_t des_tables[1]
static

Definition at line 354 of file crypt.c.

◆ ExpandTr

const unsigned char ExpandTr[]
static
Initial value:
= {
32, 1, 2, 3, 4, 5,
4, 5, 6, 7, 8, 9,
8, 9, 10, 11, 12, 13,
12, 13, 14, 15, 16, 17,
16, 17, 18, 19, 20, 21,
20, 21, 22, 23, 24, 25,
24, 25, 26, 27, 28, 29,
28, 29, 30, 31, 32, 1,
}

Definition at line 189 of file crypt.c.

Referenced by init_des().

◆ IP

const unsigned char IP[]
static
Initial value:
= {
58, 50, 42, 34, 26, 18, 10, 2,
60, 52, 44, 36, 28, 20, 12, 4,
62, 54, 46, 38, 30, 22, 14, 6,
64, 56, 48, 40, 32, 24, 16, 8,
57, 49, 41, 33, 25, 17, 9, 1,
59, 51, 43, 35, 27, 19, 11, 3,
61, 53, 45, 37, 29, 21, 13, 5,
63, 55, 47, 39, 31, 23, 15, 7,
}

Definition at line 176 of file crypt.c.

Referenced by init_des().

◆ itoa64

const unsigned char itoa64[]
static
Initial value:
=
"./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"

Definition at line 314 of file crypt.c.

Referenced by crypt_r().

◆ P32Tr

const unsigned char P32Tr[]
static
Initial value:
= {
16, 7, 20, 21,
29, 12, 28, 17,
1, 15, 23, 26,
5, 18, 31, 10,
2, 8, 24, 14,
32, 27, 3, 9,
19, 13, 30, 6,
22, 11, 4, 25,
}

Definition at line 290 of file crypt.c.

Referenced by init_des().

◆ PC1

const unsigned char PC1[]
static
Initial value:
= {
57, 49, 41, 33, 25, 17, 9,
1, 58, 50, 42, 34, 26, 18,
10, 2, 59, 51, 43, 35, 27,
19, 11, 3, 60, 52, 44, 36,
63, 55, 47, 39, 31, 23, 15,
7, 62, 54, 46, 38, 30, 22,
14, 6, 61, 53, 45, 37, 29,
21, 13, 5, 28, 20, 12, 4,
}

Definition at line 200 of file crypt.c.

Referenced by init_des().

◆ PC2

const unsigned char PC2[]
static
Initial value:
= {
9, 18, 14, 17, 11, 24, 1, 5,
22, 25, 3, 28, 15, 6, 21, 10,
35, 38, 23, 19, 12, 4, 26, 8,
43, 54, 16, 7, 27, 20, 13, 2,
0, 0, 41, 52, 31, 37, 47, 55,
0, 0, 30, 40, 51, 45, 33, 48,
0, 0, 44, 49, 39, 56, 34, 53,
0, 0, 46, 42, 50, 36, 29, 32,
}

Definition at line 219 of file crypt.c.

Referenced by init_des().

◆ Rotates

const unsigned char Rotates[]
static
Initial value:
= {
1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1,
}

Definition at line 213 of file crypt.c.

Referenced by des_setkey_r(), and init_des().

◆ S

const unsigned char S[8][64]
static

Definition at line 231 of file crypt.c.

Referenced by init_des().