Debian Codegraph @

plugin/world/blowfish_ecb/lib/common.rb

raw symbols SCIP
1 # rubocop:disable Naming/MethodParameterName
2
3 module Blowfish::ECB::Common
4 class << self
5 # call-seq:
6 # encrypt(l, r, Key) -> [int, int]
7 #
8 # Encrypts pseudo-64bit-value (l, r) into bytes using given key.
9 # Both l and r must be 32-bit integers.
10 # Original: https://github.com/openssl/openssl/blob/e8e2b131ca253f9e28c511c8294e27ddbd0b60c6/crypto/bf/bf_enc.c#L30
11 #
12 # Note that unlike the original implementation, this method returns a new tuple representing the result
13 # rather than destructively updating the input argument.
14 def encrypt(l, r, key)
15 l ^= key.p[0]
16 r = bf_enc(r, l, key.s, key.p[1])
17 l = bf_enc(l, r, key.s, key.p[2])
18 r = bf_enc(r, l, key.s, key.p[3])
19 l = bf_enc(l, r, key.s, key.p[4])
20 r = bf_enc(r, l, key.s, key.p[5])
21 l = bf_enc(l, r, key.s, key.p[6])
22 r = bf_enc(r, l, key.s, key.p[7])
23 l = bf_enc(l, r, key.s, key.p[8])
24 r = bf_enc(r, l, key.s, key.p[9])
25 l = bf_enc(l, r, key.s, key.p[10])
26 r = bf_enc(r, l, key.s, key.p[11])
27 l = bf_enc(l, r, key.s, key.p[12])
28 r = bf_enc(r, l, key.s, key.p[13])
29 l = bf_enc(l, r, key.s, key.p[14])
30 r = bf_enc(r, l, key.s, key.p[15])
31 l = bf_enc(l, r, key.s, key.p[16])
32 r ^= key.p[17]
33
34 [r & 0xffffffff, l & 0xffffffff]
35 end
36
37 # call-seq:
38 # decrypt(l, r, Key) -> [int, int]
39 #
40 # Decrypts pseudo-64bit-value (l, r) into bytes using given key.
41 # Both l and r must be 32-bit integers.
42 # Original: https://github.com/openssl/openssl/blob/e8e2b131ca253f9e28c511c8294e27ddbd0b60c6/crypto/bf/bf_enc.c#L69
43 #
44 # Note that unlike the original implementation, this method returns a new tuple representing the result
45 # rather than destructively updating the input argument.
46 def decrypt(l, r, key)
47 l ^= key.p[17]
48 r = bf_enc(r, l, key.s, key.p[16])
49 l = bf_enc(l, r, key.s, key.p[15])
50 r = bf_enc(r, l, key.s, key.p[14])
51 l = bf_enc(l, r, key.s, key.p[13])
52 r = bf_enc(r, l, key.s, key.p[12])
53 l = bf_enc(l, r, key.s, key.p[11])
54 r = bf_enc(r, l, key.s, key.p[10])
55 l = bf_enc(l, r, key.s, key.p[9])
56 r = bf_enc(r, l, key.s, key.p[8])
57 l = bf_enc(l, r, key.s, key.p[7])
58 r = bf_enc(r, l, key.s, key.p[6])
59 l = bf_enc(l, r, key.s, key.p[5])
60 r = bf_enc(r, l, key.s, key.p[4])
61 l = bf_enc(l, r, key.s, key.p[3])
62 r = bf_enc(r, l, key.s, key.p[2])
63 l = bf_enc(l, r, key.s, key.p[1])
64 r ^= key.p[0]
65
66 [r & 0xffffffff, l & 0xffffffff]
67 end
68
69 private
70
71 # call-seq:
72 # bf_enc(int, int, [int], int) -> int
73 #
74 # Calculates one step of the Blowfish encryption.
75 # Original: https://github.com/openssl/openssl/blob/e8e2b131ca253f9e28c511c8294e27ddbd0b60c6/crypto/bf/bf_local.h#L76
76 #
77 # Note that due to limitation in Ruby, unlike what the original BF_ENC is doing,
78 # this method returns the calculated value rather than destructively reassigning l.
79 def bf_enc(l, r, s, p)
80 tmp = s[0x0000 + ((r >> 24) & 0xff)]
81 tmp += s[0x0100 + ((r >> 16) & 0xff)]
82 tmp ^= s[0x0200 + ((r >> 8) & 0xff)]
83 tmp += s[0x0300 + (r & 0xff)]
84 l ^ p ^ (tmp & 0xffffffff)
85 end
86 end
87 end
88
89 # rubocop:enable Naming/MethodParameterName