2 * Copyright (c) 2003 Markus Friedl <markus@openbsd.org>
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.
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.
17 RCSID("$OpenBSD: cipher-ctr.c,v 1.4 2004/02/06 23:41:13 dtucker Exp $");
19 #include <openssl/evp.h>
24 #if OPENSSL_VERSION_NUMBER < 0x00906000L
28 #if OPENSSL_VERSION_NUMBER < 0x00907000L
30 #define AES_KEY rijndael_ctx
31 #define AES_BLOCK_SIZE 16
32 #define AES_encrypt(a, b, c) rijndael_encrypt(c, a, b)
33 #define AES_set_encrypt_key(a, b, c) rijndael_set_key(c, (char *)a, b, 1)
35 #include <openssl/aes.h>
38 const EVP_CIPHER *evp_aes_128_ctr(void);
39 void ssh_aes_ctr_iv(EVP_CIPHER_CTX *, int, u_char *, u_int);
41 struct ssh_aes_ctr_ctx
44 u_char aes_counter[AES_BLOCK_SIZE];
48 * increment counter 'ctr',
49 * the counter is of size 'len' bytes and stored in network-byte-order.
50 * (LSB at ctr[len-1], MSB at ctr[0])
53 ssh_ctr_inc(u_char *ctr, u_int len)
57 for (i = len - 1; i >= 0; i--)
58 if (++ctr[i]) /* continue on overflow */
63 ssh_aes_ctr(EVP_CIPHER_CTX *ctx, u_char *dest, const u_char *src,
66 struct ssh_aes_ctr_ctx *c;
68 u_char buf[AES_BLOCK_SIZE];
72 if ((c = EVP_CIPHER_CTX_get_app_data(ctx)) == NULL)
77 AES_encrypt(c->aes_counter, buf, &c->aes_ctx);
78 ssh_ctr_inc(c->aes_counter, AES_BLOCK_SIZE);
80 *(dest++) = *(src++) ^ buf[n];
81 n = (n + 1) % AES_BLOCK_SIZE;
87 ssh_aes_ctr_init(EVP_CIPHER_CTX *ctx, const u_char *key, const u_char *iv,
90 struct ssh_aes_ctr_ctx *c;
92 if ((c = EVP_CIPHER_CTX_get_app_data(ctx)) == NULL) {
93 c = xmalloc(sizeof(*c));
94 EVP_CIPHER_CTX_set_app_data(ctx, c);
97 AES_set_encrypt_key(key, EVP_CIPHER_CTX_key_length(ctx) * 8,
100 memcpy(c->aes_counter, iv, AES_BLOCK_SIZE);
105 ssh_aes_ctr_cleanup(EVP_CIPHER_CTX *ctx)
107 struct ssh_aes_ctr_ctx *c;
109 if ((c = EVP_CIPHER_CTX_get_app_data(ctx)) != NULL) {
110 memset(c, 0, sizeof(*c));
112 EVP_CIPHER_CTX_set_app_data(ctx, NULL);
118 ssh_aes_ctr_iv(EVP_CIPHER_CTX *evp, int doset, u_char * iv, u_int len)
120 struct ssh_aes_ctr_ctx *c;
122 if ((c = EVP_CIPHER_CTX_get_app_data(evp)) == NULL)
123 fatal("ssh_aes_ctr_iv: no context");
125 memcpy(c->aes_counter, iv, len);
127 memcpy(iv, c->aes_counter, len);
131 evp_aes_128_ctr(void)
133 static EVP_CIPHER aes_ctr;
135 memset(&aes_ctr, 0, sizeof(EVP_CIPHER));
136 aes_ctr.nid = NID_undef;
137 aes_ctr.block_size = AES_BLOCK_SIZE;
138 aes_ctr.iv_len = AES_BLOCK_SIZE;
139 aes_ctr.key_len = 16;
140 aes_ctr.init = ssh_aes_ctr_init;
141 aes_ctr.cleanup = ssh_aes_ctr_cleanup;
142 aes_ctr.do_cipher = ssh_aes_ctr;
144 aes_ctr.flags = EVP_CIPH_CBC_MODE | EVP_CIPH_VARIABLE_LENGTH |
145 EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_CUSTOM_IV;