Skip to content
Permalink
Browse files

Initial commit

  • Loading branch information...
fhunleth committed Oct 11, 2018
0 parents commit 50c35aa54b44e197de68824cb362b7d11725d547
Showing with 338 additions and 0 deletions.
  1. +4 −0 .formatter.exs
  2. +24 −0 .gitignore
  3. +21 −0 README.md
  4. +30 −0 config/config.exs
  5. +77 −0 lib/atecc508a.ex
  6. +83 −0 lib/atecc508a/date.ex
  7. +27 −0 mix.exs
  8. +3 −0 mix.lock
  9. +68 −0 test/atecc508a_test.exs
  10. +1 −0 test/test_helper.exs
@@ -0,0 +1,4 @@
# Used by "mix format"
[
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"]
]
@@ -0,0 +1,24 @@
# The directory Mix will write compiled artifacts to.
/_build/

# If you run "mix test --cover", coverage assets end up here.
/cover/

# The directory Mix downloads your dependencies sources to.
/deps/

# Where 3rd-party dependencies like ExDoc output generated docs.
/doc/

# Ignore .fetch files in case you like to edit your project deps locally.
/.fetch

# If the VM crashes, it generates a dump, let's ignore it too.
erl_crash.dump

# Also ignore archive artifacts (built via "mix archive.build").
*.ez

# Ignore package tarball (built via "mix hex.build").
atecc508a-*.tar

@@ -0,0 +1,21 @@
# Atecc508a

**TODO: Add description**

## Installation

If [available in Hex](https://hex.pm/docs/publish), the package can be installed
by adding `atecc508a` to your list of dependencies in `mix.exs`:

```elixir
def deps do
[
{:atecc508a, "~> 0.1.0"}
]
end
```

Documentation can be generated with [ExDoc](https://github.com/elixir-lang/ex_doc)
and published on [HexDocs](https://hexdocs.pm). Once published, the docs can
be found at [https://hexdocs.pm/atecc508a](https://hexdocs.pm/atecc508a).

@@ -0,0 +1,30 @@
# This file is responsible for configuring your application
# and its dependencies with the aid of the Mix.Config module.
use Mix.Config

# This configuration is loaded before any dependency and is restricted
# to this project. If another project depends on this project, this
# file won't be loaded nor affect the parent project. For this reason,
# if you want to provide default values for your application for
# 3rd-party users, it should be done in your "mix.exs" file.

# You can configure your application as:
#
# config :atecc508a, key: :value
#
# and access this configuration in your application as:
#
# Application.get_env(:atecc508a, :key)
#
# You can also configure a 3rd-party app:
#
# config :logger, level: :info
#

# It is also possible to import configuration files, relative to this
# directory. For example, you can emulate configuration per environment
# by uncommenting the line below and defining dev.exs, test.exs and such.
# Configuration from the imported file will override the ones defined
# here (which is why it is important to import them last).
#
# import_config "#{Mix.env()}.exs"
@@ -0,0 +1,77 @@
defmodule ATECC508A do
@moduledoc """
Documentation for Atecc508a.
"""

@doc """
Hello world.
"""

@spec decode(<<_::24>>) :: {DateTime.t(), DateTime.t()}
def decode(<<raw_year::5, month::4, day::5, hour::5, expire_years::5>>) do
issue_date = %DateTime{
year: raw_year + 2000,
month: month,
day: day,
hour: hour,
minute: 0,
second: 0,
microsecond: {0, 0},
std_offset: 0,
utc_offset: 0,
zone_abbr: "UTC",
time_zone: "Etc/UTC"
}

expire_date =
if expire_years != 0 do
%DateTime{issue_date | year: issue_date.year + expire_years}
else
# Special "no expiration date"
max_date()
end

{issue_date, expire_date}
end

@spec encode(DateTime.t(), DateTime.t()) :: <<_::24>>
def encode(issue_date, expire_date) do
expire_years = calc_expire_years(issue_date, expire_date)
issue_year = calc_issue_year(issue_date.year)

<<issue_year::5, issue_date.month::4, issue_date.day::5, issue_date.hour::5, expire_years::5>>
end

defp max_date() do
# See RFC 5280 4.1.2.5.2
%DateTime{
year: 9999,
month: 12,
day: 31,
hour: 23,
minute: 59,
second: 59,
microsecond: {0, 0},
std_offset: 0,
utc_offset: 0,
zone_abbr: "UTC",
time_zone: "Etc/UTC"
}
end

defp calc_issue_year(year) when year < 2000, do: 0
defp calc_issue_year(year) when year > 2031, do: 31
defp calc_issue_year(year), do: year - 2000

defp calc_expire_years(issue_date, expire_date) do
delta_years = expire_date.year - issue_date.year
# delta_years has to fit in 5 bytes and 0 = doesn't expire
cond do
delta_years < 1 -> 1
delta_years > 31 -> 0
true -> delta_years
end
end
end
@@ -0,0 +1,83 @@
defmodule ATECC508A.Date do
@moduledoc """
Handle the ATECC508's encoded dates
"""

@doc """
Decode an issue date/expiration bitstring
"""
@spec decode(<<_::24>>) :: {DateTime.t(), DateTime.t()}
def decode(<<raw_year::5, month::4, day::5, hour::5, expire_years::5>>) do
issue_date = %DateTime{
year: raw_year + 2000,
month: month,
day: day,
hour: hour,
minute: 0,
second: 0,
microsecond: {0, 0},
std_offset: 0,
utc_offset: 0,
zone_abbr: "UTC",
time_zone: "Etc/UTC"
}

expire_date =
if expire_years != 0 do
%DateTime{issue_date | year: issue_date.year + expire_years}
else
# Special "no expiration date"
max_date()
end

{issue_date, expire_date}
end

@doc """
Encode an issue date/expiration bitstring
This function can easily lose precision on the dates and times since
so little is encoded. If accepting arbitrary datetimes, you'll want
to check that the conversion didn't truncate in strange ways.
Important: the max issue year is 2031!!
"""
@spec encode(DateTime.t(), DateTime.t()) :: <<_::24>>
def encode(issue_date, expire_date) do
expire_years = calc_expire_years(issue_date, expire_date)
issue_year = calc_issue_year(issue_date.year)

<<issue_year::5, issue_date.month::4, issue_date.day::5, issue_date.hour::5, expire_years::5>>
end

defp max_date() do
# See RFC 5280 4.1.2.5.2
%DateTime{
year: 9999,
month: 12,
day: 31,
hour: 23,
minute: 59,
second: 59,
microsecond: {0, 0},
std_offset: 0,
utc_offset: 0,
zone_abbr: "UTC",
time_zone: "Etc/UTC"
}
end

defp calc_issue_year(year) when year < 2000, do: 0
defp calc_issue_year(year) when year > 2031, do: 31
defp calc_issue_year(year), do: year - 2000

defp calc_expire_years(issue_date, expire_date) do
delta_years = expire_date.year - issue_date.year
# delta_years has to fit in 5 bytes and 0 = doesn't expire
cond do
delta_years < 1 -> 1
delta_years > 31 -> 0
true -> delta_years
end
end
end
27 mix.exs
@@ -0,0 +1,27 @@
defmodule Atecc508a.MixProject do
use Mix.Project

def project do
[
app: :atecc508a,
version: "0.1.0",
elixir: "~> 1.7",
start_permanent: Mix.env() == :prod,
deps: deps()
]
end

# Run "mix help compile.app" to learn about applications.
def application do
[
extra_applications: [:logger]
]
end

# Run "mix help deps" to learn about dependencies.
defp deps do
[
{:x509, "~> 0.3"}
]
end
end
@@ -0,0 +1,3 @@
%{
"x509": {:hex, :x509, "0.3.0", "c6f3db66960c6e4f424d1e6cca5c7d730e0a577af8dc115a613f4560ce1df6d3", [:mix], [], "hexpm"},
}
@@ -0,0 +1,68 @@
defmodule ATECC508ATest do
use ExUnit.Case
doctest ATECC508A

test "decodes the date in the spec" do
{issue_date, expire_date} = ATECC508A.Date.decode(<<0x75, 0x3E, 0x0E>>)

assert DateTime.compare(issue_date, %DateTime{
year: 2014,
month: 10,
day: 15,
hour: 16,
minute: 0,
second: 0,
microsecond: {0, 0},
std_offset: 0,
utc_offset: 0,
zone_abbr: "UTC",
time_zone: "Etc/UTC"
})

assert DateTime.compare(expire_date, %DateTime{
year: 2028,
month: 10,
day: 15,
hour: 16,
minute: 0,
second: 0,
microsecond: {0, 0},
std_offset: 0,
utc_offset: 0,
zone_abbr: "UTC",
time_zone: "Etc/UTC"
})
end

test "encodes the date in the spec" do
issue_date = %DateTime{
year: 2014,
month: 10,
day: 15,
hour: 16,
minute: 0,
second: 0,
microsecond: {0, 0},
std_offset: 0,
utc_offset: 0,
zone_abbr: "UTC",
time_zone: "Etc/UTC"
}

expire_date = %DateTime{
year: 2028,
month: 10,
day: 15,
hour: 16,
minute: 0,
second: 0,
microsecond: {0, 0},
std_offset: 0,
utc_offset: 0,
zone_abbr: "UTC",
time_zone: "Etc/UTC"
}

assert ATECC508A.Date.encode(issue_date, expire_date) == <<0x75, 0x3E, 0xE>>
end
end
@@ -0,0 +1 @@
ExUnit.start()

0 comments on commit 50c35aa

Please sign in to comment.
You can’t perform that action at this time.