{"record":{"id":"297540d0c74d8def","repo":"grafana/k6","slug":"public-exponent-is-even","errorCode":null,"errorMessage":"public exponent is even","messagePattern":"public exponent is even","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/js/modules/k6/webcrypto/rsa.go","lineNumber":147,"sourceCode":"\t}\n\n\tvar err error\n\tprivateKey.handle, publicKey.handle, err = generateRSAKeyPair(rsakgp.ModulusLength, publicExponent)\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\n\treturn &CryptoKeyPair{\n\t\tPrivateKey: privateKey,\n\t\tPublicKey:  publicKey,\n\t}, nil\n}\n\n// validatePublicExponent validates the public exponent.\n// it's done same way how golang's rsa package does it + additional check for evenness.\nfunc validatePublicExponent(e int) error {\n\tif e%2 == 0 {\n\t\treturn errors.New(\"public exponent is even\")\n\t}\n\n\tif e < 2 {\n\t\treturn errors.New(\"public exponent too small\")\n\t}\n\tif e > 1<<31-1 {\n\t\treturn errors.New(\"public exponent too large\")\n\t}\n\n\treturn nil\n}\n\nfunc generateRSAKeyPair(\n\tmodulusLength int,\n\tpublicExponent int,\n) (any, any, error) {\n\tprivateKey, err := rsa.GenerateKey(rand.Reader, modulusLength)\n\tif err != nil {","sourceCodeStart":129,"sourceCodeEnd":165,"githubUrl":"https://github.com/grafana/k6/blob/01ffac6f245854c1b8adc6a69857c76a15f90022/internal/js/modules/k6/webcrypto/rsa.go#L129-L165","documentation":"Thrown by crypto.subtle.generateKey for RSA algorithms (RSASSA-PKCS1-v1_5, RSA-PSS, RSA-OAEP) when publicExponent is even. validatePublicExponent (internal/js/modules/k6/webcrypto/rsa.go:147) mirrors Go's rsa package plus an explicit evenness check: phi(n) is always even for a two-prime modulus, so an even exponent can never be coprime with it and is rejected up front.","triggerScenarios":"Passing publicExponent: new Uint8Array([2]) (or any even big-endian byte sequence such as [0x01, 0x00, 0x00] = 65536) in the generateKey algorithm identifier. The error is thrown synchronously before any key generation work starts.","commonSituations":"Typing 65536 (0x10000) instead of the standard 65537 (0x01 0x00 0x01); a byte-shifted exponent array; an exponent read from configuration that was computed with a power of two.","solutions":["Use the standard exponent 65537, encoded as new Uint8Array([0x01, 0x00, 0x01])","If policy forces a non-default exponent, choose an odd value between 3 and 2^31-1 (e.g. 3 or 17)","If the exponent comes from config, assert it is odd before calling generateKey so the failure names the bad value"],"exampleFix":"// before\npublicExponent: new Uint8Array([0x01, 0x00, 0x00]) // 65536 — even\n\n// after\npublicExponent: new Uint8Array([0x01, 0x00, 0x01]) // 65537","handlingStrategy":"validation","validationCode":"function decodeBigEndian(bytes) {\n  return bytes.reduce((acc, b) => acc * 256 + b, 0);\n}\n\nfunction validatePublicExponent(bytes) {\n  const e = decodeBigEndian(bytes);\n  if (e % 2 === 0) throw new Error(`publicExponent ${e} is even — use [0x01, 0x00, 0x01] (65537)`);\n  if (e < 2) throw new Error(`publicExponent ${e} is too small`);\n  if (e > 2 ** 31 - 1) throw new Error(`publicExponent ${e} is too large`);\n  return bytes;\n}\n\nvalidatePublicExponent(publicExponent);\nawait crypto.subtle.generateKey({ name: 'RSA-OAEP', modulusLength: 2048, publicExponent, hash: 'SHA-256' }, true, ['encrypt', 'decrypt']);","typeGuard":null,"tryCatchPattern":"try {\n  keyPair = await crypto.subtle.generateKey(alg, true, usages);\n} catch (err) {\n  if (/public exponent/.test(String(err))) {\n    throw new Error(`bad publicExponent — falling back to 65537: ${err.message}`);\n  }\n  throw err;\n}","preventionTips":["Standardize on 65537 ([0x01, 0x00, 0x01]) across all scripts","Decode configured exponent bytes and assert odd, 2 <= e <= 2^31-1 before generateKey","Watch for the 65536-vs-65537 off-by-one when typing hex bytes"],"tags":["webcrypto","rsa","generatekey","public-exponent","key-generation"],"backgroundTag":"invalid-rsa-key-parameters","analyzedSha":"01ffac6f245854c1b8adc6a69857c76a15f90022","analyzedAt":"2026-08-18T03:05:52.393Z","contentChangedAt":"2026-08-18T03:05:52.393Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}