twofish_common.c (58e16d792a6a8c6b750f637a4649967fcac853dc) twofish_common.c (674f368a952c48ede71784935a799a5205b92b6c)
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * Common Twofish algorithm parts shared between the c and assembler
4 * implementations
5 *
6 * Originally Twofish for GPG
7 * By Matthew Skala <mskala@ansuz.sooke.bc.ca>, July 26, 1998
8 * 256-bit key length added March 20, 1999

--- 553 unchanged lines hidden (view full) ---

562 x = CALC_K256_2 (k, l, 0); \
563 y = CALC_K256_2 (m, n, 4); \
564 y = rol32(y, 8); \
565 x += y; y += x; ctx->a[j] = x; \
566 ctx->a[(j) + 1] = rol32(y, 9)
567
568/* Perform the key setup. */
569int __twofish_setkey(struct twofish_ctx *ctx, const u8 *key,
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * Common Twofish algorithm parts shared between the c and assembler
4 * implementations
5 *
6 * Originally Twofish for GPG
7 * By Matthew Skala <mskala@ansuz.sooke.bc.ca>, July 26, 1998
8 * 256-bit key length added March 20, 1999

--- 553 unchanged lines hidden (view full) ---

562 x = CALC_K256_2 (k, l, 0); \
563 y = CALC_K256_2 (m, n, 4); \
564 y = rol32(y, 8); \
565 x += y; y += x; ctx->a[j] = x; \
566 ctx->a[(j) + 1] = rol32(y, 9)
567
568/* Perform the key setup. */
569int __twofish_setkey(struct twofish_ctx *ctx, const u8 *key,
570 unsigned int key_len, u32 *flags)
570 unsigned int key_len)
571{
572 int i, j, k;
573
574 /* Temporaries for CALC_K. */
575 u32 x, y;
576
577 /* The S vector used to key the S-boxes, split up into individual bytes.
578 * 128-bit keys use only sa through sh; 256-bit use all of them. */
579 u8 sa = 0, sb = 0, sc = 0, sd = 0, se = 0, sf = 0, sg = 0, sh = 0;
580 u8 si = 0, sj = 0, sk = 0, sl = 0, sm = 0, sn = 0, so = 0, sp = 0;
581
582 /* Temporary for CALC_S. */
583 u8 tmp;
584
585 /* Check key length. */
586 if (key_len % 8)
571{
572 int i, j, k;
573
574 /* Temporaries for CALC_K. */
575 u32 x, y;
576
577 /* The S vector used to key the S-boxes, split up into individual bytes.
578 * 128-bit keys use only sa through sh; 256-bit use all of them. */
579 u8 sa = 0, sb = 0, sc = 0, sd = 0, se = 0, sf = 0, sg = 0, sh = 0;
580 u8 si = 0, sj = 0, sk = 0, sl = 0, sm = 0, sn = 0, so = 0, sp = 0;
581
582 /* Temporary for CALC_S. */
583 u8 tmp;
584
585 /* Check key length. */
586 if (key_len % 8)
587 {
588 *flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
589 return -EINVAL; /* unsupported key length */
587 return -EINVAL; /* unsupported key length */
590 }
591
592 /* Compute the first two words of the S vector. The magic numbers are
593 * the entries of the RS matrix, preprocessed through poly_to_exp. The
594 * numbers in the comments are the original (polynomial form) matrix
595 * entries. */
596 CALC_S (sa, sb, sc, sd, 0, 0x00, 0x2D, 0x01, 0x2D); /* 01 A4 02 A4 */
597 CALC_S (sa, sb, sc, sd, 1, 0x2D, 0xA4, 0x44, 0x8A); /* A4 56 A1 55 */
598 CALC_S (sa, sb, sc, sd, 2, 0x8A, 0xD5, 0xBF, 0xD1); /* 55 82 FC 87 */

--- 84 unchanged lines hidden (view full) ---

683 }
684
685 return 0;
686}
687EXPORT_SYMBOL_GPL(__twofish_setkey);
688
689int twofish_setkey(struct crypto_tfm *tfm, const u8 *key, unsigned int key_len)
690{
588
589 /* Compute the first two words of the S vector. The magic numbers are
590 * the entries of the RS matrix, preprocessed through poly_to_exp. The
591 * numbers in the comments are the original (polynomial form) matrix
592 * entries. */
593 CALC_S (sa, sb, sc, sd, 0, 0x00, 0x2D, 0x01, 0x2D); /* 01 A4 02 A4 */
594 CALC_S (sa, sb, sc, sd, 1, 0x2D, 0xA4, 0x44, 0x8A); /* A4 56 A1 55 */
595 CALC_S (sa, sb, sc, sd, 2, 0x8A, 0xD5, 0xBF, 0xD1); /* 55 82 FC 87 */

--- 84 unchanged lines hidden (view full) ---

680 }
681
682 return 0;
683}
684EXPORT_SYMBOL_GPL(__twofish_setkey);
685
686int twofish_setkey(struct crypto_tfm *tfm, const u8 *key, unsigned int key_len)
687{
691 return __twofish_setkey(crypto_tfm_ctx(tfm), key, key_len,
692 &tfm->crt_flags);
688 return __twofish_setkey(crypto_tfm_ctx(tfm), key, key_len);
693}
694EXPORT_SYMBOL_GPL(twofish_setkey);
695
696MODULE_LICENSE("GPL");
697MODULE_DESCRIPTION("Twofish cipher common functions");
689}
690EXPORT_SYMBOL_GPL(twofish_setkey);
691
692MODULE_LICENSE("GPL");
693MODULE_DESCRIPTION("Twofish cipher common functions");