|
| 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 | + |
| 10 | +/* The implementation is based on: |
| 11 | + * "Extending the Salsa20 nonce", https://cr.yp.to/snuffle/xsalsa-20081128.pdf |
| 12 | + * "Salsa20 specification", http://cr.yp.to/snuffle/spec.pdf |
| 13 | + * and salsa20-ref.c version 20051118 |
| 14 | + * Public domain from D. J. Bernstein |
| 15 | + */ |
| 16 | + |
| 17 | +#include "tomcrypt.h" |
| 18 | + |
| 19 | +#ifdef LTC_XSALSA20 |
| 20 | + |
| 21 | +#ifdef LTC_SHA256 |
| 22 | +int _sha256(unsigned char *hash, const unsigned char *data, const int datalen) { |
| 23 | + hash_state md; |
| 24 | + sha256_init(&md); |
| 25 | + sha256_process(&md, data, datalen); |
| 26 | + sha256_done(&md, hash); |
| 27 | + return CRYPT_OK; |
| 28 | +} |
| 29 | +#endif |
| 30 | + |
| 31 | +int xsalsa20_test(void) |
| 32 | +{ |
| 33 | +#ifndef LTC_TEST |
| 34 | + return CRYPT_NOP; |
| 35 | +#else |
| 36 | + |
| 37 | + /*************************************************************************** |
| 38 | + * verify a round trip: |
| 39 | + */ |
| 40 | + { |
| 41 | + const unsigned char key[] = {0x1b,0x27,0x55,0x64,0x73,0xe9,0x85,0xd4,0x62,0xcd,0x51,0x19,0x7a,0x9a,0x46,0xc7,0x60,0x09,0x54,0x9e,0xac,0x64,0x74,0xf2,0x06,0xc4,0xee,0x08,0x44,0xf6,0x83,0x89}; |
| 42 | + const unsigned char nonce[] = {0x69,0x69,0x6e,0xe9,0x55,0xb6,0x2b,0x73,0xcd,0x62,0xbd,0xa8,0x75,0xfc,0x73,0xd6,0x82,0x19,0xe0,0x03,0x6b,0x7a,0x0b,0x37}; |
| 43 | + const void *msg = "Kilroy was here!"; |
| 44 | + unsigned char msglen = 17; /* includes trailing NULL */ |
| 45 | + int rounds = 20; |
| 46 | + unsigned char ciphertext[17]; |
| 47 | + unsigned char msg2[17]; |
| 48 | + salsa20_state st; |
| 49 | + int err; |
| 50 | + |
| 51 | + if ((err = xsalsa20_setup(&st, key, 32, nonce, 24, rounds)) != CRYPT_OK) return err; |
| 52 | + if ((err = salsa20_crypt(&st, msg, msglen, ciphertext)) != CRYPT_OK) return err; |
| 53 | + if ((err = salsa20_done(&st)) != CRYPT_OK) return err; |
| 54 | + |
| 55 | + if ((err = xsalsa20_setup(&st, key, 32, nonce, 24, rounds)) != CRYPT_OK) return err; |
| 56 | + if ((err = salsa20_crypt(&st, ciphertext, msglen, msg2)) != CRYPT_OK) return err; |
| 57 | + if ((err = salsa20_done(&st)) != CRYPT_OK) return err; |
| 58 | + |
| 59 | + if (compare_testvector(msg, msglen, msg2, msglen, "XSALSA20-TV1", 1)) return CRYPT_FAIL_TESTVECTOR; |
| 60 | + } |
| 61 | + |
| 62 | +#ifdef LTC_SHA256 |
| 63 | + /*************************************************************************** |
| 64 | + * verify correct generation of a keystream |
| 65 | + */ |
| 66 | + { |
| 67 | + const unsigned char key[] = {0x1b,0x27,0x55,0x64,0x73,0xe9,0x85,0xd4,0x62,0xcd,0x51,0x19,0x7a,0x9a,0x46,0xc7,0x60,0x09,0x54,0x9e,0xac,0x64,0x74,0xf2,0x06,0xc4,0xee,0x08,0x44,0xf6,0x83,0x89}; |
| 68 | + const unsigned char nonce[] = {0x69,0x69,0x6e,0xe9,0x55,0xb6,0x2b,0x73,0xcd,0x62,0xbd,0xa8,0x75,0xfc,0x73,0xd6,0x82,0x19,0xe0,0x03,0x6b,0x7a,0x0b,0x37}; |
| 69 | + const unsigned char expecthash[] = {0x6a,0x60,0x57,0x65,0x27,0xe0,0x00,0x51,0x6d,0xb0,0xda,0x60,0x46,0x20,0xf6,0xd0,0x95,0x65,0x45,0x39,0xf4,0x86,0x83,0x43,0x64,0xdf,0xd9,0x5a,0x6f,0x3f,0xbe,0xb7}; |
| 70 | + int rounds = 20; |
| 71 | + unsigned char keystream[91101]; |
| 72 | + unsigned long keystreamlen = 91101; |
| 73 | + unsigned char hash[32]; |
| 74 | + salsa20_state st; |
| 75 | + int err; |
| 76 | + |
| 77 | + if ((err = xsalsa20_setup(&st, key, 32, nonce, 24, rounds)) != CRYPT_OK) return err; |
| 78 | + if ((err = salsa20_keystream(&st, keystream, keystreamlen)) != CRYPT_OK) return err; |
| 79 | + if ((err = salsa20_done(&st)) != CRYPT_OK) return err; |
| 80 | + if ((err = _sha256(hash, keystream, keystreamlen)) != CRYPT_OK) return err; |
| 81 | + if (compare_testvector(hash, sizeof(hash), expecthash, sizeof(expecthash), "XSALSA20-TV2", 1)) return CRYPT_FAIL_TESTVECTOR; |
| 82 | + } |
| 83 | + |
| 84 | + return CRYPT_OK; |
| 85 | +#endif |
| 86 | + |
| 87 | +#endif |
| 88 | +} |
| 89 | + |
| 90 | +#endif |
| 91 | + |
| 92 | +/* ref: $Format:%D$ */ |
| 93 | +/* git commit: $Format:%H$ */ |
| 94 | +/* commit time: $Format:%ai$ */ |
0 commit comments