This repository has been archived by the owner on Feb 5, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Extract shared CMAC/SIV code into a Util module - Add a ct_equal to do constant time-ish comparisons - Add a CodeClimate config
- Loading branch information
Showing
8 changed files
with
83 additions
and
100 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
/.bundle/ | ||
/.yardoc | ||
/.rakeTasks | ||
/Gemfile.lock | ||
/_yardoc/ | ||
/coverage/ | ||
|
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
# encoding: binary | ||
# frozen_string_literal: true | ||
|
||
module SIVChain | ||
# Utility functions | ||
module Util | ||
DOUBLE_CONSTANT = (("\x0" * 15) + "\x87").freeze | ||
|
||
module_function | ||
|
||
# Perform a doubling operation as described in the CMAC and SIV papers | ||
def double(value) | ||
overflow = 0 | ||
words = value.unpack("N4").reverse | ||
words = words.map do |word| | ||
new_word = (word << 1) & 0xFFFFFFFF | ||
new_word |= overflow | ||
overflow = (word & 0x80000000) >= 0x80000000 ? 1 : 0 | ||
new_word | ||
end | ||
result = words.reverse.pack("N4") | ||
|
||
# TODO: not constant time! | ||
return result if value[0].ord < 0x80 | ||
xor(result, DOUBLE_CONSTANT) | ||
end | ||
|
||
# Perform an xor on arbitrary bytestrings | ||
def xor(a, b) | ||
length = [a.length, b.length].min | ||
output = "\0" * length | ||
length.times do |i| | ||
output[i] = (a[i].ord ^ b[i].ord).chr | ||
end | ||
output | ||
end | ||
|
||
# Perform a constant time-ish comparison of two bytestrings | ||
def ct_equal(a, b) | ||
return false unless a.bytesize == b.bytesize | ||
|
||
l = a.unpack("C*") | ||
r = 0 | ||
i = -1 | ||
|
||
b.each_byte { |v| r |= v ^ l[i += 1] } | ||
|
||
r.zero? | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters