2 * AES CTR routines supporting the Power 7+ Nest Accelerators driver
4 * Copyright (C) 2011-2012 International Business Machines Inc.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 only.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 * Author: Kent Yoder <yoder1@us.ibm.com>
22 #include <crypto/aes.h>
23 #include <crypto/ctr.h>
24 #include <crypto/algapi.h>
25 #include <linux/module.h>
26 #include <linux/types.h>
27 #include <linux/crypto.h>
30 #include "nx_csbcpb.h"
34 static int ctr_aes_nx_set_key(struct crypto_tfm
*tfm
,
38 struct nx_crypto_ctx
*nx_ctx
= crypto_tfm_ctx(tfm
);
39 struct nx_csbcpb
*csbcpb
= nx_ctx
->csbcpb
;
41 nx_ctx_init(nx_ctx
, HCOP_FC_AES
);
45 NX_CPB_SET_KEY_SIZE(csbcpb
, NX_KS_AES_128
);
46 nx_ctx
->ap
= &nx_ctx
->props
[NX_PROPS_AES_128
];
49 NX_CPB_SET_KEY_SIZE(csbcpb
, NX_KS_AES_192
);
50 nx_ctx
->ap
= &nx_ctx
->props
[NX_PROPS_AES_192
];
53 NX_CPB_SET_KEY_SIZE(csbcpb
, NX_KS_AES_256
);
54 nx_ctx
->ap
= &nx_ctx
->props
[NX_PROPS_AES_256
];
60 csbcpb
->cpb
.hdr
.mode
= NX_MODE_AES_CTR
;
61 memcpy(csbcpb
->cpb
.aes_ctr
.key
, in_key
, key_len
);
66 static int ctr3686_aes_nx_set_key(struct crypto_tfm
*tfm
,
70 struct nx_crypto_ctx
*nx_ctx
= crypto_tfm_ctx(tfm
);
72 if (key_len
< CTR_RFC3686_NONCE_SIZE
)
75 memcpy(nx_ctx
->priv
.ctr
.nonce
,
76 in_key
+ key_len
- CTR_RFC3686_NONCE_SIZE
,
77 CTR_RFC3686_NONCE_SIZE
);
79 key_len
-= CTR_RFC3686_NONCE_SIZE
;
81 return ctr_aes_nx_set_key(tfm
, in_key
, key_len
);
84 static int ctr_aes_nx_crypt(struct blkcipher_desc
*desc
,
85 struct scatterlist
*dst
,
86 struct scatterlist
*src
,
89 struct nx_crypto_ctx
*nx_ctx
= crypto_blkcipher_ctx(desc
->tfm
);
90 struct nx_csbcpb
*csbcpb
= nx_ctx
->csbcpb
;
91 unsigned long irq_flags
;
92 unsigned int processed
= 0, to_process
;
95 spin_lock_irqsave(&nx_ctx
->lock
, irq_flags
);
98 to_process
= nbytes
- processed
;
100 rc
= nx_build_sg_lists(nx_ctx
, desc
, dst
, src
, &to_process
,
101 processed
, csbcpb
->cpb
.aes_ctr
.iv
);
105 if (!nx_ctx
->op
.inlen
|| !nx_ctx
->op
.outlen
) {
110 rc
= nx_hcall_sync(nx_ctx
, &nx_ctx
->op
,
111 desc
->flags
& CRYPTO_TFM_REQ_MAY_SLEEP
);
115 memcpy(desc
->info
, csbcpb
->cpb
.aes_cbc
.cv
, AES_BLOCK_SIZE
);
117 atomic_inc(&(nx_ctx
->stats
->aes_ops
));
118 atomic64_add(csbcpb
->csb
.processed_byte_count
,
119 &(nx_ctx
->stats
->aes_bytes
));
121 processed
+= to_process
;
122 } while (processed
< nbytes
);
124 spin_unlock_irqrestore(&nx_ctx
->lock
, irq_flags
);
128 static int ctr3686_aes_nx_crypt(struct blkcipher_desc
*desc
,
129 struct scatterlist
*dst
,
130 struct scatterlist
*src
,
133 struct nx_crypto_ctx
*nx_ctx
= crypto_blkcipher_ctx(desc
->tfm
);
136 memcpy(iv
, nx_ctx
->priv
.ctr
.nonce
, CTR_RFC3686_IV_SIZE
);
137 memcpy(iv
+ CTR_RFC3686_NONCE_SIZE
,
138 desc
->info
, CTR_RFC3686_IV_SIZE
);
139 iv
[12] = iv
[13] = iv
[14] = 0;
144 return ctr_aes_nx_crypt(desc
, dst
, src
, nbytes
);
147 struct crypto_alg nx_ctr_aes_alg
= {
148 .cra_name
= "ctr(aes)",
149 .cra_driver_name
= "ctr-aes-nx",
151 .cra_flags
= CRYPTO_ALG_TYPE_BLKCIPHER
,
153 .cra_ctxsize
= sizeof(struct nx_crypto_ctx
),
154 .cra_type
= &crypto_blkcipher_type
,
155 .cra_module
= THIS_MODULE
,
156 .cra_init
= nx_crypto_ctx_aes_ctr_init
,
157 .cra_exit
= nx_crypto_ctx_exit
,
159 .min_keysize
= AES_MIN_KEY_SIZE
,
160 .max_keysize
= AES_MAX_KEY_SIZE
,
161 .ivsize
= AES_BLOCK_SIZE
,
162 .setkey
= ctr_aes_nx_set_key
,
163 .encrypt
= ctr_aes_nx_crypt
,
164 .decrypt
= ctr_aes_nx_crypt
,
168 struct crypto_alg nx_ctr3686_aes_alg
= {
169 .cra_name
= "rfc3686(ctr(aes))",
170 .cra_driver_name
= "rfc3686-ctr-aes-nx",
172 .cra_flags
= CRYPTO_ALG_TYPE_BLKCIPHER
,
174 .cra_ctxsize
= sizeof(struct nx_crypto_ctx
),
175 .cra_type
= &crypto_blkcipher_type
,
176 .cra_module
= THIS_MODULE
,
177 .cra_init
= nx_crypto_ctx_aes_ctr_init
,
178 .cra_exit
= nx_crypto_ctx_exit
,
180 .min_keysize
= AES_MIN_KEY_SIZE
+ CTR_RFC3686_NONCE_SIZE
,
181 .max_keysize
= AES_MAX_KEY_SIZE
+ CTR_RFC3686_NONCE_SIZE
,
182 .ivsize
= CTR_RFC3686_IV_SIZE
,
184 .setkey
= ctr3686_aes_nx_set_key
,
185 .encrypt
= ctr3686_aes_nx_crypt
,
186 .decrypt
= ctr3686_aes_nx_crypt
,