예제 #1
0
파일: aes.go 프로젝트: xyz12810/deblocus
func (a *AESDecrypter) XORKeyStream(dst, src []byte) {
	var out_len int
	c_out_len := (*C.int)(unsafe.Pointer(&out_len))
	c_in := (*C.uint8_t)(unsafe.Pointer(&src[0]))
	c_out := (*C.uint8_t)(unsafe.Pointer(&dst[0]))
	C.EVP_DecryptUpdate(a.ctx, c_out, c_out_len, c_in, (C.int)(len(dst)))
}
예제 #2
0
파일: ciphers.go 프로젝트: Machyne/mongo
func (ctx *decryptionCipherCtx) DecryptUpdate(input []byte) ([]byte, error) {
	outbuf := make([]byte, len(input)+ctx.BlockSize())
	outlen := C.int(len(outbuf))
	res := C.EVP_DecryptUpdate(ctx.ctx, (*C.uchar)(&outbuf[0]), &outlen,
		(*C.uchar)(&input[0]), C.int(len(input)))
	if res != 1 {
		return nil, fmt.Errorf("failed to decrypt [result %d]", res)
	}
	return outbuf[:outlen], nil
}
예제 #3
0
func (ctx *authDecryptionCipherCtx) ExtraData(aad []byte) error {
	if aad == nil {
		return nil
	}
	var outlen C.int
	if 1 != C.EVP_DecryptUpdate(ctx.ctx, nil, &outlen, (*C.uchar)(&aad[0]),
		C.int(len(aad))) {
		return errors.New("failed to add additional authenticated data")
	}
	return nil
}
예제 #4
0
func (self *CipherCtx) DecryptUpdate(dst []byte, src []byte) (int, error) {
	dstbuf := (*C.uchar)(unsafe.Pointer(&dst[0]))
	var dstlen C.int
	srcbuf := (*C.uchar)(unsafe.Pointer(&src[0]))
	srclen := len(src)
	ret := C.EVP_DecryptUpdate(self.evp_cipher_ctx, dstbuf, &dstlen, srcbuf, C.int(srclen))
	if int(ret) != 1 {
		return 0, errors.New("problem decrypting")
	}

	return int(dstlen), nil
}
예제 #5
0
func (ctx *CipherCtx) DecryptUpdate(dst []byte, src []byte) (int, error) {
	dstbuf := (*C.uchar)(unsafe.Pointer(&dst[0]))
	var dstlen C.int
	srcbuf := (*C.uchar)(unsafe.Pointer(&src[0]))
	srclen := len(src)
	ret := C.EVP_DecryptUpdate(ctx.evp_cipher_ctx, dstbuf, &dstlen, srcbuf, C.int(srclen))
	if int(ret) != 1 {
		//return 0, ErrProblemDecrypting
		return 0, sslerr.Error()
	}

	return int(dstlen), nil
}
예제 #6
0
// Open decrypts "in" using "iv" and "authData" and append the result to "dst"
func (g stupidGCM) Open(dst, iv, in, authData []byte) ([]byte, error) {
	if len(iv) != ivLen {
		log.Panicf("Only %d-byte IVs are supported", ivLen)
	}
	if len(in) <= tagLen {
		log.Panic("Input data too short")
	}
	buf := make([]byte, len(in)-tagLen)
	ciphertext := in[:len(in)-tagLen]
	tag := in[len(in)-tagLen:]

	// https://wiki.openssl.org/index.php/EVP_Authenticated_Encryption_and_Decryption#Authenticated_Encryption_using_GCM_mode

	// Create scratch space "context"
	ctx := C.EVP_CIPHER_CTX_new()
	if ctx == nil {
		log.Panic("EVP_CIPHER_CTX_new failed")
	}

	// Set cipher to AES-256
	if C.EVP_DecryptInit_ex(ctx, C.EVP_aes_256_gcm(), nil, nil, nil) != 1 {
		log.Panic("EVP_DecryptInit_ex I failed")
	}

	// Use 16-byte IV
	if C.EVP_CIPHER_CTX_ctrl(ctx, C.EVP_CTRL_GCM_SET_IVLEN, ivLen, nil) != 1 {
		log.Panic("EVP_CIPHER_CTX_ctrl EVP_CTRL_GCM_SET_IVLEN failed")
	}

	// Set key and IV
	if C.EVP_DecryptInit_ex(ctx, nil, nil, (*C.uchar)(&g.key[0]), (*C.uchar)(&iv[0])) != 1 {
		log.Panic("EVP_DecryptInit_ex II failed")
	}

	// Set expected GMAC tag
	if C.EVP_CIPHER_CTX_ctrl(ctx, C.EVP_CTRL_GCM_SET_TAG, tagLen, (unsafe.Pointer)(&tag[0])) != 1 {
		log.Panic("EVP_CIPHER_CTX_ctrl failed")
	}

	// Provide authentication data
	var resultLen C.int
	if C.EVP_DecryptUpdate(ctx, nil, &resultLen, (*C.uchar)(&authData[0]), C.int(len(authData))) != 1 {
		log.Panic("EVP_DecryptUpdate authData failed")
	}
	if int(resultLen) != len(authData) {
		log.Panicf("Unexpected length %d", resultLen)
	}

	// Decrypt "ciphertext" into "buf"
	if C.EVP_DecryptUpdate(ctx, (*C.uchar)(&buf[0]), &resultLen, (*C.uchar)(&ciphertext[0]), C.int(len(ciphertext))) != 1 {
		log.Panic("EVP_DecryptUpdate failed")
	}
	if int(resultLen) != len(ciphertext) {
		log.Panicf("Unexpected length %d", resultLen)
	}

	// Check GMAC
	dummy := make([]byte, 16)
	res := C.EVP_DecryptFinal_ex(ctx, (*C.uchar)(&dummy[0]), &resultLen)
	if resultLen != 0 {
		log.Panicf("Unexpected length %d", resultLen)
	}

	// Free scratch space
	C.EVP_CIPHER_CTX_free(ctx)

	if res != 1 {
		return nil, fmt.Errorf("stupidgcm: message authentication failed")
	}

	return append(dst, buf...), nil
}