Skip to content

Commit fd49993

Browse files
committed
First part of the FIPS module.
Change-Id: Ic3a91ccd2c8cdc364740f256fdb8a7ff66177947 Reviewed-on: https://boringssl-review.googlesource.com/14506 Reviewed-by: Adam Langley <[email protected]> Commit-Queue: Adam Langley <[email protected]>
1 parent 0ef8c7b commit fd49993

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+1708
-379
lines changed

CMakeLists.txt

+4
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,10 @@ if (OPENSSL_NO_ASM)
236236
set(ARCH "generic")
237237
endif()
238238

239+
if(FIPS)
240+
add_definitions(-DBORINGSSL_FIPS)
241+
endif()
242+
239243
# Add minimal googletest targets. The provided one has many side-effects, and
240244
# googletest has a very straightforward build.
241245
add_library(gtest third_party/googletest/src/gtest-all.cc)

crypto/CMakeLists.txt

+33-11
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,6 @@ add_subdirectory(bytestring)
6767
add_subdirectory(pool)
6868

6969
# Level 0.2 - depends on nothing but itself
70-
add_subdirectory(sha)
71-
add_subdirectory(md4)
72-
add_subdirectory(md5)
7370
add_subdirectory(modes)
7471
add_subdirectory(aes)
7572
add_subdirectory(des)
@@ -80,7 +77,7 @@ add_subdirectory(poly1305)
8077
add_subdirectory(curve25519)
8178

8279
# Level 1, depends only on 0.*
83-
add_subdirectory(digest)
80+
add_subdirectory(digest_extra)
8481
add_subdirectory(cipher)
8582
add_subdirectory(rand)
8683
add_subdirectory(bio)
@@ -96,7 +93,7 @@ add_subdirectory(rsa)
9693
add_subdirectory(ec)
9794
add_subdirectory(ecdh)
9895
add_subdirectory(ecdsa)
99-
add_subdirectory(hmac)
96+
add_subdirectory(hmac_extra)
10097

10198
# Level 3
10299
add_subdirectory(cmac)
@@ -112,8 +109,12 @@ add_subdirectory(pkcs8)
112109
# Test support code
113110
add_subdirectory(test)
114111

112+
add_subdirectory(fipsmodule)
113+
115114
add_library(
116-
crypto
115+
crypto_base
116+
117+
OBJECT
117118

118119
cpu-aarch64-linux.c
119120
cpu-arm.c
@@ -129,17 +130,31 @@ add_library(
129130
thread_none.c
130131
thread_pthread.c
131132
thread_win.c
133+
)
134+
135+
if(FIPS)
136+
SET_SOURCE_FILES_PROPERTIES(fipsmodule/bcm.o PROPERTIES EXTERNAL_OBJECT true)
137+
SET_SOURCE_FILES_PROPERTIES(fipsmodule/bcm.o PROPERTIES GENERATED true)
138+
139+
set(
140+
CRYPTO_FIPS_OBJECTS
141+
142+
fipsmodule/bcm.o
143+
)
144+
endif()
145+
146+
add_library(
147+
crypto
132148

149+
$<TARGET_OBJECTS:crypto_base>
133150
$<TARGET_OBJECTS:stack>
134151
$<TARGET_OBJECTS:lhash>
135152
$<TARGET_OBJECTS:err>
136153
$<TARGET_OBJECTS:base64>
137154
$<TARGET_OBJECTS:bytestring>
138155
$<TARGET_OBJECTS:pool>
139-
$<TARGET_OBJECTS:sha>
140-
$<TARGET_OBJECTS:md4>
141-
$<TARGET_OBJECTS:md5>
142-
$<TARGET_OBJECTS:digest>
156+
$<TARGET_OBJECTS:fipsmodule>
157+
$<TARGET_OBJECTS:digest_extra>
143158
$<TARGET_OBJECTS:cipher>
144159
$<TARGET_OBJECTS:modes>
145160
$<TARGET_OBJECTS:aes>
@@ -162,16 +177,23 @@ add_library(
162177
$<TARGET_OBJECTS:ec>
163178
$<TARGET_OBJECTS:ecdh>
164179
$<TARGET_OBJECTS:ecdsa>
165-
$<TARGET_OBJECTS:hmac>
166180
$<TARGET_OBJECTS:cmac>
167181
$<TARGET_OBJECTS:evp>
168182
$<TARGET_OBJECTS:hkdf>
169183
$<TARGET_OBJECTS:pem>
170184
$<TARGET_OBJECTS:x509>
171185
$<TARGET_OBJECTS:x509v3>
172186
$<TARGET_OBJECTS:pkcs8_lib>
187+
188+
${CRYPTO_FIPS_OBJECTS}
173189
)
174190

191+
if(FIPS)
192+
add_dependencies(crypto bcm_o_target)
193+
endif()
194+
195+
SET_TARGET_PROPERTIES(crypto PROPERTIES LINKER_LANGUAGE C)
196+
175197
if(NOT MSVC AND NOT ANDROID)
176198
target_link_libraries(crypto pthread)
177199
endif()

crypto/crypto.c

+5-2
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,11 @@ uint32_t OPENSSL_armcap_P = 0;
9393

9494
#endif
9595

96+
#if defined(BORINGSSL_FIPS)
97+
/* In FIPS mode, the power-on self-test function calls |CRYPTO_library_init|
98+
* because we have to ensure that CPUID detection occurs first. */
99+
#define BORINGSSL_NO_STATIC_INITIALIZER
100+
#endif
96101

97102
#if defined(OPENSSL_WINDOWS) && !defined(BORINGSSL_NO_STATIC_INITIALIZER)
98103
#define OPENSSL_CDECL __cdecl
@@ -166,5 +171,3 @@ int ENGINE_register_all_complete(void) {
166171
}
167172

168173
void OPENSSL_load_builtin_modules(void) {}
169-
170-
int FIPS_mode(void) { return 0; }

crypto/digest/CMakeLists.txt crypto/digest_extra/CMakeLists.txt

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
include_directories(../../include)
22

33
add_library(
4-
digest
4+
digest_extra
55

66
OBJECT
77

8-
digest.c
9-
digests.c
8+
digest_extra.c
109
)
1110

1211
add_executable(

crypto/digest/digests.c crypto/digest_extra/digest_extra.c

-191
Original file line numberDiff line numberDiff line change
@@ -56,206 +56,15 @@
5656

5757
#include <openssl/digest.h>
5858

59-
#include <assert.h>
6059
#include <string.h>
6160

6261
#include <openssl/asn1.h>
6362
#include <openssl/bytestring.h>
64-
#include <openssl/md4.h>
65-
#include <openssl/md5.h>
6663
#include <openssl/nid.h>
67-
#include <openssl/sha.h>
6864

6965
#include "internal.h"
7066
#include "../internal.h"
7167

72-
#if defined(NDEBUG)
73-
#define CHECK(x) (void) (x)
74-
#else
75-
#define CHECK(x) assert(x)
76-
#endif
77-
78-
79-
static void md4_init(EVP_MD_CTX *ctx) {
80-
CHECK(MD4_Init(ctx->md_data));
81-
}
82-
83-
static void md4_update(EVP_MD_CTX *ctx, const void *data, size_t count) {
84-
CHECK(MD4_Update(ctx->md_data, data, count));
85-
}
86-
87-
static void md4_final(EVP_MD_CTX *ctx, uint8_t *out) {
88-
CHECK(MD4_Final(out, ctx->md_data));
89-
}
90-
91-
static const EVP_MD md4_md = {
92-
NID_md4, MD4_DIGEST_LENGTH, 0 /* flags */, md4_init,
93-
md4_update, md4_final, 64 /* block size */, sizeof(MD4_CTX),
94-
};
95-
96-
const EVP_MD *EVP_md4(void) { return &md4_md; }
97-
98-
99-
static void md5_init(EVP_MD_CTX *ctx) {
100-
CHECK(MD5_Init(ctx->md_data));
101-
}
102-
103-
static void md5_update(EVP_MD_CTX *ctx, const void *data, size_t count) {
104-
CHECK(MD5_Update(ctx->md_data, data, count));
105-
}
106-
107-
static void md5_final(EVP_MD_CTX *ctx, uint8_t *out) {
108-
CHECK(MD5_Final(out, ctx->md_data));
109-
}
110-
111-
static const EVP_MD md5_md = {
112-
NID_md5, MD5_DIGEST_LENGTH, 0 /* flags */, md5_init,
113-
md5_update, md5_final, 64 /* block size */, sizeof(MD5_CTX),
114-
};
115-
116-
const EVP_MD *EVP_md5(void) { return &md5_md; }
117-
118-
119-
static void sha1_init(EVP_MD_CTX *ctx) {
120-
CHECK(SHA1_Init(ctx->md_data));
121-
}
122-
123-
static void sha1_update(EVP_MD_CTX *ctx, const void *data, size_t count) {
124-
CHECK(SHA1_Update(ctx->md_data, data, count));
125-
}
126-
127-
static void sha1_final(EVP_MD_CTX *ctx, uint8_t *md) {
128-
CHECK(SHA1_Final(md, ctx->md_data));
129-
}
130-
131-
static const EVP_MD sha1_md = {
132-
NID_sha1, SHA_DIGEST_LENGTH, 0 /* flags */, sha1_init,
133-
sha1_update, sha1_final, 64 /* block size */, sizeof(SHA_CTX),
134-
};
135-
136-
const EVP_MD *EVP_sha1(void) { return &sha1_md; }
137-
138-
139-
static void sha224_init(EVP_MD_CTX *ctx) {
140-
CHECK(SHA224_Init(ctx->md_data));
141-
}
142-
143-
static void sha224_update(EVP_MD_CTX *ctx, const void *data, size_t count) {
144-
CHECK(SHA224_Update(ctx->md_data, data, count));
145-
}
146-
147-
static void sha224_final(EVP_MD_CTX *ctx, uint8_t *md) {
148-
CHECK(SHA224_Final(md, ctx->md_data));
149-
}
150-
151-
static const EVP_MD sha224_md = {
152-
NID_sha224, SHA224_DIGEST_LENGTH, 0 /* flags */,
153-
sha224_init, sha224_update, sha224_final,
154-
64 /* block size */, sizeof(SHA256_CTX),
155-
};
156-
157-
const EVP_MD *EVP_sha224(void) { return &sha224_md; }
158-
159-
160-
static void sha256_init(EVP_MD_CTX *ctx) {
161-
CHECK(SHA256_Init(ctx->md_data));
162-
}
163-
164-
static void sha256_update(EVP_MD_CTX *ctx, const void *data, size_t count) {
165-
CHECK(SHA256_Update(ctx->md_data, data, count));
166-
}
167-
168-
static void sha256_final(EVP_MD_CTX *ctx, uint8_t *md) {
169-
CHECK(SHA256_Final(md, ctx->md_data));
170-
}
171-
172-
static const EVP_MD sha256_md = {
173-
NID_sha256, SHA256_DIGEST_LENGTH, 0 /* flags */,
174-
sha256_init, sha256_update, sha256_final,
175-
64 /* block size */, sizeof(SHA256_CTX),
176-
};
177-
178-
const EVP_MD *EVP_sha256(void) { return &sha256_md; }
179-
180-
181-
static void sha384_init(EVP_MD_CTX *ctx) {
182-
CHECK(SHA384_Init(ctx->md_data));
183-
}
184-
185-
static void sha384_update(EVP_MD_CTX *ctx, const void *data, size_t count) {
186-
CHECK(SHA384_Update(ctx->md_data, data, count));
187-
}
188-
189-
static void sha384_final(EVP_MD_CTX *ctx, uint8_t *md) {
190-
CHECK(SHA384_Final(md, ctx->md_data));
191-
}
192-
193-
static const EVP_MD sha384_md = {
194-
NID_sha384, SHA384_DIGEST_LENGTH, 0 /* flags */,
195-
sha384_init, sha384_update, sha384_final,
196-
128 /* block size */, sizeof(SHA512_CTX),
197-
};
198-
199-
const EVP_MD *EVP_sha384(void) { return &sha384_md; }
200-
201-
202-
static void sha512_init(EVP_MD_CTX *ctx) {
203-
CHECK(SHA512_Init(ctx->md_data));
204-
}
205-
206-
static void sha512_update(EVP_MD_CTX *ctx, const void *data, size_t count) {
207-
CHECK(SHA512_Update(ctx->md_data, data, count));
208-
}
209-
210-
static void sha512_final(EVP_MD_CTX *ctx, uint8_t *md) {
211-
CHECK(SHA512_Final(md, ctx->md_data));
212-
}
213-
214-
static const EVP_MD sha512_md = {
215-
NID_sha512, SHA512_DIGEST_LENGTH, 0 /* flags */,
216-
sha512_init, sha512_update, sha512_final,
217-
128 /* block size */, sizeof(SHA512_CTX),
218-
};
219-
220-
const EVP_MD *EVP_sha512(void) { return &sha512_md; }
221-
222-
223-
typedef struct {
224-
MD5_CTX md5;
225-
SHA_CTX sha1;
226-
} MD5_SHA1_CTX;
227-
228-
static void md5_sha1_init(EVP_MD_CTX *md_ctx) {
229-
MD5_SHA1_CTX *ctx = md_ctx->md_data;
230-
CHECK(MD5_Init(&ctx->md5) && SHA1_Init(&ctx->sha1));
231-
}
232-
233-
static void md5_sha1_update(EVP_MD_CTX *md_ctx, const void *data,
234-
size_t count) {
235-
MD5_SHA1_CTX *ctx = md_ctx->md_data;
236-
CHECK(MD5_Update(&ctx->md5, data, count) &&
237-
SHA1_Update(&ctx->sha1, data, count));
238-
}
239-
240-
static void md5_sha1_final(EVP_MD_CTX *md_ctx, uint8_t *out) {
241-
MD5_SHA1_CTX *ctx = md_ctx->md_data;
242-
CHECK(MD5_Final(out, &ctx->md5) &&
243-
SHA1_Final(out + MD5_DIGEST_LENGTH, &ctx->sha1));
244-
}
245-
246-
static const EVP_MD md5_sha1_md = {
247-
NID_md5_sha1,
248-
MD5_DIGEST_LENGTH + SHA_DIGEST_LENGTH,
249-
0 /* flags */,
250-
md5_sha1_init,
251-
md5_sha1_update,
252-
md5_sha1_final,
253-
64 /* block size */,
254-
sizeof(MD5_SHA1_CTX),
255-
};
256-
257-
const EVP_MD *EVP_md5_sha1(void) { return &md5_sha1_md; }
258-
25968

26069
struct nid_to_digest {
26170
int nid;
File renamed without changes.

crypto/digest_extra/internal.h

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/* Copyright (c) 2017, Google Inc.
2+
*
3+
* Permission to use, copy, modify, and/or distribute this software for any
4+
* purpose with or without fee is hereby granted, provided that the above
5+
* copyright notice and this permission notice appear in all copies.
6+
*
7+
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8+
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9+
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
10+
* SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11+
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
12+
* OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
13+
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
14+
15+
#ifndef OPENSSL_HEADER_DIGEST_EXTRA_INTERNAL_H
16+
#define OPENSSL_HEADER_DIGEST_EXTRA_INTERNAL_H
17+
18+
#include <openssl/base.h>
19+
20+
#if defined(__cplusplus)
21+
extern "C" {
22+
#endif
23+
24+
25+
const EVP_MD *EVP_parse_digest_algorithm(CBS *cbs);
26+
27+
28+
#if defined(__cplusplus)
29+
} /* extern C */
30+
#endif
31+
32+
#endif /* OPENSSL_HEADER_DIGEST_EXTRA_INTERNAL */

crypto/evp/digestsign.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858
#include <openssl/err.h>
5959

6060
#include "internal.h"
61-
#include "../digest/internal.h"
61+
#include "../fipsmodule/digest/internal.h"
6262

6363

6464
static const struct evp_md_pctx_ops md_pctx_ops = {

0 commit comments

Comments
 (0)