diff options
Diffstat (limited to '')
| -rw-r--r-- | src/lib/libcrypto/chacha/chacha.c | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/src/lib/libcrypto/chacha/chacha.c b/src/lib/libcrypto/chacha/chacha.c new file mode 100644 index 0000000000..d76d64de4a --- /dev/null +++ b/src/lib/libcrypto/chacha/chacha.c | |||
| @@ -0,0 +1,38 @@ | |||
| 1 | /* | ||
| 2 | * Copyright (c) 2014 Joel Sing <jsing@openbsd.org> | ||
| 3 | * | ||
| 4 | * Permission to use, copy, modify, and distribute this software for any | ||
| 5 | * purpose with or without fee is hereby granted, provided that the above | ||
| 6 | * copyright notice and this permission notice appear in all copies. | ||
| 7 | * | ||
| 8 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | ||
| 9 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | ||
| 10 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | ||
| 11 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | ||
| 12 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | ||
| 13 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | ||
| 14 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||
| 15 | */ | ||
| 16 | |||
| 17 | #include "chacha-merged.c" | ||
| 18 | |||
| 19 | void | ||
| 20 | CRYPTO_chacha_20(unsigned char *out, const unsigned char *in, size_t len, | ||
| 21 | const unsigned char key[32], const unsigned char iv[8], size_t counter) | ||
| 22 | { | ||
| 23 | struct chacha_ctx ctx; | ||
| 24 | |||
| 25 | /* | ||
| 26 | * chacha_ivsetup expects the counter to be in u8. Rather than | ||
| 27 | * converting size_t to u8 and then back again, pass a counter of | ||
| 28 | * NULL and manually assign it afterwards. | ||
| 29 | */ | ||
| 30 | chacha_keysetup(&ctx, key, 256); | ||
| 31 | chacha_ivsetup(&ctx, iv, NULL); | ||
| 32 | if (counter != 0) { | ||
| 33 | ctx.input[12] = (uint32_t)counter; | ||
| 34 | ctx.input[13] = (uint32_t)(((uint64_t)counter) >> 32); | ||
| 35 | } | ||
| 36 | |||
| 37 | chacha_encrypt_bytes(&ctx, in, out, (uint32_t)len); | ||
| 38 | } | ||
