Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 
0003 #ifndef _CRYPTO_INTERNAL_CHACHA_H
0004 #define _CRYPTO_INTERNAL_CHACHA_H
0005 
0006 #include <crypto/chacha.h>
0007 #include <crypto/internal/skcipher.h>
0008 #include <linux/crypto.h>
0009 
0010 struct chacha_ctx {
0011     u32 key[8];
0012     int nrounds;
0013 };
0014 
0015 static inline int chacha_setkey(struct crypto_skcipher *tfm, const u8 *key,
0016                 unsigned int keysize, int nrounds)
0017 {
0018     struct chacha_ctx *ctx = crypto_skcipher_ctx(tfm);
0019     int i;
0020 
0021     if (keysize != CHACHA_KEY_SIZE)
0022         return -EINVAL;
0023 
0024     for (i = 0; i < ARRAY_SIZE(ctx->key); i++)
0025         ctx->key[i] = get_unaligned_le32(key + i * sizeof(u32));
0026 
0027     ctx->nrounds = nrounds;
0028     return 0;
0029 }
0030 
0031 static inline int chacha20_setkey(struct crypto_skcipher *tfm, const u8 *key,
0032                   unsigned int keysize)
0033 {
0034     return chacha_setkey(tfm, key, keysize, 20);
0035 }
0036 
0037 static inline int chacha12_setkey(struct crypto_skcipher *tfm, const u8 *key,
0038                   unsigned int keysize)
0039 {
0040     return chacha_setkey(tfm, key, keysize, 12);
0041 }
0042 
0043 #endif /* _CRYPTO_CHACHA_H */