Skip to content

Instantly share code, notes, and snippets.

So, the jose library needs a little work done to separate the ES (Ephemeral-Static) and SS (Static-Static) operations.

The JWA RFC 7518 only defines ES operations and actually states (https://tools.ietf.org/html/rfc7518#section-4.6):

A new ephemeral public key value MUST be generated for each key agreement operation.

The separate modes of operation were clarified by a different working group, COSE WG, in RFC 8152 (https://tools.ietf.org/html/rfc8152#section-12.4):

Ephemeral-Static (ES) DH: where the sender of the message creates a one-time DH key and uses a static key for the recipient. The

Run in shell with:

erlc bench_binary_find.erl && erl -noshell -pa . -s bench_binary_find
Erlang/OTP 20 (master, unpatched)

binary:split/3 is up to 80x slower than a tail recursion calling binary:split/2 multiple times.

@potatosalad
potatosalad / jose-example.exs
Last active January 23, 2017 22:50
RSA jwk-set+json
secret_jwk = JOSE.JWK.generate_key({:rsa, 2048})
# Save the secret RSA key to a PEM file.
JOSE.JWK.to_pem_file("rsa-secret.pem", secret_jwk)
# Or optionally as a password protected PEM file.
JOSE.JWK.to_pem_file("secret-password", "rsa-secret-protected.pem", secret_jwk)
# Save the jwk-set+json to a file.
public_jwk = secret_jwk |> JOSE.JWK.to_public()
JOSE.JWK.to_file("public-rsa.json", :jose_jwk_set.to_map([public_jwk |> JOSE.JWK.to_record()], %{}))
# Or use JOSE.JWK.to_public_file/2 to skip converting secret_jwk to public_jwk
JOSE.JWK.to_public_file("public-rsa.json", :jose_jwk_set.to_map([secret_jwk |> JOSE.JWK.to_record()], %{}))
@potatosalad
potatosalad / sodium-message_encryptor.ex
Last active October 14, 2016 16:44
Example plug message encryptor using libsodium
defmodule Sodium.MessageEncryptor do
@moduledoc ~S"""
AES256-GCM Content-Encryption with AES256-GCM Key-Wrapping using [libsodium](https://github.com/potatosalad/erlang-libsodium).
For example:
# Encryption: `secret' must be at least 256-bits, `sign_secret' may be any length
iex> encrypted = Sodium.MessageEncryptor.encrypt("test", "abcdefghijklmnopqrstuvwxyz012345", "abcdefghijklmnop")
"QTI1NkdDTQ.VGbRU_MS0G_sFvfpUkoXmK8Hl1TBI5tPVN_SUl6NI7yYrZDgdV3u2OrnWwXAgg8A-FZNOtJV7P-nICoQ.Ht5ty7EvPx-YY-bn.98CpxA.1KZaBS79yPKdXuTbdqL66A"
@potatosalad
potatosalad / libsodium_aes_gcm_example.exs
Created October 13, 2016 21:33
Example usages of AES-GCM with libsodium
# Static Constants
plain_text = "test"
plain_text_size = byte_size(plain_text)
aad = "A256GCM" # AAD (Additional Authenticated Data)
####################################
# Example 1: Direct Key Encryption #
####################################
# In this example, the CEK must be known by the encryptor and decryptor (ie. a shared secret)
@potatosalad
potatosalad / nifsy-dirty.md
Created July 28, 2016 15:56
nifsy dirty schedulers vs uv (preliminary benchmark)
name total usec/iter usec/line
1 KB (16 B) - nifsy 6728 134.56 2.990
1 KB (1 KB) - nifsy 5176 103.52 2.300
1 KB (64KB) - nifsy 5329 106.58 2.368
1 MB (1 KB) - nifsy 210460 4209.20 2.907
1 MB (64KB) - nifsy 169180 3383.60 2.337
1 MB (1 MB) - nifsy 194220 3884.40 2.683

Keybase proof

I hereby claim:

  • I am potatosalad on github.
  • I am potatosalad (https://keybase.io/potatosalad) on keybase.
  • I have a public key whose fingerprint is DE0E 3767 CFFB D7F4 D978 1FB9 E2D9 1EA8 2EDA 44FF

To claim this, I am signing this object:

@potatosalad
potatosalad / jwk_ec256_alice_sk.json
Last active January 19, 2016 21:38
Example keys for JOSE.JWE documentation https://hexdocs.pm/jose
{
"crv": "P-256",
"d": "JPUl6o2Or-ez1P6xErxJvgvDcqz-gIOCdTmc14Kw0Dw",
"kty": "EC",
"x": "48QU3Q0CySxwJbEwWrJrYXlp88_dVpHTxq4avc66h5Q",
"y": "ZzqrINtMMxHxQ-QB72RMcdlmDsOIwlKhMqVm_gYWC14"
}
# variables used in examples below
p = 2^448 - 2^224 - 1
u = 5
v = 355293926785568175264127502063783334808976399387714271831880898435169088786967410002932673765864550910142774147268105838985595290606362
x = 224580040295924300187604334099896036246789641632564134246125461686950415467406032909029192869357953282578032075146446173674602635247710
y = 298819210078481492676017930443930673437544040154080242095928241372331506189835876003536878655418784733982303233503462500531545062832660
# testing (u, v) functions; both return True
u == y^2/x^2 % p
v == (2 - x^2 - y^2)*y/x^3 % p
# Map using maps used by examples:
map_of_maps = %{
%{key1: :a, key2: :b} => %{val1: 1, val2: 2},
%{key1: :a, key2: :c} => %{val1: 3, val2: 8},
%{key1: :b, key2: :d} => %{val1: 5, val2: 3}
}
# Map using tuples used by examples:
map_of_tuples = %{
{:a, :b} => {1, 2},