LCOV - code coverage report
Current view: top level - contrib/pgcrypto - px-hmac.c (source / functions) Hit Total Coverage
Test: PostgreSQL 18devel Lines: 62 72 86.1 %
Date: 2024-11-21 08:14:44 Functions: 6 8 75.0 %
Legend: Lines: hit not hit

          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
      40          28 : hmac_result_size(PX_HMAC *h)
      41             : {
      42          28 :     return px_md_result_size(h->md);
      43             : }
      44             : 
      45             : static unsigned
      46           0 : hmac_block_size(PX_HMAC *h)
      47             : {
      48           0 :     return px_md_block_size(h->md);
      49             : }
      50             : 
      51             : static void
      52          28 : hmac_init(PX_HMAC *h, const uint8 *key, unsigned klen)
      53             : {
      54             :     unsigned    bs,
      55             :                 i;
      56             :     uint8      *keybuf;
      57          28 :     PX_MD      *md = h->md;
      58             : 
      59          28 :     bs = px_md_block_size(md);
      60          28 :     keybuf = palloc0(bs);
      61             : 
      62          28 :     if (klen > bs)
      63             :     {
      64           8 :         px_md_update(md, key, klen);
      65           8 :         px_md_finish(md, keybuf);
      66           8 :         px_md_reset(md);
      67             :     }
      68             :     else
      69          20 :         memcpy(keybuf, key, klen);
      70             : 
      71        1820 :     for (i = 0; i < bs; i++)
      72             :     {
      73        1792 :         h->p.ipad[i] = keybuf[i] ^ HMAC_IPAD;
      74        1792 :         h->p.opad[i] = keybuf[i] ^ HMAC_OPAD;
      75             :     }
      76             : 
      77          28 :     px_memset(keybuf, 0, bs);
      78          28 :     pfree(keybuf);
      79             : 
      80          28 :     px_md_update(md, h->p.ipad, bs);
      81          28 : }
      82             : 
      83             : static void
      84           0 : hmac_reset(PX_HMAC *h)
      85             : {
      86           0 :     PX_MD      *md = h->md;
      87           0 :     unsigned    bs = px_md_block_size(md);
      88             : 
      89           0 :     px_md_reset(md);
      90           0 :     px_md_update(md, h->p.ipad, bs);
      91           0 : }
      92             : 
      93             : static void
      94          28 : hmac_update(PX_HMAC *h, const uint8 *data, unsigned dlen)
      95             : {
      96          28 :     px_md_update(h->md, data, dlen);
      97          28 : }
      98             : 
      99             : static void
     100          28 : hmac_finish(PX_HMAC *h, uint8 *dst)
     101             : {
     102          28 :     PX_MD      *md = h->md;
     103             :     unsigned    bs,
     104             :                 hlen;
     105             :     uint8      *buf;
     106             : 
     107          28 :     bs = px_md_block_size(md);
     108          28 :     hlen = px_md_result_size(md);
     109             : 
     110          28 :     buf = palloc(hlen);
     111             : 
     112          28 :     px_md_finish(md, buf);
     113             : 
     114          28 :     px_md_reset(md);
     115          28 :     px_md_update(md, h->p.opad, bs);
     116          28 :     px_md_update(md, buf, hlen);
     117          28 :     px_md_finish(md, dst);
     118             : 
     119          28 :     px_memset(buf, 0, hlen);
     120          28 :     pfree(buf);
     121          28 : }
     122             : 
     123             : static void
     124          28 : hmac_free(PX_HMAC *h)
     125             : {
     126             :     unsigned    bs;
     127             : 
     128          28 :     bs = px_md_block_size(h->md);
     129          28 :     px_md_free(h->md);
     130             : 
     131          28 :     px_memset(h->p.ipad, 0, bs);
     132          28 :     px_memset(h->p.opad, 0, bs);
     133          28 :     pfree(h->p.ipad);
     134          28 :     pfree(h->p.opad);
     135          28 :     pfree(h);
     136          28 : }
     137             : 
     138             : 
     139             : /* PUBLIC FUNCTIONS */
     140             : 
     141             : int
     142          30 : 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             : 
     149          30 :     err = px_find_digest(name, &md);
     150          30 :     if (err)
     151           2 :         return err;
     152             : 
     153          28 :     bs = px_md_block_size(md);
     154          28 :     if (bs < 2)
     155             :     {
     156           0 :         px_md_free(md);
     157           0 :         return PXE_HASH_UNUSABLE_FOR_HMAC;
     158             :     }
     159             : 
     160          28 :     h = palloc(sizeof(*h));
     161          28 :     h->p.ipad = palloc(bs);
     162          28 :     h->p.opad = palloc(bs);
     163          28 :     h->md = md;
     164             : 
     165          28 :     h->result_size = hmac_result_size;
     166          28 :     h->block_size = hmac_block_size;
     167          28 :     h->reset = hmac_reset;
     168          28 :     h->update = hmac_update;
     169          28 :     h->finish = hmac_finish;
     170          28 :     h->free = hmac_free;
     171          28 :     h->init = hmac_init;
     172             : 
     173          28 :     *res = h;
     174             : 
     175          28 :     return 0;
     176             : }

Generated by: LCOV version 1.14