mirror of
https://github.com/nadoo/glider.git
synced 2026-11-19 07:09:42 +08:00
general: add vendor folder
This commit is contained in:
1 parent
621e8ed018
commit
3c766d0b68
103 files changed
+18061
-19
No files matched your search
+368
@@ -0,0 +1,368 @@
|
||||
// Copyright (c) 2013 Damian Gryski <damian@gryski.com>
|
||||
// Licensed under the GPLv3 or, at your option, any later version.
|
||||
|
||||
// Package camellia is an implementation of the CAMELLIA encryption algorithm
|
||||
/*
|
||||
|
||||
This is an unoptimized version based on the description in RFC 3713.
|
||||
|
||||
References:
|
||||
http://en.wikipedia.org/wiki/Camellia_%28cipher%29
|
||||
https://info.isl.ntt.co.jp/crypt/eng/camellia/
|
||||
*/
|
||||
package camellia
|
||||
|
||||
import (
|
||||
"crypto/cipher"
|
||||
"encoding/binary"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
const BlockSize = 16
|
||||
|
||||
type KeySizeError int
|
||||
|
||||
func (k KeySizeError) Error() string {
|
||||
return "camellia: invalid key size " + strconv.Itoa(int(k))
|
||||
}
|
||||
|
||||
type camelliaCipher struct {
|
||||
kw [5]uint64
|
||||
k [25]uint64
|
||||
ke [7]uint64
|
||||
klen int
|
||||
}
|
||||
|
||||
const (
|
||||
sigma1 = 0xA09E667F3BCC908B
|
||||
sigma2 = 0xB67AE8584CAA73B2
|
||||
sigma3 = 0xC6EF372FE94F82BE
|
||||
sigma4 = 0x54FF53A5F1D36F1C
|
||||
sigma5 = 0x10E527FADE682D1D
|
||||
sigma6 = 0xB05688C2B3E6C1FD
|
||||
)
|
||||
|
||||
func init() {
|
||||
// initialize other sboxes
|
||||
for i := range sbox1 {
|
||||
sbox2[i] = rotl8(sbox1[i], 1)
|
||||
sbox3[i] = rotl8(sbox1[i], 7)
|
||||
sbox4[i] = sbox1[rotl8(uint8(i), 1)]
|
||||
}
|
||||
}
|
||||
|
||||
func rotl128(k [2]uint64, rot uint) (hi, lo uint64) {
|
||||
|
||||
if rot > 64 {
|
||||
rot -= 64
|
||||
k[0], k[1] = k[1], k[0]
|
||||
}
|
||||
|
||||
t := k[0] >> (64 - rot)
|
||||
hi = (k[0] << rot) | (k[1] >> (64 - rot))
|
||||
lo = (k[1] << rot) | t
|
||||
return hi, lo
|
||||
}
|
||||
|
||||
func rotl32(k uint32, rot uint) uint32 {
|
||||
return (k << rot) | (k >> (32 - rot))
|
||||
}
|
||||
|
||||
func rotl8(k byte, rot uint) byte {
|
||||
return (k << rot) | (k >> (8 - rot))
|
||||
}
|
||||
|
||||
// New creates and returns a new cipher.Block.
|
||||
// The key argument should be 16, 24, or 32 bytes.
|
||||
func New(key []byte) (cipher.Block, error) {
|
||||
|
||||
klen := len(key)
|
||||
switch klen {
|
||||
default:
|
||||
return nil, KeySizeError(klen)
|
||||
case 16, 24, 32:
|
||||
break
|
||||
}
|
||||
|
||||
var d1, d2 uint64
|
||||
|
||||
var kl [2]uint64
|
||||
var kr [2]uint64
|
||||
var ka [2]uint64
|
||||
var kb [2]uint64
|
||||
|
||||
kl[0] = binary.BigEndian.Uint64(key[0:])
|
||||
kl[1] = binary.BigEndian.Uint64(key[8:])
|
||||
|
||||
switch klen {
|
||||
case 24:
|
||||
kr[0] = binary.BigEndian.Uint64(key[16:])
|
||||
kr[1] = ^kr[0]
|
||||
case 32:
|
||||
kr[0] = binary.BigEndian.Uint64(key[16:])
|
||||
kr[1] = binary.BigEndian.Uint64(key[24:])
|
||||
|
||||
}
|
||||
|
||||
d1 = (kl[0] ^ kr[0])
|
||||
d2 = (kl[1] ^ kr[1])
|
||||
|
||||
d2 = d2 ^ f(d1, sigma1)
|
||||
d1 = d1 ^ f(d2, sigma2)
|
||||
|
||||
d1 = d1 ^ (kl[0])
|
||||
d2 = d2 ^ (kl[1])
|
||||
d2 = d2 ^ f(d1, sigma3)
|
||||
d1 = d1 ^ f(d2, sigma4)
|
||||
ka[0] = d1
|
||||
ka[1] = d2
|
||||
d1 = (ka[0] ^ kr[0])
|
||||
d2 = (ka[1] ^ kr[1])
|
||||
d2 = d2 ^ f(d1, sigma5)
|
||||
d1 = d1 ^ f(d2, sigma6)
|
||||
kb[0] = d1
|
||||
kb[1] = d2
|
||||
|
||||
// here we generate our keys
|
||||
c := new(camelliaCipher)
|
||||
|
||||
c.klen = klen
|
||||
|
||||
if klen == 16 {
|
||||
|
||||
c.kw[1], c.kw[2] = rotl128(kl, 0)
|
||||
|
||||
c.k[1], c.k[2] = rotl128(ka, 0)
|
||||
c.k[3], c.k[4] = rotl128(kl, 15)
|
||||
c.k[5], c.k[6] = rotl128(ka, 15)
|
||||
|
||||
c.ke[1], c.ke[2] = rotl128(ka, 30)
|
||||
|
||||
c.k[7], c.k[8] = rotl128(kl, 45)
|
||||
c.k[9], _ = rotl128(ka, 45)
|
||||
_, c.k[10] = rotl128(kl, 60)
|
||||
c.k[11], c.k[12] = rotl128(ka, 60)
|
||||
|
||||
c.ke[3], c.ke[4] = rotl128(kl, 77)
|
||||
|
||||
c.k[13], c.k[14] = rotl128(kl, 94)
|
||||
c.k[15], c.k[16] = rotl128(ka, 94)
|
||||
c.k[17], c.k[18] = rotl128(kl, 111)
|
||||
|
||||
c.kw[3], c.kw[4] = rotl128(ka, 111)
|
||||
|
||||
} else {
|
||||
// 24 or 32
|
||||
|
||||
c.kw[1], c.kw[2] = rotl128(kl, 0)
|
||||
|
||||
c.k[1], c.k[2] = rotl128(kb, 0)
|
||||
c.k[3], c.k[4] = rotl128(kr, 15)
|
||||
c.k[5], c.k[6] = rotl128(ka, 15)
|
||||
|
||||
c.ke[1], c.ke[2] = rotl128(kr, 30)
|
||||
|
||||
c.k[7], c.k[8] = rotl128(kb, 30)
|
||||
c.k[9], c.k[10] = rotl128(kl, 45)
|
||||
c.k[11], c.k[12] = rotl128(ka, 45)
|
||||
|
||||
c.ke[3], c.ke[4] = rotl128(kl, 60)
|
||||
|
||||
c.k[13], c.k[14] = rotl128(kr, 60)
|
||||
c.k[15], c.k[16] = rotl128(kb, 60)
|
||||
c.k[17], c.k[18] = rotl128(kl, 77)
|
||||
|
||||
c.ke[5], c.ke[6] = rotl128(ka, 77)
|
||||
|
||||
c.k[19], c.k[20] = rotl128(kr, 94)
|
||||
c.k[21], c.k[22] = rotl128(ka, 94)
|
||||
c.k[23], c.k[24] = rotl128(kl, 111)
|
||||
|
||||
c.kw[3], c.kw[4] = rotl128(kb, 111)
|
||||
}
|
||||
|
||||
return c, nil
|
||||
}
|
||||
|
||||
func (c *camelliaCipher) Encrypt(dst, src []byte) {
|
||||
|
||||
d1 := binary.BigEndian.Uint64(src[0:])
|
||||
d2 := binary.BigEndian.Uint64(src[8:])
|
||||
|
||||
d1 ^= c.kw[1]
|
||||
d2 ^= c.kw[2]
|
||||
|
||||
d2 = d2 ^ f(d1, c.k[1])
|
||||
d1 = d1 ^ f(d2, c.k[2])
|
||||
d2 = d2 ^ f(d1, c.k[3])
|
||||
d1 = d1 ^ f(d2, c.k[4])
|
||||
d2 = d2 ^ f(d1, c.k[5])
|
||||
d1 = d1 ^ f(d2, c.k[6])
|
||||
|
||||
d1 = fl(d1, c.ke[1])
|
||||
d2 = flinv(d2, c.ke[2])
|
||||
|
||||
d2 = d2 ^ f(d1, c.k[7])
|
||||
d1 = d1 ^ f(d2, c.k[8])
|
||||
d2 = d2 ^ f(d1, c.k[9])
|
||||
d1 = d1 ^ f(d2, c.k[10])
|
||||
d2 = d2 ^ f(d1, c.k[11])
|
||||
d1 = d1 ^ f(d2, c.k[12])
|
||||
|
||||
d1 = fl(d1, c.ke[3])
|
||||
d2 = flinv(d2, c.ke[4])
|
||||
|
||||
d2 = d2 ^ f(d1, c.k[13])
|
||||
d1 = d1 ^ f(d2, c.k[14])
|
||||
d2 = d2 ^ f(d1, c.k[15])
|
||||
d1 = d1 ^ f(d2, c.k[16])
|
||||
d2 = d2 ^ f(d1, c.k[17])
|
||||
d1 = d1 ^ f(d2, c.k[18])
|
||||
|
||||
if c.klen > 16 {
|
||||
// 24 or 32
|
||||
|
||||
d1 = fl(d1, c.ke[5])
|
||||
d2 = flinv(d2, c.ke[6])
|
||||
|
||||
d2 = d2 ^ f(d1, c.k[19])
|
||||
d1 = d1 ^ f(d2, c.k[20])
|
||||
d2 = d2 ^ f(d1, c.k[21])
|
||||
d1 = d1 ^ f(d2, c.k[22])
|
||||
d2 = d2 ^ f(d1, c.k[23])
|
||||
d1 = d1 ^ f(d2, c.k[24])
|
||||
}
|
||||
|
||||
d2 = d2 ^ c.kw[3]
|
||||
d1 = d1 ^ c.kw[4]
|
||||
|
||||
binary.BigEndian.PutUint64(dst[0:], d2)
|
||||
binary.BigEndian.PutUint64(dst[8:], d1)
|
||||
}
|
||||
|
||||
func (c *camelliaCipher) Decrypt(dst, src []byte) {
|
||||
|
||||
d2 := binary.BigEndian.Uint64(src[0:])
|
||||
d1 := binary.BigEndian.Uint64(src[8:])
|
||||
|
||||
d1 = d1 ^ c.kw[4]
|
||||
d2 = d2 ^ c.kw[3]
|
||||
|
||||
if c.klen > 16 {
|
||||
// 24 or 32
|
||||
|
||||
d1 = d1 ^ f(d2, c.k[24])
|
||||
d2 = d2 ^ f(d1, c.k[23])
|
||||
d1 = d1 ^ f(d2, c.k[22])
|
||||
d2 = d2 ^ f(d1, c.k[21])
|
||||
d1 = d1 ^ f(d2, c.k[20])
|
||||
d2 = d2 ^ f(d1, c.k[19])
|
||||
|
||||
d2 = fl(d2, c.ke[6])
|
||||
d1 = flinv(d1, c.ke[5])
|
||||
}
|
||||
|
||||
d1 = d1 ^ f(d2, c.k[18])
|
||||
d2 = d2 ^ f(d1, c.k[17])
|
||||
d1 = d1 ^ f(d2, c.k[16])
|
||||
d2 = d2 ^ f(d1, c.k[15])
|
||||
d1 = d1 ^ f(d2, c.k[14])
|
||||
d2 = d2 ^ f(d1, c.k[13])
|
||||
|
||||
d2 = fl(d2, c.ke[4])
|
||||
d1 = flinv(d1, c.ke[3])
|
||||
|
||||
d1 = d1 ^ f(d2, c.k[12])
|
||||
d2 = d2 ^ f(d1, c.k[11])
|
||||
d1 = d1 ^ f(d2, c.k[10])
|
||||
d2 = d2 ^ f(d1, c.k[9])
|
||||
d1 = d1 ^ f(d2, c.k[8])
|
||||
d2 = d2 ^ f(d1, c.k[7])
|
||||
|
||||
d2 = fl(d2, c.ke[2])
|
||||
d1 = flinv(d1, c.ke[1])
|
||||
|
||||
d1 = d1 ^ f(d2, c.k[6])
|
||||
d2 = d2 ^ f(d1, c.k[5])
|
||||
d1 = d1 ^ f(d2, c.k[4])
|
||||
d2 = d2 ^ f(d1, c.k[3])
|
||||
d1 = d1 ^ f(d2, c.k[2])
|
||||
d2 = d2 ^ f(d1, c.k[1])
|
||||
|
||||
d2 ^= c.kw[2]
|
||||
d1 ^= c.kw[1]
|
||||
|
||||
binary.BigEndian.PutUint64(dst[0:], d1)
|
||||
binary.BigEndian.PutUint64(dst[8:], d2)
|
||||
}
|
||||
|
||||
func (c *camelliaCipher) BlockSize() int {
|
||||
return BlockSize
|
||||
}
|
||||
|
||||
func f(fin, ke uint64) uint64 {
|
||||
var x uint64
|
||||
x = fin ^ ke
|
||||
t1 := sbox1[uint8(x>>56)]
|
||||
t2 := sbox2[uint8(x>>48)]
|
||||
t3 := sbox3[uint8(x>>40)]
|
||||
t4 := sbox4[uint8(x>>32)]
|
||||
t5 := sbox2[uint8(x>>24)]
|
||||
t6 := sbox3[uint8(x>>16)]
|
||||
t7 := sbox4[uint8(x>>8)]
|
||||
t8 := sbox1[uint8(x)]
|
||||
y1 := t1 ^ t3 ^ t4 ^ t6 ^ t7 ^ t8
|
||||
y2 := t1 ^ t2 ^ t4 ^ t5 ^ t7 ^ t8
|
||||
y3 := t1 ^ t2 ^ t3 ^ t5 ^ t6 ^ t8
|
||||
y4 := t2 ^ t3 ^ t4 ^ t5 ^ t6 ^ t7
|
||||
y5 := t1 ^ t2 ^ t6 ^ t7 ^ t8
|
||||
y6 := t2 ^ t3 ^ t5 ^ t7 ^ t8
|
||||
y7 := t3 ^ t4 ^ t5 ^ t6 ^ t8
|
||||
y8 := t1 ^ t4 ^ t5 ^ t6 ^ t7
|
||||
return uint64(y1)<<56 | uint64(y2)<<48 | uint64(y3)<<40 | uint64(y4)<<32 | uint64(y5)<<24 | uint64(y6)<<16 | uint64(y7)<<8 | uint64(y8)
|
||||
}
|
||||
|
||||
func fl(flin, ke uint64) uint64 {
|
||||
x1 := uint32(flin >> 32)
|
||||
x2 := uint32(flin & 0xffffffff)
|
||||
k1 := uint32(ke >> 32)
|
||||
k2 := uint32(ke & 0xffffffff)
|
||||
x2 = x2 ^ rotl32(x1&k1, 1)
|
||||
x1 = x1 ^ (x2 | k2)
|
||||
return uint64(x1)<<32 | uint64(x2)
|
||||
}
|
||||
|
||||
func flinv(flin, ke uint64) uint64 {
|
||||
y1 := uint32(flin >> 32)
|
||||
y2 := uint32(flin & 0xffffffff)
|
||||
k1 := uint32(ke >> 32)
|
||||
k2 := uint32(ke & 0xffffffff)
|
||||
y1 = y1 ^ (y2 | k2)
|
||||
y2 = y2 ^ rotl32(y1&k1, 1)
|
||||
return uint64(y1)<<32 | uint64(y2)
|
||||
}
|
||||
|
||||
var sbox1 = [...]byte{
|
||||
0x70, 0x82, 0x2c, 0xec, 0xb3, 0x27, 0xc0, 0xe5, 0xe4, 0x85, 0x57, 0x35, 0xea, 0x0c, 0xae, 0x41,
|
||||
0x23, 0xef, 0x6b, 0x93, 0x45, 0x19, 0xa5, 0x21, 0xed, 0x0e, 0x4f, 0x4e, 0x1d, 0x65, 0x92, 0xbd,
|
||||
0x86, 0xb8, 0xaf, 0x8f, 0x7c, 0xeb, 0x1f, 0xce, 0x3e, 0x30, 0xdc, 0x5f, 0x5e, 0xc5, 0x0b, 0x1a,
|
||||
0xa6, 0xe1, 0x39, 0xca, 0xd5, 0x47, 0x5d, 0x3d, 0xd9, 0x01, 0x5a, 0xd6, 0x51, 0x56, 0x6c, 0x4d,
|
||||
0x8b, 0x0d, 0x9a, 0x66, 0xfb, 0xcc, 0xb0, 0x2d, 0x74, 0x12, 0x2b, 0x20, 0xf0, 0xb1, 0x84, 0x99,
|
||||
0xdf, 0x4c, 0xcb, 0xc2, 0x34, 0x7e, 0x76, 0x05, 0x6d, 0xb7, 0xa9, 0x31, 0xd1, 0x17, 0x04, 0xd7,
|
||||
0x14, 0x58, 0x3a, 0x61, 0xde, 0x1b, 0x11, 0x1c, 0x32, 0x0f, 0x9c, 0x16, 0x53, 0x18, 0xf2, 0x22,
|
||||
0xfe, 0x44, 0xcf, 0xb2, 0xc3, 0xb5, 0x7a, 0x91, 0x24, 0x08, 0xe8, 0xa8, 0x60, 0xfc, 0x69, 0x50,
|
||||
0xaa, 0xd0, 0xa0, 0x7d, 0xa1, 0x89, 0x62, 0x97, 0x54, 0x5b, 0x1e, 0x95, 0xe0, 0xff, 0x64, 0xd2,
|
||||
0x10, 0xc4, 0x00, 0x48, 0xa3, 0xf7, 0x75, 0xdb, 0x8a, 0x03, 0xe6, 0xda, 0x09, 0x3f, 0xdd, 0x94,
|
||||
0x87, 0x5c, 0x83, 0x02, 0xcd, 0x4a, 0x90, 0x33, 0x73, 0x67, 0xf6, 0xf3, 0x9d, 0x7f, 0xbf, 0xe2,
|
||||
0x52, 0x9b, 0xd8, 0x26, 0xc8, 0x37, 0xc6, 0x3b, 0x81, 0x96, 0x6f, 0x4b, 0x13, 0xbe, 0x63, 0x2e,
|
||||
0xe9, 0x79, 0xa7, 0x8c, 0x9f, 0x6e, 0xbc, 0x8e, 0x29, 0xf5, 0xf9, 0xb6, 0x2f, 0xfd, 0xb4, 0x59,
|
||||
0x78, 0x98, 0x06, 0x6a, 0xe7, 0x46, 0x71, 0xba, 0xd4, 0x25, 0xab, 0x42, 0x88, 0xa2, 0x8d, 0xfa,
|
||||
0x72, 0x07, 0xb9, 0x55, 0xf8, 0xee, 0xac, 0x0a, 0x36, 0x49, 0x2a, 0x68, 0x3c, 0x38, 0xf1, 0xa4,
|
||||
0x40, 0x28, 0xd3, 0x7b, 0xbb, 0xc9, 0x43, 0xc1, 0x15, 0xe3, 0xad, 0xf4, 0x77, 0xc7, 0x80, 0x9e,
|
||||
}
|
||||
|
||||
var sbox2 [256]byte
|
||||
var sbox3 [256]byte
|
||||
var sbox4 [256]byte
|
||||
|
||||
var _ cipher.Block = &camelliaCipher{}
|
||||
+58
@@ -0,0 +1,58 @@
|
||||
#!/usr/bin/perl
|
||||
|
||||
# to run the full verification suite:
|
||||
# curl http://info.isl.ntt.co.jp/crypt/eng/camellia/dl/cryptrec/t_camellia.txt |perl ./t_camellia.pl >cfull_test.go
|
||||
# perl -pi -e 's/range camelliaTests/$&Full/' camellia_test.go
|
||||
# go test
|
||||
|
||||
print <<GOCODE;
|
||||
package camellia
|
||||
|
||||
var camelliaTestsFull = []struct {
|
||||
key []byte
|
||||
plain []byte
|
||||
cipher []byte
|
||||
}{
|
||||
GOCODE
|
||||
|
||||
my $k;
|
||||
my $p;
|
||||
my $c;
|
||||
|
||||
# K No.010 : EF CD AB 89 67 45 23 01 10 32 54 76 98 BA DC FE 10 32 54 76 98 BA DC FE
|
||||
sub linetostr {
|
||||
my $l = shift;
|
||||
|
||||
$l =~ s/.*: *//;
|
||||
$l =~ s/\s*$//;
|
||||
my @h = split / /, $l;
|
||||
$l = join(",", map "0x$_", @h);
|
||||
return $l;
|
||||
}
|
||||
|
||||
|
||||
while(<>) {
|
||||
next if /^\s*$/ or /^Camellia/;
|
||||
if (/^K/) {
|
||||
$k = linetostr($_);
|
||||
}
|
||||
|
||||
if (/^P/) {
|
||||
$p = linetostr($_);
|
||||
}
|
||||
|
||||
if (/^C/) {
|
||||
$c = linetostr($_);
|
||||
print <<"GOCODE";
|
||||
{
|
||||
[]byte{$k},
|
||||
[]byte{$p},
|
||||
[]byte{$c},
|
||||
},
|
||||
GOCODE
|
||||
}
|
||||
}
|
||||
|
||||
print <<GOCODE
|
||||
}
|
||||
GOCODE
|
||||
+250
@@ -0,0 +1,250 @@
|
||||
// Package idea implements the IDEA block cipher
|
||||
/*
|
||||
For more information, please see https://en.wikipedia.org/wiki/International_Data_Encryption_Algorithm
|
||||
|
||||
This implementation derived from Public Domain code by Colin Plumb available at https://www.schneier.com/book-applied-source.html
|
||||
*/
|
||||
package idea
|
||||
|
||||
import (
|
||||
"crypto/cipher"
|
||||
"encoding/binary"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
const rounds = 8
|
||||
const keyLen = (6*rounds + 4)
|
||||
|
||||
// KeySizeError is returned for incorrect key sizes
|
||||
type KeySizeError int
|
||||
|
||||
func (k KeySizeError) Error() string {
|
||||
return "idea: invalid key size " + strconv.Itoa(int(k))
|
||||
}
|
||||
|
||||
type ideaCipher struct {
|
||||
ek [keyLen]uint16
|
||||
dk [keyLen]uint16
|
||||
}
|
||||
|
||||
// NewCipher returns a cipher.Block implementing the IDEA block cipher. The key argument should be 16 bytes.
|
||||
func NewCipher(key []byte) (cipher.Block, error) {
|
||||
|
||||
if l := len(key); l != 16 {
|
||||
return nil, KeySizeError(l)
|
||||
}
|
||||
|
||||
cipher := &ideaCipher{}
|
||||
|
||||
expandKey(key, cipher.ek[:])
|
||||
|
||||
// key inversion is expensive, we could do this lazily
|
||||
invertKey(cipher.ek[:], cipher.dk[:])
|
||||
|
||||
return cipher, nil
|
||||
}
|
||||
|
||||
func (c *ideaCipher) BlockSize() int { return 8 }
|
||||
func (c *ideaCipher) Encrypt(dst, src []byte) { crypt(src, dst, c.ek[:]) }
|
||||
func (c *ideaCipher) Decrypt(dst, src []byte) { crypt(src, dst, c.dk[:]) }
|
||||
|
||||
// mulInv computes the multiplicative inverse of x mod 2^16+1
|
||||
func mulInv(x uint16) (ret uint16) {
|
||||
|
||||
if x <= 1 {
|
||||
return x // 0 and 1 are self-inverse
|
||||
}
|
||||
|
||||
t1 := uint16(0x10001 / uint32(x)) // Since x >= 2, this fits into 16 bits
|
||||
y := uint16(0x10001 % uint32(x))
|
||||
|
||||
if y == 1 {
|
||||
return 1 - t1
|
||||
}
|
||||
|
||||
var t0 uint16 = 1
|
||||
var q uint16
|
||||
|
||||
for y != 1 {
|
||||
q = x / y
|
||||
x = x % y
|
||||
t0 += q * t1
|
||||
if x == 1 {
|
||||
return t0
|
||||
}
|
||||
q = y / x
|
||||
y = y % x
|
||||
t1 += q * t0
|
||||
}
|
||||
return 1 - t1
|
||||
}
|
||||
|
||||
// mul computes x*y mod 2^16+1
|
||||
func mul(x, y uint16) uint16 {
|
||||
|
||||
if y == 0 {
|
||||
return 1 - x
|
||||
}
|
||||
|
||||
if x == 0 {
|
||||
return 1 - y
|
||||
}
|
||||
|
||||
t32 := uint32(x) * uint32(y)
|
||||
x = uint16(t32)
|
||||
y = uint16(t32 >> 16)
|
||||
|
||||
if x < y {
|
||||
return x - y + 1
|
||||
}
|
||||
|
||||
return x - y
|
||||
}
|
||||
|
||||
// expandKey computes encryption round-keys from a user-supplied key
|
||||
func expandKey(key []byte, EK []uint16) {
|
||||
var i, j int
|
||||
|
||||
for j = 0; j < 8; j++ {
|
||||
EK[j] = (uint16(key[0]) << 8) + uint16(key[1])
|
||||
key = key[2:]
|
||||
}
|
||||
for i = 0; j < keyLen; j++ {
|
||||
i++
|
||||
EK[i+7] = EK[i&7]<<9 | EK[(i+1)&7]>>7
|
||||
EK = EK[i&8:]
|
||||
i &= 7
|
||||
}
|
||||
}
|
||||
|
||||
// invertKey computes the decryption round-keys from a set of encryption round-keys
|
||||
func invertKey(EK []uint16, DK []uint16) {
|
||||
|
||||
var t1, t2, t3 uint16
|
||||
var p [keyLen]uint16
|
||||
pidx := keyLen
|
||||
ekidx := 0
|
||||
|
||||
t1 = mulInv(EK[ekidx])
|
||||
ekidx++
|
||||
t2 = -EK[ekidx]
|
||||
ekidx++
|
||||
t3 = -EK[ekidx]
|
||||
ekidx++
|
||||
pidx--
|
||||
p[pidx] = mulInv(EK[ekidx])
|
||||
ekidx++
|
||||
pidx--
|
||||
p[pidx] = t3
|
||||
pidx--
|
||||
p[pidx] = t2
|
||||
pidx--
|
||||
p[pidx] = t1
|
||||
|
||||
for i := 0; i < rounds-1; i++ {
|
||||
t1 = EK[ekidx]
|
||||
ekidx++
|
||||
pidx--
|
||||
p[pidx] = EK[ekidx]
|
||||
ekidx++
|
||||
pidx--
|
||||
p[pidx] = t1
|
||||
|
||||
t1 = mulInv(EK[ekidx])
|
||||
ekidx++
|
||||
t2 = -EK[ekidx]
|
||||
ekidx++
|
||||
t3 = -EK[ekidx]
|
||||
ekidx++
|
||||
pidx--
|
||||
p[pidx] = mulInv(EK[ekidx])
|
||||
ekidx++
|
||||
pidx--
|
||||
p[pidx] = t2
|
||||
pidx--
|
||||
p[pidx] = t3
|
||||
pidx--
|
||||
p[pidx] = t1
|
||||
}
|
||||
|
||||
t1 = EK[ekidx]
|
||||
ekidx++
|
||||
pidx--
|
||||
p[pidx] = EK[ekidx]
|
||||
ekidx++
|
||||
pidx--
|
||||
p[pidx] = t1
|
||||
|
||||
t1 = mulInv(EK[ekidx])
|
||||
ekidx++
|
||||
t2 = -EK[ekidx]
|
||||
ekidx++
|
||||
t3 = -EK[ekidx]
|
||||
ekidx++
|
||||
pidx--
|
||||
p[pidx] = mulInv(EK[ekidx])
|
||||
pidx--
|
||||
p[pidx] = t3
|
||||
pidx--
|
||||
p[pidx] = t2
|
||||
pidx--
|
||||
p[pidx] = t1
|
||||
|
||||
copy(DK, p[:])
|
||||
}
|
||||
|
||||
// crypt performs IDEA encryption given input/output buffers and a set of round-keys
|
||||
func crypt(inbuf, outbuf []byte, key []uint16) {
|
||||
|
||||
var x1, x2, x3, x4, s2, s3 uint16
|
||||
|
||||
x1 = binary.BigEndian.Uint16(inbuf[0:])
|
||||
x2 = binary.BigEndian.Uint16(inbuf[2:])
|
||||
x3 = binary.BigEndian.Uint16(inbuf[4:])
|
||||
x4 = binary.BigEndian.Uint16(inbuf[6:])
|
||||
|
||||
for r := rounds; r > 0; r-- {
|
||||
|
||||
x1 = mul(x1, key[0])
|
||||
key = key[1:]
|
||||
x2 += key[0]
|
||||
key = key[1:]
|
||||
x3 += key[0]
|
||||
key = key[1:]
|
||||
|
||||
x4 = mul(x4, key[0])
|
||||
key = key[1:]
|
||||
|
||||
s3 = x3
|
||||
x3 ^= x1
|
||||
x3 = mul(x3, key[0])
|
||||
key = key[1:]
|
||||
s2 = x2
|
||||
|
||||
x2 ^= x4
|
||||
x2 += x3
|
||||
x2 = mul(x2, key[0])
|
||||
key = key[1:]
|
||||
x3 += x2
|
||||
|
||||
x1 ^= x2
|
||||
x4 ^= x3
|
||||
|
||||
x2 ^= s3
|
||||
x3 ^= s2
|
||||
|
||||
}
|
||||
x1 = mul(x1, key[0])
|
||||
key = key[1:]
|
||||
|
||||
x3 += key[0]
|
||||
key = key[1:]
|
||||
x2 += key[0]
|
||||
key = key[1:]
|
||||
x4 = mul(x4, key[0])
|
||||
|
||||
binary.BigEndian.PutUint16(outbuf[0:], x1)
|
||||
binary.BigEndian.PutUint16(outbuf[2:], x3)
|
||||
binary.BigEndian.PutUint16(outbuf[4:], x2)
|
||||
binary.BigEndian.PutUint16(outbuf[6:], x4)
|
||||
}
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2015 Damian Gryski <damian@gryski.com>
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
+284
@@ -0,0 +1,284 @@
|
||||
// Package rc2 implements the RC2 cipher
|
||||
/*
|
||||
https://www.ietf.org/rfc/rfc2268.txt
|
||||
http://people.csail.mit.edu/rivest/pubs/KRRR98.pdf
|
||||
|
||||
This code is licensed under the MIT license.
|
||||
*/
|
||||
package rc2
|
||||
|
||||
import (
|
||||
"crypto/cipher"
|
||||
"encoding/binary"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
// The rc2 block size in bytes
|
||||
const BlockSize = 8
|
||||
|
||||
type rc2Cipher struct {
|
||||
k [64]uint16
|
||||
}
|
||||
|
||||
// KeySizeError indicates the supplied key was invalid
|
||||
type KeySizeError int
|
||||
|
||||
func (k KeySizeError) Error() string { return "rc2: invalid key size " + strconv.Itoa(int(k)) }
|
||||
|
||||
// EffectiveKeySizeError indicates the supplied effective key length was invalid
|
||||
type EffectiveKeySizeError int
|
||||
|
||||
func (k EffectiveKeySizeError) Error() string {
|
||||
return "rc2: invalid effective key size " + strconv.Itoa(int(k))
|
||||
}
|
||||
|
||||
// New returns a new rc2 cipher with the given key and effective key length t1
|
||||
func New(key []byte, t1 int) (cipher.Block, error) {
|
||||
if l := len(key); l == 0 || l > 128 {
|
||||
return nil, KeySizeError(l)
|
||||
}
|
||||
|
||||
if t1 < 8 || t1 > 1024 {
|
||||
return nil, EffectiveKeySizeError(t1)
|
||||
}
|
||||
|
||||
return &rc2Cipher{
|
||||
k: expandKey(key, t1),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (c *rc2Cipher) BlockSize() int { return BlockSize }
|
||||
|
||||
var piTable = [256]byte{
|
||||
0xd9, 0x78, 0xf9, 0xc4, 0x19, 0xdd, 0xb5, 0xed, 0x28, 0xe9, 0xfd, 0x79, 0x4a, 0xa0, 0xd8, 0x9d,
|
||||
0xc6, 0x7e, 0x37, 0x83, 0x2b, 0x76, 0x53, 0x8e, 0x62, 0x4c, 0x64, 0x88, 0x44, 0x8b, 0xfb, 0xa2,
|
||||
0x17, 0x9a, 0x59, 0xf5, 0x87, 0xb3, 0x4f, 0x13, 0x61, 0x45, 0x6d, 0x8d, 0x09, 0x81, 0x7d, 0x32,
|
||||
0xbd, 0x8f, 0x40, 0xeb, 0x86, 0xb7, 0x7b, 0x0b, 0xf0, 0x95, 0x21, 0x22, 0x5c, 0x6b, 0x4e, 0x82,
|
||||
0x54, 0xd6, 0x65, 0x93, 0xce, 0x60, 0xb2, 0x1c, 0x73, 0x56, 0xc0, 0x14, 0xa7, 0x8c, 0xf1, 0xdc,
|
||||
0x12, 0x75, 0xca, 0x1f, 0x3b, 0xbe, 0xe4, 0xd1, 0x42, 0x3d, 0xd4, 0x30, 0xa3, 0x3c, 0xb6, 0x26,
|
||||
0x6f, 0xbf, 0x0e, 0xda, 0x46, 0x69, 0x07, 0x57, 0x27, 0xf2, 0x1d, 0x9b, 0xbc, 0x94, 0x43, 0x03,
|
||||
0xf8, 0x11, 0xc7, 0xf6, 0x90, 0xef, 0x3e, 0xe7, 0x06, 0xc3, 0xd5, 0x2f, 0xc8, 0x66, 0x1e, 0xd7,
|
||||
0x08, 0xe8, 0xea, 0xde, 0x80, 0x52, 0xee, 0xf7, 0x84, 0xaa, 0x72, 0xac, 0x35, 0x4d, 0x6a, 0x2a,
|
||||
0x96, 0x1a, 0xd2, 0x71, 0x5a, 0x15, 0x49, 0x74, 0x4b, 0x9f, 0xd0, 0x5e, 0x04, 0x18, 0xa4, 0xec,
|
||||
0xc2, 0xe0, 0x41, 0x6e, 0x0f, 0x51, 0xcb, 0xcc, 0x24, 0x91, 0xaf, 0x50, 0xa1, 0xf4, 0x70, 0x39,
|
||||
0x99, 0x7c, 0x3a, 0x85, 0x23, 0xb8, 0xb4, 0x7a, 0xfc, 0x02, 0x36, 0x5b, 0x25, 0x55, 0x97, 0x31,
|
||||
0x2d, 0x5d, 0xfa, 0x98, 0xe3, 0x8a, 0x92, 0xae, 0x05, 0xdf, 0x29, 0x10, 0x67, 0x6c, 0xba, 0xc9,
|
||||
0xd3, 0x00, 0xe6, 0xcf, 0xe1, 0x9e, 0xa8, 0x2c, 0x63, 0x16, 0x01, 0x3f, 0x58, 0xe2, 0x89, 0xa9,
|
||||
0x0d, 0x38, 0x34, 0x1b, 0xab, 0x33, 0xff, 0xb0, 0xbb, 0x48, 0x0c, 0x5f, 0xb9, 0xb1, 0xcd, 0x2e,
|
||||
0xc5, 0xf3, 0xdb, 0x47, 0xe5, 0xa5, 0x9c, 0x77, 0x0a, 0xa6, 0x20, 0x68, 0xfe, 0x7f, 0xc1, 0xad,
|
||||
}
|
||||
|
||||
func expandKey(key []byte, t1 int) [64]uint16 {
|
||||
|
||||
l := make([]byte, 128)
|
||||
copy(l, key)
|
||||
|
||||
var t = len(key)
|
||||
var t8 = (t1 + 7) / 8
|
||||
var tm = byte(255 % uint(1<<(8+uint(t1)-8*uint(t8))))
|
||||
|
||||
for i := len(key); i < 128; i++ {
|
||||
l[i] = piTable[l[i-1]+l[uint8(i-t)]]
|
||||
}
|
||||
|
||||
l[128-t8] = piTable[l[128-t8]&tm]
|
||||
|
||||
for i := 127 - t8; i >= 0; i-- {
|
||||
l[i] = piTable[l[i+1]^l[i+t8]]
|
||||
}
|
||||
|
||||
var k [64]uint16
|
||||
|
||||
for i := range k {
|
||||
k[i] = uint16(l[2*i]) + uint16(l[2*i+1])*256
|
||||
}
|
||||
|
||||
return k
|
||||
}
|
||||
|
||||
func rotl16(x uint16, b uint) uint16 {
|
||||
return (x >> (16 - b)) | (x << b)
|
||||
}
|
||||
|
||||
func (c *rc2Cipher) Encrypt(dst, src []byte) {
|
||||
|
||||
r0 := binary.LittleEndian.Uint16(src[0:])
|
||||
r1 := binary.LittleEndian.Uint16(src[2:])
|
||||
r2 := binary.LittleEndian.Uint16(src[4:])
|
||||
r3 := binary.LittleEndian.Uint16(src[6:])
|
||||
|
||||
var j int
|
||||
|
||||
// These three mix blocks have not been extracted to a common function for to performance reasons.
|
||||
for j <= 16 {
|
||||
// mix r0
|
||||
r0 = r0 + c.k[j] + (r3 & r2) + ((^r3) & r1)
|
||||
r0 = rotl16(r0, 1)
|
||||
j++
|
||||
|
||||
// mix r1
|
||||
r1 = r1 + c.k[j] + (r0 & r3) + ((^r0) & r2)
|
||||
r1 = rotl16(r1, 2)
|
||||
j++
|
||||
|
||||
// mix r2
|
||||
r2 = r2 + c.k[j] + (r1 & r0) + ((^r1) & r3)
|
||||
r2 = rotl16(r2, 3)
|
||||
j++
|
||||
|
||||
// mix r3
|
||||
r3 = r3 + c.k[j] + (r2 & r1) + ((^r2) & r0)
|
||||
r3 = rotl16(r3, 5)
|
||||
j++
|
||||
}
|
||||
|
||||
r0 = r0 + c.k[r3&63]
|
||||
r1 = r1 + c.k[r0&63]
|
||||
r2 = r2 + c.k[r1&63]
|
||||
r3 = r3 + c.k[r2&63]
|
||||
|
||||
for j <= 40 {
|
||||
// mix r0
|
||||
r0 = r0 + c.k[j] + (r3 & r2) + ((^r3) & r1)
|
||||
r0 = rotl16(r0, 1)
|
||||
j++
|
||||
|
||||
// mix r1
|
||||
r1 = r1 + c.k[j] + (r0 & r3) + ((^r0) & r2)
|
||||
r1 = rotl16(r1, 2)
|
||||
j++
|
||||
|
||||
// mix r2
|
||||
r2 = r2 + c.k[j] + (r1 & r0) + ((^r1) & r3)
|
||||
r2 = rotl16(r2, 3)
|
||||
j++
|
||||
|
||||
// mix r3
|
||||
r3 = r3 + c.k[j] + (r2 & r1) + ((^r2) & r0)
|
||||
r3 = rotl16(r3, 5)
|
||||
j++
|
||||
}
|
||||
|
||||
r0 = r0 + c.k[r3&63]
|
||||
r1 = r1 + c.k[r0&63]
|
||||
r2 = r2 + c.k[r1&63]
|
||||
r3 = r3 + c.k[r2&63]
|
||||
|
||||
for j <= 60 {
|
||||
// mix r0
|
||||
r0 = r0 + c.k[j] + (r3 & r2) + ((^r3) & r1)
|
||||
r0 = rotl16(r0, 1)
|
||||
j++
|
||||
|
||||
// mix r1
|
||||
r1 = r1 + c.k[j] + (r0 & r3) + ((^r0) & r2)
|
||||
r1 = rotl16(r1, 2)
|
||||
j++
|
||||
|
||||
// mix r2
|
||||
r2 = r2 + c.k[j] + (r1 & r0) + ((^r1) & r3)
|
||||
r2 = rotl16(r2, 3)
|
||||
j++
|
||||
|
||||
// mix r3
|
||||
r3 = r3 + c.k[j] + (r2 & r1) + ((^r2) & r0)
|
||||
r3 = rotl16(r3, 5)
|
||||
j++
|
||||
}
|
||||
|
||||
binary.LittleEndian.PutUint16(dst[0:], r0)
|
||||
binary.LittleEndian.PutUint16(dst[2:], r1)
|
||||
binary.LittleEndian.PutUint16(dst[4:], r2)
|
||||
binary.LittleEndian.PutUint16(dst[6:], r3)
|
||||
}
|
||||
|
||||
func (c *rc2Cipher) Decrypt(dst, src []byte) {
|
||||
|
||||
r0 := binary.LittleEndian.Uint16(src[0:])
|
||||
r1 := binary.LittleEndian.Uint16(src[2:])
|
||||
r2 := binary.LittleEndian.Uint16(src[4:])
|
||||
r3 := binary.LittleEndian.Uint16(src[6:])
|
||||
|
||||
j := 63
|
||||
|
||||
for j >= 44 {
|
||||
// unmix r3
|
||||
r3 = rotl16(r3, 16-5)
|
||||
r3 = r3 - c.k[j] - (r2 & r1) - ((^r2) & r0)
|
||||
j--
|
||||
|
||||
// unmix r2
|
||||
r2 = rotl16(r2, 16-3)
|
||||
r2 = r2 - c.k[j] - (r1 & r0) - ((^r1) & r3)
|
||||
j--
|
||||
|
||||
// unmix r1
|
||||
r1 = rotl16(r1, 16-2)
|
||||
r1 = r1 - c.k[j] - (r0 & r3) - ((^r0) & r2)
|
||||
j--
|
||||
|
||||
// unmix r0
|
||||
r0 = rotl16(r0, 16-1)
|
||||
r0 = r0 - c.k[j] - (r3 & r2) - ((^r3) & r1)
|
||||
j--
|
||||
}
|
||||
|
||||
r3 = r3 - c.k[r2&63]
|
||||
r2 = r2 - c.k[r1&63]
|
||||
r1 = r1 - c.k[r0&63]
|
||||
r0 = r0 - c.k[r3&63]
|
||||
|
||||
for j >= 20 {
|
||||
// unmix r3
|
||||
r3 = rotl16(r3, 16-5)
|
||||
r3 = r3 - c.k[j] - (r2 & r1) - ((^r2) & r0)
|
||||
j--
|
||||
|
||||
// unmix r2
|
||||
r2 = rotl16(r2, 16-3)
|
||||
r2 = r2 - c.k[j] - (r1 & r0) - ((^r1) & r3)
|
||||
j--
|
||||
|
||||
// unmix r1
|
||||
r1 = rotl16(r1, 16-2)
|
||||
r1 = r1 - c.k[j] - (r0 & r3) - ((^r0) & r2)
|
||||
j--
|
||||
|
||||
// unmix r0
|
||||
r0 = rotl16(r0, 16-1)
|
||||
r0 = r0 - c.k[j] - (r3 & r2) - ((^r3) & r1)
|
||||
j--
|
||||
}
|
||||
|
||||
r3 = r3 - c.k[r2&63]
|
||||
r2 = r2 - c.k[r1&63]
|
||||
r1 = r1 - c.k[r0&63]
|
||||
r0 = r0 - c.k[r3&63]
|
||||
|
||||
for j >= 0 {
|
||||
// unmix r3
|
||||
r3 = rotl16(r3, 16-5)
|
||||
r3 = r3 - c.k[j] - (r2 & r1) - ((^r2) & r0)
|
||||
j--
|
||||
|
||||
// unmix r2
|
||||
r2 = rotl16(r2, 16-3)
|
||||
r2 = r2 - c.k[j] - (r1 & r0) - ((^r1) & r3)
|
||||
j--
|
||||
|
||||
// unmix r1
|
||||
r1 = rotl16(r1, 16-2)
|
||||
r1 = r1 - c.k[j] - (r0 & r3) - ((^r0) & r2)
|
||||
j--
|
||||
|
||||
// unmix r0
|
||||
r0 = rotl16(r0, 16-1)
|
||||
r0 = r0 - c.k[j] - (r3 & r2) - ((^r3) & r1)
|
||||
j--
|
||||
}
|
||||
|
||||
binary.LittleEndian.PutUint16(dst[0:], r0)
|
||||
binary.LittleEndian.PutUint16(dst[2:], r1)
|
||||
binary.LittleEndian.PutUint16(dst[4:], r2)
|
||||
binary.LittleEndian.PutUint16(dst[6:], r3)
|
||||
}
|
||||
Reference in new issue
Block a user