root/shared/hash.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. sha256
  2. genkey
  3. genchk
  4. hashcmp
  5. hashfd

   1 /*
   2 ** Copyright (c) 2024 Amelia Campbell
   3 **
   4 ** Redistribution and use in source and binary forms, with or without
   5 ** modification, are permitted provided that the following conditions are
   6 ** met: 
   7 **
   8 **  1. Redistributions of source code must retain the above copyright
   9 **     notice, this list of conditions and the following disclaimer.
  10 **  2. Redistributions in binary form must reproduce the above copyright
  11 **     notice, this list of conditions and the following disclaimer in the
  12 **     documentation and/or other materials provided with the
  13 **     distribution.
  14 **  3. Neither the name of the copyright holder nor the names of its
  15 **     contributors may be used to endorse or promote products derived
  16 **     from this software without specific prior written permission.
  17 **
  18 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  19 ** IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  20 ** TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
  21 ** PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22 ** HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29 */
  30 
  31 #include <stdio.h>
  32 #include <string.h>
  33 #include <openssl/evp.h>
  34 #include <openssl/rand.h>
  35 
  36 #include <shared/macro.h>
  37 #include <shared/term.h>
  38 
  39 #include "hash.h"
  40 
  41 #define SHA_ROUNDS 24680
  42 
  43 #ifdef USE_AGENT
  44 unsigned char **key_override = NULL;
  45 #endif
  46 
  47 
  48 void sha256(unsigned char *d, size_t dl, hash_t *md)
  49     {
  50     EVP_MD_CTX *ctx;
  51 
  52     if (!(ctx = EVP_MD_CTX_create()))
  53         fatal("failed to EVP_MD_CTX_create()\n");
  54     if (1 != EVP_DigestInit_ex(ctx, EVP_sha256(), NULL))
  55         fatal("failed to EVP_DigestInit_ex()\n");
  56     if (1 != EVP_DigestUpdate(ctx, d, dl))
  57         fatal("failed to EVP_DigestUpdate()\n");
  58     if (1 != EVP_DigestFinal(ctx, md->r, NULL))
  59         fatal("failed to EVP_DigestFinal()\n");
  60     EVP_MD_CTX_destroy(ctx);
  61     }
  62 
  63 
  64 void genkey(unsigned char *k, size_t kl, hash_t *md)
  65     {
  66     int i;
  67     hash_t acc;
  68 
  69 #ifdef USE_AGENT
  70     if (key_override)
  71         {
  72         /* allow agent to override key at hash level */
  73         memcpy(md->r, *key_override, HASH_TSZ);
  74         return;
  75         }
  76 #endif
  77 
  78     /* generate 256-bit AES key from sha256 of k with SHA_ROUNDS repeat */
  79     sha256(k, kl, &acc);
  80 
  81     for (i = 0; i < SHA_ROUNDS; i++)
  82         sha256(acc.r, HASH_ASZ, i == (SHA_ROUNDS - 1) ? md : &acc);
  83     }
  84 
  85 
  86 void genchk(unsigned char *k, size_t kl, salt_t *s, hash_t *md, int new)
  87     {
  88     struct {
  89         hash_t md;
  90         salt_t salt;
  91         } comb;
  92 
  93     memset(&comb, 0, sizeof(comb));
  94     if (new)
  95         {
  96         RAND_bytes(comb.salt.r, SALT_SZ);
  97         memcpy(s->r, comb.salt.r, SALT_SZ);
  98         }
  99     else
 100         memcpy(comb.salt.r, s->r, SALT_SZ);
 101 
 102     genkey(k, kl, &comb.md);
 103     genkey((unsigned char *)&comb, sizeof(comb), md);
 104     }
 105 
 106 
 107 int hashcmp(hash_t *h1, hash_t *h2)
 108     {
 109     return memcmp(h1->r, h2->r, HASH_TSZ) == 0;
 110     }
 111 
 112 
 113 void hashfd(FILE *in, hash_t *md)
 114     {
 115     EVP_MD_CTX *ctx;
 116     unsigned char buf[256];
 117     size_t read;
 118 
 119     if (!(ctx = EVP_MD_CTX_create()))
 120         fatal("failed to EVP_MD_CTX_create()\n");
 121     if (1 != EVP_DigestInit_ex(ctx, EVP_sha256(), NULL))
 122         fatal("failed to EVP_DigestInit_ex()\n");
 123     fseek(in, 0, SEEK_SET);
 124     do
 125         {
 126         read = fread(buf, 1, 256, in);
 127         if (1 != EVP_DigestUpdate(ctx, buf, read))
 128             fatal("failed to EVP_DigestUpdate()\n");
 129         } while (read == 256);
 130     if (1 != EVP_DigestFinal(ctx, md->r, NULL))
 131         fatal("failed to EVP_DigestFinal()\n");
 132     EVP_MD_CTX_destroy(ctx);
 133     fseek(in, 0, SEEK_SET);
 134     }

/* [previous][next][first][last][top][bottom][index][help] */