poly1305
Poly1305 is a one-time authenticator originally designed by D. J. Bernstein. Poly1305 takes a 32-byte one-time key and a message and produces a 16-byte tag. It can be used to verify the data integrity and the authenticity of a message.
This module provide
poly1305
Installation
You can install this module from github.
v install https://github.com/blackshirt/poly1305
Usage
-
Provide your secret keys with length 32 bytes, you can generate its randomly from
crypto.rand
or you can use chacha20.otk_key_gen()
for generating one-time key intended for poly1305 keygen. If you want to use chacha20.otk_key_gen()
function, you should install chacha20
module to your path. Its available at chacha20 -
If you would use
chacha20.otk_key_gen
provide its with nonce bytes, with length 12 or 24 bytes. -
And then generates one-time key with
chacha20.otk_key_gen
. -
Create Poly1305 mac instance with generated one-time key. If you are not going to use
chacha20.otk_key_gen
to generate key, make sure your key random enought to create poly1305 (feeds it with crypto.rand
) -
feeds your poly1305 instance with messages you want to be authenticated by calling
write
method. -
And then, call finalize to produce your 16 byte tag associated with your messages.
module main
import crypto.rand
import blackshirt.chacha20
import blackshirt.poly1305
fn main() {
// messages to auth
msg := 'Hello my Girls.....!!'.bytes()
// provides key with length 32 bytes
key := rand.read(32) ?
// provides your nonce with length 12 or 24 bytes
nonce := rand.read(12) ?
// and then create one-time key for poly1305
otk := chacha20.otk_key_gen(key, nonce)
// create new poly305 mac
mut poly := poly1305.new_with_key(otk) ?
// or if you dont want using `chacha20.otk_key_gen`, you can directly
// using key to instantiate poly1305 mac
// mut poly := poly1305.new_with_key(key) ?
// write message to mac
poly.write(msg)
// and then call finalize to produce 16 byte tag
tag := poly.finalize()
res := poly1305.verify_mac(tag, msg, otk) ?
if res {
println('Verified')
} else {
println('Not verified')
}
}