mirror of
https://github.com/go-i2p/go-i2p.git
synced 2025-07-17 13:54:33 -04:00
migrate hmac to crypto/hmac, I swear we do everything the hard way...
This commit is contained in:
@ -32,10 +32,10 @@ please keep up with these changes, as they will not be backward compatible and r
|
|||||||
- [ ] RSA_SHA512_4096
|
- [ ] RSA_SHA512_4096
|
||||||
- [ ] Ed25519
|
- [ ] Ed25519
|
||||||
- [ ] Red25519
|
- [ ] Red25519
|
||||||
- [ ] ElGamal
|
- [X] ElGamal
|
||||||
- [ ] AES256
|
- [X] AES256
|
||||||
- [X] X25519
|
- [X] X25519
|
||||||
- [ ] ChaCha20/Poly1305
|
- [X] ChaCha20/Poly1305
|
||||||
- [ ] Elligator2
|
- [ ] Elligator2
|
||||||
- [ ] HKDF
|
- [ ] HKDF
|
||||||
- [X] HMAC
|
- [X] HMAC
|
||||||
|
@ -1,44 +1,27 @@
|
|||||||
package hmac
|
package hmac
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"crypto/hmac"
|
||||||
"crypto/md5"
|
"crypto/md5"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
|
||||||
IPAD = byte(0x36)
|
|
||||||
OPAD = byte(0x5C)
|
|
||||||
)
|
|
||||||
|
|
||||||
type (
|
type (
|
||||||
HMACKey [32]byte
|
HMACKey [32]byte
|
||||||
HMACDigest [16]byte
|
HMACDigest [16]byte
|
||||||
)
|
)
|
||||||
|
|
||||||
func (hk HMACKey) xor(p byte) (i []byte) {
|
// I2PHMAC computes HMAC-MD5 using the provided key and data
|
||||||
i = make([]byte, 64)
|
|
||||||
for idx, b := range hk {
|
|
||||||
i[idx] = b ^ p
|
|
||||||
}
|
|
||||||
c := 32
|
|
||||||
for c > 0 {
|
|
||||||
c--
|
|
||||||
i[c+32] = p
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// do i2p hmac
|
|
||||||
func I2PHMAC(data []byte, k HMACKey) (d HMACDigest) {
|
func I2PHMAC(data []byte, k HMACKey) (d HMACDigest) {
|
||||||
buff := make([]byte, 64+len(data))
|
// Create a new HMAC instance using MD5 hash and our key
|
||||||
ip := k.xor(IPAD)
|
mac := hmac.New(md5.New, k[:])
|
||||||
copy(buff, ip)
|
|
||||||
copy(buff[64:], data)
|
|
||||||
h := md5.Sum(buff)
|
|
||||||
|
|
||||||
buff = make([]byte, 96)
|
// Write data to HMAC
|
||||||
copy(buff, k.xor(OPAD))
|
mac.Write(data)
|
||||||
copy(buff[64:], h[:])
|
|
||||||
// go zeros slices so we do not have to zero
|
// Calculate the HMAC and extract the digest
|
||||||
d = md5.Sum(buff)
|
digest := mac.Sum(nil)
|
||||||
|
|
||||||
|
// Copy to our fixed-size return type
|
||||||
|
copy(d[:], digest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user