[erlang-questions] NIF Problem
Jachym Holecek
freza@REDACTED
Thu Apr 28 12:07:11 CEST 2011
# Travis Jensen 2011-04-28:
> [... This results in SIGBUS after returning to Erlang ...]
>
> 1. static ERL_NIF_TERM aes_ctr_encrypt_with_state(ErlNifEnv* env, int argc, const ERL_NIF_TERM
> argv[])
> 2. {/* ({Key, IVec, ECount, Num}, Data) */
> 3. ErlNifBinary key_bin, ivec_bin, text_bin, ecount_bin;
> 4. AES_KEY aes_key;
> 5. unsigned int num = 0;
> 6. ERL_NIF_TERM ret, num2_term, cipher_term, ivec2_term, ecount2_term, new_state_term;
> 7. int state_arity;
> 8. const ERL_NIF_TERM *state_term;
> 9. unsigned char * ivec2_buf;
> 10. unsigned char * ecount2_buf;
> 11.
> 12. if (!enif_get_tuple(env, argv[0], &state_arity, &state_term)
> 13. || state_arity != 4
> 14. || !enif_inspect_iolist_as_binary(env, state_term[0], &key_bin)
> 15. || AES_set_encrypt_key(key_bin.data, key_bin.size*8, &aes_key) != 0
> 16. || !enif_inspect_binary(env, state_term[1], &ivec_bin) || ivec_bin.size != 16
> 17. || !enif_inspect_binary(env, state_term[2], &ecount_bin) || ecount_bin.size !=
> AES_BLOCK_SIZE
> 18. || !enif_get_uint(env, state_term[3], &num)
> 19. || !enif_inspect_iolist_as_binary(env, argv[1], &text_bin)) {
> 20. return enif_make_badarg(env);
> 21. }
> 22.
> 23. ivec2_buf = enif_make_new_binary(env, ivec_bin.size, &ivec2_term);
> 24. ecount2_buf = enif_make_new_binary(env, ecount_bin.size, &ecount2_term);
> 25.
> 26. memcpy(ivec2_buf, ivec_bin.data, 16);
> 27. memcpy(ecount2_buf, ecount_bin.data, ecount_bin.size);
> 28.
> 29. AES_ctr128_encrypt((unsigned char *) text_bin.data,
> 30. enif_make_new_binary(env, text_bin.size, &cipher_term),
> 31. text_bin.size, &aes_key, ivec2_buf, ecount2_buf, &num);
> 32.
> 33. num2_term = enif_make_uint(env, num);
> 34. new_state_term = enif_make_tuple4(env, key_bin, ivec2_term, ecount2_term, num2_term);
> 35. ret = enif_make_tuple2(env, new_state_term, cipher_term);
> 36. return ret;
> 37. }
Just a random guess really, but your key_bin doesn't seem to be a proper term
suitable for returning to Erlang, shoudln't you run enif_make_binary() on it?
The compiler should warn you about that too, unless ERL_NIF_TERM is something
hopelessly vague like "void *"...
Regards,
-- Jachym
More information about the erlang-questions
mailing list