2 * Crypto wrapper for internal crypto implementation - Cipher wrappers
3 * Copyright (c) 2006-2009, Jouni Malinen <j@w1.fi>
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
17 struct crypto_cipher {
18 enum crypto_cipher_alg alg;
31 struct des3_key_s key;
43 struct crypto_cipher * crypto_cipher_init(enum crypto_cipher_alg alg,
44 const u8 *iv, const u8 *key,
47 struct crypto_cipher *ctx;
49 ctx = os_zalloc(sizeof(*ctx));
56 case CRYPTO_CIPHER_ALG_RC4:
57 if (key_len > sizeof(ctx->u.rc4.key)) {
61 ctx->u.rc4.keylen = key_len;
62 os_memcpy(ctx->u.rc4.key, key, key_len);
64 case CRYPTO_CIPHER_ALG_AES:
65 ctx->u.aes.ctx_enc = aes_encrypt_init(key, key_len);
66 if (ctx->u.aes.ctx_enc == NULL) {
70 ctx->u.aes.ctx_dec = aes_decrypt_init(key, key_len);
71 if (ctx->u.aes.ctx_dec == NULL) {
72 aes_encrypt_deinit(ctx->u.aes.ctx_enc);
76 os_memcpy(ctx->u.aes.cbc, iv, AES_BLOCK_SIZE);
78 case CRYPTO_CIPHER_ALG_3DES:
83 des3_key_setup(key, &ctx->u.des3.key);
84 os_memcpy(ctx->u.des3.cbc, iv, 8);
86 case CRYPTO_CIPHER_ALG_DES:
91 des_key_setup(key, ctx->u.des.ek, ctx->u.des.dk);
92 os_memcpy(ctx->u.des.cbc, iv, 8);
103 int crypto_cipher_encrypt(struct crypto_cipher *ctx, const u8 *plain,
104 u8 *crypt, size_t len)
109 case CRYPTO_CIPHER_ALG_RC4:
111 os_memcpy(crypt, plain, len);
112 rc4_skip(ctx->u.rc4.key, ctx->u.rc4.keylen,
113 ctx->u.rc4.used_bytes, crypt, len);
114 ctx->u.rc4.used_bytes += len;
116 case CRYPTO_CIPHER_ALG_AES:
117 if (len % AES_BLOCK_SIZE)
119 blocks = len / AES_BLOCK_SIZE;
120 for (i = 0; i < blocks; i++) {
121 for (j = 0; j < AES_BLOCK_SIZE; j++)
122 ctx->u.aes.cbc[j] ^= plain[j];
123 aes_encrypt(ctx->u.aes.ctx_enc, ctx->u.aes.cbc,
125 os_memcpy(crypt, ctx->u.aes.cbc, AES_BLOCK_SIZE);
126 plain += AES_BLOCK_SIZE;
127 crypt += AES_BLOCK_SIZE;
130 case CRYPTO_CIPHER_ALG_3DES:
134 for (i = 0; i < blocks; i++) {
135 for (j = 0; j < 8; j++)
136 ctx->u.des3.cbc[j] ^= plain[j];
137 des3_encrypt(ctx->u.des3.cbc, &ctx->u.des3.key,
139 os_memcpy(crypt, ctx->u.des3.cbc, 8);
144 case CRYPTO_CIPHER_ALG_DES:
148 for (i = 0; i < blocks; i++) {
149 for (j = 0; j < 8; j++)
150 ctx->u.des3.cbc[j] ^= plain[j];
151 des_block_encrypt(ctx->u.des.cbc, ctx->u.des.ek,
153 os_memcpy(crypt, ctx->u.des.cbc, 8);
166 int crypto_cipher_decrypt(struct crypto_cipher *ctx, const u8 *crypt,
167 u8 *plain, size_t len)
173 case CRYPTO_CIPHER_ALG_RC4:
175 os_memcpy(plain, crypt, len);
176 rc4_skip(ctx->u.rc4.key, ctx->u.rc4.keylen,
177 ctx->u.rc4.used_bytes, plain, len);
178 ctx->u.rc4.used_bytes += len;
180 case CRYPTO_CIPHER_ALG_AES:
181 if (len % AES_BLOCK_SIZE)
183 blocks = len / AES_BLOCK_SIZE;
184 for (i = 0; i < blocks; i++) {
185 os_memcpy(tmp, crypt, AES_BLOCK_SIZE);
186 aes_decrypt(ctx->u.aes.ctx_dec, crypt, plain);
187 for (j = 0; j < AES_BLOCK_SIZE; j++)
188 plain[j] ^= ctx->u.aes.cbc[j];
189 os_memcpy(ctx->u.aes.cbc, tmp, AES_BLOCK_SIZE);
190 plain += AES_BLOCK_SIZE;
191 crypt += AES_BLOCK_SIZE;
194 case CRYPTO_CIPHER_ALG_3DES:
198 for (i = 0; i < blocks; i++) {
199 os_memcpy(tmp, crypt, 8);
200 des3_decrypt(crypt, &ctx->u.des3.key, plain);
201 for (j = 0; j < 8; j++)
202 plain[j] ^= ctx->u.des3.cbc[j];
203 os_memcpy(ctx->u.des3.cbc, tmp, 8);
208 case CRYPTO_CIPHER_ALG_DES:
212 for (i = 0; i < blocks; i++) {
213 os_memcpy(tmp, crypt, 8);
214 des_block_decrypt(crypt, ctx->u.des.dk, plain);
215 for (j = 0; j < 8; j++)
216 plain[j] ^= ctx->u.des.cbc[j];
217 os_memcpy(ctx->u.des.cbc, tmp, 8);
230 void crypto_cipher_deinit(struct crypto_cipher *ctx)
233 case CRYPTO_CIPHER_ALG_AES:
234 aes_encrypt_deinit(ctx->u.aes.ctx_enc);
235 aes_decrypt_deinit(ctx->u.aes.ctx_dec);
237 case CRYPTO_CIPHER_ALG_3DES: