Back to index

php5  5.3.10
Classes | Defines | Functions
crypt_freesec.h File Reference
#include "php_config.h"
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  php_crypt_extended_data

Defines

#define MD5_HASH_MAX_LEN   120

Functions

void _crypt_extended_init (void)
char * _crypt_extended_r (const char *key, const char *setting, struct php_crypt_extended_data *data)

Class Documentation

struct php_crypt_extended_data

Definition at line 29 of file crypt_freesec.h.

Class Members
uint32_t de_keysl
uint32_t de_keysr
uint32_t en_keysl
uint32_t en_keysr
int initialized
uint32_t old_rawkey0
uint32_t old_rawkey1
uint32_t old_salt
char output
uint32_t saltbits

Define Documentation

#define MD5_HASH_MAX_LEN   120

Definition at line 27 of file crypt_freesec.h.


Function Documentation

void _crypt_extended_init ( void  )

Definition at line 215 of file crypt_freesec.c.

{
       int i, j, b, k, inbit, obit;
       uint32_t *p, *il, *ir, *fl, *fr;
       uint32_t *bits28, *bits24;
       u_char inv_key_perm[64];
       u_char u_key_perm[56];
       u_char inv_comp_perm[56];
       u_char init_perm[64], final_perm[64];
       u_char u_sbox[8][64];
       u_char un_pbox[32];

       bits24 = (bits28 = bits32 + 4) + 4;

       /*
        * Invert the S-boxes, reordering the input bits.
        */
       for (i = 0; i < 8; i++)
              for (j = 0; j < 64; j++) {
                     b = (j & 0x20) | ((j & 1) << 4) | ((j >> 1) & 0xf);
                     u_sbox[i][j] = sbox[i][b];
              }

       /*
        * Convert the inverted S-boxes into 4 arrays of 8 bits.
        * Each will handle 12 bits of the S-box input.
        */
       for (b = 0; b < 4; b++)
              for (i = 0; i < 64; i++)
                     for (j = 0; j < 64; j++)
                            m_sbox[b][(i << 6) | j] =
                                   (u_sbox[(b << 1)][i] << 4) |
                                   u_sbox[(b << 1) + 1][j];

       /*
        * Set up the initial & final permutations into a useful form, and
        * initialise the inverted key permutation.
        */
       for (i = 0; i < 64; i++) {
              init_perm[final_perm[i] = IP[i] - 1] = i;
              inv_key_perm[i] = 255;
       }

       /*
        * Invert the key permutation and initialise the inverted key
        * compression permutation.
        */
       for (i = 0; i < 56; i++) {
              u_key_perm[i] = key_perm[i] - 1;
              inv_key_perm[key_perm[i] - 1] = i;
              inv_comp_perm[i] = 255;
       }

       /*
        * Invert the key compression permutation.
        */
       for (i = 0; i < 48; i++) {
              inv_comp_perm[comp_perm[i] - 1] = i;
       }

       /*
        * Set up the OR-mask arrays for the initial and final permutations,
        * and for the key initial and compression permutations.
        */
       for (k = 0; k < 8; k++) {
              for (i = 0; i < 256; i++) {
                     *(il = &ip_maskl[k][i]) = 0;
                     *(ir = &ip_maskr[k][i]) = 0;
                     *(fl = &fp_maskl[k][i]) = 0;
                     *(fr = &fp_maskr[k][i]) = 0;
                     for (j = 0; j < 8; j++) {
                            inbit = 8 * k + j;
                            if (i & bits8[j]) {
                                   if ((obit = init_perm[inbit]) < 32)
                                          *il |= bits32[obit];
                                   else
                                          *ir |= bits32[obit-32];
                                   if ((obit = final_perm[inbit]) < 32)
                                          *fl |= bits32[obit];
                                   else
                                          *fr |= bits32[obit - 32];
                            }
                     }
              }
              for (i = 0; i < 128; i++) {
                     *(il = &key_perm_maskl[k][i]) = 0;
                     *(ir = &key_perm_maskr[k][i]) = 0;
                     for (j = 0; j < 7; j++) {
                            inbit = 8 * k + j;
                            if (i & bits8[j + 1]) {
                                   if ((obit = inv_key_perm[inbit]) == 255)
                                          continue;
                                   if (obit < 28)
                                          *il |= bits28[obit];
                                   else
                                          *ir |= bits28[obit - 28];
                            }
                     }
                     *(il = &comp_maskl[k][i]) = 0;
                     *(ir = &comp_maskr[k][i]) = 0;
                     for (j = 0; j < 7; j++) {
                            inbit = 7 * k + j;
                            if (i & bits8[j + 1]) {
                                   if ((obit=inv_comp_perm[inbit]) == 255)
                                          continue;
                                   if (obit < 24)
                                          *il |= bits24[obit];
                                   else
                                          *ir |= bits24[obit - 24];
                            }
                     }
              }
       }

       /*
        * Invert the P-box permutation, and convert into OR-masks for
        * handling the output of the S-box arrays setup above.
        */
       for (i = 0; i < 32; i++)
              un_pbox[pbox[i] - 1] = i;

       for (b = 0; b < 4; b++)
              for (i = 0; i < 256; i++) {
                     *(p = &psbox[b][i]) = 0;
                     for (j = 0; j < 8; j++) {
                            if (i & bits8[j])
                                   *p |= bits32[un_pbox[8 * b + j]];
                     }
              }
}

Here is the caller graph for this function:

char* _crypt_extended_r ( const char *  key,
const char *  setting,
struct php_crypt_extended_data data 
)

Definition at line 616 of file crypt_freesec.c.

{
       int           i;
       uint32_t      count, salt, l, r0, r1, keybuf[2];
       u_char        *p, *q;

       if (!data->initialized)
              des_init_local(data);

       /*
        * Copy the key, shifting each character up by one bit
        * and padding with zeros.
        */
       q = (u_char *) keybuf;
       while (q - (u_char *) keybuf < sizeof(keybuf)) {
              if ((*q++ = *key << 1))
                     key++;
       }
       if (des_setkey((u_char *) keybuf, data))
              return(NULL);

       if (*setting == _PASSWORD_EFMT1) {
              /*
               * "new"-style:
               *     setting - underscore, 4 chars of count, 4 chars of salt
               *     key - unlimited characters
               */
              for (i = 1, count = 0; i < 5; i++) {
                     int value = ascii_to_bin(setting[i]);
                     if (ascii64[value] != setting[i])
                            return(NULL);
                     count |= value << (i - 1) * 6;
              }
              if (!count)
                     return(NULL);

              for (i = 5, salt = 0; i < 9; i++) {
                     int value = ascii_to_bin(setting[i]);
                     if (ascii64[value] != setting[i])
                            return(NULL);
                     salt |= value << (i - 5) * 6;
              }

              while (*key) {
                     /*
                      * Encrypt the key with itself.
                      */
                     if (des_cipher((u_char *) keybuf, (u_char *) keybuf,
                         0, 1, data))
                            return(NULL);
                     /*
                      * And XOR with the next 8 characters of the key.
                      */
                     q = (u_char *) keybuf;
                     while (q - (u_char *) keybuf < sizeof(keybuf) && *key)
                            *q++ ^= *key++ << 1;

                     if (des_setkey((u_char *) keybuf, data))
                            return(NULL);
              }
              memcpy(data->output, setting, 9);
              data->output[9] = '\0';
              p = (u_char *) data->output + 9;
       } else {
              /*
               * "old"-style:
               *     setting - 2 chars of salt
               *     key - up to 8 characters
               */
              count = 25;

              if (ascii_is_unsafe(setting[0]) || ascii_is_unsafe(setting[1]))
                     return(NULL);

              salt = (ascii_to_bin(setting[1]) << 6)
                   |  ascii_to_bin(setting[0]);

              data->output[0] = setting[0];
              data->output[1] = setting[1];
              p = (u_char *) data->output + 2;
       }
       setup_salt(salt, data);
       /*
        * Do it.
        */
       if (do_des(0, 0, &r0, &r1, count, data))
              return(NULL);
       /*
        * Now encode the result...
        */
       l = (r0 >> 8);
       *p++ = ascii64[(l >> 18) & 0x3f];
       *p++ = ascii64[(l >> 12) & 0x3f];
       *p++ = ascii64[(l >> 6) & 0x3f];
       *p++ = ascii64[l & 0x3f];

       l = (r0 << 16) | ((r1 >> 16) & 0xffff);
       *p++ = ascii64[(l >> 18) & 0x3f];
       *p++ = ascii64[(l >> 12) & 0x3f];
       *p++ = ascii64[(l >> 6) & 0x3f];
       *p++ = ascii64[l & 0x3f];

       l = r1 << 2;
       *p++ = ascii64[(l >> 12) & 0x3f];
       *p++ = ascii64[(l >> 6) & 0x3f];
       *p++ = ascii64[l & 0x3f];
       *p = 0;

       return(data->output);
}

Here is the call graph for this function: