<commit_before>package dkrcrypt

// The ARIA Block cipher from KISA
// Copyright (c) 2012 <NAME> <<EMAIL>>
// Licensed under the GPLv3 or, at your option, any later version.

// The cipher operates on 128-bit items, but some pieces are in bytes.
// This entire implementation is based around byte, but we should perhaps switch to larger (32-bit?) items.

/*

References:

http://172.16.17.32/ARIA/index-e.html
http://tools.ietf.org/html/rfc5794
http://seed.kisa.or.kr/kor/aria/aria.jsp

*/

// An AriaCipher is an instance of ARIA encryption using a particular key
type AriaCipher struct {
	ek     [17][16]byte // encryption subkeys
	dk     [17][16]byte // decryption subkeys
	rounds int          // how many rounds
}

// constants for key whitening
var (
	c1 = []byte{0x51, 0x7c, 0xc1, 0xb7, 0x27, 0x22, 0x0a, 0x94, 0xfe, 0x13, 0xab, 0xe8, 0xfa, 0x9a, 0x6e, 0xe0}
	c2 = []byte{0x6d, 0xb1, 0x4a, 0xcc, 0x9e, 0x21, 0xc8, 0x20, 0xff, 0x28, 0xb1, 0xd5, 0xef, 0x5d, 0xe2, 0xb0}
	c3 = []byte{0xdb, 0x92, 0x37, 0x1d, 0x21, 0x26, 0xe9, 0x70, 0x03, 0x24, 0x97, 0x75, 0x04, 0xe8, 0xc9, 0x0e}
)

// rotate a slice of 16 bytes right by 'count' bits.  The output goes in 'o'
func rotrslice(o, b []byte, count int) []byte {

	bitrot := uint(count % 8)
	byterot := count / 8

	for i := 0; i < 16; i++ {
		o[i] = 0
	}

	for i := range b {
		oi := (i + byterot) % 16
		oi1 := (i + byterot + 1) % 16
		o[oi] = (o[oi] & (0xff << uint((8 - bitrot)))) | (b[i] >> bitrot)
		o[oi1] = (o[oi1] & (0xff >> bitrot)) | (b[i] << uint((8 - bitrot)))
	}

	return o
}

// rotate a slice of 16 bytes left by 'count' bits.  The output goes in 'o'
func rotlslice(o, b []byte, count int) []byte {
	return rotrslice(o, b, (16*8)-count)
}

// xor two slices: o = a ^ b
func xorslice(o, a, b []byte) {
	for i := 0; i < len(o); i++ {
		o[i] = a[i] ^ b[i]
	}
}

// NewAria creates and returns a new AriaCipher.
// The key argument should be 16/24/32 bytes.
func NewAria(key []byte) (*AriaCipher, error) {

	c := new(AriaCipher)

	var ck1, ck2, ck3 []byte

	switch len(key) {
	case 16:
		c.rounds = 12
		ck1, ck2, ck3 = c1, c2, c3
	case 24:
		c.rounds = 14
		ck1, ck2, ck3 = c2, c3, c1
	case 32:
		c.rounds = 16
		ck1, ck2, ck3 = c3, c1, c2
	default:
		return nil, KeySizeError(len(key))
	}

	kr := make([]byte, 16)
	kl := make([]byte, 16)

	copy(kl, key[0:16])
	copy(kr, key[16:])

	// key whitening
	w0 := make([]byte, 16)
	w1 := make([]byte, 16)
	w2 := make([]byte, 16)
	w3 := make([]byte, 16)

	copy(w0, kl)
	fo(w1, w0, ck1)
	xorslice(w1, w1, kr)

	fe(w2, w1, ck2)
	xorslice(w2, w2, w0)

	fo(w3, w2, ck3)
	xorslice(w3, w3, w1)

	out := make([]byte, 16)

	// create subkeys
	xorslice(c.ek[0][:], w0, rotrslice(out, w1, 19))
	xorslice(c.ek[1][:], w1, rotrslice(out, w2, 19))
	xorslice(c.ek[2][:], w2, rotrslice(out, w3, 19))
	xorslice(c.ek[3][:], w3, rotrslice(out, w0, 19))

	xorslice(c.ek[4][:], w0, rotrslice(out, w1, 31))
	xorslice(c.ek[5][:], w1, rotrslice(out, w2, 31))
	xorslice(c.ek[6][:], w2, rotrslice(out, w3, 31))
	xorslice(c.ek[7][:], w3, rotrslice(out, w0, 31))

	xorslice(c.ek[8][:], w0, rotlslice(out, w1, 61))
	xorslice(c.ek[9][:], w1, rotlslice(out, w2, 61))
	xorslice(c.ek[10][:], w2, rotlslice(out, w3, 61))
	xorslice(c.ek[11][:], w3, rotlslice(out, w0, 61))

	xorslice(c.ek[12][:], w0, rotlslice(out, w1, 31))
	xorslice(c.ek[13][:], w1, rotlslice(out, w2, 31))
	xorslice(c.ek[14][:], w2, rotlslice(out, w3, 31))
	xorslice(c.ek[15][:], w3, rotlslice(out, w0, 31))

	xorslice(c.ek[16][:], w0, rotlslice(out, w1, 19))

	// decryption subkeys
	copy(c.dk[0][:], c.ek[c.rounds][:])
	for i := 1; i < c.rounds; i++ {
		a(c.ek[c.rounds-i][:], c.dk[i][:])
	}<commit_msg>only initialize encryption subkeys if we need them.<commit_after>package dkrcrypt

// The ARIA Block cipher from KISA
// Copyright (c) 2012 <NAME> <<EMAIL>>
// Licensed under the GPLv3 or, at your option, any later version.

// The cipher operates on 128-bit items, but some pieces are in bytes.
// This entire implementation is based around byte, but we should perhaps switch to larger (32-bit?) items.

/*

References:

http://172.16.17.32/ARIA/index-e.html
http://tools.ietf.org/html/rfc5794
http://seed.kisa.or.kr/kor/aria/aria.jsp

*/

// An AriaCipher is an instance of ARIA encryption using a particular key
type AriaCipher struct {
	ek     [17][16]byte // encryption subkeys
	dk     [17][16]byte // decryption subkeys
	rounds int          // how many rounds
}

// constants for key whitening
var (
	c1 = []byte{0x51, 0x7c, 0xc1, 0xb7, 0x27, 0x22, 0x0a, 0x94, 0xfe, 0x13, 0xab, 0xe8, 0xfa, 0x9a, 0x6e, 0xe0}
	c2 = []byte{0x6d, 0xb1, 0x4a, 0xcc, 0x9e, 0x21, 0xc8, 0x20, 0xff, 0x28, 0xb1, 0xd5, 0xef, 0x5d, 0xe2, 0xb0}
	c3 = []byte{0xdb, 0x92, 0x37, 0x1d, 0x21, 0x26, 0xe9, 0x70, 0x03, 0x24, 0x97, 0x75, 0x04, 0xe8, 0xc9, 0x0e}
)

// rotate a slice of 16 bytes right by 'count' bits.  The output goes in 'o'
func rotrslice(o, b []byte, count int) []byte {

	bitrot := uint(count % 8)
	byterot := count / 8

	for i := 0; i < 16; i++ {
		o[i] = 0
	}

	for i := range b {
		oi := (i + byterot) % 16
		oi1 := (i + byterot + 1) % 16
		o[oi] = (o[oi] & (0xff << uint((8 - bitrot)))) | (b[i] >> bitrot)
		o[oi1] = (o[oi1] & (0xff >> bitrot)) | (b[i] << uint((8 - bitrot)))
	}

	return o
}

// rotate a slice of 16 bytes left by 'count' bits.  The output goes in 'o'
func rotlslice(o, b []byte, count int) []byte {
	return rotrslice(o, b, (16*8)-count)
}

// xor two slices: o = a ^ b
func xorslice(o, a, b []byte) {
	for i := 0; i < len(o); i++ {
		o[i] = a[i] ^ b[i]
	}
}

// NewAria creates and returns a new AriaCipher.
// The key argument should be 16/24/32 bytes.
func NewAria(key []byte) (*AriaCipher, error) {

	c := new(AriaCipher)

	var ck1, ck2, ck3 []byte

	switch len(key) {
	case 16:
		c.rounds = 12
		ck1, ck2, ck3 = c1, c2, c3
	case 24:
		c.rounds = 14
		ck1, ck2, ck3 = c2, c3, c1
	case 32:
		c.rounds = 16
		ck1, ck2, ck3 = c3, c1, c2
	default:
		return nil, KeySizeError(len(key))
	}

	kr := make([]byte, 16)
	kl := make([]byte, 16)

	copy(kl, key[0:16])
	copy(kr, key[16:])

	// key whitening
	w0 := make([]byte, 16)
	w1 := make([]byte, 16)
	w2 := make([]byte, 16)
	w3 := make([]byte, 16)

	copy(w0, kl)
	fo(w1, w0, ck1)
	xorslice(w1, w1, kr)

	fe(w2, w1, ck2)
	xorslice(w2, w2, w0)

	fo(w3, w2, ck3)
	xorslice(w3, w3, w1)

	out := make([]byte, 16)

	// create subkeys
	xorslice(c.ek[0][:], w0, rotrslice(out, w1, 19))
	xorslice(c.ek[1][:], w1, rotrslice(out, w2, 19))
	xorslice(c.ek[2][:], w2, rotrslice(out, w3, 19))
	xorslice(c.ek[3][:], w3, rotrslice(out, w0, 19))

	xorslice(c.ek[4][:], w0, rotrslice(out, w1, 31))
	xorslice(c.ek[5][:], w1, rotrslice(out, w2, 31))
	xorslice(c.ek[6][:], w2, rotrslice(out, w3, 31))
	xorslice(c.ek[7][:], w3, rotrslice(out, w0, 31))

	xorslice(c.ek[8][:], w0, rotlslice(out, w1, 61))
	xorslice(c.ek[9][:], w1, rotlslice(out, w2, 61))
	xorslice(c.ek[10][:], w2, rotlslice(out, w3, 61))
	xorslice(c.ek[11][:], w3, rotlslice(out, w0, 61))

	xorslice(c.ek[12][:], w0, rotlslice(out, w1, 31))

	if c.rounds > 12 {
		xorslice(c.ek[13][:], w1, rotlslice(out, w2, 31))
		xorslice(c.ek[14][:], w2, rotlslice(out, w3, 31))
	}

	if c.rounds > 14 {
		xorslice(c.ek[15][:], w3, rotlslice(out, w0, 31))
		xorslice(c.ek[16][:], w0, rotlslice(out, w1, 19))
	}

	// decryption subkeys
	copy(c.dk[0][:], c.ek[c.rounds][:])
	for i := 1; i < c.rounds; i++ {
		a(c.ek[c.rounds-i][:], c.dk[i][:])
	}
{'fraction_non_alphanumeric': 0.20195307201953072, 'fraction_numerical': 0.09304218093042181, 'mean_word_length': 3.5294840294840295, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1926263', 'n_tokens_mistral': 4040, 'n_tokens_neox': 3581, 'n_words': 931}