root/diary/encrypt.c

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

DEFINITIONS

This source file includes following definitions.
  1. dencrypt
  2. ddecrypt

   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 <stdlib.h>
  34 #include <openssl/evp.h>
  35 #include <openssl/rand.h>
  36 
  37 #include <shared/macro.h>
  38 #include <shared/term.h>
  39 #include <shared/hash.h>
  40 
  41 #include "encrypt.h"
  42 
  43 
  44 /* Plaintext, Plaintext Length, Key, Key Length -> Ciphertext, Tag, IV
  45 ** Tag is 16 bytes, IV is 12 bytes, Ciphertext should be same as plain
  46 */
  47 size_t dencrypt(unsigned char *p, size_t pl,
  48                 unsigned char *k, size_t kl,
  49                 unsigned char *c, unsigned char *t, unsigned char *i)
  50     {
  51     hash_t key;
  52     EVP_CIPHER_CTX *ctx;
  53     int len, cl;
  54 
  55     genkey(k, kl, &key);
  56 
  57     /* initialize the IV to 12 bytes of random data */
  58     RAND_bytes(i, AES_IV);
  59 
  60     if (!(ctx = EVP_CIPHER_CTX_new()))
  61         fatal("failed to EVP_CIPHER_CTX_new()\n");
  62     if (1 != EVP_EncryptInit_ex(ctx, EVP_aes_256_gcm(), NULL, key.r, i))
  63         fatal("failed to EVP_EncryptInit_ex()\n");
  64     if (1 != EVP_EncryptUpdate(ctx, c, &len, p, pl))
  65         fatal("failed to EVP_EncryptUpdate()\n");
  66     cl = len;
  67     if (1 != EVP_EncryptFinal_ex(ctx, c + len, &len))
  68         fatal("failed to EVP_EncryptFinal_ex()\n");
  69     cl += len;
  70     if (1 != EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_GET_TAG, AES_TAG, t))
  71         fatal("failed to EVP_CIPHER_CTX_ctrl()\n");
  72     EVP_CIPHER_CTX_free(ctx);
  73 
  74     return cl;
  75     }
  76 
  77 
  78 /* Ciphertext, Ciphertext Length, Tag, IV, Key, Key Length -> Plaintext */
  79 size_t ddecrypt(unsigned char *c, size_t cl,
  80                 unsigned char *t, unsigned char *i,
  81                 unsigned char *k, size_t kl,
  82                 unsigned char *p)
  83     {
  84     hash_t key;
  85     EVP_CIPHER_CTX *ctx;
  86     int len, pl;
  87 
  88     genkey(k, kl, &key);
  89 
  90     if (!(ctx = EVP_CIPHER_CTX_new()))
  91         fatal("failed to EVP_CIPHER_CTX_new()\n");
  92     if (1 != EVP_DecryptInit_ex(ctx, EVP_aes_256_gcm(), NULL, key.r, i))
  93         fatal("failed to EVP_EncryptInit_ex()\n");
  94     if (1 != EVP_DecryptUpdate(ctx, p, &len, c, cl))
  95         fatal("failed to EVP_EncryptUpdate()\n");
  96     pl = len;
  97     if (1 != EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_TAG, AES_TAG, t))
  98         fatal("failed to EVP_CIPHER_CTX_ctrl()\n");
  99     if (1 != EVP_DecryptFinal_ex(ctx, p + len, &len))
 100         fatal("failed to EVP_EncryptFinal_ex() -- bad key or data?\n");
 101     pl += len;
 102     EVP_CIPHER_CTX_free(ctx);
 103 
 104     return pl;
 105     }

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