Changeset View
Changeset View
Standalone View
Standalone View
cipher-chachapoly.c
- This file was copied to cipher-chachapoly-libcrypto.c.
/* | /* | ||||
* Copyright (c) 2013 Damien Miller <djm@mindrot.org> | * Copyright (c) 2013 Damien Miller <djm@mindrot.org> | ||||
* | * | ||||
* Permission to use, copy, modify, and distribute this software for any | * Permission to use, copy, modify, and distribute this software for any | ||||
* purpose with or without fee is hereby granted, provided that the above | * purpose with or without fee is hereby granted, provided that the above | ||||
* copyright notice and this permission notice appear in all copies. | * copyright notice and this permission notice appear in all copies. | ||||
* | * | ||||
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | ||||
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | ||||
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | ||||
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | ||||
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | ||||
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | ||||
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||||
*/ | */ | ||||
/* $OpenBSD: cipher-chachapoly.c,v 1.8 2016/08/03 05:41:57 djm Exp $ */ | /* $OpenBSD: cipher-chachapoly.c,v 1.9 2020/04/03 04:27:03 djm Exp $ */ | ||||
#include "includes.h" | #include "includes.h" | ||||
#ifdef WITH_OPENSSL | |||||
#include "openbsd-compat/openssl-compat.h" | |||||
#endif | |||||
#if !defined(HAVE_EVP_CHACHA20) || defined(HAVE_BROKEN_CHACHA20) | |||||
#include <sys/types.h> | #include <sys/types.h> | ||||
#include <stdarg.h> /* needed for log.h */ | #include <stdarg.h> /* needed for log.h */ | ||||
#include <string.h> | #include <string.h> | ||||
#include <stdio.h> /* needed for misc.h */ | #include <stdio.h> /* needed for misc.h */ | ||||
#include "log.h" | #include "log.h" | ||||
#include "sshbuf.h" | #include "sshbuf.h" | ||||
#include "ssherr.h" | #include "ssherr.h" | ||||
#include "cipher-chachapoly.h" | #include "cipher-chachapoly.h" | ||||
int | struct chachapoly_ctx { | ||||
chachapoly_init(struct chachapoly_ctx *ctx, | struct chacha_ctx main_ctx, header_ctx; | ||||
const u_char *key, u_int keylen) | }; | ||||
struct chachapoly_ctx * | |||||
chachapoly_new(const u_char *key, u_int keylen) | |||||
{ | { | ||||
struct chachapoly_ctx *ctx; | |||||
if (keylen != (32 + 32)) /* 2 x 256 bit keys */ | if (keylen != (32 + 32)) /* 2 x 256 bit keys */ | ||||
return SSH_ERR_INVALID_ARGUMENT; | return NULL; | ||||
if ((ctx = calloc(1, sizeof(*ctx))) == NULL) | |||||
return NULL; | |||||
chacha_keysetup(&ctx->main_ctx, key, 256); | chacha_keysetup(&ctx->main_ctx, key, 256); | ||||
chacha_keysetup(&ctx->header_ctx, key + 32, 256); | chacha_keysetup(&ctx->header_ctx, key + 32, 256); | ||||
return 0; | return ctx; | ||||
} | } | ||||
void | |||||
chachapoly_free(struct chachapoly_ctx *cpctx) | |||||
{ | |||||
freezero(cpctx, sizeof(*cpctx)); | |||||
} | |||||
/* | /* | ||||
* chachapoly_crypt() operates as following: | * chachapoly_crypt() operates as following: | ||||
* En/decrypt with header key 'aadlen' bytes from 'src', storing result | * En/decrypt with header key 'aadlen' bytes from 'src', storing result | ||||
* to 'dest'. The ciphertext here is treated as additional authenticated | * to 'dest'. The ciphertext here is treated as additional authenticated | ||||
* data for MAC calculation. | * data for MAC calculation. | ||||
* En/decrypt 'len' bytes at offset 'aadlen' from 'src' to 'dest'. Use | * En/decrypt 'len' bytes at offset 'aadlen' from 'src' to 'dest'. Use | ||||
* POLY1305_TAGLEN bytes at offset 'len'+'aadlen' as the authentication | * POLY1305_TAGLEN bytes at offset 'len'+'aadlen' as the authentication | ||||
* tag. This tag is written on encryption and verified on decryption. | * tag. This tag is written on encryption and verified on decryption. | ||||
▲ Show 20 Lines • Show All 62 Lines • ▼ Show 20 Lines | chachapoly_get_length(struct chachapoly_ctx *ctx, | ||||
if (len < 4) | if (len < 4) | ||||
return SSH_ERR_MESSAGE_INCOMPLETE; | return SSH_ERR_MESSAGE_INCOMPLETE; | ||||
POKE_U64(seqbuf, seqnr); | POKE_U64(seqbuf, seqnr); | ||||
chacha_ivsetup(&ctx->header_ctx, seqbuf, NULL); | chacha_ivsetup(&ctx->header_ctx, seqbuf, NULL); | ||||
chacha_encrypt_bytes(&ctx->header_ctx, cp, buf, 4); | chacha_encrypt_bytes(&ctx->header_ctx, cp, buf, 4); | ||||
*plenp = PEEK_U32(buf); | *plenp = PEEK_U32(buf); | ||||
return 0; | return 0; | ||||
} | } | ||||
#endif /* !defined(HAVE_EVP_CHACHA20) || defined(HAVE_BROKEN_CHACHA20) */ |