URL
https://opencores.org/ocsvn/openrisc/openrisc/trunk
Subversion Repositories openrisc
[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [libgo/] [go/] [crypto/] [des/] [block.go] - Rev 747
Compare with Previous | Blame | View Log
// Copyright 2011 The Go Authors. All rights reserved.// Use of this source code is governed by a BSD-style// license that can be found in the LICENSE file.package desimport ("encoding/binary")func cryptBlock(subkeys []uint64, dst, src []byte, decrypt bool) {b := binary.BigEndian.Uint64(src)b = permuteBlock(b, initialPermutation[:])left, right := uint32(b>>32), uint32(b)var subkey uint64for i := 0; i < 16; i++ {if decrypt {subkey = subkeys[15-i]} else {subkey = subkeys[i]}left, right = right, left^feistel(right, subkey)}// switch left & right and perform final permutationpreOutput := (uint64(right) << 32) | uint64(left)binary.BigEndian.PutUint64(dst, permuteBlock(preOutput, finalPermutation[:]))}// Encrypt one block from src into dst, using the subkeys.func encryptBlock(subkeys []uint64, dst, src []byte) {cryptBlock(subkeys, dst, src, false)}// Decrypt one block from src into dst, using the subkeys.func decryptBlock(subkeys []uint64, dst, src []byte) {cryptBlock(subkeys, dst, src, true)}// DES Feistel functionfunc feistel(right uint32, key uint64) (result uint32) {sBoxLocations := key ^ permuteBlock(uint64(right), expansionFunction[:])var sBoxResult uint32for i := uint8(0); i < 8; i++ {sBoxLocation := uint8(sBoxLocations>>42) & 0x3fsBoxLocations <<= 6// row determined by 1st and 6th bitrow := (sBoxLocation & 0x1) | ((sBoxLocation & 0x20) >> 4)// column is middle four bitscolumn := (sBoxLocation >> 1) & 0xfsBoxResult |= uint32(sBoxes[i][row][column]) << (4 * (7 - i))}return uint32(permuteBlock(uint64(sBoxResult), permutationFunction[:]))}// general purpose function to perform DES block permutationsfunc permuteBlock(src uint64, permutation []uint8) (block uint64) {for position, n := range permutation {bit := (src >> n) & 1block |= bit << uint((len(permutation)-1)-position)}return}// creates 16 28-bit blocks rotated according// to the rotation schedulefunc ksRotate(in uint32) (out []uint32) {out = make([]uint32, 16)last := infor i := 0; i < 16; i++ {// 28-bit circular left shiftleft := (last << (4 + ksRotations[i])) >> 4right := (last << 4) >> (32 - ksRotations[i])out[i] = left | rightlast = out[i]}return}// creates 16 56-bit subkeys from the original keyfunc (c *Cipher) generateSubkeys(keyBytes []byte) {// apply PC1 permutation to keykey := binary.BigEndian.Uint64(keyBytes)permutedKey := permuteBlock(key, permutedChoice1[:])// rotate halves of permuted key according to the rotation scheduleleftRotations := ksRotate(uint32(permutedKey >> 28))rightRotations := ksRotate(uint32(permutedKey<<4) >> 4)// generate subkeysfor i := 0; i < 16; i++ {// combine halves to form 56-bit input to PC2pc2Input := uint64(leftRotations[i])<<28 | uint64(rightRotations[i])// apply PC2 permutation to 7 byte inputc.subkeys[i] = permuteBlock(pc2Input, permutedChoice2[:])}}
