18d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt/*
28d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * Crypto wrapper for internal crypto implementation - Cipher wrappers
38d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * Copyright (c) 2006-2009, Jouni Malinen <j@w1.fi>
48d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt *
5c5ec7f57ead87efa365800228aa0b09a12d9e6c4Dmitry Shmidt * This software may be distributed under the terms of the BSD license.
6c5ec7f57ead87efa365800228aa0b09a12d9e6c4Dmitry Shmidt * See README for more details.
78d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt */
88d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
98d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#include "includes.h"
108d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
118d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#include "common.h"
128d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#include "crypto.h"
138d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#include "aes.h"
148d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#include "des_i.h"
158d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
168d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
178d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstruct crypto_cipher {
188d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	enum crypto_cipher_alg alg;
198d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	union {
208d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		struct {
218d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			size_t used_bytes;
228d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			u8 key[16];
238d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			size_t keylen;
248d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		} rc4;
258d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		struct {
268d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			u8 cbc[32];
278d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			void *ctx_enc;
288d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			void *ctx_dec;
298d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		} aes;
308d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		struct {
318d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			struct des3_key_s key;
328d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			u8 cbc[8];
338d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		} des3;
348d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		struct {
358d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			u32 ek[32];
368d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			u32 dk[32];
378d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			u8 cbc[8];
388d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		} des;
398d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	} u;
408d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt};
418d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
428d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
438d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstruct crypto_cipher * crypto_cipher_init(enum crypto_cipher_alg alg,
448d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt					  const u8 *iv, const u8 *key,
458d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt					  size_t key_len)
468d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
478d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct crypto_cipher *ctx;
488d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
498d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	ctx = os_zalloc(sizeof(*ctx));
508d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (ctx == NULL)
518d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return NULL;
528d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
538d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	ctx->alg = alg;
548d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
558d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	switch (alg) {
568d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	case CRYPTO_CIPHER_ALG_RC4:
578d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		if (key_len > sizeof(ctx->u.rc4.key)) {
588d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			os_free(ctx);
598d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			return NULL;
608d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		}
618d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		ctx->u.rc4.keylen = key_len;
628d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		os_memcpy(ctx->u.rc4.key, key, key_len);
638d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		break;
648d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	case CRYPTO_CIPHER_ALG_AES:
658d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		ctx->u.aes.ctx_enc = aes_encrypt_init(key, key_len);
668d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		if (ctx->u.aes.ctx_enc == NULL) {
678d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			os_free(ctx);
688d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			return NULL;
698d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		}
708d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		ctx->u.aes.ctx_dec = aes_decrypt_init(key, key_len);
718d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		if (ctx->u.aes.ctx_dec == NULL) {
728d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			aes_encrypt_deinit(ctx->u.aes.ctx_enc);
738d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			os_free(ctx);
748d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			return NULL;
758d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		}
769bce59c7fef20e34a05f04d1e33a4076083dca0cDmitry Shmidt		os_memcpy(ctx->u.aes.cbc, iv, AES_BLOCK_SIZE);
778d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		break;
788d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	case CRYPTO_CIPHER_ALG_3DES:
798d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		if (key_len != 24) {
808d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			os_free(ctx);
818d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			return NULL;
828d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		}
838d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		des3_key_setup(key, &ctx->u.des3.key);
848d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		os_memcpy(ctx->u.des3.cbc, iv, 8);
858d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		break;
868d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	case CRYPTO_CIPHER_ALG_DES:
878d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		if (key_len != 8) {
888d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			os_free(ctx);
898d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			return NULL;
908d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		}
918d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		des_key_setup(key, ctx->u.des.ek, ctx->u.des.dk);
928d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		os_memcpy(ctx->u.des.cbc, iv, 8);
938d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		break;
948d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	default:
958d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		os_free(ctx);
968d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return NULL;
978d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
988d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
998d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return ctx;
1008d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
1018d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
1028d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
1038d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtint crypto_cipher_encrypt(struct crypto_cipher *ctx, const u8 *plain,
1048d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			  u8 *crypt, size_t len)
1058d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
1068d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	size_t i, j, blocks;
1078d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
1088d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	switch (ctx->alg) {
1098d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	case CRYPTO_CIPHER_ALG_RC4:
1108d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		if (plain != crypt)
1118d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			os_memcpy(crypt, plain, len);
1128d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		rc4_skip(ctx->u.rc4.key, ctx->u.rc4.keylen,
1138d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			 ctx->u.rc4.used_bytes, crypt, len);
1148d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		ctx->u.rc4.used_bytes += len;
1158d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		break;
1168d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	case CRYPTO_CIPHER_ALG_AES:
1179bce59c7fef20e34a05f04d1e33a4076083dca0cDmitry Shmidt		if (len % AES_BLOCK_SIZE)
1188d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			return -1;
1199bce59c7fef20e34a05f04d1e33a4076083dca0cDmitry Shmidt		blocks = len / AES_BLOCK_SIZE;
1208d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		for (i = 0; i < blocks; i++) {
1219bce59c7fef20e34a05f04d1e33a4076083dca0cDmitry Shmidt			for (j = 0; j < AES_BLOCK_SIZE; j++)
1228d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				ctx->u.aes.cbc[j] ^= plain[j];
1238d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			aes_encrypt(ctx->u.aes.ctx_enc, ctx->u.aes.cbc,
1248d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				    ctx->u.aes.cbc);
1259bce59c7fef20e34a05f04d1e33a4076083dca0cDmitry Shmidt			os_memcpy(crypt, ctx->u.aes.cbc, AES_BLOCK_SIZE);
1269bce59c7fef20e34a05f04d1e33a4076083dca0cDmitry Shmidt			plain += AES_BLOCK_SIZE;
1279bce59c7fef20e34a05f04d1e33a4076083dca0cDmitry Shmidt			crypt += AES_BLOCK_SIZE;
1288d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		}
1298d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		break;
1308d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	case CRYPTO_CIPHER_ALG_3DES:
1318d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		if (len % 8)
1328d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			return -1;
1338d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		blocks = len / 8;
1348d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		for (i = 0; i < blocks; i++) {
1358d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			for (j = 0; j < 8; j++)
1368d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				ctx->u.des3.cbc[j] ^= plain[j];
1378d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			des3_encrypt(ctx->u.des3.cbc, &ctx->u.des3.key,
1388d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				     ctx->u.des3.cbc);
1398d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			os_memcpy(crypt, ctx->u.des3.cbc, 8);
1408d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			plain += 8;
1418d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			crypt += 8;
1428d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		}
1438d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		break;
1448d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	case CRYPTO_CIPHER_ALG_DES:
1458d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		if (len % 8)
1468d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			return -1;
1478d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		blocks = len / 8;
1488d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		for (i = 0; i < blocks; i++) {
1498d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			for (j = 0; j < 8; j++)
1508d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				ctx->u.des3.cbc[j] ^= plain[j];
1518d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			des_block_encrypt(ctx->u.des.cbc, ctx->u.des.ek,
1528d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt					  ctx->u.des.cbc);
1538d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			os_memcpy(crypt, ctx->u.des.cbc, 8);
1548d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			plain += 8;
1558d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			crypt += 8;
1568d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		}
1578d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		break;
1588d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	default:
1598d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return -1;
1608d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
1618d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
1628d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return 0;
1638d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
1648d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
1658d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
1668d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtint crypto_cipher_decrypt(struct crypto_cipher *ctx, const u8 *crypt,
1678d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			  u8 *plain, size_t len)
1688d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
1698d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	size_t i, j, blocks;
1708d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	u8 tmp[32];
1718d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
1728d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	switch (ctx->alg) {
1738d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	case CRYPTO_CIPHER_ALG_RC4:
1748d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		if (plain != crypt)
1758d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			os_memcpy(plain, crypt, len);
1768d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		rc4_skip(ctx->u.rc4.key, ctx->u.rc4.keylen,
1778d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			 ctx->u.rc4.used_bytes, plain, len);
1788d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		ctx->u.rc4.used_bytes += len;
1798d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		break;
1808d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	case CRYPTO_CIPHER_ALG_AES:
1819bce59c7fef20e34a05f04d1e33a4076083dca0cDmitry Shmidt		if (len % AES_BLOCK_SIZE)
1828d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			return -1;
1839bce59c7fef20e34a05f04d1e33a4076083dca0cDmitry Shmidt		blocks = len / AES_BLOCK_SIZE;
1848d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		for (i = 0; i < blocks; i++) {
1859bce59c7fef20e34a05f04d1e33a4076083dca0cDmitry Shmidt			os_memcpy(tmp, crypt, AES_BLOCK_SIZE);
1868d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			aes_decrypt(ctx->u.aes.ctx_dec, crypt, plain);
1879bce59c7fef20e34a05f04d1e33a4076083dca0cDmitry Shmidt			for (j = 0; j < AES_BLOCK_SIZE; j++)
1888d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				plain[j] ^= ctx->u.aes.cbc[j];
1899bce59c7fef20e34a05f04d1e33a4076083dca0cDmitry Shmidt			os_memcpy(ctx->u.aes.cbc, tmp, AES_BLOCK_SIZE);
1909bce59c7fef20e34a05f04d1e33a4076083dca0cDmitry Shmidt			plain += AES_BLOCK_SIZE;
1919bce59c7fef20e34a05f04d1e33a4076083dca0cDmitry Shmidt			crypt += AES_BLOCK_SIZE;
1928d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		}
1938d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		break;
1948d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	case CRYPTO_CIPHER_ALG_3DES:
1958d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		if (len % 8)
1968d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			return -1;
1978d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		blocks = len / 8;
1988d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		for (i = 0; i < blocks; i++) {
1998d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			os_memcpy(tmp, crypt, 8);
2008d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			des3_decrypt(crypt, &ctx->u.des3.key, plain);
2018d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			for (j = 0; j < 8; j++)
2028d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				plain[j] ^= ctx->u.des3.cbc[j];
2038d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			os_memcpy(ctx->u.des3.cbc, tmp, 8);
2048d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			plain += 8;
2058d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			crypt += 8;
2068d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		}
2078d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		break;
2088d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	case CRYPTO_CIPHER_ALG_DES:
2098d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		if (len % 8)
2108d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			return -1;
2118d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		blocks = len / 8;
2128d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		for (i = 0; i < blocks; i++) {
2138d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			os_memcpy(tmp, crypt, 8);
2148d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			des_block_decrypt(crypt, ctx->u.des.dk, plain);
2158d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			for (j = 0; j < 8; j++)
2168d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				plain[j] ^= ctx->u.des.cbc[j];
2178d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			os_memcpy(ctx->u.des.cbc, tmp, 8);
2188d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			plain += 8;
2198d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			crypt += 8;
2208d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		}
2218d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		break;
2228d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	default:
2238d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return -1;
2248d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
2258d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
2268d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return 0;
2278d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
2288d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
2298d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
2308d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtvoid crypto_cipher_deinit(struct crypto_cipher *ctx)
2318d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
2328d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	switch (ctx->alg) {
2338d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	case CRYPTO_CIPHER_ALG_AES:
2348d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		aes_encrypt_deinit(ctx->u.aes.ctx_enc);
2358d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		aes_decrypt_deinit(ctx->u.aes.ctx_dec);
2368d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		break;
2378d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	case CRYPTO_CIPHER_ALG_3DES:
2388d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		break;
2398d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	default:
2408d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		break;
2418d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
2428d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	os_free(ctx);
2438d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
244