Ejemplo n.º 1
0
Archivo: aes.go Proyecto: runcom/gossl
//Construct a new instance. see crypto/aes
// returning a block cipher that satisfies the crypto/cipher.Block interface
func NewCipher(key []byte) (cipher.Block, error) {
	aes := new(aesKey)
	key_p := pointerFromBytes(key)
	key_bits := C.int(len(key) * 8)
	if ret := C.AES_set_encrypt_key(key_p, key_bits, &aes._aes_encrypt_key); ret != 0 {
		// in case the error is something deep in OpenSSL land
		if err := sslerr.Error(); err != nil {
			return aes, err
		}
		if ret == -1 {
			return aes, errorInvalidKey
		}
		if ret == -2 {
			return aes, errorWrongSizeKey
		}
		// standard fallback error
		return aes, errorSettingKey
	}
	if ret := C.AES_set_decrypt_key(key_p, key_bits, &aes._aes_decrypt_key); ret != 0 {
		// in case the error is something deep in OpenSSL land
		if err := sslerr.Error(); err != nil {
			return aes, err
		}
		if ret == -1 {
			return aes, errorInvalidKey
		}
		if ret == -2 {
			return aes, errorWrongSizeKey
		}
		// standard fallback error
		return aes, errorSettingKey
	}
	return aes, nil
}
Ejemplo n.º 2
0
// Create a new AES block cipher.
// The key must be 16, 24, or 32 bytes long.
func NewAES(key []byte) cipher.Block {
	a := &aes{}
	if C.AES_set_encrypt_key((*C.uchar)(&key[0]), C.int(len(key)*8), &a.key) != 0 {
		panic("C.AES_set_encrypt_key failed")
	}
	return a
}
Ejemplo n.º 3
0
// Create a new stream cipher based on AES in counter mode.
// The key must be 16, 24, or 32 bytes long.
func newAESCTR(key []byte) cipher.Stream {
	a := &aesctr{}
	if C.AES_set_encrypt_key((*C.uchar)(&key[0]), C.int(len(key)*8), &a.key) != 0 {
		panic("C.AES_set_encrypt_key failed")
	}
	// counter automatically starts at 0
	a.idx = blocksize // need a fresh block first time
	return a
}
Ejemplo n.º 4
0
//Construct a new instance. see crypto/aes
func NewCipher(key []byte) (cipher.Block, error) {
	aes := new(AESKey)
	key_p := pointerFromBytes(key)
	key_bits := C.int(len(key) * 8)
	if C.AES_set_encrypt_key(key_p, key_bits, &aes._aes_encrypt_key) != 0 {
		return aes, errors.New("problem setting key")
	}
	if C.AES_set_decrypt_key(key_p, key_bits, &aes._aes_decrypt_key) != 0 {
		return aes, errors.New("problem setting key")
	}
	return aes, nil
}