Skip to content

Instantly share code, notes, and snippets.

@husobee

husobee/clww16.go

Created Oct 24, 2018
Embed
What would you like to do?
CLWW16 toy project
package main
import (
"crypto/hmac"
"crypto/sha256"
"fmt"
)
func main() {
fmt.Println("Example CLWW 2016 Implementation")
var (
M uint16 = 3
// input, what we are encrypting
m1 byte = 44
m2 byte = 42
// output, what we can compare
u1 uint16
u2 uint16
// temp calculation binary
b1 byte
b2 byte
)
// Encrypt
// for each bit in our inputs
for i := 7; i >= 0; i-- {
// our PRF is hmac sha256
mac1 := hmac.New(sha256.New, []byte("secretkey"))
mac2 := hmac.New(sha256.New, []byte("secretkey"))
// shift to the right location
b1 = m1 >> byte(i)
b2 = m2 >> byte(i)
// do our prf
mac1.Write([]byte{b1 << byte(i)})
tmp1 := mac1.Sum(nil)[0]
u1 = (u1 << 1) | (uint16(tmp1)+uint16((b1&1)))%M
mac2.Write([]byte{b2 << byte(i)})
tmp2 := mac2.Sum(nil)[0]
u2 = (u2 << 1) | (uint16(tmp2)+uint16((b2&1)))%M
}
fmt.Printf("u1: %b\n", u1)
fmt.Printf("u2: %b\n", u2)
var equal bool = true
// compare
for i := 7; i >= 0; i-- {
// find the first bit that doesn't match
uu1 := u1 >> uint8(i) & 1
uu2 := u2 >> uint8(i) & 1
if uu1 != uu2 {
if (uu1+1)%M == uu2 {
equal = false
fmt.Println("m1 is less than m2")
break
}
fmt.Println("m2 is less than m1")
break
}
}
if equal {
fmt.Println("m1 is equal to m2")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment