#deserialize question cookie net/http

42 messages · Page 1 of 1 (latest)

tiny dagger
#

Here my encrypter:

package crypt

import (
    "bytes"
    "crypto/cipher"
    "crypto/rand"
    "encoding/base64"
    "errors"
    "log"
)

type CrypterI interface {
    /*
        Should encrypt given data and return it in base64 format
    */
    Encrypt(data string) (string, error)
    /*
        Should Decrypt given data and return its plaintext.
        'data' here is the  base64 encoded string, when the data is decoded it represents
        the encryption cipher text in bytes
    */
    Decrypt(data string) (string, error)
}

/*
Default crypter type;
uses  AES GCM algorithm
*/
type Crypter struct {
    aes_gcm cipher.AEAD
}

const Delimiter = byte('%')

func New(block func() cipher.Block) *Crypter {
    gcm, err := cipher.NewGCM(block())
    if err != nil {
        log.Fatal(err)
    }

    return &Crypter{
        aes_gcm: gcm,
    }
}

func (c *Crypter) Encrypt(data string) (string, error) {
    nonce := make([]byte, c.aes_gcm.NonceSize())

    _, err := rand.Read(nonce)
    if err != nil {
        return "", err
    }

    encr := c.aes_gcm.Seal(nil, nonce, []byte(data), nil)
    encr = append(encr, Delimiter)
    encr = append(encr, nonce...)

    return base64.StdEncoding.EncodeToString(encr), nil
}

func (c *Crypter) Decrypt(data string) (string, error) {
    b, err := base64.StdEncoding.DecodeString(data)
    if err != nil {
        return "", err
    }

    enc, nonce, err := c.deserialize(b)
    if err != nil {
        return "", err
    }

    decr, err := c.aes_gcm.Open(nil, nonce, enc, nil)
    return string(decr), err
}

func (c *Crypter) deserialize(encr []byte) ([]byte, []byte, error) {
    i := bytes.Index(encr, []byte{Delimiter})
    if i == -1 {
        return nil, nil, errors.New("Invalid encryption marshall format")
    }

    return encr[:i], encr[(i + len([]byte{Delimiter})):], nil
}

languid grove
#

I'm unclear are you not using a secret ?

tiny dagger
#

i do

languid grove
#

where ?

tiny dagger
#

its set from here

languid grove
#

oh mb

#

forgot the API was this way

#

delimiter is a bad practice, here I think it's fine but it can get you the wrong way around randomly

#

GCM nonces are fixed size, so we usually write them before the message as-is

#

then you decode the base64 and read aes_gcm.NonceSize() bytes

#

like:

nonceSize := aes_gcm.NonceSize()
if len(base64decoded) < nonceSize {
 return errors.New("message too small")
}

nonce := base64decoded[:nonceSize]
message := base64decoded[nonceSize:]
tiny dagger
#

oh

languid grove
#

at first glance it looks safe-ish.
But this is really tricky and it would take me lots of time to do a proper review

#

crypto is dodgy, it's very not obvious when you get it wrong

tiny dagger
languid grove
#

it use a magic byte to separate

#

bytes.Index in cryptography is a huge red flag

tiny dagger
#

oh oke

#

but for the rest its good?

languid grove
languid grove
#

Crypto is the inverse of usual software, it's not good because it looks fine and test passes.
It's fine because many people tried to break it, researched it and failed to do it.

#

the secretbox package implements the same thing you are doing, but people smarter than me and you already tried breaking it and made sure it's very likely safe

tiny dagger
#

oke

river fernBOT
#

      func NewGCM(cipher Block) (AEAD, error)
    ```
NewGCM returns the given 128-bit, block cipher wrapped in Galois Counter Mode with the standard nonce length.

In general, the GHASH operation performed by this implementation of GCM is not constant-time. An exception is when the underlying Block was created by aes.NewCipher on systems with hardware support for AES. See the crypto/aes package documentation for details.
tiny dagger
#

look at this

#

isnt this literally the same as what m doing

#

so whats diff with this one and secretbox

languid grove
#

no it's not, because you wrote your code, you wrote the callback into the cipher

#

it looks fine, isn't ok security for cryptography

tiny dagger
#

but i used aes.NewCipher and its compabitle with all of the go api cipher.Block's

#

so i dont get it why it isnt

#

bro imm gonna use gorilla securecookie @languid grove

#

they do literally the same

#

even Gin uses it???