LCOV - code coverage report
Current view: top level - contrib/pgcrypto - pgp-cfb.c (source / functions) Coverage Total Hit
Test: Code coverage Lines: 0.0 % 129 0
Test Date: 2026-01-26 10:56:24 Functions: 0.0 % 9 0
Legend: Lines:     hit not hit

            Line data    Source code
       1              : /*
       2              :  * pgp-cfb.c
       3              :  *        Implements both normal and PGP-specific CFB mode.
       4              :  *
       5              :  * Copyright (c) 2005 Marko Kreen
       6              :  * All rights reserved.
       7              :  *
       8              :  * Redistribution and use in source and binary forms, with or without
       9              :  * modification, are permitted provided that the following conditions
      10              :  * are met:
      11              :  * 1. Redistributions of source code must retain the above copyright
      12              :  *        notice, this list of conditions and the following disclaimer.
      13              :  * 2. Redistributions in binary form must reproduce the above copyright
      14              :  *        notice, this list of conditions and the following disclaimer in the
      15              :  *        documentation and/or other materials provided with the distribution.
      16              :  *
      17              :  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
      18              :  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
      19              :  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
      20              :  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
      21              :  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
      22              :  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
      23              :  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
      24              :  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
      25              :  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
      26              :  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
      27              :  * SUCH DAMAGE.
      28              :  *
      29              :  * contrib/pgcrypto/pgp-cfb.c
      30              :  */
      31              : 
      32              : #include "postgres.h"
      33              : 
      34              : #include "pgp.h"
      35              : #include "px.h"
      36              : 
      37              : typedef int (*mix_data_t) (PGP_CFB *ctx, const uint8 *data, int len, uint8 *dst);
      38              : 
      39              : struct PGP_CFB
      40              : {
      41              :         PX_Cipher  *ciph;
      42              :         int                     block_size;
      43              :         int                     pos;
      44              :         int                     block_no;
      45              :         int                     resync;
      46              :         uint8           fr[PGP_MAX_BLOCK];
      47              :         uint8           fre[PGP_MAX_BLOCK];
      48              :         uint8           encbuf[PGP_MAX_BLOCK];
      49              : };
      50              : 
      51              : int
      52            0 : pgp_cfb_create(PGP_CFB **ctx_p, int algo, const uint8 *key, int key_len,
      53              :                            int resync, uint8 *iv)
      54              : {
      55            0 :         int                     res;
      56            0 :         PX_Cipher  *ciph;
      57            0 :         PGP_CFB    *ctx;
      58              : 
      59            0 :         res = pgp_load_cipher(algo, &ciph);
      60            0 :         if (res < 0)
      61            0 :                 return res;
      62              : 
      63            0 :         res = px_cipher_init(ciph, key, key_len, NULL);
      64            0 :         if (res < 0)
      65              :         {
      66            0 :                 px_cipher_free(ciph);
      67            0 :                 return res;
      68              :         }
      69              : 
      70            0 :         ctx = palloc0_object(PGP_CFB);
      71            0 :         ctx->ciph = ciph;
      72            0 :         ctx->block_size = px_cipher_block_size(ciph);
      73            0 :         ctx->resync = resync;
      74              : 
      75            0 :         if (iv)
      76            0 :                 memcpy(ctx->fr, iv, ctx->block_size);
      77              : 
      78            0 :         *ctx_p = ctx;
      79            0 :         return 0;
      80            0 : }
      81              : 
      82              : void
      83            0 : pgp_cfb_free(PGP_CFB *ctx)
      84              : {
      85            0 :         px_cipher_free(ctx->ciph);
      86            0 :         px_memset(ctx, 0, sizeof(*ctx));
      87            0 :         pfree(ctx);
      88            0 : }
      89              : 
      90              : /*
      91              :  * Data processing for normal CFB.  (PGP_PKT_SYMENCRYPTED_DATA_MDC)
      92              :  */
      93              : static int
      94            0 : mix_encrypt_normal(PGP_CFB *ctx, const uint8 *data, int len, uint8 *dst)
      95              : {
      96            0 :         int                     i;
      97              : 
      98            0 :         for (i = ctx->pos; i < ctx->pos + len; i++)
      99            0 :                 *dst++ = ctx->encbuf[i] = ctx->fre[i] ^ (*data++);
     100            0 :         ctx->pos += len;
     101            0 :         return len;
     102            0 : }
     103              : 
     104              : static int
     105            0 : mix_decrypt_normal(PGP_CFB *ctx, const uint8 *data, int len, uint8 *dst)
     106              : {
     107            0 :         int                     i;
     108              : 
     109            0 :         for (i = ctx->pos; i < ctx->pos + len; i++)
     110              :         {
     111            0 :                 ctx->encbuf[i] = *data++;
     112            0 :                 *dst++ = ctx->fre[i] ^ ctx->encbuf[i];
     113            0 :         }
     114            0 :         ctx->pos += len;
     115            0 :         return len;
     116            0 : }
     117              : 
     118              : /*
     119              :  * Data processing for old PGP CFB mode. (PGP_PKT_SYMENCRYPTED_DATA)
     120              :  *
     121              :  * The goal is to hide the horror from the rest of the code,
     122              :  * thus its all concentrated here.
     123              :  */
     124              : static int
     125            0 : mix_encrypt_resync(PGP_CFB *ctx, const uint8 *data, int len, uint8 *dst)
     126              : {
     127            0 :         int                     i,
     128              :                                 n;
     129              : 
     130              :         /* block #2 is 2 bytes long */
     131            0 :         if (ctx->block_no == 2)
     132              :         {
     133            0 :                 n = 2 - ctx->pos;
     134            0 :                 if (len < n)
     135            0 :                         n = len;
     136            0 :                 for (i = ctx->pos; i < ctx->pos + n; i++)
     137            0 :                         *dst++ = ctx->encbuf[i] = ctx->fre[i] ^ (*data++);
     138              : 
     139            0 :                 ctx->pos += n;
     140            0 :                 len -= n;
     141              : 
     142            0 :                 if (ctx->pos == 2)
     143              :                 {
     144            0 :                         memcpy(ctx->fr, ctx->encbuf + 2, ctx->block_size - 2);
     145            0 :                         memcpy(ctx->fr + ctx->block_size - 2, ctx->encbuf, 2);
     146            0 :                         ctx->pos = 0;
     147            0 :                         return n;
     148              :                 }
     149            0 :         }
     150            0 :         for (i = ctx->pos; i < ctx->pos + len; i++)
     151            0 :                 *dst++ = ctx->encbuf[i] = ctx->fre[i] ^ (*data++);
     152            0 :         ctx->pos += len;
     153            0 :         return len;
     154            0 : }
     155              : 
     156              : static int
     157            0 : mix_decrypt_resync(PGP_CFB *ctx, const uint8 *data, int len, uint8 *dst)
     158              : {
     159            0 :         int                     i,
     160              :                                 n;
     161              : 
     162              :         /* block #2 is 2 bytes long */
     163            0 :         if (ctx->block_no == 2)
     164              :         {
     165            0 :                 n = 2 - ctx->pos;
     166            0 :                 if (len < n)
     167            0 :                         n = len;
     168            0 :                 for (i = ctx->pos; i < ctx->pos + n; i++)
     169              :                 {
     170            0 :                         ctx->encbuf[i] = *data++;
     171            0 :                         *dst++ = ctx->fre[i] ^ ctx->encbuf[i];
     172            0 :                 }
     173            0 :                 ctx->pos += n;
     174            0 :                 len -= n;
     175              : 
     176            0 :                 if (ctx->pos == 2)
     177              :                 {
     178            0 :                         memcpy(ctx->fr, ctx->encbuf + 2, ctx->block_size - 2);
     179            0 :                         memcpy(ctx->fr + ctx->block_size - 2, ctx->encbuf, 2);
     180            0 :                         ctx->pos = 0;
     181            0 :                         return n;
     182              :                 }
     183            0 :         }
     184            0 :         for (i = ctx->pos; i < ctx->pos + len; i++)
     185              :         {
     186            0 :                 ctx->encbuf[i] = *data++;
     187            0 :                 *dst++ = ctx->fre[i] ^ ctx->encbuf[i];
     188            0 :         }
     189            0 :         ctx->pos += len;
     190            0 :         return len;
     191            0 : }
     192              : 
     193              : /*
     194              :  * common code for both encrypt and decrypt.
     195              :  */
     196              : static int
     197            0 : cfb_process(PGP_CFB *ctx, const uint8 *data, int len, uint8 *dst,
     198              :                         mix_data_t mix_data)
     199              : {
     200            0 :         int                     n;
     201            0 :         int                     res;
     202              : 
     203            0 :         while (len > 0 && ctx->pos > 0)
     204              :         {
     205            0 :                 n = ctx->block_size - ctx->pos;
     206            0 :                 if (len < n)
     207            0 :                         n = len;
     208              : 
     209            0 :                 n = mix_data(ctx, data, n, dst);
     210            0 :                 data += n;
     211            0 :                 dst += n;
     212            0 :                 len -= n;
     213              : 
     214            0 :                 if (ctx->pos == ctx->block_size)
     215              :                 {
     216            0 :                         memcpy(ctx->fr, ctx->encbuf, ctx->block_size);
     217            0 :                         ctx->pos = 0;
     218            0 :                 }
     219              :         }
     220              : 
     221            0 :         while (len > 0)
     222              :         {
     223            0 :                 unsigned        rlen;
     224              : 
     225            0 :                 px_cipher_encrypt(ctx->ciph, 0, ctx->fr, ctx->block_size, ctx->fre, &rlen);
     226            0 :                 if (ctx->block_no < 5)
     227            0 :                         ctx->block_no++;
     228              : 
     229            0 :                 n = ctx->block_size;
     230            0 :                 if (len < n)
     231            0 :                         n = len;
     232              : 
     233            0 :                 res = mix_data(ctx, data, n, dst);
     234            0 :                 data += res;
     235            0 :                 dst += res;
     236            0 :                 len -= res;
     237              : 
     238            0 :                 if (ctx->pos == ctx->block_size)
     239              :                 {
     240            0 :                         memcpy(ctx->fr, ctx->encbuf, ctx->block_size);
     241            0 :                         ctx->pos = 0;
     242            0 :                 }
     243            0 :         }
     244            0 :         return 0;
     245            0 : }
     246              : 
     247              : /*
     248              :  * public interface
     249              :  */
     250              : 
     251              : int
     252            0 : pgp_cfb_encrypt(PGP_CFB *ctx, const uint8 *data, int len, uint8 *dst)
     253              : {
     254            0 :         mix_data_t      mix = ctx->resync ? mix_encrypt_resync : mix_encrypt_normal;
     255              : 
     256            0 :         return cfb_process(ctx, data, len, dst, mix);
     257            0 : }
     258              : 
     259              : int
     260            0 : pgp_cfb_decrypt(PGP_CFB *ctx, const uint8 *data, int len, uint8 *dst)
     261              : {
     262            0 :         mix_data_t      mix = ctx->resync ? mix_decrypt_resync : mix_decrypt_normal;
     263              : 
     264            0 :         return cfb_process(ctx, data, len, dst, mix);
     265            0 : }
        

Generated by: LCOV version 2.3.2-1