|
| 1 | +/* LibTomCrypt, modular cryptographic library -- Tom St Denis */ |
| 2 | +/* SPDX-License-Identifier: Unlicense */ |
| 3 | +#include "tomcrypt_private.h" |
| 4 | + |
| 5 | +#ifdef LTC_SHA1 |
| 6 | + |
| 7 | +const struct ltc_hash_descriptor sha1_desc = |
| 8 | +{ |
| 9 | + "sha1", |
| 10 | + 2, |
| 11 | + 20, |
| 12 | + 64, |
| 13 | + |
| 14 | + /* OID */ |
| 15 | + { 1, 3, 14, 3, 2, 26, }, |
| 16 | + 6, |
| 17 | + |
| 18 | + &sha1_init, |
| 19 | + &sha1_process, |
| 20 | + &sha1_done, |
| 21 | + &sha1_test, |
| 22 | + NULL |
| 23 | +}; |
| 24 | + |
| 25 | +/** |
| 26 | + Initialize the hash state |
| 27 | + @param md The hash state you wish to initialize |
| 28 | + @return CRYPT_OK if successful |
| 29 | +*/ |
| 30 | +int sha1_init(hash_state * md) |
| 31 | +{ |
| 32 | + int err; |
| 33 | + |
| 34 | + #if defined LTC_SHA1_X86 |
| 35 | + ~~~todo~~~ |
| 36 | + #else |
| 37 | + err = sha1_c_init(md); if(err != CRYPT_OK){ return err; } |
| 38 | + #endif |
| 39 | + return CRYPT_OK; |
| 40 | +} |
| 41 | + |
| 42 | +/** |
| 43 | + Process a block of memory though the hash |
| 44 | + @param md The hash state |
| 45 | + @param in The data to hash |
| 46 | + @param inlen The length of the data (octets) |
| 47 | + @return CRYPT_OK if successful |
| 48 | +*/ |
| 49 | +int sha1_process(hash_state * md, const unsigned char *in, unsigned long inlen) |
| 50 | +{ |
| 51 | + int err; |
| 52 | + |
| 53 | + #if defined LTC_SHA1_X86 |
| 54 | + ~~~todo~~~ |
| 55 | + #else |
| 56 | + err = sha1_c_process(md, in, inlen); if(err != CRYPT_OK){ return err; } |
| 57 | + #endif |
| 58 | + return CRYPT_OK; |
| 59 | +} |
| 60 | + |
| 61 | +/** |
| 62 | + Terminate the hash to get the digest |
| 63 | + @param md The hash state |
| 64 | + @param out [out] The destination of the hash (20 bytes) |
| 65 | + @return CRYPT_OK if successful |
| 66 | +*/ |
| 67 | +int sha1_done(hash_state * md, unsigned char *out) |
| 68 | +{ |
| 69 | + int err; |
| 70 | + |
| 71 | + #if defined LTC_SHA1_X86 |
| 72 | + ~~~todo~~~ |
| 73 | + #else |
| 74 | + err = sha1_c_done(md, out); if(err != CRYPT_OK){ return err; } |
| 75 | + #endif |
| 76 | + return CRYPT_OK; |
| 77 | +} |
| 78 | + |
| 79 | +/** |
| 80 | + Self-test the hash |
| 81 | + @return CRYPT_OK if successful, CRYPT_NOP if self-tests have been disabled |
| 82 | +*/ |
| 83 | +int sha1_test(void) |
| 84 | +{ |
| 85 | + #ifndef LTC_TEST |
| 86 | + return CRYPT_NOP; |
| 87 | + #else |
| 88 | + static const struct { |
| 89 | + const char *msg; |
| 90 | + unsigned char hash[20]; |
| 91 | + } tests[] = { |
| 92 | + { "abc", |
| 93 | + { 0xa9, 0x99, 0x3e, 0x36, 0x47, 0x06, 0x81, 0x6a, |
| 94 | + 0xba, 0x3e, 0x25, 0x71, 0x78, 0x50, 0xc2, 0x6c, |
| 95 | + 0x9c, 0xd0, 0xd8, 0x9d } |
| 96 | + }, |
| 97 | + { "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", |
| 98 | + { 0x84, 0x98, 0x3E, 0x44, 0x1C, 0x3B, 0xD2, 0x6E, |
| 99 | + 0xBA, 0xAE, 0x4A, 0xA1, 0xF9, 0x51, 0x29, 0xE5, |
| 100 | + 0xE5, 0x46, 0x70, 0xF1 } |
| 101 | + } |
| 102 | + }; |
| 103 | + |
| 104 | + int i; |
| 105 | + unsigned char tmp[20]; |
| 106 | + hash_state md; |
| 107 | + |
| 108 | + for (i = 0; i < (int)(sizeof(tests) / sizeof(tests[0])); i++) { |
| 109 | + sha1_init(&md); |
| 110 | + sha1_process(&md, (unsigned char*)tests[i].msg, (unsigned long)XSTRLEN(tests[i].msg)); |
| 111 | + sha1_done(&md, tmp); |
| 112 | + if (ltc_compare_testvector(tmp, sizeof(tmp), tests[i].hash, sizeof(tests[i].hash), "SHA1", i)) { |
| 113 | + return CRYPT_FAIL_TESTVECTOR; |
| 114 | + } |
| 115 | + } |
| 116 | + return CRYPT_OK; |
| 117 | + #endif |
| 118 | +} |
| 119 | + |
| 120 | +#endif |
0 commit comments