Add wrapper
This commit is contained in:
		
							parent
							
								
									7729776e58
								
							
						
					
					
						commit
						d79b3830fb
					
				
							
								
								
									
										64
									
								
								src/romulus_m.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										64
									
								
								src/romulus_m.go
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,64 @@ | |||||||
|  | package src | ||||||
|  | 
 | ||||||
|  | import ( | ||||||
|  | 	"crypto/rand" | ||||||
|  | 	"errors" | ||||||
|  | ) | ||||||
|  | 
 | ||||||
|  | const abytes int = 16 | ||||||
|  | const keysize int = 16 | ||||||
|  | const noncesize int = 16 | ||||||
|  | 
 | ||||||
|  | func Encrypt(key []byte, plaintext []byte, additionalData []byte) ([]byte, error) { | ||||||
|  | 
 | ||||||
|  | 	cipher := make([]byte, len(plaintext)+abytes+noncesize) | ||||||
|  | 	var cipherlen uint64 | ||||||
|  | 	nonce := genNonce() | ||||||
|  | 	ret := romulus_m_encrypt( | ||||||
|  | 		cipher[noncesize:], | ||||||
|  | 		&cipherlen, | ||||||
|  | 		plaintext, | ||||||
|  | 		(uint64)(len(plaintext)), | ||||||
|  | 		additionalData, | ||||||
|  | 		(uint64)(len(additionalData)), | ||||||
|  | 		nil, | ||||||
|  | 		nonce, | ||||||
|  | 		key[:keysize], | ||||||
|  | 	) | ||||||
|  | 
 | ||||||
|  | 	if ret != 0 { | ||||||
|  | 		return nil, errors.New("Failed to decrypt") | ||||||
|  | 	} | ||||||
|  | 	copy(cipher, nonce) | ||||||
|  | 	return cipher[:cipherlen], nil | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | func Decrypt(key []byte, ciphertext []byte, additionalData []byte) ([]byte, bool, error) { | ||||||
|  | 
 | ||||||
|  | 	plaintext := make([]byte, len(ciphertext)) | ||||||
|  | 	var plaintextLen uint64 | ||||||
|  | 	ret := romulus_m_decrypt( | ||||||
|  | 		plaintext, | ||||||
|  | 		&plaintextLen, | ||||||
|  | 		nil, | ||||||
|  | 		ciphertext[noncesize:], | ||||||
|  | 		(uint64)(len(ciphertext)-noncesize), | ||||||
|  | 		additionalData, | ||||||
|  | 		(uint64)(len(additionalData)), | ||||||
|  | 		ciphertext[:noncesize], | ||||||
|  | 		key[:keysize], | ||||||
|  | 	) | ||||||
|  | 	if ret == -1 { | ||||||
|  | 		return nil, false, errors.New("Failed to authenticate") | ||||||
|  | 	} else if ret != 0 { | ||||||
|  | 		return nil, false, errors.New("Failed to decrypt") | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	return plaintext, true, nil | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | func genNonce() []byte { | ||||||
|  | 	buf := make([]byte, noncesize) | ||||||
|  | 	rand.Read(buf) | ||||||
|  | 	return buf | ||||||
|  | } | ||||||
| @ -1,5 +1,7 @@ | |||||||
| package src | package src | ||||||
| 
 | 
 | ||||||
|  | // Converted to go with C2GO, tweaks by 51m0n - 2022. | ||||||
|  | 
 | ||||||
| /* | /* | ||||||
|  * Date: 05 May 2021 |  * Date: 05 May 2021 | ||||||
|  * Contact: Romulus Team (Mustafa Khairallah - mustafa.khairallah@ntu.edu.sg) |  * Contact: Romulus Team (Mustafa Khairallah - mustafa.khairallah@ntu.edu.sg) | ||||||
| @ -275,6 +277,7 @@ func romulus_m_encrypt(c []byte, clen *uint64, m []byte, mlen uint64, ad []byte, | |||||||
| 	var xlen uint64 | 	var xlen uint64 | ||||||
| 
 | 
 | ||||||
| 	N = npub | 	N = npub | ||||||
|  | 	// sm | ||||||
| 	mstart := m[:] | 	mstart := m[:] | ||||||
| 	cstart := c[:] | 	cstart := c[:] | ||||||
| 	n = 16 | 	n = 16 | ||||||
|  | |||||||
| @ -1,5 +1,7 @@ | |||||||
| package src | package src | ||||||
| 
 | 
 | ||||||
|  | // Converted to go with C2GO, tweaks by 51m0n - 2022. | ||||||
|  | 
 | ||||||
| /* | /* | ||||||
|  * Date: 11 December 2015 |  * Date: 11 December 2015 | ||||||
|  * Contact: Thomas Peyrin - thomas.peyrin@gmail.com |  * Contact: Thomas Peyrin - thomas.peyrin@gmail.com | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user