diff options
-rw-r--r-- | src/lib/libc/crypt/blowfish.3 | 28 | ||||
-rw-r--r-- | src/lib/libc/crypt/blowfish.c | 140 |
2 files changed, 163 insertions, 5 deletions
diff --git a/src/lib/libc/crypt/blowfish.3 b/src/lib/libc/crypt/blowfish.3 index 0b45790658..02a1ef8738 100644 --- a/src/lib/libc/crypt/blowfish.3 +++ b/src/lib/libc/crypt/blowfish.3 | |||
@@ -1,4 +1,4 @@ | |||
1 | .\" $OpenBSD: blowfish.3,v 1.1 1997/02/16 20:58:16 provos Exp $ | 1 | .\" $OpenBSD: blowfish.3,v 1.2 1998/08/10 18:40:58 provos Exp $ |
2 | .\" Copyright 1997 Niels Provos <provos@physnet.uni-hamburg.de> | 2 | .\" Copyright 1997 Niels Provos <provos@physnet.uni-hamburg.de> |
3 | .\" All rights reserved. | 3 | .\" All rights reserved. |
4 | .\" | 4 | .\" |
@@ -45,6 +45,14 @@ | |||
45 | .Fn blf_enc "blf_ctx *state" "u_int32_t *data" "u_int16_t datalen" | 45 | .Fn blf_enc "blf_ctx *state" "u_int32_t *data" "u_int16_t datalen" |
46 | .Ft void | 46 | .Ft void |
47 | .Fn blf_dec "blf_ctx *state" "u_int32_t *data" "u_int16_t datalen" | 47 | .Fn blf_dec "blf_ctx *state" "u_int32_t *data" "u_int16_t datalen" |
48 | .Ft void | ||
49 | .Fn blf_ecb_encrypt "blf_ctx *state" "u_int8_t *data" "u_int32_t datalen" | ||
50 | .Ft void | ||
51 | .Fn blf_ecb_decrypt "blf_ctx *state" "u_int8_t *data" "u_int32_t datalen" | ||
52 | .Ft void | ||
53 | .Fn blf_cbc_encrypt "blf_ctx *state" "u_int8_t *iv" "u_int8_t *data" "u_int32_t datalen" | ||
54 | .Ft void | ||
55 | .Fn blf_cbc_decrypt "blf_ctx *state" "u_int8_t *iv" "u_int8_t *data" "u_int32_t datalen" | ||
48 | .Sh DESCRIPTION | 56 | .Sh DESCRIPTION |
49 | .Pa Blowfish | 57 | .Pa Blowfish |
50 | is a fast unpatented block cipher designed by Bruce Schneier. | 58 | is a fast unpatented block cipher designed by Bruce Schneier. |
@@ -59,12 +67,25 @@ The first argument to | |||
59 | .Fn blf_enc | 67 | .Fn blf_enc |
60 | is the initalized state derived from | 68 | is the initalized state derived from |
61 | .Fn blf_key . | 69 | .Fn blf_key . |
62 | The stream of data is encrypted in Electronic Cookbook Mode (ECB) and | 70 | The stream of 32-bit words is encrypted in Electronic Codebook |
71 | Mode (ECB) and | ||
63 | .Pa datalen | 72 | .Pa datalen |
64 | must be even. | 73 | must be even. |
65 | .Fn blf_dec | 74 | .Fn blf_dec |
66 | is used for decrypting Blowfish encrypted blocks. | 75 | is used for decrypting Blowfish encrypted blocks. |
67 | .Pp | 76 | .Pp |
77 | The functions | ||
78 | .Fn blf_ecb_encrypt | ||
79 | and | ||
80 | .Fn blf_ecb_decrypt | ||
81 | are used for encrypting and decrypting octet streams in ECB mode. | ||
82 | The functions | ||
83 | .Fn blf_cbc_encrypt | ||
84 | and | ||
85 | .Fn blf_cbc_decrypt | ||
86 | are used for encrypting and decrypting octet streams in | ||
87 | Cipherblock Chaining Mode (CBC). | ||
88 | .Pp | ||
68 | The functions | 89 | The functions |
69 | .Fn Blowfish_initstate , | 90 | .Fn Blowfish_initstate , |
70 | .Fn Blowfish_expand0state , | 91 | .Fn Blowfish_expand0state , |
@@ -74,8 +95,7 @@ and | |||
74 | .Fn Blowfish_decipher | 95 | .Fn Blowfish_decipher |
75 | are used for customization of the | 96 | are used for customization of the |
76 | .Pa Blowfish | 97 | .Pa Blowfish |
77 | cipher, i.e. for the blowfish password hashing function or for | 98 | cipher, e.g. for the blowfish password hashing function. |
78 | implementation of Cipher Block Chaining Mode (CBC). | ||
79 | .Sh SEE ALSO | 99 | .Sh SEE ALSO |
80 | .Xr crypt 3 , | 100 | .Xr crypt 3 , |
81 | .Xr passwd 1 , | 101 | .Xr passwd 1 , |
diff --git a/src/lib/libc/crypt/blowfish.c b/src/lib/libc/crypt/blowfish.c index 5ffc634efa..d946655624 100644 --- a/src/lib/libc/crypt/blowfish.c +++ b/src/lib/libc/crypt/blowfish.c | |||
@@ -1,4 +1,4 @@ | |||
1 | /* $OpenBSD: blowfish.c,v 1.8 1998/03/04 00:34:17 deraadt Exp $ */ | 1 | /* $OpenBSD: blowfish.c,v 1.9 1998/08/10 18:40:59 provos Exp $ */ |
2 | /* | 2 | /* |
3 | * Blowfish block cipher for OpenBSD | 3 | * Blowfish block cipher for OpenBSD |
4 | * Copyright 1997 Niels Provos <provos@physnet.uni-hamburg.de> | 4 | * Copyright 1997 Niels Provos <provos@physnet.uni-hamburg.de> |
@@ -584,6 +584,144 @@ blf_dec(c, data, blocks) | |||
584 | d += 2; | 584 | d += 2; |
585 | } | 585 | } |
586 | } | 586 | } |
587 | |||
588 | /* Repeating operations for little endian machines */ | ||
589 | |||
590 | #define BLF_BLK_ENC l = ntohl (*(u_int32_t *)data); \ | ||
591 | r = ntohl (*(u_int32_t *)(data+4)); \ | ||
592 | Blowfish_encipher(c, &l, &r); \ | ||
593 | *(u_int32_t *)data = htonl (l); \ | ||
594 | *(u_int32_t *)(data + 4) = htonl (r); | ||
595 | |||
596 | #define BLF_BLK_DEC l = ntohl (*(u_int32_t *)data); \ | ||
597 | r = ntohl (*(u_int32_t *)(data+4)); \ | ||
598 | Blowfish_decipher(c, &l, &r); \ | ||
599 | *(u_int32_t *)data = htonl (l); \ | ||
600 | *(u_int32_t *)(data + 4) = htonl (r); | ||
601 | |||
602 | |||
603 | #if __STDC__ | ||
604 | void | ||
605 | blf_ecb_encrypt(blf_ctx *c, u_int8_t *data, u_int32_t len) | ||
606 | #else | ||
607 | void | ||
608 | blf_ecb_encrypt(c, data, len) | ||
609 | blf_ctx *c; | ||
610 | u_int8_t *data; | ||
611 | u_int32_t len; | ||
612 | #endif | ||
613 | { | ||
614 | #if BYTE_ORDER == LITTLE_ENDIAN | ||
615 | u_int32_t l, r; | ||
616 | #endif | ||
617 | u_int32_t i; | ||
618 | |||
619 | for (i = 0; i < len; i += 8) { | ||
620 | #if BYTE_ORDER == LITTLE_ENDIAN | ||
621 | BLF_BLK_ENC; | ||
622 | #else | ||
623 | Blowfish_encipher(c, data, data + 4); | ||
624 | #endif | ||
625 | data += 8; | ||
626 | } | ||
627 | } | ||
628 | |||
629 | #if __STDC__ | ||
630 | void | ||
631 | blf_ecb_decrypt(blf_ctx *c, u_int8_t *data, u_int32_t len) | ||
632 | #else | ||
633 | void | ||
634 | blf_ecb_decrypt(c, data, len) | ||
635 | blf_ctx *c; | ||
636 | u_int8_t *data; | ||
637 | u_int32_t len; | ||
638 | #endif | ||
639 | { | ||
640 | #if BYTE_ORDER == LITTLE_ENDIAN | ||
641 | u_int32_t l, r; | ||
642 | #endif | ||
643 | u_int32_t i; | ||
644 | |||
645 | for (i = 0; i < len; i += 8) { | ||
646 | #if BYTE_ORDER == LITTLE_ENDIAN | ||
647 | BLF_BLK_DEC; | ||
648 | #else | ||
649 | Blowfish_decipher(c, data, data + 4); | ||
650 | #endif | ||
651 | data += 8; | ||
652 | } | ||
653 | } | ||
654 | |||
655 | #if __STDC__ | ||
656 | void | ||
657 | blf_cbc_encrypt(blf_ctx *c, u_int8_t *iv, u_int8_t *data, u_int32_t len) | ||
658 | #else | ||
659 | void | ||
660 | blf_cbc_encrypt(c, iv, data, len) | ||
661 | blf_ctx *c; | ||
662 | u_int8_t *iv; | ||
663 | u_int8_t *data; | ||
664 | u_int32_t len; | ||
665 | #endif | ||
666 | { | ||
667 | #if BYTE_ORDER == LITTLE_ENDIAN | ||
668 | u_int32_t l, r; | ||
669 | #endif | ||
670 | u_int32_t i; | ||
671 | |||
672 | for (i = 0; i < len; i += 8) { | ||
673 | *(u_int32_t *)data ^= *(u_int32_t *)iv; | ||
674 | *(u_int32_t *)(data + 4) ^= *(u_int32_t *)(iv + 4); | ||
675 | #if BYTE_ORDER == LITTLE_ENDIAN | ||
676 | BLF_BLK_ENC; | ||
677 | #else | ||
678 | Blowfish_encipher(c, data, data + 4); | ||
679 | #endif | ||
680 | iv = data; | ||
681 | data += 8; | ||
682 | } | ||
683 | } | ||
684 | |||
685 | #if __STDC__ | ||
686 | void | ||
687 | blf_cbc_decrypt(blf_ctx *c, u_int8_t *iva, u_int8_t *data, u_int32_t len) | ||
688 | #else | ||
689 | void | ||
690 | blf_cbc_decrypt(c, iva, data, len) | ||
691 | blf_ctx *c; | ||
692 | u_int8_t *iva; | ||
693 | u_int8_t *data; | ||
694 | u_int32_t len; | ||
695 | #endif | ||
696 | { | ||
697 | #if BYTE_ORDER == LITTLE_ENDIAN | ||
698 | u_int32_t l, r; | ||
699 | #endif | ||
700 | u_int8_t *iv; | ||
701 | u_int32_t i; | ||
702 | |||
703 | iv = data + len - 16; | ||
704 | data = data + len - 8; | ||
705 | for (i = len - 8; i >= 8; i -= 8) { | ||
706 | #if BYTE_ORDER == LITTLE_ENDIAN | ||
707 | BLF_BLK_DEC; | ||
708 | #else | ||
709 | Blowfish_decipher(c, data, data + 4); | ||
710 | #endif | ||
711 | *(u_int32_t *)data ^= *(u_int32_t *)iv; | ||
712 | *(u_int32_t *)(data + 4) ^= *(u_int32_t *)(iv + 4); | ||
713 | iv = data; | ||
714 | data -= 8; | ||
715 | } | ||
716 | #if BYTE_ORDER == LITTLE_ENDIAN | ||
717 | BLF_BLK_DEC; | ||
718 | #else | ||
719 | Blowfish_decipher(c, data, data + 4); | ||
720 | #endif | ||
721 | *(u_int32_t *)data ^= *(u_int32_t *)iva; | ||
722 | *(u_int32_t *)(data + 4) ^= *(u_int32_t *)(iva + 4); | ||
723 | } | ||
724 | |||
587 | #if 0 | 725 | #if 0 |
588 | void | 726 | void |
589 | report(u_int32_t data[], u_int16_t len) | 727 | report(u_int32_t data[], u_int16_t len) |