diff options
author | jsing <> | 2024-03-29 07:26:21 +0000 |
---|---|---|
committer | jsing <> | 2024-03-29 07:26:21 +0000 |
commit | 8a7da17ae004eee2db67b338828600b08d1210a0 (patch) | |
tree | ff469f593171397f5a426901c668618364578550 /src/lib/libcrypto/camellia/camellia.c | |
parent | c91834403d51367d38828dd80256abab0bdb4c7a (diff) | |
download | openbsd-8a7da17ae004eee2db67b338828600b08d1210a0.tar.gz openbsd-8a7da17ae004eee2db67b338828600b08d1210a0.tar.bz2 openbsd-8a7da17ae004eee2db67b338828600b08d1210a0.zip |
Consolidate camellia code.
Diffstat (limited to '')
-rw-r--r-- | src/lib/libcrypto/camellia/camellia.c | 126 |
1 files changed, 123 insertions, 3 deletions
diff --git a/src/lib/libcrypto/camellia/camellia.c b/src/lib/libcrypto/camellia/camellia.c index 336074ad7c..af6b85ff8e 100644 --- a/src/lib/libcrypto/camellia/camellia.c +++ b/src/lib/libcrypto/camellia/camellia.c | |||
@@ -1,4 +1,4 @@ | |||
1 | /* $OpenBSD: camellia.c,v 1.12 2022/11/26 16:08:51 tb Exp $ */ | 1 | /* $OpenBSD: camellia.c,v 1.13 2024/03/29 07:26:21 jsing Exp $ */ |
2 | /* ==================================================================== | 2 | /* ==================================================================== |
3 | * Copyright 2006 NTT (Nippon Telegraph and Telephone Corporation) . | 3 | * Copyright 2006 NTT (Nippon Telegraph and Telephone Corporation) . |
4 | * ALL RIGHTS RESERVED. | 4 | * ALL RIGHTS RESERVED. |
@@ -84,10 +84,25 @@ | |||
84 | 84 | ||
85 | #include <stdlib.h> | 85 | #include <stdlib.h> |
86 | #include <string.h> | 86 | #include <string.h> |
87 | #include <openssl/camellia.h> | 87 | |
88 | #include <openssl/opensslconf.h> | 88 | #include <openssl/opensslconf.h> |
89 | 89 | ||
90 | #include "cmll_local.h" | 90 | #include <openssl/camellia.h> |
91 | #include <openssl/modes.h> | ||
92 | |||
93 | typedef unsigned int u32; | ||
94 | typedef unsigned char u8; | ||
95 | |||
96 | int Camellia_Ekeygen(int keyBitLength, const u8 *rawKey, | ||
97 | KEY_TABLE_TYPE keyTable); | ||
98 | void Camellia_EncryptBlock_Rounds(int grandRounds, const u8 plaintext[], | ||
99 | const KEY_TABLE_TYPE keyTable, u8 ciphertext[]); | ||
100 | void Camellia_DecryptBlock_Rounds(int grandRounds, const u8 ciphertext[], | ||
101 | const KEY_TABLE_TYPE keyTable, u8 plaintext[]); | ||
102 | void Camellia_EncryptBlock(int keyBitLength, const u8 plaintext[], | ||
103 | const KEY_TABLE_TYPE keyTable, u8 ciphertext[]); | ||
104 | void Camellia_DecryptBlock(int keyBitLength, const u8 ciphertext[], | ||
105 | const KEY_TABLE_TYPE keyTable, u8 plaintext[]); | ||
91 | 106 | ||
92 | /* 32-bit rotations */ | 107 | /* 32-bit rotations */ |
93 | #if !defined(OPENSSL_NO_ASM) && !defined(OPENSSL_NO_INLINE_ASM) | 108 | #if !defined(OPENSSL_NO_ASM) && !defined(OPENSSL_NO_INLINE_ASM) |
@@ -564,3 +579,108 @@ Camellia_DecryptBlock(int keyBitLength, const u8 plaintext[], | |||
564 | Camellia_DecryptBlock_Rounds(keyBitLength == 128 ? 3 : 4, | 579 | Camellia_DecryptBlock_Rounds(keyBitLength == 128 ? 3 : 4, |
565 | plaintext, keyTable, ciphertext); | 580 | plaintext, keyTable, ciphertext); |
566 | } | 581 | } |
582 | |||
583 | int | ||
584 | Camellia_set_key(const unsigned char *userKey, const int bits, | ||
585 | CAMELLIA_KEY *key) | ||
586 | { | ||
587 | if (userKey == NULL || key == NULL) | ||
588 | return -1; | ||
589 | if (bits != 128 && bits != 192 && bits != 256) | ||
590 | return -2; | ||
591 | key->grand_rounds = Camellia_Ekeygen(bits, userKey, key->u.rd_key); | ||
592 | return 0; | ||
593 | } | ||
594 | |||
595 | void | ||
596 | Camellia_encrypt(const unsigned char *in, unsigned char *out, | ||
597 | const CAMELLIA_KEY *key) | ||
598 | { | ||
599 | Camellia_EncryptBlock_Rounds(key->grand_rounds, in, key->u.rd_key, out); | ||
600 | } | ||
601 | |||
602 | void | ||
603 | Camellia_decrypt(const unsigned char *in, unsigned char *out, | ||
604 | const CAMELLIA_KEY *key) | ||
605 | { | ||
606 | Camellia_DecryptBlock_Rounds(key->grand_rounds, in, key->u.rd_key, out); | ||
607 | } | ||
608 | |||
609 | void | ||
610 | Camellia_cbc_encrypt(const unsigned char *in, unsigned char *out, size_t len, | ||
611 | const CAMELLIA_KEY *key, unsigned char *ivec, const int enc) | ||
612 | { | ||
613 | if (enc) | ||
614 | CRYPTO_cbc128_encrypt(in, out, len, key, ivec, | ||
615 | (block128_f)Camellia_encrypt); | ||
616 | else | ||
617 | CRYPTO_cbc128_decrypt(in, out, len, key, ivec, | ||
618 | (block128_f)Camellia_decrypt); | ||
619 | } | ||
620 | |||
621 | /* | ||
622 | * The input and output encrypted as though 128bit cfb mode is being | ||
623 | * used. The extra state information to record how much of the | ||
624 | * 128bit block we have used is contained in *num; | ||
625 | */ | ||
626 | |||
627 | void | ||
628 | Camellia_cfb128_encrypt(const unsigned char *in, unsigned char *out, | ||
629 | size_t length, const CAMELLIA_KEY *key, unsigned char *ivec, int *num, | ||
630 | const int enc) | ||
631 | { | ||
632 | CRYPTO_cfb128_encrypt(in, out, length, key, ivec, num, enc, | ||
633 | (block128_f)Camellia_encrypt); | ||
634 | } | ||
635 | |||
636 | /* N.B. This expects the input to be packed, MS bit first */ | ||
637 | void | ||
638 | Camellia_cfb1_encrypt(const unsigned char *in, unsigned char *out, | ||
639 | size_t length, const CAMELLIA_KEY *key, unsigned char *ivec, int *num, | ||
640 | const int enc) | ||
641 | { | ||
642 | CRYPTO_cfb128_1_encrypt(in, out, length, key, ivec, num, enc, | ||
643 | (block128_f)Camellia_encrypt); | ||
644 | } | ||
645 | |||
646 | void | ||
647 | Camellia_cfb8_encrypt(const unsigned char *in, unsigned char *out, | ||
648 | size_t length, const CAMELLIA_KEY *key, unsigned char *ivec, int *num, | ||
649 | const int enc) | ||
650 | { | ||
651 | CRYPTO_cfb128_8_encrypt(in, out, length, key, ivec, num, enc, | ||
652 | (block128_f)Camellia_encrypt); | ||
653 | } | ||
654 | |||
655 | void | ||
656 | Camellia_ctr128_encrypt(const unsigned char *in, unsigned char *out, | ||
657 | size_t length, const CAMELLIA_KEY *key, | ||
658 | unsigned char ivec[CAMELLIA_BLOCK_SIZE], | ||
659 | unsigned char ecount_buf[CAMELLIA_BLOCK_SIZE], unsigned int *num) | ||
660 | { | ||
661 | CRYPTO_ctr128_encrypt(in, out, length, key, ivec, ecount_buf, num, | ||
662 | (block128_f)Camellia_encrypt); | ||
663 | } | ||
664 | |||
665 | void | ||
666 | Camellia_ecb_encrypt(const unsigned char *in, unsigned char *out, | ||
667 | const CAMELLIA_KEY *key, const int enc) | ||
668 | { | ||
669 | if (CAMELLIA_ENCRYPT == enc) | ||
670 | Camellia_encrypt(in, out, key); | ||
671 | else | ||
672 | Camellia_decrypt(in, out, key); | ||
673 | } | ||
674 | |||
675 | /* | ||
676 | * The input and output encrypted as though 128bit ofb mode is being | ||
677 | * used. The extra state information to record how much of the | ||
678 | * 128bit block we have used is contained in *num; | ||
679 | */ | ||
680 | void | ||
681 | Camellia_ofb128_encrypt(const unsigned char *in, unsigned char *out, | ||
682 | size_t length, const CAMELLIA_KEY *key, unsigned char *ivec, int *num) | ||
683 | { | ||
684 | CRYPTO_ofb128_encrypt(in, out, length, key, ivec, num, | ||
685 | (block128_f)Camellia_encrypt); | ||
686 | } | ||