Module: JOSE::JWA

Extended by:
JWA
Included in:
JWA
Defined in:
lib/jose/jwa.rb

Overview

JWA stands for JSON Web Algorithms which is defined in RFC 7518.

Cryptographic Algorithm Fallback

Native implementations of all cryptographic and public key algorithms required by the JWA specifications are not present in current versions of Ruby.

JOSE will detect whether a specific algorithm is natively supported or not and, by default, it will mark the algorithm as unsupported if a native implementation is not found.

However, JOSE also has pure Ruby versions of many of the missing algorithms which can be used as a fallback by calling JOSE.crypto_fallback= and passing true.

Defined Under Namespace

Modules: AES_KW, ConcatKDF, Curve25519, Curve25519_CryptoRb, Curve25519_RbNaCl, Curve25519_Ruby, Curve25519_Unsupported, Curve448, Curve448_Ruby, Curve448_Unsupported, Ed25519, Ed25519_CryptoRb, Ed25519_RbNaCl, Ed448, PKCS1, PKCS7, SHA3, X25519, X25519_CryptoRb, X25519_RbNaCl, X448, XChaCha20Poly1305, XChaCha20Poly1305_RbNaCl, XChaCha20Poly1305_Unsupported Classes: Edwards25519Point, Edwards448Point, EdwardsPoint, FieldElement

Constant Summary collapse

UCHAR_PACK =
'C*'.freeze
ZERO_PAD =
[0].pack('C').force_encoding('BINARY').freeze

Instance Method Summary collapse

Instance Method Details

#constant_time_compare(a, b) ⇒ Boolean

Performs a constant time comparison between two binaries to help avoid timing attacks.

Parameters:

  • a (String)
  • b (String)

Returns:

  • (Boolean)


28
29
30
31
32
33
34
35
# File 'lib/jose/jwa.rb', line 28

def constant_time_compare(a, b)
  return false if a.empty? || b.empty? || a.bytesize != b.bytesize
  l = a.unpack "C#{a.bytesize}"

  res = 0
  b.each_byte { |byte| res |= byte ^ l.shift }
  return res == 0
end

#supportsHash

Returns the current listing of supported JOSE algorithms.

JOSE::JWA.supports
# => {:jwe=>
#   {:alg=>
#     ["A128GCMKW",
#      "A192GCMKW",
#      "A256GCMKW",
#      "A128KW",
#      "A192KW",
#      "A256KW",
#      "ECDH-ES",
#      "ECDH-ES+A128KW",
#      "ECDH-ES+A192KW",
#      "ECDH-ES+A256KW",
#      "PBES2-HS256+A128KW",
#      "PBES2-HS384+A192KW",
#      "PBES2-HS512+A256KW",
#      "RSA1_5",
#      "RSA-OAEP",
#      "RSA-OAEP-256",
#      "dir"],
#    :enc=>
#     ["A128GCM",
#      "A192GCM",
#      "A256GCM",
#      "A128CBC-HS256",
#      "A192CBC-HS384",
#      "A256CBC-HS512"],
#    :zip=>
#     ["DEF"]},
#  :jwk=>
#   {:kty=>
#     ["EC",
#      "OKP",
#      "RSA",
#      "oct"],
#    :kty_EC_crv=>
#     ["P-256",
#      "P-384",
#      "P-521"],
#    :kty_OKP_crv=>
#     ["Ed25519",
#      "Ed25519ph",
#      "Ed448",
#      "Ed448ph",
#      "X25519",
#      "X448"]},
#  :jws=>
#   {:alg=>
#     ["Ed25519",
#      "Ed25519ph",
#      "Ed448",
#      "Ed448ph",
#      "EdDSA",
#      "ES256",
#      "ES384",
#      "ES512",
#      "HS256",
#      "HS384",
#      "HS512",
#      "PS256",
#      "PS384",
#      "PS512",
#      "RS256",
#      "RS384",
#      "RS512",
#      "none"]}}

Returns:

  • (Hash)


123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/jose/jwa.rb', line 123

def supports
  jwe_enc = __jwe_enc_support_check__([
    'A128GCM',
    'A192GCM',
    'A256GCM',
    'A128CBC-HS256',
    'A192CBC-HS384',
    'A256CBC-HS512',
    'C20P',
    'XC20P'
  ])
  jwe_alg = __jwe_alg_support_check__([
    ['A128GCMKW', :block],
    ['A192GCMKW', :block],
    ['A256GCMKW', :block],
    ['A128KW', :block],
    ['A192KW', :block],
    ['A256KW', :block],
    ['C20PKW', :block],
    ['ECDH-ES', :box],
    ['ECDH-ES+A128KW', :box],
    ['ECDH-ES+A192KW', :box],
    ['ECDH-ES+A256KW', :box],
    ['PBES2-HS256+A128KW', :block],
    ['PBES2-HS384+A192KW', :block],
    ['PBES2-HS512+A256KW', :block],
    ['RSA1_5', :rsa],
    ['RSA-OAEP', :rsa],
    ['RSA-OAEP-256', :rsa],
    ['XC20PKW', :block],
    ['dir', :direct]
  ], jwe_enc)
  jwe_zip = __jwe_zip_support_check__([
    'DEF'
  ], jwe_enc)
  jwk_kty, jwk_kty_EC_crv, jwk_kty_OKP_crv = __jwk_kty_support_check__([
    ['EC', ['P-256', 'P-384', 'P-521']],
    ['OKP', ['Ed25519', 'Ed25519ph', 'Ed448', 'Ed448ph', 'X25519', 'X448']],
    'RSA',
    'oct'
  ])
  jws_alg = __jws_alg_support_check__([
    'Ed25519',
    'Ed25519ph',
    'Ed448',
    'Ed448ph',
    'EdDSA',
    'ES256',
    'ES384',
    'ES512',
    'HS256',
    'HS384',
    'HS512',
    'PS256',
    'PS384',
    'PS512',
    'RS256',
    'RS384',
    'RS512',
    'X25519',
    'X448',
    'none'
  ])
  return {
    jwe: {
      alg: jwe_alg,
      enc: jwe_enc,
      zip: jwe_zip
    },
    jwk: {
      kty: jwk_kty,
      kty_EC_crv: jwk_kty_EC_crv,
      kty_OKP_crv: jwk_kty_OKP_crv
    },
    jws: {
      alg: jws_alg
    }
  }
end