Skip to content

Instantly share code, notes, and snippets.

@husobee
Created October 24, 2018 20:35
Show Gist options
  • Save husobee/1f0b43ffbf0fe1aeffedc4621cc60f90 to your computer and use it in GitHub Desktop.
Save husobee/1f0b43ffbf0fe1aeffedc4621cc60f90 to your computer and use it in GitHub Desktop.
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