"Fossies" - the Fresh Open Source Software Archive

Member "libgcgi.a-0.9.5/src/crypt.c" (22 Jun 2002, 3939 Bytes) of package /linux/www/old/gcgi-0.9.5.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "crypt.c" see the Fossies "Dox" file reference documentation.

    1 /* -*-mode:c; c-style:k&r; c-basic-offset:4; -*- */
    2 /*
    3  * cgiPop, a web-based POP email client.
    4  * Copyright (C) 2001-2002 Julian Catchen, julian@catchen.org
    5  *
    6  * This library is free software; you can redistribute it and/or
    7  * modify it under the terms of the GNU General Public
    8  * License as published by the Free Software Foundation; either
    9  * version 2 of the License, or (at your option) any later version.
   10  *
   11  * This library is distributed in the hope that it will be useful,
   12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
   13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   14  * Lesser General Public License for more details.
   15  *
   16  * You should have received a copy of the GNU General Public
   17  * License along with this library; if not, write to the Free Software
   18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   19  */
   20 
   21 #include "common.h"
   22 
   23 #ifdef USE_SSL
   24 
   25 #include <openssl/evp.h>
   26 #include "crypt.h"
   27 
   28 
   29 unsigned char *
   30 generateKey()
   31 {
   32     FILE *rng;
   33     int   num;
   34     static unsigned char *key;
   35   
   36     if ((rng = fopen("/dev/random", "r")) == NULL)
   37     return NULL;
   38 
   39     key = XMALLOC(unsigned char, EVP_MAX_KEY_LENGTH);
   40     num = 0;
   41     while (num < EVP_MAX_KEY_LENGTH) {
   42     num += fread(&key[num], 1, EVP_MAX_KEY_LENGTH - num, rng);
   43     }
   44     
   45     fclose(rng);
   46 
   47     return key;
   48 }
   49 
   50 
   51 void 
   52 encryptString(char *text, int size, unsigned char *key, char **ciphertext, int *ctlen)
   53 {
   54     EVP_CIPHER_CTX  ctx;
   55     EVP_CIPHER     *cipher;
   56     int             len;
   57     char            ivec[EVP_MAX_IV_LENGTH] = {0};
   58 
   59     cipher = EVP_bf_cbc();
   60     EVP_EncryptInit(&ctx, cipher, key, ivec);
   61 
   62     /* Allocate ciphertext and include enough length for padding */
   63     *ciphertext = XMALLOC(unsigned char, size + EVP_MAX_KEY_LENGTH + 1);
   64 
   65     EVP_EncryptUpdate(&ctx, *ciphertext, &len, text, size);
   66     *ctlen  = len;
   67     EVP_EncryptFinal(&ctx, (*ciphertext) + len, &len);
   68     *ctlen += len;
   69 }
   70 
   71 
   72 void 
   73 decryptString(char *ciphertext, int ctlen, unsigned char *key, char **text, int *size)
   74 {
   75     EVP_CIPHER_CTX  ctx;
   76     EVP_CIPHER     *cipher;
   77     int             len;
   78     char            ivec[EVP_MAX_IV_LENGTH] = {0};
   79 
   80     cipher = EVP_bf_cbc();
   81     EVP_DecryptInit(&ctx, cipher, key, ivec);
   82 
   83     /* Allocate ciphertext and include enough length for padding */
   84     *text = XMALLOC(unsigned char, ctlen);
   85     
   86     EVP_DecryptUpdate(&ctx, *text, &len, ciphertext, ctlen);
   87     *size  = len;
   88     EVP_DecryptFinal(&ctx, (*text) + len, &len);
   89     *size += len;
   90 }
   91 
   92 
   93 void 
   94 generateStringHMAC(char *text, int size, unsigned char **hmac, int *hlen)
   95 {
   96     EVP_MD_CTX ctx;
   97 
   98     *hmac = XMALLOC(unsigned char, EVP_MAX_MD_SIZE);
   99 
  100     EVP_DigestInit(&ctx, EVP_sha1());
  101     EVP_DigestUpdate(&ctx, text, size);
  102     EVP_DigestFinal(&ctx, *hmac, hlen);
  103 }
  104 
  105 
  106 int
  107 writeKeyToFile(unsigned char *key, char *path)
  108 {
  109     FILE   *out;
  110     mode_t  old_mode;
  111 
  112     old_mode = umask(077);
  113 
  114     if ((out = fopen(path, "w")) == NULL)
  115     return -1;
  116 
  117     umask(old_mode);
  118 
  119     if (fwrite(key, sizeof(unsigned char), EVP_MAX_KEY_LENGTH, out) < EVP_MAX_KEY_LENGTH)
  120     return -1;
  121 
  122     fclose(out);
  123 
  124     return 0;
  125 }
  126 
  127 int
  128 readKeyFromFile(char *path, unsigned char **key)
  129 {
  130     FILE   *in;
  131     size_t  read;
  132     off_t   offset;
  133 
  134     if ((in = fopen(path, "r")) == NULL)
  135     return -1;
  136     
  137     read   = 0;
  138     offset = 0;
  139     *key   = XMALLOC(unsigned char, EVP_MAX_KEY_LENGTH);
  140     memset(*key, 0, EVP_MAX_KEY_LENGTH);
  141 
  142     /* Read in the query from STDIN */
  143     do {
  144         read = fread(*key + offset, sizeof(unsigned char), EVP_MAX_KEY_LENGTH - offset, in);
  145         if (ferror(in)) 
  146             return -1;
  147         else if (read <= 0)
  148             continue;
  149         offset += read;
  150     } while (offset < EVP_MAX_KEY_LENGTH && read > 0);
  151 
  152     fclose(in);
  153 
  154     return 0;
  155 }
  156 
  157 
  158 void 
  159 printHexString(FILE *output, unsigned char *s, int len)
  160 {
  161     int i;
  162 
  163     for (i = 0; i < len; i++)
  164     fprintf(output, "%02x", s[i]);
  165 }
  166 
  167 #endif /* USE_SSL */