1/* $OpenBSD: cipher-ctr.c,v 1.11 2010/10/01 23:05:32 djm Exp $ */
2/*
3 * Copyright (c) 2003 Markus Friedl <markus@openbsd.org>
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
17#include "includes.h"
18
19#ifndef OPENSSL_HAVE_EVPCTR
20#include <sys/types.h>
21
22#include <stdarg.h>
23#include <string.h>
24
25#ifdef __APPLE_CRYPTO__
26#include "ossl-evp.h"
27#else
28#include <openssl/evp.h>
29#endif
30
31#include "xmalloc.h"
32#include "log.h"
33
34/* compatibility with old or broken OpenSSL versions */
35#include "openbsd-compat/openssl-compat.h"
36
37#ifndef USE_BUILTIN_RIJNDAEL
38#ifdef __APPLE_CRYPTO__
39#include "ossl-aes.h"
40#else
41#include <openssl/aes.h>
42#endif /* __APPLE_CRYPTO__ */
43#endif
44
45struct ssh_aes_ctr_ctx
46{
47	AES_KEY		aes_ctx;
48	u_char		aes_counter[AES_BLOCK_SIZE];
49};
50
51/*
52 * increment counter 'ctr',
53 * the counter is of size 'len' bytes and stored in network-byte-order.
54 * (LSB at ctr[len-1], MSB at ctr[0])
55 */
56static void
57ssh_ctr_inc(u_char *ctr, size_t len)
58{
59	int i;
60
61	for (i = len - 1; i >= 0; i--)
62		if (++ctr[i])	/* continue on overflow */
63			return;
64}
65
66static int
67ssh_aes_ctr(EVP_CIPHER_CTX *ctx, u_char *dest, const u_char *src,
68    LIBCRYPTO_EVP_INL_TYPE len)
69{
70	struct ssh_aes_ctr_ctx *c;
71	size_t n = 0;
72	u_char buf[AES_BLOCK_SIZE];
73
74	if (len == 0)
75		return (1);
76	if ((c = EVP_CIPHER_CTX_get_app_data(ctx)) == NULL)
77		return (0);
78
79	while ((len--) > 0) {
80		if (n == 0) {
81			AES_encrypt(c->aes_counter, buf, &c->aes_ctx);
82			ssh_ctr_inc(c->aes_counter, AES_BLOCK_SIZE);
83		}
84		*(dest++) = *(src++) ^ buf[n];
85		n = (n + 1) % AES_BLOCK_SIZE;
86	}
87	return (1);
88}
89
90static int
91ssh_aes_ctr_init(EVP_CIPHER_CTX *ctx, const u_char *key, const u_char *iv,
92    int enc)
93{
94	struct ssh_aes_ctr_ctx *c;
95
96	if ((c = EVP_CIPHER_CTX_get_app_data(ctx)) == NULL) {
97		c = xmalloc(sizeof(*c));
98		EVP_CIPHER_CTX_set_app_data(ctx, c);
99	}
100	if (key != NULL)
101		AES_set_encrypt_key(key, EVP_CIPHER_CTX_key_length(ctx) * 8,
102		    &c->aes_ctx);
103	if (iv != NULL)
104		memcpy(c->aes_counter, iv, AES_BLOCK_SIZE);
105	return (1);
106}
107
108static int
109ssh_aes_ctr_cleanup(EVP_CIPHER_CTX *ctx)
110{
111	struct ssh_aes_ctr_ctx *c;
112
113	if ((c = EVP_CIPHER_CTX_get_app_data(ctx)) != NULL) {
114#ifdef __APPLE_CRYPTO__
115		AES_destroy_ctx(&c->aes_ctx);
116#endif
117		memset(c, 0, sizeof(*c));
118		xfree(c);
119		EVP_CIPHER_CTX_set_app_data(ctx, NULL);
120	}
121	return (1);
122}
123
124void
125ssh_aes_ctr_iv(EVP_CIPHER_CTX *evp, int doset, u_char * iv, size_t len)
126{
127	struct ssh_aes_ctr_ctx *c;
128
129	if ((c = EVP_CIPHER_CTX_get_app_data(evp)) == NULL)
130		fatal("ssh_aes_ctr_iv: no context");
131	if (doset)
132		memcpy(c->aes_counter, iv, len);
133	else
134		memcpy(iv, c->aes_counter, len);
135}
136
137const EVP_CIPHER *
138evp_aes_128_ctr(void)
139{
140	static EVP_CIPHER aes_ctr;
141
142	memset(&aes_ctr, 0, sizeof(EVP_CIPHER));
143	aes_ctr.nid = NID_undef;
144	aes_ctr.block_size = AES_BLOCK_SIZE;
145	aes_ctr.iv_len = AES_BLOCK_SIZE;
146	aes_ctr.key_len = 16;
147	aes_ctr.init = ssh_aes_ctr_init;
148	aes_ctr.cleanup = ssh_aes_ctr_cleanup;
149	aes_ctr.do_cipher = ssh_aes_ctr;
150#ifndef SSH_OLD_EVP
151	aes_ctr.flags = EVP_CIPH_CBC_MODE | EVP_CIPH_VARIABLE_LENGTH |
152	    EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_CUSTOM_IV;
153#endif
154	return (&aes_ctr);
155}
156
157#endif /* OPENSSL_HAVE_EVPCTR */
158