blob: f053abe2e479ab4ab98bf31d18ba051d82fa3fb5 [file] [log] [blame]
Damien Miller9a3d0dc2010-10-07 22:06:42 +11001/* $OpenBSD: cipher-ctr.c,v 1.11 2010/10/01 23:05:32 djm Exp $ */
Damien Millerf5399c22003-05-18 20:53:59 +10002/*
Damien Miller0275b522003-06-18 20:29:35 +10003 * Copyright (c) 2003 Markus Friedl <markus@openbsd.org>
Damien Millerf5399c22003-05-18 20:53:59 +10004 *
Damien Miller0275b522003-06-18 20:29:35 +10005 * 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.
Damien Millerf5399c22003-05-18 20:53:59 +10008 *
Damien Miller0275b522003-06-18 20:29:35 +10009 * 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.
Damien Millerf5399c22003-05-18 20:53:59 +100016 */
17#include "includes.h"
Damien Millerf5399c22003-05-18 20:53:59 +100018
Damien Miller25a02b02012-12-13 08:18:56 +110019#ifndef OPENSSL_HAVE_EVPCTR
Damien Millerd7834352006-08-05 12:39:39 +100020#include <sys/types.h>
21
Damien Millerded319c2006-09-01 15:38:36 +100022#include <stdarg.h>
Damien Millere3476ed2006-07-24 14:13:33 +100023#include <string.h>
24
Damien Millerf5399c22003-05-18 20:53:59 +100025#include <openssl/evp.h>
26
Damien Millerf5399c22003-05-18 20:53:59 +100027#include "xmalloc.h"
Damien Millerd7834352006-08-05 12:39:39 +100028#include "log.h"
Damien Millerf5399c22003-05-18 20:53:59 +100029
Darren Tucker129d0bb2005-12-19 17:40:40 +110030/* compatibility with old or broken OpenSSL versions */
31#include "openbsd-compat/openssl-compat.h"
Damien Miller5c3a5582003-09-23 22:12:38 +100032
Darren Tuckercb520172007-06-14 23:21:32 +100033#ifndef USE_BUILTIN_RIJNDAEL
Damien Millerf5399c22003-05-18 20:53:59 +100034#include <openssl/aes.h>
35#endif
36
37const EVP_CIPHER *evp_aes_128_ctr(void);
Damien Miller9a3d0dc2010-10-07 22:06:42 +110038void ssh_aes_ctr_iv(EVP_CIPHER_CTX *, int, u_char *, size_t);
Damien Millerf5399c22003-05-18 20:53:59 +100039
40struct ssh_aes_ctr_ctx
41{
42 AES_KEY aes_ctx;
43 u_char aes_counter[AES_BLOCK_SIZE];
44};
45
46/*
47 * increment counter 'ctr',
48 * the counter is of size 'len' bytes and stored in network-byte-order.
49 * (LSB at ctr[len-1], MSB at ctr[0])
50 */
51static void
Damien Miller9a3d0dc2010-10-07 22:06:42 +110052ssh_ctr_inc(u_char *ctr, size_t len)
Damien Millerf5399c22003-05-18 20:53:59 +100053{
54 int i;
55
56 for (i = len - 1; i >= 0; i--)
57 if (++ctr[i]) /* continue on overflow */
58 return;
59}
60
61static int
62ssh_aes_ctr(EVP_CIPHER_CTX *ctx, u_char *dest, const u_char *src,
Damien Miller9a3d0dc2010-10-07 22:06:42 +110063 LIBCRYPTO_EVP_INL_TYPE len)
Damien Millerf5399c22003-05-18 20:53:59 +100064{
65 struct ssh_aes_ctr_ctx *c;
Damien Miller9a3d0dc2010-10-07 22:06:42 +110066 size_t n = 0;
Damien Millerf5399c22003-05-18 20:53:59 +100067 u_char buf[AES_BLOCK_SIZE];
68
69 if (len == 0)
70 return (1);
71 if ((c = EVP_CIPHER_CTX_get_app_data(ctx)) == NULL)
72 return (0);
73
74 while ((len--) > 0) {
75 if (n == 0) {
76 AES_encrypt(c->aes_counter, buf, &c->aes_ctx);
77 ssh_ctr_inc(c->aes_counter, AES_BLOCK_SIZE);
78 }
79 *(dest++) = *(src++) ^ buf[n];
80 n = (n + 1) % AES_BLOCK_SIZE;
81 }
82 return (1);
83}
84
85static int
86ssh_aes_ctr_init(EVP_CIPHER_CTX *ctx, const u_char *key, const u_char *iv,
87 int enc)
88{
89 struct ssh_aes_ctr_ctx *c;
90
91 if ((c = EVP_CIPHER_CTX_get_app_data(ctx)) == NULL) {
92 c = xmalloc(sizeof(*c));
93 EVP_CIPHER_CTX_set_app_data(ctx, c);
94 }
95 if (key != NULL)
Darren Tuckerfc57f712004-02-07 10:41:48 +110096 AES_set_encrypt_key(key, EVP_CIPHER_CTX_key_length(ctx) * 8,
Damien Miller0dc1bef2005-07-17 17:22:45 +100097 &c->aes_ctx);
Damien Millerf5399c22003-05-18 20:53:59 +100098 if (iv != NULL)
99 memcpy(c->aes_counter, iv, AES_BLOCK_SIZE);
100 return (1);
101}
102
103static int
104ssh_aes_ctr_cleanup(EVP_CIPHER_CTX *ctx)
105{
106 struct ssh_aes_ctr_ctx *c;
107
108 if ((c = EVP_CIPHER_CTX_get_app_data(ctx)) != NULL) {
109 memset(c, 0, sizeof(*c));
110 xfree(c);
111 EVP_CIPHER_CTX_set_app_data(ctx, NULL);
112 }
113 return (1);
114}
115
116void
Damien Miller9a3d0dc2010-10-07 22:06:42 +1100117ssh_aes_ctr_iv(EVP_CIPHER_CTX *evp, int doset, u_char * iv, size_t len)
Damien Millerf5399c22003-05-18 20:53:59 +1000118{
119 struct ssh_aes_ctr_ctx *c;
120
121 if ((c = EVP_CIPHER_CTX_get_app_data(evp)) == NULL)
122 fatal("ssh_aes_ctr_iv: no context");
123 if (doset)
124 memcpy(c->aes_counter, iv, len);
125 else
126 memcpy(iv, c->aes_counter, len);
127}
128
129const EVP_CIPHER *
130evp_aes_128_ctr(void)
131{
132 static EVP_CIPHER aes_ctr;
133
134 memset(&aes_ctr, 0, sizeof(EVP_CIPHER));
135 aes_ctr.nid = NID_undef;
136 aes_ctr.block_size = AES_BLOCK_SIZE;
137 aes_ctr.iv_len = AES_BLOCK_SIZE;
138 aes_ctr.key_len = 16;
139 aes_ctr.init = ssh_aes_ctr_init;
140 aes_ctr.cleanup = ssh_aes_ctr_cleanup;
141 aes_ctr.do_cipher = ssh_aes_ctr;
Damien Miller5c3a5582003-09-23 22:12:38 +1000142#ifndef SSH_OLD_EVP
Damien Millerf5399c22003-05-18 20:53:59 +1000143 aes_ctr.flags = EVP_CIPH_CBC_MODE | EVP_CIPH_VARIABLE_LENGTH |
144 EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_CUSTOM_IV;
Damien Miller5c3a5582003-09-23 22:12:38 +1000145#endif
Damien Millerf5399c22003-05-18 20:53:59 +1000146 return (&aes_ctr);
147}
Damien Miller25a02b02012-12-13 08:18:56 +1100148
149#endif /* OPENSSL_HAVE_EVPCTR */