3
votes

When I try to run the following code (I'm trying to encrypt the byte array representation of a string, using AES in CFB mode) I get an invalid memory address or nil pointer dereference error, on the line sEnc.XORKeyStream(msgB, msgB). The documentation says the source and destination for XORKeyStream can be the same byte array (also tried using a separate destination, but no dice), but I can't figure out what's causing the error. I'm using the latest version of the Go App Engine SDK on OS X Lion.

Since at this point I'm still trying to get the code to simply encrypt something, I'm just generating a random key and not bothering to store it yet.

func generateKey(w http.ResponseWriter, r *http.Request) {
    key := make([]byte, 32)
    n, err := rand.Read(key)
    err = err
    if(err != nil) {
        fmt.Fprint(w, err)
        return
    }
    if(n != 32) {
        fmt.Fprint(w, "Not enough bytes read.")
        return
    }
    c, err := aes.NewCipher(key)
    if(err != nil) {
        fmt.Fprint(w, err)
    }

    iv := make([]byte, 32)
    n, err = rand.Read(iv)
    err = err
    if(err != nil) {
        fmt.Fprint(w, err)
        return
    }
    if(n != 32) {
        fmt.Fprint(w, "Not enough bytes read.")
        return
    }

    sEnc := cipher.NewCFBEncrypter(c, iv)

    msg := "text to be encrypted"
    msgR := strings.NewReader(msg)
    msgB := make([]byte, msgR.Len())
    n, err = msgR.Read(msgB)
    if(err != nil) {
        fmt.Fprint(w, err)
        return
    }
    if(n == 0) {
        fmt.Fprint(w, "No bytes read.")
        return
    }
    fmt.Fprint(w, msgB)
    fmt.Fprint(w, "<br>")
    sEnc.XORKeyStream(msgB, msgB)
    fmt.Fprint(w, msgB)
}

The error messages:

2011/08/15 15:15:58 http: panic serving : runtime error: invalid memory address or nil pointer dereference runtime.panic /private/tmp/appengine/google_appengine/goroot/src/pkg/runtime/proc.c:1041 runtime.panicstring /private/tmp/appengine/google_appengine/goroot/src/pkg/runtime/runtime.c:116 runtime.sigpanic /private/tmp/appengine/google_appengine/goroot/src/pkg/runtime/darwin/thread.c:470 server.generateKey server/server.go:66 http.HandlerFunc·ServeHTTP /private/tmp/appengine/google_appengine/goroot/src/pkg/http/server.go:589 http.*ServeMux·ServeHTTP /private/tmp/appengine/google_appengine/goroot/src/pkg/http/server.go:790 appengine_internal.handleFilteredHTTP /private/tmp/appengine/google_appengine/goroot/src/pkg/appengine_sdk/go_appengine_sdk/appengine_internal/api_dev.go:58 http.HandlerFunc·ServeHTTP /private/tmp/appengine/google_appengine/goroot/src/pkg/http/server.go:589 http.*conn·serve /private/tmp/appengine/google_appengine/goroot/src/pkg/http/server.go:555 runtime.goexit /private/tmp/appengine/google_appengine/goroot/src/pkg/runtime/proc.c:178 INFO 2011-08-15 15:15:58,310 dev_appserver.py:4248] "GET /genkey/aes256 HTTP/1.1" 500 -

I've posted the rest of the code to paste bin, but it's just the includes/init function, nothing all that interesting.

1

1 Answers

3
votes

sEnc is nil as a result of an error. For cipher.NewCFBEncrypter, "the iv must be the same length as the Block's block." The AES block size is 128 bits or 16 bytes? For example,

iv := make([]byte, c.BlockSize())