mirror of
https://github.com/golang/go.git
synced 2025-05-29 03:11:26 +00:00
Split constant time functions into crypto/subtle.
R=rsc CC=go-dev http://go/go-review/1018020
This commit is contained in:
parent
d00248980b
commit
ad67a86626
@ -17,6 +17,7 @@ crypto/hmac.install: crypto/md5.install crypto/sha1.install hash.install os.inst
|
||||
crypto/md5.install: hash.install os.install
|
||||
crypto/rc4.install: os.install strconv.install
|
||||
crypto/sha1.install: hash.install os.install
|
||||
crypto/subtle.install:
|
||||
debug/dwarf.install: encoding/binary.install os.install strconv.install
|
||||
debug/macho.install: bytes.install debug/dwarf.install encoding/binary.install fmt.install io.install os.install strconv.install
|
||||
debug/elf.install: debug/dwarf.install encoding/binary.install fmt.install io.install os.install strconv.install
|
||||
|
@ -31,6 +31,7 @@ DIRS=\
|
||||
crypto/md5\
|
||||
crypto/rc4\
|
||||
crypto/sha1\
|
||||
crypto/subtle\
|
||||
debug/dwarf\
|
||||
debug/macho\
|
||||
debug/elf\
|
||||
|
@ -6,6 +6,7 @@ package rsa
|
||||
|
||||
import (
|
||||
"bytes";
|
||||
"crypto/subtle";
|
||||
big "gmp";
|
||||
"io";
|
||||
"os";
|
||||
@ -27,7 +28,7 @@ func EncryptPKCS1v15(rand io.Reader, pub *PublicKey, msg []byte) (out []byte, er
|
||||
// EM = 0x02 || PS || 0x00 || M
|
||||
em := make([]byte, k-1);
|
||||
em[0] = 2;
|
||||
ps, mm := em[1:len(em)-len(msg)-1], em[len(em)-len(msg):len(em)];
|
||||
ps, mm := em[1 : len(em)-len(msg)-1], em[len(em)-len(msg) : len(em)];
|
||||
err = nonZeroRandomBytes(ps, rand);
|
||||
if err != nil {
|
||||
return;
|
||||
@ -77,8 +78,8 @@ func DecryptPKCS1v15SessionKey(rand io.Reader, priv *PrivateKey, ciphertext []by
|
||||
return;
|
||||
}
|
||||
|
||||
valid &= constantTimeEq(int32(len(msg)), int32(len(key)));
|
||||
constantTimeCopy(valid, key, msg);
|
||||
valid &= subtle.ConstantTimeEq(int32(len(msg)), int32(len(key)));
|
||||
subtle.ConstantTimeCopy(valid, key, msg);
|
||||
return;
|
||||
}
|
||||
|
||||
@ -96,8 +97,8 @@ func decryptPKCS1v15(rand io.Reader, priv *PrivateKey, ciphertext []byte) (valid
|
||||
}
|
||||
|
||||
em := leftPad(m.Bytes(), k);
|
||||
firstByteIsZero := constantTimeByteEq(em[0], 0);
|
||||
secondByteIsTwo := constantTimeByteEq(em[1], 2);
|
||||
firstByteIsZero := subtle.ConstantTimeByteEq(em[0], 0);
|
||||
secondByteIsTwo := subtle.ConstantTimeByteEq(em[1], 2);
|
||||
|
||||
// The remainder of the plaintext must be a string of non-zero random
|
||||
// octets, followed by a 0, followed by the message.
|
||||
@ -107,9 +108,9 @@ func decryptPKCS1v15(rand io.Reader, priv *PrivateKey, ciphertext []byte) (valid
|
||||
lookingForIndex = 1;
|
||||
|
||||
for i := 2; i < len(em); i++ {
|
||||
equals0 := constantTimeByteEq(em[i], 0);
|
||||
index = constantTimeSelect(lookingForIndex & equals0, i, index);
|
||||
lookingForIndex = constantTimeSelect(equals0, 0, lookingForIndex);
|
||||
equals0 := subtle.ConstantTimeByteEq(em[i], 0);
|
||||
index = subtle.ConstantTimeSelect(lookingForIndex & equals0, i, index);
|
||||
lookingForIndex = subtle.ConstantTimeSelect(equals0, 0, lookingForIndex);
|
||||
}
|
||||
|
||||
valid = firstByteIsZero & secondByteIsTwo & (^lookingForIndex & 1);
|
||||
@ -126,7 +127,7 @@ func nonZeroRandomBytes(s []byte, rand io.Reader) (err os.Error) {
|
||||
|
||||
for i := 0; i < len(s); i++ {
|
||||
for s[i] == 0 {
|
||||
_, err = rand.Read(s[i:i+1]);
|
||||
_, err = rand.Read(s[i : i+1]);
|
||||
if err != nil {
|
||||
return;
|
||||
}
|
||||
|
@ -9,6 +9,7 @@ package rsa
|
||||
|
||||
import (
|
||||
"bytes";
|
||||
"crypto/subtle";
|
||||
big "gmp";
|
||||
"hash";
|
||||
"io";
|
||||
@ -300,58 +301,6 @@ func modInverse(a, n *big.Int) (ia *big.Int) {
|
||||
return x;
|
||||
}
|
||||
|
||||
// constantTimeCompare returns 1 iff the two equal length slices, x
|
||||
// and y, have equal contents. The time taken is a function of the length of
|
||||
// the slices and is independent of the contents.
|
||||
func constantTimeCompare(x, y []byte) int {
|
||||
var v byte;
|
||||
|
||||
for i := 0; i < len(x); i++ {
|
||||
v |= x[i]^y[i];
|
||||
}
|
||||
|
||||
return constantTimeByteEq(v, 0);
|
||||
}
|
||||
|
||||
// constantTimeSelect returns a if v is 1 and b if v is 0.
|
||||
// Its behaviour is undefined if v takes any other value.
|
||||
func constantTimeSelect(v, a, b int) int {
|
||||
return ^(v-1)&a | (v-1)&b;
|
||||
}
|
||||
|
||||
// constantTimeByteEq returns 1 if a == b and 0 otherwise.
|
||||
func constantTimeByteEq(a, b uint8) int {
|
||||
x := ^(a^b);
|
||||
x &= x>>4;
|
||||
x &= x>>2;
|
||||
x &= x>>1;
|
||||
|
||||
return int(x);
|
||||
}
|
||||
|
||||
// constantTimeEq returns 1 if a == b and 0 otherwise.
|
||||
func constantTimeEq(a, b int32) int {
|
||||
x := ^(a^b);
|
||||
x &= x>>16;
|
||||
x &= x>>8;
|
||||
x &= x>>4;
|
||||
x &= x>>2;
|
||||
x &= x>>1;
|
||||
|
||||
return int(x&1);
|
||||
}
|
||||
|
||||
// constantTimeCopy copies the contents of y into x iff v == 1. If v == 0, x is left unchanged.
|
||||
// Its behaviour is undefined if v takes any other value.
|
||||
func constantTimeCopy(v int, x, y []byte) {
|
||||
xmask := byte(v - 1);
|
||||
ymask := byte(^(v - 1));
|
||||
for i := 0; i < len(x); i++ {
|
||||
x[i] = x[i] & xmask | y[i] & ymask;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// decrypt performs an RSA decryption, resulting in a plaintext integer. If a
|
||||
// random source is given, RSA blinding is used.
|
||||
func decrypt(rand io.Reader, priv *PrivateKey, c *big.Int) (m *big.Int, err os.Error) {
|
||||
@ -419,7 +368,7 @@ func DecryptOAEP(hash hash.Hash, rand io.Reader, priv *PrivateKey, ciphertext []
|
||||
// anything about this.)
|
||||
em := leftPad(m.Bytes(), k);
|
||||
|
||||
firstByteIsZero := constantTimeByteEq(em[0], 0);
|
||||
firstByteIsZero := subtle.ConstantTimeByteEq(em[0], 0);
|
||||
|
||||
seed := em[1 : hash.Size() + 1];
|
||||
db := em[hash.Size() + 1 : len(em)];
|
||||
@ -433,7 +382,7 @@ func DecryptOAEP(hash hash.Hash, rand io.Reader, priv *PrivateKey, ciphertext []
|
||||
// attacks like: J. Manger. A Chosen Ciphertext Attack on RSA Optimal
|
||||
// Asymmetric Encryption Padding (OAEP) as Standardized in PKCS #1
|
||||
// v2.0. In J. Kilian, editor, Advances in Cryptology.
|
||||
lHash2Good := constantTimeCompare(lHash, lHash2);
|
||||
lHash2Good := subtle.ConstantTimeCompare(lHash, lHash2);
|
||||
|
||||
// The remainder of the plaintext must be zero or more 0x00, followed
|
||||
// by 0x01, followed by the message.
|
||||
@ -445,11 +394,11 @@ func DecryptOAEP(hash hash.Hash, rand io.Reader, priv *PrivateKey, ciphertext []
|
||||
rest := db[hash.Size() : len(db)];
|
||||
|
||||
for i := 0; i < len(rest); i++ {
|
||||
equals0 := constantTimeByteEq(rest[i], 0);
|
||||
equals1 := constantTimeByteEq(rest[i], 1);
|
||||
index = constantTimeSelect(lookingForIndex & equals1, i, index);
|
||||
lookingForIndex = constantTimeSelect(equals1, 0, lookingForIndex);
|
||||
invalid = constantTimeSelect(lookingForIndex & ^equals0, 1, invalid);
|
||||
equals0 := subtle.ConstantTimeByteEq(rest[i], 0);
|
||||
equals1 := subtle.ConstantTimeByteEq(rest[i], 1);
|
||||
index = subtle.ConstantTimeSelect(lookingForIndex & equals1, i, index);
|
||||
lookingForIndex = subtle.ConstantTimeSelect(equals1, 0, lookingForIndex);
|
||||
invalid = subtle.ConstantTimeSelect(lookingForIndex & ^equals0, 1, invalid);
|
||||
}
|
||||
|
||||
if firstByteIsZero & lHash2Good & ^invalid & ^lookingForIndex != 1 {
|
||||
|
@ -10,7 +10,6 @@ import (
|
||||
big "gmp";
|
||||
"os";
|
||||
"testing";
|
||||
"testing/quick";
|
||||
)
|
||||
|
||||
func TestKeyGeneration(t *testing.T) {
|
||||
@ -109,101 +108,6 @@ func TestDecryptOAEP(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
type TestConstantTimeCompareStruct struct {
|
||||
a, b []byte;
|
||||
out int;
|
||||
}
|
||||
|
||||
var testConstandTimeCompareData = []TestConstantTimeCompareStruct{
|
||||
TestConstantTimeCompareStruct{[]byte{}, []byte{}, 1},
|
||||
TestConstantTimeCompareStruct{[]byte{0x11}, []byte{0x11}, 1},
|
||||
TestConstantTimeCompareStruct{[]byte{0x12}, []byte{0x11}, 0},
|
||||
}
|
||||
|
||||
func TestConstantTimeCompare(t *testing.T) {
|
||||
for i, test := range testConstandTimeCompareData {
|
||||
if r := constantTimeCompare(test.a, test.b); r != test.out {
|
||||
t.Errorf("#%d bad result (got %x, want %x)", i, r, test.out);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
type TestConstantTimeByteEqStruct struct {
|
||||
a, b uint8;
|
||||
out int;
|
||||
}
|
||||
|
||||
var testConstandTimeByteEqData = []TestConstantTimeByteEqStruct{
|
||||
TestConstantTimeByteEqStruct{0, 0, 1},
|
||||
TestConstantTimeByteEqStruct{0, 1, 0},
|
||||
TestConstantTimeByteEqStruct{1, 0, 0},
|
||||
TestConstantTimeByteEqStruct{0xff, 0xff, 1},
|
||||
TestConstantTimeByteEqStruct{0xff, 0xfe, 0},
|
||||
}
|
||||
|
||||
func ByteEq(a, b uint8) int {
|
||||
if a == b {
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
func TestConstantTimeByteEq(t *testing.T) {
|
||||
for i, test := range testConstandTimeByteEqData {
|
||||
if r := constantTimeByteEq(test.a, test.b); r != test.out {
|
||||
t.Errorf("#%d bad result (got %x, want %x)", i, r, test.out);
|
||||
}
|
||||
}
|
||||
err := quick.CheckEqual(constantTimeByteEq, ByteEq, nil);
|
||||
if err != nil {
|
||||
t.Error(err);
|
||||
}
|
||||
}
|
||||
|
||||
func Eq(a, b int32) int {
|
||||
if a == b {
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
func TestConstantTimeEq(t *testing.T) {
|
||||
err := quick.CheckEqual(constantTimeEq, Eq, nil);
|
||||
if err != nil {
|
||||
t.Error(err);
|
||||
}
|
||||
}
|
||||
|
||||
func Copy(v int, x, y []byte) []byte {
|
||||
if len(x) > len(y) {
|
||||
x = x[0:len(y)];
|
||||
} else {
|
||||
y = y[0:len(x)];
|
||||
}
|
||||
if v == 1 {
|
||||
bytes.Copy(x, y);
|
||||
}
|
||||
return x;
|
||||
}
|
||||
|
||||
func constantTimeCopyWrapper(v int, x, y []byte) []byte {
|
||||
if len(x) > len(y) {
|
||||
x = x[0:len(y)];
|
||||
} else {
|
||||
y = y[0:len(x)];
|
||||
}
|
||||
v &= 1;
|
||||
constantTimeCopy(v, x, y);
|
||||
return x;
|
||||
}
|
||||
|
||||
func TestConstantTimeCopy(t *testing.T) {
|
||||
err := quick.CheckEqual(constantTimeCopyWrapper, Copy, nil);
|
||||
if err != nil {
|
||||
t.Error(err);
|
||||
}
|
||||
}
|
||||
|
||||
// testEncryptOAEPData contains a subset of the vectors from RSA's "Test vectors for RSA-OAEP".
|
||||
var testEncryptOAEPData = []testEncryptOAEPStruct{
|
||||
// Key 1
|
||||
@ -346,6 +250,7 @@ var testEncryptOAEPData = []testEncryptOAEPStruct{
|
||||
0x3c, 0x8c, 0x90, 0xa9, 0x7b, 0xb6, 0xb6, 0x55, 0x32,
|
||||
0x84, 0xeb, 0x42, 0x9f, 0xcc,
|
||||
},
|
||||
}},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
11
src/pkg/crypto/subtle/Makefile
Normal file
11
src/pkg/crypto/subtle/Makefile
Normal file
@ -0,0 +1,11 @@
|
||||
# Copyright 2009 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.
|
||||
|
||||
include $(GOROOT)/src/Make.$(GOARCH)
|
||||
|
||||
TARG=crypto/subtle
|
||||
GOFILES=\
|
||||
constant_time.go\
|
||||
|
||||
include $(GOROOT)/src/Make.pkg
|
59
src/pkg/crypto/subtle/constant_time.go
Normal file
59
src/pkg/crypto/subtle/constant_time.go
Normal file
@ -0,0 +1,59 @@
|
||||
// Copyright 2009 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.
|
||||
|
||||
// This package implements functions that are often useful in cryptographic
|
||||
// code but require careful thought to use correctly.
|
||||
package subtle
|
||||
|
||||
// ConstantTimeCompare returns 1 iff the two equal length slices, x
|
||||
// and y, have equal contents. The time taken is a function of the length of
|
||||
// the slices and is independent of the contents.
|
||||
func ConstantTimeCompare(x, y []byte) int {
|
||||
var v byte;
|
||||
|
||||
for i := 0; i < len(x); i++ {
|
||||
v |= x[i]^y[i];
|
||||
}
|
||||
|
||||
return ConstantTimeByteEq(v, 0);
|
||||
}
|
||||
|
||||
// ConstantTimeSelect returns x if v is 1 and y if v is 0.
|
||||
// Its behavior is undefined if v takes any other value.
|
||||
func ConstantTimeSelect(v, x, y int) int {
|
||||
return ^(v-1) & x | (v-1)&y;
|
||||
}
|
||||
|
||||
// ConstantTimeByteEq returns 1 if x == x and 0 otherwise.
|
||||
func ConstantTimeByteEq(x, y uint8) int {
|
||||
z := ^(x^y);
|
||||
z &= z>>4;
|
||||
z &= z>>2;
|
||||
z &= z>>1;
|
||||
|
||||
return int(z);
|
||||
}
|
||||
|
||||
// ConstantTimeEq returns 1 if x == y and 0 otherwise.
|
||||
func ConstantTimeEq(x, y int32) int {
|
||||
z := ^(x^y);
|
||||
z &= z>>16;
|
||||
z &= z>>8;
|
||||
z &= z>>4;
|
||||
z &= z>>2;
|
||||
z &= z>>1;
|
||||
|
||||
return int(z&1);
|
||||
}
|
||||
|
||||
// ConstantTimeCopy copies the contents of y into x iff v == 1. If v == 0, x is left unchanged.
|
||||
// Its behavior is undefined if v takes any other value.
|
||||
func ConstantTimeCopy(v int, x, y []byte) {
|
||||
xmask := byte(v-1);
|
||||
ymask := byte(^(v-1));
|
||||
for i := 0; i < len(x); i++ {
|
||||
x[i] = x[i]&xmask | y[i]&ymask;
|
||||
}
|
||||
return;
|
||||
}
|
106
src/pkg/crypto/subtle/constant_time_test.go
Normal file
106
src/pkg/crypto/subtle/constant_time_test.go
Normal file
@ -0,0 +1,106 @@
|
||||
// Copyright 2009 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 subtle
|
||||
|
||||
import (
|
||||
"bytes";
|
||||
"testing";
|
||||
"testing/quick";
|
||||
)
|
||||
|
||||
type TestConstantTimeCompareStruct struct {
|
||||
a, b []byte;
|
||||
out int;
|
||||
}
|
||||
|
||||
var testConstandTimeCompareData = []TestConstantTimeCompareStruct{
|
||||
TestConstantTimeCompareStruct{[]byte{}, []byte{}, 1},
|
||||
TestConstantTimeCompareStruct{[]byte{0x11}, []byte{0x11}, 1},
|
||||
TestConstantTimeCompareStruct{[]byte{0x12}, []byte{0x11}, 0},
|
||||
}
|
||||
|
||||
func TestConstantTimeCompare(t *testing.T) {
|
||||
for i, test := range testConstandTimeCompareData {
|
||||
if r := ConstantTimeCompare(test.a, test.b); r != test.out {
|
||||
t.Errorf("#%d bad result (got %x, want %x)", i, r, test.out);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
type TestConstantTimeByteEqStruct struct {
|
||||
a, b uint8;
|
||||
out int;
|
||||
}
|
||||
|
||||
var testConstandTimeByteEqData = []TestConstantTimeByteEqStruct{
|
||||
TestConstantTimeByteEqStruct{0, 0, 1},
|
||||
TestConstantTimeByteEqStruct{0, 1, 0},
|
||||
TestConstantTimeByteEqStruct{1, 0, 0},
|
||||
TestConstantTimeByteEqStruct{0xff, 0xff, 1},
|
||||
TestConstantTimeByteEqStruct{0xff, 0xfe, 0},
|
||||
}
|
||||
|
||||
func byteEq(a, b uint8) int {
|
||||
if a == b {
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
func TestConstantTimeByteEq(t *testing.T) {
|
||||
for i, test := range testConstandTimeByteEqData {
|
||||
if r := ConstantTimeByteEq(test.a, test.b); r != test.out {
|
||||
t.Errorf("#%d bad result (got %x, want %x)", i, r, test.out);
|
||||
}
|
||||
}
|
||||
err := quick.CheckEqual(ConstantTimeByteEq, byteEq, nil);
|
||||
if err != nil {
|
||||
t.Error(err);
|
||||
}
|
||||
}
|
||||
|
||||
func eq(a, b int32) int {
|
||||
if a == b {
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
func TestConstantTimeEq(t *testing.T) {
|
||||
err := quick.CheckEqual(ConstantTimeEq, eq, nil);
|
||||
if err != nil {
|
||||
t.Error(err);
|
||||
}
|
||||
}
|
||||
|
||||
func copy(v int, x, y []byte) []byte {
|
||||
if len(x) > len(y) {
|
||||
x = x[0:len(y)];
|
||||
} else {
|
||||
y = y[0:len(x)];
|
||||
}
|
||||
if v == 1 {
|
||||
bytes.Copy(x, y);
|
||||
}
|
||||
return x;
|
||||
}
|
||||
|
||||
func constantTimeCopyWrapper(v int, x, y []byte) []byte {
|
||||
if len(x) > len(y) {
|
||||
x = x[0:len(y)];
|
||||
} else {
|
||||
y = y[0:len(x)];
|
||||
}
|
||||
v &= 1;
|
||||
ConstantTimeCopy(v, x, y);
|
||||
return x;
|
||||
}
|
||||
|
||||
func TestConstantTimeCopy(t *testing.T) {
|
||||
err := quick.CheckEqual(constantTimeCopyWrapper, copy, nil);
|
||||
if err != nil {
|
||||
t.Error(err);
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user