IoT Security Fundamentals

📖 24 min read

Why IoT Security Is Different

Securing an IoT deployment is not the same as securing a typical web application or enterprise server. The differences are structural. Web servers have abundant compute, stable network connections, and professional administrators managing them. IoT devices often have none of these advantages: they run on microcontrollers with kilobytes of RAM, connect over unreliable wireless channels, and may sit unattended in a field, factory floor, or public space for years.

These constraints do not reduce the security requirements; they make them harder to meet. A compromised web server can be patched and restarted in minutes. A compromised fleet of ten thousand sensors distributed across an industrial facility presents a recovery challenge of an entirely different magnitude. The goal of IoT security is to prevent that compromise in the first place, limit its blast radius when it does occur, and maintain the operational integrity of systems that humans and critical infrastructure may depend on.


Device Identity and Authentication

Every device in an IoT deployment needs a unique, verifiable identity. Without that, a security system cannot distinguish legitimate devices from imposters, revoke a single compromised device, or produce meaningful audit logs. Shared credentials across a device fleet mean that capturing the credential from one device exposes every other device using the same key. This is one of the most common and most damaging mistakes in IoT deployments.

X.509 Certificates

X.509 certificates represent the strongest authentication option available for IoT devices. A certificate binds a public key to a device identity and is signed by a certificate authority (CA) that the service trusts. When a device presents its certificate during a TLS handshake, the server can verify the signature chain back to a trusted root and confirm the device is who it claims to be.

The strength comes from public key cryptography. The device holds a private key that never leaves the hardware. The certificate, which contains only the public key, can be distributed freely. Even if an attacker intercepts the certificate in transit, they cannot use it to impersonate the device because they do not have the private key.

Certificate chains add another layer of structure. A root CA signs an intermediate CA, and the intermediate CA signs individual device certificates. This hierarchy means organizations can issue certificates from an intermediate CA without exposing the root, and can revoke an entire intermediate if it becomes compromised.

The challenges with X.509 are operational rather than cryptographic. Certificates expire. A device with an expired certificate cannot authenticate, which means certificate renewal must be designed into the system from the start. Devices need a mechanism to request new certificates, and the provisioning infrastructure needs to handle that at scale. Certificate revocation is another operational concern: if a device is compromised, the certificate must be revoked, and other services must check revocation status before trusting it.

SAS Tokens

Shared Access Signature (SAS) tokens are a simpler authentication mechanism used heavily in platforms like Azure IoT Hub. A SAS token is a signed string that grants access to a resource for a specific time window. The device generates the token using a symmetric key it shares with the service. When the service receives the token, it verifies the signature using its copy of the same key.

SAS tokens are easier to implement and work well on constrained devices that lack the processing power for public key operations. Time-limiting the tokens reduces the damage window if a token is intercepted; an attacker who captures a SAS token can only use it until it expires. Rotating the underlying key periodically limits the exposure further.

The weakness is that symmetric key authentication requires both sides to hold the same secret. If the service is breached and the keys are exposed, every device using those keys is compromised. For this reason, each device should have its own unique SAS key rather than sharing a key across a fleet. Individual key issuance means a compromised device can be isolated by revoking only its key without affecting others.

TPM (Trusted Platform Module)

A Trusted Platform Module is a dedicated hardware chip that provides cryptographic functions and secure key storage. The private keys stored in a TPM cannot be extracted by software, even if an attacker gains full control of the operating system. This hardware-backed isolation is the defining characteristic of TPM security.

Beyond key storage, TPMs support attestation: the ability to prove that a device is running expected, unmodified software. Remote attestation works by asking the TPM to produce a measurement of the system state, signed by a key that only the genuine TPM can use. A provisioning service can verify this measurement and refuse to issue credentials to a device running tampered firmware.

Tamper resistance is another property of TPMs. Physical attacks on the chip trigger protective mechanisms, making it difficult to extract secrets even with direct hardware access. This matters for devices deployed in locations where physical security cannot be guaranteed.

The tradeoff is cost and complexity. TPMs add cost to the device bill of materials, and integrating TPM-based attestation into a provisioning pipeline requires meaningful engineering investment. For high-value devices or deployments where firmware integrity is critical, that investment is justified.

Comparing Authentication Methods

Choosing between authentication methods depends on the device capabilities, security requirements, and operational context of the deployment.

Factor X.509 Certificates SAS Tokens TPM-backed Keys
Cryptographic strength High (asymmetric) Medium (symmetric) High (hardware-isolated asymmetric)
Hardware requirements Moderate (needs storage, some compute) Low (symmetric HMAC is cheap) Requires TPM chip
Key exposure risk Low (private key never leaves device) Medium (symmetric key on both sides) Very low (hardware prevents extraction)
Revocation Certificate revocation lists or OCSP Revoke key in service Revoke credential in service
Renewal complexity High (certificate lifecycle management) Low (generate new token from key) Medium (key rotation with attestation)
Physical tamper resistance None (key in flash storage) None (key in flash storage) High (TPM actively resists extraction)
Firmware attestation No No Yes
Best fit High-security, capable devices Constrained devices, simpler deployments Critical infrastructure, high-value assets

Never use shared credentials across devices regardless of which mechanism you choose. Each device gets its own identity and its own key material. This is not negotiable from a security standpoint.


Communication Security

Authentication establishes who a device is. Communication security determines whether the data a device sends can be read or tampered with by anyone other than the intended recipient. For IoT devices, this means encrypting data in transit and verifying that both endpoints are legitimate.

TLS and DTLS

Transport Layer Security (TLS) encrypts the connection between a device and a cloud service or gateway. It provides both confidentiality (data cannot be read by eavesdroppers) and integrity (data cannot be modified in transit without detection). Any IoT device with sufficient resources should use TLS for all communications.

Datagram TLS (DTLS) serves the same purpose for devices that communicate over UDP rather than TCP. Many IoT protocols, including CoAP, run over UDP because it has lower overhead and is better suited to lossy wireless networks. DTLS adapts TLS concepts to work with the connectionless nature of UDP, providing equivalent security guarantees without requiring a persistent TCP session.

TLS version matters, and older versions like TLS 1.0 and 1.1 have known vulnerabilities that make them unsuitable for production use. TLS 1.2 is widely supported and acceptable; TLS 1.3 is preferred where device libraries support it, as it reduces handshake round trips (which saves both time and battery).

Certificate Pinning

Certificate pinning is a technique where a device is configured to accept only a specific certificate or certificate authority, rather than trusting any certificate signed by a well-known root CA. This prevents man-in-the-middle attacks where an attacker presents a fraudulent but technically valid certificate to intercept traffic.

Without pinning, an attacker who controls the network between a device and the cloud could present a certificate from a CA that the device trusts, decrypt the traffic, re-encrypt it, and forward it to the real endpoint. The device sees a valid TLS connection and has no way to detect the interception. With pinning, the device compares the presented certificate against its pinned expectation and rejects anything that does not match.

The operational challenge is that pinned certificates expire. When the service rotates its certificate, devices must be updated to pin the new certificate before the old one expires. This creates a firmware update dependency that must be managed carefully. Pinning to a public key rather than a full certificate reduces this burden because the key can remain stable across certificate renewals.

Mutual TLS

Standard TLS authenticates only the server; the client verifies that it is talking to the right server, but the server does not verify the client’s identity through the TLS layer. Mutual TLS (mTLS) extends this by requiring both sides to present certificates. The server presents its certificate as normal, and the device also presents a certificate that the server verifies.

mTLS is particularly valuable in IoT because it binds transport-level authentication to the TLS handshake itself, rather than relying on application-layer authentication after the connection is established. An attacker who lacks a valid device certificate cannot even complete the TLS handshake with a service that requires mTLS, which reduces the attack surface significantly.

mTLS works naturally in conjunction with X.509 device certificates. Each device’s certificate serves both as its identity credential and as the authentication material for the TLS handshake.

DTLS with Pre-Shared Keys

For devices too constrained to perform certificate-based TLS operations, DTLS with Pre-Shared Keys (PSK) offers a middle ground. Instead of exchanging and verifying certificates during the handshake, both sides start with a shared secret that was provisioned at manufacture or deployment time. The handshake uses this secret to derive session keys, which are then used to encrypt the session.

PSK mode is significantly cheaper computationally because it eliminates the asymmetric cryptography operations required for certificate validation. A device that cannot afford the CPU cycles or battery drain of a full TLS handshake may still be able to use DTLS-PSK to encrypt its communications.

The security tradeoff is that pre-shared keys have the same weakness as any symmetric key: both sides must hold the secret, and compromising either side exposes it. PSK also lacks the mutual authentication properties of certificate-based mTLS. It should be considered a pragmatic choice for constrained devices, not a preferred approach.


Constrained Device Challenges

Many of the security problems unique to IoT stem from the physical characteristics of the devices themselves. Security assumptions that hold in data centers break down when applied to a battery-powered sensor with 256 KB of flash storage.

Limited Compute, Memory, and Storage

Standard security libraries designed for servers or desktop systems often cannot run on microcontrollers. A full TLS stack with certificate parsing, asymmetric cryptography, and hash operations can require megabytes of RAM that a constrained device simply does not have. Even when a compact TLS library exists that fits in available memory, the CPU time required for cryptographic operations may be prohibitive.

This limitation forces design choices. Some deployments use protocol gateways that terminate TLS at a nearby gateway device and forward data to the cloud over a more capable connection. The device-to-gateway link might use a simpler protocol with lighter encryption or rely on physical security of a short-range wireless link. The gateway-to-cloud link then uses full TLS with strong authentication. This approach concentrates the cryptographic complexity in a single capable device while allowing constrained devices to operate within their limits.

Battery-powered devices add another dimension. Cryptographic operations consume power. Establishing a TLS connection requires a handshake with multiple round trips, and each round trip involves computation on both sides. For a device running on two AA batteries that must last three years in the field, every milliamp-hour matters. Designers often need to evaluate whether session resumption (which skips the full handshake for reconnections), connection pooling, or lower-security alternatives are acceptable given the deployment’s risk profile.

Default Passwords and Hard-Coded Credentials

One of the most widespread and persistent problems in the IoT industry is manufacturers shipping devices with default credentials. A router configured at the factory with the username “admin” and password “admin” is not secure; it is a vulnerability waiting to be exploited. When devices with identical default credentials are deployed at scale, a single credential lookup gives an attacker access to every device on the network that was never reconfigured.

Hard-coded credentials are even worse. Unlike default passwords, which a user can theoretically change, hard-coded credentials are embedded in the firmware and cannot be changed without a firmware update. When researchers discover a hard-coded backdoor password, every device running that firmware version is permanently exposed until patched.

Correct practice is for each device to receive a unique credential during manufacturing or provisioning, derived from the device’s unique identity rather than shared across the product line. Devices should also enforce credential changes during initial setup, and modern platforms like Azure IoT Hub support per-device key management that makes unique credential provisioning operationally straightforward.

Supply Chain Risk

A device’s firmware typically contains components from multiple sources: the manufacturer’s own code, an RTOS from one vendor, a communication stack from another, cryptography libraries from a third. Each of these components is a potential source of vulnerabilities, and the manufacturer may not have visibility into all of them.

Supply chain attacks can introduce vulnerabilities at any of these layers. A compromised library distributed through a legitimate package repository, a vendor with poor security practices, or a targeted attack on a firmware signing key can all result in devices shipping with security flaws that were never present in the original design.

Mitigations include maintaining a software bill of materials (SBOM) for firmware, so that when a vulnerability is discovered in a component, affected devices can be identified quickly. Secure boot ensures that devices will only run firmware signed by a trusted key, making it harder to deploy malicious firmware. Code signing for firmware updates means that even if an attacker can push an update through an OTA channel, the device rejects it if the signature is invalid.

Physical Access Threats

Devices deployed in uncontrolled environments are physically accessible to adversaries. An attacker with physical access can attempt to extract credentials from flash storage using JTAG debugging interfaces left enabled in production firmware, read memory contents through side-channel analysis, or extract the device entirely and perform analysis in a lab environment.

JTAG and serial debug interfaces, which are essential during development, should be disabled or removed before production deployment. Flash encryption, available on many modern microcontrollers, ensures that even if an attacker reads the raw flash contents, they cannot interpret the data without the encryption key. Hardware security elements, separate from the main processor, can store cryptographic keys in a way that resists physical extraction.

Physical tamper detection, where a device detects that it is being opened or probed and deletes sensitive key material, provides another layer of defense. This is common in point-of-sale terminals and payment hardware, and the same concept applies wherever IoT devices store sensitive credentials in accessible locations.


Network Security

Securing individual devices is necessary but not sufficient. The network environment in which devices operate shapes their overall security posture and limits the damage a compromised device can cause.

Network Segmentation

Placing IoT devices on the same network as corporate workstations, file servers, and databases creates unnecessary risk. If a compromised IoT device can freely communicate with a file server, an attacker who controls the device can use it as a pivot point to attack the rest of the network. Network segmentation addresses this by isolating IoT devices into their own network segment with controlled communication paths to everything else.

A common pattern is to place IoT devices on a dedicated VLAN or subnet, with firewall rules that permit only the specific communication flows those devices need. Outbound connections to the cloud endpoint on specific ports are permitted; all other outbound traffic is blocked. Inbound connections from IoT devices to the corporate LAN are blocked entirely. This containment means that a compromised device cannot reach internal resources, even if the attacker is actively trying to use it as a foothold.

In industrial environments, the Purdue Model provides a well-established framework for network segmentation. IoT and operational technology (OT) devices sit at lower levels of the model, separated from IT systems by demilitarized zones (DMZ) with strict controls on what traffic can pass between levels.

Firewall Rules for IoT Traffic

IoT devices typically have predictable communication patterns. A temperature sensor sends data to one endpoint on one port at regular intervals. It does not need to initiate connections to arbitrary internet addresses, communicate with other devices on the local network, or accept incoming connections from anywhere. Firewall rules should reflect this predictability, permitting only the specific flows the device requires and blocking everything else.

Allowlisting destination endpoints, rather than just blocking known-bad destinations, is a stronger approach. An allowlist means that a compromised device trying to communicate with an attacker’s command-and-control server will be blocked, because that server is not on the approved list. A blocklist approach, by contrast, cannot keep up with the constantly changing infrastructure attackers use.

Data Encryption at Rest

Data generated by IoT devices eventually lands in cloud storage, databases, or data lakes. Encrypting this data at rest means that a breach of the storage system does not automatically expose the device data. Cloud platforms provide built-in encryption at rest for most storage services, often with options for customer-managed keys.

The sensitivity of the data should drive the encryption strategy. Telemetry from an asset-tracking device may reveal the location and movement patterns of high-value equipment. Health data from a medical device is subject to regulatory requirements. Understanding what the data represents and what its exposure would mean is a prerequisite to designing appropriate controls.

Principle of Least Privilege

Every device should have exactly the permissions it needs to function and no more. A device that reads sensor data and sends it to a cloud endpoint does not need permission to delete messages, modify device registry entries, or invoke management APIs. Granting only the minimum necessary permissions limits the damage a compromised device can cause.

Cloud IoT platforms support this through role-based access control and fine-grained permission models. Azure IoT Hub, for example, allows a device to be granted only “Device Connect” permission, which lets it send telemetry and receive commands but prevents it from accessing any other hub resources. Designing permissions this way is straightforward when done at deployment time and significantly harder to retrofit once a fleet is in production.


Security Monitoring and Response

Prevention controls reduce the likelihood of a compromise, but no prevention strategy is perfect. Detection and response capabilities determine how quickly an organization can identify that something is wrong and contain the damage.

Anomaly Detection on Device Behavior

IoT devices are predictable by design. A manufacturing sensor reports temperature readings every thirty seconds. A connected meter uploads daily usage data at midnight. Deviations from expected behavior are a signal worth investigating. An anomaly detection system that understands normal patterns can flag behavior that falls outside them, such as a sensor that suddenly starts sending data at high frequency, attempts to connect to an unexpected endpoint, or goes silent unexpectedly.

Effective anomaly detection requires a baseline. Without knowing what normal looks like for a device or device class, it is impossible to recognize abnormal. Building this baseline requires observing devices in normal operation, understanding their communication patterns, and encoding those patterns as rules or training an ML model to recognize them.

Behavioral anomalies to watch for include unexpected protocol usage, communication with IP addresses not in the allowlist, unusual data volumes either unusually high or unusually low, connections at unexpected times, and changes in message frequency or size that do not correlate with known operational events.

Security Audit Logging

Security-relevant events in the device lifecycle should be logged and retained. These events include device provisioning and deprovisioning, authentication successes and failures, certificate or key changes, firmware updates, configuration changes, and connection events including disconnections that might indicate a network disruption or device failure.

These logs serve multiple purposes. During incident response, they provide a timeline of events that helps reconstruct what happened and when. During forensic analysis, they can establish whether a device was behaving abnormally before an incident was detected. Over time, patterns in authentication failures or connection anomalies can surface devices that are under attack or malfunctioning.

Log retention should be long enough to be useful for post-incident analysis. The right retention period depends on regulatory requirements and the operational tempo of the deployment, but six months to a year is a common minimum for security audit logs.

Incident Response for Compromised Devices

When a device is suspected to be compromised, the response options differ from those available in a traditional IT environment. You cannot simply walk over to the device and pull the ethernet cable. In an IoT deployment, the correct response mechanisms must be designed into the system before an incident occurs.

Remote quarantine means isolating the device from the network without physically touching it. For devices connected through a managed IoT platform, this typically means revoking the device’s credentials so it can no longer authenticate, and pushing a policy change to the network that blocks communication from the device’s identifier. The device may continue operating locally, but it cannot exfiltrate data or receive commands.

If remote quarantine is not sufficient, a remote shutdown command, where the device supports it, can disable operation until physical intervention is possible. For devices that do not support remote management, the response may require physically retrieving the device.

After quarantine, the investigation should determine the scope of the compromise. Were other devices affected? Was any data exfiltrated? What vulnerability was exploited? The firmware of affected devices should be forensically analyzed if possible before being wiped and reprovisioned.

Azure Defender for IoT

Azure Defender for IoT is an agentless security monitoring solution designed for IoT and Operational Technology (OT) networks. Unlike agent-based security tools that require software installation on each device, Defender for IoT works by passively analyzing network traffic using sensors placed on the network.

Because many OT devices run proprietary operating systems that cannot host agents, the agentless approach is essential in industrial environments. The sensor learns what devices are on the network, what protocols they use, and what their normal communication patterns look like. It then alerts on deviations, known attack patterns, and protocol anomalies.

Defender for IoT supports integration with Microsoft Sentinel for SIEM-based analysis, and with Azure IoT Hub for environments that use managed device connectivity. For organizations operating mixed IT/OT environments, it provides unified visibility across domains that are often managed by separate teams with limited collaboration.


Common IoT Attack Vectors

Understanding how attacks unfold in practice helps architects design controls that address real threats rather than theoretical ones.

Firmware Extraction and Reverse Engineering

Attackers who want to understand a device’s internals often start by extracting the firmware. If flash storage is not encrypted, connecting to a debugging port or desoldering the flash chip and reading it directly yields a copy of the firmware binary. Reverse engineering that binary can reveal hard-coded credentials, API keys, cryptographic keys, authentication logic, or proprietary communication protocols.

Once an attacker understands the firmware, they can modify it to create a malicious variant, host it as a fake firmware update, and distribute it to devices that lack signature verification on updates. The modified firmware might add a backdoor while maintaining all normal device functionality, making detection difficult.

Defenses include encrypted flash storage, disabled debug interfaces on production hardware, secure boot with signature verification, and firmware update mechanisms that validate signatures before applying updates.

Man-in-the-Middle on Unencrypted Protocols

Some IoT devices use older or simpler protocols that transmit data in plaintext. An attacker on the same network, or controlling a wireless access point the device connects to, can intercept all traffic. Beyond passive eavesdropping, the attacker can modify data in transit, inject commands, or replay captured messages.

This attack is straightforward to execute and straightforward to prevent. Using TLS or DTLS for all communications eliminates passive eavesdropping and makes tampering detectable. Certificate pinning or mTLS prevents interception even when the attacker can present a valid certificate from a trusted CA.

Replay Attacks on Telemetry Data

A replay attack captures a legitimate message sent by a device and retransmits it later. For telemetry data, this might mean replaying a “system normal” status message to mask a real fault condition. For command-and-control channels, replaying a legitimate command can trigger unintended actions.

TLS prevents replay attacks within a session because session keys are unique to each connection. However, if an attacker can replay messages at the application layer (for example, if message authentication does not include timestamps or sequence numbers), replays can succeed even over encrypted channels.

Defenses include message timestamps with short validity windows (reject any message with a timestamp more than a few minutes old), monotonic sequence numbers (reject any message whose sequence number is not higher than the last received), and nonce-based authentication where each message includes a unique value that the service verifies has not been seen before.

Physical Tampering and Side-Channel Attacks

An attacker with physical access to a device has options that purely network-based attackers do not. Side-channel attacks analyze power consumption, electromagnetic emissions, or timing variations in cryptographic operations to extract key material without directly reading memory. These attacks require specialized equipment and expertise but are practical against devices that implement cryptography in software without countermeasures.

Physical tampering can install hardware implants, replace firmware, intercept internal buses, or modify sensor outputs. A tampered device might report falsified readings while appearing to operate normally.

Hardware security modules and TPMs provide countermeasures by performing cryptographic operations in hardware with built-in side-channel protections. Physical security measures like tamper-evident enclosures, resin potting of circuit boards, and tamper-detection switches that trigger key deletion provide additional defense layers. For high-security deployments, regular physical inspection of deployed devices is also a necessary control.

Botnet Recruitment

Mirai-style attacks demonstrated the scale of damage that can result from compromised IoT devices. In 2016, the Mirai botnet infected hundreds of thousands of devices by scanning the internet for IoT devices with default credentials, logging in using a list of common username-and-password combinations, and enrolling them as botnet agents. The resulting DDoS attacks reached 1.2 terabits per second (Source: Cloudflare, Mirai Botnet), overwhelming some of the largest internet infrastructure providers.

Botnet recruitment exploits weak authentication, unpatched vulnerabilities, and devices with internet-accessible management interfaces. The device owner often has no awareness that their devices have been compromised; the device continues normal operation while also participating in attacks against third parties.

Prevention focuses on eliminating the conditions that allow initial access: unique per-device credentials, no default passwords, network segmentation that prevents devices from initiating arbitrary internet connections, and timely firmware updates to address known vulnerabilities. Devices that are not designed to be internet-accessible should not be exposed to the internet, even if doing so seems convenient for management purposes.

DDoS Using Compromised IoT Fleets

Once an attacker controls a large fleet of IoT devices, those devices can be directed to generate traffic toward a target. Individual devices may have limited bandwidth, but a fleet of one hundred thousand devices, each generating ten megabits per second of traffic, produces terabit-scale attacks that can overwhelm even well-provisioned targets.

The traffic generated by an IoT DDoS fleet is difficult to filter because it originates from legitimate IP addresses spread across many geographic locations and internet service providers. Traditional source-blocking strategies are ineffective at this scale.

Organizations operating large device fleets share responsibility for not contributing to DDoS attacks. A manufacturer whose devices are recruited into a botnet may face regulatory scrutiny, reputational damage, and potentially liability in some jurisdictions. Security controls that prevent initial compromise protect not just the device owner but also the broader internet ecosystem.


Building Security Into the Device Lifecycle

Security in IoT is not a feature added at the end of development; it must be integrated throughout the device lifecycle from design through decommissioning.

During design, threat modeling identifies the attack surfaces specific to the device and deployment context. What data does the device handle, and how sensitive is it? Where will devices be physically located? Who might want to attack them and why? What would a successful attack enable? Answers to these questions drive architecture decisions about authentication mechanisms, encryption requirements, and physical security measures.

During manufacturing, the secure provisioning of device credentials ensures that each device enters the field with a unique identity and no shared secrets. This requires investment in a secure manufacturing process that injects keys in a controlled environment. Attempting to retrofit unique credentials after manufacturing is expensive and error-prone.

During deployment, secure configuration means disabling unnecessary features, changing any remaining default settings, placing devices in appropriate network segments, and verifying that firmware is current before deployment.

During operation, ongoing security means receiving firmware updates from the manufacturer and applying them, monitoring device behavior for anomalies, rotating credentials on a defined schedule, and maintaining visibility into what devices exist on the network and what software version each is running.

During decommissioning, devices must have their stored credentials and sensitive data wiped before disposal or redeployment. A device discarded with its credentials intact is a source of leaked secrets. Hardware security modules that support key deletion make this straightforward; devices without this capability may need to be physically destroyed if they stored high-sensitivity credentials.

Security is the cumulative result of decisions made at each stage. Neglecting any stage creates gaps that can be exploited long after the initial oversight. Organizations that treat IoT security as a continuous operational discipline, rather than a one-time checklist, build deployments that remain defensible as threats evolve.

Found this guide helpful? Share it with your team:

Share on LinkedIn