xref: /aosp_15_r20/external/pigweed/pw_crypto/BUILD.gn (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1# Copyright 2021 The Pigweed Authors
2#
3# Licensed under the Apache License, Version 2.0 (the "License"); you may not
4# use this file except in compliance with the License. You may obtain a copy of
5# the License at
6#
7#     https://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12# License for the specific language governing permissions and limitations under
13# the License.
14
15import("//build_overrides/pigweed.gni")
16
17import("$dir_pw_bloat/bloat.gni")
18import("$dir_pw_build/facade.gni")
19import("$dir_pw_build/target_types.gni")
20import("$dir_pw_crypto/backend.gni")
21import("$dir_pw_docgen/docs.gni")
22import("$dir_pw_third_party/micro_ecc/micro_ecc.gni")
23import("$dir_pw_unit_test/test.gni")
24
25config("default_config") {
26  include_dirs = [ "public" ]
27  visibility = [ ":*" ]
28}
29
30pw_facade("sha256") {
31  backend = pw_crypto_SHA256_BACKEND
32  public_configs = [ ":default_config" ]
33  public = [ "public/pw_crypto/sha256.h" ]
34  public_deps = [
35    "$dir_pw_bytes",
36    "$dir_pw_log",
37    "$dir_pw_status",
38    "$dir_pw_stream",
39  ]
40  deps = [ "$dir_pw_assert" ]
41}
42
43pw_doc_group("docs") {
44  sources = [ "docs.rst" ]
45  report_deps = [ ":size_report" ]
46}
47
48pw_size_diff("size_report") {
49  title = "pw::crypto Size Report"
50  base = "$dir_pw_bloat:bloat_base"
51
52  binaries = []
53
54  if (pw_crypto_SHA256_BACKEND != "") {
55    binaries += [
56      {
57        target = "size_report:sha256_simple"
58        label = "SHA256 ($pw_crypto_SHA256_BACKEND)"
59      },
60    ]
61  }
62
63  if (pw_crypto_ECDSA_BACKEND != "") {
64    binaries += [
65      {
66        target = "size_report:ecdsa_p256_verify"
67        label = "ECDSA P256 Verify ($pw_crypto_ECDSA_BACKEND)"
68      },
69    ]
70  }
71
72  if (binaries == []) {
73    binaries += [
74      {
75        target = "$dir_pw_bloat:bloat_base"
76        label = "No backend is selected."
77      },
78    ]
79  }
80}
81
82pw_test_group("tests") {
83  tests = [
84    ":sha256_test",
85    ":sha256_mock_test",
86    ":ecdsa_test",
87  ]
88  if (dir_pw_third_party_micro_ecc != "") {
89    tests += [ ":ecdsa_uecc_little_endian_test" ]
90  }
91}
92
93# Sha256 tests against the selected real backend.
94pw_test("sha256_test") {
95  enable_if = pw_crypto_SHA256_BACKEND != ""
96  deps = [ ":sha256" ]
97  sources = [ "sha256_test.cc" ]
98}
99
100config("mock_config") {
101  visibility = [ ":*" ]
102  include_dirs = [ "public_overrides/mock" ]
103}
104
105pw_source_set("sha256_mock") {
106  public_configs = [ ":mock_config" ]
107  public = [
108    "public/pw_crypto/sha256_mock.h",
109    "public_overrides/mock/pw_crypto/sha256_backend.h",
110  ]
111  sources = [ "sha256_mock.cc" ]
112  public_deps = [ ":sha256.facade" ]
113}
114
115# Sha256 frontend tests against a mocked backend.
116pw_test("sha256_mock_test") {
117  # Depend on ":sha256.facade" instead of ":sha256" to bypass normal backend
118  # selection via `pw_crypto_SHA256_BACKEND`.
119  deps = [
120    ":sha256.facade",
121    ":sha256_mock",
122  ]
123  sources = [ "sha256_mock_test.cc" ]
124}
125
126config("mbedtls_config") {
127  visibility = [ ":*" ]
128  include_dirs = [ "public_overrides/mbedtls" ]
129}
130
131pw_source_set("sha256_mbedtls") {
132  public_configs = [ ":mbedtls_config" ]
133  public = [
134    "public/pw_crypto/sha256_mbedtls.h",
135    "public_overrides/mbedtls/pw_crypto/sha256_backend.h",
136  ]
137  sources = [ "sha256_mbedtls.cc" ]
138  public_deps = [
139    ":sha256.facade",
140    "$dir_pw_third_party/mbedtls",
141  ]
142}
143
144pw_source_set("sha256_mbedtls_v3") {
145  public_configs = [ ":mbedtls_config" ]
146  public = [
147    "public/pw_crypto/sha256_mbedtls.h",
148    "public_overrides/mbedtls/pw_crypto/sha256_backend.h",
149  ]
150  sources = [ "sha256_mbedtls.cc" ]
151  public_deps = [
152    ":sha256.facade",
153    "$dir_pw_third_party/mbedtls:mbedtls_v3",
154  ]
155}
156
157pw_facade("ecdsa") {
158  backend = pw_crypto_ECDSA_BACKEND
159  public_configs = [ ":default_config" ]
160  public = [ "public/pw_crypto/ecdsa.h" ]
161  public_deps = [
162    "$dir_pw_bytes",
163    "$dir_pw_status",
164  ]
165}
166
167pw_source_set("ecdsa_mbedtls") {
168  sources = [ "ecdsa_mbedtls.cc" ]
169  deps = [
170    "$dir_pw_function",
171    "$dir_pw_log",
172    "$dir_pw_third_party/mbedtls",
173  ]
174  public_deps = [ ":ecdsa.facade" ]
175}
176
177pw_source_set("ecdsa_mbedtls_v3") {
178  sources = [ "ecdsa_mbedtls.cc" ]
179  deps = [
180    "$dir_pw_function",
181    "$dir_pw_log",
182    "$dir_pw_third_party/mbedtls:mbedtls_v3",
183  ]
184  public_deps = [ ":ecdsa.facade" ]
185}
186
187pw_source_set("ecdsa_uecc") {
188  sources = [ "ecdsa_uecc.cc" ]
189  deps = [
190    "$dir_pw_log",
191    "$dir_pw_third_party/micro_ecc",
192  ]
193  public_deps = [ ":ecdsa.facade" ]
194}
195
196if (dir_pw_third_party_micro_ecc != "") {
197  pw_source_set("ecdsa_uecc_little_endian") {
198    sources = [ "ecdsa_uecc.cc" ]
199    deps = [
200      "$dir_pw_log",
201      "$dir_pw_third_party/micro_ecc:micro_ecc_little_endian",
202    ]
203    public_deps = [ ":ecdsa.facade" ]
204  }
205
206  # This test targets the micro_ecc little endian backend specifically.
207  #
208  # TODO: b/273819841 - deduplicate all backend tests.
209  pw_test("ecdsa_uecc_little_endian_test") {
210    sources = [ "ecdsa_test.cc" ]
211    deps = [ ":ecdsa_uecc_little_endian" ]
212  }
213}
214
215# This test targets the specific backend pointed to by
216# pw_crypto_ECDSA_BACKEND.
217pw_test("ecdsa_test") {
218  enable_if = pw_crypto_ECDSA_BACKEND != ""
219  deps = [ ":ecdsa" ]
220  sources = [ "ecdsa_test.cc" ]
221}
222