diff --git a/.gitignore b/.gitignore index 7375d1e..491567a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,7 +1,7 @@ # ---> Romulus-JS # Build output -dist/ +# dist/ # ---> VisualStudioCode .vscode/* @@ -171,7 +171,7 @@ out # Nuxt.js build / generate output .nuxt -dist +# dist # Gatsby files .cache/ diff --git a/dist/constants.d.ts b/dist/constants.d.ts new file mode 100644 index 0000000..ce15f1f --- /dev/null +++ b/dist/constants.d.ts @@ -0,0 +1,10 @@ +export declare const MEMBER_MASK = 32; +export declare const NB_ROUNDS = 40; +export declare const TWEAK_LENGTH = 48; +export declare const PT: number[]; +export declare const LFSR_8_TK2: number[]; +export declare const LFSR_8_TK3: number[]; +export declare const S8: number[]; +export declare const C: number[]; +export declare const T_LENGTH = 16; +export declare const COUNTER_LENGTH = 7; diff --git a/dist/constants.js b/dist/constants.js new file mode 100644 index 0000000..d0a56ff --- /dev/null +++ b/dist/constants.js @@ -0,0 +1,62 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.COUNTER_LENGTH = exports.T_LENGTH = exports.C = exports.S8 = exports.LFSR_8_TK3 = exports.LFSR_8_TK2 = exports.PT = exports.TWEAK_LENGTH = exports.NB_ROUNDS = exports.MEMBER_MASK = void 0; +// SKINNY-128/384+ block cipher constants. +exports.MEMBER_MASK = 32; +exports.NB_ROUNDS = 40; +exports.TWEAK_LENGTH = 48; +exports.PT = [9, 15, 8, 13, 10, 14, 12, 11, 0, 1, 2, 3, 4, 5, 6, 7]; +exports.LFSR_8_TK2 = [ + 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52, 54, 56, + 58, 60, 62, 65, 67, 69, 71, 73, 75, 77, 79, 81, 83, 85, 87, 89, 91, 93, 95, 97, 99, 101, 103, 105, 107, 109, + 111, 113, 115, 117, 119, 121, 123, 125, 127, 128, 130, 132, 134, 136, 138, 140, 142, 144, 146, 148, 150, 152, + 154, 156, 158, 160, 162, 164, 166, 168, 170, 172, 174, 176, 178, 180, 182, 184, 186, 188, 190, 193, 195, 197, + 199, 201, 203, 205, 207, 209, 211, 213, 215, 217, 219, 221, 223, 225, 227, 229, 231, 233, 235, 237, 239, 241, + 243, 245, 247, 249, 251, 253, 255, 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35, 37, 39, + 41, 43, 45, 47, 49, 51, 53, 55, 57, 59, 61, 63, 64, 66, 68, 70, 72, 74, 76, 78, 80, 82, 84, 86, 88, 90, 92, + 94, 96, 98, 100, 102, 104, 106, 108, 110, 112, 114, 116, 118, 120, 122, 124, 126, 129, 131, 133, 135, 137, + 139, 141, 143, 145, 147, 149, 151, 153, 155, 157, 159, 161, 163, 165, 167, 169, 171, 173, 175, 177, 179, 181, + 183, 185, 187, 189, 191, 192, 194, 196, 198, 200, 202, 204, 206, 208, 210, 212, 214, 216, 218, 220, 222, 224, + 226, 228, 230, 232, 234, 236, 238, 240, 242, 244, 246, 248, 250, 252, 254 +]; +exports.LFSR_8_TK3 = [ + 0, 128, 1, 129, 2, 130, 3, 131, 4, 132, 5, 133, 6, 134, 7, 135, 8, 136, 9, 137, 10, 138, 11, 139, 12, 140, + 13, 141, 14, 142, 15, 143, 16, 144, 17, 145, 18, 146, 19, 147, 20, 148, 21, 149, 22, 150, 23, 151, 24, 152, + 25, 153, 26, 154, 27, 155, 28, 156, 29, 157, 30, 158, 31, 159, 160, 32, 161, 33, 162, 34, 163, 35, 164, 36, + 165, 37, 166, 38, 167, 39, 168, 40, 169, 41, 170, 42, 171, 43, 172, 44, 173, 45, 174, 46, 175, 47, 176, 48, + 177, 49, 178, 50, 179, 51, 180, 52, 181, 53, 182, 54, 183, 55, 184, 56, 185, 57, 186, 58, 187, 59, 188, 60, + 189, 61, 190, 62, 191, 63, 64, 192, 65, 193, 66, 194, 67, 195, 68, 196, 69, 197, 70, 198, 71, 199, 72, 200, + 73, 201, 74, 202, 75, 203, 76, 204, 77, 205, 78, 206, 79, 207, 80, 208, 81, 209, 82, 210, 83, 211, 84, 212, + 85, 213, 86, 214, 87, 215, 88, 216, 89, 217, 90, 218, 91, 219, 92, 220, 93, 221, 94, 222, 95, 223, 224, 96, + 225, 97, 226, 98, 227, 99, 228, 100, 229, 101, 230, 102, 231, 103, 232, 104, 233, 105, 234, 106, 235, 107, + 236, 108, 237, 109, 238, 110, 239, 111, 240, 112, 241, 113, 242, 114, 243, 115, 244, 116, 245, 117, 246, + 118, 247, 119, 248, 120, 249, 121, 250, 122, 251, 123, 252, 124, 253, 125, 254, 126, 255, 127 +]; +exports.S8 = [ + 0x65, 0x4c, 0x6a, 0x42, 0x4b, 0x63, 0x43, 0x6b, 0x55, 0x75, 0x5a, 0x7a, 0x53, 0x73, 0x5b, 0x7b, + 0x35, 0x8c, 0x3a, 0x81, 0x89, 0x33, 0x80, 0x3b, 0x95, 0x25, 0x98, 0x2a, 0x90, 0x23, 0x99, 0x2b, + 0xe5, 0xcc, 0xe8, 0xc1, 0xc9, 0xe0, 0xc0, 0xe9, 0xd5, 0xf5, 0xd8, 0xf8, 0xd0, 0xf0, 0xd9, 0xf9, + 0xa5, 0x1c, 0xa8, 0x12, 0x1b, 0xa0, 0x13, 0xa9, 0x05, 0xb5, 0x0a, 0xb8, 0x03, 0xb0, 0x0b, 0xb9, + 0x32, 0x88, 0x3c, 0x85, 0x8d, 0x34, 0x84, 0x3d, 0x91, 0x22, 0x9c, 0x2c, 0x94, 0x24, 0x9d, 0x2d, + 0x62, 0x4a, 0x6c, 0x45, 0x4d, 0x64, 0x44, 0x6d, 0x52, 0x72, 0x5c, 0x7c, 0x54, 0x74, 0x5d, 0x7d, + 0xa1, 0x1a, 0xac, 0x15, 0x1d, 0xa4, 0x14, 0xad, 0x02, 0xb1, 0x0c, 0xbc, 0x04, 0xb4, 0x0d, 0xbd, + 0xe1, 0xc8, 0xec, 0xc5, 0xcd, 0xe4, 0xc4, 0xed, 0xd1, 0xf1, 0xdc, 0xfc, 0xd4, 0xf4, 0xdd, 0xfd, + 0x36, 0x8e, 0x38, 0x82, 0x8b, 0x30, 0x83, 0x39, 0x96, 0x26, 0x9a, 0x28, 0x93, 0x20, 0x9b, 0x29, + 0x66, 0x4e, 0x68, 0x41, 0x49, 0x60, 0x40, 0x69, 0x56, 0x76, 0x58, 0x78, 0x50, 0x70, 0x59, 0x79, + 0xa6, 0x1e, 0xaa, 0x11, 0x19, 0xa3, 0x10, 0xab, 0x06, 0xb6, 0x08, 0xba, 0x00, 0xb3, 0x09, 0xbb, + 0xe6, 0xce, 0xea, 0xc2, 0xcb, 0xe3, 0xc3, 0xeb, 0xd6, 0xf6, 0xda, 0xfa, 0xd3, 0xf3, 0xdb, 0xfb, + 0x31, 0x8a, 0x3e, 0x86, 0x8f, 0x37, 0x87, 0x3f, 0x92, 0x21, 0x9e, 0x2e, 0x97, 0x27, 0x9f, 0x2f, + 0x61, 0x48, 0x6e, 0x46, 0x4f, 0x67, 0x47, 0x6f, 0x51, 0x71, 0x5e, 0x7e, 0x57, 0x77, 0x5f, 0x7f, + 0xa2, 0x18, 0xae, 0x16, 0x1f, 0xa7, 0x17, 0xaf, 0x01, 0xb2, 0x0e, 0xbe, 0x07, 0xb7, 0x0f, 0xbf, + 0xe2, 0xca, 0xee, 0xc6, 0xcf, 0xe7, 0xc7, 0xef, 0xd2, 0xf2, 0xde, 0xfe, 0xd7, 0xf7, 0xdf, 0xff +]; +exports.C = [ + 0x01, 0x03, 0x07, 0x0F, 0x1F, 0x3E, 0x3D, 0x3B, 0x37, 0x2F, + 0x1E, 0x3C, 0x39, 0x33, 0x27, 0x0E, 0x1D, 0x3A, 0x35, 0x2B, + 0x16, 0x2C, 0x18, 0x30, 0x21, 0x02, 0x05, 0x0B, 0x17, 0x2E, + 0x1C, 0x38, 0x31, 0x23, 0x06, 0x0D, 0x1B, 0x36, 0x2D, 0x1A +]; +// Romulus-M cryptography specification constants. +exports.T_LENGTH = 16; +exports.COUNTER_LENGTH = 7; +//# sourceMappingURL=constants.js.map \ No newline at end of file diff --git a/dist/constants.js.map b/dist/constants.js.map new file mode 100644 index 0000000..fdf03a4 --- /dev/null +++ b/dist/constants.js.map @@ -0,0 +1 @@ +{"version":3,"file":"constants.js","sourceRoot":"","sources":["../src/constants.ts"],"names":[],"mappings":";;;AAAA,0CAA0C;AAC7B,QAAA,WAAW,GAAG,EAAE,CAAA;AAChB,QAAA,SAAS,GAAG,EAAE,CAAA;AACd,QAAA,YAAY,GAAG,EAAE,CAAA;AACjB,QAAA,EAAE,GAAG,CAAC,CAAC,EAAE,EAAE,EAAE,CAAC,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,CAAA;AAC3D,QAAA,UAAU,GAAG;IACxB,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE;IAC7G,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE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AE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI;CAC/F,CAAA;AACY,QAAA,CAAC,GAAG;IACf,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI;IAC1D,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI;IAC1D,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI;IAC1D,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI;CAC3D,CAAA;AAED,kDAAkD;AACrC,QAAA,QAAQ,GAAG,EAAE,CAAA;AACb,QAAA,cAAc,GAAG,CAAC,CAAA"} \ No newline at end of file diff --git a/dist/decrypt.d.ts b/dist/decrypt.d.ts new file mode 100644 index 0000000..fff1986 --- /dev/null +++ b/dist/decrypt.d.ts @@ -0,0 +1,2 @@ +/// +export declare function decrypt(ciphertext: Buffer, associatedData: Buffer, nonce: Buffer, key: Buffer): Buffer; diff --git a/dist/decrypt.js b/dist/decrypt.js new file mode 100644 index 0000000..f4c212c --- /dev/null +++ b/dist/decrypt.js @@ -0,0 +1,10 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.decrypt = void 0; +const romulus_m_1 = require("./romulus-m"); +function decrypt(ciphertext, associatedData, nonce, key) { + const plaintext = (0, romulus_m_1.cryptoAeadDecrypt)(Array.from(ciphertext), Array.from(associatedData), Array.from(nonce), Array.from(key)); + return Buffer.from(plaintext); +} +exports.decrypt = decrypt; +//# sourceMappingURL=decrypt.js.map \ No newline at end of file diff --git a/dist/decrypt.js.map b/dist/decrypt.js.map new file mode 100644 index 0000000..5d7f106 --- /dev/null +++ b/dist/decrypt.js.map @@ -0,0 +1 @@ +{"version":3,"file":"decrypt.js","sourceRoot":"","sources":["../src/decrypt.ts"],"names":[],"mappings":";;;AAAA,2CAA+C;AAE/C,SAAgB,OAAO,CAAE,UAAkB,EAAE,cAAsB,EAAE,KAAa,EAAE,GAAW;IAC7F,MAAM,SAAS,GAAG,IAAA,6BAAiB,EAAC,KAAK,CAAC,IAAI,CAAC,UAAU,CAAC,EAAE,KAAK,CAAC,IAAI,CAAC,cAAc,CAAC,EAAE,KAAK,CAAC,IAAI,CAAC,KAAK,CAAC,EAAE,KAAK,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,CAAA;IAC3H,OAAO,MAAM,CAAC,IAAI,CAAC,SAAS,CAAC,CAAA;AAC/B,CAAC;AAHD,0BAGC"} \ No newline at end of file diff --git a/dist/encrypt.d.ts b/dist/encrypt.d.ts new file mode 100644 index 0000000..80e359c --- /dev/null +++ b/dist/encrypt.d.ts @@ -0,0 +1,2 @@ +/// +export declare function encrypt(message: Buffer, associatedData: Buffer, nonce: Buffer, key: Buffer): Buffer; diff --git a/dist/encrypt.js b/dist/encrypt.js new file mode 100644 index 0000000..305472c --- /dev/null +++ b/dist/encrypt.js @@ -0,0 +1,10 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.encrypt = void 0; +const romulus_m_1 = require("./romulus-m"); +function encrypt(message, associatedData, nonce, key) { + const ciphertext = (0, romulus_m_1.cryptoAeadEncrypt)(Array.from(message), Array.from(associatedData), Array.from(nonce), Array.from(key)); + return Buffer.from(ciphertext); +} +exports.encrypt = encrypt; +//# sourceMappingURL=encrypt.js.map \ No newline at end of file diff --git a/dist/encrypt.js.map b/dist/encrypt.js.map new file mode 100644 index 0000000..4a6df8c --- /dev/null +++ b/dist/encrypt.js.map @@ -0,0 +1 @@ +{"version":3,"file":"encrypt.js","sourceRoot":"","sources":["../src/encrypt.ts"],"names":[],"mappings":";;;AAAA,2CAA+C;AAE/C,SAAgB,OAAO,CAAE,OAAe,EAAE,cAAsB,EAAE,KAAa,EAAE,GAAW;IAC1F,MAAM,UAAU,GAAG,IAAA,6BAAiB,EAAC,KAAK,CAAC,IAAI,CAAC,OAAO,CAAC,EAAE,KAAK,CAAC,IAAI,CAAC,cAAc,CAAC,EAAE,KAAK,CAAC,IAAI,CAAC,KAAK,CAAC,EAAE,KAAK,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,CAAA;IACzH,OAAO,MAAM,CAAC,IAAI,CAAC,UAAU,CAAC,CAAA;AAChC,CAAC;AAHD,0BAGC"} \ No newline at end of file diff --git a/dist/index.d.ts b/dist/index.d.ts new file mode 100644 index 0000000..3666e6e --- /dev/null +++ b/dist/index.d.ts @@ -0,0 +1,2 @@ +export { encrypt } from './encrypt'; +export { decrypt } from './decrypt'; diff --git a/dist/index.js b/dist/index.js new file mode 100644 index 0000000..f6af5f3 --- /dev/null +++ b/dist/index.js @@ -0,0 +1,8 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.decrypt = exports.encrypt = void 0; +var encrypt_1 = require("./encrypt"); +Object.defineProperty(exports, "encrypt", { enumerable: true, get: function () { return encrypt_1.encrypt; } }); +var decrypt_1 = require("./decrypt"); +Object.defineProperty(exports, "decrypt", { enumerable: true, get: function () { return decrypt_1.decrypt; } }); +//# sourceMappingURL=index.js.map \ No newline at end of file diff --git a/dist/index.js.map b/dist/index.js.map new file mode 100644 index 0000000..6411314 --- /dev/null +++ b/dist/index.js.map @@ -0,0 +1 @@ +{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AAAA,qCAAmC;AAA1B,kGAAA,OAAO,OAAA;AAChB,qCAAmC;AAA1B,kGAAA,OAAO,OAAA"} \ No newline at end of file diff --git a/dist/romulus-m.d.ts b/dist/romulus-m.d.ts new file mode 100644 index 0000000..d6dcac4 --- /dev/null +++ b/dist/romulus-m.d.ts @@ -0,0 +1,18 @@ +/** + * Encrypt a message using the Romulus-M cryptography specification. + * @param message The message to encrypt. + * @param associatedData The associated data to encrypt. + * @param nonce A 128 bit nonce. + * @param key A 128 bit encryption key. + * @returns The encrypted ciphertext. + */ +export declare function cryptoAeadEncrypt(message: number[], associatedData: number[], nonce: number[], key: number[]): number[]; +/** + * Decrypt a message using the Romulus-M cryptography specification. + * @param ciphertext The ciphertext to decrypt. + * @param associatedData The associated data. + * @param nonce The nonce. + * @param key The key. + * @returns The decrypted plaintext. + */ +export declare function cryptoAeadDecrypt(ciphertext: number[], associatedData: number[], nonce: number[], key: number[]): number[]; diff --git a/dist/romulus-m.js b/dist/romulus-m.js new file mode 100644 index 0000000..c47659d --- /dev/null +++ b/dist/romulus-m.js @@ -0,0 +1,316 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.cryptoAeadDecrypt = exports.cryptoAeadEncrypt = void 0; +const constants_1 = require("./constants"); +const skinny_128_384_plus_1 = require("./skinny-128-384-plus"); +/** + * Parse message into blocks. + * @param message The message to parse. + * @param blockLength The block length. + * @returns An array of blocks. + */ +function parse(message, blockLength) { + // Keep track of position in message currently parsed into blocks. + let cursor = 0; + // Slice message into blocks. + let ret = []; + while (message.length - cursor >= blockLength) { + ret.push(...[message.slice(cursor, cursor + blockLength)]); + cursor = cursor + blockLength; + } + // Append any remaining blocks regardless of block length. These will be padded later. + if (message.length - cursor > 0) { + ret.push(...[message.slice(cursor)]); + } + // If no message, return a single block. + if (message.length === 0) { + ret = [[]]; + } + // Insert empty array at position 0. + ret.splice(0, 0, []); + return ret; +} +/** + * Pads the byte length of message to padLength. The final byte (when padded) contains the original message length. + * @param message The message to pad. + * @param padLength The length to pad the message to. + * @returns A padded block. + */ +function pad(message, padLength) { + // If there is no message, return a fully padded block. + if (message.length === 0) { + return Array(16).fill(0); + } + // Return a copy of the message if no padding is required. + if (message.length === padLength) { + return [...message]; + } + // Pad a copy of the message to padLength. + const ret = [...message]; + const requiredPadding = padLength - message.length - 1; + ret.push(...Array(requiredPadding).fill(0)); + // Set the final byte of the padded blocked to the length of the original message. + ret[padLength - 1] = message.length; + return ret; +} +/** + * Generate the key stream from the internal state by multiplying the state S and the constant matrix G. + * @param state The state from which the key stream will be generated. + * @returns The key stream. + */ +function g(state) { + return state.map(x => { + return (x >> 1) ^ (x & 0x80) ^ ((x & 0x01) << 7); + }); +} +/** + * The state update function. Pads an M block. + * @param state The internal state, S. + * @param mBlock An M block. + * @returns [S', C] where S' = M ⊕ S and C = M ⊕ G(S) + */ +function rho(state, mBlock) { + // G(S) + const gOfS = g(state); + // C = M ⊕ G(S) + const c = [...Array(16).keys()].map(i => mBlock[i] ^ gOfS[i]); + // S' = M ⊕ S + const nextState = [...Array(16).keys()].map(i => state[i] ^ mBlock[i]); + return [nextState, c]; +} +/** + * The state update function. Pads a C block. + * @param state The internal state, S. + * @param cBlock A C block. + * @returns [S', M] where M = C ⊕ G(S) and S' = C ⊕ M. + */ +function inverseRoh(state, cBlock) { + // G(S) + const gOfS = g(state); + // M = C ⊕ G(S) + const mBlock = [...Array(16).keys()].map(i => cBlock[i] ^ gOfS[i]); + // S' = S ⊕ M + const nextState = [...Array(16).keys()].map(i => state[i] ^ mBlock[i]); + return [nextState, mBlock]; +} +/** + * Increments the 56 bit LFSR-based counter. + * @param counter The old counter. + * @returns An incremented counter. + */ +function increaseCounter(counter) { + const fb0 = counter[6] >> 7; + counter[6] = (counter[6] << 1) | (counter[5] >> 7); + counter[5] = (counter[5] << 1) | (counter[4] >> 7); + counter[4] = (counter[4] << 1) | (counter[3] >> 7); + counter[3] = (counter[3] << 1) | (counter[2] >> 7); + counter[2] = (counter[2] << 1) | (counter[1] >> 7); + counter[1] = (counter[1] << 1) | (counter[0] >> 7); + if (fb0 === 1) { + counter[0] = (counter[0] << 1) ^ 0x95; + } + else { + counter[0] = (counter[0] << 1); + } + return counter; +} +/** + * Returns a reset counter. + * @returns A reset counter. + */ +function resetCounter() { + const counter = Array(constants_1.COUNTER_LENGTH).fill(0); + counter[0] = 1; + return counter; +} +/** + * Returns a zeroed buffer. + * @param bufferLength The length of the buffer to return. + * @returns A zeroed buffer. + */ +function zeroedBuffer(bufferLength) { + return Array(bufferLength).fill(0); +} +/** + * Calculate the domain separation. + * @param combinedData The parsed and concatenated message and associated data, + * @param parsedMessageLength The length of the parsed message. + * @param parsedAssociatedDataLength The length of the parsed associated data. + */ +function calculateDomainSeparation(combinedData, parsedMessageLength, parsedAssociatedDataLength) { + let domainSeparation = 16; + if (combinedData[parsedAssociatedDataLength].length < 16) { + domainSeparation = domainSeparation ^ 2; + } + if (combinedData[parsedAssociatedDataLength + parsedMessageLength].length < 16) { + domainSeparation = domainSeparation ^ 1; + } + if (parsedAssociatedDataLength % 2 === 0) { + domainSeparation = domainSeparation ^ 8; + } + if (parsedMessageLength % 2 === 0) { + domainSeparation = domainSeparation ^ 4; + } + return domainSeparation; +} +/** + * Encrypt a message using the Romulus-M cryptography specification. + * @param message The message to encrypt. + * @param associatedData The associated data to encrypt. + * @param nonce A 128 bit nonce. + * @param key A 128 bit encryption key. + * @returns The encrypted ciphertext. + */ +function cryptoAeadEncrypt(message, associatedData, nonce, key) { + // Buffer for ciphertext. + const ciphertext = []; + // Reset state and counter. + let state = zeroedBuffer(16); + let counter = resetCounter(); + // Carve message and associated data into blocks. + const messageBlocks = parse(message, 16); + const messageBlockCount = messageBlocks.length - 1; + const associatedDataBlocks = parse(associatedData, 16); + const associatedDataBlockCount = associatedDataBlocks.length - 1; + // Concatenate the message and associated data blocks, excluding each array's first element. + const combinedDataBlocks = associatedDataBlocks.slice(1).concat(messageBlocks.slice(1)); + // Insert empty array at position 0. + combinedDataBlocks.splice(0, 0, []); + // Calculate domain separation for final encryption stage. + const domainSeparation = calculateDomainSeparation(combinedDataBlocks, messageBlockCount, associatedDataBlockCount); + // Pad combined data. + combinedDataBlocks[associatedDataBlockCount] = pad(combinedDataBlocks[associatedDataBlockCount], 16); + combinedDataBlocks[associatedDataBlockCount + messageBlockCount] = pad(combinedDataBlocks[associatedDataBlockCount + messageBlockCount], 16); + // Do the encryption. + // See https://romulusae.github.io/romulus/docs/Romulusv1.3.pdf for more information. + let x = 8; + for (let i = 1; i < Math.floor((associatedDataBlockCount + messageBlockCount) / 2) + 1; i++) { + [state] = rho(state, combinedDataBlocks[2 * i - 1]); + counter = increaseCounter(counter); + if (i === Math.floor(associatedDataBlockCount / 2) + 1) { + x = x ^ 4; + } + state = (0, skinny_128_384_plus_1.skinnyEncrypt)(state, (0, skinny_128_384_plus_1.tweakeyEncode)(counter, x, combinedDataBlocks[2 * i], key)); + counter = increaseCounter(counter); + } + if (associatedDataBlockCount % 2 === messageBlockCount % 2) { + [state] = rho(state, zeroedBuffer(16)); + } + else { + [state] = rho(state, combinedDataBlocks[associatedDataBlockCount + messageBlockCount]); + counter = increaseCounter(counter); + } + const [, authenticationTag] = rho((0, skinny_128_384_plus_1.skinnyEncrypt)(state, (0, skinny_128_384_plus_1.tweakeyEncode)(counter, domainSeparation, nonce, key)), zeroedBuffer(16)); + if (message.length === 0) { + return authenticationTag; + } + state = [...authenticationTag]; + counter = resetCounter(); + const originalFinalMessageBlockLength = messageBlocks[messageBlockCount].length; + messageBlocks[messageBlockCount] = pad(messageBlocks[messageBlockCount], 16); + for (let i = 1; i < messageBlockCount + 1; i++) { + state = (0, skinny_128_384_plus_1.skinnyEncrypt)(state, (0, skinny_128_384_plus_1.tweakeyEncode)(counter, 4, nonce, key)); + let cBlock; + [state, cBlock] = rho(state, messageBlocks[i]); + counter = increaseCounter(counter); + if (i < messageBlockCount) { + ciphertext.push(...cBlock); + } + else { + ciphertext.push(...cBlock.slice(0, originalFinalMessageBlockLength)); + } + } + // The authentication tag is stored in the final 16 bytes of the ciphertext. + ciphertext.push(...authenticationTag); + return ciphertext; +} +exports.cryptoAeadEncrypt = cryptoAeadEncrypt; +/** + * Decrypt a message using the Romulus-M cryptography specification. + * @param ciphertext The ciphertext to decrypt. + * @param associatedData The associated data. + * @param nonce The nonce. + * @param key The key. + * @returns The decrypted plaintext. + */ +function cryptoAeadDecrypt(ciphertext, associatedData, nonce, key) { + // Buffer for decrypted message. + const message = []; + // The authentication tag is represented by the final 16 bytes of the ciphertext. + const authenticationTag = ciphertext.slice(-16); + ciphertext.length -= 16; + // Reset state and counter. + let state = zeroedBuffer(16); + let counter = resetCounter(); + if (ciphertext.length !== 0) { + // Combine the ciphertext + state = [...authenticationTag]; + const ciphertextBlocks = parse(ciphertext, 16); + const ciphertextBlockCount = ciphertextBlocks.length - 1; + const finalCiphertextBlockLength = ciphertextBlocks[ciphertextBlockCount].length; + ciphertextBlocks[ciphertextBlockCount] = pad(ciphertextBlocks[ciphertextBlockCount], 16); + for (let i = 1; i < ciphertextBlockCount + 1; i++) { + state = (0, skinny_128_384_plus_1.skinnyEncrypt)(state, (0, skinny_128_384_plus_1.tweakeyEncode)(counter, 4, nonce, key)); + let mBlock; + [state, mBlock] = inverseRoh(state, ciphertextBlocks[i]); + counter = increaseCounter(counter); + if (i < ciphertextBlockCount) { + message.push(...mBlock); + } + else { + message.push(...mBlock.slice(0, finalCiphertextBlockLength)); + } + } + } + else { + state = []; + } + // Reset state and counter. + state = zeroedBuffer(16); + counter = resetCounter(); + // Carve the message and associated data into blocks. + const messageBlocks = parse(message, 16); + const messageBlockLength = messageBlocks.length - 1; + const associatedDataBlocks = parse(associatedData, 16); + const associatedDataBlockCount = associatedDataBlocks.length - 1; + // Concatenate the message and associated data blocks, excluding each array's first element. + const combinedData = associatedDataBlocks.slice(1).concat(messageBlocks.slice(1)); + // Insert empty array at position 0. + combinedData.splice(0, 0, []); + // Calculate domain separation for final decryption stage. + const domainSeparation = calculateDomainSeparation(combinedData, messageBlockLength, associatedDataBlockCount); + // Pad combined data. + combinedData[associatedDataBlockCount] = pad(combinedData[associatedDataBlockCount], 16); + combinedData[associatedDataBlockCount + messageBlockLength] = pad(combinedData[associatedDataBlockCount + messageBlockLength], 16); + let x = 8; + for (let i = 1; i < Math.floor((associatedDataBlockCount + messageBlockLength) / 2) + 1; i++) { + [state] = rho(state, combinedData[2 * i - 1]); + counter = increaseCounter(counter); + if (i === Math.floor(associatedDataBlockCount / 2) + 1) { + x = x ^ 4; + } + state = (0, skinny_128_384_plus_1.skinnyEncrypt)(state, (0, skinny_128_384_plus_1.tweakeyEncode)(counter, x, combinedData[2 * i], key)); + counter = increaseCounter(counter); + } + if (associatedDataBlockCount % 2 === messageBlockLength % 2) { + [state] = rho(state, zeroedBuffer(16)); + } + else { + [state] = rho(state, combinedData[associatedDataBlockCount + messageBlockLength]); + counter = increaseCounter(counter); + } + // Calculate authentication tag. + const [, computedTag] = rho((0, skinny_128_384_plus_1.skinnyEncrypt)(state, (0, skinny_128_384_plus_1.tweakeyEncode)(counter, domainSeparation, nonce, key)), zeroedBuffer(16)); + let compare = 0; + for (let i = 0; i < 16; i++) { + compare |= (authenticationTag[i] ^ computedTag[i]); + } + if (compare !== 0) { + return []; + } + else { + return message; + } +} +exports.cryptoAeadDecrypt = cryptoAeadDecrypt; +//# sourceMappingURL=romulus-m.js.map \ No newline at end of file diff --git a/dist/romulus-m.js.map b/dist/romulus-m.js.map new file mode 100644 index 0000000..05c314d --- /dev/null +++ b/dist/romulus-m.js.map @@ -0,0 +1 @@ +{"version":3,"file":"romulus-m.js","sourceRoot":"","sources":["../src/romulus-m.ts"],"names":[],"mappings":";;;AAAA,2CAA4C;AAC5C,+DAAoE;AAEpE;;;;;GAKG;AACH,SAAS,KAAK,CAAE,OAAiB,EAAE,WAAmB;IACpD,kEAAkE;IAClE,IAAI,MAAM,GAAG,CAAC,CAAA;IAEd,6BAA6B;IAC7B,IAAI,GAAG,GAAe,EAAE,CAAA;IACxB,OAAO,OAAO,CAAC,MAAM,GAAG,MAAM,IAAI,WAAW,EAAE;QAC7C,GAAG,CAAC,IAAI,CAAC,GAAG,CAAC,OAAO,CAAC,KAAK,CAAC,MAAM,EAAE,MAAM,G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,CAAA;KACV;SAAM;QACL,OAAO,OAAO,CAAA;KACf;AACH,CAAC;AA5FD,8CA4FC"} \ No newline at end of file diff --git a/dist/skinny-128-384-plus.d.ts b/dist/skinny-128-384-plus.d.ts new file mode 100644 index 0000000..1c65554 --- /dev/null +++ b/dist/skinny-128-384-plus.d.ts @@ -0,0 +1,16 @@ +/** + * Create a tweakey based on the specified domain separation, nonce, key and current counter state. + * @param counter The counter. + * @param domainSeparation The domain separation. + * @param nonce The nonce. + * @param key The encryption key. + * @returns The tweakey. + */ +export declare function tweakeyEncode(counter: number[], domainSeparation: number, nonce: number[], key: number[]): number[]; +/** + * Perform a round of SKINNY-188/384+ encryption. + * @param plaintext The plaintext to encrypt. + * @param tweakey The tweakey to use for encryption. + * @returns The ciphertext. + */ +export declare function skinnyEncrypt(plaintext: number[], tweakey: number[]): number[]; diff --git a/dist/skinny-128-384-plus.js b/dist/skinny-128-384-plus.js new file mode 100644 index 0000000..e59da9e --- /dev/null +++ b/dist/skinny-128-384-plus.js @@ -0,0 +1,59 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.skinnyEncrypt = exports.tweakeyEncode = void 0; +const constants_1 = require("./constants"); +/** + * Create a tweakey based on the specified domain separation, nonce, key and current counter state. + * @param counter The counter. + * @param domainSeparation The domain separation. + * @param nonce The nonce. + * @param key The encryption key. + * @returns The tweakey. + */ +function tweakeyEncode(counter, domainSeparation, nonce, key) { + return counter.concat([domainSeparation ^ constants_1.MEMBER_MASK], Array(8).fill(0), nonce, key); +} +exports.tweakeyEncode = tweakeyEncode; +/** + * Perform a round of SKINNY-188/384+ encryption. + * @param plaintext The plaintext to encrypt. + * @param tweakey The tweakey to use for encryption. + * @returns The ciphertext. + */ +function skinnyEncrypt(plaintext, tweakey) { + const tk = Array(constants_1.NB_ROUNDS + 1).fill(Array(constants_1.TWEAK_LENGTH).fill(0)); + tk[0] = [...Array(constants_1.TWEAK_LENGTH).keys()].map(i => tweakey[i]); + for (let i = 0; i < constants_1.NB_ROUNDS - 1; i++) { + tk[i + 1] = [...tk[i]]; + for (let j = 0; j < constants_1.TWEAK_LENGTH; j++) { + tk[i + 1][j] = tk[i][j - j % 16 + constants_1.PT[j % 16]]; + } + for (let j = 0; j < 8; j++) { + tk[i + 1][j + 16] = constants_1.LFSR_8_TK2[tk[i + 1][j + 16]]; + tk[i + 1][j + 32] = constants_1.LFSR_8_TK3[tk[i + 1][j + 32]]; + } + } + let s = [...Array(16).keys()].map(i => plaintext[i]); + for (let i = 0; i < constants_1.NB_ROUNDS; i++) { + for (let j = 0; j < 16; j++) { + s[j] = constants_1.S8[s[j]]; + } + s[0] ^= (constants_1.C[i] & 0xf); + s[4] ^= (constants_1.C[i] >> 4) & 0xf; + s[8] ^= 0x2; + for (let j = 0; j < 8; j++) { + s[j] ^= tk[i][j] ^ tk[i][j + 16] ^ tk[i][j + 32]; + } + s = [s[0], s[1], s[2], s[3], s[7], s[4], s[5], s[6], s[10], s[11], s[8], s[9], s[13], s[14], s[15], s[12]]; + for (let j = 0; j < 4; j++) { + const tmp = [...s]; + s[j] = tmp[j] ^ tmp[8 + j] ^ tmp[12 + j]; + s[4 + j] = tmp[j]; + s[8 + j] = tmp[4 + j] ^ tmp[8 + j]; + s[12 + j] = tmp[0 + j] ^ tmp[8 + j]; + } + } + return [...Array(16).keys()].map(i => s[i]); +} +exports.skinnyEncrypt = skinnyEncrypt; +//# sourceMappingURL=skinny-128-384-plus.js.map \ No newline at end of file diff --git a/dist/skinny-128-384-plus.js.map b/dist/skinny-128-384-plus.js.map new file mode 100644 index 0000000..db4918b --- /dev/null +++ b/dist/skinny-128-384-plus.js.map @@ -0,0 +1 @@ +{"version":3,"file":"skinny-128-384-plus.js","sourceRoot":"","sources":["../src/skinny-128-384-plus.ts"],"names":[],"mappings":";;;AAAA,2CAAqG;AAErG;;;;;;;GAOG;AACH,SAAgB,aAAa,CAAE,OAAiB,EAAE,gBAAwB,EAAE,KAAe,EAAE,GAAa;IACxG,OAAO,OA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