LCOV - differential code coverage report
Current view: top level - contrib/pgcrypto - px-hmac.c (source / functions) Coverage Total Hit UBC CBC
Current: Differential Code Coverage HEAD vs 15 Lines: 86.1 % 72 62 10 62
Current Date: 2023-04-08 17:13:01 Functions: 75.0 % 8 6 2 6
Baseline: 15 Line coverage date bins:
Baseline Date: 2023-04-08 15:09:40 (240..) days: 86.1 % 72 62 10 62
Legend: Lines: hit not hit Function coverage date bins:
(240..) days: 75.0 % 8 6 2 6

 Age         Owner                  TLA  Line data    Source code
                                  1                 : /*
                                  2                 :  * px-hmac.c
                                  3                 :  *      HMAC implementation.
                                  4                 :  *
                                  5                 :  * Copyright (c) 2001 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/px-hmac.c
                                 30                 :  */
                                 31                 : 
                                 32                 : #include "postgres.h"
                                 33                 : 
                                 34                 : #include "px.h"
                                 35                 : 
                                 36                 : #define HMAC_IPAD 0x36
                                 37                 : #define HMAC_OPAD 0x5C
                                 38                 : 
                                 39                 : static unsigned
 5050 bruce                      40 CBC          14 : hmac_result_size(PX_HMAC *h)
                                 41                 : {
 7901                            42              14 :     return px_md_result_size(h->md);
                                 43                 : }
                                 44                 : 
                                 45                 : static unsigned
 5050 bruce                      46 UBC           0 : hmac_block_size(PX_HMAC *h)
                                 47                 : {
 7901                            48               0 :     return px_md_block_size(h->md);
                                 49                 : }
                                 50                 : 
                                 51                 : static void
 5050 bruce                      52 CBC          14 : hmac_init(PX_HMAC *h, const uint8 *key, unsigned klen)
                                 53                 : {
                                 54                 :     unsigned    bs,
                                 55                 :                 i;
                                 56                 :     uint8      *keybuf;
 7901                            57              14 :     PX_MD      *md = h->md;
                                 58                 : 
                                 59              14 :     bs = px_md_block_size(md);
  926 michael                    60              14 :     keybuf = palloc0(bs);
                                 61                 : 
 7901 bruce                      62              14 :     if (klen > bs)
                                 63                 :     {
                                 64               4 :         px_md_update(md, key, klen);
                                 65               4 :         px_md_finish(md, keybuf);
                                 66               4 :         px_md_reset(md);
                                 67                 :     }
                                 68                 :     else
                                 69              10 :         memcpy(keybuf, key, klen);
                                 70                 : 
                                 71             910 :     for (i = 0; i < bs; i++)
                                 72                 :     {
                                 73             896 :         h->p.ipad[i] = keybuf[i] ^ HMAC_IPAD;
                                 74             896 :         h->p.opad[i] = keybuf[i] ^ HMAC_OPAD;
                                 75                 :     }
                                 76                 : 
 3279                            77              14 :     px_memset(keybuf, 0, bs);
  926 michael                    78              14 :     pfree(keybuf);
                                 79                 : 
 7901 bruce                      80              14 :     px_md_update(md, h->p.ipad, bs);
                                 81              14 : }
                                 82                 : 
                                 83                 : static void
 5050 bruce                      84 UBC           0 : hmac_reset(PX_HMAC *h)
                                 85                 : {
 7901                            86               0 :     PX_MD      *md = h->md;
 7810                            87               0 :     unsigned    bs = px_md_block_size(md);
                                 88                 : 
 7901                            89               0 :     px_md_reset(md);
                                 90               0 :     px_md_update(md, h->p.ipad, bs);
                                 91               0 : }
                                 92                 : 
                                 93                 : static void
 5050 bruce                      94 CBC          14 : hmac_update(PX_HMAC *h, const uint8 *data, unsigned dlen)
                                 95                 : {
 7901                            96              14 :     px_md_update(h->md, data, dlen);
                                 97              14 : }
                                 98                 : 
                                 99                 : static void
 5050                           100              14 : hmac_finish(PX_HMAC *h, uint8 *dst)
                                101                 : {
 7901                           102              14 :     PX_MD      *md = h->md;
                                103                 :     unsigned    bs,
                                104                 :                 hlen;
                                105                 :     uint8      *buf;
                                106                 : 
                                107              14 :     bs = px_md_block_size(md);
                                108              14 :     hlen = px_md_result_size(md);
                                109                 : 
  926 michael                   110              14 :     buf = palloc(hlen);
                                111                 : 
 7901 bruce                     112              14 :     px_md_finish(md, buf);
                                113                 : 
                                114              14 :     px_md_reset(md);
                                115              14 :     px_md_update(md, h->p.opad, bs);
                                116              14 :     px_md_update(md, buf, hlen);
                                117              14 :     px_md_finish(md, dst);
                                118                 : 
 3279                           119              14 :     px_memset(buf, 0, hlen);
  926 michael                   120              14 :     pfree(buf);
 7901 bruce                     121              14 : }
                                122                 : 
                                123                 : static void
 5050                           124              14 : hmac_free(PX_HMAC *h)
                                125                 : {
                                126                 :     unsigned    bs;
                                127                 : 
 7901                           128              14 :     bs = px_md_block_size(h->md);
                                129              14 :     px_md_free(h->md);
                                130                 : 
 3279                           131              14 :     px_memset(h->p.ipad, 0, bs);
                                132              14 :     px_memset(h->p.opad, 0, bs);
  926 michael                   133              14 :     pfree(h->p.ipad);
                                134              14 :     pfree(h->p.opad);
                                135              14 :     pfree(h);
 7901 bruce                     136              14 : }
                                137                 : 
                                138                 : 
                                139                 : /* PUBLIC FUNCTIONS */
                                140                 : 
                                141                 : int
 5050                           142              15 : px_find_hmac(const char *name, PX_HMAC **res)
                                143                 : {
                                144                 :     int         err;
                                145                 :     PX_MD      *md;
                                146                 :     PX_HMAC    *h;
                                147                 :     unsigned    bs;
                                148                 : 
 7901                           149              15 :     err = px_find_digest(name, &md);
                                150              15 :     if (err)
                                151               1 :         return err;
                                152                 : 
                                153              14 :     bs = px_md_block_size(md);
                                154              14 :     if (bs < 2)
                                155                 :     {
 7901 bruce                     156 UBC           0 :         px_md_free(md);
 6593 neilc                     157               0 :         return PXE_HASH_UNUSABLE_FOR_HMAC;
                                158                 :     }
                                159                 : 
  926 michael                   160 CBC          14 :     h = palloc(sizeof(*h));
                                161              14 :     h->p.ipad = palloc(bs);
                                162              14 :     h->p.opad = palloc(bs);
 7901 bruce                     163              14 :     h->md = md;
                                164                 : 
                                165              14 :     h->result_size = hmac_result_size;
                                166              14 :     h->block_size = hmac_block_size;
                                167              14 :     h->reset = hmac_reset;
                                168              14 :     h->update = hmac_update;
                                169              14 :     h->finish = hmac_finish;
                                170              14 :     h->free = hmac_free;
                                171              14 :     h->init = hmac_init;
                                172                 : 
                                173              14 :     *res = h;
                                174                 : 
                                175              14 :     return 0;
                                176                 : }
        

Generated by: LCOV version v1.16-55-g56c0a2a