func (s *state) update() { // generate a local line Key if s.localLineKey == nil { s.localLineKey, _ = generateKey() } // make local token if s.localToken == nil && s.localLineKey != nil { s.localToken = new(cipherset.Token) sha := sha256.Sum256(s.localLineKey.Public()[:16]) copy((*s.localToken)[:], sha[:16]) } // make remote token if s.remoteToken == nil && s.remoteLineKey != nil { s.remoteToken = new(cipherset.Token) sha := sha256.Sum256(s.remoteLineKey.Public()[:16]) copy((*s.remoteToken)[:], sha[:16]) } // generate line keys if s.localToken != nil && s.remoteToken != nil && (s.lineEncryptionKey == nil || s.lineDecryptionKey == nil) { sharedKey := ecdh.ComputeShared( secp160r1.P160(), s.remoteLineKey.pub.x, s.remoteLineKey.pub.y, s.localLineKey.prv.d) sha := sha256.New() sha.Write(sharedKey) sha.Write(s.localLineKey.Public()) sha.Write(s.remoteLineKey.Public()) s.lineEncryptionKey = fold(sha.Sum(nil), 16) sha.Reset() sha.Write(sharedKey) sha.Write(s.remoteLineKey.Public()) sha.Write(s.localLineKey.Public()) s.lineDecryptionKey = fold(sha.Sum(nil), 16) } }
func (c *cipher) DecryptMessage(localKey, remoteKey cipherset.Key, p []byte) ([]byte, error) { if len(p) < 21+4+4 { return nil, cipherset.ErrInvalidMessage } var ( ctLen = len(p) - (21 + 4 + 4) out = make([]byte, ctLen) cs1aLocalKey, _ = localKey.(*key) cs1aRemoteKey, _ = remoteKey.(*key) remoteLineKey = p[:21] iv = p[21 : 21+4] ciphertext = p[21+4 : 21+4+ctLen] mac = p[21+4+ctLen:] ) if cs1aLocalKey == nil || cs1aRemoteKey == nil { return nil, cipherset.ErrInvalidState } { // verify mac macKey := ecdh.ComputeShared(secp160r1.P160(), cs1aRemoteKey.pub.x, cs1aRemoteKey.pub.y, cs1aLocalKey.prv.d) macKey = append(macKey, iv...) h := hmac.New(sha256.New, macKey) h.Write(p[:21+4+ctLen]) if subtle.ConstantTimeCompare(mac, fold(h.Sum(nil), 4)) != 1 { return nil, cipherset.ErrInvalidMessage } } { // descrypt inner ephemX, ephemY := eccp.Unmarshal(secp160r1.P160(), remoteLineKey) if ephemX == nil || ephemY == nil { return nil, cipherset.ErrInvalidMessage } shared := ecdh.ComputeShared(secp160r1.P160(), ephemX, ephemY, cs1aLocalKey.prv.d) if shared == nil { return nil, cipherset.ErrInvalidMessage } aharedSum := sha256.Sum256(shared) aesKey := fold(aharedSum[:], 16) if aesKey == nil { return nil, cipherset.ErrInvalidMessage } aesBlock, err := aes.NewCipher(aesKey) if err != nil { return nil, cipherset.ErrInvalidMessage } var aesIv [16]byte copy(aesIv[:], iv) aes := Cipher.NewCTR(aesBlock, aesIv[:]) if aes == nil { return nil, cipherset.ErrInvalidMessage } aes.XORKeyStream(out, ciphertext) } return out, nil }
func (s *state) EncryptMessage(in []byte) ([]byte, error) { var ( ctLen = len(in) out = bufpool.New().SetLen(21 + 4 + ctLen + 4) raw = out.RawBytes() ) if !s.CanEncryptMessage() { panic("unable to encrypt message") } // copy public senderLineKey copy(raw[:21], s.localLineKey.Public()) // copy the nonce _, err := io.ReadFull(rand.Reader, raw[21:21+4]) if err != nil { return nil, err } { // encrypt inner shared := ecdh.ComputeShared( secp160r1.P160(), s.remoteKey.pub.x, s.remoteKey.pub.y, s.localLineKey.prv.d) if shared == nil { return nil, cipherset.ErrInvalidMessage } aharedSum := sha256.Sum256(shared) aesKey := fold(aharedSum[:], 16) if aesKey == nil { return nil, cipherset.ErrInvalidMessage } aesBlock, err := aes.NewCipher(aesKey) if err != nil { return nil, err } var aesIv [16]byte copy(aesIv[:], raw[21:21+4]) aes := Cipher.NewCTR(aesBlock, aesIv[:]) if aes == nil { return nil, cipherset.ErrInvalidMessage } aes.XORKeyStream(raw[21+4:21+4+ctLen], in) } { // compute HMAC macKey := ecdh.ComputeShared(secp160r1.P160(), s.remoteKey.pub.x, s.remoteKey.pub.y, s.localKey.prv.d) macKey = append(macKey, raw[21:21+4]...) h := hmac.New(sha256.New, macKey) h.Write(raw[:21+4+ctLen]) sum := h.Sum(nil) copy(raw[21+4+ctLen:], fold(sum, 4)) } out.SetLen(21 + 4 + ctLen + 4) return out.Get(nil), nil }
func (c *cipher) DecryptHandshake(localKey cipherset.Key, p []byte) (cipherset.Handshake, error) { if len(p) < 21+4+4 { return nil, cipherset.ErrInvalidMessage } var ( ctLen = len(p) - (21 + 4 + 4) out = bufpool.New() cs1aLocalKey, _ = localKey.(*key) remoteKey *key remoteLineKey *key hshake *handshake remoteLineKeyData = p[:21] iv = p[21 : 21+4] ciphertext = p[21+4 : 21+4+ctLen] mac = p[21+4+ctLen:] ) if cs1aLocalKey == nil { return nil, cipherset.ErrInvalidState } { // decrypt inner ephemX, ephemY := eccp.Unmarshal(secp160r1.P160(), remoteLineKeyData) if ephemX == nil || ephemY == nil { return nil, cipherset.ErrInvalidMessage } shared := ecdh.ComputeShared(secp160r1.P160(), ephemX, ephemY, cs1aLocalKey.prv.d) if shared == nil { return nil, cipherset.ErrInvalidMessage } aharedSum := sha256.Sum256(shared) aesKey := fold(aharedSum[:], 16) if aesKey == nil { return nil, cipherset.ErrInvalidMessage } aesBlock, err := aes.NewCipher(aesKey) if err != nil { return nil, cipherset.ErrInvalidMessage } var aesIv [16]byte copy(aesIv[:], iv) aes := Cipher.NewCTR(aesBlock, aesIv[:]) if aes == nil { return nil, cipherset.ErrInvalidMessage } out.SetLen(ctLen) aes.XORKeyStream(out.RawBytes(), ciphertext) remoteLineKey = &key{} remoteLineKey.pub.x, remoteLineKey.pub.y = ephemX, ephemY } { // decode inner inner, err := lob.Decode(out) if err != nil { return nil, cipherset.ErrInvalidMessage } at, hasAt := inner.Header().GetUint32("at") if !hasAt { return nil, cipherset.ErrInvalidMessage } delete(inner.Header().Extra, "at") parts, err := cipherset.PartsFromHeader(inner.Header()) if err != nil { return nil, cipherset.ErrInvalidMessage } if inner.BodyLen() != 21 { return nil, cipherset.ErrInvalidMessage } remoteKey = &key{} remoteKey.pub.x, remoteKey.pub.y = eccp.Unmarshal(secp160r1.P160(), inner.Body(nil)) if !remoteKey.CanEncrypt() { return nil, cipherset.ErrInvalidMessage } hshake = &handshake{} hshake.at = at hshake.key = remoteKey hshake.lineKey = remoteLineKey hshake.parts = parts } { // verify mac var nonce [16]byte copy(nonce[:], iv) macKey := ecdh.ComputeShared(secp160r1.P160(), remoteKey.pub.x, remoteKey.pub.y, cs1aLocalKey.prv.d) macKey = append(macKey, nonce[:]...) h := hmac.New(sha256.New, macKey) h.Write(p[:21+4+ctLen]) if subtle.ConstantTimeCompare(mac, fold(h.Sum(nil), 4)) != 1 { return nil, cipherset.ErrInvalidMessage } } return hshake, nil }