Merge from mainline (167278:168000).
[official-gcc/graphite-test-results.git] / libgo / go / crypto / block / cipher.go
bloba50d05c2942b258e6226ba8be50d15301b6b96f3
1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
5 // The block package implements standard block cipher modes
6 // that can be wrapped around low-level block cipher implementations.
7 // See http://csrc.nist.gov/groups/ST/toolkit/BCM/current_modes.html
8 // and NIST Special Publication 800-38A.
9 package block
11 // A Cipher represents an implementation of block cipher
12 // using a given key. It provides the capability to encrypt
13 // or decrypt individual blocks. The mode implementations
14 // extend that capability to streams of blocks.
15 type Cipher interface {
16 // BlockSize returns the cipher's block size.
17 BlockSize() int
19 // Encrypt encrypts the first block in src into dst.
20 // Src and dst may point at the same memory.
21 Encrypt(dst, src []byte)
23 // Decrypt decrypts the first block in src into dst.
24 // Src and dst may point at the same memory.
25 Decrypt(dst, src []byte)
28 // Utility routines
30 func shift1(dst, src []byte) byte {
31 var b byte
32 for i := len(src) - 1; i >= 0; i-- {
33 bb := src[i] >> 7
34 dst[i] = src[i]<<1 | b
35 b = bb
37 return b
40 func same(p, q []byte) bool {
41 if len(p) != len(q) {
42 return false
44 for i := 0; i < len(p); i++ {
45 if p[i] != q[i] {
46 return false
49 return true
52 func dup(p []byte) []byte {
53 q := make([]byte, len(p))
54 copy(q, p)
55 return q