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 elgamal implements ElGamal encryption, suitable for OpenPGP, as specified in "A Public-Key Cryptosystem and a Signature Scheme Based on Discrete Logarithms," IEEE Transactions on Information Theory, v. IT-31, n. 4, 1985, pp. 469-472. This form of ElGamal embeds PKCS#1 v1.5 padding, which may make it unsuitable for other protocols. RSA should be used in preference in any case.
package elgamal // import "golang.org/x/crypto/openpgp/elgamal"

import (
	
	
	
	
	
)
PublicKey represents an ElGamal public key.
type PublicKey struct {
	G, P, Y *big.Int
}
PrivateKey represents an ElGamal private key.
type PrivateKey struct {
	PublicKey
	X *big.Int
}
Encrypt encrypts the given message to the given public key. The result is a pair of integers. Errors can result from reading random, or because msg is too large to be encrypted to the public key.
func ( io.Reader,  *PublicKey,  []byte) (,  *big.Int,  error) {
	 := (.P.BitLen() + 7) / 8
	if len() > -11 {
		 = errors.New("elgamal: message too long")
		return
	}
EM = 0x02 || PS || 0x00 || M
	 := make([]byte, -1)
	[0] = 2
	,  := [1:len()-len()-1], [len()-len():]
	 = nonZeroRandomBytes(, )
	if  != nil {
		return
	}
	[len()-len()-1] = 0
	copy(, )

	 := new(big.Int).SetBytes()

	,  := rand.Int(, .P)
	if  != nil {
		return
	}

	 = new(big.Int).Exp(.G, , .P)
	 := new(big.Int).Exp(.Y, , .P)
	 = .Mul(, )
	.Mod(, .P)

	return
}
Decrypt takes two integers, resulting from an ElGamal encryption, and returns the plaintext of the message. An error can result only if the ciphertext is invalid. Users should keep in mind that this is a padding oracle and thus, if exposed to an adaptive chosen ciphertext attack, can be used to break the cryptosystem. See ``Chosen Ciphertext Attacks Against Protocols Based on the RSA Encryption Standard PKCS #1'', Daniel Bleichenbacher, Advances in Cryptology (Crypto '98),
func ( *PrivateKey, ,  *big.Int) ( []byte,  error) {
	 := new(big.Int).Exp(, .X, .P)
	if .ModInverse(, .P) == nil {
		return nil, errors.New("elgamal: invalid private key")
	}
	.Mul(, )
	.Mod(, .P)
	 := .Bytes()

	 := subtle.ConstantTimeByteEq([0], 2)
The remainder of the plaintext must be a string of non-zero random octets, followed by a 0, followed by the message. lookingForIndex: 1 iff we are still looking for the zero. index: the offset of the first zero byte.
	var ,  int
	 = 1

	for  := 1;  < len(); ++ {
		 := subtle.ConstantTimeByteEq([], 0)
		 = subtle.ConstantTimeSelect(&, , )
		 = subtle.ConstantTimeSelect(, 0, )
	}

	if  != 1 ||  != 0 ||  < 9 {
		return nil, errors.New("elgamal: decryption error")
	}
	return [+1:], nil
}
nonZeroRandomBytes fills the given slice with non-zero random octets.
func ( []byte,  io.Reader) ( error) {
	_,  = io.ReadFull(, )
	if  != nil {
		return
	}

	for  := 0;  < len(); ++ {
		for [] == 0 {
			_,  = io.ReadFull(, [:+1])
			if  != nil {
				return
			}
		}
	}

	return