1
0
mirror of https://github.com/golang/go synced 2024-11-25 07:17:56 -07:00

crypto/rsa: add EncryptNoPadding/DecryptNoPadding

This commit is contained in:
fusion32 2023-10-25 00:54:13 -03:00
parent f09db2bb93
commit 26624a44af
2 changed files with 333 additions and 0 deletions

View File

@ -495,6 +495,25 @@ func encrypt(pub *PublicKey, plaintext []byte) ([]byte, error) {
return bigmod.NewNat().ExpShort(m, e, N).Bytes(N), nil return bigmod.NewNat().ExpShort(m, e, N).Bytes(N), nil
} }
// EncryptNoPadding encrypts plaintext using no padding scheme.
//
// The given plaintext will be left-padded with zeros if it's smaller than
// pub.Size(). This needs to be considered when using [DecryptNoPadding]
// to retrieve the original message.
//
// WARNING: This should only be used to implement cryptographically sound
// padding schemes in the application code. Encrypting user data directly
// with this function is insecure.
func EncryptNoPadding(pub *PublicKey, plaintext []byte) ([]byte, error) {
if err := checkPub(pub); err != nil {
return nil, err
}
if len(plaintext) > pub.Size() {
return nil, ErrMessageTooLong
}
return encrypt(pub, plaintext)
}
// EncryptOAEP encrypts the given message with RSA-OAEP. // EncryptOAEP encrypts the given message with RSA-OAEP.
// //
// OAEP is parameterised by a hash function that is used as a random oracle. // OAEP is parameterised by a hash function that is used as a random oracle.
@ -695,6 +714,20 @@ func decrypt(priv *PrivateKey, ciphertext []byte, check bool) ([]byte, error) {
return m.Bytes(N), nil return m.Bytes(N), nil
} }
// DecryptNoPadding decrypts ciphertext using no padding scheme.
//
// The returned plaintext (assuming no errors) will be left-padded with
// zeros to match priv.Size() bytes. See [EncryptNoPadding] for details.
func DecryptNoPadding(priv *PrivateKey, ciphertext []byte) ([]byte, error) {
if err := checkPub(&priv.PublicKey); err != nil {
return nil, err
}
if len(ciphertext) > priv.Size() {
return nil, ErrDecryption
}
return decrypt(priv, ciphertext, noCheck)
}
// DecryptOAEP decrypts ciphertext using RSA-OAEP. // DecryptOAEP decrypts ciphertext using RSA-OAEP.
// //
// OAEP is parameterised by a hash function that is used as a random oracle. // OAEP is parameterised by a hash function that is used as a random oracle.

View File

@ -880,3 +880,303 @@ var testEncryptOAEPData = []testEncryptOAEPStruct{
}, },
}, },
} }
func TestEncryptDecryptNoPadding(t *testing.T) {
testData := []struct {
keyBits int
messages [][]byte
}{
{
keyBits: 1024,
messages: [][]byte{
{
0x48, 0xFE, 0x33, 0x2D, 0xA2, 0xFD, 0xA4, 0x39,
0x4A, 0x4D, 0x5B, 0x10, 0x69, 0x79, 0x66, 0x55,
},
{
0xF9, 0xA4, 0xCE, 0x85, 0x20, 0xE2, 0xA3, 0xA5,
0x7D, 0xAF, 0x66, 0x71, 0x03, 0x55, 0xA5, 0xA7,
0x3B, 0x43, 0x69, 0x2A, 0xA1, 0x63, 0x71, 0x97,
0xCE, 0x22, 0xEF, 0x27, 0x8E, 0xEF, 0xE8, 0x19,
},
{
0x1A, 0x69, 0xC2, 0xBE, 0x7E, 0x3C, 0x55, 0xA1,
0xF8, 0xAD, 0x6C, 0xE0, 0xF8, 0x94, 0xCF, 0xB8,
0xF5, 0x60, 0xE2, 0xD5, 0x38, 0x24, 0x3B, 0xBD,
0x07, 0xA9, 0x99, 0x96, 0x97, 0x82, 0x6A, 0x8A,
0x09, 0xD7, 0x0C, 0x50, 0x79, 0x4F, 0x82, 0x4C,
0x6B, 0x16, 0x7B, 0x77, 0xD7, 0x32, 0x99, 0x03,
},
{
0x0B, 0xAE, 0x77, 0x84, 0xDD, 0x49, 0x31, 0x19,
0x75, 0xC7, 0x9D, 0x96, 0xC6, 0x44, 0xB9, 0x29,
0xB4, 0xA0, 0xE9, 0x84, 0x32, 0x39, 0xDF, 0xFD,
0x6F, 0xCC, 0x71, 0xDE, 0x13, 0x6A, 0xD7, 0xA3,
0xD4, 0xE6, 0x1E, 0x03, 0xF5, 0xC5, 0xA7, 0x3F,
0x00, 0xD9, 0x4A, 0xE4, 0xE0, 0xAA, 0x61, 0x37,
0x88, 0xD0, 0x9C, 0x18, 0x47, 0x74, 0xD9, 0xEB,
0xE6, 0x1F, 0xED, 0xBC, 0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x3E, 0x95, 0x57,
0x57, 0x5E, 0xD8, 0x86, 0x17, 0xE9, 0xE5, 0x06,
0x1E, 0xA9, 0xFE, 0x66, 0x5F, 0x14, 0xB6, 0x8C,
0x73, 0xA6, 0x7A, 0x47, 0x47, 0x7E, 0xD5, 0x1C,
0xE3, 0x01, 0xB1, 0x2C, 0x11, 0x02, 0x79, 0xE7,
0x86, 0x19, 0x40, 0xA4, 0x6D, 0xF0, 0x9E, 0x4B,
0x5B, 0x1B, 0xAF, 0xAD, 0xA8, 0x31, 0xC1, 0x2B,
0x6D, 0xE4, 0x84, 0x23, 0x66, 0xB3, 0xD0, 0xA2,
0xE2, 0x21, 0xD4, 0x99, 0xFE, 0xD7, 0xA4, 0xD1,
0xF0, 0xC5, 0x7F, 0x42, 0x8F, 0xC0, 0xFF, 0x7C,
0xC9, 0xF6, 0xEB, 0xA0, 0xD2, 0x53, 0xDE, 0x3A,
0xB6, 0x27, 0x0B, 0x01, 0x0F, 0xAE, 0xD2, 0x19,
0x5A, 0xAB, 0x6D, 0xD6, 0xDF, 0x25, 0x0E, 0x0D,
0x39, 0xFD, 0x9D, 0x12, 0xAF, 0x49, 0x4F, 0xD3,
0x7F, 0xA5, 0xC1, 0xEC, 0xEB, 0x6A, 0x9D, 0x77,
0xBF, 0x54, 0xC3, 0x7E, 0xF1, 0xFE, 0xF0, 0xB1,
},
},
},
}
zeroPadded := func(keyBytes int, message []byte) []byte {
result := make([]byte, keyBytes)
offset := keyBytes - len(message)
copy(result[offset:], message)
return result
}
for i, test := range testData {
priv, err := GenerateKey(rand.Reader, test.keyBits)
if err != nil {
t.Errorf("GenerateKey(%d): %v", test.keyBits, err)
}
keyBytes := test.keyBits / 8
for j, message := range test.messages {
enc, err := EncryptNoPadding(&priv.PublicKey, message)
if err != nil {
t.Errorf("EncryptNoPadding: %v", err)
continue
}
dec, err := DecryptNoPadding(priv, enc)
if err != nil {
t.Errorf("DecryptNoPadding: %v", err)
continue
}
zMessage := zeroPadded(keyBytes, message)
if !bytes.Equal(dec, zMessage) {
t.Errorf("#%d,%d: round trip %q -> %q", i, j, zMessage, dec)
}
}
}
}