~ubuntu-branches/ubuntu/utopic/dropbear/utopic-proposed

« back to all changes in this revision

Viewing changes to libtomcrypt/src/hashes/sha2/sha224.c

  • Committer: Bazaar Package Importer
  • Author(s): Matt Johnston
  • Date: 2005-12-08 19:20:21 UTC
  • mfrom: (1.2.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20051208192021-nyp9rwnt77nsg6ty
Tags: 0.47-1
* New upstream release.
* SECURITY: Fix incorrect buffer sizing.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
 
2
 *
 
3
 * LibTomCrypt is a library that provides various cryptographic
 
4
 * algorithms in a highly modular and flexible manner.
 
5
 *
 
6
 * The library is free for all purposes without any express
 
7
 * guarantee it works.
 
8
 *
 
9
 * Tom St Denis, tomstdenis@gmail.com, http://libtomcrypt.org
 
10
 */
 
11
/**
 
12
   @param sha224.c
 
13
   SHA-224 new NIST standard based off of SHA-256 truncated to 224 bits (Tom St Denis)
 
14
*/
 
15
 
 
16
const struct ltc_hash_descriptor sha224_desc =
 
17
{
 
18
    "sha224",
 
19
    10,
 
20
    28,
 
21
    64,
 
22
 
 
23
    /* OID */
 
24
   { 2, 16, 840, 1, 101, 3, 4, 2, 4,  },
 
25
   9,
 
26
 
 
27
    &sha224_init,
 
28
    &sha256_process,
 
29
    &sha224_done,
 
30
    &sha224_test
 
31
};
 
32
 
 
33
/* init the sha256 er... sha224 state ;-) */
 
34
/**
 
35
   Initialize the hash state
 
36
   @param md   The hash state you wish to initialize
 
37
   @return CRYPT_OK if successful
 
38
*/
 
39
int sha224_init(hash_state * md)
 
40
{
 
41
    LTC_ARGCHK(md != NULL);
 
42
 
 
43
    md->sha256.curlen = 0;
 
44
    md->sha256.length = 0;
 
45
    md->sha256.state[0] = 0xc1059ed8UL;
 
46
    md->sha256.state[1] = 0x367cd507UL;
 
47
    md->sha256.state[2] = 0x3070dd17UL;
 
48
    md->sha256.state[3] = 0xf70e5939UL;
 
49
    md->sha256.state[4] = 0xffc00b31UL;
 
50
    md->sha256.state[5] = 0x68581511UL;
 
51
    md->sha256.state[6] = 0x64f98fa7UL;
 
52
    md->sha256.state[7] = 0xbefa4fa4UL;
 
53
    return CRYPT_OK;
 
54
}
 
55
 
 
56
/**
 
57
   Terminate the hash to get the digest
 
58
   @param md  The hash state
 
59
   @param out [out] The destination of the hash (28 bytes)
 
60
   @return CRYPT_OK if successful
 
61
*/
 
62
int sha224_done(hash_state * md, unsigned char *out)
 
63
{
 
64
    unsigned char buf[32];
 
65
    int err;
 
66
 
 
67
    LTC_ARGCHK(md  != NULL);
 
68
    LTC_ARGCHK(out != NULL);
 
69
 
 
70
    err = sha256_done(md, buf);
 
71
    XMEMCPY(out, buf, 28);
 
72
#ifdef LTC_CLEAN_STACK
 
73
    zeromem(buf, sizeof(buf));
 
74
#endif 
 
75
    return err;
 
76
}
 
77
 
 
78
/**
 
79
  Self-test the hash
 
80
  @return CRYPT_OK if successful, CRYPT_NOP if self-tests have been disabled
 
81
*/  
 
82
int  sha224_test(void)
 
83
{
 
84
 #ifndef LTC_TEST
 
85
    return CRYPT_NOP;
 
86
 #else    
 
87
  static const struct {
 
88
      char *msg;
 
89
      unsigned char hash[28];
 
90
  } tests[] = {
 
91
    { "abc",
 
92
      { 0x23, 0x09, 0x7d, 0x22, 0x34, 0x05, 0xd8,
 
93
        0x22, 0x86, 0x42, 0xa4, 0x77, 0xbd, 0xa2,
 
94
        0x55, 0xb3, 0x2a, 0xad, 0xbc, 0xe4, 0xbd,
 
95
        0xa0, 0xb3, 0xf7, 0xe3, 0x6c, 0x9d, 0xa7 }
 
96
    },
 
97
    { "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
 
98
      { 0x75, 0x38, 0x8b, 0x16, 0x51, 0x27, 0x76,
 
99
        0xcc, 0x5d, 0xba, 0x5d, 0xa1, 0xfd, 0x89,
 
100
        0x01, 0x50, 0xb0, 0xc6, 0x45, 0x5c, 0xb4,
 
101
        0xf5, 0x8b, 0x19, 0x52, 0x52, 0x25, 0x25 }
 
102
    },
 
103
  };
 
104
 
 
105
  int i;
 
106
  unsigned char tmp[28];
 
107
  hash_state md;
 
108
 
 
109
  for (i = 0; i < (int)(sizeof(tests) / sizeof(tests[0])); i++) {
 
110
      sha224_init(&md);
 
111
      sha224_process(&md, (unsigned char*)tests[i].msg, (unsigned long)strlen(tests[i].msg));
 
112
      sha224_done(&md, tmp);
 
113
      if (memcmp(tmp, tests[i].hash, 28) != 0) {
 
114
         return CRYPT_FAIL_TESTVECTOR;
 
115
      }
 
116
  }
 
117
  return CRYPT_OK;
 
118
 #endif
 
119
}
 
120
 
 
121
 
 
122
/* $Source: /cvs/libtom/libtomcrypt/src/hashes/sha2/sha224.c,v $ */
 
123
/* $Revision: 1.5 $ */
 
124
/* $Date: 2005/05/23 02:42:07 $ */