~ubuntu-branches/ubuntu/hardy/gnupg/hardy-updates

« back to all changes in this revision

Viewing changes to cipher/sha256.c

  • Committer: Bazaar Package Importer
  • Author(s): Martin Pitt
  • Date: 2006-11-03 09:18:26 UTC
  • mto: This revision was merged to the branch mainline in revision 11.
  • Revision ID: james.westby@ubuntu.com-20061103091826-89kwl8tk1xypbmtk
Tags: upstream-1.4.5
ImportĀ upstreamĀ versionĀ 1.4.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* sha256.c - SHA256 hash function
2
 
 *      Copyright (C) 2003 Free Software Foundation, Inc.
 
1
/* sha256.c - SHA224 and SHA256 hash functions
 
2
 * Copyright (C) 2003, 2006 Free Software Foundation, Inc.
3
3
 *
4
4
 * Please see below for more legal information!
5
5
 *
25
25
/*  Test vectors from FIPS-180-2:
26
26
 *
27
27
 *  "abc"
 
28
 * 224:
 
29
 *  23097D22 3405D822 8642A477 BDA255B3 2AADBCE4 BDA0B3F7 E36C9DA7
 
30
 * 256:
28
31
 *  BA7816BF 8F01CFEA 414140DE 5DAE2223 B00361A3 96177A9C B410FF61 F20015AD
29
32
 *
30
33
 *  "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"
 
34
 * 224:
 
35
 *  75388B16 512776CC 5DBA5DA1 FD890150 B0C6455C B4F58B19 52522525
 
36
 * 256:
31
37
 *  248D6A61 D20638B8 E5C02693 0C3E6039 A33CE459 64FF2167 F6ECEDD4 19DB06C1
32
38
 *
33
39
 *  "a" x 1000000
 
40
 * 224:
 
41
 *  20794655 980C91D8 BBB4C1EA 97618A4B F03F4258 1948B2EE 4EE7AD67
 
42
 * 256:
34
43
 *  CDC76E5C 9914FB92 81A1C7E2 84D73E67 F1809A48 A497200E 046D39CC C7112CD0
35
44
 */
36
45
 
76
85
    hd->count = 0;
77
86
}
78
87
 
 
88
void
 
89
sha224_init( SHA256_CONTEXT *hd )
 
90
{
 
91
    hd->h0 = 0xc1059ed8;
 
92
    hd->h1 = 0x367cd507;
 
93
    hd->h2 = 0x3070dd17;
 
94
    hd->h3 = 0xf70e5939;
 
95
    hd->h4 = 0xffc00b31;
 
96
    hd->h5 = 0x68581511;
 
97
    hd->h6 = 0x64f98fa7;
 
98
    hd->h7 = 0xbefa4fa4;
 
99
 
 
100
    hd->nblocks = 0;
 
101
    hd->count = 0;
 
102
}
 
103
 
79
104
 
80
105
/****************
81
106
 * Transform the message w which consists of 16 32-bit words
207
232
 * returns the digest.
208
233
 * The handle is prepared for a new cycle, but adding bytes to the
209
234
 * handle will the destroy the returned buffer.
210
 
 * Returns: 32 bytes representing the digest.
 
235
 * Returns: 32 bytes representing the digest.  When used for sha224,
 
236
 * we take the leftmost 28 of those bytes.
211
237
 */
212
238
 
213
239
static void
270
296
    X(4);
271
297
    X(5);
272
298
    X(6);
 
299
    /* Note that this last chunk is included even for SHA224.  We just
 
300
       ignore it. */
273
301
    X(7);
274
302
#undef X
275
303
}
316
344
 
317
345
    return "SHA256";
318
346
}
 
347
 
 
348
/* SHA224 is really a truncated SHA256 with a different
 
349
   initialization */
 
350
const char *
 
351
sha224_get_info( int algo, size_t *contextsize,
 
352
                 byte **r_asnoid, int *r_asnlen, int *r_mdlen,
 
353
                 void (**r_init)( void *c ),
 
354
                 void (**r_write)( void *c, byte *buf, size_t nbytes ),
 
355
                 void (**r_final)( void *c ),
 
356
                 byte *(**r_read)( void *c )
 
357
                 )
 
358
{
 
359
    static byte asn[] = /* Object ID is 2.16.840.1.101.3.4.2.4 */
 
360
      { 
 
361
        0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86,
 
362
        0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x04, 0x05,
 
363
        0x00, 0x04, 0x20
 
364
      };
 
365
 
 
366
    if( algo != 11 )
 
367
        return NULL;
 
368
 
 
369
    *contextsize = sizeof(SHA256_CONTEXT);
 
370
    *r_asnoid = asn;
 
371
    *r_asnlen = DIM(asn);
 
372
    *r_mdlen = 28;
 
373
    *(void  (**)(SHA256_CONTEXT *))r_init                 = sha224_init;
 
374
    *(void  (**)(SHA256_CONTEXT *, byte*, size_t))r_write = sha256_write;
 
375
    *(void  (**)(SHA256_CONTEXT *))r_final                = sha256_final;
 
376
    *(byte *(**)(SHA256_CONTEXT *))r_read                 = sha256_read;
 
377
 
 
378
    return "SHA224";
 
379
}