Coverage Report

Created: 2024-11-21 07:03

/src/libtomcrypt/src/hashes/sha2/sha384.c
Line
Count
Source (jump to first uncovered line)
1
/* LibTomCrypt, modular cryptographic library -- Tom St Denis */
2
/* SPDX-License-Identifier: Unlicense */
3
/**
4
   @param sha384.c
5
   LTC_SHA384 hash included in sha512.c, Tom St Denis
6
*/
7
8
#include "tomcrypt_private.h"
9
10
#if defined(LTC_SHA384) && defined(LTC_SHA512)
11
12
const struct ltc_hash_descriptor sha384_desc =
13
{
14
    "sha384",
15
    4,
16
    48,
17
    128,
18
19
    /* OID */
20
   { 2, 16, 840, 1, 101, 3, 4, 2, 2,  },
21
   9,
22
23
    &sha384_init,
24
    &sha512_process,
25
    &sha384_done,
26
    &sha384_test,
27
    NULL
28
};
29
30
/**
31
   Initialize the hash state
32
   @param md   The hash state you wish to initialize
33
   @return CRYPT_OK if successful
34
*/
35
int sha384_init(hash_state * md)
36
4.61k
{
37
4.61k
    LTC_ARGCHK(md != NULL);
38
39
4.61k
    md->sha512.curlen = 0;
40
4.61k
    md->sha512.length = 0;
41
4.61k
    md->sha512.state[0] = CONST64(0xcbbb9d5dc1059ed8);
42
4.61k
    md->sha512.state[1] = CONST64(0x629a292a367cd507);
43
4.61k
    md->sha512.state[2] = CONST64(0x9159015a3070dd17);
44
4.61k
    md->sha512.state[3] = CONST64(0x152fecd8f70e5939);
45
4.61k
    md->sha512.state[4] = CONST64(0x67332667ffc00b31);
46
4.61k
    md->sha512.state[5] = CONST64(0x8eb44a8768581511);
47
4.61k
    md->sha512.state[6] = CONST64(0xdb0c2e0d64f98fa7);
48
4.61k
    md->sha512.state[7] = CONST64(0x47b5481dbefa4fa4);
49
4.61k
    return CRYPT_OK;
50
4.61k
}
51
52
/**
53
   Terminate the hash to get the digest
54
   @param md  The hash state
55
   @param out [out] The destination of the hash (48 bytes)
56
   @return CRYPT_OK if successful
57
*/
58
int sha384_done(hash_state * md, unsigned char *out)
59
4.61k
{
60
4.61k
   unsigned char buf[64];
61
62
4.61k
   LTC_ARGCHK(md  != NULL);
63
4.61k
   LTC_ARGCHK(out != NULL);
64
65
4.61k
    if (md->sha512.curlen >= sizeof(md->sha512.buf)) {
66
0
       return CRYPT_INVALID_ARG;
67
0
    }
68
69
4.61k
   sha512_done(md, buf);
70
4.61k
   XMEMCPY(out, buf, 48);
71
#ifdef LTC_CLEAN_STACK
72
   zeromem(buf, sizeof(buf));
73
#endif
74
4.61k
   return CRYPT_OK;
75
4.61k
}
76
77
/**
78
  Self-test the hash
79
  @return CRYPT_OK if successful, CRYPT_NOP if self-tests have been disabled
80
*/
81
int  sha384_test(void)
82
0
{
83
 #ifndef LTC_TEST
84
    return CRYPT_NOP;
85
 #else
86
0
  static const struct {
87
0
      const char *msg;
88
0
      unsigned char hash[48];
89
0
  } tests[] = {
90
0
    { "abc",
91
0
      { 0xcb, 0x00, 0x75, 0x3f, 0x45, 0xa3, 0x5e, 0x8b,
92
0
        0xb5, 0xa0, 0x3d, 0x69, 0x9a, 0xc6, 0x50, 0x07,
93
0
        0x27, 0x2c, 0x32, 0xab, 0x0e, 0xde, 0xd1, 0x63,
94
0
        0x1a, 0x8b, 0x60, 0x5a, 0x43, 0xff, 0x5b, 0xed,
95
0
        0x80, 0x86, 0x07, 0x2b, 0xa1, 0xe7, 0xcc, 0x23,
96
0
        0x58, 0xba, 0xec, 0xa1, 0x34, 0xc8, 0x25, 0xa7 }
97
0
    },
98
0
    { "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu",
99
0
      { 0x09, 0x33, 0x0c, 0x33, 0xf7, 0x11, 0x47, 0xe8,
100
0
        0x3d, 0x19, 0x2f, 0xc7, 0x82, 0xcd, 0x1b, 0x47,
101
0
        0x53, 0x11, 0x1b, 0x17, 0x3b, 0x3b, 0x05, 0xd2,
102
0
        0x2f, 0xa0, 0x80, 0x86, 0xe3, 0xb0, 0xf7, 0x12,
103
0
        0xfc, 0xc7, 0xc7, 0x1a, 0x55, 0x7e, 0x2d, 0xb9,
104
0
        0x66, 0xc3, 0xe9, 0xfa, 0x91, 0x74, 0x60, 0x39 }
105
0
    },
106
0
  };
107
108
0
  int i;
109
0
  unsigned char tmp[48];
110
0
  hash_state md;
111
112
0
  for (i = 0; i < (int)(sizeof(tests) / sizeof(tests[0])); i++) {
113
0
      sha384_init(&md);
114
0
      sha384_process(&md, (unsigned char*)tests[i].msg, (unsigned long)XSTRLEN(tests[i].msg));
115
0
      sha384_done(&md, tmp);
116
0
      if (compare_testvector(tmp, sizeof(tmp), tests[i].hash, sizeof(tests[i].hash), "SHA384", i)) {
117
0
         return CRYPT_FAIL_TESTVECTOR;
118
0
      }
119
0
  }
120
0
  return CRYPT_OK;
121
0
 #endif
122
0
}
123
124
#endif /* defined(LTC_SHA384) && defined(LTC_SHA512) */